From 089d6219a1430b1dc58f9567fe370cef6fcdda90 Mon Sep 17 00:00:00 2001 From: krahets Date: Thu, 15 Jun 2023 01:59:09 +0800 Subject: [PATCH] deploy --- .DS_Store | Bin 12292 -> 12292 bytes .../hash_collision_chaining.png | Bin 86607 -> 0 bytes .../hash_collision_linear_probing.png | Bin 58163 -> 0 bytes .../hash_table_chaining.png | Bin 0 -> 91610 bytes .../hash_table_linear_probing.png | Bin 0 -> 68495 bytes chapter_hashing/hash_collision/index.html | 17 +- .../hash_map.assets/hash_collision.png | Bin 81300 -> 91972 bytes .../hash_map.assets/hash_function.png | Bin 90233 -> 100007 bytes chapter_hashing/hash_map.assets/hash_map.png | Bin 66528 -> 0 bytes .../hash_map.assets/hash_table_lookup.png | Bin 0 -> 61522 bytes chapter_hashing/hash_map/index.html | 44 ++--- search/search_index.json | 2 +- sitemap.xml | 152 +++++++++--------- sitemap.xml.gz | Bin 763 -> 763 bytes 14 files changed, 102 insertions(+), 113 deletions(-) delete mode 100644 chapter_hashing/hash_collision.assets/hash_collision_chaining.png delete mode 100644 chapter_hashing/hash_collision.assets/hash_collision_linear_probing.png create mode 100644 chapter_hashing/hash_collision.assets/hash_table_chaining.png create mode 100644 chapter_hashing/hash_collision.assets/hash_table_linear_probing.png delete mode 100644 chapter_hashing/hash_map.assets/hash_map.png create mode 100644 chapter_hashing/hash_map.assets/hash_table_lookup.png diff --git a/.DS_Store b/.DS_Store index 48e8be2d8e7d8963ef53a1accc6ca2dccfec4427..886ed5efbbff7ba96e863b309feaccfeb1dde8bd 100644 GIT binary patch delta 785 zcmZokXi3jE;3*rN>~${B^!C%+TqLE@V-U0|4;C!{txPKcLT@mMX8ZN&aU=?>WRhd|}$ zgm{qnCd^7e(;I}9CR+*fGG~QL0NE;&^MtwB&5v+=1i7eQSRaY4JoyGtU9JGbKH&;7&=JT{n>Y@=!*kdtm0oSdIqzyJb5ALf7rg>&;= zT#|C~lYl}TfmddOZM0T3oUEbB!;awFF$)(kOy*OQ+dNBkHY2k>U(RG6br}(4%^Sa+ mhByHtkpedrSwf&71IY~r1q>4>=xqL=?!rnZyC#277XbifmKOW~ delta 795 zcmZokXi3*ki-pL+< zyv%9>43ql=)h5>o@-c0f1u~7;`?mz%`T69?WJV$N$?pVtkocxd2N)*j38_tv6XInS z5M2jk8?i@Cjsz=z2vmMfhzE&p!YnX>VRD18(qt=PUS~t8a-J|3d&SoJH;11d znOrZdkHl7cZ@F82Ou{a|bIMeLE-#*<%&@G&w> z{w|`$Y`gWuXY$Wfa-PmzlqDlmDyq7gF-(2E92Hj@>Uc$vjLVu5TA_BAK@!TzgP z@`SU`D|s=CF8~IIvdiQIWnN~vTLwUO00+C3J;)7?K#tpFJ{2x@`3bsU&GITi%<=&Y zlWkND1ai_1gOl@f3xKiCz`(hGIY>}AH{S)G6e1t!fi+pH8cxGBp4I diff --git a/chapter_hashing/hash_collision.assets/hash_collision_chaining.png b/chapter_hashing/hash_collision.assets/hash_collision_chaining.png deleted file mode 100644 index 6653bfa1251dbe52a5c67787fb8259d889567499..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 86607 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 diff --git a/chapter_hashing/hash_collision.assets/hash_table_chaining.png b/chapter_hashing/hash_collision.assets/hash_table_chaining.png new file mode 100644 index 0000000000000000000000000000000000000000..4b0cdf46eebfd76b0f57e518e9ba027d5d795378 GIT binary patch literal 91610 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 sskfxYi41aNwWpQw7N)al<$N2e0u#@&Et; literal 0 HcmV?d00001 diff --git a/chapter_hashing/hash_collision.assets/hash_table_linear_probing.png b/chapter_hashing/hash_collision.assets/hash_table_linear_probing.png new file mode 100644 index 0000000000000000000000000000000000000000..80f78dec1a8ae7cb232afb056a28df470ec25f0b GIT binary patch literal 68495 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%0Ah6Ff$c`Pj=MxT>wvD+SZZfuMOYw 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 literal 0 HcmV?d00001 diff --git a/chapter_hashing/hash_collision/index.html b/chapter_hashing/hash_collision/index.html index 1c5ed031d..928e9081d 100644 --- a/chapter_hashing/hash_collision/index.html +++ b/chapter_hashing/hash_collision/index.html @@ -2140,18 +2140,17 @@

在理想情况下,哈希函数为每个输入生成唯一的输出,实现 key 和数组索引的一一对应。但实际上,哈希函数的输入空间通常远大于输出空间,因此多个输入产生相同输出的情况是不可避免的。例如,输入空间为全体整数,输出空间为数组容量大小,则必然有多个整数映射至同一数组索引。

-

这种多个输入对应同一输出索引的现象被称为「哈希冲突 Hash Collision」。哈希冲突会导致查询结果错误,严重影响哈希表的可用性。哈希冲突的解决方法主要有两种:

+

这种多个输入对应同一输出索引的现象被称为「哈希冲突 Hash Collision」。哈希冲突会导致查询结果错误,严重影响哈希表的可用性。哈希冲突的处理方法主要有两种:

  • 扩大哈希表容量:哈希表容量越大,键值对聚集的概率就越低。极端情况下,当输入空间和输出空间大小相等时,哈希表等同于数组,每个 key 都对应唯一的数组索引。
  • -
  • 优化哈希表结构:常用方法包括链式地址和开放寻址。
  • +
  • 优化哈希表结构:常用方法包括链式地址和开放寻址。这类方法的思路是通过改良数据结构,使得哈希表可以在发生哈希冲突时仍然可以正常工作。当然,这些优化往往是以牺牲时间效率为代价的。

6.2.1.   哈希表扩容

-

哈希函数的最后一步通常是对桶数量 \(n\) 取余,作用是将哈希值映射到桶索引范围,从而将 key 放入对应的桶中。当哈希表容量越大(即 \(n\) 越大)时,多个 key 被分配到同一个桶中的概率就越低,冲突就越少。

-

因此,当哈希表内的冲突总体较为严重时,编程语言通常通过扩容哈希表来缓解冲突。类似于数组扩容,哈希表扩容需将所有键值对从原哈希表迁移至新哈希表,开销较大。

+

哈希函数的最后一步通常是对桶数量 \(n\) 取余,作用是将哈希值映射到桶索引范围,从而将 key 放入对应的桶中。当哈希表容量越大(即 \(n\) 越大)时,多个 key 被分配到同一个桶中的概率就越低,冲突就越少。因此,当哈希表内的冲突总体较为严重时,编程语言通常通过扩容哈希表来缓解冲突。类似于数组扩容,哈希表扩容需将所有键值对从原哈希表迁移至新哈希表,开销较大。

编程语言通常使用「负载因子 Load Factor」来衡量哈希冲突的严重程度,定义为哈希表中元素数量除以桶数量,常作为哈希表扩容的触发条件。在 Java 中,当负载因子超过 \(0.75\) 时,系统会将 HashMap 容量扩展为原先的 \(2\) 倍。

6.2.2.   链式地址

在原始哈希表中,每个桶仅能存储一个键值对。「链式地址 Separate Chaining」将单个元素转换为链表,将键值对作为链表节点,将所有发生冲突的键值对都存储在同一链表中。

-

链式地址哈希表

+

链式地址哈希表

Fig. 链式地址哈希表

链式地址下,哈希表的操作方法包括:

@@ -2160,7 +2159,7 @@
  • 添加元素:先通过哈希函数访问链表头节点,然后将节点(即键值对)添加到链表中。
  • 删除元素:根据哈希函数的结果访问链表头部,接着遍历链表以查找目标节点,并将其删除。
  • -

    尽管链式地址法解决了哈希冲突问题,但仍存在一些局限性,包括:

    +

    该方法存在一些局限性,包括:

    -

    然而,在哈希表中进行增删查的时间复杂度都是 \(O(1)\) 。哈希表全面胜出!因此,哈希表常用于对查找效率要求较高的场景。

    @@ -2143,6 +2142,7 @@
    +

    观察发现,在哈希表中进行增删查改的时间复杂度都是 \(O(1)\) ,非常高效。因此,哈希表常用于对查找效率要求较高的场景。

    6.1.1.   哈希表常用操作

    哈希表的基本操作包括 初始化、查询操作、添加与删除键值对

    @@ -2508,23 +2508,19 @@
    -

    6.1.2.   哈希函数

    -

    哈希表的底层实现为数组,同时可能包含链表、二叉树(红黑树)等数据结构,以提高查询性能(将在下节讨论)。

    -

    首先考虑最简单的情况,仅使用一个数组来实现哈希表。通常,我们将数组中的每个空位称为「桶 Bucket」,用于存储键值对。

    -

    我们将键值对 key, value 封装成一个类 Pair ,并将所有 Pair 放入数组中。这样,数组中的每个 Pair 都具有唯一的索引。为了建立 key 和索引之间的映射关系,我们需要使用「哈希函数 Hash Function」。

    -

    设哈希表的数组为 buckets ,哈希函数为 f(x) ,那么查询操作的步骤如下:

    -
      -
    1. 输入 key ,通过哈希函数计算出索引 index ,即 index = f(key)
    2. -
    3. 通过索引在数组中访问到键值对 pair ,即 pair = buckets[index] ,然后从 pair 中获取对应的 value
    4. -
    -

    以学生数据 key 学号 -> value 姓名 为例,我们可以设计如下哈希函数:

    +

    6.1.2.   哈希表简单实现

    +

    我们先考虑最简单的情况,仅用一个数组来实现哈希表。在哈希表中,我们通常将数组中的每个空位称为「桶 Bucket」,每个桶可存储一个键值对。因此,查询操作就是定位输入的 key 对应的桶,从而得到 value

    +

    那么,如何基于 key 来定位对应的桶呢?这是通过「哈希函数 Hash Function」实现的。哈希函数的作用是将一个较大的输入空间映射到一个较小的输出空间。在哈希表中,输入空间是所有 key ,输出空间是所有桶(数组索引)。换句话说,输入一个 key ,我们可以通过哈希函数得到该 key 对应的键值对存储在数组中的位置

    +

    哈希函数的计算过程分为两步:输入一个 key ,首先通过函数 hash(key) 计算得到哈希值,接下来将哈希值对桶数量(数组长度)取模,从而获取该 key 对应的数组索引 index 。计算公式如下

    \[ -f(x) = x \bmod {100} +index = \text{hash}(key) \bmod {c} \]
    -

    其中 \(\bmod\) 表示取余运算。

    +

    其中, \(\bmod\) 表示取余运算, \(c\) 为桶数量(数组长度)。随后,我们就可以利用 index 在哈希表中访问对应的桶,从而获取 value

    +

    设数组长度 \(c = 100\) , \(\text{hash}(key) = key\) ,易得哈希函数为 \(key \bmod 100\) 。下图以 key 学号和 value 姓名为例,展示了哈希函数的工作原理。

    哈希函数工作原理

    Fig. 哈希函数工作原理

    +

    以下代码给出了一个简单哈希表实现。其中,我们将 keyvalue 封装成一个类 Pair ,以表示键值对。

    @@ -3455,21 +3451,15 @@ f(x) = x \bmod {100}

    6.1.3.   哈希冲突

    -

    细心的你可能已经注意到,在某些情况下,哈希函数 \(f(x) = x \bmod 100\) 可能无法正常工作。具体来说,当输入的 key 后两位相同时,哈希函数的计算结果也会相同,从而指向同一个 value 。例如,查询学号为 \(12836\)\(20336\) 的两个学生时,我们得到:

    +

    本质上看,哈希函数的是将一个庞大的输入空间(key 范围)映射到一个较小的输出空间(数组索引范围)。因此,理论上一定存在”多个输入对应相同输出”的情况

    +

    对于上述示例中的哈希函数,当输入的 key 后两位相同时,哈希函数的输出结果也相同。例如,查询学号为 \(12836\)\(20336\) 的两个学生时,我们得到:

    \[ -f(12836) = f(20336) = 36 +12836 \bmod 100 = 20336 \bmod 100 = 36 \]
    -

    这两个学号指向了同一个姓名,这显然是错误的。我们把这种情况称为「哈希冲突 Hash Collision」。在后续章节中,我们将讨论如何解决哈希冲突的问题。

    +

    两个学号指向了同一个姓名,这显然是不对的。我们把这种情况称为“哈希冲突”。在下节中,我们将重点讨论如何解决冲突问题。

    哈希冲突示例

    Fig. 哈希冲突示例

    -

    综上所述,一个优秀的哈希函数应具备以下特性:

    -
      -
    • 尽可能减少哈希冲突的发生;
    • -
    • 查询效率高且稳定,能够在绝大多数情况下达到 \(O(1)\) 时间复杂度;
    • -
    • 较高的空间利用率,即使“键值对占用空间 / 哈希表总占用空间”比例最大化;
    • -
    - diff --git a/search/search_index.json b/search/search_index.json index 747212c59..e9bfea1cd 100644 --- a/search/search_index.json +++ b/search/search_index.json @@ -1 +1 @@ -{"config":{"lang":["ja"],"separator":"[\\s\\-\uff0c\u3002]+","pipeline":["stemmer"]},"docs":[{"location":"","title":"Home","text":"

    \u300a Hello \u7b97\u6cd5 \u300b

    \u52a8\u753b\u56fe\u89e3\u3001\u80fd\u8fd0\u884c\u3001\u53ef\u63d0\u95ee\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u5feb\u901f\u5165\u95e8\u6559\u7a0b

    @Krahets

    \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

    \u300c\u5168\u4e66\u52a8\u753b\u56fe\u89e3\u300d

    \u5185\u5bb9\u6e05\u6670\u6613\u61c2\u3001\u5b66\u4e60\u66f2\u7ebf\u5e73\u6ed1\u7535\u8111\u3001\u5e73\u677f\u3001\u624b\u673a\u5168\u7ec8\u7aef\u9605\u8bfb

    \"A picture is worth a thousand words.\"

    \u201c\u4e00\u56fe\u80dc\u5343\u8a00\u201d

    \u300c\u4ee3\u7801\u4e00\u952e\u8fd0\u884c\u300d

    \u63d0\u4f9b\u5404\u4e2a\u7b97\u6cd5\u4e0e\u6570\u636e\u7ed3\u6784\u7684\u7b80\u6d01\u5b9e\u73b0\u4e0e\u6d4b\u8bd5\u6837\u4f8b\uff0c\u7686\u53ef\u76f4\u63a5\u8fd0\u884c\u652f\u6301 Java, C++, Python, Go, JS, TS, C#, Swift, Zig \u7b49\u8bed\u8a00

    \"Talk is cheap. Show me the code.\"

    \u201c\u5c11\u5439\u725b\uff0c\u770b\u4ee3\u7801\u201d

    \u300c\u53ef\u8ba8\u8bba\u4e0e\u63d0\u95ee\u300d

    \u9f13\u52b1\u5c0f\u4f19\u4f34\u4eec\u4e92\u5e2e\u4e92\u52a9\u3001\u5171\u540c\u6210\u957f\u63d0\u95ee\u4e0e\u8bc4\u8bba\u4e00\u822c\u80fd\u5728\u4e24\u65e5\u5185\u5f97\u5230\u56de\u590d

    \u201c\u8ffd\u98ce\u8d76\u6708\u83ab\u505c\u7559\uff0c\u5e73\u829c\u5c3d\u5904\u662f\u6625\u5c71\u201d

    \u4e00\u8d77\u52a0\u6cb9\uff01

    \u5e8f

    \u4e24\u5e74\u524d\uff0c\u6211\u5728\u529b\u6263\u4e0a\u5206\u4eab\u4e86\u300a\u5251\u6307 Offer\u300b\u7cfb\u5217\u9898\u89e3\uff0c\u53d7\u5230\u4e86\u8bb8\u591a\u670b\u53cb\u7684\u559c\u7231\u4e0e\u652f\u6301\u3002\u5728\u6b64\u671f\u95f4\uff0c\u6211\u56de\u7b54\u4e86\u4f17\u591a\u8bfb\u8005\u7684\u8bc4\u8bba\u95ee\u9898\uff0c\u5176\u4e2d\u6700\u5e38\u89c1\u7684\u4e00\u4e2a\u95ee\u9898\u662f\u201c\u5982\u4f55\u5165\u95e8\u5b66\u4e60\u7b97\u6cd5\u201d\u3002\u6211\u9010\u6e10\u4e5f\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\uff0c\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\u5730\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\u4e66\u7c4d\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\u5360\u636e\u4e86\u5927\u90e8\u5206\u7cbe\u529b\uff0c\u539a\u91cd\u7684\u4e66\u7c4d\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\u6b64\u95ee\u9898\u7684\u7ed9\u51fa\u7684\u7b54\u6848\uff0c\u867d\u7136\u4e0d\u4e00\u5b9a\u6b63\u786e\uff0c\u4f46\u81f3\u5c11\u662f\u4e00\u6b21\u79ef\u6781\u7684\u5c1d\u8bd5\u3002\u8fd9\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\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\u5e94\u5bf9\u5237\u9898\u548c\u9605\u8bfb\u6587\u732e\uff0c\u9010\u6b65\u6784\u5efa\u8d77\u5b8c\u6574\u7684\u77e5\u8bc6\u4f53\u7cfb\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\u5176 LeetBook\u300a\u56fe\u89e3\u7b97\u6cd5\u6570\u636e\u7ed3\u6784\u300b\u5df2\u88ab\u8ba2\u9605 24 \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\u6210\u957f\u3002\u611f\u8c22\u6bcf\u4e00\u4f4d\u6295\u5165\u65f6\u95f4\u4e0e\u7cbe\u529b\u7684\u64b0\u7a3f\u4eba\uff0c\u662f\u4ed6\u4eec\u65e0\u79c1\u5949\u732e\u4f7f\u8fd9\u672c\u4e66\u8d8a\u53d8\u8d8a\u597d\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, gvenusleo, justin\u2010tse, krahets, nuomi1, Reanon, 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 justin-tseJS / TS krahetsJava / Python nuomi1Swift ReanonGo / C sjinzhRust / Zig"},{"location":"chapter_appendix/contribution/","title":"13.2. \u00a0 \u4e00\u8d77\u53c2\u4e0e\u521b\u4f5c","text":"

    \u5f00\u6e90\u7684\u9b45\u529b

    \u7eb8\u8d28\u4e66\u7c4d\u7684\u4e24\u6b21\u5370\u5237\u7684\u95f4\u9694\u65f6\u95f4\u5f80\u5f80\u9700\u8981\u6570\u5e74\uff0c\u5185\u5bb9\u66f4\u65b0\u975e\u5e38\u4e0d\u65b9\u4fbf\u3002\u4f46\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

    \u7531\u4e8e\u4f5c\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\u5931\u6548\u94fe\u63a5\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\u5e2e\u52a9\u5176\u4ed6\u8bfb\u8005\u83b7\u5f97\u66f4\u4f18\u8d28\u7684\u5b66\u4e60\u8d44\u6e90\u3002\u6240\u6709\u64b0\u7a3f\u4eba\u5c06\u5728\u4ed3\u5e93\u548c\u7f51\u7ad9\u4e3b\u9875\u4e0a\u5c55\u793a\uff0c\u4ee5\u611f\u8c22\u4ed6\u4eec\u5bf9\u5f00\u6e90\u793e\u533a\u7684\u65e0\u79c1\u5949\u732e\uff01

    "},{"location":"chapter_appendix/contribution/#1321","title":"13.2.1. \u00a0 \u5185\u5bb9\u5fae\u8c03","text":"

    \u5728\u6bcf\u4e2a\u9875\u9762\u7684\u53f3\u4e0a\u89d2\u6709\u4e00\u4e2a\u300c\u7f16\u8f91\u300d\u56fe\u6807\uff0c\u60a8\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u4fee\u6539\u6587\u672c\u6216\u4ee3\u7801\uff1a

    1. \u70b9\u51fb\u7f16\u8f91\u6309\u94ae\uff0c\u5982\u679c\u9047\u5230\u201c\u9700\u8981 Fork \u6b64\u4ed3\u5e93\u201d\u7684\u63d0\u793a\uff0c\u8bf7\u540c\u610f\u8be5\u64cd\u4f5c\uff1b
    2. \u4fee\u6539 Markdown \u6e90\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u786e\u4fdd\u5185\u5bb9\u6b63\u786e\uff0c\u540c\u65f6\u5c3d\u91cf\u4fdd\u6301\u6392\u7248\u683c\u5f0f\u7684\u7edf\u4e00\uff1b
    3. \u5728\u9875\u9762\u5e95\u90e8\u586b\u5199\u4fee\u6539\u8bf4\u660e\uff0c\u7136\u540e\u70b9\u51fb\u201cPropose file change\u201d\u6309\u94ae\uff1b\u9875\u9762\u8df3\u8f6c\u540e\uff0c\u70b9\u51fb\u201cCreate pull request\u201d\u6309\u94ae\u5373\u53ef\u53d1\u8d77\u62c9\u53d6\u8bf7\u6c42\u3002

    Fig. \u9875\u9762\u7f16\u8f91\u6309\u952e

    \u7531\u4e8e\u56fe\u7247\u65e0\u6cd5\u76f4\u63a5\u4fee\u6539\uff0c\u56e0\u6b64\u9700\u8981\u901a\u8fc7\u65b0\u5efa Issue \u6216\u8bc4\u8bba\u7559\u8a00\u6765\u63cf\u8ff0\u56fe\u7247\u95ee\u9898\uff0c\u6211\u4eec\u4f1a\u5c3d\u5feb\u91cd\u65b0\u7ed8\u5236\u5e76\u66ff\u6362\u56fe\u7247\u3002

    "},{"location":"chapter_appendix/contribution/#1322","title":"13.2.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 Pull Request \u5de5\u4f5c\u6d41\u7a0b\uff1a

    1. \u767b\u5f55 GitHub \uff0c\u5c06\u672c\u4ed3\u5e93 Fork \u5230\u4e2a\u4eba\u8d26\u53f7\u4e0b\uff1b
    2. \u8fdb\u5165\u60a8\u7684 Fork \u4ed3\u5e93\u7f51\u9875\uff0c\u4f7f\u7528 git clone \u547d\u4ee4\u5c06\u4ed3\u5e93\u514b\u9686\u81f3\u672c\u5730\uff1b
    3. \u5728\u672c\u5730\u8fdb\u884c\u5185\u5bb9\u521b\u4f5c\uff0c\u5e76\u901a\u8fc7\u8fd0\u884c\u6d4b\u8bd5\u4ee5\u9a8c\u8bc1\u4ee3\u7801\u7684\u6b63\u786e\u6027\uff1b
    4. \u5c06\u672c\u5730\u6240\u505a\u66f4\u6539 Commit \uff0c\u7136\u540e Push \u81f3\u8fdc\u7a0b\u4ed3\u5e93\uff1b
    5. \u5237\u65b0\u4ed3\u5e93\u7f51\u9875\uff0c\u70b9\u51fb\u201cCreate pull request\u201d\u6309\u94ae\u5373\u53ef\u53d1\u8d77\u62c9\u53d6\u8bf7\u6c42\uff1b
    "},{"location":"chapter_appendix/contribution/#1323-docker","title":"13.2.3. \u00a0 Docker \u90e8\u7f72","text":"

    \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 Docker \u6765\u90e8\u7f72\u672c\u9879\u76ee\u3002\u6267\u884c\u4ee5\u4e0b\u811a\u672c\uff0c\u7a0d\u7b49\u7247\u523b\u540e\uff0c\u5373\u53ef\u4f7f\u7528\u6d4f\u89c8\u5668\u6253\u5f00 http://localhost:8000 \u6765\u8bbf\u95ee\u672c\u9879\u76ee\u3002

    git clone https://github.com/krahets/hello-algo.git\ncd hello-algo\ndocker-compose up -d\n

    \u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5373\u53ef\u5220\u9664\u90e8\u7f72\u3002

    docker-compose down\n
    "},{"location":"chapter_appendix/installation/","title":"13.1. \u00a0 \u7f16\u7a0b\u73af\u5883\u5b89\u88c5","text":""},{"location":"chapter_appendix/installation/#1311-vscode","title":"13.1.1. \u00a0 VSCode","text":"

    \u672c\u4e66\u63a8\u8350\u4f7f\u7528\u5f00\u6e90\u8f7b\u91cf\u7684 VSCode \u4f5c\u4e3a\u672c\u5730 IDE \uff0c\u4e0b\u8f7d\u5e76\u5b89\u88c5 VSCode \u3002

    "},{"location":"chapter_appendix/installation/#1312-java","title":"13.1.2. \u00a0 Java \u73af\u5883","text":"
    1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 OpenJDK\uff08\u7248\u672c\u9700\u6ee1\u8db3 > JDK 9\uff09\u3002
    2. \u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 java \uff0c\u5b89\u88c5 Extension Pack for Java \u3002
    "},{"location":"chapter_appendix/installation/#1313-cc","title":"13.1.3. \u00a0 C/C++ \u73af\u5883","text":"
    1. Windows \u7cfb\u7edf\u9700\u8981\u5b89\u88c5 MinGW\uff08\u914d\u7f6e\u6559\u7a0b\uff09\uff0cMacOS \u81ea\u5e26 Clang \u65e0\u9700\u5b89\u88c5\u3002
    2. \u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 c++ \uff0c\u5b89\u88c5 C/C++ Extension Pack \u3002
    3. \uff08\u53ef\u9009\uff09\u6253\u5f00 Settings \u9875\u9762\uff0c\u641c\u7d22 Clang_format_fallback Style \u4ee3\u7801\u683c\u5f0f\u5316\u9009\u9879\uff0c\u8bbe\u7f6e\u4e3a { BasedOnStyle: Microsoft, BreakBeforeBraces: Attach } \u3002
    "},{"location":"chapter_appendix/installation/#1314-python","title":"13.1.4. \u00a0 Python \u73af\u5883","text":"
    1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 Miniconda3 \u3002
    2. \u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 python \uff0c\u5b89\u88c5 Python Extension Pack \u3002
    3. \uff08\u53ef\u9009\uff09\u5728\u547d\u4ee4\u884c\u8f93\u5165 pip install black \uff0c\u5b89\u88c5\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\u3002
    "},{"location":"chapter_appendix/installation/#1315-go","title":"13.1.5. \u00a0 Go \u73af\u5883","text":"
    1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 go \u3002
    2. \u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 go \uff0c\u5b89\u88c5 Go \u3002
    3. \u5feb\u6377\u952e Ctrl + 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\u3002
    "},{"location":"chapter_appendix/installation/#1316-javascript","title":"13.1.6. \u00a0 JavaScript \u73af\u5883","text":"
    1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 node.js \u3002
    2. \u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 javascript \uff0c\u5b89\u88c5 JavaScript (ES6) code snippets \u3002
    3. \uff08\u53ef\u9009\uff09\u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 Prettier \uff0c\u5b89\u88c5\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\u3002
    "},{"location":"chapter_appendix/installation/#1317-c","title":"13.1.7. \u00a0 C# \u73af\u5883","text":"
    1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 .Net 6.0 \uff1b
    2. \u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 c# \uff0c\u5b89\u88c5 c# \u3002
    "},{"location":"chapter_appendix/installation/#1318-swift","title":"13.1.8. \u00a0 Swift \u73af\u5883","text":"
    1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 Swift\uff1b
    2. \u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 swift \uff0c\u5b89\u88c5 Swift for Visual Studio Code\u3002
    "},{"location":"chapter_appendix/installation/#1319-rust","title":"13.1.9. \u00a0 Rust \u73af\u5883","text":"
    1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 Rust\uff1b
    2. \u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 rust \uff0c\u5b89\u88c5 rust-analyzer\u3002
    "},{"location":"chapter_array_and_linkedlist/","title":"4. \u00a0 \u6570\u7ec4\u4e0e\u94fe\u8868","text":""},{"location":"chapter_array_and_linkedlist/array/","title":"4.1. \u00a0 \u6570\u7ec4","text":"

    \u300c\u6570\u7ec4 Array\u300d\u662f\u4e00\u79cd\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u5176\u5c06\u76f8\u540c\u7c7b\u578b\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\u5143\u7d20\u7684\u300c\u7d22\u5f15 Index\u300d\u3002

    Fig. \u6570\u7ec4\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f

    \u6570\u7ec4\u521d\u59cb\u5316\u3002\u901a\u5e38\u6709\u65e0\u521d\u59cb\u503c\u548c\u7ed9\u5b9a\u521d\u59cb\u503c\u4e24\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5728\u672a\u7ed9\u5b9a\u521d\u59cb\u503c\u7684\u60c5\u51b5\u4e0b\uff0c\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\u901a\u5e38\u4f1a\u88ab\u521d\u59cb\u5316\u4e3a\u9ed8\u8ba4\u503c \\(0\\) \u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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.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\nint* arr1 = new int[5];\nint* nums1 = new int[5] { 1, 3, 2, 5, 4 };\n
    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.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.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.c
    int arr[5] = { 0 }; // { 0, 0, 0, 0, 0 }\nint nums[5] = { 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.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.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
    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
    "},{"location":"chapter_array_and_linkedlist/array/#411","title":"4.1.1. \u00a0 \u6570\u7ec4\u4f18\u70b9","text":"

    \u5728\u6570\u7ec4\u4e2d\u8bbf\u95ee\u5143\u7d20\u975e\u5e38\u9ad8\u6548\u3002\u7531\u4e8e\u6570\u7ec4\u5143\u7d20\u88ab\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\uff0c\u56e0\u6b64\u8ba1\u7b97\u6570\u7ec4\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\u975e\u5e38\u5bb9\u6613\u3002\u7ed9\u5b9a\u6570\u7ec4\u9996\u4e2a\u5143\u7d20\u7684\u5730\u5740\u548c\u67d0\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u516c\u5f0f\u8ba1\u7b97\u5f97\u5230\u8be5\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\uff0c\u4ece\u800c\u76f4\u63a5\u8bbf\u95ee\u6b64\u5143\u7d20\u3002

    Fig. \u6570\u7ec4\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\u8ba1\u7b97

    # \u5143\u7d20\u5185\u5b58\u5730\u5740 = \u6570\u7ec4\u5185\u5b58\u5730\u5740 + \u5143\u7d20\u957f\u5ea6 * \u5143\u7d20\u7d22\u5f15\nelementAddr = firtstElementAddr + elementLength * elementIndex\n

    \u4e3a\u4ec0\u4e48\u6570\u7ec4\u5143\u7d20\u7684\u7d22\u5f15\u8981\u4ece \\(0\\) \u5f00\u59cb\u7f16\u53f7\u5462\uff1f

    \u89c2\u5bdf\u4e0a\u56fe\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

    \u7136\u800c\uff0c\u4ece\u5730\u5740\u8ba1\u7b97\u516c\u5f0f\u7684\u89d2\u5ea6\u770b\uff0c\u7d22\u5f15\u672c\u8d28\u4e0a\u8868\u793a\u7684\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\u7d22\u5f15\u4e3a \\(0\\) \u4e5f\u662f\u5408\u7406\u7684\u3002

    \u8bbf\u95ee\u5143\u7d20\u7684\u9ad8\u6548\u6027\u5e26\u6765\u4e86\u8bf8\u591a\u4fbf\u5229\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u968f\u673a\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u5143\u7d20\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart array.java
    /* \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20 */\nint randomAccess(int[] nums) {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nint randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nint randomNum = nums[randomIndex];\nreturn randomNum;\n}\n
    array.cpp
    /* \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20 */\nint randomAccess(int *nums, int size) {\n// \u5728\u533a\u95f4 [0, size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nint randomIndex = rand() % size;\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nint randomNum = nums[randomIndex];\nreturn randomNum;\n}\n
    array.py
    def 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\nrandom_index = random.randint(0, len(nums) - 1)\n# \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nrandom_num = nums[random_index]\nreturn random_num\n
    array.go
    /* \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20 */\nfunc randomAccess(nums []int) (randomNum int) {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nrandomIndex := rand.Intn(len(nums))\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nrandomNum = nums[randomIndex]\nreturn\n}\n
    array.js
    /* \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20 */\nfunction randomAccess(nums) {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nconst random_index = Math.floor(Math.random() * nums.length);\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nconst random_num = nums[random_index];\nreturn random_num;\n}\n
    array.ts
    /* \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20 */\nfunction randomAccess(nums: number[]): number {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nconst random_index = Math.floor(Math.random() * nums.length);\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nconst random_num = nums[random_index];\nreturn random_num;\n}\n
    array.c
    /* \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20 */\nint randomAccess(int *nums, int size) {\n// \u5728\u533a\u95f4 [0, size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nint randomIndex = rand() % size;\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nint randomNum = nums[randomIndex];\nreturn randomNum;\n}\n
    array.cs
    /* \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20 */\nint randomAccess(int[] nums) {\nRandom random = new();\n// \u5728\u533a\u95f4 [0, nums.Length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nint randomIndex = random.Next(nums.Length);\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nint randomNum = nums[randomIndex];\nreturn randomNum;\n}\n
    array.swift
    /* \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20 */\nfunc randomAccess(nums: [Int]) -> Int {\n// \u5728\u533a\u95f4 [0, nums.count) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nlet randomIndex = nums.indices.randomElement()!\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nlet randomNum = nums[randomIndex]\nreturn randomNum\n}\n
    array.zig
    // \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20\nfn randomAccess(nums: []i32) i32 {\n// \u5728\u533a\u95f4 [0, nums.len) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6574\u6570\nvar randomIndex = std.crypto.random.intRangeLessThan(usize, 0, nums.len);\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nvar randomNum = nums[randomIndex];\nreturn randomNum;\n}\n
    array.dart
    /* \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20 */\nint randomAccess(List nums) {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nint randomIndex = Random().nextInt(nums.length);\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nint randomNum = nums[randomIndex];\nreturn randomNum;\n}\n
    "},{"location":"chapter_array_and_linkedlist/array/#412","title":"4.1.2. \u00a0 \u6570\u7ec4\u7f3a\u70b9","text":"

    \u6570\u7ec4\u5728\u521d\u59cb\u5316\u540e\u957f\u5ea6\u4e0d\u53ef\u53d8\u3002\u7531\u4e8e\u7cfb\u7edf\u65e0\u6cd5\u4fdd\u8bc1\u6570\u7ec4\u4e4b\u540e\u7684\u5185\u5b58\u7a7a\u95f4\u662f\u53ef\u7528\u7684\uff0c\u56e0\u6b64\u6570\u7ec4\u957f\u5ea6\u65e0\u6cd5\u6269\u5c55\u3002\u800c\u82e5\u5e0c\u671b\u6269\u5bb9\u6570\u7ec4\uff0c\u5219\u9700\u65b0\u5efa\u4e00\u4e2a\u6570\u7ec4\uff0c\u7136\u540e\u628a\u539f\u6570\u7ec4\u5143\u7d20\u4f9d\u6b21\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\uff0c\u5728\u6570\u7ec4\u5f88\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd9\u662f\u975e\u5e38\u8017\u65f6\u7684\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nint[] res = new int[nums.length + enlarge];\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor (int i = 0; i < nums.length; i++) {\nres[i] = nums[i];\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res;\n}\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\nint *res = new int[size + enlarge];\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor (int i = 0; i < size; i++) {\nres[i] = nums[i];\n}\n// \u91ca\u653e\u5185\u5b58\ndelete[] nums;\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res;\n}\n
    array.py
    def 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\nres = [0] * (len(nums) + enlarge)\n# \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor i in range(len(nums)):\nres[i] = nums[i]\n# \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res\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\nres := make([]int, len(nums)+enlarge)\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor i, num := range nums {\nres[i] = num\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn 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\u662f\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\nconst 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\nfor (let i = 0; i < nums.length; i++) {\nres[i] = nums[i];\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn 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\u662f\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\nconst 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\nfor (let i = 0; i < nums.length; i++) {\nres[i] = nums[i];\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn 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\nint *res = (int *)malloc(sizeof(int) * (size + enlarge));\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor (int i = 0; i < size; i++) {\nres[i] = nums[i];\n}\n// \u521d\u59cb\u5316\u6269\u5c55\u540e\u7684\u7a7a\u95f4\nfor (int i = size; i < size + enlarge; i++) {\nres[i] = 0;\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn 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\nint[] res = new int[nums.Length + enlarge];\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor (int i = 0; i < nums.Length; i++) {\nres[i] = nums[i];\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn 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\nvar 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\nfor i in nums.indices {\nres[i] = nums[i]\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn 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\nvar res = try mem_allocator.alloc(i32, nums.len + enlarge);\nstd.mem.set(i32, res, 0);\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nstd.mem.copy(i32, res, nums);\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res;\n}\n
    array.dart
    /* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nList extend(List nums, int enlarge) {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\nList<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\nfor (var i = 0; i < nums.length; i++) {\nres[i] = nums[i];\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res;\n}\n

    \u6570\u7ec4\u4e2d\u63d2\u5165\u6216\u5220\u9664\u5143\u7d20\u6548\u7387\u4f4e\u4e0b\u3002\u5982\u679c\u6211\u4eec\u60f3\u8981\u5728\u6570\u7ec4\u4e2d\u95f4\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\uff0c\u7531\u4e8e\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\u653e\u4efb\u4f55\u6570\u636e\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4e0d\u5f97\u4e0d\u5c06\u6b64\u7d22\u5f15\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u7136\u540e\u518d\u628a\u5143\u7d20\u8d4b\u503c\u7ed9\u8be5\u7d22\u5f15\u3002

    Fig. \u6570\u7ec4\u63d2\u5165\u5143\u7d20

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nfor (int i = nums.length - 1; i > index; i--) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num;\n}\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\nfor (int i = size - 1; i > index; i--) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num;\n}\n
    array.py
    def insert(nums: list[int], num: int, index: int) -> None:\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\nfor i in range(len(nums) - 1, index, -1):\nnums[i] = nums[i - 1]\n# \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num\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\nfor i := len(nums) - 1; i > index; i-- {\nnums[i] = nums[i-1]\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[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\nfor (let i = nums.length - 1; i > index; i--) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[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\nfor (let i = nums.length - 1; i > index; i--) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[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\nfor (int i = size - 1; i > index; i--) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[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\nfor (int i = nums.Length - 1; i > index; i--) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[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\nfor i in sequence(first: nums.count - 1, next: { $0 > index + 1 ? $0 - 1 : nil }) {\nnums[i] = nums[i - 1]\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[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\nvar i = nums.len - 1;\nwhile (i > index) : (i -= 1) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num;\n}\n
    array.dart
    /* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(List 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\nfor (var i = nums.length - 1; i > index; i--) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num;\n}\n

    \u5220\u9664\u5143\u7d20\u4e5f\u7c7b\u4f3c\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\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\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5220\u9664\u5143\u7d20\u540e\uff0c\u539f\u5148\u672b\u5c3e\u7684\u5143\u7d20\u53d8\u5f97\u201c\u65e0\u610f\u4e49\u201d\u4e86\uff0c\u6211\u4eec\u65e0\u9700\u7279\u610f\u53bb\u4fee\u6539\u5b83\u3002

    Fig. \u6570\u7ec4\u5220\u9664\u5143\u7d20

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart array.java
    /* \u5220\u9664\u7d22\u5f15 index \u5904\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\nfor (int i = index; i < nums.length - 1; i++) {\nnums[i] = nums[i + 1];\n}\n}\n
    array.cpp
    /* \u5220\u9664\u7d22\u5f15 index \u5904\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\nfor (int i = index; i < size - 1; i++) {\nnums[i] = nums[i + 1];\n}\n}\n
    array.py
    def remove(nums: list[int], index: int) -> None:\n\"\"\"\u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20\"\"\"\n# \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor i in range(index, len(nums) - 1):\nnums[i] = nums[i + 1]\n
    array.go
    /* \u5220\u9664\u7d22\u5f15 index \u5904\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\nfor i := index; i < len(nums)-1; i++ {\nnums[i] = nums[i+1]\n}\n}\n
    array.js
    /* \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 */\nfunction remove(nums, index) {\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (let i = index; i < nums.length - 1; i++) {\nnums[i] = nums[i + 1];\n}\n}\n
    array.ts
    /* \u5220\u9664\u7d22\u5f15 index \u5904\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\nfor (let i = index; i < nums.length - 1; i++) {\nnums[i] = nums[i + 1];\n}\n}\n
    array.c
    /* \u5220\u9664\u7d22\u5f15 index \u5904\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\nfor (int i = index; i < size - 1; i++) {\nnums[i] = nums[i + 1];\n}\n}\n
    array.cs
    /* \u5220\u9664\u7d22\u5f15 index \u5904\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\nfor (int i = index; i < nums.Length - 1; i++) {\nnums[i] = nums[i + 1];\n}\n}\n
    array.swift
    /* \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 */\nfunc remove(nums: inout [Int], index: Int) {\nlet count = nums.count\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor i in sequence(first: index, next: { $0 < count - 1 - 1 ? $0 + 1 : nil }) {\nnums[i] = nums[i + 1]\n}\n}\n
    array.zig
    // \u5220\u9664\u7d22\u5f15 index \u5904\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\nvar i = index;\nwhile (i < nums.len - 1) : (i += 1) {\nnums[i] = nums[i + 1];\n}\n}\n
    array.dart
    /* \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 */\nvoid remove(List nums, int index) {\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (var i = index; i < nums.length - 1; i++) {\nnums[i] = nums[i + 1];\n}\n}\n

    \u603b\u7ed3\u6765\u770b\uff0c\u6570\u7ec4\u7684\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u6709\u4ee5\u4e0b\u7f3a\u70b9\uff1a

    • \u65f6\u95f4\u590d\u6742\u5ea6\u9ad8\uff1a\u6570\u7ec4\u7684\u63d2\u5165\u548c\u5220\u9664\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u6570\u7ec4\u957f\u5ea6\u3002
    • \u4e22\u5931\u5143\u7d20\uff1a\u7531\u4e8e\u6570\u7ec4\u7684\u957f\u5ea6\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u5728\u63d2\u5165\u5143\u7d20\u540e\uff0c\u8d85\u51fa\u6570\u7ec4\u957f\u5ea6\u8303\u56f4\u7684\u5143\u7d20\u4f1a\u4e22\u5931\u3002
    • \u5185\u5b58\u6d6a\u8d39\uff1a\u6211\u4eec\u53ef\u4ee5\u521d\u59cb\u5316\u4e00\u4e2a\u6bd4\u8f83\u957f\u7684\u6570\u7ec4\uff0c\u53ea\u7528\u524d\u9762\u4e00\u90e8\u5206\uff0c\u8fd9\u6837\u5728\u63d2\u5165\u6570\u636e\u65f6\uff0c\u4e22\u5931\u7684\u672b\u5c3e\u5143\u7d20\u90fd\u662f\u6211\u4eec\u4e0d\u5173\u5fc3\u7684\uff0c\u4f46\u8fd9\u6837\u505a\u540c\u65f6\u4e5f\u4f1a\u9020\u6210\u5185\u5b58\u7a7a\u95f4\u7684\u6d6a\u8d39\u3002
    "},{"location":"chapter_array_and_linkedlist/array/#413","title":"4.1.3. \u00a0 \u6570\u7ec4\u5e38\u7528\u64cd\u4f5c","text":"

    \u6570\u7ec4\u904d\u5386\u3002\u4ee5\u4e0b\u4ecb\u7ecd\u4e24\u79cd\u5e38\u7528\u7684\u904d\u5386\u65b9\u6cd5\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart array.java
    /* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int[] nums) {\nint count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i < nums.length; i++) {\ncount++;\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (int num : nums) {\ncount++;\n}\n}\n
    array.cpp
    /* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int *nums, int size) {\nint count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i < size; i++) {\ncount++;\n}\n}\n
    array.py
    def traverse(nums: list[int]) -> None:\n\"\"\"\u904d\u5386\u6570\u7ec4\"\"\"\ncount = 0\n# \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor i in range(len(nums)):\ncount += 1\n# \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor num in nums:\ncount += 1\n# \u540c\u65f6\u904d\u5386\u6570\u636e\u7d22\u5f15\u548c\u5143\u7d20\nfor i, num in enumerate(nums):\ncount += 1\n
    array.go
    /* \u904d\u5386\u6570\u7ec4 */\nfunc traverse(nums []int) {\ncount := 0\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor i := 0; i < len(nums); i++ {\ncount++\n}\ncount = 0\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor range nums {\ncount++\n}\n}\n
    array.js
    /* \u904d\u5386\u6570\u7ec4 */\nfunction traverse(nums) {\nlet count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (let i = 0; i < nums.length; i++) {\ncount++;\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (let num of nums) {\ncount += 1;\n}\n}\n
    array.ts
    /* \u904d\u5386\u6570\u7ec4 */\nfunction traverse(nums: number[]): void {\nlet count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (let i = 0; i < nums.length; i++) {\ncount++;\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (let num of nums) {\ncount += 1;\n}\n}\n
    array.c
    /* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int *nums, int size) {\nint count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i < size; i++) {\ncount++;\n}\n}\n
    array.cs
    /* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int[] nums) {\nint count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i < nums.Length; i++) {\ncount++;\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nforeach (int num in nums) {\ncount++;\n}\n}\n
    array.swift
    /* \u904d\u5386\u6570\u7ec4 */\nfunc traverse(nums: [Int]) {\nvar count = 0\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor _ in nums.indices {\ncount += 1\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor _ in nums {\ncount += 1\n}\n}\n
    array.zig
    // \u904d\u5386\u6570\u7ec4\nfn traverse(nums: []i32) void {\nvar count: i32 = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nvar i: i32 = 0;\nwhile (i < nums.len) : (i += 1) {\ncount += 1;\n}\ncount = 0;\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (nums) |_| {\ncount += 1;\n}\n}\n
    array.dart
    /* \u904d\u5386\u6570\u7ec4\u5143\u7d20 */\nvoid traverse(List nums) {\nvar count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (var i = 0; i < nums.length; i++) {\ncount++;\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (var num in nums) {\ncount++;\n}\n// \u901a\u8fc7 forEach \u65b9\u6cd5\u904d\u5386\u6570\u7ec4\nnums.forEach((element) {\ncount++;\n});\n}\n

    \u6570\u7ec4\u67e5\u627e\u3002\u901a\u8fc7\u904d\u5386\u6570\u7ec4\uff0c\u67e5\u627e\u6570\u7ec4\u5185\u7684\u6307\u5b9a\u5143\u7d20\uff0c\u5e76\u8f93\u51fa\u5bf9\u5e94\u7d22\u5f15\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart array.java
    /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int[] nums, int target) {\nfor (int i = 0; i < nums.length; i++) {\nif (nums[i] == target)\nreturn i;\n}\nreturn -1;\n}\n
    array.cpp
    /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int *nums, int size, int target) {\nfor (int i = 0; i < size; i++) {\nif (nums[i] == target)\nreturn i;\n}\nreturn -1;\n}\n
    array.py
    def find(nums: list[int], target: int) -> int:\n\"\"\"\u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\"\"\"\nfor i in range(len(nums)):\nif nums[i] == target:\nreturn i\nreturn -1\n
    array.go
    /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunc find(nums []int, target int) (index int) {\nindex = -1\nfor i := 0; i < len(nums); i++ {\nif nums[i] == target {\nindex = i\nbreak\n}\n}\nreturn\n}\n
    array.js
    /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunction find(nums, target) {\nfor (let i = 0; i < nums.length; i++) {\nif (nums[i] == target) return i;\n}\nreturn -1;\n}\n
    array.ts
    /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunction find(nums: number[], target: number): number {\nfor (let i = 0; i < nums.length; i++) {\nif (nums[i] === target) {\nreturn i;\n}\n}\nreturn -1;\n}\n
    array.c
    /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int *nums, int size, int target) {\nfor (int i = 0; i < size; i++) {\nif (nums[i] == target)\nreturn i;\n}\nreturn -1;\n}\n
    array.cs
    /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int[] nums, int target) {\nfor (int i = 0; i < nums.Length; i++) {\nif (nums[i] == target)\nreturn i;\n}\nreturn -1;\n}\n
    array.swift
    /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunc find(nums: [Int], target: Int) -> Int {\nfor i in nums.indices {\nif nums[i] == target {\nreturn i\n}\n}\nreturn -1\n}\n
    array.zig
    // \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\nfn find(nums: []i32, target: i32) i32 {\nfor (nums) |num, i| {\nif (num == target) return @intCast(i32, i);\n}\nreturn -1;\n}\n
    array.dart
    /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(List nums, int target) {\nfor (var i = 0; i < nums.length; i++) {\nif (nums[i] == target) return i;\n}\nreturn -1;\n}\n
    "},{"location":"chapter_array_and_linkedlist/array/#414","title":"4.1.4. \u00a0 \u6570\u7ec4\u5178\u578b\u5e94\u7528","text":"

    \u968f\u673a\u8bbf\u95ee\u3002\u5982\u679c\u6211\u4eec\u60f3\u8981\u968f\u673a\u62bd\u53d6\u4e00\u4e9b\u6837\u672c\uff0c\u90a3\u4e48\u53ef\u4ee5\u7528\u6570\u7ec4\u5b58\u50a8\uff0c\u5e76\u751f\u6210\u4e00\u4e2a\u968f\u673a\u5e8f\u5217\uff0c\u6839\u636e\u7d22\u5f15\u5b9e\u73b0\u6837\u672c\u7684\u968f\u673a\u62bd\u53d6\u3002

    \u4e8c\u5206\u67e5\u627e\u3002\u4f8b\u5982\u524d\u6587\u67e5\u5b57\u5178\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b57\u5178\u4e2d\u7684\u6240\u6709\u5b57\u6309\u7167\u62fc\u97f3\u987a\u5e8f\u5b58\u50a8\u5728\u6570\u7ec4\u4e2d\uff0c\u7136\u540e\u4f7f\u7528\u4e0e\u65e5\u5e38\u67e5\u7eb8\u8d28\u5b57\u5178\u76f8\u540c\u7684\u201c\u7ffb\u5f00\u4e2d\u95f4\uff0c\u6392\u9664\u4e00\u534a\u201d\u7684\u65b9\u5f0f\uff0c\u6765\u5b9e\u73b0\u4e00\u4e2a\u67e5\u7535\u5b50\u5b57\u5178\u7684\u7b97\u6cd5\u3002

    \u6df1\u5ea6\u5b66\u4e60\u3002\u795e\u7ecf\u7f51\u7edc\u4e2d\u5927\u91cf\u4f7f\u7528\u4e86\u5411\u91cf\u3001\u77e9\u9635\u3001\u5f20\u91cf\u4e4b\u95f4\u7684\u7ebf\u6027\u4ee3\u6570\u8fd0\u7b97\uff0c\u8fd9\u4e9b\u6570\u636e\u90fd\u662f\u4ee5\u6570\u7ec4\u7684\u5f62\u5f0f\u6784\u5efa\u7684\u3002\u6570\u7ec4\u662f\u795e\u7ecf\u7f51\u7edc\u7f16\u7a0b\u4e2d\u6700\u5e38\u4f7f\u7528\u7684\u6570\u636e\u7ed3\u6784\u3002

    "},{"location":"chapter_array_and_linkedlist/linked_list/","title":"4.2. \u00a0 \u94fe\u8868","text":"

    \u5185\u5b58\u7a7a\u95f4\u662f\u6240\u6709\u7a0b\u5e8f\u7684\u516c\u5171\u8d44\u6e90\uff0c\u6392\u9664\u5df2\u88ab\u5360\u7528\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u7a7a\u95f2\u5185\u5b58\u7a7a\u95f4\u901a\u5e38\u6563\u843d\u5728\u5185\u5b58\u5404\u5904\u3002\u5728\u4e0a\u4e00\u8282\u4e2d\uff0c\u6211\u4eec\u63d0\u5230\u5b58\u50a8\u6570\u7ec4\u7684\u5185\u5b58\u7a7a\u95f4\u5fc5\u987b\u662f\u8fde\u7eed\u7684\uff0c\u800c\u5f53\u6211\u4eec\u9700\u8981\u7533\u8bf7\u4e00\u4e2a\u975e\u5e38\u5927\u7684\u6570\u7ec4\u65f6\uff0c\u7a7a\u95f2\u5185\u5b58\u4e2d\u53ef\u80fd\u6ca1\u6709\u8fd9\u4e48\u5927\u7684\u8fde\u7eed\u7a7a\u95f4\u3002\u4e0e\u6570\u7ec4\u76f8\u6bd4\uff0c\u94fe\u8868\u66f4\u5177\u7075\u6d3b\u6027\uff0c\u5b83\u53ef\u4ee5\u88ab\u5b58\u50a8\u5728\u975e\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\u3002

    \u300c\u94fe\u8868 Linked List\u300d\u662f\u4e00\u79cd\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u5176\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a\u8282\u70b9\u5bf9\u8c61\uff0c\u5404\u4e2a\u8282\u70b9\u4e4b\u95f4\u901a\u8fc7\u6307\u9488\u8fde\u63a5\uff0c\u4ece\u5f53\u524d\u8282\u70b9\u901a\u8fc7\u6307\u9488\u53ef\u4ee5\u8bbf\u95ee\u5230\u4e0b\u4e00\u4e2a\u8282\u70b9\u3002\u7531\u4e8e\u6307\u9488\u8bb0\u5f55\u4e86\u4e0b\u4e2a\u8282\u70b9\u7684\u5185\u5b58\u5730\u5740\uff0c\u56e0\u6b64\u65e0\u9700\u4fdd\u8bc1\u5185\u5b58\u5730\u5740\u7684\u8fde\u7eed\u6027\uff0c\u4ece\u800c\u53ef\u4ee5\u5c06\u5404\u4e2a\u8282\u70b9\u5206\u6563\u5b58\u50a8\u5728\u5185\u5b58\u5404\u5904\u3002

    \u94fe\u8868\u300c\u8282\u70b9 Node\u300d\u5305\u542b\u4e24\u9879\u6570\u636e\uff0c\u4e00\u662f\u8282\u70b9\u300c\u503c Value\u300d\uff0c\u4e8c\u662f\u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u300c\u6307\u9488 Pointer\u300d\uff0c\u6216\u79f0\u300c\u5f15\u7528 Reference\u300d\u3002

    Fig. \u94fe\u8868\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    /* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nint val;        // \u8282\u70b9\u503c\nListNode next;  // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode(int x) { val = x; }  // \u6784\u9020\u51fd\u6570\n}\n
    /* \u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct ListNode {\nint val;         // \u8282\u70b9\u503c\nListNode *next;  // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode(int x) : val(x), next(nullptr) {}  // \u6784\u9020\u51fd\u6570\n};\n
    class ListNode:\n\"\"\"\u94fe\u8868\u8282\u70b9\u7c7b\"\"\"\ndef __init__(self, val: int):\nself.val: int = val                  # \u8282\u70b9\u503c\nself.next: Optional[ListNode] = None # \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n
    /* \u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype ListNode struct {\nVal  int       // \u8282\u70b9\u503c\nNext *ListNode // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n}\n// NewListNode \u6784\u9020\u51fd\u6570\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u94fe\u8868\nfunc NewListNode(val int) *ListNode {\nreturn &ListNode{\nVal:  val,\nNext: nil,\n}\n}\n
    /* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nval;\nnext;\nconstructor(val, next) {\nthis.val = (val === undefined ? 0 : val);       // \u8282\u70b9\u503c\nthis.next = (next === undefined ? null : next); // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n}\n}\n
    /* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nval: number;\nnext: ListNode | null;\nconstructor(val?: number, next?: ListNode | null) {\nthis.val = val === undefined ? 0 : val;        // \u8282\u70b9\u503c\nthis.next = next === undefined ? null : next;  // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n}\n}\n
    /* \u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct ListNode {\nint val;               // \u8282\u70b9\u503c\nstruct ListNode *next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n};\ntypedef struct ListNode ListNode;\n/* \u6784\u9020\u51fd\u6570 */\nListNode *newListNode(int val) {\nListNode *node, *next;\nnode = (ListNode *) malloc(sizeof(ListNode));\nnode->val = val;\nnode->next = NULL;\nreturn node;\n}\n
    /* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode\n{\nint val;         // \u8282\u70b9\u503c\nListNode next;   // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\nListNode(int x) => val = x;  //\u6784\u9020\u51fd\u6570\n}\n
    /* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nvar val: Int // \u8282\u70b9\u503c\nvar next: ListNode? // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\ninit(x: Int) { // \u6784\u9020\u51fd\u6570\nval = x\n}\n}\n
    // \u94fe\u8868\u8282\u70b9\u7c7b\npub fn ListNode(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nval: T = 0, // \u8282\u70b9\u503c\nnext: ?*Self = null, // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n// \u6784\u9020\u51fd\u6570\npub fn init(self: *Self, x: i32) void {\nself.val = x;\nself.next = null;\n}\n};\n}\n
    /* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nint val; // \u8282\u70b9\u503c\nListNode? next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode(this.val, [this.next]); // \u6784\u9020\u51fd\u6570\n}\n

    \u5c3e\u8282\u70b9\u6307\u5411\u4ec0\u4e48\uff1f

    \u6211\u4eec\u5c06\u94fe\u8868\u7684\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u79f0\u4e3a\u300c\u5c3e\u8282\u70b9\u300d\uff0c\u5176\u6307\u5411\u7684\u662f\u201c\u7a7a\u201d\uff0c\u5728 Java, C++, Python \u4e2d\u5206\u522b\u8bb0\u4e3a \\(\\text{null}\\) , \\(\\text{nullptr}\\) , \\(\\text{None}\\) \u3002\u5728\u4e0d\u5f15\u8d77\u6b67\u4e49\u7684\u524d\u63d0\u4e0b\uff0c\u672c\u4e66\u90fd\u4f7f\u7528 \\(\\text{null}\\) \u6765\u8868\u793a\u7a7a\u3002

    \u5982\u4f55\u79f0\u547c\u94fe\u8868\uff1f

    \u5728\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u6570\u7ec4\u6574\u4f53\u5c31\u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u4f8b\u5982\u6570\u7ec4 nums \uff0c\u5305\u542b\u5404\u4e2a\u5143\u7d20 nums[0] , nums[1] \u7b49\u7b49\u3002\u800c\u94fe\u8868\u662f\u7531\u591a\u4e2a\u8282\u70b9\u5bf9\u8c61\u7ec4\u6210\uff0c\u6211\u4eec\u901a\u5e38\u5c06\u5934\u8282\u70b9\u5f53\u4f5c\u94fe\u8868\u7684\u4ee3\u79f0\uff0c\u4f8b\u5982\u5934\u8282\u70b9 head \u548c\u94fe\u8868 head \u5b9e\u9645\u4e0a\u662f\u540c\u4e49\u7684\u3002

    \u94fe\u8868\u521d\u59cb\u5316\u65b9\u6cd5\u3002\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\u5f15\u7528\u6307\u5411\u5173\u7cfb\u3002\u5b8c\u6210\u540e\uff0c\u5373\u53ef\u4ee5\u4ece\u94fe\u8868\u7684\u5934\u8282\u70b9\uff08\u5373\u9996\u4e2a\u8282\u70b9\uff09\u51fa\u53d1\uff0c\u901a\u8fc7\u6307\u9488 next \u4f9d\u6b21\u8bbf\u95ee\u6240\u6709\u8282\u70b9\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\u5f15\u7528\u6307\u5411\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\u5f15\u7528\u6307\u5411\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n
    linked_list.py
    # \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\u5f15\u7528\u6307\u5411\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\u5f15\u7528\u6307\u5411\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\u5f15\u7528\u6307\u5411\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\u5f15\u7528\u6307\u5411\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\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\u5f15\u7528\u6307\u5411\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 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\u5f15\u7528\u6307\u5411\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\u5f15\u7528\u6307\u5411\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\u5f15\u7528\u6307\u5411\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\u5f15\u7528\u6307\u5411\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
    "},{"location":"chapter_array_and_linkedlist/linked_list/#421","title":"4.2.1. \u00a0 \u94fe\u8868\u4f18\u70b9","text":"

    \u94fe\u8868\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\u7684\u64cd\u4f5c\u6548\u7387\u9ad8\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u60f3\u5728\u94fe\u8868\u4e2d\u95f4\u7684\u4e24\u4e2a\u8282\u70b9 A , B \u4e4b\u95f4\u63d2\u5165\u4e00\u4e2a\u65b0\u8282\u70b9 P \uff0c\u6211\u4eec\u53ea\u9700\u8981\u6539\u53d8\u4e24\u4e2a\u8282\u70b9\u6307\u9488\u5373\u53ef\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff1b\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u6570\u7ec4\u7684\u63d2\u5165\u64cd\u4f5c\u6548\u7387\u8981\u4f4e\u5f97\u591a\u3002

    Fig. \u94fe\u8868\u63d2\u5165\u8282\u70b9

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart linked_list.java
    /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode n0, ListNode P) {\nListNode n1 = n0.next;\nP.next = n1;\nn0.next = P;\n}\n
    linked_list.cpp
    /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode *n0, ListNode *P) {\nListNode *n1 = n0->next;\nP->next = n1;\nn0->next = P;\n}\n
    linked_list.py
    def insert(n0: ListNode, P: ListNode) -> None:\n\"\"\"\u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P\"\"\"\nn1 = n0.next\nP.next = n1\nn0.next = P\n
    linked_list.go
    /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunc insertNode(n0 *ListNode, P *ListNode) {\nn1 := n0.Next\nP.Next = n1\nn0.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) {\nconst n1 = n0.next;\nP.next = n1;\nn0.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 {\nconst n1 = n0.next;\nP.next = n1;\nn0.next = 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) {\nListNode *n1 = n0->next;\nP->next = n1;\nn0->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) {\nListNode? n1 = n0.next;\nP.next = n1;\nn0.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) {\nlet n1 = n0.next\nP.next = n1\nn0.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 {\nvar n1 = n0.?.next;\nP.?.next = n1;\nn0.?.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) {\nListNode? n1 = n0.next;\nP.next = n1;\nn0.next = P;\n}\n

    \u5728\u94fe\u8868\u4e2d\u5220\u9664\u8282\u70b9\u4e5f\u975e\u5e38\u65b9\u4fbf\uff0c\u53ea\u9700\u6539\u53d8\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488\u5373\u53ef\u3002\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5c3d\u7ba1\u5728\u5220\u9664\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u8282\u70b9 P \u4ecd\u7136\u6307\u5411 n1 \uff0c\u4f46\u5b9e\u9645\u4e0a P \u5df2\u7ecf\u4e0d\u518d\u5c5e\u4e8e\u6b64\u94fe\u8868\uff0c\u56e0\u4e3a\u904d\u5386\u6b64\u94fe\u8868\u65f6\u65e0\u6cd5\u8bbf\u95ee\u5230 P \u3002

    Fig. \u94fe\u8868\u5220\u9664\u8282\u70b9

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart linked_list.java
    /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode n0) {\nif (n0.next == null)\nreturn;\n// n0 -> P -> n1\nListNode P = n0.next;\nListNode n1 = P.next;\nn0.next = n1;\n}\n
    linked_list.cpp
    /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode *n0) {\nif (n0->next == nullptr)\nreturn;\n// n0 -> P -> n1\nListNode *P = n0->next;\nListNode *n1 = P->next;\nn0->next = n1;\n// \u91ca\u653e\u5185\u5b58\ndelete P;\n}\n
    linked_list.py
    def remove(n0: ListNode) -> None:\n\"\"\"\u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9\"\"\"\nif not n0.next:\nreturn\n# n0 -> P -> n1\nP = n0.next\nn1 = P.next\nn0.next = n1\n
    linked_list.go
    /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunc removeNode(n0 *ListNode) {\nif n0.Next == nil {\nreturn\n}\n// n0 -> P -> n1\nP := n0.Next\nn1 := P.Next\nn0.Next = n1\n}\n
    linked_list.js
    /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunction remove(n0) {\nif (!n0.next) return;\n// n0 -> P -> n1\nconst P = n0.next;\nconst n1 = P.next;\nn0.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 {\nif (!n0.next) {\nreturn;\n}\n// n0 -> P -> n1\nconst P = n0.next;\nconst n1 = P.next;\nn0.next = n1;\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 removeNode(ListNode *n0) {\nif (!n0->next)\nreturn;\n// n0 -> P -> n1\nListNode *P = n0->next;\nListNode *n1 = P->next;\nn0->next = n1;\n// \u91ca\u653e\u5185\u5b58\nfree(P);\n}\n
    linked_list.cs
    /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode n0) {\nif (n0.next == null)\nreturn;\n// n0 -> P -> n1\nListNode P = n0.next;\nListNode? n1 = P.next;\nn0.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) {\nif n0.next == nil {\nreturn\n}\n// n0 -> P -> n1\nlet P = n0.next\nlet n1 = P?.next\nn0.next = n1\nP?.next = nil\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 {\nif (n0.?.next == null) return;\n// n0 -> P -> n1\nvar P = n0.?.next;\nvar n1 = P.?.next;\nn0.?.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) {\nif (n0.next == null) return;\n// n0 -> P -> n1\nListNode P = n0.next!;\nListNode? n1 = P.next;\nn0.next = n1;\n}\n
    "},{"location":"chapter_array_and_linkedlist/linked_list/#422","title":"4.2.2. \u00a0 \u94fe\u8868\u7f3a\u70b9","text":"

    \u94fe\u8868\u8bbf\u95ee\u8282\u70b9\u6548\u7387\u8f83\u4f4e\u3002\u5982\u4e0a\u8282\u6240\u8ff0\uff0c\u6570\u7ec4\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u4e0b\u8bbf\u95ee\u4efb\u610f\u5143\u7d20\u3002\u7136\u800c\uff0c\u94fe\u8868\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95ee\u4efb\u610f\u8282\u70b9\uff0c\u8fd9\u662f\u56e0\u4e3a\u7cfb\u7edf\u9700\u8981\u4ece\u5934\u8282\u70b9\u51fa\u53d1\uff0c\u9010\u4e2a\u5411\u540e\u904d\u5386\u76f4\u81f3\u627e\u5230\u76ee\u6807\u8282\u70b9\u3002\u4f8b\u5982\uff0c\u82e5\u8981\u8bbf\u95ee\u94fe\u8868\u7d22\u5f15\u4e3a index\uff08\u5373\u7b2c index + 1 \u4e2a\uff09\u7684\u8282\u70b9\uff0c\u5219\u9700\u8981\u5411\u540e\u904d\u5386 index \u8f6e\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart linked_list.java
    /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode access(ListNode head, int index) {\nfor (int i = 0; i < index; i++) {\nif (head == null)\nreturn null;\nhead = head.next;\n}\nreturn head;\n}\n
    linked_list.cpp
    /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode *access(ListNode *head, int index) {\nfor (int i = 0; i < index; i++) {\nif (head == nullptr)\nreturn nullptr;\nhead = head->next;\n}\nreturn head;\n}\n
    linked_list.py
    def access(head: ListNode, index: int) -> ListNode | None:\n\"\"\"\u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9\"\"\"\nfor _ in range(index):\nif not head:\nreturn None\nhead = head.next\nreturn head\n
    linked_list.go
    /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunc access(head *ListNode, index int) *ListNode {\nfor i := 0; i < index; i++ {\nif head == nil {\nreturn nil\n}\nhead = head.Next\n}\nreturn head\n}\n
    linked_list.js
    /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunction access(head, index) {\nfor (let i = 0; i < index; i++) {\nif (!head) {\nreturn null;\n}\nhead = head.next;\n}\nreturn 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 {\nfor (let i = 0; i < index; i++) {\nif (!head) {\nreturn null;\n}\nhead = head.next;\n}\nreturn head;\n}\n
    linked_list.c
    /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode *access(ListNode *head, int index) {\nwhile (head && head->next && index) {\nhead = head->next;\nindex--;\n}\nreturn head;\n}\n
    linked_list.cs
    /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode? access(ListNode head, int index) {\nfor (int i = 0; i < index; i++) {\nif (head == null)\nreturn null;\nhead = head.next;\n}\nreturn 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? {\nvar head: ListNode? = head\nfor _ in 0 ..< index {\nif head == nil {\nreturn nil\n}\nhead = head?.next\n}\nreturn 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) {\nvar head = node;\nvar i: i32 = 0;\nwhile (i < index) : (i += 1) {\nhead = head.?.next;\nif (head == null) return null;\n}\nreturn head;\n}\n
    linked_list.dart
    /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode? access(ListNode? head, int index) {\nfor (var i = 0; i < index; i++) {\nif (head == null) return null;\nhead = head.next;\n}\nreturn head;\n}\n

    \u94fe\u8868\u7684\u5185\u5b58\u5360\u7528\u8f83\u5927\u3002\u94fe\u8868\u4ee5\u8282\u70b9\u4e3a\u5355\u4f4d\uff0c\u6bcf\u4e2a\u8282\u70b9\u9664\u4e86\u4fdd\u5b58\u503c\u4e4b\u5916\uff0c\u8fd8\u9700\u989d\u5916\u4fdd\u5b58\u6307\u9488\uff08\u5f15\u7528\uff09\u3002\u8fd9\u610f\u5473\u7740\u5728\u76f8\u540c\u6570\u636e\u91cf\u7684\u60c5\u51b5\u4e0b\uff0c\u94fe\u8868\u6bd4\u6570\u7ec4\u9700\u8981\u5360\u7528\u66f4\u591a\u7684\u5185\u5b58\u7a7a\u95f4\u3002

    "},{"location":"chapter_array_and_linkedlist/linked_list/#423","title":"4.2.3. \u00a0 \u94fe\u8868\u5e38\u7528\u64cd\u4f5c","text":"

    \u904d\u5386\u94fe\u8868\u67e5\u627e\u3002\u904d\u5386\u94fe\u8868\uff0c\u67e5\u627e\u94fe\u8868\u5185\u503c\u4e3a target \u7684\u8282\u70b9\uff0c\u8f93\u51fa\u8282\u70b9\u5728\u94fe\u8868\u4e2d\u7684\u7d22\u5f15\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart linked_list.java
    /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode head, int target) {\nint index = 0;\nwhile (head != null) {\nif (head.val == target)\nreturn index;\nhead = head.next;\nindex++;\n}\nreturn -1;\n}\n
    linked_list.cpp
    /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode *head, int target) {\nint index = 0;\nwhile (head != nullptr) {\nif (head->val == target)\nreturn index;\nhead = head->next;\nindex++;\n}\nreturn -1;\n}\n
    linked_list.py
    def find(head: ListNode, target: int) -> int:\n\"\"\"\u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9\"\"\"\nindex = 0\nwhile head:\nif head.val == target:\nreturn index\nhead = head.next\nindex += 1\nreturn -1\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 {\nindex := 0\nfor head != nil {\nif head.Val == target {\nreturn index\n}\nhead = head.Next\nindex++\n}\nreturn -1\n}\n
    linked_list.js
    /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunction find(head, target) {\nlet index = 0;\nwhile (head !== null) {\nif (head.val === target) {\nreturn index;\n}\nhead = head.next;\nindex += 1;\n}\nreturn -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 {\nlet index = 0;\nwhile (head !== null) {\nif (head.val === target) {\nreturn index;\n}\nhead = head.next;\nindex += 1;\n}\nreturn -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) {\nint index = 0;\nwhile (head) {\nif (head->val == target)\nreturn index;\nhead = head->next;\nindex++;\n}\nreturn -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) {\nint index = 0;\nwhile (head != null) {\nif (head.val == target)\nreturn index;\nhead = head.next;\nindex++;\n}\nreturn -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 {\nvar head: ListNode? = head\nvar index = 0\nwhile head != nil {\nif head?.val == target {\nreturn index\n}\nhead = head?.next\nindex += 1\n}\nreturn -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 {\nvar head = node;\nvar index: i32 = 0;\nwhile (head != null) {\nif (head.?.val == target) return index;\nhead = head.?.next;\nindex += 1;\n}\nreturn -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) {\nint index = 0;\nwhile (head != null) {\nif (head.val == target) {\nreturn index;\n}\nhead = head.next;\nindex++;\n}\nreturn -1;\n}\n
    "},{"location":"chapter_array_and_linkedlist/linked_list/#424","title":"4.2.4. \u00a0 \u5e38\u89c1\u94fe\u8868\u7c7b\u578b","text":"

    \u5355\u5411\u94fe\u8868\u3002\u5373\u4e0a\u8ff0\u4ecb\u7ecd\u7684\u666e\u901a\u94fe\u8868\u3002\u5355\u5411\u94fe\u8868\u7684\u8282\u70b9\u5305\u542b\u503c\u548c\u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\u4e24\u9879\u6570\u636e\u3002\u6211\u4eec\u5c06\u9996\u4e2a\u8282\u70b9\u79f0\u4e3a\u5934\u8282\u70b9\uff0c\u5c06\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u6210\u4e3a\u5c3e\u8282\u70b9\uff0c\u5c3e\u8282\u70b9\u6307\u5411 \\(\\text{null}\\) \u3002

    \u73af\u5f62\u94fe\u8868\u3002\u5982\u679c\u6211\u4eec\u4ee4\u5355\u5411\u94fe\u8868\u7684\u5c3e\u8282\u70b9\u6307\u5411\u5934\u8282\u70b9\uff08\u5373\u9996\u5c3e\u76f8\u63a5\uff09\uff0c\u5219\u5f97\u5230\u4e00\u4e2a\u73af\u5f62\u94fe\u8868\u3002\u5728\u73af\u5f62\u94fe\u8868\u4e2d\uff0c\u4efb\u610f\u8282\u70b9\u90fd\u53ef\u4ee5\u89c6\u4f5c\u5934\u8282\u70b9\u3002

    \u53cc\u5411\u94fe\u8868\u3002\u4e0e\u5355\u5411\u94fe\u8868\u76f8\u6bd4\uff0c\u53cc\u5411\u94fe\u8868\u8bb0\u5f55\u4e86\u4e24\u4e2a\u65b9\u5411\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\u3002\u53cc\u5411\u94fe\u8868\u7684\u8282\u70b9\u5b9a\u4e49\u540c\u65f6\u5305\u542b\u6307\u5411\u540e\u7ee7\u8282\u70b9\uff08\u4e0b\u4e00\u8282\u70b9\uff09\u548c\u524d\u9a71\u8282\u70b9\uff08\u4e0a\u4e00\u8282\u70b9\uff09\u7684\u6307\u9488\u3002\u76f8\u8f83\u4e8e\u5355\u5411\u94fe\u8868\uff0c\u53cc\u5411\u94fe\u8868\u66f4\u5177\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u671d\u4e24\u4e2a\u65b9\u5411\u904d\u5386\u94fe\u8868\uff0c\u4f46\u76f8\u5e94\u5730\u4e5f\u9700\u8981\u5360\u7528\u66f4\u591a\u7684\u5185\u5b58\u7a7a\u95f4\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nint val;        // \u8282\u70b9\u503c\nListNode next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode(int x) { val = x; }  // \u6784\u9020\u51fd\u6570\n}\n
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct ListNode {\nint val;         // \u8282\u70b9\u503c\nListNode *next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode *prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode(int x) : val(x), next(nullptr), prev(nullptr) {}  // \u6784\u9020\u51fd\u6570\n};\n
    class ListNode:\n\"\"\"\u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b\"\"\"\ndef __init__(self, val: int):\nself.val: int = val                   # \u8282\u70b9\u503c\nself.next: Optional[ListNode] = None  # \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nself.prev: Optional[ListNode] = None  # \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype DoublyListNode struct {\nVal  int             // \u8282\u70b9\u503c\nNext *DoublyListNode // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nPrev *DoublyListNode // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n}\n// NewDoublyListNode \u521d\u59cb\u5316\nfunc NewDoublyListNode(val int) *DoublyListNode {\nreturn &DoublyListNode{\nVal:  val,\nNext: nil,\nPrev: nil,\n}\n}\n
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nval;\nnext;\nprev;\nconstructor(val, next, prev) {\nthis.val = val  ===  undefined ? 0 : val;        // \u8282\u70b9\u503c\nthis.next = next  ===  undefined ? null : next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nthis.prev = prev  ===  undefined ? null : prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n}\n}\n
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nval: number;\nnext: ListNode | null;\nprev: ListNode | null;\nconstructor(val?: number, next?: ListNode | null, prev?: ListNode | null) {\nthis.val = val  ===  undefined ? 0 : val;        // \u8282\u70b9\u503c\nthis.next = next  ===  undefined ? null : next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nthis.prev = prev  ===  undefined ? null : prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n}\n}\n
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct ListNode {\nint val;               // \u8282\u70b9\u503c\nstruct ListNode *next; // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nstruct ListNode *prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n};\ntypedef struct ListNode ListNode;\n/* \u6784\u9020\u51fd\u6570 */\nListNode *newListNode(int val) {\nListNode *node, *next;\nnode = (ListNode *) malloc(sizeof(ListNode));\nnode->val = val;\nnode->next = NULL;\nnode->prev = NULL;\nreturn node;\n}\n
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nint val;        // \u8282\u70b9\u503c\nListNode next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode(int x) => val = x;  // \u6784\u9020\u51fd\u6570\n}\n
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nvar val: Int // \u8282\u70b9\u503c\nvar next: ListNode? // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nvar prev: ListNode? // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\ninit(x: Int) { // \u6784\u9020\u51fd\u6570\nval = x\n}\n}\n
    // \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b\npub fn ListNode(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nval: T = 0, // \u8282\u70b9\u503c\nnext: ?*Self = null, // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nprev: ?*Self = null, // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n// \u6784\u9020\u51fd\u6570\npub fn init(self: *Self, x: i32) void {\nself.val = x;\nself.next = null;\nself.prev = null;\n}\n};\n}\n
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nint val;        // \u8282\u70b9\u503c\nListNode next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode(this.val, [this.next, this.prev]);  // \u6784\u9020\u51fd\u6570\n}\n

    Fig. \u5e38\u89c1\u94fe\u8868\u79cd\u7c7b

    "},{"location":"chapter_array_and_linkedlist/list/","title":"4.3. \u00a0 \u5217\u8868","text":"

    \u6570\u7ec4\u957f\u5ea6\u4e0d\u53ef\u53d8\u5bfc\u81f4\u5b9e\u7528\u6027\u964d\u4f4e\u3002\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4e8b\u5148\u65e0\u6cd5\u786e\u5b9a\u9700\u8981\u5b58\u50a8\u591a\u5c11\u6570\u636e\uff0c\u8fd9\u4f7f\u6570\u7ec4\u957f\u5ea6\u7684\u9009\u62e9\u53d8\u5f97\u56f0\u96be\u3002\u82e5\u957f\u5ea6\u8fc7\u5c0f\uff0c\u9700\u8981\u5728\u6301\u7eed\u6dfb\u52a0\u6570\u636e\u65f6\u9891\u7e41\u6269\u5bb9\u6570\u7ec4\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\u51fa\u73b0\u4e86\u4e00\u79cd\u88ab\u79f0\u4e3a\u300c\u52a8\u6001\u6570\u7ec4 Dynamic Array\u300d\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5373\u957f\u5ea6\u53ef\u53d8\u7684\u6570\u7ec4\uff0c\u4e5f\u5e38\u88ab\u79f0\u4e3a\u300c\u5217\u8868 List\u300d\u3002\u5217\u8868\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\uff0c\u7ee7\u627f\u4e86\u6570\u7ec4\u7684\u4f18\u70b9\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u52a8\u6001\u6269\u5bb9\u3002\u5728\u5217\u8868\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u81ea\u7531\u6dfb\u52a0\u5143\u7d20\uff0c\u800c\u65e0\u9700\u62c5\u5fc3\u8d85\u8fc7\u5bb9\u91cf\u9650\u5236\u3002

    "},{"location":"chapter_array_and_linkedlist/list/#431","title":"4.3.1. \u00a0 \u5217\u8868\u5e38\u7528\u64cd\u4f5c","text":"

    \u521d\u59cb\u5316\u5217\u8868\u3002\u901a\u5e38\u6211\u4eec\u4f1a\u4f7f\u7528\u201c\u65e0\u521d\u59cb\u503c\u201d\u548c\u201c\u6709\u521d\u59cb\u503c\u201d\u7684\u4e24\u79cd\u521d\u59cb\u5316\u65b9\u6cd5\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart list.java
    /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<Integer> list1 = 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> list = new ArrayList<>(Arrays.asList(numbers));\n
    list.cpp
    /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u9700\u6ce8\u610f\uff0cC++ \u4e2d vector \u5373\u662f\u672c\u6587\u63cf\u8ff0\u7684 list\n// \u65e0\u521d\u59cb\u503c\nvector<int> list1;\n// \u6709\u521d\u59cb\u503c\nvector<int> list = { 1, 3, 2, 5, 4 };\n
    list.py
    # \u521d\u59cb\u5316\u5217\u8868\n# \u65e0\u521d\u59cb\u503c\nlist1: List[int] = []\n# \u6709\u521d\u59cb\u503c\nlist: List[int] = [1, 3, 2, 5, 4]\n
    list_test.go
    /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nlist1 := []int\n// \u6709\u521d\u59cb\u503c\nlist := []int{1, 3, 2, 5, 4}\n
    list.js
    /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nconst list1 = [];\n// \u6709\u521d\u59cb\u503c\nconst list = [1, 3, 2, 5, 4];\n
    list.ts
    /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nconst list1: number[] = [];\n// \u6709\u521d\u59cb\u503c\nconst list: number[] = [1, 3, 2, 5, 4];\n
    list.c
    // C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
    list.cs
    /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<int> list1 = new ();\n// \u6709\u521d\u59cb\u503c\nint[] numbers = new int[] { 1, 3, 2, 5, 4 };\nList<int> list = numbers.ToList();\n
    list.swift
    /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nlet list1: [Int] = []\n// \u6709\u521d\u59cb\u503c\nvar list = [1, 3, 2, 5, 4]\n
    list.zig
    // \u521d\u59cb\u5316\u5217\u8868\nvar list = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer list.deinit();\ntry list.appendSlice(&[_]i32{ 1, 3, 2, 5, 4 });\n
    list.dart
    /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<int> list1 = [];\n// \u6709\u521d\u59cb\u503c\nList<int> list = [1, 3, 2, 5, 4];\n

    \u8bbf\u95ee\u4e0e\u66f4\u65b0\u5143\u7d20\u3002\u7531\u4e8e\u5217\u8868\u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart list.java
    /* \u8bbf\u95ee\u5143\u7d20 */\nint num = list.get(1);  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist.set(1, 0);  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
    list.cpp
    /* \u8bbf\u95ee\u5143\u7d20 */\nint num = list[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
    list.py
    # \u8bbf\u95ee\u5143\u7d20\nnum: int = list[1]  # \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n# \u66f4\u65b0\u5143\u7d20\nlist[1] = 0    # \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
    list_test.go
    /* \u8bbf\u95ee\u5143\u7d20 */\nnum := list[1]  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0     // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
    list.js
    /* \u8bbf\u95ee\u5143\u7d20 */\nconst num = list[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
    list.ts
    /* \u8bbf\u95ee\u5143\u7d20 */\nconst num: number = list[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[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.cs
    /* \u8bbf\u95ee\u5143\u7d20 */\nint num = list[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
    list.swift
    /* \u8bbf\u95ee\u5143\u7d20 */\nlet num = list[1] // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0 // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
    list.zig
    // \u8bbf\u95ee\u5143\u7d20\nvar num = list.items[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n// \u66f4\u65b0\u5143\u7d20\nlist.items[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0  \n
    list.dart
    /* \u8bbf\u95ee\u5143\u7d20 */\nint num = list[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n

    \u5728\u5217\u8868\u4e2d\u6dfb\u52a0\u3001\u63d2\u5165\u3001\u5220\u9664\u5143\u7d20\u3002\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart list.java
    /* \u6e05\u7a7a\u5217\u8868 */\nlist.clear();\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.add(1);\nlist.add(3);\nlist.add(2);\nlist.add(5);\nlist.add(4);\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.add(3, 6);  // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n/* \u5220\u9664\u5143\u7d20 */\nlist.remove(3);  // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
    list.cpp
    /* \u6e05\u7a7a\u5217\u8868 */\nlist.clear();\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.push_back(1);\nlist.push_back(3);\nlist.push_back(2);\nlist.push_back(5);\nlist.push_back(4);\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.insert(list.begin() + 3, 6);  // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n/* \u5220\u9664\u5143\u7d20 */\nlist.erase(list.begin() + 3);      // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
    list.py
    # \u6e05\u7a7a\u5217\u8868\nlist.clear()\n# \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\nlist.append(1)\nlist.append(3)\nlist.append(2)\nlist.append(5)\nlist.append(4)\n# \u4e2d\u95f4\u63d2\u5165\u5143\u7d20\nlist.insert(3, 6)  # \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n# \u5220\u9664\u5143\u7d20\nlist.pop(3)        # \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
    list_test.go
    /* \u6e05\u7a7a\u5217\u8868 */\nlist = nil\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist = append(list, 1)\nlist = append(list, 3)\nlist = append(list, 2)\nlist = append(list, 5)\nlist = append(list, 4)\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist = append(list[:3], append([]int{6}, list[3:]...)...) // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n/* \u5220\u9664\u5143\u7d20 */\nlist = append(list[:3], list[4:]...) // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
    list.js
    /* \u6e05\u7a7a\u5217\u8868 */\nlist.length = 0;\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.push(1);\nlist.push(3);\nlist.push(2);\nlist.push(5);\nlist.push(4);\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.splice(3, 0, 6);\n/* \u5220\u9664\u5143\u7d20 */\nlist.splice(3, 1);\n
    list.ts
    /* \u6e05\u7a7a\u5217\u8868 */\nlist.length = 0;\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.push(1);\nlist.push(3);\nlist.push(2);\nlist.push(5);\nlist.push(4);\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.splice(3, 0, 6);\n/* \u5220\u9664\u5143\u7d20 */\nlist.splice(3, 1);\n
    list.c
    // C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
    list.cs
    /* \u6e05\u7a7a\u5217\u8868 */\nlist.Clear();\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.Add(1);\nlist.Add(3);\nlist.Add(2);\nlist.Add(5);\nlist.Add(4);\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.Insert(3, 6);\n/* \u5220\u9664\u5143\u7d20 */\nlist.RemoveAt(3);\n
    list.swift
    /* \u6e05\u7a7a\u5217\u8868 */\nlist.removeAll()\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.append(1)\nlist.append(3)\nlist.append(2)\nlist.append(5)\nlist.append(4)\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.insert(6, at: 3) // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n/* \u5220\u9664\u5143\u7d20 */\nlist.remove(at: 3) // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
    list.zig
    // \u6e05\u7a7a\u5217\u8868\nlist.clearRetainingCapacity();\n// \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\ntry list.append(1);\ntry list.append(3);\ntry list.append(2);\ntry list.append(5);\ntry list.append(4);\n// \u4e2d\u95f4\u63d2\u5165\u5143\u7d20\ntry list.insert(3, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n// \u5220\u9664\u5143\u7d20\n_ = list.orderedRemove(3); // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
    list.dart
    /* \u6e05\u7a7a\u5217\u8868 */\nlist.clear();\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.add(1);\nlist.add(3);\nlist.add(2);\nlist.add(5);\nlist.add(4);\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.insert(3, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n/* \u5220\u9664\u5143\u7d20 */\nlist.removeAt(3); // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n

    \u904d\u5386\u5217\u8868\u3002\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart list.java
    /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < list.size(); i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (int n : list) {\ncount++;\n}\n
    list.cpp
    /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < list.size(); i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (int n : list) {\ncount++;\n}\n
    list.py
    # \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868\ncount = 0\nfor i in range(len(list)):\ncount += 1\n# \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20\ncount = 0\nfor n in list:\ncount += 1\n
    list_test.go
    /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\ncount := 0\nfor i := 0; i < len(list); i++ {\ncount++\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0\nfor range list {\ncount++\n}\n
    list.js
    /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nlet count = 0;\nfor (let i = 0; i < list.length; i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (const n of list) {\ncount++;\n}\n
    list.ts
    /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nlet count = 0;\nfor (let i = 0; i < list.length; i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (const n of list) {\ncount++;\n}\n
    list.c
    // C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
    list.cs
    /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < list.Count(); i++)\n{\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nforeach (int n in list)\n{\ncount++;\n}\n
    list.swift
    /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nvar count = 0\nfor _ in list.indices {\ncount += 1\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0\nfor _ in list {\ncount += 1\n}\n
    list.zig
    // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868\nvar count: i32 = 0;\nvar i: i32 = 0;\nwhile (i < list.items.len) : (i += 1) {\ncount += 1;\n}\n// \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20\ncount = 0;\nfor (list.items) |_| {\ncount += 1;\n}\n
    list.dart
    /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < list.length; i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (int n in list) {\ncount++;\n}\n

    \u62fc\u63a5\u4e24\u4e2a\u5217\u8868\u3002\u7ed9\u5b9a\u4e00\u4e2a\u65b0\u5217\u8868 list1 \uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8be5\u5217\u8868\u62fc\u63a5\u5230\u539f\u5217\u8868\u7684\u5c3e\u90e8\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart list.java
    /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<Integer> list1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));\nlist.addAll(list1);  // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n
    list.cpp
    /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nvector<int> list1 = { 6, 8, 7, 10, 9 };\n// \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\nlist.insert(list.end(), list1.begin(), list1.end());\n
    list.py
    # \u62fc\u63a5\u4e24\u4e2a\u5217\u8868\nlist1: List[int] = [6, 8, 7, 10, 9]\nlist += list1  # \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n
    list_test.go
    /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nlist1 := []int{6, 8, 7, 10, 9}\nlist = append(list, list1...)  // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n
    list.js
    /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nconst list1 = [6, 8, 7, 10, 9];\nlist.push(...list1);  // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n
    list.ts
    /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nconst list1: number[] = [6, 8, 7, 10, 9];\nlist.push(...list1);  // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n
    list.c
    // C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
    list.cs
    /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<int> list1 = new() { 6, 8, 7, 10, 9 };\nlist.AddRange(list1);  // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n
    list.swift
    /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nlet list1 = [6, 8, 7, 10, 9]\nlist.append(contentsOf: list1) // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n
    list.zig
    // \u62fc\u63a5\u4e24\u4e2a\u5217\u8868\nvar list1 = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer list1.deinit();\ntry list1.appendSlice(&[_]i32{ 6, 8, 7, 10, 9 });\ntry list.insertSlice(list.items.len, list1.items); // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n
    list.dart
    /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<int> list1 = [6, 8, 7, 10, 9];\nlist.addAll(list1);  // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n

    \u6392\u5e8f\u5217\u8868\u3002\u6392\u5e8f\u4e5f\u662f\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\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\u5bdf\u7684\u300c\u4e8c\u5206\u67e5\u627e\u300d\u548c\u300c\u53cc\u6307\u9488\u300d\u7b97\u6cd5\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart list.java
    /* \u6392\u5e8f\u5217\u8868 */\nCollections.sort(list);  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
    list.cpp
    /* \u6392\u5e8f\u5217\u8868 */\nsort(list.begin(), list.end());  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
    list.py
    # \u6392\u5e8f\u5217\u8868\nlist.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(list)  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
    list.js
    /* \u6392\u5e8f\u5217\u8868 */  list.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 */\nlist.sort((a, b) => a - b);  // \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.cs
    /* \u6392\u5e8f\u5217\u8868 */\nlist.Sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
    list.swift
    /* \u6392\u5e8f\u5217\u8868 */\nlist.sort() // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
    list.zig
    // \u6392\u5e8f\u5217\u8868\nstd.sort.sort(i32, list.items, {}, comptime std.sort.asc(i32));\n
    list.dart
    /* \u6392\u5e8f\u5217\u8868 */\nlist.sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
    "},{"location":"chapter_array_and_linkedlist/list/#432","title":"4.3.2. \u00a0 \u5217\u8868\u5b9e\u73b0 *","text":"

    \u4e3a\u4e86\u5e2e\u52a9\u52a0\u6df1\u5bf9\u5217\u8868\u7684\u7406\u89e3\uff0c\u6211\u4eec\u5728\u6b64\u63d0\u4f9b\u4e00\u4e2a\u7b80\u6613\u7248\u5217\u8868\u5b9e\u73b0\u3002\u9700\u8981\u5173\u6ce8\u4e09\u4e2a\u6838\u5fc3\u70b9\uff1a

    • \u521d\u59cb\u5bb9\u91cf\uff1a\u9009\u53d6\u4e00\u4e2a\u5408\u7406\u7684\u6570\u7ec4\u521d\u59cb\u5bb9\u91cf\u3002\u5728\u672c\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9009\u62e9 10 \u4f5c\u4e3a\u521d\u59cb\u5bb9\u91cf\u3002
    • \u6570\u91cf\u8bb0\u5f55\uff1a\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf 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\u3002
    • \u6269\u5bb9\u673a\u5236\uff1a\u63d2\u5165\u5143\u7d20\u65f6\u53ef\u80fd\u8d85\u51fa\u5217\u8868\u5bb9\u91cf\uff0c\u6b64\u65f6\u9700\u8981\u6269\u5bb9\u5217\u8868\u3002\u6269\u5bb9\u65b9\u6cd5\u662f\u6839\u636e\u6269\u5bb9\u500d\u6570\u521b\u5efa\u4e00\u4e2a\u66f4\u5927\u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u5f53\u524d\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\u4f9d\u6b21\u79fb\u52a8\u81f3\u65b0\u6570\u7ec4\u3002\u5728\u672c\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u89c4\u5b9a\u6bcf\u6b21\u5c06\u6570\u7ec4\u6269\u5bb9\u81f3\u4e4b\u524d\u7684 2 \u500d\u3002

    \u672c\u793a\u4f8b\u65e8\u5728\u5e2e\u52a9\u8bfb\u8005\u76f4\u89c2\u7406\u89e3\u5217\u8868\u7684\u5de5\u4f5c\u673a\u5236\u3002\u5b9e\u9645\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u5217\u8868\u5b9e\u73b0\u66f4\u52a0\u6807\u51c6\u548c\u590d\u6742\uff0c\u5404\u4e2a\u53c2\u6570\u7684\u8bbe\u5b9a\u4e5f\u975e\u5e38\u6709\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart my_list.java
    /* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate int[] nums; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nprivate int capacity = 10; // \u5217\u8868\u5bb9\u91cf\nprivate int size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nprivate int extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n/* \u6784\u9020\u65b9\u6cd5 */\npublic MyList() {\nnums = new int[capacity];\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09 */\npublic int size() {\nreturn size;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\npublic int capacity() {\nreturn capacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\npublic int get(int index) {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index < 0 || index >= size)\nthrow new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\nreturn nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\npublic void set(int index, int num) {\nif (index < 0 || index >= size)\nthrow new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\nnums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\npublic void add(int num) {\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (size == capacity())\nextendCapacity();\nnums[size] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nsize++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\npublic void insert(int index, int num) {\nif (index < 0 || index >= size)\nthrow new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (size == capacity())\nextendCapacity();\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (int j = size - 1; j >= index; j--) {\nnums[j + 1] = nums[j];\n}\nnums[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nsize++;\n}\n/* \u5220\u9664\u5143\u7d20 */\npublic int remove(int index) {\nif (index < 0 || index >= size)\nthrow new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\nint num = nums[index];\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (int j = index; j < size - 1; j++) {\nnums[j] = nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nsize--;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\npublic 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\nnums = Arrays.copyOf(nums, capacity() * extendRatio);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\ncapacity = nums.length;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\npublic int[] toArray() {\nint size = size();\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nint[] nums = new int[size];\nfor (int i = 0; i < size; i++) {\nnums[i] = get(i);\n}\nreturn nums;\n}\n}\n
    my_list.cpp
    /* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate:\nint *nums;             // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nint numsCapacity = 10; // \u5217\u8868\u5bb9\u91cf\nint numsSize = 0;      // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nint extendRatio = 2;   // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nMyList() {\nnums = new int[numsCapacity];\n}\n/* \u6790\u6784\u65b9\u6cd5 */\n~MyList() {\ndelete[] nums;\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\nint size() {\nreturn numsSize;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nint capacity() {\nreturn numsCapacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\nint get(int index) {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index < 0 || index >= size())\nthrow out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\nreturn nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nvoid set(int index, int num) {\nif (index < 0 || index >= size())\nthrow out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\nnums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nvoid add(int num) {\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (size() == capacity())\nextendCapacity();\nnums[size()] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nnumsSize++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nvoid insert(int index, int num) {\nif (index < 0 || index >= size())\nthrow out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (size() == capacity())\nextendCapacity();\n// \u7d22\u5f15 i \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (int j = size() - 1; j >= index; j--) {\nnums[j + 1] = nums[j];\n}\nnums[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nnumsSize++;\n}\n/* \u5220\u9664\u5143\u7d20 */\nint remove(int index) {\nif (index < 0 || index >= size())\nthrow out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\nint num = nums[index];\n// \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (int j = index; j < size() - 1; j++) {\nnums[j] = nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nnumsSize--;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\nvoid extendCapacity() {\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\nint newCapacity = capacity() * extendRatio;\nint *tmp = nums;\nnums = new int[newCapacity];\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor (int i = 0; i < size(); i++) {\nnums[i] = tmp[i];\n}\n// \u91ca\u653e\u5185\u5b58\ndelete[] tmp;\nnumsCapacity = newCapacity;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a Vector \u7528\u4e8e\u6253\u5370 */\nvector<int> toVector() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvector<int> vec(size());\nfor (int i = 0; i < size(); i++) {\nvec[i] = nums[i];\n}\nreturn vec;\n}\n};\n
    my_list.py
    class MyList:\n\"\"\"\u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0\"\"\"\ndef __init__(self):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.__capacity: int = 10  # \u5217\u8868\u5bb9\u91cf\nself.__nums: list[int] = [0] * self.__capacity  # \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nself.__size: int = 0  # \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nself.__extend_ratio: int = 2  # \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\ndef size(self) -> int:\n\"\"\"\u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\"\"\"\nreturn self.__size\ndef capacity(self) -> int:\n\"\"\"\u83b7\u53d6\u5217\u8868\u5bb9\u91cf\"\"\"\nreturn self.__capacity\ndef 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\nif index < 0 or index >= self.__size:\nraise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\nreturn self.__nums[index]\ndef set(self, num: int, index: int) -> None:\n\"\"\"\u66f4\u65b0\u5143\u7d20\"\"\"\nif index < 0 or index >= self.__size:\nraise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\nself.__nums[index] = num\ndef add(self, num: int) -> None:\n\"\"\"\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\"\"\"\n# \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif self.size() == self.capacity():\nself.extend_capacity()\nself.__nums[self.__size] = num\nself.__size += 1\ndef insert(self, num: int, index: int) -> None:\n\"\"\"\u4e2d\u95f4\u63d2\u5165\u5143\u7d20\"\"\"\nif index < 0 or index >= self.__size:\nraise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n# \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif self.__size == self.capacity():\nself.extend_capacity()\n# \u7d22\u5f15 i \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor j in range(self.__size - 1, index - 1, -1):\nself.__nums[j + 1] = self.__nums[j]\nself.__nums[index] = num\n# \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.__size += 1\ndef remove(self, index: int) -> int:\n\"\"\"\u5220\u9664\u5143\u7d20\"\"\"\nif index < 0 or index >= self.__size:\nraise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\nnum = self.__nums[index]\n# \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor j in range(index, self.__size - 1):\nself.__nums[j] = self.__nums[j + 1]\n# \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.__size -= 1\n# \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num\ndef extend_capacity(self) -> None:\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\nself.__nums = self.__nums + [0] * self.capacity() * (self.__extend_ratio - 1)\n# \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nself.__capacity = len(self.__nums)\ndef to_array(self) -> list[int]:\n\"\"\"\u8fd4\u56de\u6709\u6548\u957f\u5ea6\u7684\u5217\u8868\"\"\"\nreturn self.__nums[: self.__size]\n
    my_list.go
    /* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\ntype myList struct {\nnumsCapacity int\nnums         []int\nnumsSize     int\nextendRatio  int\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newMyList() *myList {\nreturn &myList{\nnumsCapacity: 10,              // \u5217\u8868\u5bb9\u91cf\nnums:         make([]int, 10), // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nnumsSize:     0,               // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nextendRatio:  2,               // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n}\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09 */\nfunc (l *myList) size() int {\nreturn l.numsSize\n}\n/*  \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nfunc (l *myList) capacity() int {\nreturn l.numsCapacity\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\nif index < 0 || index >= l.numsSize {\npanic(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nreturn l.nums[index]\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nfunc (l *myList) set(num, index int) {\nif index < 0 || index >= l.numsSize {\npanic(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nl.nums[index] = num\n}\n/* \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\nif l.numsSize == l.numsCapacity {\nl.extendCapacity()\n}\nl.nums[l.numsSize] = num\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nl.numsSize++\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nfunc (l *myList) insert(num, index int) {\nif index < 0 || index >= l.numsSize {\npanic(\"\u7d22\u5f15\u8d8a\u754c\")\n}\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif l.numsSize == l.numsCapacity {\nl.extendCapacity()\n}\n// \u7d22\u5f15 i \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor j := l.numsSize - 1; j >= index; j-- {\nl.nums[j+1] = l.nums[j]\n}\nl.nums[index] = num\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nl.numsSize++\n}\n/* \u5220\u9664\u5143\u7d20 */\nfunc (l *myList) remove(index int) int {\nif index < 0 || index >= l.numsSize {\npanic(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nnum := l.nums[index]\n// \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor j := index; j < l.numsSize-1; j++ {\nl.nums[j] = l.nums[j+1]\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nl.numsSize--\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num\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\nl.nums = append(l.nums, make([]int, l.numsCapacity*(l.extendRatio-1))...)\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nl.numsCapacity = len(l.nums)\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\nreturn l.nums[:l.numsSize]\n}\n
    my_list.js
    /* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\n#nums = 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\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n#extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor() {\nthis.#nums = new Array(this.#capacity);\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\nsize() {\nreturn this.#size;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\ncapacity() {\nreturn this.#capacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\nget(index) {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\nreturn this.#nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nset(index, num) {\nif (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\nthis.#nums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nadd(num) {\n// \u5982\u679c\u957f\u5ea6\u7b49\u4e8e\u5bb9\u91cf\uff0c\u5219\u9700\u8981\u6269\u5bb9\nif (this.#size === this.#capacity) {\nthis.extendCapacity();\n}\n// \u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u5c3e\u90e8\nthis.#nums[this.#size] = num;\nthis.#size++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\ninsert(index, num) {\nif (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\nif (this.#size === this.#capacity) {\nthis.extendCapacity();\n}\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (let j = this.#size - 1; j >= index; j--) {\nthis.#nums[j + 1] = this.#nums[j];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nthis.#nums[index] = num;\nthis.#size++;\n}\n/* \u5220\u9664\u5143\u7d20 */\nremove(index) {\nif (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\nlet num = this.#nums[index];\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (let j = index; j < this.#size - 1; j++) {\nthis.#nums[j] = this.#nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nthis.#size--;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\nextendCapacity() {\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\nthis.#nums = this.#nums.concat(\nnew Array(this.capacity() * (this.#extendRatio - 1))\n);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nthis.#capacity = this.#nums.length;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\ntoArray() {\nlet size = this.size();\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nconst nums = new Array(size);\nfor (let i = 0; i < size; i++) {\nnums[i] = this.get(i);\n}\nreturn nums;\n}\n}\n
    my_list.ts
    /* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate nums: Array<number>; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nprivate _capacity: number = 10; // \u5217\u8868\u5bb9\u91cf\nprivate _size: number = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nprivate extendRatio: number = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor() {\nthis.nums = new Array(this._capacity);\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\npublic size(): number {\nreturn this._size;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\npublic capacity(): number {\nreturn this._capacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\npublic get(index: number): number {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\nreturn this.nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\npublic set(index: number, num: number): void {\nif (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\nthis.nums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\npublic add(num: number): void {\n// \u5982\u679c\u957f\u5ea6\u7b49\u4e8e\u5bb9\u91cf\uff0c\u5219\u9700\u8981\u6269\u5bb9\nif (this._size === this._capacity) this.extendCapacity();\n// \u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u5c3e\u90e8\nthis.nums[this._size] = num;\nthis._size++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\npublic insert(index: number, num: number): void {\nif (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\nif (this._size === this._capacity) {\nthis.extendCapacity();\n}\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (let j = this._size - 1; j >= index; j--) {\nthis.nums[j + 1] = this.nums[j];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nthis.nums[index] = num;\nthis._size++;\n}\n/* \u5220\u9664\u5143\u7d20 */\npublic remove(index: number): number {\nif (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\nlet num = this.nums[index];\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (let j = index; j < this._size - 1; j++) {\nthis.nums[j] = this.nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nthis._size--;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\npublic 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\nthis.nums = this.nums.concat(\nnew Array(this.capacity() * (this.extendRatio - 1))\n);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nthis._capacity = this.nums.length;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\npublic toArray(): number[] {\nlet size = this.size();\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nconst nums = new Array(size);\nfor (let i = 0; i < size; i++) {\nnums[i] = this.get(i);\n}\nreturn nums;\n}\n}\n
    my_list.c
    /* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nstruct myList {\nint *nums;       // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nint capacity;    // \u5217\u8868\u5bb9\u91cf\nint size;        // \u5217\u8868\u5927\u5c0f\nint extendRatio; // \u5217\u8868\u6bcf\u6b21\u6269\u5bb9\u7684\u500d\u6570\n};\ntypedef struct myList myList;\n/* \u6784\u9020\u51fd\u6570 */\nmyList *newMyList() {\nmyList *list = malloc(sizeof(myList));\nlist->capacity = 10;\nlist->nums = malloc(sizeof(int) * list->capacity);\nlist->size = 0;\nlist->extendRatio = 2;\nreturn list;\n}\n/* \u6790\u6784\u51fd\u6570 */\nvoid delMyList(myList *list) {\nfree(list->nums);\nfree(list);\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6 */\nint size(myList *list) {\nreturn list->size;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nint capacity(myList *list) {\nreturn list->capacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\nint get(myList *list, int index) {\nassert(index >= 0 && index < list->size);\nreturn list->nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nvoid set(myList *list, int index, int num) {\nassert(index >= 0 && index < list->size);\nlist->nums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nvoid add(myList *list, int num) {\nif (size(list) == capacity(list)) {\nextendCapacity(list); // \u6269\u5bb9\n}\nlist->nums[size(list)] = num;\nlist->size++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nvoid insert(myList *list, int index, int num) {\nassert(index >= 0 && index < size(list));\nfor (int i = size(list); i > index; --i) {\nlist->nums[i] = list->nums[i - 1];\n}\nlist->nums[index] = num;\nlist->size++;\n}\n/* \u5220\u9664\u5143\u7d20 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nint removeNum(myList *list, int index) {\nassert(index >= 0 && index < size(list));\nint num = list->nums[index];\nfor (int i = index; i < size(list) - 1; i++) {\nlist->nums[i] = list->nums[i + 1];\n}\nlist->size--;\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\nvoid extendCapacity(myList *list) {\n// \u5148\u5206\u914d\u7a7a\u95f4\nint newCapacity = capacity(list) * list->extendRatio;\nint *extend = (int *)malloc(sizeof(int) * newCapacity);\nint *temp = list->nums;\n// \u62f7\u8d1d\u65e7\u6570\u636e\u5230\u65b0\u6570\u636e\nfor (int i = 0; i < size(list); i++)\nextend[i] = list->nums[i];\n// \u91ca\u653e\u65e7\u6570\u636e\nfree(temp);\n// \u66f4\u65b0\u65b0\u6570\u636e\nlist->nums = extend;\nlist->capacity = newCapacity;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a Array \u7528\u4e8e\u6253\u5370 */\nint *toArray(myList *list) {\nreturn list->nums;\n}\n
    my_list.cs
    /* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate int[] nums;           // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nprivate int numsCapacity = 10;    // \u5217\u8868\u5bb9\u91cf\nprivate int numsSize = 0;         // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nprivate int extendRatio = 2;  // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n/* \u6784\u9020\u65b9\u6cd5 */\npublic MyList() {\nnums = new int[numsCapacity];\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\npublic int size() {\nreturn numsSize;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\npublic int capacity() {\nreturn numsCapacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\npublic int get(int index) {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index < 0 || index >= numsSize)\nthrow new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\nreturn nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\npublic void set(int index, int num) {\nif (index < 0 || index >= numsSize)\nthrow new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\nnums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\npublic void add(int num) {\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (numsSize == numsCapacity)\nextendCapacity();\nnums[numsSize] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nnumsSize++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\npublic void insert(int index, int num) {\nif (index < 0 || index >= numsSize)\nthrow new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (numsSize == numsCapacity)\nextendCapacity();\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (int j = numsSize - 1; j >= index; j--) {\nnums[j + 1] = nums[j];\n}\nnums[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nnumsSize++;\n}\n/* \u5220\u9664\u5143\u7d20 */\npublic int remove(int index) {\nif (index < 0 || index >= numsSize)\nthrow new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\nint num = nums[index];\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (int j = index; j < numsSize - 1; j++) {\nnums[j] = nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nnumsSize--;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\npublic void extendCapacity() {\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a numsCapacity * extendRatio \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nSystem.Array.Resize(ref nums, numsCapacity * extendRatio);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nnumsCapacity = nums.Length;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\npublic int[] toArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nint[] nums = new int[numsSize];\nfor (int i = 0; i < numsSize; i++) {\nnums[i] = get(i);\n}\nreturn nums;\n}\n}\n
    my_list.swift
    /* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate var nums: [Int] // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nprivate var _capacity = 10 // \u5217\u8868\u5bb9\u91cf\nprivate var _size = 0 // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nprivate let extendRatio = 2 // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n/* \u6784\u9020\u65b9\u6cd5 */\ninit() {\nnums = Array(repeating: 0, count: _capacity)\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\nfunc size() -> Int {\n_size\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nfunc capacity() -> Int {\n_capacity\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\nfunc get(index: Int) -> Int {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u9519\u8bef\uff0c\u4e0b\u540c\nif index < 0 || index >= _size {\nfatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nreturn nums[index]\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nfunc set(index: Int, num: Int) {\nif index < 0 || index >= _size {\nfatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nnums[index] = num\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nfunc add(num: Int) {\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif _size == _capacity {\nextendCapacity()\n}\nnums[_size] = num\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n_size += 1\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nfunc insert(index: Int, num: Int) {\nif index < 0 || index >= _size {\nfatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n}\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif _size == _capacity {\nextendCapacity()\n}\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor j in sequence(first: _size - 1, next: { $0 >= index + 1 ? $0 - 1 : nil }) {\nnums[j + 1] = nums[j]\n}\nnums[index] = num\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n_size += 1\n}\n/* \u5220\u9664\u5143\u7d20 */\n@discardableResult\nfunc remove(index: Int) -> Int {\nif index < 0 || index >= _size {\nfatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nlet num = nums[index]\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor j in index ..< (_size - 1) {\nnums[j] = nums[j + 1]\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n_size -= 1\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num\n}\n/* \u5217\u8868\u6269\u5bb9 */\nfunc 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\nnums = nums + Array(repeating: 0, count: _capacity * (extendRatio - 1))\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n_capacity = nums.count\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\nfunc toArray() -> [Int] {\nvar nums = Array(repeating: 0, count: _size)\nfor i in 0 ..< _size {\nnums[i] = get(index: i)\n}\nreturn nums\n}\n}\n
    my_list.zig
    // \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0\nfn MyList(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nnums: []T = undefined,                        // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nnums_capacity: usize = 10,                     // \u5217\u8868\u5bb9\u91cf\nnum_size: usize = 0,                           // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nextend_ratio: usize = 2,                       // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\nmem_arena: ?std.heap.ArenaAllocator = null,\nmem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n// \u6784\u9020\u65b9\u6cd5\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u5217\u8868\uff09\npub fn init(self: *Self, allocator: std.mem.Allocator) !void {\nif (self.mem_arena == null) {\nself.mem_arena = std.heap.ArenaAllocator.init(allocator);\nself.mem_allocator = self.mem_arena.?.allocator();\n}\nself.nums = try self.mem_allocator.alloc(T, self.nums_capacity);\nstd.mem.set(T, self.nums, @as(T, 0));\n}\n// \u6790\u6784\u65b9\u6cd5\uff08\u91ca\u653e\u5185\u5b58\uff09\npub fn deinit(self: *Self) void {\nif (self.mem_arena == null) return;\nself.mem_arena.?.deinit();\n}\n// \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\npub fn size(self: *Self) usize {\nreturn self.num_size;\n}\n// \u83b7\u53d6\u5217\u8868\u5bb9\u91cf\npub fn capacity(self: *Self) usize {\nreturn self.nums_capacity;\n}\n// \u8bbf\u95ee\u5143\u7d20\npub fn get(self: *Self, index: usize) T {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\nreturn self.nums[index];\n}  // \u66f4\u65b0\u5143\u7d20\npub fn set(self: *Self, index: usize, num: T) void {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\nself.nums[index] = num;\n}  // \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\npub fn add(self: *Self, num: T) !void {\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (self.size() == self.capacity()) try self.extendCapacity();\nself.nums[self.size()] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.num_size += 1;\n}  // \u4e2d\u95f4\u63d2\u5165\u5143\u7d20\npub fn insert(self: *Self, index: usize, num: T) !void {\nif (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\nif (self.size() == self.capacity()) try self.extendCapacity();\n// \u7d22\u5f15 i \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nvar j = self.size() - 1;\nwhile (j >= index) : (j -= 1) {\nself.nums[j + 1] = self.nums[j];\n}\nself.nums[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.num_size += 1;\n}\n// \u5220\u9664\u5143\u7d20\npub fn remove(self: *Self, index: usize) T {\nif (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\nvar num = self.nums[index];\n// \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nvar j = index;\nwhile (j < self.size() - 1) : (j += 1) {\nself.nums[j] = self.nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.num_size -= 1;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n// \u5217\u8868\u6269\u5bb9\npub fn extendCapacity(self: *Self) !void {\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\nvar newCapacity = self.capacity() * self.extend_ratio;\nvar extend = try self.mem_allocator.alloc(T, newCapacity);\nstd.mem.set(T, extend, @as(T, 0));\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nstd.mem.copy(T, extend, self.nums);\nself.nums = extend;\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nself.nums_capacity = newCapacity;\n}\n// \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\npub fn toArray(self: *Self) ![]T {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvar nums = try self.mem_allocator.alloc(T, self.size());\nstd.mem.set(T, nums, @as(T, 0));\nfor (nums) |*num, i| {\nnum.* = self.get(i);\n}\nreturn nums;\n}\n};\n}\n
    my_list.dart
    /* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nlate List<int> _nums; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nint _capacity = 10; // \u5217\u8868\u5bb9\u91cf\nint _size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nint _extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n/* \u6784\u9020\u65b9\u6cd5 */\nMyList() {\n_nums = List.filled(_capacity, 0);\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\nint size() => _size;\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nint capacity() => _capacity;\n/* \u8bbf\u95ee\u5143\u7d20 */\nint get(int index) {\nif (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\nreturn _nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nvoid set(int index, int num) {\nif (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n_nums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nvoid add(int num) {\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (_size == _capacity) extendCapacity();\n_nums[_size] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n_size++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nvoid insert(int index, int num) {\nif (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\nif (_size == _capacity) extendCapacity();\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (var j = _size - 1; j >= index; j--) {\n_nums[j + 1] = _nums[j];\n}\n_nums[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n_size++;\n}\n/* \u5220\u9664\u5143\u7d20 */\nint remove(int index) {\nif (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\nint num = _nums[index];\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (var j = index; j < _size - 1; j++) {\n_nums[j] = _nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n_size--;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\nvoid extendCapacity() {\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 _extendRatio \u500d\u7684\u65b0\u6570\u7ec4\nfinal _newNums = List.filled(_capacity * _extendRatio, 0);\n// \u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nList.copyRange(_newNums, 0, _nums);\n// \u66f4\u65b0 _nums \u7684\u5f15\u7528\n_nums = _newNums;\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n_capacity = _nums.length;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\nList<int> toArray() {\nList<int> nums = [];\nfor (var i = 0; i < _size; i++) {\nnums.add(get(i));\n}\nreturn nums;\n}\n}\n
    "},{"location":"chapter_array_and_linkedlist/summary/","title":"4.4. \u00a0 \u5c0f\u7ed3","text":"
    • \u6570\u7ec4\u548c\u94fe\u8868\u662f\u4e24\u79cd\u57fa\u672c\u6570\u636e\u7ed3\u6784\uff0c\u5206\u522b\u4ee3\u8868\u6570\u636e\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u548c\u79bb\u6563\u7a7a\u95f4\u5b58\u50a8\u65b9\u5f0f\u3002\u4e24\u8005\u7684\u4f18\u7f3a\u70b9\u5448\u73b0\u51fa\u4e92\u8865\u7684\u7279\u6027\u3002
    • \u6570\u7ec4\u652f\u6301\u968f\u673a\u8bbf\u95ee\u3001\u5360\u7528\u5185\u5b58\u8f83\u5c11\uff1b\u4f46\u63d2\u5165\u548c\u5220\u9664\u5143\u7d20\u6548\u7387\u4f4e\uff0c\u4e14\u521d\u59cb\u5316\u540e\u957f\u5ea6\u4e0d\u53ef\u53d8\u3002
    • \u94fe\u8868\u901a\u8fc7\u66f4\u6539\u6307\u9488\u5b9e\u73b0\u9ad8\u6548\u7684\u8282\u70b9\u63d2\u5165\u4e0e\u5220\u9664\uff0c\u4e14\u53ef\u4ee5\u7075\u6d3b\u8c03\u6574\u957f\u5ea6\uff1b\u4f46\u8282\u70b9\u8bbf\u95ee\u6548\u7387\u4f4e\u3001\u5360\u7528\u5185\u5b58\u8f83\u591a\u3002\u5e38\u89c1\u7684\u94fe\u8868\u7c7b\u578b\u5305\u62ec\u5355\u5411\u94fe\u8868\u3001\u5faa\u73af\u94fe\u8868\u3001\u53cc\u5411\u94fe\u8868\u3002
    • \u52a8\u6001\u6570\u7ec4\uff0c\u53c8\u79f0\u5217\u8868\uff0c\u662f\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u4e00\u79cd\u6570\u636e\u7ed3\u6784\u3002\u5b83\u4fdd\u7559\u4e86\u6570\u7ec4\u7684\u4f18\u52bf\uff0c\u540c\u65f6\u53ef\u4ee5\u7075\u6d3b\u8c03\u6574\u957f\u5ea6\u3002\u5217\u8868\u7684\u51fa\u73b0\u6781\u5927\u5730\u63d0\u9ad8\u4e86\u6570\u7ec4\u7684\u6613\u7528\u6027\uff0c\u4f46\u53ef\u80fd\u5bfc\u81f4\u90e8\u5206\u5185\u5b58\u7a7a\u95f4\u6d6a\u8d39\u3002
    • \u4e0b\u8868\u603b\u7ed3\u5e76\u5bf9\u6bd4\u4e86\u6570\u7ec4\u4e0e\u94fe\u8868\u7684\u5404\u9879\u7279\u6027\u4e0e\u64cd\u4f5c\u6548\u7387\u3002
    \u6570\u7ec4 \u94fe\u8868 \u5b58\u50a8\u65b9\u5f0f \u8fde\u7eed\u5185\u5b58\u7a7a\u95f4 \u79bb\u6563\u5185\u5b58\u7a7a\u95f4 \u6570\u636e\u7ed3\u6784\u957f\u5ea6 \u957f\u5ea6\u4e0d\u53ef\u53d8 \u957f\u5ea6\u53ef\u53d8 \u5185\u5b58\u4f7f\u7528\u7387 \u5360\u7528\u5185\u5b58\u5c11\u3001\u7f13\u5b58\u5c40\u90e8\u6027\u597d \u5360\u7528\u5185\u5b58\u591a \u4f18\u52bf\u64cd\u4f5c \u968f\u673a\u8bbf\u95ee \u63d2\u5165\u3001\u5220\u9664 \u8bbf\u95ee\u5143\u7d20 \\(O(1)\\) \\(O(N)\\) \u6dfb\u52a0\u5143\u7d20 \\(O(N)\\) \\(O(1)\\) \u5220\u9664\u5143\u7d20 \\(O(N)\\) \\(O(1)\\)

    \u7f13\u5b58\u5c40\u90e8\u6027

    \u5728\u8ba1\u7b97\u673a\u4e2d\uff0c\u6570\u636e\u8bfb\u5199\u901f\u5ea6\u6392\u5e8f\u662f\u201c\u786c\u76d8 < \u5185\u5b58 < CPU \u7f13\u5b58\u201d\u3002\u5f53\u6211\u4eec\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u65f6\uff0c\u8ba1\u7b97\u673a\u4e0d\u4ec5\u4f1a\u52a0\u8f7d\u5b83\uff0c\u8fd8\u4f1a\u7f13\u5b58\u5176\u5468\u56f4\u7684\u5176\u4ed6\u6570\u636e\uff0c\u4ece\u800c\u501f\u52a9\u9ad8\u901f\u7f13\u5b58\u6765\u63d0\u5347\u540e\u7eed\u64cd\u4f5c\u7684\u6267\u884c\u901f\u5ea6\u3002\u94fe\u8868\u5219\u4e0d\u7136\uff0c\u8ba1\u7b97\u673a\u53ea\u80fd\u6328\u4e2a\u5730\u7f13\u5b58\u5404\u4e2a\u8282\u70b9\uff0c\u8fd9\u6837\u7684\u591a\u6b21\u201c\u642c\u8fd0\u201d\u964d\u4f4e\u4e86\u6574\u4f53\u6548\u7387\u3002

    "},{"location":"chapter_array_and_linkedlist/summary/#441-q-a","title":"4.4.1. \u00a0 Q & A","text":"

    \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

    \u6808\u5185\u5b58\u5206\u914d\u7531\u7f16\u8bd1\u5668\u81ea\u52a8\u5b8c\u6210\uff0c\u800c\u5806\u5185\u5b58\u7531\u7a0b\u5e8f\u5458\u5728\u4ee3\u7801\u4e2d\u5206\u914d\uff08\u6ce8\u610f\uff0c\u8fd9\u91cc\u7684\u6808\u548c\u5806\u548c\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u6808\u548c\u5806\u4e0d\u662f\u540c\u4e00\u6982\u5ff5\uff09\u3002

    1. \u6808\u4e0d\u7075\u6d3b\uff0c\u5206\u914d\u7684\u5185\u5b58\u5927\u5c0f\u4e0d\u53ef\u66f4\u6539\uff1b\u5806\u76f8\u5bf9\u7075\u6d3b\uff0c\u53ef\u4ee5\u52a8\u6001\u5206\u914d\u5185\u5b58\uff1b
    2. \u6808\u662f\u4e00\u5757\u6bd4\u8f83\u5c0f\u7684\u5185\u5b58\uff0c\u5bb9\u6613\u51fa\u73b0\u5185\u5b58\u4e0d\u8db3\uff1b\u5806\u5185\u5b58\u5f88\u5927\uff0c\u4f46\u662f\u7531\u4e8e\u662f\u52a8\u6001\u5206\u914d\uff0c\u5bb9\u6613\u788e\u7247\u5316\uff0c\u7ba1\u7406\u5806\u5185\u5b58\u7684\u96be\u5ea6\u66f4\u5927\u3001\u6210\u672c\u66f4\u9ad8\uff1b
    3. \u8bbf\u95ee\u6808\u6bd4\u8bbf\u95ee\u5806\u66f4\u5feb\uff0c\u56e0\u4e3a\u6808\u5185\u5b58\u8f83\u5c0f\u3001\u5bf9\u7f13\u5b58\u53cb\u597d\uff0c\u5806\u5e27\u5206\u6563\u5728\u5f88\u5927\u7684\u7a7a\u95f4\u5185\uff0c\u4f1a\u51fa\u73b0\u66f4\u591a\u7684\u7f13\u5b58\u672a\u547d\u4e2d\uff1b

    \u4e3a\u4ec0\u4e48\u6570\u7ec4\u4f1a\u5f3a\u8c03\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\u7ed3\u70b9\u7ec4\u6210\uff0c\u7ed3\u70b9\u4e4b\u95f4\u7531\u6307\u9488\u8fde\u63a5\uff0c\u5404\u4e2a\u7ed3\u70b9\u53ef\u4ee5\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u4f8b\u5982 int, double, string, object \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\u5982\u679c\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\u90a3\u4e48\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 elementLength \u3002

    // \u5143\u7d20\u5185\u5b58\u5730\u5740 = \u6570\u7ec4\u5185\u5b58\u5730\u5740 + \u5143\u7d20\u957f\u5ea6 * \u5143\u7d20\u7d22\u5f15\nelementAddr = firtstElementAddr + elementLength * elementIndex\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\u7ed3\u70b9\u904d\u5386\u5230\u5c3e\u7ed3\u70b9\u5df2\u7ecf\u9047\u4e0d\u5230 P \u4e86\u3002\u8fd9\u610f\u5473\u7740\u7ed3\u70b9 P \u5df2\u7ecf\u4ece\u94fe\u8868\u4e2d\u5220\u9664\u4e86\uff0c\u6b64\u65f6\u7ed3\u70b9 P \u6307\u5411\u54ea\u91cc\u90fd\u4e0d\u4f1a\u5bf9\u8fd9\u6761\u94fe\u8868\u4ea7\u751f\u5f71\u54cd\u4e86\u3002

    \u4ece\u5783\u573e\u56de\u6536\u7684\u89d2\u5ea6\u770b\uff0c\u5bf9\u4e8e Java, Python, Go \u7b49\u62e5\u6709\u81ea\u52a8\u5783\u573e\u56de\u6536\u7684\u8bed\u8a00\u6765\u8bf4\uff0c\u8282\u70b9 P \u662f\u5426\u88ab\u56de\u6536\u53d6\u51b3\u4e8e\u662f\u5426\u6709\u4ecd\u5b58\u5728\u6307\u5411\u5b83\u7684\u5f15\u7528\uff0c\u800c\u4e0d\u662f P.next \u7684\u503c\u3002\u5728 C, 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)\\) \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\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\u7ed3\u70b9\u3001\u5c3e\u7ed3\u70b9\uff0c\u6bcf\u6b21\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u90fd\u662f \\(O(1)\\) \u3002

    \u56fe\u7247\u201c\u94fe\u8868\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f\u201d\u4e2d\uff0c\u6d45\u84dd\u8272\u7684\u5b58\u50a8\u7ed3\u70b9\u6307\u9488\u662f\u5360\u7528\u4e00\u5757\u5185\u5b58\u5730\u5740\u5417\uff1f\u8fd8\u662f\u548c\u7ed3\u70b9\u503c\u5404\u5360\u4e00\u534a\u5462\uff1f

    \u6587\u4e2d\u53ea\u662f\u4e00\u4e2a\u793a\u610f\u56fe\uff0c\u53ea\u662f\u5b9a\u6027\u8868\u793a\u3002\u5b9a\u91cf\u7684\u8bdd\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u5206\u6790\uff1a

    • \u4e0d\u540c\u7c7b\u578b\u7684\u7ed3\u70b9\u503c\u5360\u7528\u7684\u7a7a\u95f4\u662f\u4e0d\u540c\u7684\uff0c\u6bd4\u5982 int, long, double, \u6216\u8005\u662f\u7c7b\u7684\u5b9e\u4f8b\u7b49\u7b49\u3002
    • \u6307\u9488\u53d8\u91cf\u5360\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u5927\u5c0f\u6839\u636e\u6240\u4f7f\u7528\u7684\u64cd\u4f5c\u7cfb\u7edf\u53ca\u7f16\u8bd1\u73af\u5883\u800c\u5b9a\uff0c\u5927\u591a\u4e3a 8 \u5b57\u8282\u6216 4 \u5b57\u8282\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\u5927\u5927\u63d0\u5347\u4e86\u6570\u7ec4\u7684\u5b9e\u7528\u6027\uff0c\u4f46\u526f\u4f5c\u7528\u662f\u4f1a\u9020\u6210\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\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u4e3a\u4e86\u9632\u6b62\u9891\u7e41\u6269\u5bb9\uff0c\u6269\u5bb9\u4e00\u822c\u90fd\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

    "},{"location":"chapter_backtracking/","title":"12. \u00a0 \u56de\u6eaf","text":""},{"location":"chapter_backtracking/backtracking_algorithm/","title":"12.1. \u00a0 \u56de\u6eaf\u7b97\u6cd5","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\u300c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u300d\u6765\u904d\u5386\u89e3\u7a7a\u95f4\u3002\u5728\u4e8c\u53c9\u6811\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\u4e0b\u9762\uff0c\u6211\u4eec\u4ece\u4e8c\u53c9\u6811\u7684\u524d\u5e8f\u904d\u5386\u5165\u624b\uff0c\u9010\u6b65\u4e86\u89e3\u56de\u6eaf\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u3002

    \u4f8b\u9898\u4e00

    \u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811\uff0c\u641c\u7d22\u5e76\u8bb0\u5f55\u6240\u6709\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\uff0c\u8fd4\u56de\u8282\u70b9\u5217\u8868\u3002

    \u89e3\u9898\u601d\u8def\uff1a\u524d\u5e8f\u904d\u5386\u8fd9\u9897\u6811\uff0c\u5e76\u5224\u65ad\u5f53\u524d\u8282\u70b9\u7684\u503c\u662f\u5426\u4e3a \\(7\\) \uff0c\u82e5\u662f\u5219\u5c06\u8be5\u8282\u70b9\u7684\u503c\u52a0\u5165\u5230\u7ed3\u679c\u5217\u8868 res \u4e4b\u4e2d\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart preorder_traversal_i_compact.java
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode root) {\nif (root == null) {\nreturn;\n}\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.add(root);\n}\npreOrder(root.left);\npreOrder(root.right);\n}\n
    preorder_traversal_i_compact.cpp
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode *root) {\nif (root == nullptr) {\nreturn;\n}\nif (root->val == 7) {\n// \u8bb0\u5f55\u89e3\nres.push_back(root);\n}\npreOrder(root->left);\npreOrder(root->right);\n}\n
    preorder_traversal_i_compact.py
    def pre_order(root: TreeNode) -> None:\n\"\"\"\u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00\"\"\"\nif root is None:\nreturn\nif root.val == 7:\n# \u8bb0\u5f55\u89e3\nres.append(root)\npre_order(root.left)\npre_order(root.right)\n
    preorder_traversal_i_compact.go
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunc preOrderI(root *TreeNode, res *[]*TreeNode) {\nif root == nil {\nreturn\n}\nif int(root.Val) == 7 {\n// \u8bb0\u5f55\u89e3\n*res = append(*res, root)\n}\npreOrderI(root.Left, res)\npreOrderI(root.Right, res)\n}\n
    preorder_traversal_i_compact.js
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunction preOrder(root, res) {\nif (root === null) {\nreturn;\n}\nif (root.val === 7) {\n// \u8bb0\u5f55\u89e3\nres.push(root);\n}\npreOrder(root.left, res);\npreOrder(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 {\nif (root === null) {\nreturn;\n}\nif (root.val === 7) {\n// \u8bb0\u5f55\u89e3\nres.push(root);\n}\npreOrder(root.left, res);\npreOrder(root.right, res);\n}\n
    preorder_traversal_i_compact.c
    [class]{}-[func]{preOrder}\n
    preorder_traversal_i_compact.cs
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode root) {\nif (root == null) {\nreturn;\n}\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.Add(root);\n}\npreOrder(root.left);\npreOrder(root.right);\n}\n
    preorder_traversal_i_compact.swift
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunc preOrder(root: TreeNode?) {\nguard let root = root else {\nreturn\n}\nif root.val == 7 {\n// \u8bb0\u5f55\u89e3\nres.append(root)\n}\npreOrder(root: root.left)\npreOrder(root: root.right)\n}\n
    preorder_traversal_i_compact.zig
    [class]{}-[func]{preOrder}\n
    preorder_traversal_i_compact.dart
    [class]{}-[func]{preOrder}\n

    Fig. \u5728\u524d\u5e8f\u904d\u5386\u4e2d\u641c\u7d22\u8282\u70b9

    "},{"location":"chapter_backtracking/backtracking_algorithm/#1211","title":"12.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\u7ed3\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\u7b49\u4ef7\u4e8e\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\u8fd4\u56de\u6839\u8282\u70b9\u5230\u8fd9\u4e9b\u8282\u70b9\u7684\u8def\u5f84\u3002

    \u89e3\u9898\u601d\u8def\uff1a\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart preorder_traversal_ii_compact.java
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(TreeNode root) {\nif (root == null) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.add(root);\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.add(new ArrayList<>(path));\n}\npreOrder(root.left);\npreOrder(root.right);\n// \u56de\u9000\npath.remove(path.size() - 1);\n}\n
    preorder_traversal_ii_compact.cpp
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(TreeNode *root) {\nif (root == nullptr) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.push_back(root);\nif (root->val == 7) {\n// \u8bb0\u5f55\u89e3\nres.push_back(path);\n}\npreOrder(root->left);\npreOrder(root->right);\n// \u56de\u9000\npath.pop_back();\n}\n
    preorder_traversal_ii_compact.py
    def pre_order(root: TreeNode) -> None:\n\"\"\"\u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c\"\"\"\nif root is None:\nreturn\n# \u5c1d\u8bd5\npath.append(root)\nif root.val == 7:\n# \u8bb0\u5f55\u89e3\nres.append(list(path))\npre_order(root.left)\npre_order(root.right)\n# \u56de\u9000\npath.pop()\n
    preorder_traversal_ii_compact.go
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunc preOrderII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {\nif root == nil {\nreturn\n}\n// \u5c1d\u8bd5\n*path = append(*path, root)\nif int(root.Val) == 7 {\n// \u8bb0\u5f55\u89e3\n*res = append(*res, *path)\n}\npreOrderII(root.Left, res, path)\npreOrderII(root.Right, res, path)\n// \u56de\u9000\n*path = (*path)[:len(*path)-1]\n}\n
    preorder_traversal_ii_compact.js
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunction preOrder(root, path, res) {\nif (root === null) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.push(root);\nif (root.val === 7) {\n// \u8bb0\u5f55\u89e3\nres.push([...path]);\n}\npreOrder(root.left, path, res);\npreOrder(root.right, path, res);\n// \u56de\u9000\npath.pop();\n}\n
    preorder_traversal_ii_compact.ts
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunction preOrder(\nroot: TreeNode | null,\npath: TreeNode[],\nres: TreeNode[][]\n): void {\nif (root === null) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.push(root);\nif (root.val === 7) {\n// \u8bb0\u5f55\u89e3\nres.push([...path]);\n}\npreOrder(root.left, path, res);\npreOrder(root.right, path, res);\n// \u56de\u9000\npath.pop();\n}\n
    preorder_traversal_ii_compact.c
    [class]{}-[func]{preOrder}\n
    preorder_traversal_ii_compact.cs
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(TreeNode root) {\nif (root == null) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.Add(root);\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.Add(new List<TreeNode>(path));\n}\npreOrder(root.left);\npreOrder(root.right);\n// \u56de\u9000\npath.RemoveAt(path.Count - 1);\n}\n
    preorder_traversal_ii_compact.swift
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunc preOrder(root: TreeNode?) {\nguard let root = root else {\nreturn\n}\n// \u5c1d\u8bd5\npath.append(root)\nif root.val == 7 {\n// \u8bb0\u5f55\u89e3\nres.append(path)\n}\npreOrder(root: root.left)\npreOrder(root: root.right)\n// \u56de\u9000\npath.removeLast()\n}\n
    preorder_traversal_ii_compact.zig
    [class]{}-[func]{preOrder}\n
    preorder_traversal_ii_compact.dart
    [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\u6362\u53e5\u8bdd\u8bf4\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\u662f\u4e92\u4e3a\u76f8\u53cd\u7684\u3002

    <1><2><3><4><5><6><7><8><9><10><11>

    "},{"location":"chapter_backtracking/backtracking_algorithm/#1212","title":"12.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\u8fd4\u56de\u6839\u8282\u70b9\u5230\u8fd9\u4e9b\u8282\u70b9\u7684\u8def\u5f84\uff0c\u8def\u5f84\u4e2d\u4e0d\u80fd\u5305\u542b\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\u3002

    \u89e3\u9898\u601d\u8def\uff1a\u5728\u4f8b\u9898\u4e8c\u7684\u57fa\u7840\u4e0a\u6dfb\u52a0\u526a\u679d\u64cd\u4f5c\uff0c\u5f53\u9047\u5230\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\u65f6\uff0c\u5219\u7ec8\u6b62\u7ee7\u7eed\u641c\u7d22\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart preorder_traversal_iii_compact.java
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(TreeNode root) {\n// \u526a\u679d\nif (root == null || root.val == 3) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.add(root);\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.add(new ArrayList<>(path));\n}\npreOrder(root.left);\npreOrder(root.right);\n// \u56de\u9000\npath.remove(path.size() - 1);\n}\n
    preorder_traversal_iii_compact.cpp
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(TreeNode *root) {\n// \u526a\u679d\nif (root == nullptr || root->val == 3) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.push_back(root);\nif (root->val == 7) {\n// \u8bb0\u5f55\u89e3\nres.push_back(path);\n}\npreOrder(root->left);\npreOrder(root->right);\n// \u56de\u9000\npath.pop_back();\n}\n
    preorder_traversal_iii_compact.py
    def pre_order(root: TreeNode) -> None:\n\"\"\"\u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09\"\"\"\n# \u526a\u679d\nif root is None or root.val == 3:\nreturn\n# \u5c1d\u8bd5\npath.append(root)\nif root.val == 7:\n# \u8bb0\u5f55\u89e3\nres.append(list(path))\npre_order(root.left)\npre_order(root.right)\n# \u56de\u9000\npath.pop()\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\nif root == nil || root.Val == 3 {\nreturn\n}\n// \u5c1d\u8bd5\n*path = append(*path, root)\nif int(root.Val) == 7 {\n// \u8bb0\u5f55\u89e3\n*res = append(*res, *path)\n}\npreOrderIII(root.Left, res, path)\npreOrderIII(root.Right, res, path)\n// \u56de\u9000\n*path = (*path)[:len(*path)-1]\n}\n
    preorder_traversal_iii_compact.js
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunction preOrder(root, path, res) {\n// \u526a\u679d\nif (root === null || root.val === 3) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.push(root);\nif (root.val === 7) {\n// \u8bb0\u5f55\u89e3\nres.push([...path]);\n}\npreOrder(root.left, path, res);\npreOrder(root.right, path, res);\n// \u56de\u9000\npath.pop();\n}\n
    preorder_traversal_iii_compact.ts
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunction preOrder(\nroot: TreeNode | null,\npath: TreeNode[],\nres: TreeNode[][]\n): void {\n// \u526a\u679d\nif (root === null || root.val === 3) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.push(root);\nif (root.val === 7) {\n// \u8bb0\u5f55\u89e3\nres.push([...path]);\n}\npreOrder(root.left, path, res);\npreOrder(root.right, path, res);\n// \u56de\u9000\npath.pop();\n}\n
    preorder_traversal_iii_compact.c
    [class]{}-[func]{preOrder}\n
    preorder_traversal_iii_compact.cs
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(TreeNode root) {\n// \u526a\u679d\nif (root == null || root.val == 3) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.Add(root);\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.Add(new List<TreeNode>(path));\n}\npreOrder(root.left);\npreOrder(root.right);\n// \u56de\u9000\npath.RemoveAt(path.Count - 1);\n}\n
    preorder_traversal_iii_compact.swift
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunc preOrder(root: TreeNode?) {\n// \u526a\u679d\nguard let root = root, root.val != 3 else {\nreturn\n}\n// \u5c1d\u8bd5\npath.append(root)\nif root.val == 7 {\n// \u8bb0\u5f55\u89e3\nres.append(path)\n}\npreOrder(root: root.left)\npreOrder(root: root.right)\n// \u56de\u9000\npath.removeLast()\n}\n
    preorder_traversal_iii_compact.zig
    [class]{}-[func]{preOrder}\n
    preorder_traversal_iii_compact.dart
    [class]{}-[func]{preOrder}\n

    \u526a\u679d\u662f\u4e00\u4e2a\u975e\u5e38\u5f62\u8c61\u7684\u540d\u8bcd\u3002\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u5229\u7528\u7ea6\u675f\u6761\u4ef6\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\u5347\u641c\u7d22\u6548\u7387\u3002

    Fig. \u6839\u636e\u7ea6\u675f\u6761\u4ef6\u526a\u679d

    "},{"location":"chapter_backtracking/backtracking_algorithm/#1213","title":"12.1.3. \u00a0 \u5e38\u7528\u672f\u8bed","text":"

    \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\u3002

    \u540d\u8bcd \u5b9a\u4e49 \u4f8b\u9898\u4e09 \u89e3 Solution \u89e3\u662f\u6ee1\u8db3\u95ee\u9898\u7279\u5b9a\u6761\u4ef6\u7684\u7b54\u6848\u3002\u56de\u6eaf\u7b97\u6cd5\u7684\u76ee\u6807\u662f\u627e\u5230\u4e00\u4e2a\u6216\u591a\u4e2a\u6ee1\u8db3\u6761\u4ef6\u7684\u89e3 \u6839\u8282\u70b9\u5230\u8282\u70b9 \\(7\\) \u7684\u6240\u6709\u8def\u5f84\uff0c\u4e14\u8def\u5f84\u4e2d\u4e0d\u5305\u542b\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9 \u72b6\u6001 State \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\u5373 path \u8282\u70b9\u5217\u8868 \u7ea6\u675f\u6761\u4ef6 Constraint \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 \u8981\u6c42\u8def\u5f84\u4e2d\u4e0d\u80fd\u5305\u542b\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9 \u5c1d\u8bd5 Attempt \u5c1d\u8bd5\u662f\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u6839\u636e\u5f53\u524d\u72b6\u6001\u548c\u53ef\u7528\u9009\u62e9\u6765\u63a2\u7d22\u89e3\u7a7a\u95f4\u7684\u8fc7\u7a0b\u3002\u5c1d\u8bd5\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 Backtracking \u56de\u9000\u6307\u5728\u641c\u7d22\u4e2d\u9047\u5230\u5230\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u6216\u65e0\u6cd5\u7ee7\u7eed\u641c\u7d22\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\u7ed3\u70b9\u3001\u7ed3\u675f\u7ed3\u70b9\u8bbf\u95ee\u3001\u9047\u5230\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\u65f6\u7ec8\u6b62\u641c\u7d22\uff0c\u51fd\u6570\u8fd4\u56de \u526a\u679d Pruning \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\u7ec8\u6b62\u7ee7\u7eed\u641c\u7d22

    Tip

    \u89e3\u3001\u72b6\u6001\u3001\u7ea6\u675f\u6761\u4ef6\u7b49\u672f\u8bed\u662f\u901a\u7528\u7684\uff0c\u9002\u7528\u4e8e\u56de\u6eaf\u7b97\u6cd5\u3001\u52a8\u6001\u89c4\u5212\u3001\u8d2a\u5fc3\u7b97\u6cd5\u7b49\u3002

    "},{"location":"chapter_backtracking/backtracking_algorithm/#1214","title":"12.1.4. \u00a0 \u6846\u67b6\u4ee3\u7801","text":"

    \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\u4e3a\u63d0\u5347\u4ee3\u7801\u901a\u7528\u6027\uff0c\u6211\u4eec\u5e0c\u671b\u5c06\u56de\u6eaf\u7b97\u6cd5\u7684\u201c\u5c1d\u8bd5\u3001\u56de\u9000\u3001\u526a\u679d\u201d\u7684\u4e3b\u4f53\u6846\u67b6\u63d0\u70bc\u51fa\u6765\u3002

    \u8bbe state \u4e3a\u95ee\u9898\u7684\u5f53\u524d\u72b6\u6001\uff0cchoices \u8868\u793a\u5f53\u524d\u72b6\u6001\u4e0b\u53ef\u4ee5\u505a\u51fa\u7684\u9009\u62e9\uff0c\u5219\u53ef\u5f97\u5230\u4ee5\u4e0b\u56de\u6eaf\u7b97\u6cd5\u7684\u6846\u67b6\u4ee3\u7801\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State state, List<Choice> choices, List<State> res) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (Choice choice : choices) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\nbacktrack(state, choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice);\n}\n}\n}\n
    /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State *state, vector<Choice *> &choices, vector<State *> &res) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (Choice choice : choices) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\nbacktrack(state, choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice);\n}\n}\n}\n
    def backtrack(state: State, choices: list[choice], res: list[state]) -> None:\n\"\"\"\u56de\u6eaf\u7b97\u6cd5\u6846\u67b6\"\"\"\n# \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif is_solution(state):\n# \u8bb0\u5f55\u89e3\nrecord_solution(state, res)\nreturn\n# \u904d\u5386\u6240\u6709\u9009\u62e9\nfor choice in choices:\n# \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif is_valid(state, choice):\n# \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmake_choice(state, choice)\nbacktrack(state, choices, res)\n# \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundo_choice(state, choice)\n
    /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfunc backtrack(state *State, choices []Choice, res *[]State) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif isSolution(state) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor _, choice := range choices {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif isValid(state, choice) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice)\nbacktrack(state, choices, res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice)\n}\n}\n}\n
    /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfunction backtrack(state, choices, res) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (let choice of choices) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\nbacktrack(state, choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(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\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (let choice of choices) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\nbacktrack(state, choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(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\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res, numRes);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i < numChoices; i++) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, &choices[i])) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, &choices[i]);\nbacktrack(state, choices, numChoices, res, numRes);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, &choices[i]);\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\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nforeach (Choice choice in choices) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\nbacktrack(state, choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(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\nif isSolution(state: state) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state: state, res: &res)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor choice in choices {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif isValid(state: state, choice: choice) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state: &state, choice: choice)\nbacktrack(state: &state, choices: choices, res: &res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state: &state, choice: choice)\n}\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\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (Choice choice in choices) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\nbacktrack(state, choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice);\n}\n}\n}\n

    \u4e0b\u9762\uff0c\u6211\u4eec\u5c1d\u8bd5\u57fa\u4e8e\u6b64\u6846\u67b6\u6765\u89e3\u51b3\u4f8b\u9898\u4e09\u3002\u5728\u4f8b\u9898\u4e09\u4e2d\uff0c\u72b6\u6001 state \u662f\u8282\u70b9\u904d\u5386\u8def\u5f84\uff0c\u9009\u62e9 choices \u662f\u5f53\u524d\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\uff0c\u7ed3\u679c res \u662f\u8def\u5f84\u5217\u8868\uff0c\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart preorder_traversal_iii_template.java
    /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nboolean isSolution(List<TreeNode> state) {\nreturn !state.isEmpty() && state.get(state.size() - 1).val == 7;\n}\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\nres.add(new ArrayList<>(state));\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) {\nreturn choice != null && choice.val != 3;\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(List<TreeNode> state, TreeNode choice) {\nstate.add(choice);\n}\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(List<TreeNode> state, TreeNode choice) {\nstate.remove(state.size() - 1);\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\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (TreeNode choice : choices) {\n// \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(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\nundoChoice(state, choice);\n}\n}\n}\n
    preorder_traversal_iii_template.cpp
    /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool isSolution(vector<TreeNode *> &state) {\nreturn !state.empty() && state.back()->val == 7;\n}\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(vector<TreeNode *> &state, vector<vector<TreeNode *>> &res) {\nres.push_back(state);\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) {\nreturn choice != nullptr && choice->val != 3;\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(vector<TreeNode *> &state, TreeNode *choice) {\nstate.push_back(choice);\n}\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(vector<TreeNode *> &state, TreeNode *choice) {\nstate.pop_back();\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\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (TreeNode *choice : choices) {\n// \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nvector<TreeNode *> nextChoices{choice->left, choice->right};\nbacktrack(state, nextChoices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice);\n}\n}\n}\n
    preorder_traversal_iii_template.py
    def is_solution(state: list[TreeNode]) -> bool:\n\"\"\"\u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3\"\"\"\nreturn state and state[-1].val == 7\ndef record_solution(state: list[TreeNode], res: list[list[TreeNode]]):\n\"\"\"\u8bb0\u5f55\u89e3\"\"\"\nres.append(list(state))\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\"\"\"\nreturn choice is not None and choice.val != 3\ndef make_choice(state: list[TreeNode], choice: TreeNode):\n\"\"\"\u66f4\u65b0\u72b6\u6001\"\"\"\nstate.append(choice)\ndef undo_choice(state: list[TreeNode], choice: TreeNode):\n\"\"\"\u6062\u590d\u72b6\u6001\"\"\"\nstate.pop()\ndef backtrack(\nstate: 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\nif is_solution(state):\n# \u8bb0\u5f55\u89e3\nrecord_solution(state, res)\nreturn\n# \u904d\u5386\u6240\u6709\u9009\u62e9\nfor choice in choices:\n# \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif is_valid(state, choice):\n# \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmake_choice(state, choice)\n# \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, [choice.left, choice.right], res)\n# \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundo_choice(state, choice)\n
    preorder_traversal_iii_template.go
    /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfunc isSolution(state *[]*TreeNode) bool {\nreturn len(*state) != 0 && (*state)[len(*state)-1].Val == 7\n}\n/* \u8bb0\u5f55\u89e3 */\nfunc recordSolution(state *[]*TreeNode, res *[][]*TreeNode) {\n*res = append(*res, *state)\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 {\nreturn choice != nil && choice.Val != 3\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nfunc makeChoice(state *[]*TreeNode, choice *TreeNode) {\n*state = append(*state, choice)\n}\n/* \u6062\u590d\u72b6\u6001 */\nfunc undoChoice(state *[]*TreeNode, choice *TreeNode) {\n*state = (*state)[:len(*state)-1]\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\nif isSolution(state) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor _, choice := range *choices {\n// \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif isValid(state, choice) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice)\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\ntemp := make([]*TreeNode, 0)\ntemp = append(temp, choice.Left, choice.Right)\nbacktrackIII(state, &temp, res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice)\n}\n}\n}\n
    preorder_traversal_iii_template.js
    /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfunction isSolution(state) {\nreturn state && state[state.length - 1]?.val === 7;\n}\n/* \u8bb0\u5f55\u89e3 */\nfunction recordSolution(state, res) {\nres.push([...state]);\n}\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfunction isValid(state, choice) {\nreturn choice !== null && choice.val !== 3;\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nfunction makeChoice(state, choice) {\nstate.push(choice);\n}\n/* \u6062\u590d\u72b6\u6001 */\nfunction undoChoice(state) {\nstate.pop();\n}\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfunction backtrack(state, choices, res) {\n// \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (const choice of choices) {\n// \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, [choice.left, choice.right], res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(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 {\nreturn state && state[state.length - 1]?.val === 7;\n}\n/* \u8bb0\u5f55\u89e3 */\nfunction recordSolution(state: TreeNode[], res: TreeNode[][]): void {\nres.push([...state]);\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 {\nreturn choice !== null && choice.val !== 3;\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nfunction makeChoice(state: TreeNode[], choice: TreeNode): void {\nstate.push(choice);\n}\n/* \u6062\u590d\u72b6\u6001 */\nfunction undoChoice(state: TreeNode[]): void {\nstate.pop();\n}\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfunction backtrack(\nstate: TreeNode[],\nchoices: TreeNode[],\nres: TreeNode[][]\n): void {\n// \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (const choice of choices) {\n// \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, [choice.left, choice.right], res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state);\n}\n}\n}\n
    preorder_traversal_iii_template.c
    [class]{}-[func]{isSolution}\n[class]{}-[func]{recordSolution}\n[class]{}-[func]{isValid}\n[class]{}-[func]{makeChoice}\n[class]{}-[func]{undoChoice}\n[class]{}-[func]{backtrack}\n
    preorder_traversal_iii_template.cs
    /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool isSolution(List<TreeNode> state) {\nreturn state.Count != 0 && state[^1].val == 7;\n}\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\nres.Add(new List<TreeNode>(state));\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) {\nreturn choice != null && choice.val != 3;\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(List<TreeNode> state, TreeNode choice) {\nstate.Add(choice);\n}\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(List<TreeNode> state, TreeNode choice) {\nstate.RemoveAt(state.Count - 1);\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\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nforeach (TreeNode choice in choices) {\n// \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, new List<TreeNode> { choice.left, choice.right }, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(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/* \u8bb0\u5f55\u89e3 */\nfunc recordSolution(state: [TreeNode], res: inout [[TreeNode]]) {\nres.append(state)\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 {\nchoice != nil && choice!.val != 3\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nfunc makeChoice(state: inout [TreeNode], choice: TreeNode) {\nstate.append(choice)\n}\n/* \u6062\u590d\u72b6\u6001 */\nfunc undoChoice(state: inout [TreeNode], choice: TreeNode) {\nstate.removeLast()\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\nif isSolution(state: state) {\nrecordSolution(state: state, res: &res)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor choice in choices {\n// \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif isValid(state: state, choice: choice) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state: &state, choice: choice)\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(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\nundoChoice(state: &state, choice: choice)\n}\n}\n}\n
    preorder_traversal_iii_template.zig
    [class]{}-[func]{isSolution}\n[class]{}-[func]{recordSolution}\n[class]{}-[func]{isValid}\n[class]{}-[func]{makeChoice}\n[class]{}-[func]{undoChoice}\n[class]{}-[func]{backtrack}\n
    preorder_traversal_iii_template.dart
    [class]{}-[func]{isSolution}\n[class]{}-[func]{recordSolution}\n[class]{}-[func]{isValid}\n[class]{}-[func]{makeChoice}\n[class]{}-[func]{undoChoice}\n[class]{}-[func]{backtrack}\n

    \u76f8\u8f83\u4e8e\u57fa\u4e8e\u524d\u5e8f\u904d\u5386\u7684\u5b9e\u73b0\u4ee3\u7801\uff0c\u57fa\u4e8e\u56de\u6eaf\u7b97\u6cd5\u6846\u67b6\u7684\u5b9e\u73b0\u4ee3\u7801\u867d\u7136\u663e\u5f97\u5570\u55e6\uff0c\u4f46\u901a\u7528\u6027\u66f4\u597d\u3002\u5b9e\u9645\u4e0a\uff0c\u6240\u6709\u56de\u6eaf\u95ee\u9898\u90fd\u53ef\u4ee5\u5728\u8be5\u6846\u67b6\u4e0b\u89e3\u51b3\u3002\u6211\u4eec\u9700\u8981\u6839\u636e\u5177\u4f53\u95ee\u9898\u6765\u5b9a\u4e49 state \u548c choices \uff0c\u5e76\u5b9e\u73b0\u6846\u67b6\u4e2d\u7684\u5404\u4e2a\u65b9\u6cd5\u3002

    "},{"location":"chapter_backtracking/backtracking_algorithm/#1215","title":"12.1.5. \u00a0 \u5178\u578b\u4f8b\u9898","text":"

    \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

    • \u5168\u6392\u5217\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u96c6\u5408\uff0c\u6c42\u51fa\u5176\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u7ec4\u5408\u3002
    • \u5b50\u96c6\u548c\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u96c6\u5408\u548c\u4e00\u4e2a\u76ee\u6807\u548c\uff0c\u627e\u5230\u96c6\u5408\u4e2d\u6240\u6709\u548c\u4e3a\u76ee\u6807\u548c\u7684\u5b50\u96c6\u3002
    • \u6c49\u8bfa\u5854\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e09\u4e2a\u67f1\u5b50\u548c\u4e00\u7cfb\u5217\u5927\u5c0f\u4e0d\u540c\u7684\u5706\u76d8\uff0c\u8981\u6c42\u5c06\u6240\u6709\u5706\u76d8\u4ece\u4e00\u4e2a\u67f1\u5b50\u79fb\u52a8\u5230\u53e6\u4e00\u4e2a\u67f1\u5b50\uff0c\u6bcf\u6b21\u53ea\u80fd\u79fb\u52a8\u4e00\u4e2a\u5706\u76d8\uff0c\u4e14\u4e0d\u80fd\u5c06\u5927\u5706\u76d8\u653e\u5728\u5c0f\u5706\u76d8\u4e0a\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

    • \\(n\\) \u7687\u540e\uff1a\u5728 \\(n \\times n\\) \u7684\u68cb\u76d8\u4e0a\u653e\u7f6e \\(n\\) \u4e2a\u7687\u540e\uff0c\u4f7f\u5f97\u5b83\u4eec\u4e92\u4e0d\u653b\u51fb\u3002
    • \u6570\u72ec\uff1a\u5728 \\(9 \\times 9\\) \u7684\u7f51\u683c\u4e2d\u586b\u5165\u6570\u5b57 \\(1\\) ~ \\(9\\) \uff0c\u4f7f\u5f97\u6bcf\u884c\u3001\u6bcf\u5217\u548c\u6bcf\u4e2a \\(3 \\times 3\\) \u5b50\u7f51\u683c\u4e2d\u7684\u6570\u5b57\u4e0d\u91cd\u590d\u3002
    • \u56fe\u7740\u8272\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u65e0\u5411\u56fe\uff0c\u7528\u6700\u5c11\u7684\u989c\u8272\u7ed9\u56fe\u7684\u6bcf\u4e2a\u9876\u70b9\u7740\u8272\uff0c\u4f7f\u5f97\u76f8\u90bb\u9876\u70b9\u989c\u8272\u4e0d\u540c\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

    • 0-1 \u80cc\u5305\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u7ec4\u7269\u54c1\u548c\u4e00\u4e2a\u80cc\u5305\uff0c\u6bcf\u4e2a\u7269\u54c1\u6709\u4e00\u5b9a\u7684\u4ef7\u503c\u548c\u91cd\u91cf\uff0c\u8981\u6c42\u5728\u80cc\u5305\u5bb9\u91cf\u9650\u5236\u5185\uff0c\u9009\u62e9\u7269\u54c1\u4f7f\u5f97\u603b\u4ef7\u503c\u6700\u5927\u3002
    • \u65c5\u884c\u5546\u95ee\u9898\uff1a\u5728\u4e00\u4e2a\u56fe\u4e2d\uff0c\u4ece\u4e00\u4e2a\u70b9\u51fa\u53d1\uff0c\u8bbf\u95ee\u6240\u6709\u5176\u4ed6\u70b9\u6070\u597d\u4e00\u6b21\u540e\u8fd4\u56de\u8d77\u70b9\uff0c\u6c42\u6700\u77ed\u8def\u5f84\u3002
    • \u6700\u5927\u56e2\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u65e0\u5411\u56fe\uff0c\u627e\u5230\u6700\u5927\u7684\u5b8c\u5168\u5b50\u56fe\uff0c\u5373\u5b50\u56fe\u4e2d\u7684\u4efb\u610f\u4e24\u4e2a\u9876\u70b9\u4e4b\u95f4\u90fd\u6709\u8fb9\u76f8\u8fde\u3002

    \u5728\u63a5\u4e0b\u6765\u7684\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u4e00\u8d77\u653b\u514b\u51e0\u4e2a\u7ecf\u5178\u7684\u56de\u6eaf\u7b97\u6cd5\u95ee\u9898\u3002

    "},{"location":"chapter_backtracking/n_queens_problem/","title":"12.3. \u00a0 N \u7687\u540e\u95ee\u9898","text":"

    Question

    \u6839\u636e\u56fd\u9645\u8c61\u68cb\u7684\u89c4\u5219\uff0c\u7687\u540e\u53ef\u4ee5\u653b\u51fb\u4e0e\u4e4b\u5904\u5728\u540c\u4e00\u884c\u6216\u540c\u4e00\u5217\u6216\u540c\u4e00\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\u4e0b\u56fe\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

    Fig. 4 \u7687\u540e\u95ee\u9898\u7684\u89e3

    \u672c\u9898\u5171\u6709\u4e09\u4e2a\u7ea6\u675f\u6761\u4ef6\uff1a\u591a\u4e2a\u7687\u540e\u4e0d\u80fd\u5728\u540c\u4e00\u884c\u3001\u540c\u4e00\u5217\u548c\u540c\u4e00\u5bf9\u89d2\u7ebf\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5bf9\u89d2\u7ebf\u5206\u4e3a\u4e3b\u5bf9\u89d2\u7ebf \\ \u548c\u526f\u5bf9\u89d2\u7ebf / \u4e24\u79cd\u3002

    Fig. n \u7687\u540e\u95ee\u9898\u7684\u7ea6\u675f\u6761\u4ef6

    \u7687\u540e\u7684\u6570\u91cf\u548c\u68cb\u76d8\u7684\u884c\u6570\u90fd\u4e3a \\(n\\) \uff0c\u56e0\u6b64\u6211\u4eec\u5bb9\u6613\u5f97\u5230\u7b2c\u4e00\u4e2a\u63a8\u8bba\uff1a\u68cb\u76d8\u6bcf\u884c\u90fd\u5141\u8bb8\u4e14\u53ea\u5141\u8bb8\u653e\u7f6e\u4e00\u4e2a\u7687\u540e\u3002\u8fd9\u610f\u5473\u7740\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\u6b64\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

    \u4e0b\u56fe\u5c55\u793a\u4e86 \\(4\\) \u7687\u540e\u95ee\u9898\u7684\u9010\u884c\u653e\u7f6e\u8fc7\u7a0b\u3002\u53d7\u7bc7\u5e45\u9650\u5236\uff0c\u4e0b\u56fe\u4ec5\u5c55\u5f00\u4e86\u7b2c\u4e00\u884c\u7684\u4e00\u4e2a\u641c\u7d22\u5206\u652f\u3002\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u4e0d\u6ee1\u8db3\u5217\u7ea6\u675f\u548c\u5bf9\u89d2\u7ebf\u7ea6\u675f\u7684\u65b9\u6848\u90fd\u526a\u679d\u4e86\u3002

    Fig. \u9010\u884c\u653e\u7f6e\u7b56\u7565

    \u4e3a\u4e86\u5b9e\u73b0\u6839\u636e\u5217\u7ea6\u675f\u526a\u679d\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\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\u89c2\u5bdf\u77e9\u9635\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\u76f8\u7b49\uff0c\u5373 row - col \u4e3a\u6052\u5b9a\u503c\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u82e5\u4e24\u4e2a\u683c\u5b50\u6ee1\u8db3 row1 - col1 == row2 - col2 \uff0c\u5219\u8fd9\u4e24\u4e2a\u683c\u5b50\u4e00\u5b9a\u5904\u5728\u4e00\u6761\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u3002

    \u5229\u7528\u8be5\u6027\u8d28\uff0c\u6211\u4eec\u53ef\u4ee5\u501f\u52a9\u4e00\u4e2a\u6570\u7ec4 diag1 \u6765\u8bb0\u5f55\u6bcf\u6761\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\u3002\u6ce8\u610f\uff0c\\(n\\) \u7ef4\u65b9\u9635 row - col \u7684\u8303\u56f4\u662f \\([-n + 1, n - 1]\\) \uff0c\u56e0\u6b64\u5171\u6709 \\(2n - 1\\) \u6761\u4e3b\u5bf9\u89d2\u7ebf\u3002

    Fig. \u5904\u7406\u5217\u7ea6\u675f\u548c\u5bf9\u89d2\u7ebf\u7ea6\u675f

    \u540c\u7406\uff0c\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u7684\u6240\u6709\u683c\u5b50\u7684 row + col \u662f\u6052\u5b9a\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u540c\u6837\u7684\u65b9\u6cd5\uff0c\u501f\u52a9\u6570\u7ec4 diag2 \u6765\u5904\u7406\u6b21\u5bf9\u89d2\u7ebf\u7ea6\u675f\u3002

    \u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u6211\u4eec\u4fbf\u53ef\u4ee5\u5199\u51fa \\(n\\) \u7687\u540e\u7684\u89e3\u9898\u4ee3\u7801\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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,\nboolean[] cols, boolean[] diags1, boolean[] diags2) {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (row == n) {\nList<List<String>> copyState = new ArrayList<>();\nfor (List<String> sRow : state) {\ncopyState.add(new ArrayList<>(sRow));\n}\nres.add(copyState);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor (int col = 0; col < n; col++) {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\nint diag1 = row - col + n - 1;\nint diag2 = row + col;\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728 (\u5217 \u6216 \u4e3b\u5bf9\u89d2\u7ebf \u6216 \u526f\u5bf9\u89d2\u7ebf) \u5305\u542b\u7687\u540e\nif (!(cols[col] || diags1[diag1] || diags2[diag2])) {\n// \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\nstate.get(row).set(col, \"Q\");\ncols[col] = diags1[diag1] = diags2[diag2] = true;\n// \u653e\u7f6e\u4e0b\u4e00\u884c\nbacktrack(row + 1, n, state, res, cols, diags1, diags2);\n// \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\nstate.get(row).set(col, \"#\");\ncols[col] = diags1[diag1] = diags2[diag2] = false;\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\nList<List<String>> state = new ArrayList<>();\nfor (int i = 0; i < n; i++) {\nList<String> row = new ArrayList<>();\nfor (int j = 0; j < n; j++) {\nrow.add(\"#\");\n}\nstate.add(row);\n}\nboolean[] cols = new boolean[n]; // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nboolean[] diags1 = new boolean[2 * n - 1]; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nboolean[] diags2 = new boolean[2 * n - 1]; // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nList<List<List<String>>> res = new ArrayList<>();\nbacktrack(0, n, state, res, cols, diags1, diags2);\nreturn res;\n}\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,\nvector<bool> &diags1, vector<bool> &diags2) {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (row == n) {\nres.push_back(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor (int col = 0; col < n; col++) {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\nint diag1 = row - col + n - 1;\nint diag2 = row + col;\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728 (\u5217 \u6216 \u4e3b\u5bf9\u89d2\u7ebf \u6216 \u526f\u5bf9\u89d2\u7ebf) \u5305\u542b\u7687\u540e\nif (!(cols[col] || diags1[diag1] || diags2[diag2])) {\n// \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\nstate[row][col] = \"Q\";\ncols[col] = diags1[diag1] = diags2[diag2] = true;\n// \u653e\u7f6e\u4e0b\u4e00\u884c\nbacktrack(row + 1, n, state, res, cols, diags1, diags2);\n// \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\nstate[row][col] = \"#\";\ncols[col] = diags1[diag1] = diags2[diag2] = false;\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\nvector<vector<string>> state(n, vector<string>(n, \"#\"));\nvector<bool> cols(n, false);           // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nvector<bool> diags1(2 * n - 1, false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nvector<bool> diags2(2 * n - 1, false); // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nvector<vector<vector<string>>> res;\nbacktrack(0, n, state, res, cols, diags1, diags2);\nreturn res;\n}\n
    n_queens.py
    def backtrack(\nrow: int,\nn: int,\nstate: list[list[str]],\nres: list[list[list[str]]],\ncols: list[bool],\ndiags1: list[bool],\ndiags2: list[bool],\n):\n\"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e\"\"\"\n# \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif row == n:\nres.append([list(row) for row in state])\nreturn\n# \u904d\u5386\u6240\u6709\u5217\nfor col in range(n):\n# \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\ndiag1 = row - col + n - 1\ndiag2 = row + col\n# \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728 (\u5217 \u6216 \u4e3b\u5bf9\u89d2\u7ebf \u6216 \u526f\u5bf9\u89d2\u7ebf) \u5305\u542b\u7687\u540e\nif not (cols[col] or diags1[diag1] or diags2[diag2]):\n# \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\nstate[row][col] = \"Q\"\ncols[col] = diags1[diag1] = diags2[diag2] = True\n# \u653e\u7f6e\u4e0b\u4e00\u884c\nbacktrack(row + 1, n, state, res, cols, diags1, diags2)\n# \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\nstate[row][col] = \"#\"\ncols[col] = diags1[diag1] = diags2[diag2] = False\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\nstate = [[\"#\" for _ in range(n)] for _ in range(n)]\ncols = [False] * n  # \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\ndiags1 = [False] * (2 * n - 1)  # \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\ndiags2 = [False] * (2 * n - 1)  # \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nres = []\nbacktrack(0, n, state, res, cols, diags1, diags2)\nreturn res\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\nif row == n {\nnewState := make([][]string, len(*state))\nfor i, _ := range newState {\nnewState[i] = make([]string, len((*state)[0]))\ncopy(newState[i], (*state)[i])\n}\n*res = append(*res, newState)\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor col := 0; col < n; col++ {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\ndiag1 := row - col + n - 1\ndiag2 := row + col\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728 (\u5217 \u6216 \u4e3b\u5bf9\u89d2\u7ebf \u6216 \u526f\u5bf9\u89d2\u7ebf) \u5305\u542b\u7687\u540e\nif !((*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\nbacktrack(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/* \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\nif row == n {\nnewState := make([][]string, len(*state))\nfor i, _ := range newState {\nnewState[i] = make([]string, len((*state)[0]))\ncopy(newState[i], (*state)[i])\n}\n*res = append(*res, newState)\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor col := 0; col < n; col++ {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\ndiag1 := row - col + n - 1\ndiag2 := row + col\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728 (\u5217 \u6216 \u4e3b\u5bf9\u89d2\u7ebf \u6216 \u526f\u5bf9\u89d2\u7ebf) \u5305\u542b\u7687\u540e\nif !((*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\nbacktrack(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}\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\nstate := make([][]string, n)\nfor i := 0; i < n; i++ {\nrow := make([]string, n)\nfor i := 0; i < n; i++ {\nrow[i] = \"#\"\n}\nstate[i] = row\n}\n// \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\ncols := make([]bool, n)\ndiags1 := make([]bool, 2*n-1)\ndiags2 := make([]bool, 2*n-1)\nres := make([][][]string, 0)\nbacktrack(0, n, &state, &res, &cols, &diags1, &diags2)\nreturn 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\nif (row === n) {\nres.push(state.map((row) => row.slice()));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor (let col = 0; col < n; col++) {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\nconst diag1 = row - col + n - 1;\nconst diag2 = row + col;\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728 (\u5217 \u6216 \u4e3b\u5bf9\u89d2\u7ebf \u6216 \u526f\u5bf9\u89d2\u7ebf) \u5305\u542b\u7687\u540e\nif (!(cols[col] || diags1[diag1] || diags2[diag2])) {\n// \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\nstate[row][col] = 'Q';\ncols[col] = diags1[diag1] = diags2[diag2] = true;\n// \u653e\u7f6e\u4e0b\u4e00\u884c\nbacktrack(row + 1, n, state, res, cols, diags1, diags2);\n// \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\nstate[row][col] = '#';\ncols[col] = diags1[diag1] = diags2[diag2] = false;\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\nconst state = Array.from({ length: n }, () => Array(n).fill('#'));\nconst cols = Array(n).fill(false); // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nconst diags1 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nconst diags2 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nconst res = [];\nbacktrack(0, n, state, res, cols, diags1, diags2);\nreturn res;\n}\n
    n_queens.ts
    /* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nfunction backtrack(\nrow: number,\nn: number,\nstate: string[][],\nres: string[][][],\ncols: boolean[],\ndiags1: boolean[],\ndiags2: boolean[]\n): void {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (row === n) {\nres.push(state.map((row) => row.slice()));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor (let col = 0; col < n; col++) {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\nconst diag1 = row - col + n - 1;\nconst diag2 = row + col;\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728 (\u5217 \u6216 \u4e3b\u5bf9\u89d2\u7ebf \u6216 \u526f\u5bf9\u89d2\u7ebf) \u5305\u542b\u7687\u540e\nif (!(cols[col] || diags1[diag1] || diags2[diag2])) {\n// \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\nstate[row][col] = 'Q';\ncols[col] = diags1[diag1] = diags2[diag2] = true;\n// \u653e\u7f6e\u4e0b\u4e00\u884c\nbacktrack(row + 1, n, state, res, cols, diags1, diags2);\n// \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\nstate[row][col] = '#';\ncols[col] = diags1[diag1] = diags2[diag2] = false;\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\nconst state = Array.from({ length: n }, () => Array(n).fill('#'));\nconst cols = Array(n).fill(false); // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nconst diags1 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nconst diags2 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nconst res: string[][][] = [];\nbacktrack(0, n, state, res, cols, diags1, diags2);\nreturn res;\n}\n
    n_queens.c
    [class]{}-[func]{backtrack}\n[class]{}-[func]{nQueens}\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,\nbool[] cols, bool[] diags1, bool[] diags2) {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (row == n) {\nList<List<string>> copyState = new List<List<string>>();\nforeach (List<string> sRow in state) {\ncopyState.Add(new List<string>(sRow));\n}\nres.Add(copyState);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor (int col = 0; col < n; col++) {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\nint diag1 = row - col + n - 1;\nint diag2 = row + col;\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728 (\u5217 \u6216 \u4e3b\u5bf9\u89d2\u7ebf \u6216 \u526f\u5bf9\u89d2\u7ebf) \u5305\u542b\u7687\u540e\nif (!(cols[col] || diags1[diag1] || diags2[diag2])) {\n// \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\nstate[row][col] = \"Q\";\ncols[col] = diags1[diag1] = diags2[diag2] = true;\n// \u653e\u7f6e\u4e0b\u4e00\u884c\nbacktrack(row + 1, n, state, res, cols, diags1, diags2);\n// \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\nstate[row][col] = \"#\";\ncols[col] = diags1[diag1] = diags2[diag2] = false;\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\nList<List<string>> state = new List<List<string>>();\nfor (int i = 0; i < n; i++) {\nList<string> row = new List<string>();\nfor (int j = 0; j < n; j++) {\nrow.Add(\"#\");\n}\nstate.Add(row);\n}\nbool[] cols = new bool[n]; // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nbool[] diags1 = new bool[2 * n - 1]; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nbool[] diags2 = new bool[2 * n - 1]; // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nList<List<List<string>>> res = new List<List<List<string>>>();\nbacktrack(0, n, state, res, cols, diags1, diags2);\nreturn 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\nif row == n {\nres.append(state)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor col in 0 ..< n {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\nlet diag1 = row - col + n - 1\nlet diag2 = row + col\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728 (\u5217 \u6216 \u4e3b\u5bf9\u89d2\u7ebf \u6216 \u526f\u5bf9\u89d2\u7ebf) \u5305\u542b\u7687\u540e\nif !(cols[col] || diags1[diag1] || diags2[diag2]) {\n// \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\nstate[row][col] = \"Q\"\ncols[col] = true\ndiags1[diag1] = true\ndiags2[diag2] = true\n// \u653e\u7f6e\u4e0b\u4e00\u884c\nbacktrack(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\nstate[row][col] = \"#\"\ncols[col] = false\ndiags1[diag1] = false\ndiags2[diag2] = false\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\nvar state = Array(repeating: Array(repeating: \"#\", count: n), count: n)\nvar cols = Array(repeating: false, count: n) // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nvar diags1 = Array(repeating: false, count: 2 * n - 1) // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nvar diags2 = Array(repeating: false, count: 2 * n - 1) // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nvar res: [[[String]]] = []\nbacktrack(row: 0, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)\nreturn res\n}\n
    n_queens.zig
    [class]{}-[func]{backtrack}\n[class]{}-[func]{nQueens}\n
    n_queens.dart
    [class]{}-[func]{backtrack}\n[class]{}-[func]{nQueens}\n
    "},{"location":"chapter_backtracking/n_queens_problem/#1231","title":"12.3.1. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"

    \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, n-1, \\cdots, 2, 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\u5730\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

    state \u4f7f\u7528 \\(O(n^2)\\) \u7a7a\u95f4\uff0ccols , diags1 , 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

    "},{"location":"chapter_backtracking/permutations_problem/","title":"12.2. \u00a0 \u5168\u6392\u5217\u95ee\u9898","text":"

    \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\u8fd9\u4e2a\u96c6\u5408\u4e2d\u5143\u7d20\u7684\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u3002

    \u5982\u4e0b\u8868\u6240\u793a\uff0c\u5217\u4e3e\u4e86\u51e0\u4e2a\u793a\u4f8b\u6570\u7ec4\u548c\u5176\u5bf9\u5e94\u7684\u6240\u6709\u6392\u5217\u3002

    \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/#1221","title":"12.2.1. \u00a0 \u65e0\u91cd\u590d\u7684\u60c5\u51b5","text":"

    Question

    \u8f93\u5165\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\u6570\u7ec4\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\\) \u3001\u518d\u9009\u62e9 \\(3\\) \u3001\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\u7b97\u6cd5\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\u6ce8\u610f\uff0c\u6bcf\u4e2a\u5143\u7d20\u53ea\u5141\u8bb8\u88ab\u9009\u62e9\u4e00\u6b21\uff0c\u56e0\u6b64\u5728\u904d\u5386\u9009\u62e9\u65f6\uff0c\u5e94\u5f53\u6392\u9664\u5df2\u7ecf\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u3002

    \u5982\u4e0b\u56fe\u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u641c\u7d22\u8fc7\u7a0b\u5c55\u5f00\u6210\u4e00\u4e2a\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

    Fig. \u5168\u6392\u5217\u7684\u9012\u5f52\u6811

    \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\u4ee3\u7801\u884c\u6570\uff0c\u6211\u4eec\u4e0d\u5355\u72ec\u5b9e\u73b0\u6846\u67b6\u4ee3\u7801\u4e2d\u7684\u5404\u4e2a\u51fd\u6570\uff0c\u800c\u662f\u5c06\u4ed6\u4eec\u5c55\u5f00\u5728 backtrack() \u51fd\u6570\u4e2d\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nif (state.size() == choices.length) {\nres.add(new ArrayList<Integer>(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i < choices.length; i++) {\nint 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\nif (!selected[i]) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nselected[i] = true;\nstate.add(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.remove(state.size() - 1);\n}\n}\n}\n/* \u5168\u6392\u5217 I */\nList<List<Integer>> permutationsI(int[] nums) {\nList<List<Integer>> res = new ArrayList<List<Integer>>();\nbacktrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);\nreturn res;\n}\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\nif (state.size() == choices.size()) {\nres.push_back(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i < choices.size(); i++) {\nint 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\nif (!selected[i]) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nselected[i] = true;\nstate.push_back(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.pop_back();\n}\n}\n}\n/* \u5168\u6392\u5217 I */\nvector<vector<int>> permutationsI(vector<int> nums) {\nvector<int> state;\nvector<bool> selected(nums.size(), false);\nvector<vector<int>> res;\nbacktrack(state, nums, selected, res);\nreturn res;\n}\n
    permutations_i.py
    def backtrack(\nstate: 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\nif len(state) == len(choices):\nres.append(list(state))\nreturn\n# \u904d\u5386\u6240\u6709\u9009\u62e9\nfor i, choice in enumerate(choices):\n# \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\nif not selected[i]:\n# \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nselected[i] = True\nstate.append(choice)\n# \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res)\n# \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = False\nstate.pop()\ndef permutations_i(nums: list[int]) -> list[list[int]]:\n\"\"\"\u5168\u6392\u5217 I\"\"\"\nres = []\nbacktrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\nreturn res\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\nif len(*state) == len(*choices) {\nnewState := append([]int{}, *state...)\n*res = append(*res, newState)\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor i := 0; i < len(*choices); i++ {\nchoice := (*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\nif !(*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\nbacktrackI(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/* \u5168\u6392\u5217 I */\nfunc permutationsI(nums []int) [][]int {\nres := make([][]int, 0)\nstate := make([]int, 0)\nselected := make([]bool, len(nums))\nbacktrackI(&state, &nums, &selected, &res)\nreturn 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\nif (state.length === choices.length) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nchoices.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\nif (!selected[i]) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nselected[i] = true;\nstate.push(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.pop();\n}\n});\n}\n/* \u5168\u6392\u5217 I */\nfunction permutationsI(nums) {\nconst res = [];\nbacktrack([], nums, Array(nums.length).fill(false), res);\nreturn res;\n}\n
    permutations_i.ts
    /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunction backtrack(\nstate: number[],\nchoices: number[],\nselected: boolean[],\nres: number[][]\n): void {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (state.length === choices.length) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nchoices.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\nif (!selected[i]) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nselected[i] = true;\nstate.push(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.pop();\n}\n});\n}\n/* \u5168\u6392\u5217 I */\nfunction permutationsI(nums: number[]): number[][] {\nconst res: number[][] = [];\nbacktrack([], nums, Array(nums.length).fill(false), res);\nreturn res;\n}\n
    permutations_i.c
    [class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsI}\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\nif (state.Count == choices.Length) {\nres.Add(new List<int>(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i < choices.Length; i++) {\nint 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\nif (!selected[i]) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nselected[i] = true;\nstate.Add(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.RemoveAt(state.Count - 1);\n}\n}\n}\n/* \u5168\u6392\u5217 I */\nList<List<int>> permutationsI(int[] nums) {\nList<List<int>> res = new List<List<int>>();\nbacktrack(new List<int>(), nums, new bool[nums.Length], res);\nreturn 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\nif state.count == choices.count {\nres.append(state)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (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\nif !selected[i] {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nselected[i] = true\nstate.append(choice)\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(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\nselected[i] = false\nstate.removeLast()\n}\n}\n}\n/* \u5168\u6392\u5217 I */\nfunc permutationsI(nums: [Int]) -> [[Int]] {\nvar state: [Int] = []\nvar selected = Array(repeating: false, count: nums.count)\nvar res: [[Int]] = []\nbacktrack(state: &state, choices: nums, selected: &selected, res: &res)\nreturn res\n}\n
    permutations_i.zig
    [class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsI}\n
    permutations_i.dart
    [class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsI}\n

    \u9700\u8981\u91cd\u70b9\u5173\u6ce8\u7684\u662f\uff0c\u6211\u4eec\u5f15\u5165\u4e86\u4e00\u4e2a\u5e03\u5c14\u578b\u6570\u7ec4 selected \uff0c\u5b83\u7684\u957f\u5ea6\u4e0e\u8f93\u5165\u6570\u7ec4\u957f\u5ea6\u76f8\u7b49\uff0c\u5176\u4e2d selected[i] \u8868\u793a choices[i] \u662f\u5426\u5df2\u88ab\u9009\u62e9\u3002\u6211\u4eec\u5229\u7528 selected \u907f\u514d\u67d0\u4e2a\u5143\u7d20\u88ab\u91cd\u590d\u9009\u62e9\uff0c\u4ece\u800c\u5b9e\u73b0\u526a\u679d\u3002

    \u5982\u4e0b\u56fe\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, 3 \u7684\u5206\u652f\u3002\u4ece\u672c\u8d28\u4e0a\u7406\u89e3\uff0c\u6b64\u526a\u679d\u64cd\u4f5c\u53ef\u5c06\u641c\u7d22\u7a7a\u95f4\u5927\u5c0f\u4ece \\(O(n^n)\\) \u964d\u4f4e\u81f3 \\(O(n!)\\) \u3002

    Fig. \u5168\u6392\u5217\u526a\u679d\u793a\u4f8b

    "},{"location":"chapter_backtracking/permutations_problem/#1222","title":"12.2.2. \u00a0 \u8003\u8651\u91cd\u590d\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\u7684\u5143\u7d20 \\(1\\) \uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u7b2c\u4e8c\u4e2a\u5143\u7d20\u8bb0\u4e3a \\(\\hat{1}\\) \u3002\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u4e0a\u8ff0\u65b9\u6cd5\u751f\u6210\u7684\u6392\u5217\u6709\u4e00\u534a\u90fd\u662f\u91cd\u590d\u7684\u3002

    Fig. \u91cd\u590d\u6392\u5217

    \u90a3\u4e48\uff0c\u5982\u4f55\u53bb\u9664\u91cd\u590d\u7684\u6392\u5217\u5462\uff1f\u6700\u76f4\u63a5\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868\uff0c\u76f4\u63a5\u5bf9\u6392\u5217\u7ed3\u679c\u8fdb\u884c\u53bb\u91cd\u3002\u7136\u800c\uff0c\u8fd9\u6837\u505a\u4e0d\u591f\u4f18\u96c5\uff0c\u56e0\u4e3a\u751f\u6210\u91cd\u590d\u6392\u5217\u7684\u641c\u7d22\u5206\u652f\u662f\u6ca1\u6709\u5fc5\u8981\u7684\uff0c\u5e94\u5f53\u88ab\u63d0\u524d\u8bc6\u522b\u5e76\u526a\u679d\uff0c\u8fd9\u6837\u53ef\u4ee5\u63d0\u5347\u7b97\u6cd5\u6548\u7387\u3002

    \u89c2\u5bdf\u53d1\u73b0\uff0c\u5728\u7b2c\u4e00\u8f6e\u4e2d\uff0c\u9009\u62e9 \\(1\\) \u6216\u9009\u62e9 \\(\\hat{1}\\) \u662f\u7b49\u4ef7\u7684\uff0c\u56e0\u4e3a\u5728\u8fd9\u4e24\u4e2a\u9009\u62e9\u4e4b\u4e0b\u751f\u6210\u7684\u6240\u6709\u6392\u5217\u90fd\u662f\u91cd\u590d\u7684\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5e94\u8be5\u628a \\(\\hat{1}\\) \u526a\u679d\u6389\u3002\u540c\u7406\uff0c\u5728\u7b2c\u4e00\u8f6e\u9009\u62e9 \\(2\\) \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\u9700\u8981\u5c06\u7b2c\u4e8c\u8f6e\u7684 \\(\\hat{1}\\) \u526a\u679d\u3002

    Fig. \u91cd\u590d\u6392\u5217\u526a\u679d

    \u672c\u8d28\u4e0a\u770b\uff0c\u6211\u4eec\u7684\u76ee\u6807\u662f\u5b9e\u73b0\u5728\u67d0\u4e00\u8f6e\u9009\u62e9\u4e2d\uff0c\u591a\u4e2a\u76f8\u7b49\u7684\u5143\u7d20\u4ec5\u88ab\u9009\u62e9\u4e00\u6b21\u3002\u56e0\u6b64\uff0c\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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nif (state.size() == choices.length) {\nres.add(new ArrayList<Integer>(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nSet<Integer> duplicated = new HashSet<Integer>();\nfor (int i = 0; i < choices.length; i++) {\nint 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\nif (!selected[i] && !duplicated.contains(choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nduplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\nselected[i] = true;\nstate.add(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.remove(state.size() - 1);\n}\n}\n}\n/* \u5168\u6392\u5217 II */\nList<List<Integer>> permutationsII(int[] nums) {\nList<List<Integer>> res = new ArrayList<List<Integer>>();\nbacktrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);\nreturn res;\n}\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\nif (state.size() == choices.size()) {\nres.push_back(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nunordered_set<int> duplicated;\nfor (int i = 0; i < choices.size(); i++) {\nint 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\nif (!selected[i] && duplicated.find(choice) == duplicated.end()) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nduplicated.emplace(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\nselected[i] = true;\nstate.push_back(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.pop_back();\n}\n}\n}\n/* \u5168\u6392\u5217 II */\nvector<vector<int>> permutationsII(vector<int> nums) {\nvector<int> state;\nvector<bool> selected(nums.size(), false);\nvector<vector<int>> res;\nbacktrack(state, nums, selected, res);\nreturn res;\n}\n
    permutations_ii.py
    def backtrack(\nstate: 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\nif len(state) == len(choices):\nres.append(list(state))\nreturn\n# \u904d\u5386\u6240\u6709\u9009\u62e9\nduplicated = set[int]()\nfor 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\nif not selected[i] and choice not in duplicated:\n# \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nduplicated.add(choice)  # \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\nselected[i] = True\nstate.append(choice)\n# \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res)\n# \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = False\nstate.pop()\ndef permutations_ii(nums: list[int]) -> list[list[int]]:\n\"\"\"\u5168\u6392\u5217 II\"\"\"\nres = []\nbacktrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\nreturn res\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\nif len(*state) == len(*choices) {\nnewState := append([]int{}, *state...)\n*res = append(*res, newState)\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nduplicated := make(map[int]struct{}, 0)\nfor i := 0; i < len(*choices); i++ {\nchoice := (*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\nif _, 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\nduplicated[choice] = struct{}{}\n(*selected)[i] = true\n*state = append(*state, choice)\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrackI(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/* \u5168\u6392\u5217 II */\nfunc permutationsII(nums []int) [][]int {\nres := make([][]int, 0)\nstate := make([]int, 0)\nselected := make([]bool, len(nums))\nbacktrackII(&state, &nums, &selected, &res)\nreturn 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\nif (state.length === choices.length) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nconst duplicated = new Set();\nchoices.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\nif (!selected[i] && !duplicated.has(choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nduplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\nselected[i] = true;\nstate.push(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.pop();\n}\n});\n}\n/* \u5168\u6392\u5217 II */\nfunction permutationsII(nums) {\nconst res = [];\nbacktrack([], nums, Array(nums.length).fill(false), res);\nreturn res;\n}\n
    permutations_ii.ts
    /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunction backtrack(\nstate: number[],\nchoices: number[],\nselected: boolean[],\nres: number[][]\n): void {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (state.length === choices.length) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nconst duplicated = new Set();\nchoices.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\nif (!selected[i] && !duplicated.has(choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nduplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\nselected[i] = true;\nstate.push(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.pop();\n}\n});\n}\n/* \u5168\u6392\u5217 II */\nfunction permutationsII(nums: number[]): number[][] {\nconst res: number[][] = [];\nbacktrack([], nums, Array(nums.length).fill(false), res);\nreturn res;\n}\n
    permutations_ii.c
    [class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsII}\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\nif (state.Count == choices.Length) {\nres.Add(new List<int>(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nISet<int> duplicated = new HashSet<int>();\nfor (int i = 0; i < choices.Length; i++) {\nint 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\nif (!selected[i] && !duplicated.Contains(choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nduplicated.Add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\nselected[i] = true;\nstate.Add(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.RemoveAt(state.Count - 1);\n}\n}\n}\n/* \u5168\u6392\u5217 II */\nList<List<int>> permutationsII(int[] nums) {\nList<List<int>> res = new List<List<int>>();\nbacktrack(new List<int>(), nums, new bool[nums.Length], res);\nreturn 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\nif state.count == choices.count {\nres.append(state)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nvar duplicated: Set<Int> = []\nfor (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\nif !selected[i], !duplicated.contains(choice) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nduplicated.insert(choice) // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\nselected[i] = true\nstate.append(choice)\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(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\nselected[i] = false\nstate.removeLast()\n}\n}\n}\n/* \u5168\u6392\u5217 II */\nfunc permutationsII(nums: [Int]) -> [[Int]] {\nvar state: [Int] = []\nvar selected = Array(repeating: false, count: nums.count)\nvar res: [[Int]] = []\nbacktrack(state: &state, choices: nums, selected: &selected, res: &res)\nreturn res\n}\n
    permutations_ii.zig
    [class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsII}\n
    permutations_ii.dart
    [class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsII}\n

    \u6ce8\u610f\uff0c\u867d\u7136 selected \u548c duplicated \u90fd\u8d77\u5230\u526a\u679d\u7684\u4f5c\u7528\uff0c\u4f46\u4ed6\u4eec\u526a\u6389\u7684\u662f\u4e0d\u540c\u7684\u5206\u652f\uff1a

    • \u526a\u679d\u6761\u4ef6\u4e00\uff1a\u6574\u4e2a\u641c\u7d22\u8fc7\u7a0b\u4e2d\u53ea\u6709\u4e00\u4e2a selected \u3002\u5b83\u8bb0\u5f55\u7684\u662f\u5f53\u524d\u72b6\u6001\u4e2d\u5305\u542b\u54ea\u4e9b\u5143\u7d20\uff0c\u4f5c\u7528\u662f\u907f\u514d\u67d0\u4e2a\u5143\u7d20\u5728 state \u4e2d\u91cd\u590d\u51fa\u73b0\u3002
    • \u526a\u679d\u6761\u4ef6\u4e8c\uff1a\u6bcf\u8f6e\u9009\u62e9\uff08\u5373\u6bcf\u4e2a\u5f00\u542f\u7684 backtrack \u51fd\u6570\uff09\u90fd\u5305\u542b\u4e00\u4e2a duplicated \u3002\u5b83\u8bb0\u5f55\u7684\u662f\u5728\u904d\u5386\u4e2d\u54ea\u4e9b\u5143\u7d20\u5df2\u88ab\u9009\u62e9\u8fc7\uff0c\u4f5c\u7528\u662f\u4fdd\u8bc1\u76f8\u7b49\u5143\u7d20\u53ea\u88ab\u9009\u62e9\u4e00\u6b21\uff0c\u4ee5\u907f\u514d\u4ea7\u751f\u91cd\u590d\u7684\u641c\u7d22\u5206\u652f\u3002

    \u4e0b\u56fe\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

    Fig. \u4e24\u79cd\u526a\u679d\u6761\u4ef6\u7684\u4f5c\u7528\u8303\u56f4

    "},{"location":"chapter_backtracking/permutations_problem/#1223","title":"12.2.3. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"

    \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\uff0c\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\uff0c\u5168\u6392\u5217 I \u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u5168\u6392\u5217 II \u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002

    "},{"location":"chapter_computational_complexity/","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":""},{"location":"chapter_computational_complexity/performance_evaluation/","title":"2.1. \u00a0 \u7b97\u6cd5\u6548\u7387\u8bc4\u4f30","text":""},{"location":"chapter_computational_complexity/performance_evaluation/#211","title":"2.1.1. \u00a0 \u7b97\u6cd5\u8bc4\u4ef7\u7ef4\u5ea6","text":"

    \u4ece\u603b\u4f53\u4e0a\u770b\uff0c\u7b97\u6cd5\u8bbe\u8ba1\u8ffd\u6c42\u4ee5\u4e0b\u4e24\u4e2a\u5c42\u9762\u7684\u76ee\u6807\uff1a

    1. \u627e\u5230\u95ee\u9898\u89e3\u6cd5\u3002\u7b97\u6cd5\u9700\u8981\u5728\u89c4\u5b9a\u7684\u8f93\u5165\u8303\u56f4\u5185\uff0c\u53ef\u9760\u5730\u6c42\u5f97\u95ee\u9898\u7684\u6b63\u786e\u89e3\u3002
    2. \u5bfb\u6c42\u6700\u4f18\u89e3\u6cd5\u3002\u540c\u4e00\u4e2a\u95ee\u9898\u53ef\u80fd\u5b58\u5728\u591a\u79cd\u89e3\u6cd5\uff0c\u6211\u4eec\u5e0c\u671b\u627e\u5230\u5c3d\u53ef\u80fd\u9ad8\u6548\u7684\u7b97\u6cd5\u3002

    \u56e0\u6b64\uff0c\u5728\u80fd\u591f\u89e3\u51b3\u95ee\u9898\u7684\u524d\u63d0\u4e0b\uff0c\u7b97\u6cd5\u6548\u7387\u6210\u4e3a\u4e3b\u8981\u7684\u8bc4\u4ef7\u7ef4\u5ea6\uff0c\u4e3b\u8981\u5305\u62ec\uff1a

    • \u65f6\u95f4\u6548\u7387\uff0c\u5373\u7b97\u6cd5\u8fd0\u884c\u901f\u5ea6\u7684\u5feb\u6162\u3002
    • \u7a7a\u95f4\u6548\u7387\uff0c\u5373\u7b97\u6cd5\u5360\u7528\u5185\u5b58\u7a7a\u95f4\u7684\u5927\u5c0f\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\u638c\u63e1\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\u7684\u65b9\u6cd5\u5219\u81f3\u5173\u91cd\u8981\uff0c\u56e0\u4e3a\u53ea\u6709\u4e86\u89e3\u8bc4\u4ef7\u6807\u51c6\uff0c\u6211\u4eec\u624d\u80fd\u8fdb\u884c\u7b97\u6cd5\u4e4b\u95f4\u7684\u5bf9\u6bd4\u5206\u6790\uff0c\u4ece\u800c\u6307\u5bfc\u7b97\u6cd5\u8bbe\u8ba1\u4e0e\u4f18\u5316\u8fc7\u7a0b\u3002

    "},{"location":"chapter_computational_complexity/performance_evaluation/#212","title":"2.1.2. \u00a0 \u6548\u7387\u8bc4\u4f30\u65b9\u6cd5","text":""},{"location":"chapter_computational_complexity/performance_evaluation/#_1","title":"\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\u6211\u4eec\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u5c31\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\u5c40\u9650\u6027\u3002

    \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\u8868\u73b0\u3002\u4f8b\u5982\uff0c\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\u6211\u4eec\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\u800c\u8fd9\u662f\u4e0d\u73b0\u5b9e\u7684\u3002

    \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\u8f93\u5165\u6570\u636e\u91cf\u8f83\u5c0f\u65f6\uff0c\u7b97\u6cd5 A \u7684\u8fd0\u884c\u65f6\u95f4\u53ef\u80fd\u77ed\u4e8e\u7b97\u6cd5 B\uff1b\u800c\u8f93\u5165\u6570\u636e\u91cf\u8f83\u5927\u65f6\uff0c\u6d4b\u8bd5\u7ed3\u679c\u53ef\u80fd\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\u8fd9\u6837\u9700\u8981\u5360\u7528\u5927\u91cf\u7684\u8ba1\u7b97\u8d44\u6e90\u3002

    "},{"location":"chapter_computational_complexity/performance_evaluation/#_2","title":"\u7406\u8bba\u4f30\u7b97","text":"

    \u7531\u4e8e\u5b9e\u9645\u6d4b\u8bd5\u5177\u6709\u8f83\u5927\u7684\u5c40\u9650\u6027\uff0c\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\u590d\u6742\u5ea6\u5206\u6790 Complexity Analysis\u300d\u6216\u300c\u6e10\u8fd1\u590d\u6742\u5ea6\u5206\u6790 Asymptotic Complexity Analysis\u300d\u3002

    \u590d\u6742\u5ea6\u5206\u6790\u8bc4\u4f30\u7684\u662f\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u968f\u7740\u8f93\u5165\u6570\u636e\u91cf\u589e\u591a\u65f6\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\uff1a

    • \u201c\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u201d\u53ef\u5206\u4e3a\u201c\u8fd0\u884c\u65f6\u95f4\u201d\u548c\u201c\u5360\u7528\u7a7a\u95f4\u201d\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u5c06\u590d\u6742\u5ea6\u5206\u4e3a\u300c\u65f6\u95f4\u590d\u6742\u5ea6 Time Complexity\u300d\u548c\u300c\u7a7a\u95f4\u590d\u6742\u5ea6 Space Complexity\u300d\uff1b
    • \u201c\u968f\u7740\u8f93\u5165\u6570\u636e\u91cf\u589e\u591a\u65f6\u201d\u8868\u793a\u590d\u6742\u5ea6\u4e0e\u8f93\u5165\u6570\u636e\u91cf\u6709\u5173\uff0c\u53cd\u6620\u4e86\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u4e0e\u8f93\u5165\u6570\u636e\u91cf\u4e4b\u95f4\u7684\u5173\u7cfb\uff1b
    • \u201c\u589e\u957f\u8d8b\u52bf\u201d\u8868\u793a\u590d\u6742\u5ea6\u5206\u6790\u5173\u6ce8\u7684\u662f\u7b97\u6cd5\u65f6\u95f4\u4e0e\u7a7a\u95f4\u7684\u589e\u957f\u8d8b\u52bf\uff0c\u800c\u975e\u5177\u4f53\u7684\u8fd0\u884c\u65f6\u95f4\u6216\u5360\u7528\u7a7a\u95f4\uff1b

    \u590d\u6742\u5ea6\u5206\u6790\u514b\u670d\u4e86\u5b9e\u9645\u6d4b\u8bd5\u65b9\u6cd5\u7684\u5f0a\u7aef\u3002\u9996\u5148\uff0c\u5b83\u72ec\u7acb\u4e8e\u6d4b\u8bd5\u73af\u5883\uff0c\u56e0\u6b64\u5206\u6790\u7ed3\u679c\u9002\u7528\u4e8e\u6240\u6709\u8fd0\u884c\u5e73\u53f0\u3002\u5176\u6b21\uff0c\u5b83\u53ef\u4ee5\u4f53\u73b0\u4e0d\u540c\u6570\u636e\u91cf\u4e0b\u7684\u7b97\u6cd5\u6548\u7387\uff0c\u5c24\u5176\u662f\u5728\u5927\u6570\u636e\u91cf\u4e0b\u7684\u7b97\u6cd5\u6027\u80fd\u3002

    \u5982\u679c\u4f60\u5bf9\u590d\u6742\u5ea6\u5206\u6790\u7684\u6982\u5ff5\u4ecd\u611f\u5230\u56f0\u60d1\uff0c\u65e0\u9700\u62c5\u5fc3\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7ae0\u8282\u8be6\u7ec6\u4ecb\u7ecd\u3002

    "},{"location":"chapter_computational_complexity/performance_evaluation/#213","title":"2.1.3. \u00a0 \u590d\u6742\u5ea6\u5206\u6790\u91cd\u8981\u6027","text":"

    \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\u544a\u8bc9\u6211\u4eec\u6267\u884c\u67d0\u4e2a\u7b97\u6cd5\u6240\u9700\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u8d44\u6e90\uff0c\u5e76\u4f7f\u6211\u4eec\u80fd\u591f\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\u7b2c\u4e00\u7ae0\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\u6211\u4eec\u96be\u4ee5\u907f\u514d\u8981\u5206\u6790\u5176\u8fd0\u884c\u901f\u5ea6\u548c\u7a7a\u95f4\u4f7f\u7528\u60c5\u51b5\u3002\u56e0\u6b64\uff0c\u5728\u6df1\u5165\u5b66\u4e60\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u4e4b\u524d\uff0c\u5efa\u8bae\u8bfb\u8005\u5148\u5bf9\u590d\u6742\u5ea6\u5efa\u7acb\u521d\u6b65\u7684\u4e86\u89e3\uff0c\u5e76\u80fd\u591f\u5b8c\u6210\u7b80\u5355\u6848\u4f8b\u7684\u590d\u6742\u5ea6\u5206\u6790\u3002

    "},{"location":"chapter_computational_complexity/space_complexity/","title":"2.3. \u00a0 \u7a7a\u95f4\u590d\u6742\u5ea6","text":"

    \u300c\u7a7a\u95f4\u590d\u6742\u5ea6 Space Complexity\u300d\u7528\u4e8e\u8861\u91cf\u7b97\u6cd5\u4f7f\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\u3002

    "},{"location":"chapter_computational_complexity/space_complexity/#231","title":"2.3.1. \u00a0 \u7b97\u6cd5\u76f8\u5173\u7a7a\u95f4","text":"

    \u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\uff1a

    • \u300c\u8f93\u5165\u7a7a\u95f4\u300d\u7528\u4e8e\u5b58\u50a8\u7b97\u6cd5\u7684\u8f93\u5165\u6570\u636e\uff1b
    • \u300c\u6682\u5b58\u7a7a\u95f4\u300d\u7528\u4e8e\u5b58\u50a8\u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u53d8\u91cf\u3001\u5bf9\u8c61\u3001\u51fd\u6570\u4e0a\u4e0b\u6587\u7b49\u6570\u636e\uff1b
    • \u300c\u8f93\u51fa\u7a7a\u95f4\u300d\u7528\u4e8e\u5b58\u50a8\u7b97\u6cd5\u7684\u8f93\u51fa\u6570\u636e\uff1b

    \u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u7edf\u8ba1\u8303\u56f4\u662f\u300c\u6682\u5b58\u7a7a\u95f4\u300d+\u300c\u8f93\u51fa\u7a7a\u95f4\u300d\u3002

    \u6682\u5b58\u7a7a\u95f4\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5212\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\uff1a

    • \u300c\u6682\u5b58\u6570\u636e\u300d\u7528\u4e8e\u4fdd\u5b58\u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u5404\u79cd\u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u7b49\u3002
    • \u300c\u6808\u5e27\u7a7a\u95f4\u300d\u7528\u4e8e\u4fdd\u5b58\u8c03\u7528\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u6570\u636e\u3002\u7cfb\u7edf\u5728\u6bcf\u6b21\u8c03\u7528\u51fd\u6570\u65f6\u90fd\u4f1a\u5728\u6808\u9876\u90e8\u521b\u5efa\u4e00\u4e2a\u6808\u5e27\uff0c\u51fd\u6570\u8fd4\u56de\u540e\uff0c\u6808\u5e27\u7a7a\u95f4\u4f1a\u88ab\u91ca\u653e\u3002
    • \u300c\u6307\u4ee4\u7a7a\u95f4\u300d\u7528\u4e8e\u4fdd\u5b58\u7f16\u8bd1\u540e\u7684\u7a0b\u5e8f\u6307\u4ee4\uff0c\u5728\u5b9e\u9645\u7edf\u8ba1\u4e2d\u901a\u5e38\u5ffd\u7565\u4e0d\u8ba1\u3002

    \u56e0\u6b64\uff0c\u5728\u5206\u6790\u4e00\u6bb5\u7a0b\u5e8f\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u65f6\uff0c\u6211\u4eec\u4e00\u822c\u7edf\u8ba1 \u6682\u5b58\u6570\u636e\u3001\u8f93\u51fa\u6570\u636e\u3001\u6808\u5e27\u7a7a\u95f4 \u4e09\u90e8\u5206\u3002

    Fig. \u7b97\u6cd5\u4f7f\u7528\u7684\u76f8\u5173\u7a7a\u95f4

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    /* \u7c7b */\nclass Node {\nint val;\nNode next;\nNode(int x) { val = x; }\n}\n/* \u51fd\u6570 */\nint function() {\n// do something...\nreturn 0;\n}\nint algorithm(int n) {        // \u8f93\u5165\u6570\u636e\nfinal int a = 0;          // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nint b = 0;                // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nNode node = new Node(0);  // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nint c = function();       // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c;         // \u8f93\u51fa\u6570\u636e\n}\n
    /* \u7ed3\u6784\u4f53 */\nstruct Node {\nint val;\nNode *next;\nNode(int x) : val(x), next(nullptr) {}\n};\n/* \u51fd\u6570 */\nint func() {\n// do something...\nreturn 0;\n}\nint algorithm(int n) {        // \u8f93\u5165\u6570\u636e\nconst int a = 0;          // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nint b = 0;                // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nNode* node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nint c = func();           // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c;         // \u8f93\u51fa\u6570\u636e\n}\n
    class Node:\n\"\"\"\u7c7b\"\"\"\ndef __init__(self, x: int):\nself.val: int = x                 # \u8282\u70b9\u503c\nself.next: Optional[Node] = None  # \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\ndef function() -> int:\n\"\"\"\u51fd\u6570\"\"\"\n# do something...\nreturn 0\ndef algorithm(n) -> int:  # \u8f93\u5165\u6570\u636e\nA = 0                 # \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff0c\u4e00\u822c\u7528\u5927\u5199\u5b57\u6bcd\u8868\u793a\uff09\nb = 0                 # \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nnode = Node(0)        # \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nc = function()        # \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn A + b + c      # \u8f93\u51fa\u6570\u636e\n
    /* \u7ed3\u6784\u4f53 */\ntype node struct {\nval  int\nnext *node\n}\n/* \u521b\u5efa node \u7ed3\u6784\u4f53  */\nfunc newNode(val int) *node {\nreturn &node{val: val}\n}\n/* \u51fd\u6570 */\nfunc function() int {\n// do something...\nreturn 0\n}\nfunc algorithm(n int) int { // \u8f93\u5165\u6570\u636e\nconst a = 0             // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nb := 0                  // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nnewNode(0)              // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nc := function()         // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c        // \u8f93\u51fa\u6570\u636e\n}\n
    /* \u7c7b */\nclass Node {\nval;\nnext;\nconstructor(val) {\nthis.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\nthis.next = null;                       // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n}\n}\n/* \u51fd\u6570 */\nfunction constFunc() {\n// do something\nreturn 0;\n}\nfunction algorithm(n) {       // \u8f93\u5165\u6570\u636e\nconst a = 0;              // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nlet b = 0;                // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nconst node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nconst c = constFunc();    // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c;         // \u8f93\u51fa\u6570\u636e\n}\n
    /* \u7c7b */\nclass Node {\nval: number;\nnext: Node | null;\nconstructor(val?: number) {\nthis.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\nthis.next = null;                       // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n}\n}\n/* \u51fd\u6570 */\nfunction constFunc(): number {\n// do something\nreturn 0;\n}\nfunction algorithm(n: number): number { // \u8f93\u5165\u6570\u636e\nconst a = 0;                        // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nlet b = 0;                          // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nconst node = new Node(0);           // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nconst c = constFunc();              // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c;                   // \u8f93\u51fa\u6570\u636e\n}\n
    /* \u51fd\u6570 */\nint func() {\n// do something...\nreturn 0;\n}\nint algorithm(int n) { // \u8f93\u5165\u6570\u636e\nconst int a = 0;   // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nint b = 0;         // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nint c = func();    // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c;  // \u8f93\u51fa\u6570\u636e\n}\n
    /* \u7c7b */\nclass Node\n{\nint val;\nNode next;\nNode(int x) { val = x; }\n}\n/* \u51fd\u6570 */\nint function()\n{\n// do something...\nreturn 0;\n}\nint algorithm(int n)          // \u8f93\u5165\u6570\u636e\n{\nconst int a = 0;          // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nint b = 0;                // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nNode node = new Node(0);  // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nint c = function();       // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c;         // \u8f93\u51fa\u6570\u636e\n}\n
    /* \u7c7b */\nclass Node {\nvar val: Int\nvar next: Node?\ninit(x: Int) {\nval = x\n}\n}\n/* \u51fd\u6570 */\nfunc function() -> Int {\n// do something...\nreturn 0\n}\nfunc algorithm(n: Int) -> Int { // \u8f93\u5165\u6570\u636e\nlet a = 0             // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nvar b = 0             // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nlet node = Node(x: 0) // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nlet c = function()    // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c      // \u8f93\u51fa\u6570\u636e\n}\n
    \n
    /* \u7c7b */\nclass Node {\nint val;\nNode next;\nNode(this.val, [this.next]);\n}\n/* \u51fd\u6570 */\nint function() {\n// do something...\nreturn 0;\n}\nint algorithm(int n) {  // \u8f93\u5165\u6570\u636e\nconst int a = 0;      // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nint b = 0;            // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nNode node = Node(0);  // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nint c = function();   // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c;     // \u8f93\u51fa\u6570\u636e\n}\n
    "},{"location":"chapter_computational_complexity/space_complexity/#232","title":"2.3.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\u662f\u5c06\u7edf\u8ba1\u5bf9\u8c61\u4ece\u201c\u8ba1\u7b97\u64cd\u4f5c\u6570\u91cf\u201d\u8f6c\u4e3a\u201c\u4f7f\u7528\u7a7a\u95f4\u5927\u5c0f\u201d\u3002\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u540c\u7684\u662f\uff0c\u6211\u4eec\u901a\u5e38\u53ea\u5173\u6ce8\u300c\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u300d\uff0c\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

    \u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e2d\u7684\u201c\u6700\u5dee\u201d\u6709\u4e24\u5c42\u542b\u4e49\uff0c\u5206\u522b\u662f\u8f93\u5165\u6570\u636e\u7684\u6700\u5dee\u5206\u5e03\u548c\u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u6700\u5dee\u65f6\u95f4\u70b9\u3002

    • \u4ee5\u6700\u5dee\u8f93\u5165\u6570\u636e\u4e3a\u51c6\u3002\u5f53 \\(n < 10\\) \u65f6\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff1b\u4f46\u5f53 \\(n > 10\\) \u65f6\uff0c\u521d\u59cb\u5316\u7684\u6570\u7ec4 nums \u5360\u7528 \\(O(n)\\) \u7a7a\u95f4\uff1b\u56e0\u6b64\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff1b
    • \u4ee5\u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u5cf0\u503c\u5185\u5b58\u4e3a\u51c6\u3002\u4f8b\u5982\uff0c\u7a0b\u5e8f\u5728\u6267\u884c\u6700\u540e\u4e00\u884c\u4e4b\u524d\uff0c\u5360\u7528 \\(O(1)\\) \u7a7a\u95f4\uff1b\u5f53\u521d\u59cb\u5316\u6570\u7ec4 nums \u65f6\uff0c\u7a0b\u5e8f\u5360\u7528 \\(O(n)\\) \u7a7a\u95f4\uff1b\u56e0\u6b64\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff1b
    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    void algorithm(int n) {\nint a = 0;                   // O(1)\nint[] b = new int[10000];    // O(1)\nif (n > 10)\nint[] nums = new int[n]; // O(n)\n}\n
    void algorithm(int n) {\nint a = 0;               // O(1)\nvector<int> b(10000);    // O(1)\nif (n > 10)\nvector<int> nums(n); // O(n)\n}\n
    def algorithm(n: int) -> None:\na = 0                     # O(1)\nb = [0] * 10000     # O(1)\nif n > 10:\nnums = [0] * n  # O(n)\n
    func algorithm(n int) {\na := 0                      // O(1)\nb := make([]int, 10000)     // O(1)\nvar nums []int\nif n > 10 {\nnums := make([]int, n)  // O(n)\n}\nfmt.Println(a, b, nums)\n}\n
    function algorithm(n) {\nconst a = 0;                   // O(1)\nconst b = new Array(10000);    // O(1)\nif (n > 10) {\nconst nums = new Array(n); // O(n)\n}\n}\n
    function algorithm(n: number): void {\nconst a = 0;                   // O(1)\nconst b = new Array(10000);    // O(1)\nif (n > 10) {\nconst nums = new Array(n); // O(n)\n}\n}\n
    void algorithm(int n) {\nint a = 0;               // O(1)\nint b[10000];            // O(1)\nif (n > 10)\nvector<int> nums(n); // O(n)\n}\n
    void algorithm(int n)\n{\nint a = 0;                   // O(1)\nint[] b = new int[10000];    // O(1)\nif (n > 10)\n{\nint[] nums = new int[n]; // O(n)\n}\n}\n
    func algorithm(n: Int) {\nlet a = 0 // O(1)\nlet b = Array(repeating: 0, count: 10000) // O(1)\nif n > 10 {\nlet nums = Array(repeating: 0, count: n) // O(n)\n}\n}\n
    \n
    void algorithm(int n) {\nint a = 0;                            // O(1)\nList<int> b = List.filled(10000, 0);  // O(1)\nif (n > 10) {\nList<int> nums = List.filled(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\u4f8b\u5982\uff0c\u51fd\u6570 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)\\) \u3002\u800c\u9012\u5f52\u51fd\u6570 recur() \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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    int function() {\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\nfor (int i = 0; i < n; i++) {\nfunction();\n}\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\nif (n == 1) return;\nreturn recur(n - 1);\n}\n
    int func() {\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\nfor (int i = 0; i < n; i++) {\nfunc();\n}\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\nif (n == 1) return;\nreturn recur(n - 1);\n}\n
    def function() -> int:\n# do something\nreturn 0\ndef loop(n: int) -> None:\n\"\"\"\u5faa\u73af O(1)\"\"\"\nfor _ in range(n):\nfunction()\ndef recur(n: int) -> int:\n\"\"\"\u9012\u5f52 O(n)\"\"\"\nif n == 1: return\nreturn recur(n - 1)\n
    func function() int {\n// do something\nreturn 0\n}\n/* \u5faa\u73af O(1) */\nfunc loop(n int) {\nfor i := 0; i < n; i++ {\nfunction()\n}\n}\n/* \u9012\u5f52 O(n) */\nfunc recur(n int) {\nif n == 1 {\nreturn\n}\nrecur(n - 1)\n}\n
    function constFunc() {\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nfunction loop(n) {\nfor (let i = 0; i < n; i++) {\nconstFunc();\n}\n}\n/* \u9012\u5f52 O(n) */\nfunction recur(n) {\nif (n === 1) return;\nreturn recur(n - 1);\n}\n
    function constFunc(): number {\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nfunction loop(n: number): void {\nfor (let i = 0; i < n; i++) {\nconstFunc();\n}\n}\n/* \u9012\u5f52 O(n) */\nfunction recur(n: number): void {\nif (n === 1) return;\nreturn recur(n - 1);\n}\n
    int func() {\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\nfor (int i = 0; i < n; i++) {\nfunc();\n}\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\nif (n == 1) return;\nreturn recur(n - 1);\n}\n
    int function()\n{\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n)\n{\nfor (int i = 0; i < n; i++)\n{\nfunction();\n}\n}\n/* \u9012\u5f52 O(n) */\nint recur(int n)\n{\nif (n == 1) return 1;\nreturn recur(n - 1);\n}\n
    @discardableResult\nfunc function() -> Int {\n// do something\nreturn 0\n}\n/* \u5faa\u73af O(1) */\nfunc loop(n: Int) {\nfor _ in 0 ..< n {\nfunction()\n}\n}\n/* \u9012\u5f52 O(n) */\nfunc recur(n: Int) {\nif n == 1 {\nreturn\n}\nrecur(n: n - 1)\n}\n
    \n
    int function() {\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\nfor (int i = 0; i < n; i++) {\nfunction();\n}\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\nif (n == 1) return;\nreturn recur(n - 1);\n}\n
    "},{"location":"chapter_computational_complexity/space_complexity/#233","title":"2.3.3. \u00a0 \u5e38\u89c1\u7c7b\u578b","text":"

    \u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u5e38\u89c1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u7c7b\u578b\u6709\uff08\u4ece\u4f4e\u5230\u9ad8\u6392\u5217\uff09

    \\[ \\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} \\]

    Fig. \u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u5e38\u89c1\u7c7b\u578b

    Tip

    \u90e8\u5206\u793a\u4f8b\u4ee3\u7801\u9700\u8981\u4e00\u4e9b\u524d\u7f6e\u77e5\u8bc6\uff0c\u5305\u62ec\u6570\u7ec4\u3001\u94fe\u8868\u3001\u4e8c\u53c9\u6811\u3001\u9012\u5f52\u7b97\u6cd5\u7b49\u3002\u5982\u679c\u9047\u5230\u770b\u4e0d\u61c2\u7684\u5730\u65b9\u65e0\u9700\u62c5\u5fc3\uff0c\u53ef\u4ee5\u5728\u5b66\u4e60\u5b8c\u540e\u9762\u7ae0\u8282\u540e\u518d\u6765\u590d\u4e60\uff0c\u73b0\u9636\u6bb5\u6211\u4eec\u5148\u4e13\u6ce8\u4e8e\u7406\u89e3\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u542b\u4e49\u548c\u63a8\u7b97\u65b9\u6cd5\u3002

    "},{"location":"chapter_computational_complexity/space_complexity/#o1","title":"\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\u5373\u4e0d\u4f1a\u7d2f\u79ef\u5360\u7528\u7a7a\u95f4\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart space_complexity.java
    /* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nfinal int a = 0;\nint b = 0;\nint[] nums = new int[10000];\nListNode node = new ListNode(0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i < n; i++) {\nint c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i < n; i++) {\nfunction();\n}\n}\n
    space_complexity.cpp
    /* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst int a = 0;\nint b = 0;\nvector<int> nums(10000);\nListNode node(0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i < n; i++) {\nint c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i < n; i++) {\nfunc();\n}\n}\n
    space_complexity.py
    def constant(n: int) -> None:\n\"\"\"\u5e38\u6570\u9636\"\"\"\n# \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\na = 0\nnums = [0] * 10000\nnode = ListNode(0)\n# \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor _ in range(n):\nc = 0\n# \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor _ in range(n):\nfunction()\n
    space_complexity.go
    /* \u5e38\u6570\u9636 */\nfunc spaceConstant(n int) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst a = 0\nb := 0\nnums := make([]int, 10000)\nListNode := newNode(0)\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nvar c int\nfor i := 0; i < n; i++ {\nc = 0\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor i := 0; i < n; i++ {\nfunction()\n}\nfmt.Println(a, b, nums, c, ListNode)\n}\n
    space_complexity.js
    /* \u5e38\u6570\u9636 */\nfunction constant(n) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst a = 0;\nconst b = 0;\nconst nums = new Array(10000);\nconst node = new ListNode(0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (let i = 0; i < n; i++) {\nconst c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (let i = 0; i < n; i++) {\nconstFunc();\n}\n}\n
    space_complexity.ts
    /* \u5e38\u6570\u9636 */\nfunction constant(n: number): void {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst a = 0;\nconst b = 0;\nconst nums = new Array(10000);\nconst node = new ListNode(0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (let i = 0; i < n; i++) {\nconst c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (let i = 0; i < n; i++) {\nconstFunc();\n}\n}\n
    space_complexity.c
    /* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst int a = 0;\nint b = 0;\nint nums[1000];\nListNode *node = newListNode(0);\nfree(node);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i < n; i++) {\nint c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i < n; i++) {\nfunc();\n}\n}\n
    space_complexity.cs
    /* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nint a = 0;\nint b = 0;\nint[] nums = new int[10000];\nListNode node = new ListNode(0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i < n; i++) {\nint c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i < n; i++) {\nfunction();\n}\n}\n
    space_complexity.swift
    /* \u5e38\u6570\u9636 */\nfunc constant(n: Int) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nlet a = 0\nvar b = 0\nlet nums = Array(repeating: 0, count: 10000)\nlet node = ListNode(x: 0)\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor _ in 0 ..< n {\nlet c = 0\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor _ in 0 ..< n {\nfunction()\n}\n}\n
    space_complexity.zig
    // \u5e38\u6570\u9636\nfn constant(n: i32) void {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst a: i32 = 0;\nvar b: i32 = 0;\nvar nums = [_]i32{0}**10000;\nvar node = inc.ListNode(i32){.val = 0};\nvar i: i32 = 0;\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nwhile (i < n) : (i += 1) {\nvar c: i32 = 0;\n_ = c;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\ni = 0;\nwhile (i < n) : (i += 1) {\n_ = function();\n}\n_ = a;\n_ = b;\n_ = nums;\n_ = node;\n}\n
    space_complexity.dart
    /* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nfinal int a = 0;\nint b = 0;\nList<int> nums = List.filled(10000, 0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (var i = 0; i < n; i++) {\nint c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (var i = 0; i < n; i++) {\nfunction();\n}\n}\n
    "},{"location":"chapter_computational_complexity/space_complexity/#on","title":"\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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart space_complexity.java
    /* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\nint[] nums = new int[n];\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nList<ListNode> nodes = new ArrayList<>();\nfor (int i = 0; i < n; i++) {\nnodes.add(new ListNode(i));\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nMap<Integer, String> map = new HashMap<>();\nfor (int i = 0; i < n; i++) {\nmap.put(i, String.valueOf(i));\n}\n}\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\nvector<int> nums(n);\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nvector<ListNode> nodes;\nfor (int i = 0; i < n; i++) {\nnodes.push_back(ListNode(i));\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nunordered_map<int, string> map;\nfor (int i = 0; i < n; i++) {\nmap[i] = to_string(i);\n}\n}\n
    space_complexity.py
    def linear(n: int) -> None:\n\"\"\"\u7ebf\u6027\u9636\"\"\"\n# \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nnums = [0] * n\n# \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nmapp = dict[int, str]()\nfor i in range(n):\nmapp[i] = str(i)\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\nvar nodes []*node\nfor i := 0; i < n; i++ {\nnodes = append(nodes, newNode(i))\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nm := make(map[int]string, n)\nfor i := 0; i < n; i++ {\nm[i] = strconv.Itoa(i)\n}\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\nconst nums = new Array(n);\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nconst nodes = [];\nfor (let i = 0; i < n; i++) {\nnodes.push(new ListNode(i));\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nconst map = new Map();\nfor (let i = 0; i < n; i++) {\nmap.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\nconst nums = new Array(n);\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nconst nodes: ListNode[] = [];\nfor (let i = 0; i < n; i++) {\nnodes.push(new ListNode(i));\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nconst map = new Map();\nfor (let i = 0; i < n; i++) {\nmap.set(i, i.toString());\n}\n}\n
    space_complexity.c
    /* \u54c8\u5e0c\u8868 */\nstruct hashTable {\nint key;\nint val;\nUT_hash_handle hh; // \u57fa\u4e8e uthash.h \u5b9e\u73b0\n};\ntypedef struct hashTable hashTable;\n/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\nint *nums = malloc(sizeof(int) * n);\nfree(nums);\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nListNode **nodes = malloc(sizeof(ListNode *) * n);\nfor (int i = 0; i < n; i++) {\nnodes[i] = newListNode(i);\n}\n// \u5185\u5b58\u91ca\u653e\nfor (int i = 0; i < n; i++) {\nfree(nodes[i]);\n}\nfree(nodes);\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nhashTable *h = NULL;\nfor (int i = 0; i < n; i++) {\nhashTable *tmp = malloc(sizeof(hashTable));\ntmp->key = i;\ntmp->val = i;\nHASH_ADD_INT(h, key, tmp);\n}\n// \u5185\u5b58\u91ca\u653e\nhashTable *curr, *tmp;\nHASH_ITER(hh, h, curr, tmp) {\nHASH_DEL(h, curr);\nfree(curr);\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\nint[] nums = new int[n];\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nList<ListNode> nodes = new();\nfor (int i = 0; i < n; i++) {\nnodes.Add(new ListNode(i));\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nDictionary<int, string> map = new();\nfor (int i = 0; i < n; i++) {\nmap.Add(i, i.ToString());\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\nlet nums = Array(repeating: 0, count: n)\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nlet nodes = (0 ..< n).map { ListNode(x: $0) }\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nlet map = Dictionary(uniqueKeysWithValues: (0 ..< n).map { ($0, \"\\($0)\") })\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\nvar nums = [_]i32{0}**n;\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nvar nodes = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer nodes.deinit();\nvar i: i32 = 0;\nwhile (i < n) : (i += 1) {\ntry nodes.append(i);\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nvar map = std.AutoArrayHashMap(i32, []const u8).init(std.heap.page_allocator);\ndefer map.deinit();\nvar j: i32 = 0;\nwhile (j < n) : (j += 1) {\nconst string = try std.fmt.allocPrint(std.heap.page_allocator, \"{d}\", .{j});\ndefer std.heap.page_allocator.free(string);\ntry map.put(i, string);\n}\n_ = nums;\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\nList<int> nums = List.filled(n, 0);\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nList<ListNode> nodes = [];\nfor (var i = 0; i < n; i++) {\nnodes.add(ListNode(i));\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nMap<int, String> map = HashMap();\nfor (var i = 0; i < n; i++) {\nmap.putIfAbsent(i, () => i.toString());\n}\n}\n

    \u4ee5\u4e0b\u9012\u5f52\u51fd\u6570\u4f1a\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684 algorithm() \u51fd\u6570\uff0c\u4f7f\u7528 \\(O(n)\\) \u5927\u5c0f\u7684\u6808\u5e27\u7a7a\u95f4\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart space_complexity.java
    /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\nSystem.out.println(\"\u9012\u5f52 n = \" + n);\nif (n == 1)\nreturn;\nlinearRecur(n - 1);\n}\n
    space_complexity.cpp
    /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\ncout << \"\u9012\u5f52 n = \" << n << endl;\nif (n == 1)\nreturn;\nlinearRecur(n - 1);\n}\n
    space_complexity.py
    def linear_recur(n: int) -> None:\n\"\"\"\u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\nprint(\"\u9012\u5f52 n =\", n)\nif n == 1:\nreturn\nlinear_recur(n - 1)\n
    space_complexity.go
    /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc spaceLinearRecur(n int) {\nfmt.Println(\"\u9012\u5f52 n =\", n)\nif n == 1 {\nreturn\n}\nspaceLinearRecur(n - 1)\n}\n
    space_complexity.js
    /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction linearRecur(n) {\nconsole.log(`\u9012\u5f52 n = ${n}`);\nif (n === 1) return;\nlinearRecur(n - 1);\n}\n
    space_complexity.ts
    /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction linearRecur(n: number): void {\nconsole.log(`\u9012\u5f52 n = ${n}`);\nif (n === 1) return;\nlinearRecur(n - 1);\n}\n
    space_complexity.c
    /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\nprintf(\"\u9012\u5f52 n = %d\\r\\n\", n);\nif (n == 1)\nreturn;\nlinearRecur(n - 1);\n}\n
    space_complexity.cs
    /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\nConsole.WriteLine(\"\u9012\u5f52 n = \" + n);\nif (n == 1) return;\nlinearRecur(n - 1);\n}\n
    space_complexity.swift
    /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc linearRecur(n: Int) {\nprint(\"\u9012\u5f52 n = \\(n)\")\nif n == 1 {\nreturn\n}\nlinearRecur(n: n - 1)\n}\n
    space_complexity.zig
    // \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn linearRecur(comptime n: i32) void {\nstd.debug.print(\"\u9012\u5f52 n = {}\\n\", .{n});\nif (n == 1) return;\nlinearRecur(n - 1);\n}\n
    space_complexity.dart
    /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\nprint('\u9012\u5f52 n = $n');\nif (n == 1) return;\nlinearRecur(n - 1);\n}\n

    Fig. \u9012\u5f52\u51fd\u6570\u4ea7\u751f\u7684\u7ebf\u6027\u9636\u7a7a\u95f4\u590d\u6742\u5ea6

    "},{"location":"chapter_computational_complexity/space_complexity/#on2","title":"\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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart space_complexity.java
    /* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n// \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\nint[][] numMatrix = new int[n][n];\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nList<List<Integer>> numList = new ArrayList<>();\nfor (int i = 0; i < n; i++) {\nList<Integer> tmp = new ArrayList<>();\nfor (int j = 0; j < n; j++) {\ntmp.add(0);\n}\nnumList.add(tmp);\n}\n}\n
    space_complexity.cpp
    /* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nvector<vector<int>> numMatrix;\nfor (int i = 0; i < n; i++) {\nvector<int> tmp;\nfor (int j = 0; j < n; j++) {\ntmp.push_back(0);\n}\nnumMatrix.push_back(tmp);\n}\n}\n
    space_complexity.py
    def quadratic(n: int) -> None:\n\"\"\"\u5e73\u65b9\u9636\"\"\"\n# \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nnum_matrix = [[0] * n for _ in range(n)]\n
    space_complexity.go
    /* \u5e73\u65b9\u9636 */\nfunc spaceQuadratic(n int) {\n// \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\nnumMatrix := make([][]int, n)\nfor i := 0; i < n; i++ {\nnumMatrix[i] = make([]int, n)\n}\n}\n
    space_complexity.js
    /* \u5e73\u65b9\u9636 */\nfunction quadratic(n) {\n// \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\nconst numMatrix = Array(n)\n.fill(null)\n.map(() => Array(n).fill(null));\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nconst numList = [];\nfor (let i = 0; i < n; i++) {\nconst tmp = [];\nfor (let j = 0; j < n; j++) {\ntmp.push(0);\n}\nnumList.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\nconst numMatrix = Array(n)\n.fill(null)\n.map(() => Array(n).fill(null));\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nconst numList = [];\nfor (let i = 0; i < n; i++) {\nconst tmp = [];\nfor (let j = 0; j < n; j++) {\ntmp.push(0);\n}\nnumList.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\nint **numMatrix = malloc(sizeof(int *) * n);\nfor (int i = 0; i < n; i++) {\nint *tmp = malloc(sizeof(int) * n);\nfor (int j = 0; j < n; j++) {\ntmp[j] = 0;\n}\nnumMatrix[i] = tmp;\n}\n// \u5185\u5b58\u91ca\u653e\nfor (int i = 0; i < n; i++) {\nfree(numMatrix[i]);\n}\nfree(numMatrix);\n}\n
    space_complexity.cs
    /* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n// \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\nint[,] numMatrix = new int[n, n];\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nList<List<int>> numList = new();\nfor (int i = 0; i < n; i++) {\nList<int> tmp = new();\nfor (int j = 0; j < n; j++) {\ntmp.Add(0);\n}\nnumList.Add(tmp);\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\nlet numList = Array(repeating: Array(repeating: 0, count: n), count: n)\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\nvar nodes = std.ArrayList(std.ArrayList(i32)).init(std.heap.page_allocator);\ndefer nodes.deinit();\nvar i: i32 = 0;\nwhile (i < n) : (i += 1) {\nvar tmp = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer tmp.deinit();\nvar j: i32 = 0;\nwhile (j < n) : (j += 1) {\ntry tmp.append(0);\n}\ntry nodes.append(tmp);\n}\n}\n
    space_complexity.dart
    /* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n// \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\nList<List<int>> numMatrix = List.generate(n, (_) => List.filled(n, 0));\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nList<List<int>> numList = [];\nfor (var i = 0; i < n; i++) {\nList<int> tmp = [];\nfor (int j = 0; j < n; j++) {\ntmp.add(0);\n}\nnumList.add(tmp);\n}\n}\n

    \u5728\u4ee5\u4e0b\u9012\u5f52\u51fd\u6570\u4e2d\uff0c\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684 algorithm() \uff0c\u5e76\u4e14\u6bcf\u4e2a\u51fd\u6570\u4e2d\u90fd\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u6570\u7ec4\uff0c\u957f\u5ea6\u5206\u522b\u4e3a \\(n, n-1, n-2, ..., 2, 1\\) \uff0c\u5e73\u5747\u957f\u5ea6\u4e3a \\(\\frac{n}{2}\\) \uff0c\u56e0\u6b64\u603b\u4f53\u5360\u7528 \\(O(n^2)\\) \u7a7a\u95f4\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart space_complexity.java
    /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\nif (n <= 0)\nreturn 0;\n// \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\nint[] nums = new int[n];\nSystem.out.println(\"\u9012\u5f52 n = \" + n + \" \u4e2d\u7684 nums \u957f\u5ea6 = \" + nums.length);\nreturn quadraticRecur(n - 1);\n}\n
    space_complexity.cpp
    /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\nif (n <= 0)\nreturn 0;\nvector<int> nums(n);\ncout << \"\u9012\u5f52 n = \" << n << \" \u4e2d\u7684 nums \u957f\u5ea6 = \" << nums.size() << endl;\nreturn quadraticRecur(n - 1);\n}\n
    space_complexity.py
    def quadratic_recur(n: int) -> int:\n\"\"\"\u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\nif n <= 0:\nreturn 0\n# \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\nnums = [0] * n\nreturn quadratic_recur(n - 1)\n
    space_complexity.go
    /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc spaceQuadraticRecur(n int) int {\nif n <= 0 {\nreturn 0\n}\nnums := make([]int, n)\nfmt.Printf(\"\u9012\u5f52 n = %d \u4e2d\u7684 nums \u957f\u5ea6 = %d \\n\", n, len(nums))\nreturn spaceQuadraticRecur(n - 1)\n}\n
    space_complexity.js
    /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction quadraticRecur(n) {\nif (n <= 0) return 0;\nconst nums = new Array(n);\nconsole.log(`\u9012\u5f52 n = ${n} \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}`);\nreturn quadraticRecur(n - 1);\n}\n
    space_complexity.ts
    /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction quadraticRecur(n: number): number {\nif (n <= 0) return 0;\nconst nums = new Array(n);\nconsole.log(`\u9012\u5f52 n = ${n} \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}`);\nreturn quadraticRecur(n - 1);\n}\n
    space_complexity.c
    /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\nif (n <= 0)\nreturn 0;\nint *nums = malloc(sizeof(int) * n);\nprintf(\"\u9012\u5f52 n = %d \u4e2d\u7684 nums \u957f\u5ea6 = %d\\r\\n\", n, n);\nfree(nums);\nreturn quadraticRecur(n - 1);\n}\n
    space_complexity.cs
    /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\nif (n <= 0) return 0;\nint[] nums = new int[n];\nConsole.WriteLine(\"\u9012\u5f52 n = \" + n + \" \u4e2d\u7684 nums \u957f\u5ea6 = \" + nums.Length);\nreturn quadraticRecur(n - 1);\n}\n
    space_complexity.swift
    /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\n@discardableResult\nfunc quadraticRecur(n: Int) -> Int {\nif n <= 0 {\nreturn 0\n}\n// \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\nlet nums = Array(repeating: 0, count: n)\nprint(\"\u9012\u5f52 n = \\(n) \u4e2d\u7684 nums \u957f\u5ea6 = \\(nums.count)\")\nreturn quadraticRecur(n: n - 1)\n}\n
    space_complexity.zig
    // \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn quadraticRecur(comptime n: i32) i32 {\nif (n <= 0) return 0;\nvar nums = [_]i32{0}**n;\nstd.debug.print(\"\u9012\u5f52 n = {} \u4e2d\u7684 nums \u957f\u5ea6 = {}\\n\", .{n, nums.len});\nreturn quadraticRecur(n - 1);\n}\n
    space_complexity.dart
    /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\nif (n <= 0) return 0;\nList<int> nums = List.filled(n, 0);\nprint('\u9012\u5f52 n = $n \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}');\nreturn quadraticRecur(n - 1);\n}\n

    Fig. \u9012\u5f52\u51fd\u6570\u4ea7\u751f\u7684\u5e73\u65b9\u9636\u7a7a\u95f4\u590d\u6742\u5ea6

    "},{"location":"chapter_computational_complexity/space_complexity/#o2n","title":"\u6307\u6570\u9636 \\(O(2^n)\\)","text":"

    \u6307\u6570\u9636\u5e38\u89c1\u4e8e\u4e8c\u53c9\u6811\u3002\u9ad8\u5ea6\u4e3a \\(n\\) \u7684\u300c\u6ee1\u4e8c\u53c9\u6811\u300d\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(2^n - 1\\) \uff0c\u5360\u7528 \\(O(2^n)\\) \u7a7a\u95f4\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart space_complexity.java
    /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode buildTree(int n) {\nif (n == 0)\nreturn null;\nTreeNode root = new TreeNode(0);\nroot.left = buildTree(n - 1);\nroot.right = buildTree(n - 1);\nreturn root;\n}\n
    space_complexity.cpp
    /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode *buildTree(int n) {\nif (n == 0)\nreturn nullptr;\nTreeNode *root = new TreeNode(0);\nroot->left = buildTree(n - 1);\nroot->right = buildTree(n - 1);\nreturn root;\n}\n
    space_complexity.py
    def build_tree(n: int) -> TreeNode | None:\n\"\"\"\u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09\"\"\"\nif n == 0:\nreturn None\nroot = TreeNode(0)\nroot.left = build_tree(n - 1)\nroot.right = build_tree(n - 1)\nreturn root\n
    space_complexity.go
    /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunc buildTree(n int) *treeNode {\nif n == 0 {\nreturn nil\n}\nroot := newTreeNode(0)\nroot.left = buildTree(n - 1)\nroot.right = buildTree(n - 1)\nreturn root\n}\n
    space_complexity.js
    /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunction buildTree(n) {\nif (n === 0) return null;\nconst root = new TreeNode(0);\nroot.left = buildTree(n - 1);\nroot.right = buildTree(n - 1);\nreturn root;\n}\n
    space_complexity.ts
    /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunction buildTree(n: number): TreeNode | null {\nif (n === 0) return null;\nconst root = new TreeNode(0);\nroot.left = buildTree(n - 1);\nroot.right = buildTree(n - 1);\nreturn root;\n}\n
    space_complexity.c
    /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode *buildTree(int n) {\nif (n == 0)\nreturn NULL;\nTreeNode *root = newTreeNode(0);\nroot->left = buildTree(n - 1);\nroot->right = buildTree(n - 1);\nreturn root;\n}\n
    space_complexity.cs
    /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode? buildTree(int n) {\nif (n == 0) return null;\nTreeNode root = new TreeNode(0);\nroot.left = buildTree(n - 1);\nroot.right = buildTree(n - 1);\nreturn root;\n}\n
    space_complexity.swift
    /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunc buildTree(n: Int) -> TreeNode? {\nif n == 0 {\nreturn nil\n}\nlet root = TreeNode(x: 0)\nroot.left = buildTree(n: n - 1)\nroot.right = buildTree(n: n - 1)\nreturn 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) {\nif (n == 0) return null;\nconst root = try mem_allocator.create(inc.TreeNode(i32));\nroot.init(0);\nroot.left = try buildTree(mem_allocator, n - 1);\nroot.right = try buildTree(mem_allocator, n - 1);\nreturn root;\n}\n
    space_complexity.dart
    /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode? buildTree(int n) {\nif (n == 0) return null;\nTreeNode root = TreeNode(0);\nroot.left = buildTree(n - 1);\nroot.right = buildTree(n - 1);\nreturn root;\n}\n

    Fig. \u6ee1\u4e8c\u53c9\u6811\u4ea7\u751f\u7684\u6307\u6570\u9636\u7a7a\u95f4\u590d\u6742\u5ea6

    "},{"location":"chapter_computational_complexity/space_complexity/#olog-n","title":"\u5bf9\u6570\u9636 \\(O(\\log n)\\)","text":"

    \u5bf9\u6570\u9636\u5e38\u89c1\u4e8e\u5206\u6cbb\u7b97\u6cd5\u548c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u7b49\u3002

    \u4f8b\u5982\u201c\u5f52\u5e76\u6392\u5e8f\u201d\u7b97\u6cd5\uff0c\u8f93\u5165\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\uff0c\u6bcf\u8f6e\u9012\u5f52\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\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\u201c\u6570\u5b57\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\u201d\uff0c\u8f93\u5165\u4efb\u610f\u6b63\u6574\u6570 \\(n\\) \uff0c\u5b83\u7684\u4f4d\u6570\u4e3a \\(\\log_{10} n\\) \uff0c\u5373\u5bf9\u5e94\u5b57\u7b26\u4e32\u957f\u5ea6\u4e3a \\(\\log_{10} n\\) \uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log_{10} n) = O(\\log n)\\) \u3002

    "},{"location":"chapter_computational_complexity/space_complexity/#234","title":"2.3.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\u662f\u975e\u5e38\u56f0\u96be\u7684\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\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\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\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002

    "},{"location":"chapter_computational_complexity/summary/","title":"2.4. \u00a0 \u5c0f\u7ed3","text":"

    \u7b97\u6cd5\u6548\u7387\u8bc4\u4f30

    • \u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u662f\u8bc4\u4ef7\u7b97\u6cd5\u6027\u80fd\u7684\u4e24\u4e2a\u5173\u952e\u7ef4\u5ea6\u3002
    • \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b9e\u9645\u6d4b\u8bd5\u6765\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\uff0c\u4f46\u96be\u4ee5\u6d88\u9664\u6d4b\u8bd5\u73af\u5883\u7684\u5f71\u54cd\uff0c\u4e14\u4f1a\u8017\u8d39\u5927\u91cf\u8ba1\u7b97\u8d44\u6e90\u3002
    • \u590d\u6742\u5ea6\u5206\u6790\u53ef\u4ee5\u514b\u670d\u5b9e\u9645\u6d4b\u8bd5\u7684\u5f0a\u7aef\uff0c\u5206\u6790\u7ed3\u679c\u9002\u7528\u4e8e\u6240\u6709\u8fd0\u884c\u5e73\u53f0\uff0c\u5e76\u4e14\u80fd\u591f\u63ed\u793a\u7b97\u6cd5\u5728\u4e0d\u540c\u6570\u636e\u89c4\u6a21\u4e0b\u7684\u6548\u7387\u3002

    \u65f6\u95f4\u590d\u6742\u5ea6

    • \u65f6\u95f4\u590d\u6742\u5ea6\u7528\u4e8e\u8861\u91cf\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u968f\u6570\u636e\u91cf\u589e\u957f\u7684\u8d8b\u52bf\uff0c\u53ef\u4ee5\u6709\u6548\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u5931\u6548\uff0c\u5982\u5728\u8f93\u5165\u6570\u636e\u91cf\u8f83\u5c0f\u6216\u65f6\u95f4\u590d\u6742\u5ea6\u76f8\u540c\u65f6\uff0c\u65e0\u6cd5\u7cbe\u786e\u5bf9\u6bd4\u7b97\u6cd5\u6548\u7387\u7684\u4f18\u52a3\u3002
    • \u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4f7f\u7528\u5927 \\(O\\) \u7b26\u53f7\u8868\u793a\uff0c\u5373\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c\uff0c\u53cd\u6620\u5f53 \\(n\\) \u8d8b\u5411\u6b63\u65e0\u7a77\u65f6\uff0c\\(T(n)\\) \u7684\u589e\u957f\u7ea7\u522b\u3002
    • \u63a8\u7b97\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u4e3a\u4e24\u6b65\uff0c\u9996\u5148\u7edf\u8ba1\u8ba1\u7b97\u64cd\u4f5c\u6570\u91cf\uff0c\u7136\u540e\u5224\u65ad\u6e10\u8fd1\u4e0a\u754c\u3002
    • \u5e38\u89c1\u65f6\u95f4\u590d\u6742\u5ea6\u4ece\u5c0f\u5230\u5927\u6392\u5217\u6709 \\(O(1)\\) , \\(O(\\log n)\\) , \\(O(n)\\) , \\(O(n \\log n)\\) , \\(O(n^2)\\) , \\(O(2^n)\\) , \\(O(n!)\\) \u7b49\u3002
    • \u67d0\u4e9b\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u975e\u56fa\u5b9a\uff0c\u800c\u662f\u4e0e\u8f93\u5165\u6570\u636e\u7684\u5206\u5e03\u6709\u5173\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u4e3a\u6700\u5dee\u3001\u6700\u4f73\u3001\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u51e0\u4e4e\u4e0d\u7528\uff0c\u56e0\u4e3a\u8f93\u5165\u6570\u636e\u4e00\u822c\u9700\u8981\u6ee1\u8db3\u4e25\u683c\u6761\u4ef6\u624d\u80fd\u8fbe\u5230\u6700\u4f73\u60c5\u51b5\u3002
    • \u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u53cd\u6620\u7b97\u6cd5\u5728\u968f\u673a\u6570\u636e\u8f93\u5165\u4e0b\u7684\u8fd0\u884c\u6548\u7387\uff0c\u6700\u63a5\u8fd1\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u7b97\u6cd5\u6027\u80fd\u3002\u8ba1\u7b97\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u9700\u8981\u7edf\u8ba1\u8f93\u5165\u6570\u636e\u5206\u5e03\u4ee5\u53ca\u7efc\u5408\u540e\u7684\u6570\u5b66\u671f\u671b\u3002

    \u7a7a\u95f4\u590d\u6742\u5ea6

    • \u7c7b\u4f3c\u4e8e\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u7528\u4e8e\u8861\u91cf\u7b97\u6cd5\u5360\u7528\u7a7a\u95f4\u968f\u6570\u636e\u91cf\u589e\u957f\u7684\u8d8b\u52bf\u3002
    • \u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u76f8\u5173\u5185\u5b58\u7a7a\u95f4\u53ef\u5206\u4e3a\u8f93\u5165\u7a7a\u95f4\u3001\u6682\u5b58\u7a7a\u95f4\u3001\u8f93\u51fa\u7a7a\u95f4\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u8f93\u5165\u7a7a\u95f4\u4e0d\u8ba1\u5165\u7a7a\u95f4\u590d\u6742\u5ea6\u8ba1\u7b97\u3002\u6682\u5b58\u7a7a\u95f4\u53ef\u5206\u4e3a\u6307\u4ee4\u7a7a\u95f4\u3001\u6570\u636e\u7a7a\u95f4\u3001\u6808\u5e27\u7a7a\u95f4\uff0c\u5176\u4e2d\u6808\u5e27\u7a7a\u95f4\u901a\u5e38\u4ec5\u5728\u9012\u5f52\u51fd\u6570\u4e2d\u5f71\u54cd\u7a7a\u95f4\u590d\u6742\u5ea6\u3002
    • \u6211\u4eec\u901a\u5e38\u53ea\u5173\u6ce8\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u5373\u7edf\u8ba1\u7b97\u6cd5\u5728\u6700\u5dee\u8f93\u5165\u6570\u636e\u548c\u6700\u5dee\u8fd0\u884c\u65f6\u95f4\u70b9\u4e0b\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u3002
    • \u5e38\u89c1\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece\u5c0f\u5230\u5927\u6392\u5217\u6709 \\(O(1)\\) , \\(O(\\log n)\\) , \\(O(n)\\) , \\(O(n^2)\\) , \\(O(2^n)\\) \u7b49\u3002
    "},{"location":"chapter_computational_complexity/summary/#241-q-a","title":"2.4.1. \u00a0 Q & A","text":"

    \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\u88ab\u4f18\u5316\u81f3 \\(O(1)\\) \u3002\u4e0d\u8fc7\u7edd\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\uff08\u4f8b\u5982 Java, Python, C++, Go, C# \u7b49\uff09 \u90fd\u4e0d\u652f\u6301\u81ea\u52a8\u4f18\u5316\u5c3e\u9012\u5f52\uff0c\u56e0\u6b64\u4e00\u822c\u6765\u8bf4\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

    \u51fd\u6570\uff08function\uff09\u53ef\u4ee5\u72ec\u7acb\u88ab\u6267\u884c\uff0c\u6240\u6709\u53c2\u6570\u90fd\u4ee5\u663e\u5f0f\u4f20\u9012\u3002 \u65b9\u6cd5\uff08method\uff09\u4e0e\u4e00\u4e2a\u5bf9\u8c61\u5173\u8054\uff0c\u65b9\u6cd5\u88ab\u9690\u5f0f\u4f20\u9012\u7ed9\u8c03\u7528\u5b83\u7684\u5bf9\u8c61\uff0c\u65b9\u6cd5\u80fd\u591f\u5bf9\u7c7b\u7684\u5b9e\u4f8b\u4e2d\u5305\u542b\u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002

    \u56fe\u7247\u201c\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u5e38\u89c1\u7c7b\u578b\u201d\u53cd\u6620\u7684\u662f\u5426\u662f\u5360\u7528\u7a7a\u95f4\u7684\u7edd\u5bf9\u5927\u5c0f\uff1f

    \u4e0d\u662f\uff0c\u8be5\u56fe\u7247\u5c55\u793a\u7684\u662f\u7a7a\u95f4\u590d\u6742\u5ea6\uff08\u5373\u589e\u957f\u8d8b\u52bf\uff09\uff0c\u800c\u4e0d\u662f\u5360\u7528\u7a7a\u95f4\u7684\u7edd\u5bf9\u5927\u5c0f\u3002\u6bcf\u4e2a\u66f2\u7ebf\u90fd\u5305\u542b\u4e00\u4e2a\u5e38\u6570\u9879\uff0c\u7528\u6765\u628a\u6240\u6709\u66f2\u7ebf\u7684\u53d6\u503c\u8303\u56f4\u538b\u7f29\u5230\u4e00\u4e2a\u89c6\u89c9\u8212\u9002\u7684\u8303\u56f4\u5185\u3002 \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\uff1b\u4f46\u76f8\u5bf9\u5730 \\(n = 8^5\\) \u5c31\u5f88\u597d\u9009\u4e86\uff0c\u8fd9\u662f\u590d\u6742\u5ea6\u5360\u4e3b\u5bfc\u7684\u60c5\u51b5\u3002

    "},{"location":"chapter_computational_complexity/time_complexity/","title":"2.2. \u00a0 \u65f6\u95f4\u590d\u6742\u5ea6","text":""},{"location":"chapter_computational_complexity/time_complexity/#221","title":"2.2.1. \u00a0 \u7edf\u8ba1\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4","text":"

    \u8fd0\u884c\u65f6\u95f4\u53ef\u4ee5\u76f4\u89c2\u4e14\u51c6\u786e\u5730\u53cd\u6620\u7b97\u6cd5\u7684\u6548\u7387\u3002\u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\u51c6\u786e\u9884\u4f30\u4e00\u6bb5\u4ee3\u7801\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u5e94\u8be5\u5982\u4f55\u64cd\u4f5c\u5462\uff1f

    1. \u786e\u5b9a\u8fd0\u884c\u5e73\u53f0\uff0c\u5305\u62ec\u786c\u4ef6\u914d\u7f6e\u3001\u7f16\u7a0b\u8bed\u8a00\u3001\u7cfb\u7edf\u73af\u5883\u7b49\uff0c\u8fd9\u4e9b\u56e0\u7d20\u90fd\u4f1a\u5f71\u54cd\u4ee3\u7801\u7684\u8fd0\u884c\u6548\u7387\u3002
    2. \u8bc4\u4f30\u5404\u79cd\u8ba1\u7b97\u64cd\u4f5c\u6240\u9700\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u4f8b\u5982\u52a0\u6cd5\u64cd\u4f5c + \u9700\u8981 1 ns\uff0c\u4e58\u6cd5\u64cd\u4f5c * \u9700\u8981 10 ns\uff0c\u6253\u5370\u64cd\u4f5c\u9700\u8981 5 ns \u7b49\u3002
    3. \u7edf\u8ba1\u4ee3\u7801\u4e2d\u6240\u6709\u7684\u8ba1\u7b97\u64cd\u4f5c\uff0c\u5e76\u5c06\u6240\u6709\u64cd\u4f5c\u7684\u6267\u884c\u65f6\u95f4\u6c42\u548c\uff0c\u4ece\u800c\u5f97\u5230\u8fd0\u884c\u65f6\u95f4\u3002

    \u4f8b\u5982\u4ee5\u4e0b\u4ee3\u7801\uff0c\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u6839\u636e\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5f97\u5230\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u4e3a \\(6n + 12\\) ns \u3002

    \\[ 1 + 1 + 10 + (1 + 5) \\times n = 6n + 12 \\] JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\nint a = 2;  // 1 ns\na = a + 1;  // 1 ns\na = a * 2;  // 10 ns\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i < n; i++) {  // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\nSystem.out.println(0);     // 5 ns\n}\n}\n
    // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\nint a = 2;  // 1 ns\na = a + 1;  // 1 ns\na = a * 2;  // 10 ns\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i < n; i++) {  // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\ncout << 0 << endl;         // 5 ns\n}\n}\n
    # \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\ndef algorithm(n: int) -> None:\na = 2      # 1 ns\na = a + 1  # 1 ns\na = a * 2  # 10 ns\n# \u5faa\u73af n \u6b21\nfor _ in range(n):  # 1 ns\nprint(0)        # 5 ns\n
    // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunc algorithm(n int) {\na := 2      // 1 ns\na = a + 1   // 1 ns\na = a * 2   // 10 ns\n// \u5faa\u73af n \u6b21\nfor i := 0; i < n; i++ {    // 1 ns\nfmt.Println(a)          // 5 ns\n}\n}\n
    // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunction algorithm(n) {\nvar a = 2; // 1 ns\na = a + 1; // 1 ns\na = a * 2; // 10 ns\n// \u5faa\u73af n \u6b21\nfor(let i = 0; i < n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\nconsole.log(0); // 5 ns\n}\n}\n
    // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunction algorithm(n: number): void {\nvar a: number = 2; // 1 ns\na = a + 1; // 1 ns\na = a * 2; // 10 ns\n// \u5faa\u73af n \u6b21\nfor(let i = 0; i < n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\nconsole.log(0); // 5 ns\n}\n}\n
    // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\nint a = 2;  // 1 ns\na = a + 1;  // 1 ns\na = a * 2;  // 10 ns\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i < n; i++) {   // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\nprintf(\"%d\", 0);            // 5 ns\n}\n}\n
    // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n)\n{\nint a = 2;  // 1 ns\na = a + 1;  // 1 ns\na = a * 2;  // 10 ns\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i < n; i++)\n{  // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\nConsole.WriteLine(0);     // 5 ns\n}\n}\n
    // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunc algorithm(n: Int) {\nvar a = 2 // 1 ns\na = a + 1 // 1 ns\na = a * 2 // 10 ns\n// \u5faa\u73af n \u6b21\nfor _ in 0 ..< n { // 1 ns\nprint(0) // 5 ns\n}\n}\n
    \n
    // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\nint a = 2; // 1 ns\na = a + 1; // 1 ns\na = a * 2; // 10 ns\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i < n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\nprint(0); // 5 ns\n}\n}\n

    \u7136\u800c\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\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/#222","title":"2.2.2. \u00a0 \u7edf\u8ba1\u65f6\u95f4\u589e\u957f\u8d8b\u52bf","text":"

    \u300c\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u300d\u91c7\u53d6\u4e86\u4e00\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\uff0c\u5176\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\u8f83\u4e3a\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 , B , C \u3002

    • \u7b97\u6cd5 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\u300c\u5e38\u6570\u9636\u300d\u3002
    • \u7b97\u6cd5 B \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\u300c\u7ebf\u6027\u9636\u300d\u3002
    • \u7b97\u6cd5 C \u4e2d\u7684\u6253\u5370\u64cd\u4f5c\u9700\u8981\u5faa\u73af \\(1000000\\) \u6b21\uff0c\u4f46\u8fd0\u884c\u65f6\u95f4\u4ecd\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\u300c\u5e38\u6570\u9636\u300d\u3002
    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    // \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\nSystem.out.println(0);\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\nfor (int i = 0; i < n; i++) {\nSystem.out.println(0);\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\nfor (int i = 0; i < 1000000; i++) {\nSystem.out.println(0);\n}\n}\n
    // \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\ncout << 0 << endl;\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\nfor (int i = 0; i < n; i++) {\ncout << 0 << endl;\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\nfor (int i = 0; i < 1000000; i++) {\ncout << 0 << endl;\n}\n}\n
    # \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_A(n: int) -> None:\nprint(0)\n# \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\ndef algorithm_B(n: int) -> None:\nfor _ in range(n):\nprint(0)\n# \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_C(n: int) -> None:\nfor _ in range(1000000):\nprint(0)\n
    // \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithm_A(n int) {\nfmt.Println(0)\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunc algorithm_B(n int) {\nfor i := 0; i < n; i++ {\nfmt.Println(0)\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithm_C(n int) {\nfor i := 0; i < 1000000; i++ {\nfmt.Println(0)\n}\n}\n
    // \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_A(n) {\nconsole.log(0);\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunction algorithm_B(n) {\nfor (let i = 0; i < n; i++) {\nconsole.log(0);\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_C(n) {\nfor (let i = 0; i < 1000000; i++) {\nconsole.log(0);\n}\n}\n
    // \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_A(n: number): void {\nconsole.log(0);\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunction algorithm_B(n: number): void {\nfor (let i = 0; i < n; i++) {\nconsole.log(0);\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_C(n: number): void {\nfor (let i = 0; i < 1000000; i++) {\nconsole.log(0);\n}\n}\n
    // \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\nprintf(\"%d\", 0);\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\nfor (int i = 0; i < n; i++) {\nprintf(\"%d\", 0);\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\nfor (int i = 0; i < 1000000; i++) {\nprintf(\"%d\", 0);\n}\n}\n
    // \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n)\n{\nConsole.WriteLine(0);\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n)\n{\nfor (int i = 0; i < n; i++)\n{\nConsole.WriteLine(0);\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n)\n{\nfor (int i = 0; i < 1000000; i++)\n{\nConsole.WriteLine(0);\n}\n}\n
    // \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithmA(n: Int) {\nprint(0)\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunc algorithmB(n: Int) {\nfor _ in 0 ..< n {\nprint(0)\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithmC(n: Int) {\nfor _ in 0 ..< 1000000 {\nprint(0)\n}\n}\n
    \n
    // \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithmA(int n) {\nprint(0);\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithmB(int n) {\nfor (int i = 0; i < n; i++) {\nprint(0);\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithmC(int n) {\nfor (int i = 0; i < 1000000; i++) {\nprint(0);\n}\n}\n

    Fig. \u7b97\u6cd5 A, B, C \u7684\u65f6\u95f4\u589e\u957f\u8d8b\u52bf

    \u76f8\u8f83\u4e8e\u76f4\u63a5\u7edf\u8ba1\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u6709\u54ea\u4e9b\u4f18\u52bf\u548c\u5c40\u9650\u6027\u5462\uff1f

    \u65f6\u95f4\u590d\u6742\u5ea6\u80fd\u591f\u6709\u6548\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\u3002\u4f8b\u5982\uff0c\u7b97\u6cd5 B \u7684\u8fd0\u884c\u65f6\u95f4\u5448\u7ebf\u6027\u589e\u957f\uff0c\u5728 \\(n > 1\\) \u65f6\u6bd4\u7b97\u6cd5 A \u6162\uff0c\u5728 \\(n > 1000000\\) \u65f6\u6bd4\u7b97\u6cd5 C \u6162\u3002\u4e8b\u5b9e\u4e0a\uff0c\u53ea\u8981\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u8db3\u591f\u5927\uff0c\u590d\u6742\u5ea6\u4e3a\u300c\u5e38\u6570\u9636\u300d\u7684\u7b97\u6cd5\u4e00\u5b9a\u4f18\u4e8e\u300c\u7ebf\u6027\u9636\u300d\u7684\u7b97\u6cd5\uff0c\u8fd9\u6b63\u662f\u65f6\u95f4\u589e\u957f\u8d8b\u52bf\u6240\u8868\u8fbe\u7684\u542b\u4e49\u3002

    \u65f6\u95f4\u590d\u6742\u5ea6\u7684\u63a8\u7b97\u65b9\u6cd5\u66f4\u7b80\u4fbf\u3002\u663e\u7136\uff0c\u8fd0\u884c\u5e73\u53f0\u548c\u8ba1\u7b97\u64cd\u4f5c\u7c7b\u578b\u90fd\u4e0e\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u65e0\u5173\u3002\u56e0\u6b64\u5728\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u5730\u5c06\u6240\u6709\u8ba1\u7b97\u64cd\u4f5c\u7684\u6267\u884c\u65f6\u95f4\u89c6\u4e3a\u76f8\u540c\u7684\u201c\u5355\u4f4d\u65f6\u95f4\u201d\uff0c\u4ece\u800c\u5c06\u201c\u8ba1\u7b97\u64cd\u4f5c\u7684\u8fd0\u884c\u65f6\u95f4\u7684\u7edf\u8ba1\u201d\u7b80\u5316\u4e3a\u201c\u8ba1\u7b97\u64cd\u4f5c\u7684\u6570\u91cf\u7684\u7edf\u8ba1\u201d\uff0c\u8fd9\u6837\u7684\u7b80\u5316\u65b9\u6cd5\u5927\u5927\u964d\u4f4e\u4e86\u4f30\u7b97\u96be\u5ea6\u3002

    \u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u5b58\u5728\u4e00\u5b9a\u7684\u5c40\u9650\u6027\u3002\u4f8b\u5982\uff0c\u5c3d\u7ba1\u7b97\u6cd5 A \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\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

    "},{"location":"chapter_computational_complexity/time_complexity/#223","title":"2.2.3. \u00a0 \u51fd\u6570\u6e10\u8fd1\u4e0a\u754c","text":"

    \u8bbe\u7b97\u6cd5\u7684\u8ba1\u7b97\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\u4e0b\u7b97\u6cd5\u7684\u64cd\u4f5c\u6570\u91cf\u4e3a

    \\[ T(n) = 3 + 2n \\] JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    void algorithm(int n) {\nint a = 1;  // +1\na = a + 1;  // +1\na = a * 2;  // +1\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i < n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\nSystem.out.println(0);    // +1\n}\n}\n
    void algorithm(int n) {\nint a = 1;  // +1\na = a + 1;  // +1\na = a * 2;  // +1\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i < n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\ncout << 0 << endl;    // +1\n}\n}\n
    def algorithm(n: int) -> None:\na = 1      # +1\na = a + 1  # +1\na = a * 2  # +1\n# \u5faa\u73af n \u6b21\nfor i in range(n):  # +1\nprint(0)        # +1\n
    func algorithm(n int) {\na := 1      // +1\na = a + 1   // +1\na = a * 2   // +1\n// \u5faa\u73af n \u6b21\nfor i := 0; i < n; i++ {   // +1\nfmt.Println(a)         // +1\n}\n}\n
    function algorithm(n) {\nvar a = 1; // +1\na += 1; // +1\na *= 2; // +1\n// \u5faa\u73af n \u6b21\nfor(let i = 0; i < n; i++){ // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\nconsole.log(0); // +1\n}\n}\n
    function algorithm(n: number): void{\nvar a: number = 1; // +1\na += 1; // +1\na *= 2; // +1\n// \u5faa\u73af n \u6b21\nfor(let i = 0; i < n; i++){ // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\nconsole.log(0); // +1\n}\n}\n
    void algorithm(int n) {\nint a = 1;  // +1\na = a + 1;  // +1\na = a * 2;  // +1\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i < n; i++) {   // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\nprintf(\"%d\", 0);            // +1\n}\n}  
    void algorithm(int n)\n{\nint a = 1;  // +1\na = a + 1;  // +1\na = a * 2;  // +1\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i < n; i++) // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n{\nConsole.WriteLine(0);   // +1\n}\n}\n
    func algorithm(n: Int) {\nvar a = 1 // +1\na = a + 1 // +1\na = a * 2 // +1\n// \u5faa\u73af n \u6b21\nfor _ in 0 ..< n { // +1\nprint(0) // +1\n}\n}\n
    \n
    void algorithm(int n) {\nint a = 1; // +1\na = a + 1; // +1\na = a * 2; // +1\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i < n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\nprint(0); // +1\n}\n}\n

    \\(T(n)\\) \u662f\u4e00\u6b21\u51fd\u6570\uff0c\u8bf4\u660e\u65f6\u95f4\u589e\u957f\u8d8b\u52bf\u662f\u7ebf\u6027\u7684\uff0c\u56e0\u6b64\u53ef\u4ee5\u5f97\u51fa\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

    \u63a8\u7b97\u65f6\u95f4\u590d\u6742\u5ea6\u672c\u8d28\u4e0a\u662f\u8ba1\u7b97\u201c\u64cd\u4f5c\u6570\u91cf\u51fd\u6570 \\(T(n)\\)\u201d\u7684\u6e10\u8fd1\u4e0a\u754c\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u6765\u770b\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c\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) $$ \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)) $$

    Fig. \u51fd\u6570\u7684\u6e10\u8fd1\u4e0a\u754c

    \u4ece\u672c\u8d28\u4e0a\u8bb2\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

    "},{"location":"chapter_computational_complexity/time_complexity/#224","title":"2.2.4. \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\u9700\u62c5\u5fc3\u3002\u56e0\u4e3a\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u6211\u4eec\u53ea\u9700\u8981\u638c\u63e1\u63a8\u7b97\u65b9\u6cd5\uff0c\u6570\u5b66\u610f\u4e49\u53ef\u4ee5\u9010\u6e10\u9886\u609f\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":"\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\u88ab\u5ffd\u7565\u3002\u6839\u636e\u6b64\u539f\u5219\uff0c\u53ef\u4ee5\u603b\u7ed3\u51fa\u4ee5\u4e0b\u8ba1\u6570\u7b80\u5316\u6280\u5de7\uff1a

    1. \u5ffd\u7565\u4e0e \\(n\\) \u65e0\u5173\u7684\u64cd\u4f5c\u3002\u56e0\u4e3a\u5b83\u4eec\u90fd\u662f \\(T(n)\\) \u4e2d\u7684\u5e38\u6570\u9879\uff0c\u5bf9\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u4ea7\u751f\u5f71\u54cd\u3002
    2. \u7701\u7565\u6240\u6709\u7cfb\u6570\u3002\u4f8b\u5982\uff0c\u5faa\u73af \\(2n\\) \u6b21\u3001\\(5n + 1\\) \u6b21\u7b49\uff0c\u90fd\u53ef\u4ee5\u7b80\u5316\u8bb0\u4e3a \\(n\\) \u6b21\uff0c\u56e0\u4e3a \\(n\\) \u524d\u9762\u7684\u7cfb\u6570\u5bf9\u65f6\u95f4\u590d\u6742\u5ea6\u6ca1\u6709\u5f71\u54cd\u3002
    3. \u5faa\u73af\u5d4c\u5957\u65f6\u4f7f\u7528\u4e58\u6cd5\u3002\u603b\u64cd\u4f5c\u6570\u91cf\u7b49\u4e8e\u5916\u5c42\u5faa\u73af\u548c\u5185\u5c42\u5faa\u73af\u64cd\u4f5c\u6570\u91cf\u4e4b\u79ef\uff0c\u6bcf\u4e00\u5c42\u5faa\u73af\u4f9d\u7136\u53ef\u4ee5\u5206\u522b\u5957\u7528\u4e0a\u8ff0 1. \u548c 2. \u6280\u5de7\u3002

    \u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u4f7f\u7528\u4e0a\u8ff0\u6280\u5de7\u524d\u3001\u540e\u7684\u7edf\u8ba1\u7ed3\u679c\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} \\]

    \u6700\u7ec8\uff0c\u4e24\u8005\u90fd\u80fd\u63a8\u51fa\u76f8\u540c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7ed3\u679c\uff0c\u5373 \\(O(n^2)\\) \u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    void algorithm(int n) {\nint a = 1;  // +0\uff08\u6280\u5de7 1\uff09\na = a + n;  // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor (int i = 0; i < 5 * n + 1; i++) {\nSystem.out.println(0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (int i = 0; i < 2 * n; i++) {\nfor (int j = 0; j < n + 1; j++) {\nSystem.out.println(0);\n}\n}\n}\n
    void algorithm(int n) {\nint a = 1;  // +0\uff08\u6280\u5de7 1\uff09\na = a + n;  // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor (int i = 0; i < 5 * n + 1; i++) {\ncout << 0 << endl;\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (int i = 0; i < 2 * n; i++) {\nfor (int j = 0; j < n + 1; j++) {\ncout << 0 << endl;\n}\n}\n}\n
    def algorithm(n: int) -> None:\na = 1      # +0\uff08\u6280\u5de7 1\uff09\na = a + n  # +0\uff08\u6280\u5de7 1\uff09\n# +n\uff08\u6280\u5de7 2\uff09\nfor i in range(5 * n + 1):\nprint(0)\n# +n*n\uff08\u6280\u5de7 3\uff09\nfor i in range(2 * n):\nfor j in range(n + 1):\nprint(0)\n
    func algorithm(n int) {\na := 1     // +0\uff08\u6280\u5de7 1\uff09\na = a + n  // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor i := 0; i < 5 * n + 1; i++ {\nfmt.Println(0)\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor i := 0; i < 2 * n; i++ {\nfor j := 0; j < n + 1; j++ {\nfmt.Println(0)\n}\n}\n}\n
    function algorithm(n) {\nlet a = 1;  // +0\uff08\u6280\u5de7 1\uff09\na = a + n;  // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor (let i = 0; i < 5 * n + 1; i++) {\nconsole.log(0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (let i = 0; i < 2 * n; i++) {\nfor (let j = 0; j < n + 1; j++) {\nconsole.log(0);\n}\n}\n}\n
    function algorithm(n: number): void {\nlet a = 1;  // +0\uff08\u6280\u5de7 1\uff09\na = a + n;  // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor (let i = 0; i < 5 * n + 1; i++) {\nconsole.log(0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (let i = 0; i < 2 * n; i++) {\nfor (let j = 0; j < n + 1; j++) {\nconsole.log(0);\n}\n}\n}\n
    void algorithm(int n) {\nint a = 1;  // +0\uff08\u6280\u5de7 1\uff09\na = a + n;  // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor (int i = 0; i < 5 * n + 1; i++) {\nprintf(\"%d\", 0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (int i = 0; i < 2 * n; i++) {\nfor (int j = 0; j < n + 1; j++) {\nprintf(\"%d\", 0);\n}\n}\n}\n
    void algorithm(int n)\n{\nint a = 1;  // +0\uff08\u6280\u5de7 1\uff09\na = a + n;  // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor (int i = 0; i < 5 * n + 1; i++)\n{\nConsole.WriteLine(0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (int i = 0; i < 2 * n; i++)\n{\nfor (int j = 0; j < n + 1; j++)\n{\nConsole.WriteLine(0);\n}\n}\n}\n
    func algorithm(n: Int) {\nvar a = 1 // +0\uff08\u6280\u5de7 1\uff09\na = a + n // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor _ in 0 ..< (5 * n + 1) {\nprint(0)\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor _ in 0 ..< (2 * n) {\nfor _ in 0 ..< (n + 1) {\nprint(0)\n}\n}\n}\n
    \n
    void algorithm(int n) {\nint a = 1; // +0\uff08\u6280\u5de7 1\uff09\na = a + n; // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor (int i = 0; i < 5 * n + 1; i++) {\nprint(0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (int i = 0; i < 2 * n; i++) {\nfor (int j = 0; j < n + 1; j++) {\nprint(0);\n}\n}\n}\n
    "},{"location":"chapter_computational_complexity/time_complexity/#_2","title":"\u7b2c\u4e8c\u6b65\uff1a\u5224\u65ad\u6e10\u8fd1\u4e0a\u754c","text":"

    \u65f6\u95f4\u590d\u6742\u5ea6\u7531\u591a\u9879\u5f0f \\(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\u88ab\u5ffd\u7565\u3002

    \u4ee5\u4e0b\u8868\u683c\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

    \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/#225","title":"2.2.5. \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\u5305\u62ec\uff08\u6309\u7167\u4ece\u4f4e\u5230\u9ad8\u7684\u987a\u5e8f\u6392\u5217\uff09\uff1a

    \\[ \\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} \\]

    Fig. \u65f6\u95f4\u590d\u6742\u5ea6\u7684\u5e38\u89c1\u7c7b\u578b

    Tip

    \u90e8\u5206\u793a\u4f8b\u4ee3\u7801\u9700\u8981\u4e00\u4e9b\u9884\u5907\u77e5\u8bc6\uff0c\u5305\u62ec\u6570\u7ec4\u3001\u9012\u5f52\u7b97\u6cd5\u7b49\u3002\u5982\u679c\u9047\u5230\u4e0d\u7406\u89e3\u7684\u90e8\u5206\uff0c\u8bf7\u4e0d\u8981\u62c5\u5fc3\uff0c\u53ef\u4ee5\u5728\u5b66\u4e60\u5b8c\u540e\u9762\u7ae0\u8282\u540e\u518d\u56de\u987e\u3002\u73b0\u9636\u6bb5\uff0c\u8bf7\u5148\u4e13\u6ce8\u4e8e\u7406\u89e3\u65f6\u95f4\u590d\u6742\u5ea6\u7684\u542b\u4e49\u548c\u63a8\u7b97\u65b9\u6cd5\u3002

    "},{"location":"chapter_computational_complexity/time_complexity/#o1","title":"\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

    \u5bf9\u4e8e\u4ee5\u4e0b\u7b97\u6cd5\uff0c\u5c3d\u7ba1\u64cd\u4f5c\u6570\u91cf size \u53ef\u80fd\u5f88\u5927\uff0c\u4f46\u7531\u4e8e\u5176\u4e0e\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart time_complexity.java
    /* \u5e38\u6570\u9636 */\nint constant(int n) {\nint count = 0;\nint size = 100000;\nfor (int i = 0; i < size; i++)\ncount++;\nreturn count;\n}\n
    time_complexity.cpp
    /* \u5e38\u6570\u9636 */\nint constant(int n) {\nint count = 0;\nint size = 100000;\nfor (int i = 0; i < size; i++)\ncount++;\nreturn count;\n}\n
    time_complexity.py
    def constant(n: int) -> int:\n\"\"\"\u5e38\u6570\u9636\"\"\"\ncount = 0\nsize = 100000\nfor _ in range(size):\ncount += 1\nreturn count\n
    time_complexity.go
    /* \u5e38\u6570\u9636 */\nfunc constant(n int) int {\ncount := 0\nsize := 100000\nfor i := 0; i < size; i++ {\ncount++\n}\nreturn count\n}\n
    time_complexity.js
    /* \u5e38\u6570\u9636 */\nfunction constant(n) {\nlet count = 0;\nconst size = 100000;\nfor (let i = 0; i < size; i++) count++;\nreturn count;\n}\n
    time_complexity.ts
    /* \u5e38\u6570\u9636 */\nfunction constant(n: number): number {\nlet count = 0;\nconst size = 100000;\nfor (let i = 0; i < size; i++) count++;\nreturn count;\n}\n
    time_complexity.c
    /* \u5e38\u6570\u9636 */\nint constant(int n) {\nint count = 0;\nint size = 100000;\nint i = 0;\nfor (int i = 0; i < size; i++) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.cs
    /* \u5e38\u6570\u9636 */\nint constant(int n) {\nint count = 0;\nint size = 100000;\nfor (int i = 0; i < size; i++)\ncount++;\nreturn count;\n}\n
    time_complexity.swift
    /* \u5e38\u6570\u9636 */\nfunc constant(n: Int) -> Int {\nvar count = 0\nlet size = 100_000\nfor _ in 0 ..< size {\ncount += 1\n}\nreturn count\n}\n
    time_complexity.zig
    // \u5e38\u6570\u9636\nfn constant(n: i32) i32 {\n_ = n;\nvar count: i32 = 0;\nconst size: i32 = 100_000;\nvar i: i32 = 0;\nwhile(i<size) : (i += 1) {\ncount += 1;\n}\nreturn count;\n}\n
    time_complexity.dart
    /* \u5e38\u6570\u9636 */\nint constant(int n) {\nint count = 0;\nint size = 100000;\nfor (var i = 0; i < size; i++) {\ncount++;\n}\nreturn count;\n}\n
    "},{"location":"chapter_computational_complexity/time_complexity/#on","title":"\u7ebf\u6027\u9636 \\(O(n)\\)","text":"

    \u7ebf\u6027\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u76f8\u5bf9\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4ee5\u7ebf\u6027\u7ea7\u522b\u589e\u957f\u3002\u7ebf\u6027\u9636\u901a\u5e38\u51fa\u73b0\u5728\u5355\u5c42\u5faa\u73af\u4e2d\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart time_complexity.java
    /* \u7ebf\u6027\u9636 */\nint linear(int n) {\nint count = 0;\nfor (int i = 0; i < n; i++)\ncount++;\nreturn count;\n}\n
    time_complexity.cpp
    /* \u7ebf\u6027\u9636 */\nint linear(int n) {\nint count = 0;\nfor (int i = 0; i < n; i++)\ncount++;\nreturn count;\n}\n
    time_complexity.py
    def linear(n: int) -> int:\n\"\"\"\u7ebf\u6027\u9636\"\"\"\ncount = 0\nfor _ in range(n):\ncount += 1\nreturn count\n
    time_complexity.go
    /* \u7ebf\u6027\u9636 */\nfunc linear(n int) int {\ncount := 0\nfor i := 0; i < n; i++ {\ncount++\n}\nreturn count\n}\n
    time_complexity.js
    /* \u7ebf\u6027\u9636 */\nfunction linear(n) {\nlet count = 0;\nfor (let i = 0; i < n; i++) count++;\nreturn count;\n}\n
    time_complexity.ts
    /* \u7ebf\u6027\u9636 */\nfunction linear(n: number): number {\nlet count = 0;\nfor (let i = 0; i < n; i++) count++;\nreturn count;\n}\n
    time_complexity.c
    /* \u7ebf\u6027\u9636 */\nint linear(int n) {\nint count = 0;\nfor (int i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.cs
    /* \u7ebf\u6027\u9636 */\nint linear(int n) {\nint count = 0;\nfor (int i = 0; i < n; i++)\ncount++;\nreturn count;\n}\n
    time_complexity.swift
    /* \u7ebf\u6027\u9636 */\nfunc linear(n: Int) -> Int {\nvar count = 0\nfor _ in 0 ..< n {\ncount += 1\n}\nreturn count\n}\n
    time_complexity.zig
    // \u7ebf\u6027\u9636\nfn linear(n: i32) i32 {\nvar count: i32 = 0;\nvar i: i32 = 0;\nwhile (i < n) : (i += 1) {\ncount += 1;\n}\nreturn count;\n}\n
    time_complexity.dart
    /* \u7ebf\u6027\u9636 */\nint linear(int n) {\nint count = 0;\nfor (var i = 0; i < n; i++) {\ncount++;\n}\nreturn 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\u3002

    \u5982\u4f55\u786e\u5b9a\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \uff1f

    \u6570\u636e\u5927\u5c0f \\(n\\) \u9700\u6839\u636e\u8f93\u5165\u6570\u636e\u7684\u7c7b\u578b\u6765\u5177\u4f53\u786e\u5b9a\u3002\u4f8b\u5982\uff0c\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u76f4\u63a5\u5c06 \\(n\\) \u89c6\u4e3a\u8f93\u5165\u6570\u636e\u5927\u5c0f\uff1b\u5728\u4e0b\u9762\u904d\u5386\u6570\u7ec4\u7684\u793a\u4f8b\u4e2d\uff0c\u6570\u636e\u5927\u5c0f \\(n\\) \u4e3a\u6570\u7ec4\u7684\u957f\u5ea6\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart time_complexity.java
    /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(int[] nums) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (int num : nums) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.cpp
    /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(vector<int> &nums) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (int num : nums) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.py
    def array_traversal(nums: list[int]) -> int:\n\"\"\"\u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09\"\"\"\ncount = 0\n# \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor num in nums:\ncount += 1\nreturn count\n
    time_complexity.go
    /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunc arrayTraversal(nums []int) int {\ncount := 0\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor range nums {\ncount++\n}\nreturn count\n}\n
    time_complexity.js
    /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunction arrayTraversal(nums) {\nlet count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (let i = 0; i < nums.length; i++) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.ts
    /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunction arrayTraversal(nums: number[]): number {\nlet count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (let i = 0; i < nums.length; i++) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.c
    /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(int *nums, int n) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (int i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.cs
    /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(int[] nums) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nforeach (int num in nums) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.swift
    /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunc arrayTraversal(nums: [Int]) -> Int {\nvar count = 0\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor _ in nums {\ncount += 1\n}\nreturn count\n}\n
    time_complexity.zig
    // \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09\nfn arrayTraversal(nums: []i32) i32 {\nvar count: i32 = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (nums) |_| {\ncount += 1;\n}\nreturn count;\n}\n
    time_complexity.dart
    /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(List<int> nums) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (var num in nums) {\ncount++;\n}\nreturn count;\n}\n
    "},{"location":"chapter_computational_complexity/time_complexity/#on2","title":"\u5e73\u65b9\u9636 \\(O(n^2)\\)","text":"

    \u5e73\u65b9\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u76f8\u5bf9\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f\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\u90fd\u4e3a \\(O(n)\\) \uff0c\u56e0\u6b64\u603b\u4f53\u4e3a \\(O(n^2)\\) \u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart time_complexity.java
    /* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (int i = 0; i < n; i++) {\nfor (int j = 0; j < n; j++) {\ncount++;\n}\n}\nreturn count;\n}\n
    time_complexity.cpp
    /* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (int i = 0; i < n; i++) {\nfor (int j = 0; j < n; j++) {\ncount++;\n}\n}\nreturn count;\n}\n
    time_complexity.py
    def quadratic(n: int) -> int:\n\"\"\"\u5e73\u65b9\u9636\"\"\"\ncount = 0\n# \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor i in range(n):\nfor j in range(n):\ncount += 1\nreturn count\n
    time_complexity.go
    /* \u5e73\u65b9\u9636 */\nfunc quadratic(n int) int {\ncount := 0\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor i := 0; i < n; i++ {\nfor j := 0; j < n; j++ {\ncount++\n}\n}\nreturn count\n}\n
    time_complexity.js
    /* \u5e73\u65b9\u9636 */\nfunction quadratic(n) {\nlet count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (let i = 0; i < n; i++) {\nfor (let j = 0; j < n; j++) {\ncount++;\n}\n}\nreturn count;\n}\n
    time_complexity.ts
    /* \u5e73\u65b9\u9636 */\nfunction quadratic(n: number): number {\nlet count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (let i = 0; i < n; i++) {\nfor (let j = 0; j < n; j++) {\ncount++;\n}\n}\nreturn count;\n}\n
    time_complexity.c
    /* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (int i = 0; i < n; i++) {\nfor (int j = 0; j < n; j++) {\ncount++;\n}\n}\nreturn count;\n}\n
    time_complexity.cs
    /* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (int i = 0; i < n; i++) {\nfor (int j = 0; j < n; j++) {\ncount++;\n}\n}\nreturn count;\n}\n
    time_complexity.swift
    /* \u5e73\u65b9\u9636 */\nfunc quadratic(n: Int) -> Int {\nvar count = 0\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor _ in 0 ..< n {\nfor _ in 0 ..< n {\ncount += 1\n}\n}\nreturn count\n}\n
    time_complexity.zig
    // \u5e73\u65b9\u9636\nfn quadratic(n: i32) i32 {\nvar count: i32 = 0;\nvar i: i32 = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nwhile (i < n) : (i += 1) {\nvar j: i32 = 0;\nwhile (j < n) : (j += 1) {\ncount += 1;\n}\n}\nreturn count;\n}\n
    time_complexity.dart
    /* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (int i = 0; i < n; i++) {\nfor (int j = 0; j < n; j++) {\ncount++;\n}\n}\nreturn count;\n}\n

    Fig. \u5e38\u6570\u9636\u3001\u7ebf\u6027\u9636\u3001\u5e73\u65b9\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

    \u4ee5\u300c\u5192\u6ce1\u6392\u5e8f\u300d\u4e3a\u4f8b\uff0c\u5916\u5c42\u5faa\u73af\u6267\u884c \\(n - 1\\) \u6b21\uff0c\u5185\u5c42\u5faa\u73af\u6267\u884c \\(n-1, n-2, \\cdots, 2, 1\\) \u6b21\uff0c\u5e73\u5747\u4e3a \\(\\frac{n}{2}\\) \u6b21\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002

    \\[ O((n - 1) \\frac{n}{2}) = O(n^2) \\] JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart time_complexity.java
    /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(int[] nums) {\nint count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (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 \nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n
    time_complexity.cpp
    /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(vector<int> &nums) {\nint count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (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 \nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n
    time_complexity.py
    def bubble_sort(nums: list[int]) -> int:\n\"\"\"\u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09\"\"\"\ncount = 0  # \u8ba1\u6570\u5668\n# \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor 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 \nfor j in range(i):\nif nums[j] > nums[j + 1]:\n# \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\ntmp: int = nums[j]\nnums[j] = nums[j + 1]\nnums[j + 1] = tmp\ncount += 3  # \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\nreturn count\n
    time_complexity.go
    /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunc bubbleSort(nums []int) int {\ncount := 0 // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor 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\nfor j := 0; j < i; j++ {\nif nums[j] > nums[j+1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\ntmp := nums[j]\nnums[j] = nums[j+1]\nnums[j+1] = tmp\ncount += 3 // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count\n}\n
    time_complexity.js
    /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunction bubbleSort(nums) {\nlet count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (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 \nfor (let j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n
    time_complexity.ts
    /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunction bubbleSort(nums: number[]): number {\nlet count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (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 \nfor (let j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n
    time_complexity.c
    /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(int *nums, int n) {\nint count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (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 \nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n
    time_complexity.cs
    /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(int[] nums) {\nint count = 0;  // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (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 \nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3;  // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n
    time_complexity.swift
    /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunc bubbleSort(nums: inout [Int]) -> Int {\nvar count = 0 // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor 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 \nfor j in 0 ..< i {\nif nums[j] > nums[j + 1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j]\nnums[j] = nums[j + 1]\nnums[j + 1] = tmp\ncount += 3 // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count\n}\n
    time_complexity.zig
    // \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09\nfn bubbleSort(nums: []i32) i32 {\nvar count: i32 = 0;  // \u8ba1\u6570\u5668 \n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nvar i: i32 = @intCast(i32, nums.len ) - 1;\nwhile (i > 0) : (i -= 1) {\nvar 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 \nwhile (j < i) : (j += 1) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nvar tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3;  // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n
    time_complexity.dart
    /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(List<int> nums) {\nint count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (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\nfor (var j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n
    "},{"location":"chapter_computational_complexity/time_complexity/#o2n","title":"\u6307\u6570\u9636 \\(O(2^n)\\)","text":"

    Note

    \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

    \u6307\u6570\u9636\u589e\u957f\u975e\u5e38\u8fc5\u901f\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u901a\u5e38\u662f\u4e0d\u53ef\u63a5\u53d7\u7684\u3002\u82e5\u4e00\u4e2a\u95ee\u9898\u4f7f\u7528\u300c\u66b4\u529b\u679a\u4e3e\u300d\u6c42\u89e3\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \uff0c\u90a3\u4e48\u901a\u5e38\u9700\u8981\u4f7f\u7528\u300c\u52a8\u6001\u89c4\u5212\u300d\u6216\u300c\u8d2a\u5fc3\u7b97\u6cd5\u300d\u7b49\u65b9\u6cd5\u6765\u89e3\u51b3\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart time_complexity.java
    /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\nint count = 0, base = 1;\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (int i = 0; i < n; i++) {\nfor (int j = 0; j < base; j++) {\ncount++;\n}\nbase *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n
    time_complexity.cpp
    /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\nint count = 0, base = 1;\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (int i = 0; i < n; i++) {\nfor (int j = 0; j < base; j++) {\ncount++;\n}\nbase *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n
    time_complexity.py
    def exponential(n: int) -> int:\n\"\"\"\u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\"\"\"\ncount = 0\nbase = 1\n# cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor _ in range(n):\nfor _ in range(base):\ncount += 1\nbase *= 2\n# count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count\n
    time_complexity.go
    /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09*/\nfunc exponential(n int) int {\ncount, base := 0, 1\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor i := 0; i < n; i++ {\nfor j := 0; j < base; j++ {\ncount++\n}\nbase *= 2\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count\n}\n
    time_complexity.js
    /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction exponential(n) {\nlet count = 0,\nbase = 1;\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (let i = 0; i < n; i++) {\nfor (let j = 0; j < base; j++) {\ncount++;\n}\nbase *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n
    time_complexity.ts
    /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction exponential(n: number): number {\nlet count = 0,\nbase = 1;\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (let i = 0; i < n; i++) {\nfor (let j = 0; j < base; j++) {\ncount++;\n}\nbase *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n
    time_complexity.c
    /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\nint count = 0;\nint bas = 1;\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (int i = 0; i < n; i++) {\nfor (int j = 0; j < bas; j++) {\ncount++;\n}\nbas *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n
    time_complexity.cs
    /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\nint count = 0, bas = 1;\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (int i = 0; i < n; i++) {\nfor (int j = 0; j < bas; j++) {\ncount++;\n}\nbas *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n
    time_complexity.swift
    /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunc exponential(n: Int) -> Int {\nvar count = 0\nvar base = 1\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor _ in 0 ..< n {\nfor _ in 0 ..< base {\ncount += 1\n}\nbase *= 2\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count\n}\n
    time_complexity.zig
    // \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\nfn exponential(n: i32) i32 {\nvar count: i32 = 0;\nvar bas: i32 = 1;\nvar i: i32 = 0;\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nwhile (i < n) : (i += 1) {\nvar j: i32 = 0;\nwhile (j < bas) : (j += 1) {\ncount += 1;\n}\nbas *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n
    time_complexity.dart
    /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\nint count = 0, base = 1;\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (var i = 0; i < n; i++) {\nfor (var j = 0; j < base; j++) {\ncount++;\n}\nbase *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n

    Fig. \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\u3002\u4f8b\u5982\u4ee5\u4e0b\u4ee3\u7801\uff0c\u4e0d\u65ad\u5730\u4e00\u5206\u4e3a\u4e8c\uff0c\u7ecf\u8fc7 \\(n\\) \u6b21\u5206\u88c2\u540e\u505c\u6b62\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart time_complexity.java
    /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\nif (n == 1)\nreturn 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
    time_complexity.cpp
    /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\nif (n == 1)\nreturn 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
    time_complexity.py
    def exp_recur(n: int) -> int:\n\"\"\"\u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\nif n == 1:\nreturn 1\nreturn exp_recur(n - 1) + exp_recur(n - 1) + 1\n
    time_complexity.go
    /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09*/\nfunc expRecur(n int) int {\nif n == 1 {\nreturn 1\n}\nreturn expRecur(n-1) + expRecur(n-1) + 1\n}\n
    time_complexity.js
    /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction expRecur(n) {\nif (n == 1) return 1;\nreturn 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 {\nif (n == 1) return 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
    time_complexity.c
    /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\nif (n == 1)\nreturn 1;\nreturn 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) {\nif (n == 1) return 1;\nreturn 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 {\nif n == 1 {\nreturn 1\n}\nreturn expRecur(n: n - 1) + expRecur(n: n - 1) + 1\n}\n
    time_complexity.zig
    // \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn expRecur(n: i32) i32 {\nif (n == 1) return 1;\nreturn 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) {\nif (n == 1) return 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
    "},{"location":"chapter_computational_complexity/time_complexity/#olog-n","title":"\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\u7684\u60c5\u51b5\u201d\u3002\u5bf9\u6570\u9636\u4ec5\u6b21\u4e8e\u5e38\u6570\u9636\uff0c\u65f6\u95f4\u589e\u957f\u7f13\u6162\uff0c\u662f\u7406\u60f3\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002

    \u5bf9\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u300c\u4e8c\u5206\u67e5\u627e\u300d\u548c\u300c\u5206\u6cbb\u7b97\u6cd5\u300d\u4e2d\uff0c\u4f53\u73b0\u4e86\u201c\u4e00\u5206\u4e3a\u591a\u201d\u548c\u201c\u5316\u7e41\u4e3a\u7b80\u201d\u7684\u7b97\u6cd5\u601d\u60f3\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart time_complexity.java
    /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(float n) {\nint count = 0;\nwhile (n > 1) {\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n
    time_complexity.cpp
    /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(float n) {\nint count = 0;\nwhile (n > 1) {\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n
    time_complexity.py
    def logarithmic(n: float) -> int:\n\"\"\"\u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\"\"\"\ncount = 0\nwhile n > 1:\nn = n / 2\ncount += 1\nreturn count\n
    time_complexity.go
    /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09*/\nfunc logarithmic(n float64) int {\ncount := 0\nfor n > 1 {\nn = n / 2\ncount++\n}\nreturn count\n}\n
    time_complexity.js
    /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction logarithmic(n) {\nlet count = 0;\nwhile (n > 1) {\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n
    time_complexity.ts
    /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction logarithmic(n: number): number {\nlet count = 0;\nwhile (n > 1) {\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n
    time_complexity.c
    /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(float n) {\nint count = 0;\nwhile (n > 1) {\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n
    time_complexity.cs
    /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(float n) {\nint count = 0;\nwhile (n > 1) {\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n
    time_complexity.swift
    /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunc logarithmic(n: Double) -> Int {\nvar count = 0\nvar n = n\nwhile n > 1 {\nn = n / 2\ncount += 1\n}\nreturn count\n}\n
    time_complexity.zig
    // \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\nfn logarithmic(n: f32) i32 {\nvar count: i32 = 0;\nvar n_var = n;\nwhile (n_var > 1)\n{\nn_var = n_var / 2;\ncount +=1;\n}\nreturn count;\n}\n
    time_complexity.dart
    /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(num n) {\nint count = 0;\nwhile (n > 1) {\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n

    Fig. \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\u3002\u4ee5\u4e0b\u4ee3\u7801\u5f62\u6210\u4e86\u4e00\u4e2a\u9ad8\u5ea6\u4e3a \\(\\log_2 n\\) \u7684\u9012\u5f52\u6811\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart time_complexity.java
    /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(float n) {\nif (n <= 1)\nreturn 0;\nreturn logRecur(n / 2) + 1;\n}\n
    time_complexity.cpp
    /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(float n) {\nif (n <= 1)\nreturn 0;\nreturn logRecur(n / 2) + 1;\n}\n
    time_complexity.py
    def log_recur(n: float) -> int:\n\"\"\"\u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\nif n <= 1:\nreturn 0\nreturn log_recur(n / 2) + 1\n
    time_complexity.go
    /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09*/\nfunc logRecur(n float64) int {\nif n <= 1 {\nreturn 0\n}\nreturn logRecur(n/2) + 1\n}\n
    time_complexity.js
    /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction logRecur(n) {\nif (n <= 1) return 0;\nreturn logRecur(n / 2) + 1;\n}\n
    time_complexity.ts
    /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction logRecur(n: number): number {\nif (n <= 1) return 0;\nreturn logRecur(n / 2) + 1;\n}\n
    time_complexity.c
    /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(float n) {\nif (n <= 1)\nreturn 0;\nreturn logRecur(n / 2) + 1;\n}\n
    time_complexity.cs
    /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(float n) {\nif (n <= 1) return 0;\nreturn logRecur(n / 2) + 1;\n}\n
    time_complexity.swift
    /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc logRecur(n: Double) -> Int {\nif n <= 1 {\nreturn 0\n}\nreturn logRecur(n: n / 2) + 1\n}\n
    time_complexity.zig
    // \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn logRecur(n: f32) i32 {\nif (n <= 1) return 0;\nreturn logRecur(n / 2) + 1;\n}\n
    time_complexity.dart
    /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(num n) {\nif (n <= 1) return 0;\nreturn logRecur(n / 2) + 1;\n}\n
    "},{"location":"chapter_computational_complexity/time_complexity/#on-log-n","title":"\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

    \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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart time_complexity.java
    /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(float n) {\nif (n <= 1)\nreturn 1;\nint count = linearLogRecur(n / 2) +\nlinearLogRecur(n / 2);\nfor (int i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.cpp
    /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(float n) {\nif (n <= 1)\nreturn 1;\nint count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\nfor (int i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.py
    def linear_log_recur(n: float) -> int:\n\"\"\"\u7ebf\u6027\u5bf9\u6570\u9636\"\"\"\nif n <= 1:\nreturn 1\ncount: int = linear_log_recur(n // 2) + linear_log_recur(n // 2)\nfor _ in range(n):\ncount += 1\nreturn count\n
    time_complexity.go
    /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunc linearLogRecur(n float64) int {\nif n <= 1 {\nreturn 1\n}\ncount := linearLogRecur(n/2) +\nlinearLogRecur(n/2)\nfor i := 0.0; i < n; i++ {\ncount++\n}\nreturn count\n}\n
    time_complexity.js
    /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunction linearLogRecur(n) {\nif (n <= 1) return 1;\nlet count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\nfor (let i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.ts
    /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunction linearLogRecur(n: number): number {\nif (n <= 1) return 1;\nlet count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\nfor (let i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.c
    /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(float n) {\nif (n <= 1)\nreturn 1;\nint count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\nfor (int i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.cs
    /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(float n) {\nif (n <= 1) return 1;\nint count = linearLogRecur(n / 2) +\nlinearLogRecur(n / 2);\nfor (int i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.swift
    /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunc linearLogRecur(n: Double) -> Int {\nif n <= 1 {\nreturn 1\n}\nvar count = linearLogRecur(n: n / 2) + linearLogRecur(n: n / 2)\nfor _ in stride(from: 0, to: n, by: 1) {\ncount += 1\n}\nreturn count\n}\n
    time_complexity.zig
    // \u7ebf\u6027\u5bf9\u6570\u9636\nfn linearLogRecur(n: f32) i32 {\nif (n <= 1) return 1;\nvar count: i32 = linearLogRecur(n / 2) +\nlinearLogRecur(n / 2);\nvar i: f32 = 0;\nwhile (i < n) : (i += 1) {\ncount += 1;\n}\nreturn count;\n}\n
    time_complexity.dart
    /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(num n) {\nif (n <= 1) return 1;\nint count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\nfor (var i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n

    Fig. \u7ebf\u6027\u5bf9\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

    "},{"location":"chapter_computational_complexity/time_complexity/#on_1","title":"\u9636\u4e58\u9636 \\(O(n!)\\)","text":"

    \u9636\u4e58\u9636\u5bf9\u5e94\u6570\u5b66\u4e0a\u7684\u300c\u5168\u6392\u5217\u300d\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 \\cdots \\times 2 \\times 1 \\]

    \u9636\u4e58\u901a\u5e38\u4f7f\u7528\u9012\u5f52\u5b9e\u73b0\u3002\u4f8b\u5982\u4ee5\u4e0b\u4ee3\u7801\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\u7ec8\u6b62\u5206\u88c2\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart time_complexity.java
    /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\nif (n == 0)\nreturn 1;\nint count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor (int i = 0; i < n; i++) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n
    time_complexity.cpp
    /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\nif (n == 0)\nreturn 1;\nint count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor (int i = 0; i < n; i++) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n
    time_complexity.py
    def factorial_recur(n: int) -> int:\n\"\"\"\u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\nif n == 0:\nreturn 1\ncount = 0\n# \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor _ in range(n):\ncount += factorial_recur(n - 1)\nreturn count\n
    time_complexity.go
    /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc factorialRecur(n int) int {\nif n == 0 {\nreturn 1\n}\ncount := 0\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor i := 0; i < n; i++ {\ncount += factorialRecur(n - 1)\n}\nreturn count\n}\n
    time_complexity.js
    /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction factorialRecur(n) {\nif (n == 0) return 1;\nlet count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor (let i = 0; i < n; i++) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n
    time_complexity.ts
    /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction factorialRecur(n: number): number {\nif (n == 0) return 1;\nlet count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor (let i = 0; i < n; i++) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n
    time_complexity.c
    /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\nif (n == 0)\nreturn 1;\nint count = 0;\nfor (int i = 0; i < n; i++) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n
    time_complexity.cs
    /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\nif (n == 0) return 1;\nint count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor (int i = 0; i < n; i++) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n
    time_complexity.swift
    /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc factorialRecur(n: Int) -> Int {\nif n == 0 {\nreturn 1\n}\nvar count = 0\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor _ in 0 ..< n {\ncount += factorialRecur(n: n - 1)\n}\nreturn count\n}\n
    time_complexity.zig
    // \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn factorialRecur(n: i32) i32 {\nif (n == 0) return 1;\nvar count: i32 = 0;\nvar i: i32 = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nwhile (i < n) : (i += 1) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n
    time_complexity.dart
    /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\nif (n == 0) return 1;\nint count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor (var i = 0; i < n; i++) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n

    Fig. \u9636\u4e58\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

    "},{"location":"chapter_computational_complexity/time_complexity/#226","title":"2.2.6. \u00a0 \u6700\u5dee\u3001\u6700\u4f73\u3001\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6","text":"

    \u67d0\u4e9b\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u662f\u56fa\u5b9a\u7684\uff0c\u800c\u662f\u4e0e\u8f93\u5165\u6570\u636e\u7684\u5206\u5e03\u6709\u5173\u3002\u4f8b\u5982\uff0c\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\u4f46\u5143\u7d20\u987a\u5e8f\u662f\u968f\u673a\u6253\u4e71\u7684\uff1b\u7b97\u6cd5\u7684\u4efb\u52a1\u662f\u8fd4\u56de\u5143\u7d20 \\(1\\) \u7684\u7d22\u5f15\u3002\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\uff1a

    • \u5f53 nums = [?, ?, ..., 1] \uff0c\u5373\u5f53\u672b\u5c3e\u5143\u7d20\u662f \\(1\\) \u65f6\uff0c\u9700\u8981\u5b8c\u6574\u904d\u5386\u6570\u7ec4\uff0c\u6b64\u65f6\u8fbe\u5230 \u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\)\uff1b
    • \u5f53 nums = [1, ?, ?, ...] \uff0c\u5373\u5f53\u9996\u4e2a\u6570\u5b57\u4e3a \\(1\\) \u65f6\uff0c\u65e0\u8bba\u6570\u7ec4\u591a\u957f\u90fd\u4e0d\u9700\u8981\u7ee7\u7eed\u904d\u5386\uff0c\u6b64\u65f6\u8fbe\u5230 \u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 \\(\\Omega(1)\\)\uff1b

    \u201c\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c\u201d\u4f7f\u7528\u5927 \\(O\\) \u8bb0\u53f7\u8868\u793a\uff0c\u4ee3\u8868\u300c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u300d\u3002\u76f8\u5e94\u5730\uff0c\u201c\u51fd\u6570\u6e10\u8fd1\u4e0b\u754c\u201d\u7528 \\(\\Omega\\) \u8bb0\u53f7\u6765\u8868\u793a\uff0c\u4ee3\u8868\u300c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u300d\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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) {\nInteger[] nums = new Integer[n];\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (int i = 0; i < n; i++) {\nnums[i] = i + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nCollections.shuffle(Arrays.asList(nums));\n// Integer[] -> int[]\nint[] res = new int[n];\nfor (int i = 0; i < n; i++) {\nres[i] = nums[i];\n}\nreturn res;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int[] nums) {\nfor (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)\nif (nums[i] == 1)\nreturn i;\n}\nreturn -1;\n}\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) {\nvector<int> nums(n);\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (int i = 0; i < n; i++) {\nnums[i] = i + 1;\n}\n// \u4f7f\u7528\u7cfb\u7edf\u65f6\u95f4\u751f\u6210\u968f\u673a\u79cd\u5b50\nunsigned seed = chrono::system_clock::now().time_since_epoch().count();\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nshuffle(nums.begin(), nums.end(), default_random_engine(seed));\nreturn nums;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(vector<int> &nums) {\nfor (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)\nif (nums[i] == 1)\nreturn i;\n}\nreturn -1;\n}\n
    worst_best_time_complexity.py
    def 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\nnums = [i for i in range(1, n + 1)]\n# \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nrandom.shuffle(nums)\nreturn nums\ndef find_one(nums: list[int]) -> int:\n\"\"\"\u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15\"\"\"\nfor 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)\nif nums[i] == 1:\nreturn i\nreturn -1\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 {\nnums := make([]int, n)\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor i := 0; i < n; i++ {\nnums[i] = i + 1\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nrand.Shuffle(len(nums), func(i, j int) {\nnums[i], nums[j] = nums[j], nums[i]\n})\nreturn nums\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunc findOne(nums []int) int {\nfor 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)\nif nums[i] == 1 {\nreturn i\n}\n}\nreturn -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) {\nconst nums = Array(n);\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (let i = 0; i < n; i++) {\nnums[i] = i + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nfor (let i = 0; i < n; i++) {\nconst r = Math.floor(Math.random() * (i + 1));\nconst temp = nums[i];\nnums[i] = nums[r];\nnums[r] = temp;\n}\nreturn nums;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunction findOne(nums) {\nfor (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)\nif (nums[i] === 1) {\nreturn i;\n}\n}\nreturn -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[] {\nconst nums = Array(n);\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (let i = 0; i < n; i++) {\nnums[i] = i + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nfor (let i = 0; i < n; i++) {\nconst r = Math.floor(Math.random() * (i + 1));\nconst temp = nums[i];\nnums[i] = nums[r];\nnums[r] = temp;\n}\nreturn nums;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunction findOne(nums: number[]): number {\nfor (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)\nif (nums[i] === 1) {\nreturn i;\n}\n}\nreturn -1;\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\u4e3an\uff0c\u5143\u7d20\u7c7b\u578b\u4e3aint\uff09\nint *nums = (int *)malloc(n * sizeof(int));\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (int i = 0; i < n; i++) {\nnums[i] = i + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nfor (int i = n - 1; i > 0; i--) {\nint j = rand() % (i + 1);\nint temp = nums[i];\nnums[i] = nums[j];\nnums[j] = temp;\n}\nreturn nums;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int *nums, int n) {\nfor (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)\nif (nums[i] == 1)\nreturn i;\n}\nreturn -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) {\nint[] nums = new int[n];\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (int i = 0; i < n; i++) {\nnums[i] = i + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nfor (int i = 0; i < nums.Length; i++) {\nvar index = new Random().Next(i, nums.Length);\nvar tmp = nums[i];\nvar ran = nums[index];\nnums[i] = ran;\nnums[index] = tmp;\n}\nreturn nums;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int[] nums) {\nfor (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)\nif (nums[i] == 1)\nreturn i;\n}\nreturn -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 }\nvar nums = Array(1 ... n)\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nnums.shuffle()\nreturn nums\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunc findOne(nums: [Int]) -> Int {\nfor 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)\nif nums[i] == 1 {\nreturn i\n}\n}\nreturn -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\npub fn randomNumbers(comptime n: usize) [n]i32 {\nvar nums: [n]i32 = undefined;\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (nums) |*num, i| {\nnum.* = @intCast(i32, i) + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nconst rand = std.crypto.random;\nrand.shuffle(i32, &nums);\nreturn nums;\n}\n// \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15\npub fn findOne(nums: []i32) i32 {\nfor (nums) |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)\nif (num == 1) return @intCast(i32, i);\n}\nreturn -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) {\nfinal nums = List.filled(n, 0);\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (var i = 0; i < n; i++) {\nnums[i] = i + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nnums.shuffle();\nreturn nums;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(List<int> nums) {\nfor (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)\nif (nums[i] == 1) return i;\n}\nreturn -1;\n}\n

    Tip

    \u5b9e\u9645\u5e94\u7528\u4e2d\u6211\u4eec\u5f88\u5c11\u4f7f\u7528\u300c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u300d\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\u76f8\u53cd\uff0c\u300c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u300d\u66f4\u4e3a\u5b9e\u7528\uff0c\u56e0\u4e3a\u5b83\u7ed9\u51fa\u4e86\u4e00\u4e2a\u201c\u6548\u7387\u5b89\u5168\u503c\u201d\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\u6216\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u53ea\u51fa\u73b0\u5728\u201c\u7279\u6b8a\u5206\u5e03\u7684\u6570\u636e\u201d\u4e2d\uff0c\u8fd9\u4e9b\u60c5\u51b5\u7684\u51fa\u73b0\u6982\u7387\u53ef\u80fd\u5f88\u5c0f\uff0c\u56e0\u6b64\u5e76\u4e0d\u80fd\u6700\u771f\u5b9e\u5730\u53cd\u6620\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u3002\u76f8\u8f83\u4e4b\u4e0b\uff0c\u300c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u300d\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\u5219\u662f\u6570\u7ec4\u957f\u5ea6\u7684\u4e00\u534a \\(\\frac{n}{2}\\) \uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(\\Theta(\\frac{n}{2}) = \\Theta(n)\\) \u3002

    \u4f46\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5c24\u5176\u662f\u8f83\u4e3a\u590d\u6742\u7684\u7b97\u6cd5\uff0c\u8ba1\u7b97\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u6bd4\u8f83\u56f0\u96be\uff0c\u56e0\u4e3a\u5f88\u96be\u7b80\u4fbf\u5730\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\u6211\u4eec\u5e38\u5e38\u4f7f\u7528\u5b83\u6765\u8868\u793a\u300c\u5e73\u5747\u590d\u6742\u5ea6\u300d\uff0c\u4f46\u4ece\u4e25\u683c\u610f\u4e49\u4e0a\u770b\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":"3. \u00a0 \u6570\u636e\u7ed3\u6784\u7b80\u4ecb","text":""},{"location":"chapter_data_structure/basic_data_types/","title":"3.2. \u00a0 \u57fa\u672c\u6570\u636e\u7c7b\u578b","text":"

    \u8c08\u53ca\u8ba1\u7b97\u673a\u4e2d\u7684\u6570\u636e\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\u3002\u5b83\u5305\u62ec\uff1a

    • \u6574\u6570\u7c7b\u578b byte , short , int , long \uff1b
    • \u6d6e\u70b9\u6570\u7c7b\u578b float , double \uff0c\u7528\u4e8e\u8868\u793a\u5c0f\u6570\uff1b
    • \u5b57\u7b26\u7c7b\u578b char \uff0c\u7528\u4e8e\u8868\u793a\u5404\u79cd\u8bed\u8a00\u7684\u5b57\u6bcd\u3001\u6807\u70b9\u7b26\u53f7\u3001\u751a\u81f3\u8868\u60c5\u7b26\u53f7\u7b49\uff1b
    • \u5e03\u5c14\u7c7b\u578b bool \uff0c\u7528\u4e8e\u8868\u793a\u201c\u662f\u201d\u4e0e\u201c\u5426\u201d\u5224\u65ad\uff1b

    \u6240\u6709\u57fa\u672c\u6570\u636e\u7c7b\u578b\u90fd\u4ee5\u4e8c\u8fdb\u5236\u7684\u5f62\u5f0f\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u4e2d\u3002\u5728\u8ba1\u7b97\u673a\u4e2d\uff0c\u6211\u4eec\u5c06 \\(1\\) \u4e2a\u4e8c\u8fdb\u5236\u4f4d\u79f0\u4e3a \\(1\\) \u6bd4\u7279\uff0c\u5e76\u89c4\u5b9a \\(1\\) \u5b57\u8282\uff08byte\uff09\u7531 \\(8\\) \u6bd4\u7279\uff08bits\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\uff0c\u4f8b\u5982\uff1a

    • \u6574\u6570\u7c7b\u578b byte \u5360\u7528 \\(1\\) byte = \\(8\\) bits \uff0c\u53ef\u4ee5\u8868\u793a \\(2^{8}\\) \u4e2a\u4e0d\u540c\u7684\u6570\u5b57\uff1b
    • \u6574\u6570\u7c7b\u578b int \u5360\u7528 \\(4\\) bytes = \\(32\\) bits \uff0c\u53ef\u4ee5\u8868\u793a \\(2^{32}\\) \u4e2a\u6570\u5b57\uff1b

    \u4e0b\u8868\u5217\u4e3e\u4e86\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\u9700\u786c\u80cc\uff0c\u5927\u81f4\u7406\u89e3\u5373\u53ef\uff0c\u9700\u8981\u65f6\u53ef\u4ee5\u901a\u8fc7\u67e5\u8868\u6765\u56de\u5fc6\u3002

    \u7c7b\u578b \u7b26\u53f7 \u5360\u7528\u7a7a\u95f4 \u6700\u5c0f\u503c \u6700\u5927\u503c \u9ed8\u8ba4\u503c \u6574\u6570 byte 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 f\\) double 8 bytes \\(2.225 \\times 10^{-308}\\) \\(1.798 \\times 10^{308}\\) \\(0.0\\) \u5b57\u7b26 char 2 bytes / 1 byte \\(0\\) \\(2^{16} - 1\\) \\(0\\) \u5e03\u5c14 bool 1 byte \\(\\text{false}\\) \\(\\text{true}\\) \\(\\text{false}\\)

    Tip

    \u5b57\u7b26\u7684\u5360\u7528\u7a7a\u95f4\u5927\u5c0f\u53d6\u51b3\u4e8e\u7f16\u7a0b\u8bed\u8a00\u91c7\u7528\u7684\u5b57\u7b26\u7f16\u7801\u65b9\u6cd5\uff0c\u8be6\u89c1\u300c\u5b57\u7b26\u7f16\u7801\u300d\u7ae0\u8282\u3002

    \u73b0\u4ee3\u8ba1\u7b97\u673a CPU \u901a\u5e38\u5c06 1 \u5b57\u8282\u4f5c\u4e3a\u6700\u5c0f\u5bfb\u5740\u5185\u5b58\u5355\u5143\u3002\u56e0\u6b64\uff0c\u5373\u4f7f\u8868\u793a\u5e03\u5c14\u91cf\u4ec5\u9700 1 \u4f4d\uff08\\(0\\) \u6216 \\(1\\)\uff09\uff0c\u5b83\u5728\u5185\u5b58\u4e2d\u901a\u5e38\u88ab\u5b58\u50a8\u4e3a 1 \u5b57\u8282\u3002

    \u90a3\u4e48\uff0c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e0e\u6570\u636e\u7ed3\u6784\u4e4b\u95f4\u6709\u4ec0\u4e48\u8054\u7cfb\u4e0e\u533a\u522b\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\u5b83\u7684\u4e3b\u8bed\u662f\u201c\u7ed3\u6784\u201d\uff0c\u800c\u975e\u201c\u6570\u636e\u201d\u3002\u5982\u679c\u60f3\u8981\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 \u3001\u8fd8\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\u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    /* \u4f7f\u7528\u591a\u79cd\u300c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u300d\u6765\u521d\u59cb\u5316\u300c\u6570\u7ec4\u300d */\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nboolean[] booleans = new boolean[5];\n
    /* \u4f7f\u7528\u591a\u79cd\u300c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u300d\u6765\u521d\u59cb\u5316\u300c\u6570\u7ec4\u300d */\nint numbers[5];\nfloat decimals[5];\nchar characters[5];\nbool booleans[5];\n
    # Python \u7684 list \u53ef\u4ee5\u81ea\u7531\u5b58\u50a8\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5bf9\u8c61\nlist = [0, 0.0, 'a', False]\n
    // \u4f7f\u7528\u591a\u79cd\u300c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u300d\u6765\u521d\u59cb\u5316\u300c\u6570\u7ec4\u300d\nvar numbers = [5]int{}\nvar decimals = [5]float64{}\nvar characters = [5]byte{}\nvar booleans = [5]bool{}\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\u300c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u300d\u6765\u521d\u59cb\u5316\u300c\u6570\u7ec4\u300d */\nconst numbers: number[] = [];\nconst characters: string[] = [];\nconst booleans: boolean[] = [];\n
    /* \u4f7f\u7528\u591a\u79cd\u300c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u300d\u6765\u521d\u59cb\u5316\u300c\u6570\u7ec4\u300d */\nint numbers[10];\nfloat decimals[10];\nchar characters[10];\nbool booleans[10];\n
    /* \u4f7f\u7528\u591a\u79cd\u300c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u300d\u6765\u521d\u59cb\u5316\u300c\u6570\u7ec4\u300d */\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nbool[] booleans = new bool[5];\n
    /* \u4f7f\u7528\u591a\u79cd\u300c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u300d\u6765\u521d\u59cb\u5316\u300c\u6570\u7ec4\u300d */\nlet numbers = Array(repeating: Int(), count: 5)\nlet decimals = Array(repeating: Double(), count: 5)\nlet characters = Array(repeating: Character(\"a\"), count: 5)\nlet booleans = Array(repeating: Bool(), count: 5)\n
    \n
    /* \u4f7f\u7528\u591a\u79cd\u300c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u300d\u6765\u521d\u59cb\u5316\u300c\u6570\u7ec4\u300d */\nList<int> numbers = List.filled(5, 0);\nList<double> decimals = List.filled(5, 0.0);\nList<String> characters = List.filled(5, 'a');\nList<bool> booleans = List.filled(5, false);\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\u300c\u5b57\u7b26\u96c6\u300d\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

    "},{"location":"chapter_data_structure/character_encoding/#341-ascii","title":"3.4.1. \u00a0 ASCII \u5b57\u7b26\u96c6","text":"

    \u300cASCII \u7801\u300d\u662f\u6700\u65e9\u51fa\u73b0\u7684\u5b57\u7b26\u96c6\uff0c\u5168\u79f0\u4e3a\u201c\u7f8e\u56fd\u6807\u51c6\u4fe1\u606f\u4ea4\u6362\u4ee3\u7801\u201d\u3002\u5b83\u4f7f\u7528 7 \u4f4d\u4e8c\u8fdb\u5236\u6570\uff08\u5373\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\u8fd9\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

    Fig. 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\u4f8b\u5982\uff0c\u6c49\u5b57\u5927\u7ea6\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\u7684\u7f55\u89c1\u5b57\u548c\u7e41\u4f53\u5b57\u3002\u4e4b\u540e\u5728 GB2312 \u7684\u57fa\u7840\u4e0a\uff0c\u6269\u5c55\u5f97\u5230\u4e86\u300cGBK\u300d\u5b57\u7b26\u96c6\uff0c\u5b83\u5171\u6536\u5f55\u4e86 21886 \u4e2a\u6c49\u5b57\u3002\u5728 GBK \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\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\uff1b\u53e6\u4e00\u65b9\u9762\uff0c\u540c\u4e00\u79cd\u8bed\u8a00\u4e5f\u5b58\u5728\u591a\u79cd\u5b57\u7b26\u96c6\u6807\u51c6\uff0c\u5982\u679c\u4e24\u53f0\u7535\u8111\u5b89\u88c5\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\u4eba\u4eec\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\u5168\u79f0\u4e3a\u201c\u7edf\u4e00\u5b57\u7b26\u7f16\u7801\u201d\uff0c\u7406\u8bba\u4e0a\u80fd\u5bb9\u7eb3\u4e00\u767e\u591a\u4e07\u4e2a\u5b57\u7b26\u3002\u5b83\u81f4\u529b\u4e8e\u5c06\u5168\u7403\u8303\u56f4\u5185\u7684\u5b57\u7b26\u7eb3\u5165\u5230\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\u6b62 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\u3001\u751a\u81f3\u662f\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 3 \u5b57\u8282\u751a\u81f3 4 \u5b57\u8282\u3002

    Unicode \u662f\u4e00\u79cd\u5b57\u7b26\u96c6\u6807\u51c6\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\u540c\u4e00\u4e2a\u6587\u672c\u4e2d\u65f6\uff0c\u7cfb\u7edf\u5982\u4f55\u89e3\u6790\u5b57\u7b26\uff1f\u4f8b\u5982\uff0c\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\u4e0b\u56fe\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 \uff0c\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\u51fa\u8fd9\u4e2a\u77ed\u8bed\u7684\u5185\u5bb9\u4e86\u3002

    Fig. Unicode \u7f16\u7801\u793a\u4f8b

    \u7136\u800c\uff0cASCII \u7801\u5df2\u7ecf\u5411\u6211\u4eec\u8bc1\u660e\uff0c\u7f16\u7801\u82f1\u6587\u53ea\u9700\u8981 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\u5927\u5c0f\u7684 2 \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\u7684\u7f16\u7801\uff0c\u4f7f\u7528 1 \u5230 4 \u4e2a\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\u8981 1 \u4e2a\u5b57\u8282\uff0c\u62c9\u4e01\u5b57\u6bcd\u548c\u5e0c\u814a\u5b57\u6bcd\u9700\u8981 2 \u4e2a\u5b57\u8282\uff0c\u5e38\u7528\u7684\u4e2d\u6587\u5b57\u7b26\u9700\u8981 3 \u4e2a\u5b57\u8282\uff0c\u5176\u4ed6\u7684\u4e00\u4e9b\u751f\u50fb\u5b57\u7b26\u9700\u8981 4 \u4e2a\u5b57\u8282\u3002

    UTF-8 \u7684\u7f16\u7801\u89c4\u5219\u5e76\u4e0d\u590d\u6742\uff0c\u5206\u4e3a\u4e24\u79cd\u60c5\u51b5\uff1a

    • \u5bf9\u4e8e\u957f\u5ea6\u4e3a 1 \u5b57\u8282\u7684\u5b57\u7b26\uff0c\u5c06\u6700\u9ad8\u4f4d\u8bbe\u7f6e\u4e3a \\(0\\) \u3001\u5176\u4f59 7 \u4f4d\u8bbe\u7f6e\u4e3a Unicode \u7801\u70b9\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0cASCII \u5b57\u7b26\u5728 Unicode \u5b57\u7b26\u96c6\u4e2d\u5360\u636e\u4e86\u524d 128 \u4e2a\u7801\u70b9\u3002\u4e5f\u5c31\u662f\u8bf4\uff0cUTF-8 \u7f16\u7801\u53ef\u4ee5\u5411\u4e0b\u517c\u5bb9 ASCII \u7801\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 UTF-8 \u6765\u89e3\u6790\u5e74\u4ee3\u4e45\u8fdc\u7684 ASCII \u7801\u6587\u672c\u3002
    • \u5bf9\u4e8e\u957f\u5ea6\u4e3a \\(n\\) \u5b57\u8282\u7684\u5b57\u7b26\uff08\u5176\u4e2d \\(n > 1\\)\uff09\uff0c\u5c06\u9996\u4e2a\u5b57\u8282\u7684\u9ad8 \\(n\\) \u4f4d\u90fd\u8bbe\u7f6e\u4e3a \\(1\\) \u3001\u7b2c \\(n + 1\\) \u4f4d\u8bbe\u7f6e\u4e3a \\(0\\) \uff1b\u4ece\u7b2c\u4e8c\u4e2a\u5b57\u8282\u5f00\u59cb\uff0c\u5c06\u6bcf\u4e2a\u5b57\u8282\u7684\u9ad8 2 \u4f4d\u90fd\u8bbe\u7f6e\u4e3a \\(10\\) \uff1b\u5176\u4f59\u6240\u6709\u4f4d\u7528\u4e8e\u586b\u5145\u5b57\u7b26\u7684 Unicode \u7801\u70b9\u3002

    \u4e0b\u56fe\u5c55\u793a\u4e86\u201cHello\u7b97\u6cd5\u201d\u5bf9\u5e94\u7684 UTF-8 \u7f16\u7801\u3002\u5c06\u6700\u9ad8 \\(n\\) \u4f4d\u8bbe\u7f6e\u4e3a \\(1\\) \u6bd4\u8f83\u5bb9\u6613\u7406\u89e3\uff0c\u53ef\u4ee5\u5411\u7cfb\u7edf\u6307\u51fa\u5b57\u7b26\u7684\u957f\u5ea6\u4e3a \\(n\\) \u3002\u90a3\u4e48\uff0c\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\uff0c\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\u662f\u56e0\u4e3a\u957f\u5ea6\u4e3a 1 \u5b57\u8282\u7684\u5b57\u7b26\u7684\u6700\u9ad8\u4e00\u4f4d\u662f \\(0\\) \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\u7684\u5224\u65ad\u51fa\u5f02\u5e38\u3002

    Fig. UTF-8 \u7f16\u7801\u793a\u4f8b

    \u9664\u4e86 UTF-8 \u4e4b\u5916\uff0c\u5e38\u89c1\u7684\u7f16\u7801\u65b9\u5f0f\u8fd8\u5305\u62ec UTF-16 \u548c UTF-32 \u3002\u5b83\u4eec\u4e3a Unicode \u5b57\u7b26\u96c6\u63d0\u4f9b\u4e86\u4e0d\u540c\u7684\u7f16\u7801\u65b9\u6cd5\u3002

    • UTF-16 \u7f16\u7801\uff1a\u4f7f\u7528 2 \u6216 4 \u4e2a\u5b57\u8282\u6765\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\u3002\u6240\u6709\u7684 ASCII \u5b57\u7b26\u548c\u5f88\u591a\u5e38\u7528\u7684\u975e\u82f1\u6587\u5b57\u7b26\uff0c\u90fd\u7528 2 \u4e2a\u5b57\u8282\u8868\u793a\uff1b\u5c11\u6570\u5b57\u7b26\u9700\u8981\u7528\u5230 4 \u4e2a\u5b57\u8282\u8868\u793a\u3002\u5bf9\u4e8e 2 \u5b57\u8282\u7684\u5b57\u7b26\uff0cUTF-16 \u7f16\u7801\u4e0e Unicode \u7801\u70b9\u76f8\u7b49\u3002
    • UTF-32 \u7f16\u7801\uff1a\u6bcf\u4e2a\u5b57\u7b26\u90fd\u4f7f\u7528 4 \u4e2a\u5b57\u8282\u3002\u8fd9\u610f\u5473\u7740 UTF-32 \u4f1a\u6bd4 UTF-8 \u548c UTF-16 \u66f4\u5360\u7528\u7a7a\u95f4\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u4e3b\u8981\u4f7f\u7528 ASCII \u5b57\u7b26\u7684\u6587\u672c\u3002

    \u4ece\u5b58\u50a8\u7a7a\u95f4\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 \u4e2a\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\u53ea\u9700\u8981 2 \u4e2a\u5b57\u8282\uff0c\u800c UTF-8 \u53ef\u80fd\u9700\u8981 3 \u4e2a\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\u90fd\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\u8fd9\u662f\u56e0\u4e3a\u5728\u7b49\u957f\u7f16\u7801\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u770b\u4f5c\u6570\u7ec4\u6765\u5904\u7406\uff0c\u5177\u4f53\u6765\u8bf4\uff1a

    • \u968f\u673a\u8bbf\u95ee: UTF-16 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u8fdb\u884c\u968f\u673a\u8bbf\u95ee\u3002UTF-8 \u662f\u4e00\u79cd\u53d8\u957f\u7f16\u7801\uff0c\u8981\u627e\u5230\u7b2c \\(i\\) \u4e2a\u5b57\u7b26\uff0c\u6211\u4eec\u9700\u8981\u4ece\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\u5904\u904d\u5386\u5230\u7b2c \\(i\\) \u4e2a\u5b57\u7b26\uff0c\u8fd9\u9700\u8981 \\(O(n)\\) \u7684\u65f6\u95f4\u3002
    • \u5b57\u7b26\u8ba1\u6570: \u4e0e\u968f\u673a\u8bbf\u95ee\u7c7b\u4f3c\uff0c\u8ba1\u7b97 UTF-16 \u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u4e5f\u662f \\(O(1)\\) \u7684\u64cd\u4f5c\u3002\u4f46\u662f\uff0c\u8ba1\u7b97 UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u9700\u8981\u904d\u5386\u6574\u4e2a\u5b57\u7b26\u4e32\u3002
    • \u5b57\u7b26\u4e32\u64cd\u4f5c: \u5728 UTF-16 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u4e2d\uff0c\u5f88\u591a\u5b57\u7b26\u4e32\u64cd\u4f5c\uff08\u5982\u5206\u5272\u3001\u8fde\u63a5\u3001\u63d2\u5165\u3001\u5220\u9664\u7b49\uff09\u90fd\u66f4\u5bb9\u6613\u8fdb\u884c\u3002\u5728 UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u4e0a\u8fdb\u884c\u8fd9\u4e9b\u64cd\u4f5c\u901a\u5e38\u9700\u8981\u989d\u5916\u7684\u8ba1\u7b97\uff0c\u4ee5\u786e\u4fdd\u4e0d\u4f1a\u4ea7\u751f\u65e0\u6548\u7684 UTF-8 \u7f16\u7801\u3002

    \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\u5230\u8bb8\u591a\u56e0\u7d20\uff1a

    • Java \u7684 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
    • JavaScript \u548c TypeScript \u7684\u5b57\u7b26\u4e32\u4f7f\u7528 UTF-16 \u7f16\u7801\u7684\u539f\u56e0\u4e0e Java \u7c7b\u4f3c\u3002\u5f53 JavaScript \u8bed\u8a00\u5728 1995 \u5e74\u88ab Netscape \u516c\u53f8\u9996\u6b21\u5f15\u5165\u65f6\uff0cUnicode \u8fd8\u5904\u4e8e\u76f8\u5bf9\u65e9\u671f\u7684\u9636\u6bb5\uff0c\u90a3\u65f6\u5019\u4f7f\u7528 16 \u4f4d\u7684\u7f16\u7801\u5c31\u8db3\u591f\u8868\u793a\u6240\u6709\u7684 Unicode \u5b57\u7b26\u4e86\u3002
    • C# \u4f7f\u7528 UTF-16 \u7f16\u7801\uff0c\u4e3b\u8981\u56e0\u4e3a .NET \u5e73\u53f0\u662f\u7531 Microsoft \u8bbe\u8ba1\u7684\uff0c\u800c Microsoft \u7684\u5f88\u591a\u6280\u672f\uff0c\u5305\u62ec Windows \u64cd\u4f5c\u7cfb\u7edf\uff0c\u90fd\u5e7f\u6cdb\u5730\u4f7f\u7528 UTF-16 \u7f16\u7801\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\u56e0\u6b64\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\u589e\u52a0\u4e86\u7f16\u7a0b\u7684\u590d\u6742\u6027\u548c Debug \u96be\u5ea6\u3002

    \u51fa\u4e8e\u4ee5\u4e0a\u539f\u56e0\uff0c\u90e8\u5206\u7f16\u7a0b\u8bed\u8a00\u63d0\u51fa\u4e86\u4e0d\u540c\u7684\u7f16\u7801\u65b9\u6848\uff1a

    • Python 3 \u4f7f\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\u5bf9\u4e8e\u5168\u90e8\u662f ASCII \u5b57\u7b26\u7684\u5b57\u7b26\u4e32\uff0c\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 1 \u4e2a\u5b57\u8282\uff1b\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u7684\u5b57\u7b26\u8d85\u51fa\u4e86 ASCII \u8303\u56f4\uff0c\u4f46\u5168\u90e8\u5728\u57fa\u672c\u591a\u8bed\u8a00\u5e73\u9762\uff08BMP\uff09\u5185\uff0c\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 2 \u4e2a\u5b57\u8282\uff1b\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u6709\u8d85\u51fa BMP \u7684\u5b57\u7b26\uff0c\u90a3\u4e48\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 4 \u4e2a\u5b57\u8282\u3002
    • Go \u8bed\u8a00\u7684 string \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\u3002
    • Rust \u8bed\u8a00\u7684 str \u548c String \u7c7b\u578b\u5728\u5185\u90e8\u4f7f\u7528 UTF-8 \u7f16\u7801\u3002Rust \u4e5f\u63d0\u4f9b\u4e86 char \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\u4e24\u4e2a\u4e0d\u540c\u7684\u95ee\u9898\u3002\u5728\u6587\u4ef6\u5b58\u50a8\u6216\u7f51\u7edc\u4f20\u8f93\u4e2d\uff0c\u6211\u4eec\u4e00\u822c\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":"

    \u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u4ece\u903b\u8f91\u7ed3\u6784\u548c\u7269\u7406\u7ed3\u6784\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":"

    \u300c\u903b\u8f91\u7ed3\u6784\u300d\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\u987a\u5e8f\u4f9d\u6b21\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\u7956\u5148\u4e0e\u540e\u4ee3\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

    \u903b\u8f91\u7ed3\u6784\u901a\u5e38\u5206\u4e3a\u300c\u7ebf\u6027\u300d\u548c\u300c\u975e\u7ebf\u6027\u300d\u4e24\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\uff0c\u4f8b\u5982\u7f51\u72b6\u6216\u6811\u72b6\u7ed3\u6784\u3002

    • \u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff1a\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\uff1b
    • \u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff1a\u6811\u3001\u56fe\u3001\u5806\u3001\u54c8\u5e0c\u8868\uff1b

    Fig. \u7ebf\u6027\u4e0e\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784

    "},{"location":"chapter_data_structure/classification_of_data_structure/#312","title":"3.1.2. \u00a0 \u7269\u7406\u7ed3\u6784\uff1a\u8fde\u7eed\u4e0e\u79bb\u6563","text":"

    \u5728\u8ba1\u7b97\u673a\u4e2d\uff0c\u5185\u5b58\u548c\u786c\u76d8\u662f\u4e24\u79cd\u4e3b\u8981\u7684\u5b58\u50a8\u786c\u4ef6\u8bbe\u5907\u3002\u300c\u786c\u76d8\u300d\u4e3b\u8981\u7528\u4e8e\u957f\u671f\u5b58\u50a8\u6570\u636e\uff0c\u5bb9\u91cf\u8f83\u5927\uff08\u901a\u5e38\u53ef\u8fbe\u5230 TB \u7ea7\u522b\uff09\u3001\u901f\u5ea6\u8f83\u6162\u3002\u300c\u5185\u5b58\u300d\u7528\u4e8e\u8fd0\u884c\u7a0b\u5e8f\u65f6\u6682\u5b58\u6570\u636e\uff0c\u901f\u5ea6\u8f83\u5feb\uff0c\u4f46\u5bb9\u91cf\u8f83\u5c0f\uff08\u901a\u5e38\u4e3a GB \u7ea7\u522b\uff09\u3002

    \u5728\u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\uff0c\u76f8\u5173\u6570\u636e\u90fd\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\u3002\u4e0b\u56fe\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 1 byte \u7684\u6570\u636e\uff0c\u5728\u7b97\u6cd5\u8fd0\u884c\u65f6\uff0c\u6240\u6709\u6570\u636e\u90fd\u88ab\u5b58\u50a8\u5728\u8fd9\u4e9b\u5355\u5143\u683c\u4e2d\u3002

    \u7cfb\u7edf\u901a\u8fc7\u300c\u5185\u5b58\u5730\u5740 Memory Location\u300d\u6765\u8bbf\u95ee\u76ee\u6807\u5185\u5b58\u4f4d\u7f6e\u7684\u6570\u636e\u3002\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

    Fig. \u5185\u5b58\u6761\u3001\u5185\u5b58\u7a7a\u95f4\u3001\u5185\u5b58\u5730\u5740

    \u5185\u5b58\u662f\u6240\u6709\u7a0b\u5e8f\u7684\u5171\u4eab\u8d44\u6e90\uff0c\u5f53\u5185\u5b58\u88ab\u67d0\u4e2a\u7a0b\u5e8f\u5360\u7528\u65f6\uff0c\u5176\u4ed6\u7a0b\u5e8f\u65e0\u6cd5\u540c\u65f6\u4f7f\u7528\u3002\u56e0\u6b64\uff0c\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\u4f8b\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\u8fd0\u884c\u7684\u7a0b\u5e8f\u5f88\u591a\u5e76\u4e14\u7f3a\u5c11\u5927\u91cf\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u90a3\u4e48\u6240\u9009\u7528\u7684\u6570\u636e\u7ed3\u6784\u5fc5\u987b\u80fd\u591f\u5b58\u50a8\u5728\u79bb\u6563\u7684\u5185\u5b58\u7a7a\u95f4\u5185\u3002

    \u300c\u7269\u7406\u7ed3\u6784\u300d\u53cd\u6620\u4e86\u6570\u636e\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u53ef\u5206\u4e3a\u6570\u7ec4\u7684\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u548c\u94fe\u8868\u7684\u79bb\u6563\u7a7a\u95f4\u5b58\u50a8\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\u540c\u65f6\u5728\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u65b9\u9762\u5448\u73b0\u51fa\u4e92\u8865\u7684\u7279\u70b9\u3002

    Fig. \u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u4e0e\u79bb\u6563\u7a7a\u95f4\u5b58\u50a8

    \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\u53ef\u5b9e\u73b0\uff1a\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\u3001\u77e9\u9635\u3001\u5f20\u91cf\uff08\u7ef4\u5ea6 \\(\\geq 3\\) \u7684\u6570\u7ec4\uff09\u7b49\uff1b
    • \u57fa\u4e8e\u94fe\u8868\u53ef\u5b9e\u73b0\uff1a\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\u7b49\uff1b

    \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u4e5f\u88ab\u79f0\u4e3a\u300c\u9759\u6001\u6570\u636e\u7ed3\u6784\u300d\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\u88ab\u79f0\u4e3a\u300c\u52a8\u6001\u6570\u636e\u7ed3\u6784\u300d\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\u82e5\u611f\u89c9\u7406\u89e3\u7269\u7406\u7ed3\u6784\u6709\u56f0\u96be\uff0c\u5efa\u8bae\u5148\u9605\u8bfb\u4e0b\u4e00\u7ae0\u201c\u6570\u7ec4\u4e0e\u94fe\u8868\u201d\uff0c\u7136\u540e\u518d\u56de\u5934\u7406\u89e3\u7269\u7406\u7ed3\u6784\u7684\u542b\u4e49\u3002\u6570\u7ec4\u4e0e\u94fe\u8868\u662f\u5176\u4ed6\u6240\u6709\u6570\u636e\u7ed3\u6784\u7684\u57fa\u77f3\uff0c\u5efa\u8bae\u4f60\u6295\u5165\u66f4\u591a\u65f6\u95f4\u6df1\u5165\u4e86\u89e3\u8fd9\u4e24\u79cd\u57fa\u672c\u6570\u636e\u7ed3\u6784\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\u7684 * \u7b26\u53f7\u7684\u662f\u9009\u8bfb\u7ae0\u8282\u3002\u5982\u679c\u4f60\u65f6\u95f4\u6709\u9650\u6216\u611f\u5230\u7406\u89e3\u56f0\u96be\uff0c\u5efa\u8bae\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 \u539f\u7801\u3001\u53cd\u7801\u548c\u8865\u7801","text":"

    \u4ece\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\u3002\u4f8b\u5982\uff0cbyte \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\u5230\u539f\u7801\u3001\u53cd\u7801\u3001\u8865\u7801\u7684\u76f8\u5173\u77e5\u8bc6\u3002\u5728\u5c55\u5f00\u5206\u6790\u4e4b\u524d\uff0c\u6211\u4eec\u9996\u5148\u7ed9\u51fa\u4e09\u8005\u7684\u5b9a\u4e49\uff1a

    • \u539f\u7801\uff1a\u6211\u4eec\u5c06\u6570\u5b57\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u7684\u6700\u9ad8\u4f4d\u89c6\u4e3a\u7b26\u53f7\u4f4d\uff0c\u5176\u4e2d \\(0\\) \u8868\u793a\u6b63\u6570\uff0c\\(1\\) \u8868\u793a\u8d1f\u6570\uff0c\u5176\u4f59\u4f4d\u8868\u793a\u6570\u5b57\u7684\u503c\u3002
    • \u53cd\u7801\uff1a\u6b63\u6570\u7684\u53cd\u7801\u4e0e\u5176\u539f\u7801\u76f8\u540c\uff0c\u8d1f\u6570\u7684\u53cd\u7801\u662f\u5bf9\u5176\u539f\u7801\u9664\u7b26\u53f7\u4f4d\u5916\u7684\u6240\u6709\u4f4d\u53d6\u53cd\u3002
    • \u8865\u7801\uff1a\u6b63\u6570\u7684\u8865\u7801\u4e0e\u5176\u539f\u7801\u76f8\u540c\uff0c\u8d1f\u6570\u7684\u8865\u7801\u662f\u5728\u5176\u53cd\u7801\u7684\u57fa\u7840\u4e0a\u52a0 \\(1\\) \u3002

    Fig. \u539f\u7801\u3001\u53cd\u7801\u4e0e\u8865\u7801\u4e4b\u95f4\u7684\u76f8\u4e92\u8f6c\u6362

    \u663e\u7136\uff0c\u300c\u539f\u7801\u300d\u6700\u4e3a\u76f4\u89c2\uff0c\u7136\u800c\u6570\u5b57\u5374\u662f\u4ee5\u300c\u8865\u7801\u300d\u7684\u5f62\u5f0f\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u4e2d\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u539f\u7801\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\uff0c\u6211\u4eec\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 & = 0000 \\space 0001 + 1000 \\space 0010 \\newline & = 1000 \\space 0011 \\newline & = -3 \\end{aligned} \\]

    \u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u8ba1\u7b97\u673a\u5f15\u5165\u4e86\u300c\u53cd\u7801\u300d\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u5148\u5c06\u539f\u7801\u8f6c\u6362\u4e3a\u53cd\u7801\uff0c\u5e76\u5728\u53cd\u7801\u4e0b\u8ba1\u7b97 \\(1 + (-2)\\) \uff0c\u5e76\u5c06\u7ed3\u679c\u4ece\u53cd\u7801\u8f6c\u5316\u56de\u539f\u7801\uff0c\u5219\u53ef\u5f97\u5230\u6b63\u786e\u7ed3\u679c \\(-1\\) \u3002

    \\[ \\begin{aligned} & 1 + (-2) \\newline & = 0000 \\space 0001 \\space \\text{(\u539f\u7801)} + 1000 \\space 0010 \\space \\text{(\u539f\u7801)} \\newline & = 0000 \\space 0001 \\space \\text{(\u53cd\u7801)} + 1111 \\space 1101 \\space \\text{(\u53cd\u7801)} \\newline & = 1111 \\space 1110 \\space \\text{(\u53cd\u7801)} \\newline & = 1000 \\space 0001 \\space \\text{(\u539f\u7801)} \\newline & = -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\u7740\u4e24\u4e2a\u4e0d\u540c\u7684\u4e8c\u8fdb\u5236\u7f16\u7801\uff0c\u800c\u8fd9\u53ef\u80fd\u4f1a\u5e26\u6765\u6b67\u4e49\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u5728\u6761\u4ef6\u5224\u65ad\u4e2d\uff0c\u5982\u679c\u6ca1\u6709\u533a\u5206\u6b63\u96f6\u548c\u8d1f\u96f6\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u9519\u8bef\u7684\u5224\u65ad\u7ed3\u679c\u3002\u5982\u679c\u6211\u4eec\u60f3\u8981\u5904\u7406\u6b63\u96f6\u548c\u8d1f\u96f6\u6b67\u4e49\uff0c\u5219\u9700\u8981\u5f15\u5165\u989d\u5916\u7684\u5224\u65ad\u64cd\u4f5c\uff0c\u5176\u53ef\u80fd\u4f1a\u964d\u4f4e\u8ba1\u7b97\u673a\u7684\u8fd0\u7b97\u6548\u7387\u3002

    \\[ \\begin{aligned} +0 & = 0000 \\space 0000 \\newline -0 & = 1000 \\space 0000 \\end{aligned} \\]

    \u4e0e\u539f\u7801\u4e00\u6837\uff0c\u53cd\u7801\u4e5f\u5b58\u5728\u6b63\u8d1f\u96f6\u6b67\u4e49\u95ee\u9898\u3002\u4e3a\u6b64\uff0c\u8ba1\u7b97\u673a\u8fdb\u4e00\u6b65\u5f15\u5165\u4e86\u300c\u8865\u7801\u300d\u3002\u90a3\u4e48\uff0c\u8865\u7801\u6709\u4ec0\u4e48\u4f5c\u7528\u5462\uff1f\u6211\u4eec\u5148\u6765\u5206\u6790\u4e00\u4e0b\u8d1f\u96f6\u7684\u8865\u7801\u7684\u8ba1\u7b97\u8fc7\u7a0b\uff1a

    \\[ \\begin{aligned} -0 = \\space & 1000 \\space 0000 \\space \\text{(\u539f\u7801)} \\newline = \\space & 1111 \\space 1111 \\space \\text{(\u53cd\u7801)} \\newline = 1 \\space & 0000 \\space 0000 \\space \\text{(\u8865\u7801)} \\newline \\end{aligned} \\]

    \u5728\u8d1f\u96f6\u7684\u53cd\u7801\u57fa\u7840\u4e0a\u52a0 \\(1\\) \u4f1a\u4ea7\u751f\u8fdb\u4f4d\uff0c\u800c\u7531\u4e8e byte \u7684\u957f\u5ea6\u53ea\u6709 8 \u4f4d\uff0c\u56e0\u6b64\u6ea2\u51fa\u5230\u7b2c 9 \u4f4d\u7684 \\(1\\) \u4f1a\u88ab\u820d\u5f03\u3002\u4ece\u800c\u5f97\u5230\u8d1f\u96f6\u7684\u8865\u7801\u4e3a \\(0000 \\space 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\u4ece\u800c\u89e3\u51b3\u4e86\u6b63\u8d1f\u96f6\u6b67\u4e49\u95ee\u9898\u3002

    \u8fd8\u5269\u4f59\u6700\u540e\u4e00\u4e2a\u7591\u60d1\uff1abyte \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\u662f\u53ef\u4ee5\u4e92\u76f8\u8f6c\u6362\u7684\u3002

    \u7136\u800c\uff0c\u8865\u7801 \\(1000 \\space 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 \\space 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 \\space 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 & = 1111 \\space 1111 \\space \\text{(\u539f\u7801)} + 1000 \\space 0001 \\space \\text{(\u539f\u7801)} \\newline & = 1000 \\space 0000 \\space \\text{(\u53cd\u7801)} + 1111 \\space 1110 \\space \\text{(\u53cd\u7801)} \\newline & = 1000 \\space 0001 \\space \\text{(\u8865\u7801)} + 1111 \\space 1111 \\space \\text{(\u8865\u7801)} \\newline & = 1000 \\space 0000 \\space \\text{(\u8865\u7801)} \\newline & = -128 \\end{aligned} \\]

    \u4f60\u53ef\u80fd\u5df2\u7ecf\u53d1\u73b0\uff0c\u4e0a\u8ff0\u7684\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\u4ece\u800c\u63d0\u9ad8\u8fd0\u7b97\u901f\u5ea6\u3002

    \u7136\u800c\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\uff0c\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\u9700\u7279\u522b\u5904\u7406\u6b63\u8d1f\u96f6\u7684\u6b67\u4e49\u95ee\u9898\u3002\u8fd9\u5927\u5927\u7b80\u5316\u4e86\u786c\u4ef6\u8bbe\u8ba1\uff0c\u5e76\u63d0\u9ad8\u4e86\u8fd0\u7b97\u6548\u7387\u3002

    \u8865\u7801\u7684\u8bbe\u8ba1\u975e\u5e38\u7cbe\u5999\uff0c\u7531\u4e8e\u7bc7\u5e45\u5173\u7cfb\u6211\u4eec\u5148\u4ecb\u7ecd\u5230\u8fd9\u91cc\u3002\u5efa\u8bae\u6709\u5174\u8da3\u7684\u8bfb\u8005\u8fdb\u4e00\u6b65\u6df1\u5ea6\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\u6839\u636e IEEE 754 \u6807\u51c6\uff0c32-bit \u957f\u5ea6\u7684 float \u7531\u4ee5\u4e0b\u90e8\u5206\u6784\u6210\uff1a

    • \u7b26\u53f7\u4f4d \\(\\mathrm{S}\\) \uff1a\u5360 1 bit \uff1b
    • \u6307\u6570\u4f4d \\(\\mathrm{E}\\) \uff1a\u5360 8 bits \uff1b
    • \u5206\u6570\u4f4d \\(\\mathrm{N}\\) \uff1a\u5360 24 bits \uff0c\u5176\u4e2d 23 \u4f4d\u663e\u5f0f\u5b58\u50a8\uff1b

    \u8bbe 32-bit \u4e8c\u8fdb\u5236\u6570\u7684\u7b2c \\(i\\) \u4f4d\u4e3a \\(b_i\\) \uff0c\u5219 float \u503c\u7684\u8ba1\u7b97\u65b9\u6cd5\u5b9a\u4e49\u4e3a\uff1a

    \\[ \\text { val } = (-1)^{b_{31}} \\times 2^{\\left(b_{30} b_{29} \\ldots b_{23}\\right)_2-127} \\times\\left(1 . b_{22} b_{21} \\ldots b_0\\right)_2 \\]

    \u8f6c\u5316\u5230\u5341\u8fdb\u5236\u4e0b\u7684\u8ba1\u7b97\u516c\u5f0f\u4e3a

    \\[ \\text { val }=(-1)^{\\mathrm{S}} \\times 2^{\\mathrm{E} -127} \\times (1 + \\mathrm{N}) \\]

    \u5176\u4e2d\u5404\u9879\u7684\u53d6\u503c\u8303\u56f4\u4e3a

    \\[ \\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} \\]

    Fig. IEEE 754 \u6807\u51c6\u4e0b\u7684 float \u8868\u793a\u65b9\u5f0f

    \u4ee5\u4e0a\u56fe\u4e3a\u4f8b\uff0c\\(\\mathrm{S} = 0\\) \uff0c \\(\\mathrm{E} = 124\\) \uff0c\\(\\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\\) \uff0c\u6613\u5f97

    \\[ \\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

    \u8fdb\u4e00\u6b65\u5730\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

    \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}\\)

    \u7279\u522b\u5730\uff0c\u6b21\u6b63\u89c4\u6570\u663e\u8457\u63d0\u5347\u4e86\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\uff0c\u8fd9\u662f\u56e0\u4e3a\uff1a

    • \u6700\u5c0f\u6b63\u6b63\u89c4\u6570\u4e3a \\(2^{-126} \\approx 1.18 \\times 10^{-38}\\) \uff1b
    • \u6700\u5c0f\u6b63\u6b21\u6b63\u89c4\u6570\u4e3a \\(2^{-126} \\times 2^{-23} \\approx 1.4 \\times 10^{-45}\\) \uff1b

    \u53cc\u7cbe\u5ea6 double \u4e5f\u91c7\u7528\u7c7b\u4f3c float \u7684\u8868\u793a\u65b9\u6cd5\uff0c\u6b64\u5904\u4e0d\u518d\u8be6\u8ff0\u3002

    "},{"location":"chapter_data_structure/summary/","title":"3.5. \u00a0 \u5c0f\u7ed3","text":"

    \u6570\u636e\u7ed3\u6784\u5206\u7c7b

    • \u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u4ece\u903b\u8f91\u7ed3\u6784\u548c\u7269\u7406\u7ed3\u6784\u4e24\u4e2a\u89d2\u5ea6\u8fdb\u884c\u5206\u7c7b\u3002\u903b\u8f91\u7ed3\u6784\u63cf\u8ff0\u4e86\u6570\u636e\u5143\u7d20\u4e4b\u95f4\u7684\u903b\u8f91\u5173\u7cfb\uff0c\u800c\u7269\u7406\u7ed3\u6784\u63cf\u8ff0\u4e86\u6570\u636e\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u5b58\u50a8\u65b9\u5f0f\u3002
    • \u5e38\u89c1\u7684\u903b\u8f91\u7ed3\u6784\u5305\u62ec\u7ebf\u6027\u3001\u6811\u72b6\u548c\u7f51\u72b6\u7b49\u3002\u901a\u5e38\u6211\u4eec\u6839\u636e\u903b\u8f91\u7ed3\u6784\u5c06\u6570\u636e\u7ed3\u6784\u5206\u4e3a\u7ebf\u6027\uff08\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\uff09\u548c\u975e\u7ebf\u6027\uff08\u6811\u3001\u56fe\u3001\u5806\uff09\u4e24\u79cd\u3002\u54c8\u5e0c\u8868\u7684\u5b9e\u73b0\u53ef\u80fd\u540c\u65f6\u5305\u542b\u7ebf\u6027\u548c\u975e\u7ebf\u6027\u7ed3\u6784\u3002
    • \u5f53\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u6570\u636e\u88ab\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u3002\u6bcf\u4e2a\u5185\u5b58\u7a7a\u95f4\u90fd\u62e5\u6709\u5bf9\u5e94\u7684\u5185\u5b58\u5730\u5740\uff0c\u7a0b\u5e8f\u901a\u8fc7\u8fd9\u4e9b\u5185\u5b58\u5730\u5740\u8bbf\u95ee\u6570\u636e\u3002
    • \u7269\u7406\u7ed3\u6784\u4e3b\u8981\u5206\u4e3a\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\uff08\u6570\u7ec4\uff09\u548c\u79bb\u6563\u7a7a\u95f4\u5b58\u50a8\uff08\u94fe\u8868\uff09\u3002\u6240\u6709\u6570\u636e\u7ed3\u6784\u90fd\u662f\u7531\u6570\u7ec4\u3001\u94fe\u8868\u6216\u4e24\u8005\u7684\u7ec4\u5408\u5b9e\u73b0\u7684\u3002

    \u6570\u636e\u7c7b\u578b\u4e0e\u7f16\u7801

    • \u8ba1\u7b97\u673a\u4e2d\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u5305\u62ec\u6574\u6570 byte, short, int, long \u3001\u6d6e\u70b9\u6570 float, double \u3001\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
    • \u539f\u7801\u3001\u53cd\u7801\u548c\u8865\u7801\u662f\u5728\u8ba1\u7b97\u673a\u4e2d\u7f16\u7801\u6570\u5b57\u7684\u4e09\u79cd\u65b9\u6cd5\uff0c\u5b83\u4eec\u4e4b\u95f4\u662f\u53ef\u4ee5\u76f8\u4e92\u8f6c\u6362\u7684\u3002\u6574\u6570\u7684\u539f\u7801\u7684\u6700\u9ad8\u4f4d\u662f\u7b26\u53f7\u4f4d\uff0c\u5176\u4f59\u4f4d\u662f\u6570\u5b57\u7684\u503c\u3002
    • \u6574\u6570\u5728\u8ba1\u7b97\u673a\u4e2d\u662f\u4ee5\u8865\u7801\u7684\u5f62\u5f0f\u5b58\u50a8\u7684\u3002\u5728\u8865\u7801\u8868\u793a\u4e0b\uff0c\u8ba1\u7b97\u673a\u53ef\u4ee5\u5bf9\u6b63\u6570\u548c\u8d1f\u6570\u7684\u52a0\u6cd5\u4e00\u89c6\u540c\u4ec1\uff0c\u4e0d\u9700\u8981\u4e3a\u51cf\u6cd5\u64cd\u4f5c\u5355\u72ec\u8bbe\u8ba1\u7279\u6b8a\u7684\u786c\u4ef6\u7535\u8def\uff0c\u5e76\u4e14\u4e0d\u5b58\u5728\u6b63\u8d1f\u96f6\u6b67\u4e49\u7684\u95ee\u9898\u3002
    • \u6d6e\u70b9\u6570\u7684\u7f16\u7801\u7531 1 \u4f4d\u7b26\u53f7\u4f4d\u30018 \u4f4d\u6307\u6570\u4f4d\u548c 23 \u4f4d\u5206\u6570\u4f4d\u6784\u6210\u3002\u7531\u4e8e\u5b58\u5728\u6307\u6570\u4f4d\uff0c\u6d6e\u70b9\u6570\u7684\u53d6\u503c\u8303\u56f4\u8fdc\u5927\u4e8e\u6574\u6570\uff0c\u4ee3\u4ef7\u662f\u727a\u7272\u4e86\u7cbe\u5ea6\u3002
    • ASCII \u7801\u662f\u6700\u65e9\u51fa\u73b0\u7684\u82f1\u6587\u5b57\u7b26\u96c6\uff0c\u957f\u5ea6\u4e3a 1 \u5b57\u8282\uff0c\u5171\u6536\u5f55 127 \u4e2a\u5b57\u7b26\u3002GBK \u5b57\u7b26\u96c6\u662f\u5e38\u7528\u7684\u4e2d\u6587\u5b57\u7b26\u96c6\uff0c\u5171\u6536\u5f55\u4e24\u4e07\u591a\u4e2a\u6c49\u5b57\u3002Unicode \u81f4\u529b\u4e8e\u63d0\u4f9b\u4e00\u4e2a\u5b8c\u6574\u7684\u5b57\u7b26\u96c6\u6807\u51c6\uff0c\u6536\u5f55\u4e16\u754c\u5185\u5404\u79cd\u8bed\u8a00\u7684\u5b57\u7b26\uff0c\u4ece\u800c\u89e3\u51b3\u7531\u4e8e\u5b57\u7b26\u7f16\u7801\u65b9\u6cd5\u4e0d\u4e00\u81f4\u800c\u5bfc\u81f4\u7684\u4e71\u7801\u95ee\u9898\u3002
    • UTF-8 \u662f\u6700\u53d7\u6b22\u8fce\u7684 Unicode \u7f16\u7801\u65b9\u6cd5\uff0c\u901a\u7528\u6027\u975e\u5e38\u597d\u3002\u5b83\u662f\u4e00\u79cd\u53d8\u957f\u7684\u7f16\u7801\u65b9\u6cd5\uff0c\u5177\u6709\u5f88\u597d\u7684\u6269\u5c55\u6027\uff0c\u6709\u6548\u63d0\u5347\u4e86\u5b58\u50a8\u7a7a\u95f4\u7684\u4f7f\u7528\u6548\u7387\u3002UTF-16 \u548c UTF-32 \u662f\u7b49\u957f\u7684\u7f16\u7801\u65b9\u6cd5\u3002\u5728\u7f16\u7801\u4e2d\u6587\u65f6\uff0cUTF-16 \u6bd4 UTF-8 \u7684\u5360\u7528\u7a7a\u95f4\u66f4\u5c0f\u3002Java, C# \u7b49\u7f16\u7a0b\u8bed\u8a00\u9ed8\u8ba4\u4f7f\u7528 UTF-16 \u7f16\u7801\u3002
    "},{"location":"chapter_data_structure/summary/#351-q-a","title":"3.5.1. \u00a0 Q & A","text":"

    \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\u62c9\u94fe\u6cd5\u201d\uff08\u540e\u7eed\u6563\u5217\u8868\u7ae0\u8282\u4f1a\u8bb2\uff09\u3002\u5728\u62c9\u94fe\u6cd5\u4e2d\uff0c\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5730\u5740\uff08\u6876\uff09\u6307\u5411\u4e00\u4e2a\u94fe\u8868\uff1b\u5f53\u8fd9\u4e2a\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\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 bytes \u5417\uff1f

    \u8fd9\u4e2a\u4e0e\u7f16\u7a0b\u8bed\u8a00\u91c7\u7528\u7684\u7f16\u7801\u65b9\u6cd5\u6709\u5173\u3002\u4f8b\u5982\uff0cJava, JS, TS, C# \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

    "},{"location":"chapter_graph/","title":"9. \u00a0 \u56fe","text":""},{"location":"chapter_graph/graph/","title":"9.1. \u00a0 \u56fe","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} \\]

    Fig. \u94fe\u8868\u3001\u6811\u3001\u56fe\u4e4b\u95f4\u7684\u5173\u7cfb

    \u90a3\u4e48\uff0c\u56fe\u4e0e\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u7684\u5173\u7cfb\u662f\u4ec0\u4e48\uff1f\u5982\u679c\u6211\u4eec\u628a\u300c\u9876\u70b9\u300d\u770b\u4f5c\u8282\u70b9\uff0c\u628a\u300c\u8fb9\u300d\u770b\u4f5c\u8fde\u63a5\u5404\u4e2a\u8282\u70b9\u7684\u6307\u9488\uff0c\u5219\u53ef\u5c06\u300c\u56fe\u300d\u770b\u4f5c\u662f\u4e00\u79cd\u4ece\u300c\u94fe\u8868\u300d\u62d3\u5c55\u800c\u6765\u7684\u6570\u636e\u7ed3\u6784\u3002\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\u4ece\u800c\u66f4\u4e3a\u590d\u6742\u3002

    "},{"location":"chapter_graph/graph/#911","title":"9.1.1. \u00a0 \u56fe\u5e38\u89c1\u7c7b\u578b","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\u3002

    • \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u8fb9\u8868\u793a\u4e24\u9876\u70b9\u4e4b\u95f4\u7684\u201c\u53cc\u5411\u201d\u8fde\u63a5\u5173\u7cfb\uff0c\u4f8b\u5982\u5fae\u4fe1\u6216 QQ \u4e2d\u7684\u201c\u597d\u53cb\u5173\u7cfb\u201d\uff1b
    • \u5728\u6709\u5411\u56fe\u4e2d\uff0c\u8fb9\u5177\u6709\u65b9\u5411\u6027\uff0c\u5373 \\(A \\rightarrow B\\) \u548c \\(A \\leftarrow B\\) \u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u662f\u76f8\u4e92\u72ec\u7acb\u7684\uff0c\u4f8b\u5982\u5fae\u535a\u6216\u6296\u97f3\u4e0a\u7684\u201c\u5173\u6ce8\u201d\u4e0e\u201c\u88ab\u5173\u6ce8\u201d\u5173\u7cfb\uff1b

    Fig. \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\u3002

    • \u5bf9\u4e8e\u8fde\u901a\u56fe\uff0c\u4ece\u67d0\u4e2a\u9876\u70b9\u51fa\u53d1\uff0c\u53ef\u4ee5\u5230\u8fbe\u5176\u4f59\u4efb\u610f\u9876\u70b9\uff1b
    • \u5bf9\u4e8e\u975e\u8fde\u901a\u56fe\uff0c\u4ece\u67d0\u4e2a\u9876\u70b9\u51fa\u53d1\uff0c\u81f3\u5c11\u6709\u4e00\u4e2a\u9876\u70b9\u65e0\u6cd5\u5230\u8fbe\uff1b

    Fig. \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\u300c\u6709\u6743\u56fe Weighted Graph\u300d\u3002\u4f8b\u5982\uff0c\u5728\u738b\u8005\u8363\u8000\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

    Fig. \u6709\u6743\u56fe\u4e0e\u65e0\u6743\u56fe

    "},{"location":"chapter_graph/graph/#912","title":"9.1.2. \u00a0 \u56fe\u5e38\u7528\u672f\u8bed","text":"
    • \u300c\u90bb\u63a5 Adjacency\u300d\uff1a\u5f53\u4e24\u9876\u70b9\u4e4b\u95f4\u5b58\u5728\u8fb9\u76f8\u8fde\u65f6\uff0c\u79f0\u8fd9\u4e24\u9876\u70b9\u201c\u90bb\u63a5\u201d\u3002\u5728\u4e0a\u56fe\u4e2d\uff0c\u9876\u70b9 1 \u7684\u90bb\u63a5\u9876\u70b9\u4e3a\u9876\u70b9 2\u30013\u30015\u3002
    • \u300c\u8def\u5f84 Path\u300d\uff1a\u4ece\u9876\u70b9 A \u5230\u9876\u70b9 B \u7ecf\u8fc7\u7684\u8fb9\u6784\u6210\u7684\u5e8f\u5217\u88ab\u79f0\u4e3a\u4ece A \u5230 B \u7684\u201c\u8def\u5f84\u201d\u3002\u5728\u4e0a\u56fe\u4e2d\uff0c\u8fb9\u5e8f\u5217 1-5-2-4 \u662f\u9876\u70b9 1 \u5230\u9876\u70b9 4 \u7684\u4e00\u6761\u8def\u5f84\u3002
    • \u300c\u5ea6 Degree\u300d\u8868\u793a\u4e00\u4e2a\u9876\u70b9\u62e5\u6709\u7684\u8fb9\u6570\u3002\u5bf9\u4e8e\u6709\u5411\u56fe\uff0c\u300c\u5165\u5ea6 In-Degree\u300d\u8868\u793a\u6709\u591a\u5c11\u6761\u8fb9\u6307\u5411\u8be5\u9876\u70b9\uff0c\u300c\u51fa\u5ea6 Out-Degree\u300d\u8868\u793a\u6709\u591a\u5c11\u6761\u8fb9\u4ece\u8be5\u9876\u70b9\u6307\u51fa\u3002
    "},{"location":"chapter_graph/graph/#913","title":"9.1.3. \u00a0 \u56fe\u7684\u8868\u793a","text":"

    \u56fe\u7684\u5e38\u7528\u8868\u793a\u65b9\u6cd5\u5305\u62ec\u300c\u90bb\u63a5\u77e9\u9635\u300d\u548c\u300c\u90bb\u63a5\u8868\u300d\u3002\u4ee5\u4e0b\u4f7f\u7528\u65e0\u5411\u56fe\u8fdb\u884c\u4e3e\u4f8b\u3002

    "},{"location":"chapter_graph/graph/#_1","title":"\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\u4e0b\u56fe\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

    Fig. \u56fe\u7684\u90bb\u63a5\u77e9\u9635\u8868\u793a

    \u90bb\u63a5\u77e9\u9635\u5177\u6709\u4ee5\u4e0b\u7279\u6027\uff1a

    • \u9876\u70b9\u4e0d\u80fd\u4e0e\u81ea\u8eab\u76f8\u8fde\uff0c\u56e0\u6b64\u90bb\u63a5\u77e9\u9635\u4e3b\u5bf9\u89d2\u7ebf\u5143\u7d20\u6ca1\u6709\u610f\u4e49\u3002
    • \u5bf9\u4e8e\u65e0\u5411\u56fe\uff0c\u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u7b49\u4ef7\uff0c\u6b64\u65f6\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\u3002
    • \u5c06\u90bb\u63a5\u77e9\u9635\u7684\u5143\u7d20\u4ece \\(1\\) , \\(0\\) \u66ff\u6362\u4e3a\u6743\u91cd\uff0c\u5219\u53ef\u8868\u793a\u6709\u6743\u56fe\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\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":"\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\\) \u6761\u94fe\u8868\u5bf9\u5e94\u9876\u70b9 \\(i\\) \uff0c\u5176\u4e2d\u5b58\u50a8\u4e86\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff08\u5373\u4e0e\u8be5\u9876\u70b9\u76f8\u8fde\u7684\u9876\u70b9\uff09\u3002

    Fig. \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\u4e0a\u56fe\u53ef\u53d1\u73b0\uff0c\u90bb\u63a5\u8868\u7ed3\u6784\u4e0e\u54c8\u5e0c\u8868\u4e2d\u7684\u300c\u94fe\u5730\u5740\u6cd5\u300d\u975e\u5e38\u76f8\u4f3c\uff0c\u56e0\u6b64\u6211\u4eec\u4e5f\u53ef\u4ee5\u91c7\u7528\u7c7b\u4f3c\u65b9\u6cd5\u6765\u4f18\u5316\u6548\u7387\u3002\u4f8b\u5982\uff0c\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)\\) \uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4e2d\u5e8f\u904d\u5386\u83b7\u53d6\u6709\u5e8f\u5e8f\u5217\uff1b\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u54c8\u5e0c\u8868\uff0c\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u964d\u4f4e\u81f3 \\(O(1)\\) \u3002

    "},{"location":"chapter_graph/graph/#914","title":"9.1.4. \u00a0 \u56fe\u5e38\u89c1\u5e94\u7528","text":"

    \u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8bb8\u591a\u7cfb\u7edf\u90fd\u53ef\u4ee5\u7528\u56fe\u6765\u5efa\u6a21\uff0c\u76f8\u5e94\u7684\u5f85\u6c42\u89e3\u95ee\u9898\u4e5f\u53ef\u4ee5\u7ea6\u5316\u4e3a\u56fe\u8ba1\u7b97\u95ee\u9898\u3002

    \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\u300c\u8fb9\u300d\u7684\u64cd\u4f5c\u548c\u5bf9\u300c\u9876\u70b9\u300d\u7684\u64cd\u4f5c\u3002\u5728\u300c\u90bb\u63a5\u77e9\u9635\u300d\u548c\u300c\u90bb\u63a5\u8868\u300d\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\u6709\uff1a

    • \u6dfb\u52a0\u6216\u5220\u9664\u8fb9\uff1a\u76f4\u63a5\u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u4fee\u6539\u6307\u5b9a\u7684\u8fb9\u5373\u53ef\uff0c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002\u800c\u7531\u4e8e\u662f\u65e0\u5411\u56fe\uff0c\u56e0\u6b64\u9700\u8981\u540c\u65f6\u66f4\u65b0\u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u3002
    • \u6dfb\u52a0\u9876\u70b9\uff1a\u5728\u90bb\u63a5\u77e9\u9635\u7684\u5c3e\u90e8\u6dfb\u52a0\u4e00\u884c\u4e00\u5217\uff0c\u5e76\u5168\u90e8\u586b \\(0\\) \u5373\u53ef\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002
    • \u5220\u9664\u9876\u70b9\uff1a\u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u4e00\u884c\u4e00\u5217\u3002\u5f53\u5220\u9664\u9996\u884c\u9996\u5217\u65f6\u8fbe\u5230\u6700\u5dee\u60c5\u51b5\uff0c\u9700\u8981\u5c06 \\((n-1)^2\\) \u4e2a\u5143\u7d20\u201c\u5411\u5de6\u4e0a\u79fb\u52a8\u201d\uff0c\u4ece\u800c\u4f7f\u7528 \\(O(n^2)\\) \u65f6\u95f4\u3002
    • \u521d\u59cb\u5316\uff1a\u4f20\u5165 \\(n\\) \u4e2a\u9876\u70b9\uff0c\u521d\u59cb\u5316\u957f\u5ea6\u4e3a \\(n\\) \u7684\u9876\u70b9\u5217\u8868 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
    \u521d\u59cb\u5316\u90bb\u63a5\u77e9\u9635\u6dfb\u52a0\u8fb9\u5220\u9664\u8fb9\u6dfb\u52a0\u9876\u70b9\u5220\u9664\u9876\u70b9

    \u4ee5\u4e0b\u662f\u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u8868\u793a\u56fe\u7684\u5b9e\u73b0\u4ee3\u7801\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart graph_adjacency_matrix.java
    /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nList<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\nList<List<Integer>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n/* \u6784\u9020\u65b9\u6cd5 */\npublic GraphAdjMat(int[] vertices, int[][] edges) {\nthis.vertices = new ArrayList<>();\nthis.adjMat = new ArrayList<>();\n// \u6dfb\u52a0\u9876\u70b9\nfor (int val : vertices) {\naddVertex(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\nfor (int[] e : edges) {\naddEdge(e[0], e[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\npublic int size() {\nreturn vertices.size();\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npublic void addVertex(int val) {\nint n = size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nvertices.add(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nList<Integer> newRow = new ArrayList<>(n);\nfor (int j = 0; j < n; j++) {\nnewRow.add(0);\n}\nadjMat.add(newRow);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor (List<Integer> row : adjMat) {\nrow.add(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\npublic void removeVertex(int index) {\nif (index >= size())\nthrow new IndexOutOfBoundsException();\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nvertices.remove(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nadjMat.remove(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor (List<Integer> row : adjMat) {\nrow.remove(index);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\npublic void addEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\nthrow new IndexOutOfBoundsException();\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nadjMat.get(i).set(j, 1);\nadjMat.get(j).set(i, 1);\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\npublic void removeEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\nthrow new IndexOutOfBoundsException();\nadjMat.get(i).set(j, 0);\nadjMat.get(j).set(i, 0);\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\npublic void print() {\nSystem.out.print(\"\u9876\u70b9\u5217\u8868 = \");\nSystem.out.println(vertices);\nSystem.out.println(\"\u90bb\u63a5\u77e9\u9635 =\");\nPrintUtil.printMatrix(adjMat);\n}\n}\n
    graph_adjacency_matrix.cpp
    /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nvector<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\nvector<vector<int>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nGraphAdjMat(const vector<int> &vertices, const vector<vector<int>> &edges) {\n// \u6dfb\u52a0\u9876\u70b9\nfor (int val : vertices) {\naddVertex(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\nfor (const vector<int> &edge : edges) {\naddEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nint size() const {\nreturn vertices.size();\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(int val) {\nint n = size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nvertices.push_back(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nadjMat.emplace_back(n, 0);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor (vector<int> &row : adjMat) {\nrow.push_back(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(int index) {\nif (index >= size()) {\nthrow out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nvertices.erase(vertices.begin() + index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nadjMat.erase(adjMat.begin() + index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor (vector<int> &row : adjMat) {\nrow.erase(row.begin() + index);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid addEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\nthrow out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n}\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nadjMat[i][j] = 1;\nadjMat[j][i] = 1;\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid removeEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\nthrow out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n}\nadjMat[i][j] = 0;\nadjMat[j][i] = 0;\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nvoid print() {\ncout << \"\u9876\u70b9\u5217\u8868 = \";\nprintVector(vertices);\ncout << \"\u90bb\u63a5\u77e9\u9635 =\" << endl;\nprintVectorMatrix(adjMat);\n}\n};\n
    graph_adjacency_matrix.py
    class GraphAdjMat:\n\"\"\"\u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\"\"\"\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\nvertices: list[int] = []\n# \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\nadj_mat: list[list[int]] = []\ndef __init__(self, vertices: list[int], edges: list[list[int]]) -> None:\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.vertices: list[int] = []\nself.adj_mat: list[list[int]] = []\n# \u6dfb\u52a0\u9876\u70b9\nfor val in vertices:\nself.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\nfor e in edges:\nself.add_edge(e[0], e[1])\ndef size(self) -> int:\n\"\"\"\u83b7\u53d6\u9876\u70b9\u6570\u91cf\"\"\"\nreturn len(self.vertices)\ndef add_vertex(self, val: int) -> None:\n\"\"\"\u6dfb\u52a0\u9876\u70b9\"\"\"\nn = self.size()\n# \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nself.vertices.append(val)\n# \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nnew_row = [0] * n\nself.adj_mat.append(new_row)\n# \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor row in self.adj_mat:\nrow.append(0)\ndef remove_vertex(self, index: int) -> None:\n\"\"\"\u5220\u9664\u9876\u70b9\"\"\"\nif index >= self.size():\nraise IndexError()\n# \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nself.vertices.pop(index)\n# \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nself.adj_mat.pop(index)\n# \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor row in self.adj_mat:\nrow.pop(index)\ndef add_edge(self, i: int, j: int) -> None:\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\nif i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\nraise IndexError()\n# \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nself.adj_mat[i][j] = 1\nself.adj_mat[j][i] = 1\ndef remove_edge(self, i: int, j: int) -> None:\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\nif i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\nraise IndexError()\nself.adj_mat[i][j] = 0\nself.adj_mat[j][i] = 0\ndef print(self) -> None:\n\"\"\"\u6253\u5370\u90bb\u63a5\u77e9\u9635\"\"\"\nprint(\"\u9876\u70b9\u5217\u8868 =\", self.vertices)\nprint(\"\u90bb\u63a5\u77e9\u9635 =\")\nprint_matrix(self.adj_mat)\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\nvertices []int\n// \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\nadjMat [][]int\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newGraphAdjMat(vertices []int, edges [][]int) *graphAdjMat {\n// \u6dfb\u52a0\u9876\u70b9\nn := len(vertices)\nadjMat := make([][]int, n)\nfor i := range adjMat {\nadjMat[i] = make([]int, n)\n}\n// \u521d\u59cb\u5316\u56fe\ng := &graphAdjMat{\nvertices: vertices,\nadjMat:   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\nfor i := range edges {\ng.addEdge(edges[i][0], edges[i][1])\n}\nreturn g\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nfunc (g *graphAdjMat) size() int {\nreturn len(g.vertices)\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nfunc (g *graphAdjMat) addVertex(val int) {\nn := g.size()\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\ng.vertices = append(g.vertices, val)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nnewRow := make([]int, n)\ng.adjMat = append(g.adjMat, newRow)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor i := range g.adjMat {\ng.adjMat[i] = append(g.adjMat[i], 0)\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\nfunc (g *graphAdjMat) removeVertex(index int) {\nif index >= g.size() {\nreturn\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\ng.vertices = append(g.vertices[:index], g.vertices[index+1:]...)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\ng.adjMat = append(g.adjMat[:index], g.adjMat[index+1:]...)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor i := range g.adjMat {\ng.adjMat[i] = append(g.adjMat[i][:index], g.adjMat[i][index+1:]...)\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\nif i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {\nfmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n}\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\ng.adjMat[i][j] = 1\ng.adjMat[j][i] = 1\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\nif i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {\nfmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n}\ng.adjMat[i][j] = 0\ng.adjMat[j][i] = 0\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nfunc (g *graphAdjMat) print() {\nfmt.Printf(\"\\t\u9876\u70b9\u5217\u8868 = %v\\n\", g.vertices)\nfmt.Printf(\"\\t\u90bb\u63a5\u77e9\u9635 = \\n\")\nfor i := range g.adjMat {\nfmt.Printf(\"\\t\\t\\t%v\\n\", g.adjMat[i])\n}\n}\n
    graph_adjacency_matrix.js
    /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nvertices; // \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\nadjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n/* \u6784\u9020\u51fd\u6570 */\nconstructor(vertices, edges) {\nthis.vertices = [];\nthis.adjMat = [];\n// \u6dfb\u52a0\u9876\u70b9\nfor (const val of vertices) {\nthis.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\nfor (const e of edges) {\nthis.addEdge(e[0], e[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nsize() {\nreturn this.vertices.length;\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\naddVertex(val) {\nconst n = this.size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nthis.vertices.push(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nconst newRow = [];\nfor (let j = 0; j < n; j++) {\nnewRow.push(0);\n}\nthis.adjMat.push(newRow);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor (const row of this.adjMat) {\nrow.push(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\nremoveVertex(index) {\nif (index >= this.size()) {\nthrow new RangeError('Index Out Of Bounds Exception');\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nthis.vertices.splice(index, 1);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nthis.adjMat.splice(index, 1);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor (const row of this.adjMat) {\nrow.splice(index, 1);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\naddEdge(i, j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\nthrow new RangeError('Index Out Of Bounds Exception');\n}\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nthis.adjMat[i][j] = 1;\nthis.adjMat[j][i] = 1;\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nremoveEdge(i, j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\nthrow new RangeError('Index Out Of Bounds Exception');\n}\nthis.adjMat[i][j] = 0;\nthis.adjMat[j][i] = 0;\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nprint() {\nconsole.log('\u9876\u70b9\u5217\u8868 = ', this.vertices);\nconsole.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 {\nvertices: 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\nadjMat: number[][]; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n/* \u6784\u9020\u51fd\u6570 */\nconstructor(vertices: number[], edges: number[][]) {\nthis.vertices = [];\nthis.adjMat = [];\n// \u6dfb\u52a0\u9876\u70b9\nfor (const val of vertices) {\nthis.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\nfor (const e of edges) {\nthis.addEdge(e[0], e[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nsize(): number {\nreturn this.vertices.length;\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\naddVertex(val: number): void {\nconst n: number = this.size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nthis.vertices.push(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nconst newRow: number[] = [];\nfor (let j: number = 0; j < n; j++) {\nnewRow.push(0);\n}\nthis.adjMat.push(newRow);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor (const row of this.adjMat) {\nrow.push(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\nremoveVertex(index: number): void {\nif (index >= this.size()) {\nthrow new RangeError('Index Out Of Bounds Exception');\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nthis.vertices.splice(index, 1);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nthis.adjMat.splice(index, 1);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor (const row of this.adjMat) {\nrow.splice(index, 1);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\naddEdge(i: number, j: number): void {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\nthrow new RangeError('Index Out Of Bounds Exception');\n}\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nthis.adjMat[i][j] = 1;\nthis.adjMat[j][i] = 1;\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nremoveEdge(i: number, j: number): void {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\nthrow new RangeError('Index Out Of Bounds Exception');\n}\nthis.adjMat[i][j] = 0;\nthis.adjMat[j][i] = 0;\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nprint(): void {\nconsole.log('\u9876\u70b9\u5217\u8868 = ', this.vertices);\nconsole.log('\u90bb\u63a5\u77e9\u9635 =', this.adjMat);\n}\n}\n
    graph_adjacency_matrix.c
    [class]{graphAdjMat}-[func]{}\n
    graph_adjacency_matrix.cs
    /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nList<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\nList<List<int>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n/* \u6784\u9020\u51fd\u6570 */\npublic GraphAdjMat(int[] vertices, int[][] edges) {\nthis.vertices = new List<int>();\nthis.adjMat = new List<List<int>>();\n// \u6dfb\u52a0\u9876\u70b9\nforeach (int val in vertices) {\naddVertex(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\nforeach (int[] e in edges) {\naddEdge(e[0], e[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\npublic int size() {\nreturn vertices.Count;\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npublic void addVertex(int val) {\nint n = size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nvertices.Add(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nList<int> newRow = new List<int>(n);\nfor (int j = 0; j < n; j++) {\nnewRow.Add(0);\n}\nadjMat.Add(newRow);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nforeach (List<int> row in adjMat) {\nrow.Add(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\npublic void removeVertex(int index) {\nif (index >= size())\nthrow new IndexOutOfRangeException();\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nvertices.RemoveAt(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nadjMat.RemoveAt(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nforeach (List<int> row in adjMat) {\nrow.RemoveAt(index);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\npublic void addEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\nthrow new IndexOutOfRangeException();\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nadjMat[i][j] = 1;\nadjMat[j][i] = 1;\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\npublic void removeEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\nthrow new IndexOutOfRangeException();\nadjMat[i][j] = 0;\nadjMat[j][i] = 0;\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\npublic void print() {\nConsole.Write(\"\u9876\u70b9\u5217\u8868 = \");\nPrintUtil.PrintList(vertices);\nConsole.WriteLine(\"\u90bb\u63a5\u77e9\u9635 =\");\nPrintUtil.PrintMatrix(adjMat);\n}\n}\n
    graph_adjacency_matrix.swift
    /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nprivate 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\nprivate var adjMat: [[Int]] // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n/* \u6784\u9020\u65b9\u6cd5 */\ninit(vertices: [Int], edges: [[Int]]) {\nself.vertices = []\nadjMat = []\n// \u6dfb\u52a0\u9876\u70b9\nfor val in vertices {\naddVertex(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\nfor e in edges {\naddEdge(i: e[0], j: e[1])\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nfunc size() -> Int {\nvertices.count\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nfunc addVertex(val: Int) {\nlet n = size()\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nvertices.append(val)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nlet newRow = Array(repeating: 0, count: n)\nadjMat.append(newRow)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor i in adjMat.indices {\nadjMat[i].append(0)\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\nfunc removeVertex(index: Int) {\nif index >= size() {\nfatalError(\"\u8d8a\u754c\")\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nvertices.remove(at: index)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nadjMat.remove(at: index)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor i in adjMat.indices {\nadjMat[i].remove(at: index)\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfunc addEdge(i: Int, j: Int) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif i < 0 || j < 0 || i >= size() || j >= size() || i == j {\nfatalError(\"\u8d8a\u754c\")\n}\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nadjMat[i][j] = 1\nadjMat[j][i] = 1\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfunc removeEdge(i: Int, j: Int) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif i < 0 || j < 0 || i >= size() || j >= size() || i == j {\nfatalError(\"\u8d8a\u754c\")\n}\nadjMat[i][j] = 0\nadjMat[j][i] = 0\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nfunc print() {\nSwift.print(\"\u9876\u70b9\u5217\u8868 = \", terminator: \"\")\nSwift.print(vertices)\nSwift.print(\"\u90bb\u63a5\u77e9\u9635 =\")\nPrintUtil.printMatrix(matrix: adjMat)\n}\n}\n
    graph_adjacency_matrix.zig
    \n
    graph_adjacency_matrix.dart
    /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nList<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\nList<List<int>> adjMat = []; //\u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n/* \u6784\u9020\u65b9\u6cd5 */\nGraphAdjMat(List<int> vertices, List<List<int>> edges) {\nthis.vertices = [];\nthis.adjMat = [];\n// \u6dfb\u52a0\u9876\u70b9\nfor (int val in vertices) {\naddVertex(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\nfor (List<int> e in edges) {\naddEdge(e[0], e[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nint size() {\nreturn vertices.length;\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(int val) {\nint n = size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nvertices.add(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nList<int> newRow = List.filled(n, 0, growable: true);\nadjMat.add(newRow);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor (List<int> row in adjMat) {\nrow.add(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(int index) {\nif (index >= size()) {\nthrow IndexError;\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nvertices.removeAt(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nadjMat.removeAt(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor (List<int> row in adjMat) {\nrow.removeAt(index);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid addEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\nthrow IndexError;\n}\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nadjMat[i][j] = 1;\nadjMat[j][i] = 1;\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid removeEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\nthrow IndexError;\n}\nadjMat[i][j] = 0;\nadjMat[j][i] = 0;\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nvoid printAdjMat() {\nprint(\"\u9876\u70b9\u5217\u8868 = $vertices\");\nprint(\"\u90bb\u63a5\u77e9\u9635 = \");\nprintMatrix(adjMat);\n}\n}\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\u6709\uff1a

    • \u6dfb\u52a0\u8fb9\uff1a\u5728\u9876\u70b9\u5bf9\u5e94\u94fe\u8868\u7684\u672b\u5c3e\u6dfb\u52a0\u8fb9\u5373\u53ef\uff0c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002\u56e0\u4e3a\u662f\u65e0\u5411\u56fe\uff0c\u6240\u4ee5\u9700\u8981\u540c\u65f6\u6dfb\u52a0\u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u3002
    • \u5220\u9664\u8fb9\uff1a\u5728\u9876\u70b9\u5bf9\u5e94\u94fe\u8868\u4e2d\u67e5\u627e\u5e76\u5220\u9664\u6307\u5b9a\u8fb9\uff0c\u4f7f\u7528 \\(O(m)\\) \u65f6\u95f4\u3002\u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u9700\u8981\u540c\u65f6\u5220\u9664\u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u3002
    • \u6dfb\u52a0\u9876\u70b9\uff1a\u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u94fe\u8868\uff0c\u5e76\u5c06\u65b0\u589e\u9876\u70b9\u4f5c\u4e3a\u94fe\u8868\u5934\u8282\u70b9\uff0c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002
    • \u5220\u9664\u9876\u70b9\uff1a\u9700\u904d\u5386\u6574\u4e2a\u90bb\u63a5\u8868\uff0c\u5220\u9664\u5305\u542b\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u8fb9\uff0c\u4f7f\u7528 \\(O(n + m)\\) \u65f6\u95f4\u3002
    • \u521d\u59cb\u5316\uff1a\u5728\u90bb\u63a5\u8868\u4e2d\u521b\u5efa \\(n\\) \u4e2a\u9876\u70b9\u548c \\(2m\\) \u6761\u8fb9\uff0c\u4f7f\u7528 \\(O(n + m)\\) \u65f6\u95f4\u3002
    \u521d\u59cb\u5316\u90bb\u63a5\u8868\u6dfb\u52a0\u8fb9\u5220\u9664\u8fb9\u6dfb\u52a0\u9876\u70b9\u5220\u9664\u9876\u70b9

    \u4ee5\u4e0b\u662f\u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u56fe\u7684\u4ee3\u7801\u793a\u4f8b\u3002\u7ec6\u5fc3\u7684\u540c\u5b66\u53ef\u80fd\u6ce8\u610f\u5230\uff0c\u6211\u4eec\u5728\u90bb\u63a5\u8868\u4e2d\u4f7f\u7528 Vertex \u8282\u70b9\u7c7b\u6765\u8868\u793a\u9876\u70b9\uff0c\u8fd9\u6837\u505a\u7684\u539f\u56e0\u6709\uff1a

    • \u5982\u679c\u6211\u4eec\u9009\u62e9\u901a\u8fc7\u9876\u70b9\u503c\u6765\u533a\u5206\u4e0d\u540c\u9876\u70b9\uff0c\u90a3\u4e48\u503c\u91cd\u590d\u7684\u9876\u70b9\u5c06\u65e0\u6cd5\u88ab\u533a\u5206\u3002
    • \u5982\u679c\u7c7b\u4f3c\u90bb\u63a5\u77e9\u9635\u90a3\u6837\uff0c\u4f7f\u7528\u9876\u70b9\u5217\u8868\u7d22\u5f15\u6765\u533a\u5206\u4e0d\u540c\u9876\u70b9\u3002\u90a3\u4e48\uff0c\u5047\u8bbe\u6211\u4eec\u60f3\u8981\u5220\u9664\u7d22\u5f15\u4e3a \\(i\\) \u7684\u9876\u70b9\uff0c\u5219\u9700\u8981\u904d\u5386\u6574\u4e2a\u90bb\u63a5\u8868\uff0c\u5c06\u5176\u4e2d \\(> i\\) \u7684\u7d22\u5f15\u5168\u90e8\u51cf \\(1\\) \uff0c\u8fd9\u6837\u64cd\u4f5c\u6548\u7387\u8f83\u4f4e\u3002
    • \u56e0\u6b64\u6211\u4eec\u8003\u8651\u5f15\u5165\u9876\u70b9\u7c7b Vertex \uff0c\u4f7f\u5f97\u6bcf\u4e2a\u9876\u70b9\u90fd\u662f\u552f\u4e00\u7684\u5bf9\u8c61\uff0c\u6b64\u65f6\u5220\u9664\u9876\u70b9\u65f6\u5c31\u65e0\u9700\u6539\u52a8\u5176\u4f59\u9876\u70b9\u4e86\u3002
    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart graph_adjacency_list.java
    /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n// \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nMap<Vertex, List<Vertex>> adjList;\n/* \u6784\u9020\u65b9\u6cd5 */\npublic GraphAdjList(Vertex[][] edges) {\nthis.adjList = new HashMap<>();\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor (Vertex[] edge : edges) {\naddVertex(edge[0]);\naddVertex(edge[1]);\naddEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\npublic int size() {\nreturn adjList.size();\n}\n/* \u6dfb\u52a0\u8fb9 */\npublic void addEdge(Vertex vet1, Vertex vet2) {\nif (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\nthrow new IllegalArgumentException();\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nadjList.get(vet1).add(vet2);\nadjList.get(vet2).add(vet1);\n}\n/* \u5220\u9664\u8fb9 */\npublic void removeEdge(Vertex vet1, Vertex vet2) {\nif (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\nthrow new IllegalArgumentException();\n// \u5220\u9664\u8fb9 vet1 - vet2\nadjList.get(vet1).remove(vet2);\nadjList.get(vet2).remove(vet1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npublic void addVertex(Vertex vet) {\nif (adjList.containsKey(vet))\nreturn;\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nadjList.put(vet, new ArrayList<>());\n}\n/* \u5220\u9664\u9876\u70b9 */\npublic void removeVertex(Vertex vet) {\nif (!adjList.containsKey(vet))\nthrow new IllegalArgumentException();\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nadjList.remove(vet);\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor (List<Vertex> list : adjList.values()) {\nlist.remove(vet);\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\npublic void print() {\nSystem.out.println(\"\u90bb\u63a5\u8868 =\");\nfor (Map.Entry<Vertex, List<Vertex>> pair : adjList.entrySet()) {\nList<Integer> tmp = new ArrayList<>();\nfor (Vertex vertex : pair.getValue())\ntmp.add(vertex.val);\nSystem.out.println(pair.getKey().val + \": \" + tmp + \",\");\n}\n}\n}\n
    graph_adjacency_list.cpp
    /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\npublic:\n// \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nunordered_map<Vertex *, vector<Vertex *>> adjList;\n/* \u5728 vector \u4e2d\u5220\u9664\u6307\u5b9a\u8282\u70b9 */\nvoid remove(vector<Vertex *> &vec, Vertex *vet) {\nfor (int i = 0; i < vec.size(); i++) {\nif (vec[i] == vet) {\nvec.erase(vec.begin() + i);\nbreak;\n}\n}\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nGraphAdjList(const vector<vector<Vertex *>> &edges) {\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor (const vector<Vertex *> &edge : edges) {\naddVertex(edge[0]);\naddVertex(edge[1]);\naddEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nint size() {\nreturn adjList.size();\n}\n/* \u6dfb\u52a0\u8fb9 */\nvoid addEdge(Vertex *vet1, Vertex *vet2) {\nif (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\nthrow invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nadjList[vet1].push_back(vet2);\nadjList[vet2].push_back(vet1);\n}\n/* \u5220\u9664\u8fb9 */\nvoid removeEdge(Vertex *vet1, Vertex *vet2) {\nif (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\nthrow invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n// \u5220\u9664\u8fb9 vet1 - vet2\nremove(adjList[vet1], vet2);\nremove(adjList[vet2], vet1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(Vertex *vet) {\nif (adjList.count(vet))\nreturn;\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nadjList[vet] = vector<Vertex *>();\n}\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(Vertex *vet) {\nif (!adjList.count(vet))\nthrow invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nadjList.erase(vet);\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor (auto &adj : adjList) {\nremove(adj.second, vet);\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\nvoid print() {\ncout << \"\u90bb\u63a5\u8868 =\" << endl;\nfor (auto &adj : adjList) {\nconst auto &key = adj.first;\nconst auto &vec = adj.second;\ncout << key->val << \": \";\nprintVector(vetsToVals(vec));\n}\n}\n};\n
    graph_adjacency_list.py
    class GraphAdjList:\n\"\"\"\u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\"\"\"\ndef __init__(self, edges: list[list[Vertex]]) -> None:\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n# \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nself.adj_list = dict[Vertex, Vertex]()\n# \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor edge in edges:\nself.add_vertex(edge[0])\nself.add_vertex(edge[1])\nself.add_edge(edge[0], edge[1])\ndef size(self) -> int:\n\"\"\"\u83b7\u53d6\u9876\u70b9\u6570\u91cf\"\"\"\nreturn len(self.adj_list)\ndef add_edge(self, vet1: Vertex, vet2: Vertex) -> None:\n\"\"\"\u6dfb\u52a0\u8fb9\"\"\"\nif vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\nraise ValueError()\n# \u6dfb\u52a0\u8fb9 vet1 - vet2\nself.adj_list[vet1].append(vet2)\nself.adj_list[vet2].append(vet1)\ndef remove_edge(self, vet1: Vertex, vet2: Vertex) -> None:\n\"\"\"\u5220\u9664\u8fb9\"\"\"\nif vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\nraise ValueError()\n# \u5220\u9664\u8fb9 vet1 - vet2\nself.adj_list[vet1].remove(vet2)\nself.adj_list[vet2].remove(vet1)\ndef add_vertex(self, vet: Vertex) -> None:\n\"\"\"\u6dfb\u52a0\u9876\u70b9\"\"\"\nif vet in self.adj_list:\nreturn\n# \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nself.adj_list[vet] = []\ndef remove_vertex(self, vet: Vertex) -> None:\n\"\"\"\u5220\u9664\u9876\u70b9\"\"\"\nif vet not in self.adj_list:\nraise ValueError()\n# \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nself.adj_list.pop(vet)\n# \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor vertex in self.adj_list:\nif vet in self.adj_list[vertex]:\nself.adj_list[vertex].remove(vet)\ndef print(self) -> None:\n\"\"\"\u6253\u5370\u90bb\u63a5\u8868\"\"\"\nprint(\"\u90bb\u63a5\u8868 =\")\nfor vertex in self.adj_list:\ntmp = [v.val for v in self.adj_list[vertex]]\nprint(f\"{vertex.val}: {tmp},\")\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: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nadjList map[Vertex][]Vertex\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newGraphAdjList(edges [][]Vertex) *graphAdjList {\ng := &graphAdjList{\nadjList: make(map[Vertex][]Vertex),\n}\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor _, edge := range edges {\ng.addVertex(edge[0])\ng.addVertex(edge[1])\ng.addEdge(edge[0], edge[1])\n}\nreturn g\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nfunc (g *graphAdjList) size() int {\nreturn len(g.adjList)\n}\n/* \u6dfb\u52a0\u8fb9 */\nfunc (g *graphAdjList) addEdge(vet1 Vertex, vet2 Vertex) {\n_, ok1 := g.adjList[vet1]\n_, ok2 := g.adjList[vet2]\nif !ok1 || !ok2 || vet1 == vet2 {\npanic(\"error\")\n}\n// \u6dfb\u52a0\u8fb9 vet1 - vet2, \u6dfb\u52a0\u533f\u540d struct{},\ng.adjList[vet1] = append(g.adjList[vet1], vet2)\ng.adjList[vet2] = append(g.adjList[vet2], vet1)\n}\n/* \u5220\u9664\u8fb9 */\nfunc (g *graphAdjList) removeEdge(vet1 Vertex, vet2 Vertex) {\n_, ok1 := g.adjList[vet1]\n_, ok2 := g.adjList[vet2]\nif !ok1 || !ok2 || vet1 == vet2 {\npanic(\"error\")\n}\n// \u5220\u9664\u8fb9 vet1 - vet2\nDeleteSliceElms(g.adjList[vet1], vet2)\nDeleteSliceElms(g.adjList[vet2], vet1)\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nfunc (g *graphAdjList) addVertex(vet Vertex) {\n_, ok := g.adjList[vet]\nif ok {\nreturn\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\ng.adjList[vet] = make([]Vertex, 0)\n}\n/* \u5220\u9664\u9876\u70b9 */\nfunc (g *graphAdjList) removeVertex(vet Vertex) {\n_, ok := g.adjList[vet]\nif !ok {\npanic(\"error\")\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\ndelete(g.adjList, vet)\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor _, list := range g.adjList {\nDeleteSliceElms(list, vet)\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\nfunc (g *graphAdjList) print() {\nvar builder strings.Builder\nfmt.Printf(\"\u90bb\u63a5\u8868 = \\n\")\nfor k, v := range g.adjList {\nbuilder.WriteString(\"\\t\\t\" + strconv.Itoa(k.Val) + \": \")\nfor _, vet := range v {\nbuilder.WriteString(strconv.Itoa(vet.Val) + \" \")\n}\nfmt.Println(builder.String())\nbuilder.Reset()\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: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nadjList;\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor(edges) {\nthis.adjList = new Map();\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor (const edge of edges) {\nthis.addVertex(edge[0]);\nthis.addVertex(edge[1]);\nthis.addEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nsize() {\nreturn this.adjList.size;\n}\n/* \u6dfb\u52a0\u8fb9 */\naddEdge(vet1, vet2) {\nif (\n!this.adjList.has(vet1) ||\n!this.adjList.has(vet2) ||\nvet1 === vet2\n) {\nthrow new Error('Illegal Argument Exception');\n}\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nthis.adjList.get(vet1).push(vet2);\nthis.adjList.get(vet2).push(vet1);\n}\n/* \u5220\u9664\u8fb9 */\nremoveEdge(vet1, vet2) {\nif (\n!this.adjList.has(vet1) ||\n!this.adjList.has(vet2) ||\nvet1 === vet2\n) {\nthrow new Error('Illegal Argument Exception');\n}\n// \u5220\u9664\u8fb9 vet1 - vet2\nthis.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\nthis.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\naddVertex(vet) {\nif (this.adjList.has(vet)) return;\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nthis.adjList.set(vet, []);\n}\n/* \u5220\u9664\u9876\u70b9 */\nremoveVertex(vet) {\nif (!this.adjList.has(vet)) {\nthrow new Error('Illegal Argument Exception');\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nthis.adjList.delete(vet);\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor (let set of this.adjList.values()) {\nconst index = set.indexOf(vet);\nif (index > -1) {\nset.splice(index, 1);\n}\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\nprint() {\nconsole.log('\u90bb\u63a5\u8868 =');\nfor (const [key, value] of this.adjList) {\nconst tmp = [];\nfor (const vertex of value) {\ntmp.push(vertex.val);\n}\nconsole.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: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nadjList: Map<Vertex, Vertex[]>;\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor(edges: Vertex[][]) {\nthis.adjList = new Map();\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor (const edge of edges) {\nthis.addVertex(edge[0]);\nthis.addVertex(edge[1]);\nthis.addEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nsize(): number {\nreturn this.adjList.size;\n}\n/* \u6dfb\u52a0\u8fb9 */\naddEdge(vet1: Vertex, vet2: Vertex): void {\nif (\n!this.adjList.has(vet1) ||\n!this.adjList.has(vet2) ||\nvet1 === vet2\n) {\nthrow new Error('Illegal Argument Exception');\n}\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nthis.adjList.get(vet1).push(vet2);\nthis.adjList.get(vet2).push(vet1);\n}\n/* \u5220\u9664\u8fb9 */\nremoveEdge(vet1: Vertex, vet2: Vertex): void {\nif (\n!this.adjList.has(vet1) ||\n!this.adjList.has(vet2) ||\nvet1 === vet2\n) {\nthrow new Error('Illegal Argument Exception');\n}\n// \u5220\u9664\u8fb9 vet1 - vet2\nthis.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\nthis.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\naddVertex(vet: Vertex): void {\nif (this.adjList.has(vet)) return;\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nthis.adjList.set(vet, []);\n}\n/* \u5220\u9664\u9876\u70b9 */\nremoveVertex(vet: Vertex): void {\nif (!this.adjList.has(vet)) {\nthrow new Error('Illegal Argument Exception');\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nthis.adjList.delete(vet);\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor (let set of this.adjList.values()) {\nconst index: number = set.indexOf(vet);\nif (index > -1) {\nset.splice(index, 1);\n}\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\nprint(): void {\nconsole.log('\u90bb\u63a5\u8868 =');\nfor (const [key, value] of this.adjList.entries()) {\nconst tmp = [];\nfor (const vertex of value) {\ntmp.push(vertex.val);\n}\nconsole.log(key.val + ': ' + tmp.join());\n}\n}\n}\n
    graph_adjacency_list.c
    [class]{graphAdjList}-[func]{}\n
    graph_adjacency_list.cs
    /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n// \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\npublic Dictionary<Vertex, List<Vertex>> adjList;\n/* \u6784\u9020\u51fd\u6570 */\npublic GraphAdjList(Vertex[][] edges) {\nthis.adjList = new Dictionary<Vertex, List<Vertex>>();\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nforeach (Vertex[] edge in edges) {\naddVertex(edge[0]);\naddVertex(edge[1]);\naddEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\npublic int size() {\nreturn adjList.Count;\n}\n/* \u6dfb\u52a0\u8fb9 */\npublic void addEdge(Vertex vet1, Vertex vet2) {\nif (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\nthrow new InvalidOperationException();\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nadjList[vet1].Add(vet2);\nadjList[vet2].Add(vet1);\n}\n/* \u5220\u9664\u8fb9 */\npublic void removeEdge(Vertex vet1, Vertex vet2) {\nif (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\nthrow new InvalidOperationException();\n// \u5220\u9664\u8fb9 vet1 - vet2\nadjList[vet1].Remove(vet2);\nadjList[vet2].Remove(vet1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npublic void addVertex(Vertex vet) {\nif (adjList.ContainsKey(vet))\nreturn;\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nadjList.Add(vet, new List<Vertex>());\n}\n/* \u5220\u9664\u9876\u70b9 */\npublic void removeVertex(Vertex vet) {\nif (!adjList.ContainsKey(vet))\nthrow new InvalidOperationException();\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nadjList.Remove(vet);\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nforeach (List<Vertex> list in adjList.Values) {\nlist.Remove(vet);\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\npublic void print() {\nConsole.WriteLine(\"\u90bb\u63a5\u8868 =\");\nforeach (KeyValuePair<Vertex, List<Vertex>> pair in adjList) {\nList<int> tmp = new List<int>();\nforeach (Vertex vertex in pair.Value)\ntmp.Add(vertex.val);\nConsole.WriteLine(pair.Key.val + \": [\" + string.Join(\", \", tmp) + \"],\");\n}\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: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\npublic private(set) var adjList: [Vertex: [Vertex]]\n/* \u6784\u9020\u65b9\u6cd5 */\npublic init(edges: [[Vertex]]) {\nadjList = [:]\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor edge in edges {\naddVertex(vet: edge[0])\naddVertex(vet: edge[1])\naddEdge(vet1: edge[0], vet2: edge[1])\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\npublic func size() -> Int {\nadjList.count\n}\n/* \u6dfb\u52a0\u8fb9 */\npublic func addEdge(vet1: Vertex, vet2: Vertex) {\nif adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\nfatalError(\"\u53c2\u6570\u9519\u8bef\")\n}\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nadjList[vet1]?.append(vet2)\nadjList[vet2]?.append(vet1)\n}\n/* \u5220\u9664\u8fb9 */\npublic func removeEdge(vet1: Vertex, vet2: Vertex) {\nif adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\nfatalError(\"\u53c2\u6570\u9519\u8bef\")\n}\n// \u5220\u9664\u8fb9 vet1 - vet2\nadjList[vet1]?.removeAll(where: { $0 == vet2 })\nadjList[vet2]?.removeAll(where: { $0 == vet1 })\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npublic func addVertex(vet: Vertex) {\nif adjList[vet] != nil {\nreturn\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nadjList[vet] = []\n}\n/* \u5220\u9664\u9876\u70b9 */\npublic func removeVertex(vet: Vertex) {\nif adjList[vet] == nil {\nfatalError(\"\u53c2\u6570\u9519\u8bef\")\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nadjList.removeValue(forKey: vet)\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor key in adjList.keys {\nadjList[key]?.removeAll(where: { $0 == vet })\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\npublic func print() {\nSwift.print(\"\u90bb\u63a5\u8868 =\")\nfor pair in adjList {\nvar tmp: [Int] = []\nfor vertex in pair.value {\ntmp.append(vertex.val)\n}\nSwift.print(\"\\(pair.key.val): \\(tmp),\")\n}\n}\n}\n
    graph_adjacency_list.zig
    [class]{GraphAdjList}-[func]{}\n
    graph_adjacency_list.dart
    /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n// \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nMap<Vertex, List<Vertex>> adjList = {};\n/* \u6784\u9020\u65b9\u6cd5 */\nGraphAdjList(List<List<Vertex>> edges) {\nfor (List<Vertex> edge in edges) {\naddVertex(edge[0]);\naddVertex(edge[1]);\naddEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nint size() {\nreturn adjList.length;\n}\n/* \u6dfb\u52a0\u8fb9 */\nvoid addEdge(Vertex vet1, Vertex vet2) {\nif (!adjList.containsKey(vet1) ||\n!adjList.containsKey(vet2) ||\nvet1 == vet2) {\nthrow ArgumentError;\n}\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nadjList[vet1]!.add(vet2);\nadjList[vet2]!.add(vet1);\n}\n/* \u5220\u9664\u8fb9 */\nvoid removeEdge(Vertex vet1, Vertex vet2) {\nif (!adjList.containsKey(vet1) ||\n!adjList.containsKey(vet2) ||\nvet1 == vet2) {\nthrow ArgumentError;\n}\n// \u5220\u9664\u8fb9 vet1 - vet2\nadjList[vet1]!.remove(vet2);\nadjList[vet2]!.remove(vet1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(Vertex vet) {\nif (adjList.containsKey(vet)) return;\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nadjList[vet] = [];\n}\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(Vertex vet) {\nif (!adjList.containsKey(vet)) {\nthrow ArgumentError;\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nadjList.remove(vet);\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nadjList.forEach((key, value) {\nvalue.remove(vet);\n});\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\nvoid printAdjList() {\nprint(\"\u90bb\u63a5\u8868 =\");\nadjList.forEach((key, value) {\nList<int> tmp = [];\nfor (Vertex vertex in value) {\ntmp.add(vertex.val);\n}\nprint(\"${key.val}: $tmp,\");\n});\n}\n}\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\u4e0b\u8868\u4e3a\u90bb\u63a5\u77e9\u9635\u548c\u90bb\u63a5\u8868\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u6548\u7387\u5bf9\u6bd4\u3002

    \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\u4e0a\u8868\uff0c\u4f3c\u4e4e\u90bb\u63a5\u8868\uff08\u54c8\u5e0c\u8868\uff09\u7684\u65f6\u95f4\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\u8981\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":"

    \u56fe\u4e0e\u6811\u7684\u5173\u7cfb

    \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\u662f\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\uff0c\u5efa\u8bae\u4f60\u5728\u5b66\u4e60\u672c\u7ae0\u8282\u65f6\u878d\u4f1a\u8d2f\u901a\u4e24\u8005\u7684\u6982\u5ff5\u4e0e\u5b9e\u73b0\u65b9\u6cd5\u3002

    \u300c\u56fe\u300d\u548c\u300c\u6811\u300d\u90fd\u662f\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u90fd\u9700\u8981\u4f7f\u7528\u300c\u641c\u7d22\u7b97\u6cd5\u300d\u6765\u5b9e\u73b0\u904d\u5386\u64cd\u4f5c\u3002

    \u4e0e\u6811\u7c7b\u4f3c\uff0c\u56fe\u7684\u904d\u5386\u65b9\u5f0f\u4e5f\u53ef\u5206\u4e3a\u4e24\u79cd\uff0c\u5373\u300c\u5e7f\u5ea6\u4f18\u5148\u904d\u5386 Breadth-First Traversal\u300d\u548c\u300c\u6df1\u5ea6\u4f18\u5148\u904d\u5386 Depth-First Traversal\u300d\uff0c\u4e5f\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\u8ddd\u79bb\u6700\u8fd1\u7684\u9876\u70b9\u5f00\u59cb\u8bbf\u95ee\uff0c\u5e76\u4e00\u5c42\u5c42\u5411\u5916\u6269\u5f20\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u4ece\u67d0\u4e2a\u9876\u70b9\u51fa\u53d1\uff0c\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

    Fig. \u56fe\u7684\u5e7f\u5ea6\u4f18\u5148\u904d\u5386

    "},{"location":"chapter_graph/graph_traversal/#_1","title":"\u7b97\u6cd5\u5b9e\u73b0","text":"

    BFS \u901a\u5e38\u501f\u52a9\u300c\u961f\u5217\u300d\u6765\u5b9e\u73b0\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

    1. \u5c06\u904d\u5386\u8d77\u59cb\u9876\u70b9 startVet \u52a0\u5165\u961f\u5217\uff0c\u5e76\u5f00\u542f\u5faa\u73af\uff1b
    2. \u5728\u5faa\u73af\u7684\u6bcf\u8f6e\u8fed\u4ee3\u4e2d\uff0c\u5f39\u51fa\u961f\u9996\u9876\u70b9\u5e76\u8bb0\u5f55\u8bbf\u95ee\uff0c\u7136\u540e\u5c06\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\u52a0\u5165\u5230\u961f\u5217\u5c3e\u90e8\uff1b
    3. \u5faa\u73af\u6b65\u9aa4 2. \uff0c\u76f4\u5230\u6240\u6709\u9876\u70b9\u88ab\u8bbf\u95ee\u5b8c\u6210\u540e\u7ed3\u675f\uff1b

    \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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nList<Vertex> res = new ArrayList<>();\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nSet<Vertex> visited = new HashSet<>() {{ add(startVet); }};\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nQueue<Vertex> que = new LinkedList<>() {{ offer(startVet); }};\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile (!que.isEmpty()) {\nVertex vet = que.poll(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.add(vet);            // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (Vertex adjVet : graph.adjList.get(vet)) {\nif (visited.contains(adjVet))\ncontinue;        // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nque.offer(adjVet);   // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res;\n}\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\nvector<Vertex *> res;\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nunordered_set<Vertex *> visited = {startVet};\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nqueue<Vertex *> que;\nque.push(startVet);\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile (!que.empty()) {\nVertex *vet = que.front();\nque.pop();          // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.push_back(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (auto adjVet : graph.adjList[vet]) {\nif (visited.count(adjVet))\ncontinue;            // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nque.push(adjVet);        // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.emplace(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res;\n}\n
    graph_bfs.py
    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\nres = []\n# \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nvisited = set[Vertex]([start_vet])\n# \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nque = deque[Vertex]([start_vet])\n# \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile len(que) > 0:\nvet = que.popleft()  # \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.append(vet)  # \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n# \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor adj_vet in graph.adj_list[vet]:\nif adj_vet in visited:\ncontinue  # \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nque.append(adj_vet)  # \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.add(adj_vet)  # \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n# \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res\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\nres := make([]Vertex, 0)\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nvisited := make(map[Vertex]struct{})\nvisited[startVet] = struct{}{}\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS, \u4f7f\u7528\u5207\u7247\u6a21\u62df\u961f\u5217\nqueue := make([]Vertex, 0)\nqueue = append(queue, startVet)\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nfor len(queue) > 0 {\n// \u961f\u9996\u9876\u70b9\u51fa\u961f\nvet := queue[0]\nqueue = queue[1:]\n// \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nres = append(res, vet)\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor _, adjVet := range g.adjList[vet] {\n_, isExist := visited[adjVet]\n// \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nif !isExist {\nqueue = append(queue, adjVet)\nvisited[adjVet] = struct{}{}\n}\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn 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\nconst res = [];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nconst visited = new Set();\nvisited.add(startVet);\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nconst que = [startVet];\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile (que.length) {\nconst vet = que.shift(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (const adjVet of graph.adjList.get(vet) ?? []) {\nif (visited.has(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nque.push(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn 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\nconst res: Vertex[] = [];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nconst visited: Set<Vertex> = new Set();\nvisited.add(startVet);\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nconst que = [startVet];\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile (que.length) {\nconst vet = que.shift(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (const adjVet of graph.adjList.get(vet) ?? []) {\nif (visited.has(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nque.push(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\nvisited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res;\n}\n
    graph_bfs.c
    [class]{}-[func]{graphBFS}\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\nList<Vertex> res = new List<Vertex>();\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nHashSet<Vertex> visited = new HashSet<Vertex>() { startVet };\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nQueue<Vertex> que = new Queue<Vertex>();\nque.Enqueue(startVet);\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile (que.Count > 0) {\nVertex vet = que.Dequeue(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.Add(vet);               // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nforeach (Vertex adjVet in graph.adjList[vet]) {\nif (visited.Contains(adjVet)) {\ncontinue;          // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nque.Enqueue(adjVet);   // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.Add(adjVet);   // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn 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\nvar res: [Vertex] = []\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nvar visited: Set<Vertex> = [startVet]\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nvar que: [Vertex] = [startVet]\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile !que.isEmpty {\nlet vet = que.removeFirst() // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.append(vet) // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor adjVet in graph.adjList[vet] ?? [] {\nif visited.contains(adjVet) {\ncontinue // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nque.append(adjVet) // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.insert(adjVet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res\n}\n
    graph_bfs.zig
    [class]{}-[func]{graphBFS}\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\nList<Vertex> res = [];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nSet<Vertex> visited = {};\nvisited.add(startVet);\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nQueue<Vertex> que = Queue();\nque.add(startVet);\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile (que.isNotEmpty) {\nVertex vet = que.removeFirst(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (Vertex adjVet in graph.adjList[vet]!) {\nif (visited.contains(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nque.add(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res;\n}\n

    \u4ee3\u7801\u76f8\u5bf9\u62bd\u8c61\uff0c\u5efa\u8bae\u5bf9\u7167\u4ee5\u4e0b\u52a8\u753b\u56fe\u793a\u6765\u52a0\u6df1\u7406\u89e3\u3002

    <1><2><3><4><5><6><7><8><9><10><11>

    \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\u662f\u5141\u8bb8\u88ab\u4efb\u610f\u6253\u4e71\u7684\u3002\u4ee5\u4e0a\u56fe\u4e3a\u4f8b\uff0c\u9876\u70b9 \\(1\\) , \\(3\\) \u7684\u8bbf\u95ee\u987a\u5e8f\u53ef\u4ee5\u4ea4\u6362\u3001\u9876\u70b9 \\(2\\) , \\(4\\) , \\(6\\) \u7684\u8bbf\u95ee\u987a\u5e8f\u4e5f\u53ef\u4ee5\u4efb\u610f\u4ea4\u6362\u3002

    "},{"location":"chapter_graph/graph_traversal/#_2","title":"\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

    "},{"location":"chapter_graph/graph_traversal/#932","title":"9.3.2. \u00a0 \u6df1\u5ea6\u4f18\u5148\u904d\u5386","text":"

    \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\u5177\u4f53\u5730\uff0c\u4ece\u67d0\u4e2a\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

    Fig. \u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u904d\u5386

    "},{"location":"chapter_graph/graph_traversal/#_3","title":"\u7b97\u6cd5\u5b9e\u73b0","text":"

    \u8fd9\u79cd\u201c\u8d70\u5230\u5c3d\u5934 + \u56de\u6eaf\u201d\u7684\u7b97\u6cd5\u5f62\u5f0f\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u6765\u5b9e\u73b0\u3002\u4e0e BFS \u7c7b\u4f3c\uff0c\u5728 DFS \u4e2d\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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) {\nres.add(vet);     // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (Vertex adjVet : graph.adjList.get(vet)) {\nif (visited.contains(adjVet))\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adjVet);\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\nList<Vertex> res = new ArrayList<>();\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nSet<Vertex> visited = new HashSet<>();\ndfs(graph, visited, res, startVet);\nreturn res;\n}\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) {\nres.push_back(vet);   // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.emplace(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (Vertex *adjVet : graph.adjList[vet]) {\nif (visited.count(adjVet))\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adjVet);\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\nvector<Vertex *> res;\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nunordered_set<Vertex *> visited;\ndfs(graph, visited, res, startVet);\nreturn res;\n}\n
    graph_dfs.py
    def dfs(graph: GraphAdjList, visited: set[Vertex], res: list[Vertex], vet: Vertex):\n\"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570\"\"\"\nres.append(vet)  # \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.add(vet)  # \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n# \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor adjVet in graph.adj_list[vet]:\nif adjVet in visited:\ncontinue  # \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n# \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adjVet)\ndef graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:\n\"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS\"\"\"\n# \u9876\u70b9\u904d\u5386\u5e8f\u5217\nres = []\n# \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nvisited = set[Vertex]()\ndfs(graph, visited, res, start_vet)\nreturn res\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)\nvisited[vet] = struct{}{}\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor _, adjVet := range g.adjList[vet] {\n_, isExist := visited[adjVet]\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\nif !isExist {\ndfs(g, 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\nfunc graphDFS(g *graphAdjList, startVet Vertex) []Vertex {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nres := make([]Vertex, 0)\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nvisited := make(map[Vertex]struct{})\ndfs(g, visited, &res, startVet)\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn 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) {\nres.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (const adjVet of graph.adjList.get(vet)) {\nif (visited.has(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adjVet);\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\nconst res = [];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nconst visited = new Set();\ndfs(graph, visited, res, startVet);\nreturn res;\n}\n
    graph_dfs.ts
    /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nfunction dfs(\ngraph: GraphAdjList,\nvisited: Set<Vertex>,\nres: Vertex[],\nvet: Vertex\n): void {\nres.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (const adjVet of graph.adjList.get(vet)) {\nif (visited.has(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adjVet);\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\nconst res: Vertex[] = [];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nconst visited: Set<Vertex> = new Set();\ndfs(graph, visited, res, startVet);\nreturn res;\n}\n
    graph_dfs.c
    [class]{}-[func]{dfs}\n[class]{}-[func]{graphDFS}\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) {\nres.Add(vet);     // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.Add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nforeach (Vertex adjVet in graph.adjList[vet]) {\nif (visited.Contains(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9                             \n}\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adjVet);\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\nList<Vertex> res = new List<Vertex>();\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nHashSet<Vertex> visited = new HashSet<Vertex>();\ndfs(graph, visited, res, startVet);\nreturn 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) {\nres.append(vet) // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.insert(vet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor adjVet in graph.adjList[vet] ?? [] {\nif visited.contains(adjVet) {\ncontinue // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph: graph, visited: &visited, res: &res, vet: adjVet)\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\nvar res: [Vertex] = []\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nvar visited: Set<Vertex> = []\ndfs(graph: graph, visited: &visited, res: &res, vet: startVet)\nreturn res\n}\n
    graph_dfs.zig
    [class]{}-[func]{dfs}\n[class]{}-[func]{graphDFS}\n
    graph_dfs.dart
    /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nvoid dfs(\nGraphAdjList graph,\nSet<Vertex> visited,\nList<Vertex> res,\nVertex vet,\n) {\nres.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (Vertex adjVet in graph.adjList[vet]!) {\nif (visited.contains(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adjVet);\n}\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\nList<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nList<Vertex> res = [];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nSet<Vertex> visited = {};\ndfs(graph, visited, res, startVet);\nreturn res;\n}\n

    \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u7b97\u6cd5\u6d41\u7a0b\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5176\u4e2d\uff1a

    • \u76f4\u865a\u7ebf\u4ee3\u8868\u5411\u4e0b\u9012\u63a8\uff0c\u8868\u793a\u5f00\u542f\u4e86\u4e00\u4e2a\u65b0\u7684\u9012\u5f52\u65b9\u6cd5\u6765\u8bbf\u95ee\u65b0\u9876\u70b9\uff1b
    • \u66f2\u865a\u7ebf\u4ee3\u8868\u5411\u4e0a\u56de\u6eaf\uff0c\u8868\u793a\u6b64\u9012\u5f52\u65b9\u6cd5\u5df2\u7ecf\u8fd4\u56de\uff0c\u56de\u6eaf\u5230\u4e86\u5f00\u542f\u6b64\u9012\u5f52\u65b9\u6cd5\u7684\u4f4d\u7f6e\uff1b

    \u4e3a\u4e86\u52a0\u6df1\u7406\u89e3\uff0c\u5efa\u8bae\u5c06\u56fe\u793a\u4e0e\u4ee3\u7801\u7ed3\u5408\u8d77\u6765\uff0c\u5728\u8111\u4e2d\uff08\u6216\u8005\u7528\u7b14\u753b\u4e0b\u6765\uff09\u6a21\u62df\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>

    \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\u3001\u201c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u201d\u3001\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\u4e0d\u540c\u7684\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/#_4","title":"\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

    "},{"location":"chapter_graph/summary/","title":"9.4. \u00a0 \u5c0f\u7ed3","text":"
    • \u56fe\u7531\u9876\u70b9\u548c\u8fb9\u7ec4\u6210\uff0c\u53ef\u4ee5\u88ab\u8868\u793a\u4e3a\u4e00\u7ec4\u9876\u70b9\u548c\u4e00\u7ec4\u8fb9\u6784\u6210\u7684\u96c6\u5408\u3002
    • \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\u5177\u6709\u66f4\u9ad8\u7684\u81ea\u7531\u5ea6\uff0c\u56e0\u800c\u66f4\u4e3a\u590d\u6742\u3002
    • \u6709\u5411\u56fe\u7684\u8fb9\u5177\u6709\u65b9\u5411\u6027\uff0c\u8fde\u901a\u56fe\u4e2d\u7684\u4efb\u610f\u9876\u70b9\u5747\u53ef\u8fbe\uff0c\u6709\u6743\u56fe\u7684\u6bcf\u6761\u8fb9\u90fd\u5305\u542b\u6743\u91cd\u53d8\u91cf\u3002
    • \u90bb\u63a5\u77e9\u9635\u5229\u7528\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\u6709\u8fb9\u6216\u65e0\u8fb9\u3002\u90bb\u63a5\u77e9\u9635\u5728\u589e\u5220\u67e5\u64cd\u4f5c\u4e0a\u6548\u7387\u5f88\u9ad8\uff0c\u4f46\u7a7a\u95f4\u5360\u7528\u8f83\u591a\u3002
    • \u90bb\u63a5\u8868\u4f7f\u7528\u591a\u4e2a\u94fe\u8868\u6765\u8868\u793a\u56fe\uff0c\u7b2c \\(i\\) \u6761\u94fe\u8868\u5bf9\u5e94\u9876\u70b9 \\(i\\) \uff0c\u5176\u4e2d\u5b58\u50a8\u4e86\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\u3002\u90bb\u63a5\u8868\u76f8\u5bf9\u4e8e\u90bb\u63a5\u77e9\u9635\u66f4\u52a0\u8282\u7701\u7a7a\u95f4\uff0c\u4f46\u7531\u4e8e\u9700\u8981\u904d\u5386\u94fe\u8868\u6765\u67e5\u627e\u8fb9\uff0c\u65f6\u95f4\u6548\u7387\u8f83\u4f4e\u3002
    • \u5f53\u90bb\u63a5\u8868\u4e2d\u7684\u94fe\u8868\u8fc7\u957f\u65f6\uff0c\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811\u6216\u54c8\u5e0c\u8868\uff0c\u4ece\u800c\u63d0\u5347\u67e5\u8be2\u6548\u7387\u3002
    • \u4ece\u7b97\u6cd5\u601d\u60f3\u89d2\u5ea6\u5206\u6790\uff0c\u90bb\u63a5\u77e9\u9635\u4f53\u73b0\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\uff0c\u90bb\u63a5\u8868\u4f53\u73b0\u201c\u4ee5\u65f6\u95f4\u6362\u7a7a\u95f4\u201d\u3002
    • \u56fe\u53ef\u7528\u4e8e\u5efa\u6a21\u5404\u7c7b\u73b0\u5b9e\u7cfb\u7edf\uff0c\u5982\u793e\u4ea4\u7f51\u7edc\u3001\u5730\u94c1\u7ebf\u8def\u7b49\u3002
    • \u6811\u662f\u56fe\u7684\u4e00\u79cd\u7279\u4f8b\uff0c\u6811\u7684\u904d\u5386\u4e5f\u662f\u56fe\u7684\u904d\u5386\u7684\u4e00\u79cd\u7279\u4f8b\u3002
    • \u56fe\u7684\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u662f\u4e00\u79cd\u7531\u8fd1\u53ca\u8fdc\u3001\u5c42\u5c42\u6269\u5f20\u7684\u641c\u7d22\u65b9\u5f0f\uff0c\u901a\u5e38\u501f\u52a9\u961f\u5217\u5b9e\u73b0\u3002
    • \u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u662f\u4e00\u79cd\u4f18\u5148\u8d70\u5230\u5e95\u3001\u65e0\u8def\u53ef\u8d70\u65f6\u518d\u56de\u6eaf\u7684\u641c\u7d22\u65b9\u5f0f\uff0c\u5e38\u57fa\u4e8e\u9012\u5f52\u6765\u5b9e\u73b0\u3002
    "},{"location":"chapter_graph/summary/#941-q-a","title":"9.4.1. \u00a0 Q & A","text":"

    \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\u8ba4\u4e3a\u662f\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\uff0c\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\u4f1a\u9700\u8981\u6309\u7167\u6307\u5b9a\u89c4\u5219\u6765\u6392\u5e8f\uff0c\u6bd4\u5982\u6309\u7167\u9876\u70b9\u6dfb\u52a0\u7684\u6b21\u5e8f\u3001\u6216\u8005\u6309\u7167\u9876\u70b9\u503c\u5927\u5c0f\u7684\u987a\u5e8f\u7b49\u7b49\uff0c\u8fd9\u6837\u53ef\u4ee5\u6709\u52a9\u4e8e\u5feb\u901f\u67e5\u627e\u201c\u5e26\u6709\u67d0\u79cd\u6781\u503c\u201d\u7684\u9876\u70b9\u3002

    "},{"location":"chapter_hashing/","title":"6. \u00a0 \u6563\u5217\u8868","text":""},{"location":"chapter_hashing/hash_collision/","title":"6.2. \u00a0 \u54c8\u5e0c\u51b2\u7a81","text":"

    \u5728\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u54c8\u5e0c\u51fd\u6570\u4e3a\u6bcf\u4e2a\u8f93\u5165\u751f\u6210\u552f\u4e00\u7684\u8f93\u51fa\uff0c\u5b9e\u73b0 key \u548c\u6570\u7ec4\u7d22\u5f15\u7684\u4e00\u4e00\u5bf9\u5e94\u3002\u4f46\u5b9e\u9645\u4e0a\uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u8f93\u5165\u7a7a\u95f4\u901a\u5e38\u8fdc\u5927\u4e8e\u8f93\u51fa\u7a7a\u95f4\uff0c\u56e0\u6b64\u591a\u4e2a\u8f93\u5165\u4ea7\u751f\u76f8\u540c\u8f93\u51fa\u7684\u60c5\u51b5\u662f\u4e0d\u53ef\u907f\u514d\u7684\u3002\u4f8b\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\u6570\u7ec4\u7d22\u5f15\u3002

    \u8fd9\u79cd\u591a\u4e2a\u8f93\u5165\u5bf9\u5e94\u540c\u4e00\u8f93\u51fa\u7d22\u5f15\u7684\u73b0\u8c61\u88ab\u79f0\u4e3a\u300c\u54c8\u5e0c\u51b2\u7a81 Hash Collision\u300d\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\u54c8\u5e0c\u51b2\u7a81\u7684\u89e3\u51b3\u65b9\u6cd5\u4e3b\u8981\u6709\u4e24\u79cd\uff1a

    • \u6269\u5927\u54c8\u5e0c\u8868\u5bb9\u91cf\uff1a\u54c8\u5e0c\u8868\u5bb9\u91cf\u8d8a\u5927\uff0c\u952e\u503c\u5bf9\u805a\u96c6\u7684\u6982\u7387\u5c31\u8d8a\u4f4e\u3002\u6781\u7aef\u60c5\u51b5\u4e0b\uff0c\u5f53\u8f93\u5165\u7a7a\u95f4\u548c\u8f93\u51fa\u7a7a\u95f4\u5927\u5c0f\u76f8\u7b49\u65f6\uff0c\u54c8\u5e0c\u8868\u7b49\u540c\u4e8e\u6570\u7ec4\uff0c\u6bcf\u4e2a key \u90fd\u5bf9\u5e94\u552f\u4e00\u7684\u6570\u7ec4\u7d22\u5f15\u3002
    • \u4f18\u5316\u54c8\u5e0c\u8868\u7ed3\u6784\uff1a\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\u94fe\u5f0f\u5730\u5740\u548c\u5f00\u653e\u5bfb\u5740\u3002
    "},{"location":"chapter_hashing/hash_collision/#621","title":"6.2.1. \u00a0 \u54c8\u5e0c\u8868\u6269\u5bb9","text":"

    \u54c8\u5e0c\u51fd\u6570\u7684\u6700\u540e\u4e00\u6b65\u901a\u5e38\u662f\u5bf9\u6876\u6570\u91cf \\(n\\) \u53d6\u4f59\uff0c\u4f5c\u7528\u662f\u5c06\u54c8\u5e0c\u503c\u6620\u5c04\u5230\u6876\u7d22\u5f15\u8303\u56f4\uff0c\u4ece\u800c\u5c06 key \u653e\u5165\u5bf9\u5e94\u7684\u6876\u4e2d\u3002\u5f53\u54c8\u5e0c\u8868\u5bb9\u91cf\u8d8a\u5927\uff08\u5373 \\(n\\) \u8d8a\u5927\uff09\u65f6\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\u5f53\u54c8\u5e0c\u8868\u5185\u7684\u51b2\u7a81\u603b\u4f53\u8f83\u4e3a\u4e25\u91cd\u65f6\uff0c\u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u901a\u8fc7\u6269\u5bb9\u54c8\u5e0c\u8868\u6765\u7f13\u89e3\u51b2\u7a81\u3002\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\u5f00\u9500\u8f83\u5927\u3002

    \u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u4f7f\u7528\u300c\u8d1f\u8f7d\u56e0\u5b50 Load Factor\u300d\u6765\u8861\u91cf\u54c8\u5e0c\u51b2\u7a81\u7684\u4e25\u91cd\u7a0b\u5ea6\uff0c\u5b9a\u4e49\u4e3a\u54c8\u5e0c\u8868\u4e2d\u5143\u7d20\u6570\u91cf\u9664\u4ee5\u6876\u6570\u91cf\uff0c\u5e38\u4f5c\u4e3a\u54c8\u5e0c\u8868\u6269\u5bb9\u7684\u89e6\u53d1\u6761\u4ef6\u3002\u5728 Java \u4e2d\uff0c\u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7 \\(0.75\\) \u65f6\uff0c\u7cfb\u7edf\u4f1a\u5c06 HashMap \u5bb9\u91cf\u6269\u5c55\u4e3a\u539f\u5148\u7684 \\(2\\) \u500d\u3002

    "},{"location":"chapter_hashing/hash_collision/#622","title":"6.2.2. \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

    Fig. \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868

    \u94fe\u5f0f\u5730\u5740\u4e0b\uff0c\u54c8\u5e0c\u8868\u7684\u64cd\u4f5c\u65b9\u6cd5\u5305\u62ec\uff1a

    • \u67e5\u8be2\u5143\u7d20\uff1a\u8f93\u5165 key \uff0c\u7ecf\u8fc7\u54c8\u5e0c\u51fd\u6570\u5f97\u5230\u6570\u7ec4\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
    • \u6dfb\u52a0\u5143\u7d20\uff1a\u5148\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u8bbf\u95ee\u94fe\u8868\u5934\u8282\u70b9\uff0c\u7136\u540e\u5c06\u8282\u70b9\uff08\u5373\u952e\u503c\u5bf9\uff09\u6dfb\u52a0\u5230\u94fe\u8868\u4e2d\u3002
    • \u5220\u9664\u5143\u7d20\uff1a\u6839\u636e\u54c8\u5e0c\u51fd\u6570\u7684\u7ed3\u679c\u8bbf\u95ee\u94fe\u8868\u5934\u90e8\uff0c\u63a5\u7740\u904d\u5386\u94fe\u8868\u4ee5\u67e5\u627e\u76ee\u6807\u8282\u70b9\uff0c\u5e76\u5c06\u5176\u5220\u9664\u3002

    \u5c3d\u7ba1\u94fe\u5f0f\u5730\u5740\u6cd5\u89e3\u51b3\u4e86\u54c8\u5e0c\u51b2\u7a81\u95ee\u9898\uff0c\u4f46\u4ecd\u5b58\u5728\u4e00\u4e9b\u5c40\u9650\u6027\uff0c\u5305\u62ec\uff1a

    • \u5360\u7528\u7a7a\u95f4\u589e\u5927\uff0c\u7531\u4e8e\u94fe\u8868\u6216\u4e8c\u53c9\u6811\u5305\u542b\u8282\u70b9\u6307\u9488\uff0c\u76f8\u6bd4\u6570\u7ec4\u66f4\u52a0\u8017\u8d39\u5185\u5b58\u7a7a\u95f4\uff1b
    • \u67e5\u8be2\u6548\u7387\u964d\u4f4e\uff0c\u56e0\u4e3a\u9700\u8981\u7ebf\u6027\u904d\u5386\u94fe\u8868\u6765\u67e5\u627e\u5bf9\u5e94\u5143\u7d20\uff1b

    \u4ee5\u4e0b\u7ed9\u51fa\u4e86\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\u7684\u7b80\u5355\u5b9e\u73b0\uff0c\u9700\u8981\u6ce8\u610f\uff1a

    • \u4e3a\u4e86\u4f7f\u5f97\u4ee3\u7801\u5c3d\u91cf\u7b80\u77ed\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\u4ee3\u66ff\u94fe\u8868\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u54c8\u5e0c\u8868\uff08\u6570\u7ec4\uff09\u5305\u542b\u591a\u4e2a\u6876\uff0c\u6bcf\u4e2a\u6876\u90fd\u662f\u4e00\u4e2a\u5217\u8868\u3002
    • \u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u54c8\u5e0c\u8868\u6269\u5bb9\u65b9\u6cd5\u3002\u5177\u4f53\u6765\u770b\uff0c\u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7 \\(0.75\\) \u65f6\uff0c\u6211\u4eec\u5c06\u54c8\u5e0c\u8868\u6269\u5bb9\u81f3 \\(2\\) \u500d\u3002
    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart hash_map_chaining.java
    /* \u952e\u503c\u5bf9 */\nclass Pair {\npublic int key;\npublic String val;\npublic Pair(int key, String val) {\nthis.key = key;\nthis.val = val;\n}\n}\n/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\nint size; // \u952e\u503c\u5bf9\u6570\u91cf\nint capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\ndouble loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nint extendRatio; // \u6269\u5bb9\u500d\u6570\nList<List<Pair>> buckets; // \u6876\u6570\u7ec4\n/* \u6784\u9020\u65b9\u6cd5 */\npublic HashMapChaining() {\nsize = 0;\ncapacity = 4;\nloadThres = 2 / 3.0;\nextendRatio = 2;\nbuckets = new ArrayList<>(capacity);\nfor (int i = 0; i < capacity; i++) {\nbuckets.add(new ArrayList<>());\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(int key) {\nreturn key % capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor() {\nreturn (double) size / capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nString get(int key) {\nint index = hashFunc(key);\nList<Pair> bucket = buckets.get(index);\n// \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nfor (Pair pair : bucket) {\nif (pair.key == key) {\nreturn pair.val;\n}\n}\n// \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de null\nreturn null;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(int key, String val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (loadFactor() > loadThres) {\nextend();\n}\nint index = hashFunc(key);\nList<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\nfor (Pair pair : bucket) {\nif (pair.key == key) {\npair.val = val;\nreturn;\n}\n}\n// \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\nPair pair = new Pair(key, val);\nbucket.add(pair);\nsize++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid remove(int key) {\nint index = hashFunc(key);\nList<Pair> bucket = buckets.get(index);\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor (Pair pair : bucket) {\nif (pair.key == key)\nbucket.remove(pair);\n}\nsize--;\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nList<List<Pair>> bucketsTmp = buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\ncapacity *= extendRatio;\nbuckets = new ArrayList<>(capacity);\nfor (int i = 0; i < capacity; i++) {\nbuckets.add(new ArrayList<>());\n}\nsize = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (List<Pair> bucket : bucketsTmp) {\nfor (Pair pair : bucket) {\nput(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print() {\nfor (List<Pair> bucket : buckets) {\nList<String> res = new ArrayList<>();\nfor (Pair pair : bucket) {\nres.add(pair.key + \" -> \" + pair.val);\n}\nSystem.out.println(res);\n}\n}\n}\n
    hash_map_chaining.cpp
    /* \u952e\u503c\u5bf9 */\nstruct Pair {\npublic:\nint key;\nstring val;\nPair(int key, string val) {\nthis->key = key;\nthis->val = val;\n}\n};\n/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\nprivate:\nint size;                       // \u952e\u503c\u5bf9\u6570\u91cf\nint capacity;                   // \u54c8\u5e0c\u8868\u5bb9\u91cf\ndouble loadThres;               // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nint extendRatio;                // \u6269\u5bb9\u500d\u6570\nvector<vector<Pair *>> buckets; // \u6876\u6570\u7ec4\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nHashMapChaining() : size(0), capacity(4), loadThres(2.0 / 3), extendRatio(2) {\nbuckets.resize(capacity);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(int key) {\nreturn key % capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor() {\nreturn (double)size / (double)capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nstring get(int key) {\nint index = hashFunc(key);\n// \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nfor (Pair *pair : buckets[index]) {\nif (pair->key == key) {\nreturn pair->val;\n}\n}\n// \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de nullptr\nreturn nullptr;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(int key, string val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (loadFactor() > loadThres) {\nextend();\n}\nint index = hashFunc(key);\n// \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nfor (Pair *pair : buckets[index]) {\nif (pair->key == key) {\npair->val = val;\nreturn;\n}\n}\n// \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\nbuckets[index].push_back(new Pair(key, val));\nsize++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid remove(int key) {\nint index = hashFunc(key);\nauto &bucket = buckets[index];\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor (int i = 0; i < bucket.size(); i++) {\nif (bucket[i]->key == key) {\nPair *tmp = bucket[i];\nbucket.erase(bucket.begin() + i); // \u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\ndelete tmp;                       // \u91ca\u653e\u5185\u5b58\nsize--;\nreturn;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nvector<vector<Pair *>> bucketsTmp = buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\ncapacity *= extendRatio;\nbuckets.clear();\nbuckets.resize(capacity);\nsize = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (auto &bucket : bucketsTmp) {\nfor (Pair *pair : bucket) {\nput(pair->key, pair->val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print() {\nfor (auto &bucket : buckets) {\ncout << \"[\";\nfor (Pair *pair : bucket) {\ncout << pair->key << \" -> \" << pair->val << \", \";\n}\ncout << \"]\\n\";\n}\n}\n};\n
    hash_map_chaining.py
    class Pair:\n\"\"\"\u952e\u503c\u5bf9\"\"\"\ndef __init__(self, key: int, val: str):\nself.key = key\nself.val = val\nclass HashMapChaining:\n\"\"\"\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\"\"\"\ndef __init__(self):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.size = 0  # \u952e\u503c\u5bf9\u6570\u91cf\nself.capacity = 4  # \u54c8\u5e0c\u8868\u5bb9\u91cf\nself.load_thres = 2 / 3  # \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nself.extend_ratio = 2  # \u6269\u5bb9\u500d\u6570\nself.buckets = [[] for _ in range(self.capacity)]  # \u6876\u6570\u7ec4\ndef hash_func(self, key: int) -> int:\n\"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\nreturn key % self.capacity\ndef load_factor(self) -> float:\n\"\"\"\u8d1f\u8f7d\u56e0\u5b50\"\"\"\nreturn self.size / self.capacity\ndef get(self, key: int) -> str:\n\"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\nindex = self.hash_func(key)\nbucket = self.buckets[index]\n# \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nfor pair in bucket:\nif pair.key == key:\nreturn pair.val\n# \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de None\nreturn None\ndef 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\nif self.load_factor() > self.load_thres:\nself.extend()\nindex = self.hash_func(key)\nbucket = self.buckets[index]\n# \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nfor pair in bucket:\nif pair.key == key:\npair.val = val\nreturn\n# \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\npair = Pair(key, val)\nbucket.append(pair)\nself.size += 1\ndef remove(self, key: int):\n\"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\nindex = self.hash_func(key)\nbucket = self.buckets[index]\n# \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor pair in bucket:\nif pair.key == key:\nbucket.remove(pair)\nself.size -= 1\nreturn\ndef extend(self):\n\"\"\"\u6269\u5bb9\u54c8\u5e0c\u8868\"\"\"\n# \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nbuckets = self.buckets\n# \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nself.capacity *= self.extend_ratio\nself.buckets = [[] for _ in range(self.capacity)]\nself.size = 0\n# \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor bucket in buckets:\nfor pair in bucket:\nself.put(pair.key, pair.val)\ndef print(self):\n\"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\nfor bucket in self.buckets:\nres = []\nfor pair in bucket:\nres.append(str(pair.key) + \" -> \" + pair.val)\nprint(res)\n
    hash_map_chaining.go
    [class]{pair}-[func]{}\n[class]{hashMapChaining}-[func]{}\n
    hash_map_chaining.js
    [class]{Pair}-[func]{}\n[class]{HashMapChaining}-[func]{}\n
    hash_map_chaining.ts
    [class]{Pair}-[func]{}\n[class]{HashMapChaining}-[func]{}\n
    hash_map_chaining.c
    [class]{pair}-[func]{}\n[class]{hashMapChaining}-[func]{}\n
    hash_map_chaining.cs
    [class]{Pair}-[func]{}\n[class]{HashMapChaining}-[func]{}\n
    hash_map_chaining.swift
    [class]{Pair}-[func]{}\n[class]{HashMapChaining}-[func]{}\n
    hash_map_chaining.zig
    [class]{Pair}-[func]{}\n[class]{HashMapChaining}-[func]{}\n
    hash_map_chaining.dart
    [class]{Pair}-[func]{}\n[class]{HashMapChaining}-[func]{}\n

    Tip

    \u4e3a\u4e86\u63d0\u9ad8\u6548\u7387\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u300cAVL \u6811\u300d\u6216\u300c\u7ea2\u9ed1\u6811\u300d\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/#623","title":"6.2.3. \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\u89e3\u51b3\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\u3002

    "},{"location":"chapter_hashing/hash_collision/#_1","title":"\u7ebf\u6027\u63a2\u6d4b","text":"

    \u7ebf\u6027\u63a2\u6d4b\u91c7\u7528\u56fa\u5b9a\u6b65\u957f\u7684\u7ebf\u6027\u67e5\u627e\u6765\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u3002

    • \u63d2\u5165\u5143\u7d20\uff1a\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u8ba1\u7b97\u6570\u7ec4\u7d22\u5f15\uff0c\u82e5\u53d1\u73b0\u6876\u5185\u5df2\u6709\u5143\u7d20\uff0c\u5219\u4ece\u51b2\u7a81\u4f4d\u7f6e\u5411\u540e\u7ebf\u6027\u904d\u5386\uff08\u6b65\u957f\u901a\u5e38\u4e3a \\(1\\) \uff09\uff0c\u76f4\u81f3\u627e\u5230\u7a7a\u4f4d\uff0c\u5c06\u5143\u7d20\u63d2\u5165\u5176\u4e2d\u3002
    • \u67e5\u627e\u5143\u7d20\uff1a\u82e5\u53d1\u73b0\u54c8\u5e0c\u51b2\u7a81\uff0c\u5219\u4f7f\u7528\u76f8\u540c\u6b65\u957f\u5411\u540e\u7ebf\u6027\u904d\u5386\uff0c\u76f4\u5230\u627e\u5230\u5bf9\u5e94\u5143\u7d20\uff0c\u8fd4\u56de value \u5373\u53ef\uff1b\u6216\u8005\u82e5\u9047\u5230\u7a7a\u4f4d\uff0c\u8bf4\u660e\u76ee\u6807\u952e\u503c\u5bf9\u4e0d\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u8fd4\u56de \\(\\text{None}\\) \u3002

    Fig. \u7ebf\u6027\u63a2\u6d4b

    \u7136\u800c\uff0c\u7ebf\u6027\u63a2\u6d4b\u5b58\u5728\u4ee5\u4e0b\u7f3a\u9677\uff1a

    • \u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5143\u7d20\u3002\u5220\u9664\u5143\u7d20\u4f1a\u5728\u6570\u7ec4\u5185\u4ea7\u751f\u4e00\u4e2a\u7a7a\u4f4d\uff0c\u67e5\u627e\u5176\u4ed6\u5143\u7d20\u65f6\uff0c\u8be5\u7a7a\u4f4d\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u8bef\u5224\u5143\u7d20\u4e0d\u5b58\u5728\u3002\u56e0\u6b64\uff0c\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u6807\u5fd7\u4f4d\u6765\u6807\u8bb0\u5df2\u5220\u9664\u5143\u7d20\u3002
    • \u5bb9\u6613\u4ea7\u751f\u805a\u96c6\u3002\u6570\u7ec4\u5185\u8fde\u7eed\u88ab\u5360\u7528\u4f4d\u7f6e\u8d8a\u957f\uff0c\u8fd9\u4e9b\u8fde\u7eed\u4f4d\u7f6e\u53d1\u751f\u54c8\u5e0c\u51b2\u7a81\u7684\u53ef\u80fd\u6027\u8d8a\u5927\uff0c\u8fdb\u4e00\u6b65\u4fc3\u4f7f\u8fd9\u4e00\u4f4d\u7f6e\u7684\u201c\u805a\u5806\u751f\u957f\u201d\uff0c\u6700\u7ec8\u5bfc\u81f4\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u6548\u7387\u964d\u4f4e\u3002

    \u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u4e3a\u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\uff09\u54c8\u5e0c\u8868\u7684\u7b80\u5355\u5b9e\u73b0\uff0c\u91cd\u70b9\u5305\u62ec\uff1a

    • \u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u56fa\u5b9a\u7684\u952e\u503c\u5bf9\u5b9e\u4f8b removed \u6765\u6807\u8bb0\u5df2\u5220\u9664\u5143\u7d20\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u4e00\u4e2a\u6876\u4e3a \\(\\text{None}\\) \u6216 removed \u65f6\uff0c\u8fd9\u4e2a\u6876\u90fd\u662f\u7a7a\u7684\uff0c\u53ef\u7528\u4e8e\u653e\u7f6e\u952e\u503c\u5bf9\u3002
    • \u88ab\u6807\u8bb0\u4e3a\u5df2\u5220\u9664\u7684\u7a7a\u95f4\u662f\u53ef\u4ee5\u518d\u6b21\u88ab\u4f7f\u7528\u7684\u3002\u5f53\u63d2\u5165\u5143\u7d20\u65f6\uff0c\u82e5\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u627e\u5230\u4e86\u88ab\u6807\u8bb0\u4e3a\u5df2\u5220\u9664\u7684\u7d22\u5f15\uff0c\u5219\u53ef\u5c06\u8be5\u5143\u7d20\u653e\u7f6e\u5230\u8be5\u7d22\u5f15\u3002
    • \u5728\u7ebf\u6027\u63a2\u6d4b\u65f6\uff0c\u6211\u4eec\u4ece\u5f53\u524d\u7d22\u5f15 index \u5411\u540e\u904d\u5386\uff1b\u800c\u5f53\u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u9700\u8981\u56de\u5230\u5934\u90e8\u7ee7\u7eed\u904d\u5386\u3002
    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart hash_map_open_addressing.java
    /* \u952e\u503c\u5bf9 */\nclass Pair {\npublic int key;\npublic String val;\npublic Pair(int key, String val) {\nthis.key = key;\nthis.val = val;\n}\n}\n/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\nprivate int size; // \u952e\u503c\u5bf9\u6570\u91cf\nprivate int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\nprivate double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nprivate int extendRatio; // \u6269\u5bb9\u500d\u6570\nprivate Pair[] buckets; // \u6876\u6570\u7ec4\nprivate Pair removed; // \u5220\u9664\u6807\u8bb0\n/* \u6784\u9020\u65b9\u6cd5 */\npublic HashMapOpenAddressing() {\nsize = 0;\ncapacity = 4;\nloadThres = 2.0 / 3.0;\nextendRatio = 2;\nbuckets = new Pair[capacity];\nremoved = new Pair(-1, \"-1\");\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\npublic int hashFunc(int key) {\nreturn key % capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\npublic double loadFactor() {\nreturn (double) size / capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npublic String get(int key) {\nint index = hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (int i = 0; i < capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nint j = (index + i) % capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u8fd4\u56de null\nif (buckets[j] == null)\nreturn null;\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif (buckets[j].key == key && buckets[j] != removed)\nreturn buckets[j].val;\n}\nreturn null;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\npublic void put(int key, String val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (loadFactor() > loadThres) {\nextend();\n}\nint index = hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (int i = 0; i < capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nint j = (index + i) % 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\nif (buckets[j] == null || buckets[j] == removed) {\nbuckets[j] = new Pair(key, val);\nsize += 1;\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val\nif (buckets[j].key == key) {\nbuckets[j].val = val;\nreturn;\n}\n}\n}\n/* \u5220\u9664\u64cd\u4f5c */\npublic void remove(int key) {\nint index = hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (int i = 0; i < capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nint j = (index + i) % capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (buckets[j] == null) {\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u6807\u8bb0\u5220\u9664\u5e76\u8fd4\u56de\nif (buckets[j].key == key) {\nbuckets[j] = removed;\nsize -= 1;\nreturn;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\npublic void extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nPair[] bucketsTmp = buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\ncapacity *= extendRatio;\nbuckets = new Pair[capacity];\nsize = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (Pair pair : bucketsTmp) {\nif (pair != null && pair != removed) {\nput(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\npublic void print() {\nfor (Pair pair : buckets) {\nif (pair != null) {\nSystem.out.println(pair.key + \" -> \" + pair.val);\n} else {\nSystem.out.println(\"null\");\n}\n}\n}\n}\n
    hash_map_open_addressing.cpp
    /* \u952e\u503c\u5bf9 */\nstruct Pair {\nint key;\nstring val;\nPair(int k, string v) : key(k), val(v) {\n}\n};\n/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\nprivate:\nint size;               // \u952e\u503c\u5bf9\u6570\u91cf\nint capacity;           // \u54c8\u5e0c\u8868\u5bb9\u91cf\ndouble loadThres;       // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nint extendRatio;        // \u6269\u5bb9\u500d\u6570\nvector<Pair *> buckets; // \u6876\u6570\u7ec4\nPair *removed;          // \u5220\u9664\u6807\u8bb0\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nHashMapOpenAddressing() {\n// \u6784\u9020\u65b9\u6cd5\nsize = 0;\ncapacity = 4;\nloadThres = 2.0 / 3.0;\nextendRatio = 2;\nbuckets = vector<Pair *>(capacity, nullptr);\nremoved = new Pair(-1, \"-1\");\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(int key) {\nreturn key % capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor() {\nreturn static_cast<double>(size) / capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nstring get(int key) {\nint index = hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (int i = 0; i < capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nint j = (index + i) % capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u8fd4\u56de nullptr\nif (buckets[j] == nullptr)\nreturn nullptr;\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif (buckets[j]->key == key && buckets[j] != removed)\nreturn buckets[j]->val;\n}\nreturn nullptr;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(int key, string val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (loadFactor() > loadThres)\nextend();\nint index = hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (int i = 0; i < capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nint j = (index + i) % 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\nif (buckets[j] == nullptr || buckets[j] == removed) {\nbuckets[j] = new Pair(key, val);\nsize += 1;\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val\nif (buckets[j]->key == key) {\nbuckets[j]->val = val;\nreturn;\n}\n}\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid remove(int key) {\nint index = hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (int i = 0; i < capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nint j = (index + i) % capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (buckets[j] == nullptr)\nreturn;\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u6807\u8bb0\u5220\u9664\u5e76\u8fd4\u56de\nif (buckets[j]->key == key) {\ndelete buckets[j]; // \u91ca\u653e\u5185\u5b58\nbuckets[j] = removed;\nsize -= 1;\nreturn;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nvector<Pair *> bucketsTmp = buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\ncapacity *= extendRatio;\nbuckets = vector<Pair *>(capacity, nullptr);\nsize = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (Pair *pair : bucketsTmp) {\nif (pair != nullptr && pair != removed) {\nput(pair->key, pair->val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print() {\nfor (auto &pair : buckets) {\nif (pair != nullptr) {\ncout << pair->key << \" -> \" << pair->val << endl;\n} else {\ncout << \"nullptr\" << endl;\n}\n}\n}\n};\n
    hash_map_open_addressing.py
    class Pair:\n\"\"\"\u952e\u503c\u5bf9\"\"\"\ndef __init__(self, key: int, val: str):\nself.key = key\nself.val = val\nclass HashMapOpenAddressing:\n\"\"\"\u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868\"\"\"\ndef __init__(self):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.size = 0  # \u952e\u503c\u5bf9\u6570\u91cf\nself.capacity = 4  # \u54c8\u5e0c\u8868\u5bb9\u91cf\nself.load_thres = 2 / 3  # \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nself.extend_ratio = 2  # \u6269\u5bb9\u500d\u6570\nself.buckets: list[Pair | None] = [None] * self.capacity  # \u6876\u6570\u7ec4\nself.removed = Pair(-1, \"-1\")  # \u5220\u9664\u6807\u8bb0\ndef hash_func(self, key: int) -> int:\n\"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\nreturn key % self.capacity\ndef load_factor(self) -> float:\n\"\"\"\u8d1f\u8f7d\u56e0\u5b50\"\"\"\nreturn self.size / self.capacity\ndef get(self, key: int) -> str:\n\"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\nindex = self.hash_func(key)\n# \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor i in range(self.capacity):\n# \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nj = (index + i) % self.capacity\n# \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u8fd4\u56de None\nif self.buckets[j] is None:\nreturn None\n# \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif self.buckets[j].key == key and self.buckets[j] != self.removed:\nreturn self.buckets[j].val\ndef 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\nif self.load_factor() > self.load_thres:\nself.extend()\nindex = self.hash_func(key)\n# \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor i in range(self.capacity):\n# \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nj = (index + i) % self.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\nif self.buckets[j] in [None, self.removed]:\nself.buckets[j] = Pair(key, val)\nself.size += 1\nreturn\n# \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val\nif self.buckets[j].key == key:\nself.buckets[j].val = val\nreturn\ndef remove(self, key: int):\n\"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\nindex = self.hash_func(key)\n# \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor i in range(self.capacity):\n# \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nj = (index + i) % self.capacity\n# \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif self.buckets[j] is None:\nreturn\n# \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u6807\u8bb0\u5220\u9664\u5e76\u8fd4\u56de\nif self.buckets[j].key == key:\nself.buckets[j] = self.removed\nself.size -= 1\nreturn\ndef extend(self):\n\"\"\"\u6269\u5bb9\u54c8\u5e0c\u8868\"\"\"\n# \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nbuckets_tmp = self.buckets\n# \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nself.capacity *= self.extend_ratio\nself.buckets = [None] * self.capacity\nself.size = 0\n# \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor pair in buckets_tmp:\nif pair not in [None, self.removed]:\nself.put(pair.key, pair.val)\ndef print(self) -> None:\n\"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\nfor pair in self.buckets:\nif pair is not None:\nprint(pair.key, \"->\", pair.val)\nelse:\nprint(\"None\")\n
    hash_map_open_addressing.go
    [class]{pair}-[func]{}\n[class]{hashMapOpenAddressing}-[func]{}\n
    hash_map_open_addressing.js
    [class]{Pair}-[func]{}\n[class]{HashMapOpenAddressing}-[func]{}\n
    hash_map_open_addressing.ts
    [class]{Pair}-[func]{}\n[class]{HashMapOpenAddressing}-[func]{}\n
    hash_map_open_addressing.c
    [class]{pair}-[func]{}\n[class]{hashMapOpenAddressing}-[func]{}\n
    hash_map_open_addressing.cs
    [class]{Pair}-[func]{}\n[class]{HashMapOpenAddressing}-[func]{}\n
    hash_map_open_addressing.swift
    [class]{Pair}-[func]{}\n[class]{HashMapOpenAddressing}-[func]{}\n
    hash_map_open_addressing.zig
    [class]{Pair}-[func]{}\n[class]{HashMapOpenAddressing}-[func]{}\n
    hash_map_open_addressing.dart
    [class]{Pair}-[func]{}\n[class]{HashMapOpenAddressing}-[func]{}\n
    "},{"location":"chapter_hashing/hash_collision/#_2","title":"\u591a\u6b21\u54c8\u5e0c","text":"

    \u987e\u540d\u601d\u4e49\uff0c\u591a\u6b21\u54c8\u5e0c\u65b9\u6cd5\u662f\u4f7f\u7528\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570 \\(f_1(x)\\) , \\(f_2(x)\\) , \\(f_3(x)\\) , \\(\\cdots\\) \u8fdb\u884c\u63a2\u6d4b\u3002

    • \u63d2\u5165\u5143\u7d20\uff1a\u82e5\u54c8\u5e0c\u51fd\u6570 \\(f_1(x)\\) \u51fa\u73b0\u51b2\u7a81\uff0c\u5219\u5c1d\u8bd5 \\(f_2(x)\\) \uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u5230\u627e\u5230\u7a7a\u4f4d\u540e\u63d2\u5165\u5143\u7d20\u3002
    • \u67e5\u627e\u5143\u7d20\uff1a\u5728\u76f8\u540c\u7684\u54c8\u5e0c\u51fd\u6570\u987a\u5e8f\u4e0b\u8fdb\u884c\u67e5\u627e\uff0c\u76f4\u5230\u627e\u5230\u76ee\u6807\u5143\u7d20\u65f6\u8fd4\u56de\uff1b\u6216\u9047\u5230\u7a7a\u4f4d\u6216\u5df2\u5c1d\u8bd5\u6240\u6709\u54c8\u5e0c\u51fd\u6570\uff0c\u8bf4\u660e\u54c8\u5e0c\u8868\u4e2d\u4e0d\u5b58\u5728\u8be5\u5143\u7d20\uff0c\u5219\u8fd4\u56de \\(\\text{None}\\) \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\u589e\u52a0\u989d\u5916\u7684\u8ba1\u7b97\u91cf\u3002

    \u7f16\u7a0b\u8bed\u8a00\u7684\u9009\u62e9

    Java \u91c7\u7528\u94fe\u5f0f\u5730\u5740\u3002\u81ea JDK 1.8 \u4ee5\u6765\uff0c\u5f53 HashMap \u5185\u6570\u7ec4\u957f\u5ea6\u8fbe\u5230 64 \u4e14\u94fe\u8868\u957f\u5ea6\u8fbe\u5230 8 \u65f6\uff0c\u94fe\u8868\u4f1a\u88ab\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811\u4ee5\u63d0\u5347\u67e5\u627e\u6027\u80fd\u3002

    Python \u91c7\u7528\u5f00\u653e\u5bfb\u5740\u3002\u5b57\u5178 dict \u4f7f\u7528\u4f2a\u968f\u673a\u6570\u8fdb\u884c\u63a2\u6d4b\u3002

    Golang \u91c7\u7528\u94fe\u5f0f\u5730\u5740\u3002Go \u89c4\u5b9a\u6bcf\u4e2a\u6876\u6700\u591a\u5b58\u50a8 8 \u4e2a\u952e\u503c\u5bf9\uff0c\u8d85\u51fa\u5bb9\u91cf\u5219\u8fde\u63a5\u4e00\u4e2a\u6ea2\u51fa\u6876\uff1b\u5f53\u6ea2\u51fa\u6876\u8fc7\u591a\u65f6\uff0c\u4f1a\u6267\u884c\u4e00\u6b21\u7279\u6b8a\u7684\u7b49\u91cf\u6269\u5bb9\u64cd\u4f5c\uff0c\u4ee5\u786e\u4fdd\u6027\u80fd\u3002

    "},{"location":"chapter_hashing/hash_map/","title":"6.1. \u00a0 \u54c8\u5e0c\u8868","text":"

    \u54c8\u5e0c\u8868\u901a\u8fc7\u5efa\u7acb\u300c\u952e key\u300d\u4e0e\u300c\u503c value\u300d\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 key\uff0c\u5219\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u83b7\u53d6\u5bf9\u5e94\u7684 value \u3002

    \u4ee5\u4e00\u4e2a\u5305\u542b \\(n\\) \u4e2a\u5b66\u751f\u7684\u6570\u636e\u5e93\u4e3a\u4f8b\uff0c\u6bcf\u4e2a\u5b66\u751f\u90fd\u6709\u201c\u59d3\u540d name\u201d\u548c\u201c\u5b66\u53f7 id\u201d\u4e24\u9879\u6570\u636e\u3002\u5047\u5982\u6211\u4eec\u5e0c\u671b\u5b9e\u73b0\u67e5\u8be2\u529f\u80fd\uff0c\u4f8b\u5982\u201c\u8f93\u5165\u4e00\u4e2a\u5b66\u53f7\uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u59d3\u540d\u201d\uff0c\u5219\u53ef\u4ee5\u91c7\u7528\u54c8\u5e0c\u8868\u6765\u5b9e\u73b0\u3002

    Fig. \u54c8\u5e0c\u8868\u7684\u62bd\u8c61\u8868\u793a

    \u9664\u54c8\u5e0c\u8868\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u6216\u94fe\u8868\u5b9e\u73b0\u5143\u7d20\u67e5\u8be2\uff0c\u5176\u4e2d\uff1a

    • \u67e5\u8be2\u5143\u7d20\u9700\u8981\u904d\u5386\u6240\u6709\u5143\u7d20\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff1b
    • \u6dfb\u52a0\u5143\u7d20\u4ec5\u9700\u6dfb\u52a0\u81f3\u5c3e\u90e8\u5373\u53ef\uff0c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\uff1b
    • \u5220\u9664\u5143\u7d20\u9700\u8981\u5148\u67e5\u8be2\u518d\u5220\u9664\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff1b

    \u7136\u800c\uff0c\u5728\u54c8\u5e0c\u8868\u4e2d\u8fdb\u884c\u589e\u5220\u67e5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662f \\(O(1)\\) \u3002\u54c8\u5e0c\u8868\u5168\u9762\u80dc\u51fa\uff01\u56e0\u6b64\uff0c\u54c8\u5e0c\u8868\u5e38\u7528\u4e8e\u5bf9\u67e5\u627e\u6548\u7387\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\u3002

    \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)\\)"},{"location":"chapter_hashing/hash_map/#611","title":"6.1.1. \u00a0 \u54c8\u5e0c\u8868\u5e38\u7528\u64cd\u4f5c","text":"

    \u54c8\u5e0c\u8868\u7684\u57fa\u672c\u64cd\u4f5c\u5305\u62ec \u521d\u59cb\u5316\u3001\u67e5\u8be2\u64cd\u4f5c\u3001\u6dfb\u52a0\u4e0e\u5220\u9664\u952e\u503c\u5bf9\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart hash_map.java
    /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nMap<Integer, String> map = new HashMap<>();\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.put(12836, \"\u5c0f\u54c8\");   map.put(15937, \"\u5c0f\u5570\");   map.put(16750, \"\u5c0f\u7b97\");   map.put(13276, \"\u5c0f\u6cd5\");\nmap.put(10583, \"\u5c0f\u9e2d\");\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/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.remove(10583);\n
    hash_map.cpp
    /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nunordered_map<int, string> map;\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/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nstring name = map[15937];\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.erase(10583);\n
    hash_map.py
    # \u521d\u59cb\u5316\u54c8\u5e0c\u8868\nmapp: Dict = {}\n# \u6dfb\u52a0\u64cd\u4f5c\n# \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmapp[12836] = \"\u5c0f\u54c8\"\nmapp[15937] = \"\u5c0f\u5570\"\nmapp[16750] = \"\u5c0f\u7b97\"\nmapp[13276] = \"\u5c0f\u6cd5\"\nmapp[10583] = \"\u5c0f\u9e2d\"\n# \u67e5\u8be2\u64cd\u4f5c\n# \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nname: str = mapp[15937]\n# \u5220\u9664\u64cd\u4f5c\n# \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmapp.pop(10583)\n
    hash_map.go
    /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nmapp := make(map[int]string)\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmapp[12836] = \"\u5c0f\u54c8\"\nmapp[15937] = \"\u5c0f\u5570\"\nmapp[16750] = \"\u5c0f\u7b97\"\nmapp[13276] = \"\u5c0f\u6cd5\"\nmapp[10583] = \"\u5c0f\u9e2d\"\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nname := mapp[15937]\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\ndelete(mapp, 10583)\n
    hash_map.js
    /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nconst map = new ArrayHashMap();\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/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet name = map.get(15937);\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/* \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/* \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.c
    // C \u672a\u63d0\u4f9b\u5185\u7f6e\u54c8\u5e0c\u8868\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)\nmap.Add(12836, \"\u5c0f\u54c8\");\nmap.Add(15937, \"\u5c0f\u5570\");\nmap.Add(16750, \"\u5c0f\u7b97\");\nmap.Add(13276, \"\u5c0f\u6cd5\");\nmap.Add(10583, \"\u5c0f\u9e2d\");\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nString name = map[15937];\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.Remove(10583);\n
    hash_map.swift
    /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nvar map: [Int: String] = [:]\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/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet name = map[15937]!\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.zig
    \n
    hash_map.dart
    /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nMap<int, String> map = {};\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/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nString name = map[15937];\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.remove(10583);\n

    \u904d\u5386\u54c8\u5e0c\u8868\u6709\u4e09\u79cd\u65b9\u5f0f\uff0c\u5373 \u904d\u5386\u952e\u503c\u5bf9\u3001\u904d\u5386\u952e\u3001\u904d\u5386\u503c\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart hash_map.java
    /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor (Map.Entry <Integer, String> kv: map.entrySet()) {\nSystem.out.println(kv.getKey() + \" -> \" + kv.getValue());\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nfor (int key: map.keySet()) {\nSystem.out.println(key);\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor (String val: map.values()) {\nSystem.out.println(val);\n}\n
    hash_map.cpp
    /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor (auto kv: map) {\ncout << kv.first << \" -> \" << kv.second << endl;\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nfor (auto key: map) {\ncout << key.first << endl;\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor (auto val: map) {\ncout << val.second << endl;\n}\n
    hash_map.py
    # \u904d\u5386\u54c8\u5e0c\u8868\n# \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor key, value in mapp.items():\nprint(key, \"->\", value)\n# \u5355\u72ec\u904d\u5386\u952e key\nfor key in mapp.keys():\nprint(key)\n# \u5355\u72ec\u904d\u5386\u503c value\nfor value in mapp.values():\nprint(value)\n
    hash_map_test.go
    /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor key, value := range mapp {\nfmt.Println(key, \"->\", value)\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nfor key := range mapp {\nfmt.Println(key)\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor _, value := range mapp {\nfmt.Println(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()) {\nconsole.info(k + ' -> ' + v);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u952e Key');\nfor (const k of map.keys()) {\nconsole.info(k);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u503c Value');\nfor (const v of map.values()) {\nconsole.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()) {\nconsole.info(k + ' -> ' + v);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u952e Key');\nfor (const k of map.keys()) {\nconsole.info(k);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u503c Value');\nfor (const v of map.values()) {\nconsole.info(v);\n}\n
    hash_map.c
    // C \u672a\u63d0\u4f9b\u5185\u7f6e\u54c8\u5e0c\u8868\n
    hash_map.cs
    /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nforeach (var kv in map)\n{\nConsole.WriteLine(kv.Key + \" -> \" + kv.Value);\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nforeach (int key in map.Keys)\n{\nConsole.WriteLine(key);\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nforeach (String val in map.Values)\n{\nConsole.WriteLine(val);\n}\n
    hash_map.swift
    /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nfor (key, value) in map {\nprint(\"\\(key) -> \\(value)\")\n}\n// \u5355\u72ec\u904d\u5386\u952e Key\nfor key in map.keys {\nprint(key)\n}\n// \u5355\u72ec\u904d\u5386\u503c Value\nfor value in map.values {\nprint(value)\n}\n
    hash_map.zig
    \n
    hash_map.dart
    /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nmap.forEach((key, value) {\nprint('$key -> $value');\n});\n// \u5355\u72ec\u904d\u5386\u952e Key\nmap.keys.forEach((key) {\nprint(key);\n});\n// \u5355\u72ec\u904d\u5386\u503c Value\nmap.values.forEach((value) {\nprint(value);\n});\n
    "},{"location":"chapter_hashing/hash_map/#612","title":"6.1.2. \u00a0 \u54c8\u5e0c\u51fd\u6570","text":"

    \u54c8\u5e0c\u8868\u7684\u5e95\u5c42\u5b9e\u73b0\u4e3a\u6570\u7ec4\uff0c\u540c\u65f6\u53ef\u80fd\u5305\u542b\u94fe\u8868\u3001\u4e8c\u53c9\u6811\uff08\u7ea2\u9ed1\u6811\uff09\u7b49\u6570\u636e\u7ed3\u6784\uff0c\u4ee5\u63d0\u9ad8\u67e5\u8be2\u6027\u80fd\uff08\u5c06\u5728\u4e0b\u8282\u8ba8\u8bba\uff09\u3002

    \u9996\u5148\u8003\u8651\u6700\u7b80\u5355\u7684\u60c5\u51b5\uff0c\u4ec5\u4f7f\u7528\u4e00\u4e2a\u6570\u7ec4\u6765\u5b9e\u73b0\u54c8\u5e0c\u8868\u3002\u901a\u5e38\uff0c\u6211\u4eec\u5c06\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u7a7a\u4f4d\u79f0\u4e3a\u300c\u6876 Bucket\u300d\uff0c\u7528\u4e8e\u5b58\u50a8\u952e\u503c\u5bf9\u3002

    \u6211\u4eec\u5c06\u952e\u503c\u5bf9 key, value \u5c01\u88c5\u6210\u4e00\u4e2a\u7c7b Pair \uff0c\u5e76\u5c06\u6240\u6709 Pair \u653e\u5165\u6570\u7ec4\u4e2d\u3002\u8fd9\u6837\uff0c\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a Pair \u90fd\u5177\u6709\u552f\u4e00\u7684\u7d22\u5f15\u3002\u4e3a\u4e86\u5efa\u7acb key \u548c\u7d22\u5f15\u4e4b\u95f4\u7684\u6620\u5c04\u5173\u7cfb\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u300c\u54c8\u5e0c\u51fd\u6570 Hash Function\u300d\u3002

    \u8bbe\u54c8\u5e0c\u8868\u7684\u6570\u7ec4\u4e3a buckets \uff0c\u54c8\u5e0c\u51fd\u6570\u4e3a f(x) \uff0c\u90a3\u4e48\u67e5\u8be2\u64cd\u4f5c\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a

    1. \u8f93\u5165 key \uff0c\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u8ba1\u7b97\u51fa\u7d22\u5f15 index \uff0c\u5373 index = f(key) \uff1b
    2. \u901a\u8fc7\u7d22\u5f15\u5728\u6570\u7ec4\u4e2d\u8bbf\u95ee\u5230\u952e\u503c\u5bf9 pair \uff0c\u5373 pair = buckets[index] \uff0c\u7136\u540e\u4ece pair \u4e2d\u83b7\u53d6\u5bf9\u5e94\u7684 value \uff1b

    \u4ee5\u5b66\u751f\u6570\u636e key \u5b66\u53f7 -> value \u59d3\u540d \u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u8ba1\u5982\u4e0b\u54c8\u5e0c\u51fd\u6570\uff1a

    \\[ f(x) = x \\bmod {100} \\]

    \u5176\u4e2d \\(\\bmod\\) \u8868\u793a\u53d6\u4f59\u8fd0\u7b97\u3002

    Fig. \u54c8\u5e0c\u51fd\u6570\u5de5\u4f5c\u539f\u7406

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart array_hash_map.java
    /* \u952e\u503c\u5bf9 */\nclass Pair {\npublic int key;\npublic String val;\npublic Pair(int key, String val) {\nthis.key = key;\nthis.val = val;\n}\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\nprivate List<Pair> buckets;\npublic ArrayHashMap() {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nbuckets = new ArrayList<>();\nfor (int i = 0; i < 100; i++) {\nbuckets.add(null);\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nprivate int hashFunc(int key) {\nint index = key % 100;\nreturn index;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npublic String get(int key) {\nint index = hashFunc(key);\nPair pair = buckets.get(index);\nif (pair == null)\nreturn null;\nreturn pair.val;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\npublic void put(int key, String val) {\nPair pair = new Pair(key, val);\nint index = hashFunc(key);\nbuckets.set(index, pair);\n}\n/* \u5220\u9664\u64cd\u4f5c */\npublic void remove(int key) {\nint index = hashFunc(key);\n// \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\nbuckets.set(index, null);\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\npublic List<Pair> pairSet() {\nList<Pair> pairSet = new ArrayList<>();\nfor (Pair pair : buckets) {\nif (pair != null)\npairSet.add(pair);\n}\nreturn pairSet;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\npublic List<Integer> keySet() {\nList<Integer> keySet = new ArrayList<>();\nfor (Pair pair : buckets) {\nif (pair != null)\nkeySet.add(pair.key);\n}\nreturn keySet;\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\npublic List<String> valueSet() {\nList<String> valueSet = new ArrayList<>();\nfor (Pair pair : buckets) {\nif (pair != null)\nvalueSet.add(pair.val);\n}\nreturn valueSet;\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\npublic void print() {\nfor (Pair kv : pairSet()) {\nSystem.out.println(kv.key + \" -> \" + kv.val);\n}\n}\n}\n
    array_hash_map.cpp
    /* \u952e\u503c\u5bf9 */\nstruct Pair {\npublic:\nint key;\nstring val;\nPair(int key, string val) {\nthis->key = key;\nthis->val = val;\n}\n};\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\nprivate:\nvector<Pair *> buckets;\npublic:\nArrayHashMap() {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nbuckets = vector<Pair *>(100);\n}\n~ArrayHashMap() {\n// \u91ca\u653e\u5185\u5b58\nfor (const auto &bucket : buckets) {\ndelete bucket;\n}\nbuckets.clear();\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(int key) {\nint index = key % 100;\nreturn index;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nstring get(int key) {\nint index = hashFunc(key);\nPair *pair = buckets[index];\nif (pair == nullptr)\nreturn nullptr;\nreturn pair->val;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(int key, string val) {\nPair *pair = new Pair(key, val);\nint index = hashFunc(key);\nbuckets[index] = pair;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid remove(int key) {\nint index = hashFunc(key);\n// \u91ca\u653e\u5185\u5b58\u5e76\u7f6e\u4e3a nullptr\ndelete buckets[index];\nbuckets[index] = nullptr;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\nvector<Pair *> pairSet() {\nvector<Pair *> pairSet;\nfor (Pair *pair : buckets) {\nif (pair != nullptr) {\npairSet.push_back(pair);\n}\n}\nreturn pairSet;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\nvector<int> keySet() {\nvector<int> keySet;\nfor (Pair *pair : buckets) {\nif (pair != nullptr) {\nkeySet.push_back(pair->key);\n}\n}\nreturn keySet;\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\nvector<string> valueSet() {\nvector<string> valueSet;\nfor (Pair *pair : buckets) {\nif (pair != nullptr) {\nvalueSet.push_back(pair->val);\n}\n}\nreturn valueSet;\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print() {\nfor (Pair *kv : pairSet()) {\ncout << kv->key << \" -> \" << kv->val << endl;\n}\n}\n};\n
    array_hash_map.py
    class Pair:\n\"\"\"\u952e\u503c\u5bf9\"\"\"\ndef __init__(self, key: int, val: str):\nself.key = key\nself.val = val\nclass ArrayHashMap:\n\"\"\"\u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868\"\"\"\ndef __init__(self):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n# \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nself.buckets: list[Pair | None] = [None] * 100\ndef hash_func(self, key: int) -> int:\n\"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\nindex = key % 100\nreturn index\ndef get(self, key: int) -> str:\n\"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\nindex: int = self.hash_func(key)\npair: Pair = self.buckets[index]\nif pair is None:\nreturn None\nreturn pair.val\ndef put(self, key: int, val: str):\n\"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"\npair = Pair(key, val)\nindex: int = self.hash_func(key)\nself.buckets[index] = pair\ndef remove(self, key: int):\n\"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\nindex: int = self.hash_func(key)\n# \u7f6e\u4e3a None \uff0c\u4ee3\u8868\u5220\u9664\nself.buckets[index] = None\ndef entry_set(self) -> list[Pair]:\n\"\"\"\u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9\"\"\"\nresult: list[Pair] = []\nfor pair in self.buckets:\nif pair is not None:\nresult.append(pair)\nreturn result\ndef key_set(self) -> list[int]:\n\"\"\"\u83b7\u53d6\u6240\u6709\u952e\"\"\"\nresult = []\nfor pair in self.buckets:\nif pair is not None:\nresult.append(pair.key)\nreturn result\ndef value_set(self) -> list[str]:\n\"\"\"\u83b7\u53d6\u6240\u6709\u503c\"\"\"\nresult = []\nfor pair in self.buckets:\nif pair is not None:\nresult.append(pair.val)\nreturn result\ndef print(self):\n\"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\nfor pair in self.buckets:\nif pair is not None:\nprint(pair.key, \"->\", pair.val)\n
    array_hash_map.go
    /* \u952e\u503c\u5bf9 */\ntype pair struct {\nkey int\nval string\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\ntype arrayHashMap struct {\nbuckets []*pair\n}\n/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nfunc newArrayHashMap() *arrayHashMap {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nbuckets := make([]*pair, 100)\nreturn &arrayHashMap{buckets: buckets}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (a *arrayHashMap) hashFunc(key int) int {\nindex := key % 100\nreturn index\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (a *arrayHashMap) get(key int) string {\nindex := a.hashFunc(key)\npair := a.buckets[index]\nif pair == nil {\nreturn \"Not Found\"\n}\nreturn pair.val\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (a *arrayHashMap) put(key int, val string) {\npair := &pair{key: key, val: val}\nindex := a.hashFunc(key)\na.buckets[index] = pair\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (a *arrayHashMap) remove(key int) {\nindex := a.hashFunc(key)\n// \u7f6e\u4e3a nil \uff0c\u4ee3\u8868\u5220\u9664\na.buckets[index] = nil\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u5bf9 */\nfunc (a *arrayHashMap) pairSet() []*pair {\nvar pairs []*pair\nfor _, pair := range a.buckets {\nif pair != nil {\npairs = append(pairs, pair)\n}\n}\nreturn pairs\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\nfunc (a *arrayHashMap) keySet() []int {\nvar keys []int\nfor _, pair := range a.buckets {\nif pair != nil {\nkeys = append(keys, pair.key)\n}\n}\nreturn keys\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\nfunc (a *arrayHashMap) valueSet() []string {\nvar values []string\nfor _, pair := range a.buckets {\nif pair != nil {\nvalues = append(values, pair.val)\n}\n}\nreturn values\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (a *arrayHashMap) print() {\nfor _, pair := range a.buckets {\nif pair != nil {\nfmt.Println(pair.key, \"->\", pair.val)\n}\n}\n}\n
    array_hash_map.js
    /* \u952e\u503c\u5bf9 Number -> String */\nclass Pair {\nconstructor(key, val) {\nthis.key = key;\nthis.val = val;\n}\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n#buckets;\nconstructor() {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nthis.#buckets = new Array(100).fill(null);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\n#hashFunc(key) {\nreturn key % 100;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nget(key) {\nlet index = this.#hashFunc(key);\nlet pair = this.#buckets[index];\nif (pair === null) return null;\nreturn pair.val;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nset(key, val) {\nlet index = this.#hashFunc(key);\nthis.#buckets[index] = new Pair(key, val);\n}\n/* \u5220\u9664\u64cd\u4f5c */\ndelete(key) {\nlet index = this.#hashFunc(key);\n// \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\nthis.#buckets[index] = null;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\nentries() {\nlet arr = [];\nfor (let i = 0; i < this.#buckets.length; i++) {\nif (this.#buckets[i]) {\narr.push(this.#buckets[i]);\n}\n}\nreturn arr;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\nkeys() {\nlet arr = [];\nfor (let i = 0; i < this.#buckets.length; i++) {\nif (this.#buckets[i]) {\narr.push(this.#buckets[i]?.key);\n}\n}\nreturn arr;\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\nvalues() {\nlet arr = [];\nfor (let i = 0; i < this.#buckets.length; i++) {\nif (this.#buckets[i]) {\narr.push(this.#buckets[i]?.val);\n}\n}\nreturn arr;\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nprint() {\nlet pairSet = this.entries();\nfor (const pair of pairSet) {\nif (!pair) continue;\nconsole.info(`${pair.key} -> ${pair.val}`);\n}\n}\n}\n
    array_hash_map.ts
    /* \u952e\u503c\u5bf9 Number -> String */\nclass Pair {\npublic key: number;\npublic val: string;\nconstructor(key: number, val: string) {\nthis.key = key;\nthis.val = val;\n}\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\nprivate readonly buckets: (Pair | null)[];\nconstructor() {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nthis.buckets = new Array(100).fill(null);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nprivate hashFunc(key: number): number {\nreturn key % 100;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npublic get(key: number): string | null {\nlet index = this.hashFunc(key);\nlet pair = this.buckets[index];\nif (pair === null) return null;\nreturn pair.val;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\npublic set(key: number, val: string) {\nlet index = this.hashFunc(key);\nthis.buckets[index] = new Pair(key, val);\n}\n/* \u5220\u9664\u64cd\u4f5c */\npublic delete(key: number) {\nlet index = this.hashFunc(key);\n// \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\nthis.buckets[index] = null;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\npublic entries(): (Pair | null)[] {\nlet arr: (Pair | null)[] = [];\nfor (let i = 0; i < this.buckets.length; i++) {\nif (this.buckets[i]) {\narr.push(this.buckets[i]);\n}\n}\nreturn arr;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\npublic keys(): (number | undefined)[] {\nlet arr: (number | undefined)[] = [];\nfor (let i = 0; i < this.buckets.length; i++) {\nif (this.buckets[i]) {\narr.push(this.buckets[i]?.key);\n}\n}\nreturn arr;\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\npublic values(): (string | undefined)[] {\nlet arr: (string | undefined)[] = [];\nfor (let i = 0; i < this.buckets.length; i++) {\nif (this.buckets[i]) {\narr.push(this.buckets[i]?.val);\n}\n}\nreturn arr;\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\npublic print() {\nlet pairSet = this.entries();\nfor (const pair of pairSet) {\nif (!pair) continue;\nconsole.info(`${pair.key} -> ${pair.val}`);\n}\n}\n}\n
    array_hash_map.c
    /* \u952e\u503c\u5bf9 int->string */\nstruct pair {\nint key;\nchar *val;\n};\ntypedef struct pair pair;\n[class]{arrayHashMap}-[func]{}\n
    array_hash_map.cs
    /* \u952e\u503c\u5bf9 int->string */\nclass Pair {\npublic int key;\npublic string val;\npublic Pair(int key, string val) {\nthis.key = key;\nthis.val = val;\n}\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\nprivate List<Pair?> buckets;\npublic ArrayHashMap() {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nbuckets = new();\nfor (int i = 0; i < 100; i++) {\nbuckets.Add(null);\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nprivate int hashFunc(int key) {\nint index = key % 100;\nreturn index;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npublic string? get(int key) {\nint index = hashFunc(key);\nPair? pair = buckets[index];\nif (pair == null) return null;\nreturn pair.val;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\npublic void put(int key, string val) {\nPair pair = new Pair(key, val);\nint index = hashFunc(key);\nbuckets[index] = pair;\n}\n/* \u5220\u9664\u64cd\u4f5c */\npublic void remove(int key) {\nint index = hashFunc(key);\n// \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\nbuckets[index] = null;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\npublic List<Pair> pairSet() {\nList<Pair> pairSet = new();\nforeach (Pair? pair in buckets) {\nif (pair != null)\npairSet.Add(pair);\n}\nreturn pairSet;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\npublic List<int> keySet() {\nList<int> keySet = new();\nforeach (Pair? pair in buckets) {\nif (pair != null)\nkeySet.Add(pair.key);\n}\nreturn keySet;\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\npublic List<string> valueSet() {\nList<string> valueSet = new();\nforeach (Pair? pair in buckets) {\nif (pair != null)\nvalueSet.Add(pair.val);\n}\nreturn valueSet;\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\npublic void print() {\nforeach (Pair kv in pairSet()) {\nConsole.WriteLine(kv.key + \" -> \" + kv.val);\n}\n}\n}\n
    array_hash_map.swift
    /* \u952e\u503c\u5bf9 */\nclass Pair {\nvar key: Int\nvar val: String\ninit(key: Int, val: String) {\nself.key = key\nself.val = val\n}\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\nprivate var buckets: [Pair?] = []\ninit() {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nfor _ in 0 ..< 100 {\nbuckets.append(nil)\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nprivate func hashFunc(key: Int) -> Int {\nlet index = key % 100\nreturn index\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc get(key: Int) -> String? {\nlet index = hashFunc(key: key)\nlet pair = buckets[index]\nreturn pair?.val\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc put(key: Int, val: String) {\nlet pair = Pair(key: key, val: val)\nlet index = hashFunc(key: key)\nbuckets[index] = pair\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfunc remove(key: Int) {\nlet index = hashFunc(key: key)\n// \u7f6e\u4e3a nil \uff0c\u4ee3\u8868\u5220\u9664\nbuckets[index] = nil\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\nfunc pairSet() -> [Pair] {\nvar pairSet: [Pair] = []\nfor pair in buckets {\nif let pair = pair {\npairSet.append(pair)\n}\n}\nreturn pairSet\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\nfunc keySet() -> [Int] {\nvar keySet: [Int] = []\nfor pair in buckets {\nif let pair = pair {\nkeySet.append(pair.key)\n}\n}\nreturn keySet\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\nfunc valueSet() -> [String] {\nvar valueSet: [String] = []\nfor pair in buckets {\nif let pair = pair {\nvalueSet.append(pair.val)\n}\n}\nreturn valueSet\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc print() {\nfor pair in pairSet() {\nSwift.print(\"\\(pair.key) -> \\(pair.val)\")\n}\n}\n}\n
    array_hash_map.zig
    // \u952e\u503c\u5bf9\nconst Pair = struct {\nkey: usize = undefined,\nval: []const u8 = undefined,\npub fn init(key: usize, val: []const u8) Pair {\nreturn Pair {\n.key = key,\n.val = val,\n};\n}\n};\n// \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868\nfn ArrayHashMap(comptime T: type) type {\nreturn struct {\nbuckets: ?std.ArrayList(?T) = null,\nmem_allocator: std.mem.Allocator = undefined,\nconst Self = @This();\n// \u6784\u9020\u65b9\u6cd5\npub fn init(self: *Self, allocator: std.mem.Allocator) !void {\nself.mem_allocator = allocator;\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nself.buckets = std.ArrayList(?T).init(self.mem_allocator);\nvar i: i32 = 0;\nwhile (i < 100) : (i += 1) {\ntry self.buckets.?.append(null);\n}\n}\n// \u6790\u6784\u65b9\u6cd5\npub fn deinit(self: *Self) void {\nif (self.buckets != null) self.buckets.?.deinit();\n}\n// \u54c8\u5e0c\u51fd\u6570\nfn hashFunc(key: usize) usize {\nvar index = key % 100;\nreturn index;\n}\n// \u67e5\u8be2\u64cd\u4f5c\npub fn get(self: *Self, key: usize) []const u8 {\nvar index = hashFunc(key);\nvar pair = self.buckets.?.items[index];\nreturn pair.?.val;\n}\n// \u6dfb\u52a0\u64cd\u4f5c\npub fn put(self: *Self, key: usize, val: []const u8) !void {\nvar pair = Pair.init(key, val);\nvar index = hashFunc(key);\nself.buckets.?.items[index] = pair;\n}\n// \u5220\u9664\u64cd\u4f5c\npub fn remove(self: *Self, key: usize) !void {\nvar index = hashFunc(key);\n// \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\nself.buckets.?.items[index] = null;\n}       // \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9\npub fn pairSet(self: *Self) !*std.ArrayList(T) {\nvar entry_set = std.ArrayList(T).init(self.mem_allocator);\nfor (self.buckets.?.items) |item| {\nif (item == null) continue;\ntry entry_set.append(item.?);\n}\nreturn &entry_set;\n}  // \u83b7\u53d6\u6240\u6709\u952e\npub fn keySet(self: *Self) !*std.ArrayList(usize) {\nvar key_set = std.ArrayList(usize).init(self.mem_allocator);\nfor (self.buckets.?.items) |item| {\nif (item == null) continue;\ntry key_set.append(item.?.key);\n}\nreturn &key_set;\n}  // \u83b7\u53d6\u6240\u6709\u503c\npub fn valueSet(self: *Self) !*std.ArrayList([]const u8) {\nvar value_set = std.ArrayList([]const u8).init(self.mem_allocator);\nfor (self.buckets.?.items) |item| {\nif (item == null) continue;\ntry value_set.append(item.?.val);\n}\nreturn &value_set;\n}\n// \u6253\u5370\u54c8\u5e0c\u8868\npub fn print(self: *Self) !void {\nvar entry_set = try self.pairSet();\ndefer entry_set.deinit();\nfor (entry_set.items) |item| {\nstd.debug.print(\"{} -> {s}\\n\", .{item.key, item.val});\n}\n}\n};\n}\n
    array_hash_map.dart
    /* \u952e\u503c\u5bf9 */\nclass Pair {\nint key;\nString val;\nPair(this.key, this.val);\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\nlate List<Pair?> _buckets;\nArrayHashMap() {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n_buckets = List.filled(100, null);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint _hashFunc(int key) {\nfinal int index = key % 100;\nreturn index;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nString? get(int key) {\nfinal int index = _hashFunc(key);\nfinal Pair? pair = _buckets[index];\nif (pair == null) {\nreturn null;\n}\nreturn pair.val;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(int key, String val) {\nfinal Pair pair = Pair(key, val);\nfinal int index = _hashFunc(key);\n_buckets[index] = pair;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid remove(int key) {\nfinal int index = _hashFunc(key);\n_buckets[index] = null;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\nList<Pair> pairSet() {\nList<Pair> pairSet = [];\nfor (final Pair? pair in _buckets) {\nif (pair != null) {\npairSet.add(pair);\n}\n}\nreturn pairSet;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\nList<int> keySet() {\nList<int> keySet = [];\nfor (final Pair? pair in _buckets) {\nif (pair != null) {\nkeySet.add(pair.key);\n}\n}\nreturn keySet;\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\nList<String> values() {\nList<String> valueSet = [];\nfor (final Pair? pair in _buckets) {\nif (pair != null) {\nvalueSet.add(pair.val);\n}\n}\nreturn valueSet;\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid printHashMap() {\nfor (final Pair kv in pairSet()) {\nprint(\"${kv.key} -> ${kv.val}\");\n}\n}\n}\n
    "},{"location":"chapter_hashing/hash_map/#613","title":"6.1.3. \u00a0 \u54c8\u5e0c\u51b2\u7a81","text":"

    \u7ec6\u5fc3\u7684\u4f60\u53ef\u80fd\u5df2\u7ecf\u6ce8\u610f\u5230\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u54c8\u5e0c\u51fd\u6570 \\(f(x) = x \\bmod 100\\) \u53ef\u80fd\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5f53\u8f93\u5165\u7684 key \u540e\u4e24\u4f4d\u76f8\u540c\u65f6\uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u8ba1\u7b97\u7ed3\u679c\u4e5f\u4f1a\u76f8\u540c\uff0c\u4ece\u800c\u6307\u5411\u540c\u4e00\u4e2a value \u3002\u4f8b\u5982\uff0c\u67e5\u8be2\u5b66\u53f7\u4e3a \\(12836\\) \u548c \\(20336\\) \u7684\u4e24\u4e2a\u5b66\u751f\u65f6\uff0c\u6211\u4eec\u5f97\u5230\uff1a

    \\[ f(12836) = f(20336) = 36 \\]

    \u8fd9\u4e24\u4e2a\u5b66\u53f7\u6307\u5411\u4e86\u540c\u4e00\u4e2a\u59d3\u540d\uff0c\u8fd9\u663e\u7136\u662f\u9519\u8bef\u7684\u3002\u6211\u4eec\u628a\u8fd9\u79cd\u60c5\u51b5\u79f0\u4e3a\u300c\u54c8\u5e0c\u51b2\u7a81 Hash Collision\u300d\u3002\u5728\u540e\u7eed\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5982\u4f55\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u7684\u95ee\u9898\u3002

    Fig. \u54c8\u5e0c\u51b2\u7a81\u793a\u4f8b

    \u7efc\u4e0a\u6240\u8ff0\uff0c\u4e00\u4e2a\u4f18\u79c0\u7684\u54c8\u5e0c\u51fd\u6570\u5e94\u5177\u5907\u4ee5\u4e0b\u7279\u6027\uff1a

    • \u5c3d\u53ef\u80fd\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\u7684\u53d1\u751f\uff1b
    • \u67e5\u8be2\u6548\u7387\u9ad8\u4e14\u7a33\u5b9a\uff0c\u80fd\u591f\u5728\u7edd\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u8fbe\u5230 \\(O(1)\\) \u65f6\u95f4\u590d\u6742\u5ea6\uff1b
    • \u8f83\u9ad8\u7684\u7a7a\u95f4\u5229\u7528\u7387\uff0c\u5373\u4f7f\u201c\u952e\u503c\u5bf9\u5360\u7528\u7a7a\u95f4 / \u54c8\u5e0c\u8868\u603b\u5360\u7528\u7a7a\u95f4\u201d\u6bd4\u4f8b\u6700\u5927\u5316\uff1b
    "},{"location":"chapter_hashing/summary/","title":"6.3. \u00a0 \u5c0f\u7ed3","text":"
    • \u54c8\u5e0c\u8868\u80fd\u591f\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u5c06\u952e key \u6620\u5c04\u5230\u503c value\uff0c\u6548\u7387\u975e\u5e38\u9ad8\u3002
    • \u5e38\u89c1\u7684\u54c8\u5e0c\u8868\u64cd\u4f5c\u5305\u62ec\u67e5\u8be2\u3001\u6dfb\u52a0\u4e0e\u5220\u9664\u952e\u503c\u5bf9\u3001\u904d\u5386\u952e\u503c\u5bf9\u7b49\u3002
    • \u54c8\u5e0c\u51fd\u6570\u5c06 key \u6620\u5c04\u4e3a\u6570\u7ec4\u7d22\u5f15\uff08\u6876\u7d22\u5f15\uff09\uff0c\u4ece\u800c\u8bbf\u95ee\u5bf9\u5e94\u7684\u503c value \u3002
    • \u4e24\u4e2a\u4e0d\u540c\u7684 key \u53ef\u80fd\u5728\u7ecf\u8fc7\u54c8\u5e0c\u51fd\u6570\u540e\u5f97\u5230\u76f8\u540c\u7684\u7d22\u5f15\uff0c\u5bfc\u81f4\u67e5\u8be2\u7ed3\u679c\u51fa\u9519\uff0c\u8fd9\u79cd\u73b0\u8c61\u88ab\u79f0\u4e3a\u54c8\u5e0c\u51b2\u7a81\u3002
    • \u7f13\u89e3\u54c8\u5e0c\u51b2\u7a81\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u6269\u5bb9\u54c8\u5e0c\u8868\u548c\u4f18\u5316\u54c8\u5e0c\u8868\u7684\u8868\u793a\u65b9\u6cd5\u3002
    • \u8d1f\u8f7d\u56e0\u5b50\u5b9a\u4e49\u4e3a\u54c8\u5e0c\u8868\u4e2d\u5143\u7d20\u6570\u91cf\u9664\u4ee5\u6876\u6570\u91cf\uff0c\u53cd\u6620\u4e86\u54c8\u5e0c\u51b2\u7a81\u7684\u4e25\u91cd\u7a0b\u5ea6\uff0c\u5e38\u7528\u4f5c\u89e6\u53d1\u54c8\u5e0c\u8868\u6269\u5bb9\u7684\u6761\u4ef6\u3002\u4e0e\u6570\u7ec4\u6269\u5bb9\u7c7b\u4f3c\uff0c\u54c8\u5e0c\u8868\u6269\u5bb9\u64cd\u4f5c\u4e5f\u4f1a\u4ea7\u751f\u8f83\u5927\u7684\u5f00\u9500\u3002
    • \u94fe\u5f0f\u5730\u5740\u901a\u8fc7\u5c06\u5355\u4e2a\u5143\u7d20\u8f6c\u5316\u4e3a\u94fe\u8868\uff0c\u5c06\u6240\u6709\u51b2\u7a81\u5143\u7d20\u5b58\u50a8\u5728\u540c\u4e00\u4e2a\u94fe\u8868\u4e2d\uff0c\u4ece\u800c\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u3002\u7136\u800c\uff0c\u8fc7\u957f\u7684\u94fe\u8868\u4f1a\u964d\u4f4e\u67e5\u8be2\u6548\u7387\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u94fe\u8868\u8f6c\u6362\u4e3a AVL \u6811\u6216\u7ea2\u9ed1\u6811\u6765\u6539\u5584\u3002
    • \u5f00\u653e\u5bfb\u5740\u901a\u8fc7\u591a\u6b21\u63a2\u6d4b\u6765\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u3002\u7ebf\u6027\u63a2\u6d4b\u4f7f\u7528\u56fa\u5b9a\u6b65\u957f\uff0c\u7f3a\u70b9\u662f\u4e0d\u80fd\u5220\u9664\u5143\u7d20\u4e14\u5bb9\u6613\u4ea7\u751f\u805a\u96c6\u3002\u591a\u6b21\u54c8\u5e0c\u4f7f\u7528\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570\u8fdb\u884c\u63a2\u6d4b\uff0c\u76f8\u5bf9\u7ebf\u6027\u63a2\u6d4b\u4e0d\u6613\u4ea7\u751f\u805a\u96c6\uff0c\u4f46\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570\u589e\u52a0\u4e86\u8ba1\u7b97\u91cf\u3002
    • \u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u91c7\u53d6\u4e86\u4e0d\u540c\u7684\u54c8\u5e0c\u8868\u5b9e\u73b0\u7b56\u7565\u3002\u4f8b\u5982\uff0cJava \u7684 HashMap \u4f7f\u7528\u94fe\u5f0f\u5730\u5740\uff0c\u800c Python \u7684 Dict \u91c7\u7528\u5f00\u653e\u5bfb\u5740\u3002
    "},{"location":"chapter_heap/","title":"8. \u00a0 \u5806","text":""},{"location":"chapter_heap/build_heap/","title":"8.2. \u00a0 \u5efa\u5806\u64cd\u4f5c","text":"

    \u5982\u679c\u6211\u4eec\u60f3\u8981\u6839\u636e\u8f93\u5165\u5217\u8868\u751f\u6210\u4e00\u4e2a\u5806\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a\u300c\u5efa\u5806\u300d\u3002

    "},{"location":"chapter_heap/build_heap/#821","title":"8.2.1. \u00a0 \u501f\u52a9\u5165\u5806\u65b9\u6cd5\u5b9e\u73b0","text":"

    \u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u501f\u52a9\u201c\u5143\u7d20\u5165\u5806\u64cd\u4f5c\u201d\u5b9e\u73b0\uff0c\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u7a7a\u5806\uff0c\u7136\u540e\u5c06\u5217\u8868\u5143\u7d20\u4f9d\u6b21\u6dfb\u52a0\u5230\u5806\u4e2d\u3002

    \u8bbe\u5143\u7d20\u6570\u91cf\u4e3a \\(n\\) \uff0c\u5219\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u5165\u5806\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u3002\u5728\u4f9d\u6b21\u6dfb\u52a0\u5143\u7d20\u65f6\uff0c\u5806\u7684\u5e73\u5747\u957f\u5ea6\u4e3a \\(\\frac{n}{2}\\) \uff0c\u56e0\u6b64\u8be5\u65b9\u6cd5\u7684\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002

    "},{"location":"chapter_heap/build_heap/#822","title":"8.2.2. \u00a0 \u57fa\u4e8e\u5806\u5316\u64cd\u4f5c\u5b9e\u73b0","text":"

    \u6709\u8da3\u7684\u662f\uff0c\u5b58\u5728\u4e00\u79cd\u66f4\u9ad8\u6548\u7684\u5efa\u5806\u65b9\u6cd5\uff0c\u5176\u65f6\u95f4\u590d\u6742\u5ea6\u4ec5\u4e3a \\(O(n)\\) \u3002\u6211\u4eec\u5148\u5c06\u5217\u8868\u6240\u6709\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u5230\u5806\u4e2d\uff0c\u7136\u540e\u8fed\u4ee3\u5730\u5bf9\u5404\u4e2a\u8282\u70b9\u6267\u884c\u201c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u201d\u3002\u5f53\u7136\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u5bf9\u53f6\u8282\u70b9\u6267\u884c\u5806\u5316\u64cd\u4f5c\uff0c\u56e0\u4e3a\u5b83\u4eec\u6ca1\u6709\u5b50\u8282\u70b9\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nmaxHeap = new ArrayList<>(nums);\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor (int i = parent(size() - 1); i >= 0; i--) {\nsiftDown(i);\n}\n}\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\nmaxHeap = nums;\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor (int i = parent(size() - 1); i >= 0; i--) {\nsiftDown(i);\n}\n}\n
    my_heap.py
    def __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\nself.max_heap = nums\n# \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor i in range(self.parent(self.size() - 1), -1, -1):\nself.sift_down(i)\n
    my_heap.go
    /* \u6784\u9020\u65b9\u6cd5\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\nh := &maxHeap{data: nums}\nfor i := len(h.data) - 1; i >= 0; i-- {\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nh.siftDown(i)\n}\nreturn h\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\nthis.#maxHeap = nums === undefined ? [] : [...nums];\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor (let i = this.#parent(this.size() - 1); i >= 0; i--) {\nthis.#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\nthis.maxHeap = nums === undefined ? [] : [...nums];\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor (let i = this.parent(this.size() - 1); i >= 0; i--) {\nthis.siftDown(i);\n}\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\nmaxHeap *h = (maxHeap *)malloc(sizeof(maxHeap));\nh->size = size;\nmemcpy(h->data, nums, size * sizeof(int));\nfor (int i = size - 1; i >= 0; i--) {\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nsiftDown(h, i);\n}\nreturn h;\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\nmaxHeap = new List<int>(nums);\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nvar size = parent(this.size() - 1);\nfor (int i = size; i >= 0; i--) {\nsiftDown(i);\n}\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\nmaxHeap = nums\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor i in stride(from: parent(i: size() - 1), through: 0, by: -1) {\nsiftDown(i: i)\n}\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 {\nif (self.max_heap != null) return;\nself.max_heap = std.ArrayList(T).init(allocator);\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\ntry self.max_heap.?.appendSlice(nums);\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nvar i: usize = parent(self.size() - 1) + 1;\nwhile (i > 0) : (i -= 1) {\ntry self.siftDown(i - 1);\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\nfor (int i = _parent(size() - 1); i >= 0; i--) {\n_siftDown(i);\n}\n}\n
    "},{"location":"chapter_heap/build_heap/#823","title":"8.2.3. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"

    \u4e3a\u4ec0\u4e48\u7b2c\u4e8c\u79cd\u5efa\u5806\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(n)\\) \uff1f\u6211\u4eec\u6765\u5c55\u5f00\u63a8\u7b97\u4e00\u4e0b\u3002

    • \u5b8c\u5168\u4e8c\u53c9\u6811\u4e2d\uff0c\u8bbe\u8282\u70b9\u603b\u6570\u4e3a \\(n\\) \uff0c\u5219\u53f6\u8282\u70b9\u6570\u91cf\u4e3a \\((n + 1) / 2\\) \uff0c\u5176\u4e2d \\(/\\) \u4e3a\u5411\u4e0b\u6574\u9664\u3002\u56e0\u6b64\uff0c\u5728\u6392\u9664\u53f6\u8282\u70b9\u540e\uff0c\u9700\u8981\u5806\u5316\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\((n - 1)/2\\) \uff0c\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff1b
    • \u5728\u4ece\u9876\u81f3\u5e95\u5806\u5316\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6bcf\u4e2a\u8282\u70b9\u6700\u591a\u5806\u5316\u5230\u53f6\u8282\u70b9\uff0c\u56e0\u6b64\u6700\u5927\u8fed\u4ee3\u6b21\u6570\u4e3a\u4e8c\u53c9\u6811\u9ad8\u5ea6 \\(O(\\log n)\\) \uff1b

    \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\u7136\u800c\uff0c\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\u7279\u6027\u3002

    \u63a5\u4e0b\u6765\u6211\u4eec\u6765\u8fdb\u884c\u66f4\u4e3a\u8be6\u7ec6\u7684\u8ba1\u7b97\u3002\u4e3a\u4e86\u51cf\u5c0f\u8ba1\u7b97\u96be\u5ea6\uff0c\u6211\u4eec\u5047\u8bbe\u6811\u662f\u4e00\u4e2a\u201c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u201d\uff0c\u8be5\u5047\u8bbe\u4e0d\u4f1a\u5f71\u54cd\u8ba1\u7b97\u7ed3\u679c\u7684\u6b63\u786e\u6027\u3002\u8bbe\u4e8c\u53c9\u6811\uff08\u5373\u5806\uff09\u8282\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u6811\u9ad8\u5ea6\u4e3a \\(h\\) \u3002\u4e0a\u6587\u63d0\u5230\uff0c\u8282\u70b9\u5806\u5316\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

    Fig. \u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u5404\u5c42\u8282\u70b9\u6570\u91cf

    \u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5404\u5c42\u7684\u201c\u8282\u70b9\u6570\u91cf \\(\\times\\) \u8282\u70b9\u9ad8\u5ea6\u201d\u6c42\u548c\uff0c\u4ece\u800c\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) + \\cdots + 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

    \\[ \\begin{aligned} T(h) & = 2^0h + 2^1(h-1) + 2^2(h-2) + \\cdots + 2^{h-1}\\times1 \\newline 2 T(h) & = 2^1h + 2^2(h-1) + 2^3(h-2) + \\cdots + 2^{h}\\times1 \\newline \\end{aligned} \\]

    \u4f7f\u7528\u9519\u4f4d\u76f8\u51cf\u6cd5\uff0c\u4ee4\u4e0b\u5f0f \\(2 T(h)\\) \u51cf\u53bb\u4e0a\u5f0f \\(T(h)\\) \uff0c\u53ef\u5f97

    \\[ 2T(h) - T(h) = T(h) = -2^0h + 2^1 + 2^2 + \\cdots + 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

    \\[ \\begin{aligned} T(h) & = 2 \\frac{1 - 2^h}{1 - 2} - h \\newline & = 2^{h+1} - h \\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\u53ef\u5206\u4e3a\u4e24\u79cd\u7c7b\u578b\uff1a

    • \u300c\u5927\u9876\u5806 Max Heap\u300d\uff0c\u4efb\u610f\u8282\u70b9\u7684\u503c \\(\\geq\\) \u5176\u5b50\u8282\u70b9\u7684\u503c\uff1b
    • \u300c\u5c0f\u9876\u5806 Min Heap\u300d\uff0c\u4efb\u610f\u8282\u70b9\u7684\u503c \\(\\leq\\) \u5176\u5b50\u8282\u70b9\u7684\u503c\uff1b

    Fig. \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\uff1a

    • \u6700\u5e95\u5c42\u8282\u70b9\u9760\u5de6\u586b\u5145\uff0c\u5176\u4ed6\u5c42\u7684\u8282\u70b9\u90fd\u88ab\u586b\u6ee1\u3002
    • \u6211\u4eec\u5c06\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\u79f0\u4e3a\u300c\u5806\u9876\u300d\uff0c\u5c06\u5e95\u5c42\u6700\u9760\u53f3\u7684\u8282\u70b9\u79f0\u4e3a\u300c\u5806\u5e95\u300d\u3002
    • \u5bf9\u4e8e\u5927\u9876\u5806\uff08\u5c0f\u9876\u5806\uff09\uff0c\u5806\u9876\u5143\u7d20\uff08\u5373\u6839\u8282\u70b9\uff09\u7684\u503c\u5206\u522b\u662f\u6700\u5927\uff08\u6700\u5c0f\uff09\u7684\u3002
    "},{"location":"chapter_heap/heap/#811","title":"8.1.1. \u00a0 \u5806\u5e38\u7528\u64cd\u4f5c","text":"

    \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\u4f5c\u5b9e\u73b0\u4f18\u5148\u961f\u5217\uff0c\u5927\u9876\u5806\u76f8\u5f53\u4e8e\u5143\u7d20\u6309\u4ece\u5927\u5230\u5c0f\u987a\u5e8f\u51fa\u961f\u7684\u4f18\u5148\u961f\u5217\u3002\u4ece\u4f7f\u7528\u89d2\u5ea6\u6765\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u300c\u4f18\u5148\u961f\u5217\u300d\u548c\u300c\u5806\u300d\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\u4f7f\u7528\u300c\u5806\u300d\u6765\u547d\u540d\u3002

    \u5806\u7684\u5e38\u7528\u64cd\u4f5c\u89c1\u4e0b\u8868\uff0c\u65b9\u6cd5\u540d\u9700\u8981\u6839\u636e\u7f16\u7a0b\u8bed\u8a00\u6765\u786e\u5b9a\u3002

    \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

    Tip

    \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\u4fee\u6539 Comparator \u6765\u5b9e\u73b0\u201c\u5c0f\u9876\u5806\u201d\u4e0e\u201c\u5927\u9876\u5806\u201d\u4e4b\u95f4\u7684\u8f6c\u6362\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.offer(1);\nmaxHeap.offer(3);\nmaxHeap.offer(2);\nmaxHeap.offer(5);\nmaxHeap.offer(4);\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.peek(); // 5\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 = heap.poll();  // 5\npeek = heap.poll();  // 4\npeek = heap.poll();  // 3\npeek = heap.poll();  // 2\npeek = heap.poll();  // 1\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.size();\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = maxHeap.isEmpty();\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nminHeap = new PriorityQueue<>(Arrays.asList(1, 3, 2, 5, 4));\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/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.push(1);\nmaxHeap.push(3);\nmaxHeap.push(2);\nmaxHeap.push(5);\nmaxHeap.push(4);\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.top(); // 5\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/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.size();\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = maxHeap.empty();\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.py
    # \u521d\u59cb\u5316\u5c0f\u9876\u5806\nmin_heap, flag = [], 1\n# \u521d\u59cb\u5316\u5927\u9876\u5806\nmax_heap, flag = [], -1\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# \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# \u83b7\u53d6\u5806\u9876\u5143\u7d20\npeek: int = flag * max_heap[0] # 5\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# \u83b7\u53d6\u5806\u5927\u5c0f\nsize: int = len(max_heap)\n# \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = not max_heap\n# \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806\nmin_heap: List[int] = [1, 3, 2, 5, 4]\nheapq.heapify(min_heap)\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// 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// 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\nlast := (*h)[len(*h)-1]\n*h = (*h)[:len(*h)-1]\nreturn last\n}\n// Len sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Len() int {\nreturn len(*h)\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\nreturn (*h)[i].(int) > (*h)[j].(int)\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// Top \u83b7\u53d6\u5806\u9876\u5143\u7d20\nfunc (h *intHeap) Top() any {\nreturn (*h)[0]\n}\n/* Driver Code */\nfunc TestHeap(t *testing.T) {\n/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5927\u9876\u5806\nmaxHeap := &intHeap{}\nheap.Init(maxHeap)\n/* \u5143\u7d20\u5165\u5806 */\n// \u8c03\u7528 heap.Interface \u7684\u65b9\u6cd5\uff0c\u6765\u6dfb\u52a0\u5143\u7d20\nheap.Push(maxHeap, 1)\nheap.Push(maxHeap, 3)\nheap.Push(maxHeap, 2)\nheap.Push(maxHeap, 4)\nheap.Push(maxHeap, 5)\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\ntop := maxHeap.Top()\nfmt.Printf(\"\u5806\u9876\u5143\u7d20\u4e3a %d\\n\", top)\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u8c03\u7528 heap.Interface \u7684\u65b9\u6cd5\uff0c\u6765\u79fb\u9664\u5143\u7d20\nheap.Pop(maxHeap) // 5\nheap.Pop(maxHeap) // 4\nheap.Pop(maxHeap) // 3\nheap.Pop(maxHeap) // 2\nheap.Pop(maxHeap) // 1\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nsize := len(*maxHeap)\nfmt.Printf(\"\u5806\u5143\u7d20\u6570\u91cf\u4e3a %d\\n\", size)\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nisEmpty := len(*maxHeap) == 0\nfmt.Printf(\"\u5806\u662f\u5426\u4e3a\u7a7a %t\\n\", isEmpty)\n}\n
    heap.js
    // JavaScript \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
    heap.ts
    // TypeScript \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
    heap.c
    // C \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
    heap.cs
    /* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nPriorityQueue<int, int> minHeap = new PriorityQueue<int, int>();\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 PriorityQueue<int, int>(Comparer<int>.Create((x, y) => y - x));\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/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.Peek();//5\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/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.Count;\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = maxHeap.Count == 0;\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nminHeap = new PriorityQueue<int, int>(new List<(int, int)> { (1, 1), (3, 3), (2, 2), (5, 5), (4, 4), });\n
    heap.swift
    // Swift \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
    heap.zig
    \n
    heap.dart
    // Dart \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\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":"\u5806\u7684\u5b58\u50a8\u4e0e\u8868\u793a","text":"

    \u6211\u4eec\u5728\u4e8c\u53c9\u6811\u7ae0\u8282\u4e2d\u5b66\u4e60\u5230\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\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

    \u5177\u4f53\u800c\u8a00\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\u53d6\u6574\uff09\u3002\u5f53\u7d22\u5f15\u8d8a\u754c\u65f6\uff0c\u8868\u793a\u7a7a\u8282\u70b9\u6216\u8282\u70b9\u4e0d\u5b58\u5728\u3002

    Fig. \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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart my_heap.java
    /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nint right(int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nint parent(int i) {\nreturn (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n
    my_heap.cpp
    /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nint right(int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nint parent(int i) {\nreturn (i - 1) / 2; // \u5411\u4e0b\u53d6\u6574\n}\n
    my_heap.py
    def left(self, i: int) -> int:\n\"\"\"\u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15\"\"\"\nreturn 2 * i + 1\ndef right(self, i: int) -> int:\n\"\"\"\u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15\"\"\"\nreturn 2 * i + 2\ndef parent(self, i: int) -> int:\n\"\"\"\u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15\"\"\"\nreturn (i - 1) // 2  # \u5411\u4e0b\u6574\u9664\n
    my_heap.go
    /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nfunc (h *maxHeap) left(i int) int {\nreturn 2*i + 1\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nfunc (h *maxHeap) right(i int) int {\nreturn 2*i + 2\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nfunc (h *maxHeap) parent(i int) int {\n// \u5411\u4e0b\u6574\u9664\nreturn (i - 1) / 2\n}\n
    my_heap.js
    /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\n#left(i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\n#right(i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\n#parent(i) {\nreturn 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 {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nright(i: number): number {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nparent(i: number): number {\nreturn Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n}\n
    my_heap.c
    /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint left(maxHeap *h, int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nint right(maxHeap *h, int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nint parent(maxHeap *h, int i) {\nreturn (i - 1) / 2;\n}\n
    my_heap.cs
    /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nint right(int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nint parent(int i) {\nreturn (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n
    my_heap.swift
    /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nfunc left(i: Int) -> Int {\n2 * i + 1\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nfunc right(i: Int) -> Int {\n2 * i + 2\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.zig
    // \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15\nfn left(i: usize) usize {\nreturn 2 * i + 1;\n}\n// \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15\nfn right(i: usize) usize {\nreturn 2 * i + 2;\n}\n// \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15\nfn parent(i: usize) usize {\n// return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\nreturn @divFloor(i - 1, 2);\n}\n
    my_heap.dart
    /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint _left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nint _right(int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nint _parent(int i) {\nreturn (i - 1) ~/ 2; // \u5411\u4e0b\u6574\u9664\n}\n
    "},{"location":"chapter_heap/heap/#_2","title":"\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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart my_heap.java
    /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\nreturn maxHeap.get(0);\n}\n
    my_heap.cpp
    /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\nreturn maxHeap[0];\n}\n
    my_heap.py
    def peek(self) -> int:\n\"\"\"\u8bbf\u95ee\u5806\u9876\u5143\u7d20\"\"\"\nreturn self.max_heap[0]\n
    my_heap.go
    /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunc (h *maxHeap) peek() any {\nreturn h.data[0]\n}\n
    my_heap.js
    /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\npeek() {\nreturn this.#maxHeap[0];\n}\n
    my_heap.ts
    /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\npeek(): number {\nreturn this.maxHeap[0];\n}\n
    my_heap.c
    /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek(maxHeap *h) {\nreturn h->data[0];\n}\n
    my_heap.cs
    /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\nreturn maxHeap[0];\n}\n
    my_heap.swift
    /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunc peek() -> Int {\nmaxHeap[0]\n}\n
    my_heap.zig
    // \u8bbf\u95ee\u5806\u9876\u5143\u7d20\nfn peek(self: *Self) T {\nreturn self.max_heap.?.items[0];\n}  
    my_heap.dart
    /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\nreturn _maxHeap[0];\n}\n
    "},{"location":"chapter_heap/heap/#_3","title":"\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\u3002\u56e0\u6b64\uff0c\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\u5177\u4f53\u6765\u8bf4\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\u9700\u4ea4\u6362\u7684\u8282\u70b9\u65f6\u7ed3\u675f\u3002

    <1><2><3><4><5><6><7><8><9>

    \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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart my_heap.java
    /* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n// \u6dfb\u52a0\u8282\u70b9\nmaxHeap.add(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nsiftUp(size() - 1);\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\nwhile (true) {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nint p = parent(i);\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p < 0 || maxHeap.get(i) <= maxHeap.get(p))\nbreak;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n
    my_heap.cpp
    /* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n// \u6dfb\u52a0\u8282\u70b9\nmaxHeap.push_back(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nsiftUp(size() - 1);\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\nwhile (true) {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nint p = parent(i);\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p < 0 || maxHeap[i] <= maxHeap[p])\nbreak;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(maxHeap[i], maxHeap[p]);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n
    my_heap.py
    def push(self, val: int):\n\"\"\"\u5143\u7d20\u5165\u5806\"\"\"\n# \u6dfb\u52a0\u8282\u70b9\nself.max_heap.append(val)\n# \u4ece\u5e95\u81f3\u9876\u5806\u5316\nself.sift_up(self.size() - 1)\ndef sift_up(self, i: int):\n\"\"\"\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316\"\"\"\nwhile True:\n# \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\np = self.parent(i)\n# \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif p < 0 or self.max_heap[i] <= self.max_heap[p]:\nbreak\n# \u4ea4\u6362\u4e24\u8282\u70b9\nself.swap(i, p)\n# \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p\n
    my_heap.go
    /* \u5143\u7d20\u5165\u5806 */\nfunc (h *maxHeap) push(val any) {\n// \u6dfb\u52a0\u8282\u70b9\nh.data = append(h.data, val)\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nh.siftUp(len(h.data) - 1)\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfunc (h *maxHeap) siftUp(i int) {\nfor true {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\np := h.parent(i)\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif p < 0 || h.data[i].(int) <= h.data[p].(int) {\nbreak\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nh.swap(i, p)\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p\n}\n}\n
    my_heap.js
    /* \u5143\u7d20\u5165\u5806 */\npush(val) {\n// \u6dfb\u52a0\u8282\u70b9\nthis.#maxHeap.push(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nthis.#siftUp(this.size() - 1);\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\n#siftUp(i) {\nwhile (true) {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nconst p = this.#parent(i);\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p < 0 || this.#maxHeap[i] <= this.#maxHeap[p]) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nthis.#swap(i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n
    my_heap.ts
    /* \u5143\u7d20\u5165\u5806 */\npush(val: number): void {\n// \u6dfb\u52a0\u8282\u70b9\nthis.maxHeap.push(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nthis.siftUp(this.size() - 1);\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nsiftUp(i: number): void {\nwhile (true) {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nconst p = this.parent(i);\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p < 0 || this.maxHeap[i] <= this.maxHeap[p]) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nthis.swap(i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n
    my_heap.c
    /* \u5143\u7d20\u5165\u5806 */\nvoid push(maxHeap *h, int val) {\n// \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4e0d\u5e94\u8be5\u6dfb\u52a0\u8fd9\u4e48\u591a\u8282\u70b9\nif (h->size == MAX_SIZE) {\nprintf(\"heap is full!\");\nreturn;\n}\n// \u6dfb\u52a0\u8282\u70b9\nh->data[h->size] = val;\nh->size++;\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nsiftUp(h, h->size - 1);\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(maxHeap *h, int i) {\nwhile (true) {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nint p = parent(h, i);\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p < 0 || h->data[i] <= h->data[p]) {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(h, i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n
    my_heap.cs
    /* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n// \u6dfb\u52a0\u8282\u70b9\nmaxHeap.Add(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nsiftUp(size() - 1);\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\nwhile (true) {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nint p = parent(i);\n// \u82e5\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\uff0c\u5219\u7ed3\u675f\u5806\u5316\nif (p < 0 || maxHeap[i] <= maxHeap[p])\nbreak;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n
    my_heap.swift
    /* \u5143\u7d20\u5165\u5806 */\nfunc push(val: Int) {\n// \u6dfb\u52a0\u8282\u70b9\nmaxHeap.append(val)\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nsiftUp(i: size() - 1)\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfunc siftUp(i: Int) {\nvar i = i\nwhile true {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nlet p = parent(i: i)\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif p < 0 || maxHeap[i] <= maxHeap[p] {\nbreak\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(i: i, j: p)\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p\n}\n}\n
    my_heap.zig
    // \u5143\u7d20\u5165\u5806\nfn push(self: *Self, val: T) !void {\n// \u6dfb\u52a0\u8282\u70b9\ntry self.max_heap.?.append(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\ntry self.siftUp(self.size() - 1);\n}  // \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316\nfn siftUp(self: *Self, i_: usize) !void {\nvar i = i_;\nwhile (true) {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nvar p = parent(i);\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p < 0 or self.max_heap.?.items[i] <= self.max_heap.?.items[p]) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\ntry self.swap(i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = 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[class]{MaxHeap}-[func]{siftUp}\n
    "},{"location":"chapter_heap/heap/#_4","title":"\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\u4fee\u590d\u53d8\u5f97\u56f0\u96be\u3002\u4e3a\u4e86\u5c3d\u91cf\u51cf\u5c11\u5143\u7d20\u7d22\u5f15\u7684\u53d8\u52a8\uff0c\u6211\u4eec\u91c7\u53d6\u4ee5\u4e0b\u64cd\u4f5c\u6b65\u9aa4\uff1a

    1. \u4ea4\u6362\u5806\u9876\u5143\u7d20\u4e0e\u5806\u5e95\u5143\u7d20\uff08\u5373\u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff09\uff1b
    2. \u4ea4\u6362\u5b8c\u6210\u540e\uff0c\u5c06\u5806\u5e95\u4ece\u5217\u8868\u4e2d\u5220\u9664\uff08\u6ce8\u610f\uff0c\u7531\u4e8e\u5df2\u7ecf\u4ea4\u6362\uff0c\u5b9e\u9645\u4e0a\u5220\u9664\u7684\u662f\u539f\u6765\u7684\u5806\u9876\u5143\u7d20\uff09\uff1b
    3. \u4ece\u6839\u8282\u70b9\u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u6267\u884c\u5806\u5316\uff1b

    \u987e\u540d\u601d\u4e49\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u7684\u64cd\u4f5c\u65b9\u5411\u4e0e\u4ece\u5e95\u81f3\u9876\u5806\u5316\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\uff1b\u7136\u540e\u5faa\u73af\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u76f4\u5230\u8d8a\u8fc7\u53f6\u8282\u70b9\u6216\u9047\u5230\u65e0\u9700\u4ea4\u6362\u7684\u8282\u70b9\u65f6\u7ed3\u675f\u3002

    <1><2><3><4><5><6><7><8><9><10>

    \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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart my_heap.java
    /* \u5143\u7d20\u51fa\u5806 */\nint pop() {\n// \u5224\u7a7a\u5904\u7406\nif (isEmpty())\nthrow new IndexOutOfBoundsException();\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nswap(0, size() - 1);\n// \u5220\u9664\u8282\u70b9\nint val = maxHeap.remove(size() - 1);\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nsiftDown(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nint l = left(i), r = right(i), ma = i;\nif (l < size() && maxHeap.get(l) > maxHeap.get(ma))\nma = l;\nif (r < size() && maxHeap.get(r) > maxHeap.get(ma))\nma = r;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i)\nbreak;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(i, ma);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n
    my_heap.cpp
    /* \u5143\u7d20\u51fa\u5806 */\nvoid pop() {\n// \u5224\u7a7a\u5904\u7406\nif (empty()) {\nthrow out_of_range(\"\u5806\u4e3a\u7a7a\");\n}\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nswap(maxHeap[0], maxHeap[size() - 1]);\n// \u5220\u9664\u8282\u70b9\nmaxHeap.pop_back();\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nsiftDown(0);\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nint l = left(i), r = right(i), ma = i;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (l < size() && maxHeap[l] > maxHeap[ma])\nma = l;\nif (r < size() && maxHeap[r] > maxHeap[ma])\nma = r;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i)\nbreak;\nswap(maxHeap[i], maxHeap[ma]);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n
    my_heap.py
    def pop(self) -> int:\n\"\"\"\u5143\u7d20\u51fa\u5806\"\"\"\n# \u5224\u7a7a\u5904\u7406\nif self.is_empty():\nraise IndexError(\"\u5806\u4e3a\u7a7a\")\n# \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nself.swap(0, self.size() - 1)\n# \u5220\u9664\u8282\u70b9\nval = self.max_heap.pop()\n# \u4ece\u9876\u81f3\u5e95\u5806\u5316\nself.sift_down(0)\n# \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val\ndef sift_down(self, i: int):\n\"\"\"\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\"\"\"\nwhile True:\n# \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nl, r, ma = self.left(i), self.right(i), i\nif l < self.size() and self.max_heap[l] > self.max_heap[ma]:\nma = l\nif r < self.size() and self.max_heap[r] > self.max_heap[ma]:\nma = r\n# \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif ma == i:\nbreak\n# \u4ea4\u6362\u4e24\u8282\u70b9\nself.swap(i, ma)\n# \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma\n
    my_heap.go
    /* \u5143\u7d20\u51fa\u5806 */\nfunc (h *maxHeap) pop() any {\n// \u5224\u7a7a\u5904\u7406\nif h.isEmpty() {\nfmt.Println(\"error\")\nreturn nil\n}\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nh.swap(0, h.size()-1)\n// \u5220\u9664\u8282\u70b9\nval := h.data[len(h.data)-1]\nh.data = h.data[:len(h.data)-1]\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nh.siftDown(0)\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc (h *maxHeap) siftDown(i int) {\nfor true {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a max\nl, r, max := h.left(i), h.right(i), i\nif l < h.size() && h.data[l].(int) > h.data[max].(int) {\nmax = l\n}\nif r < h.size() && h.data[r].(int) > h.data[max].(int) {\nmax = r\n}\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif max == i {\nbreak\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nh.swap(i, max)\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = max\n}\n}\n
    my_heap.js
    /* \u5143\u7d20\u51fa\u5806 */\npop() {\n// \u5224\u7a7a\u5904\u7406\nif (this.isEmpty()) throw new Error('\u5806\u4e3a\u7a7a');\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nthis.#swap(0, this.size() - 1);\n// \u5220\u9664\u8282\u70b9\nconst val = this.#maxHeap.pop();\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nthis.#siftDown(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\n#siftDown(i) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nconst l = this.#left(i),\nr = this.#right(i);\nlet ma = i;\nif (l < this.size() && this.#maxHeap[l] > this.#maxHeap[ma]) ma = l;\nif (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\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nthis.#swap(i, ma);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n
    my_heap.ts
    /* \u5143\u7d20\u51fa\u5806 */\npop(): number {\n// \u5224\u7a7a\u5904\u7406\nif (this.isEmpty()) throw new RangeError('Heap is empty.');\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nthis.swap(0, this.size() - 1);\n// \u5220\u9664\u8282\u70b9\nconst val = this.maxHeap.pop();\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nthis.siftDown(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nsiftDown(i: number): void {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nconst l = this.left(i),\nr = this.right(i);\nlet ma = i;\nif (l < this.size() && this.maxHeap[l] > this.maxHeap[ma]) ma = l;\nif (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\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nthis.swap(i, ma);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n
    my_heap.c
    /* \u5143\u7d20\u51fa\u5806 */\nint pop(maxHeap *h) {\n// \u5224\u7a7a\u5904\u7406\nif (isEmpty(h)) {\nprintf(\"heap is empty!\");\nreturn INT_MAX;\n}\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nswap(h, 0, size(h) - 1);\n// \u5220\u9664\u8282\u70b9\nint val = h->data[h->size - 1];\nh->size--;\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nsiftDown(h, 0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(maxHeap *h, int i) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a max\nint l = left(h, i);\nint r = right(h, i);\nint max = i;\nif (l < size(h) && h->data[l] > h->data[max]) {\nmax = l;\n}\nif (r < size(h) && h->data[r] > h->data[max]) {\nmax = r;\n}\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (max == i) {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(h, i, max);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = max;\n}\n}\n
    my_heap.cs
    /* \u5143\u7d20\u51fa\u5806 */\nint pop() {\n// \u5224\u7a7a\u5904\u7406\nif (isEmpty())\nthrow new IndexOutOfRangeException();\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nswap(0, size() - 1);\n// \u5220\u9664\u8282\u70b9\nint val = maxHeap.Last();\nmaxHeap.RemoveAt(size() - 1);\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nsiftDown(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nint l = left(i), r = right(i), ma = i;\nif (l < size() && maxHeap[l] > maxHeap[ma])\nma = l;\nif (r < size() && maxHeap[r] > maxHeap[ma])\nma = r;\n// \u82e5\u201c\u8282\u70b9 i \u6700\u5927\u201d\u6216\u201c\u8d8a\u8fc7\u53f6\u8282\u70b9\u201d\uff0c\u5219\u7ed3\u675f\u5806\u5316\nif (ma == i) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(i, ma);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n
    my_heap.swift
    /* \u5143\u7d20\u51fa\u5806 */\nfunc pop() -> Int {\n// \u5224\u7a7a\u5904\u7406\nif isEmpty() {\nfatalError(\"\u5806\u4e3a\u7a7a\")\n}\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nswap(i: 0, j: size() - 1)\n// \u5220\u9664\u8282\u70b9\nlet val = maxHeap.remove(at: size() - 1)\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nsiftDown(i: 0)\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc siftDown(i: Int) {\nvar i = i\nwhile true {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nlet l = left(i: i)\nlet r = right(i: i)\nvar ma = i\nif l < size(), maxHeap[l] > maxHeap[ma] {\nma = l\n}\nif r < size(), maxHeap[r] > maxHeap[ma] {\nma = r\n}\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif ma == i {\nbreak\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(i: i, j: ma)\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma\n}\n}\n
    my_heap.zig
    // \u5143\u7d20\u51fa\u5806\nfn pop(self: *Self) !T {\n// \u5224\u65ad\u5904\u7406\nif (self.isEmpty()) unreachable;\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\ntry self.swap(0, self.size() - 1);\n// \u5220\u9664\u8282\u70b9\nvar val = self.max_heap.?.pop();\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\ntry self.siftDown(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n} // \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\nfn siftDown(self: *Self, i_: usize) !void {\nvar i = i_;\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nvar l = left(i);\nvar r = right(i);\nvar ma = i;\nif (l < self.size() and self.max_heap.?.items[l] > self.max_heap.?.items[ma]) ma = l;\nif (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\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\ntry self.swap(i, ma);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n
    my_heap.dart
    /* \u5143\u7d20\u51fa\u5806 */\nint pop() {\n// \u5224\u7a7a\u5904\u7406\nif (isEmpty()) throw Exception('\u5806\u4e3a\u7a7a');\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n_swap(0, size() - 1);\n// \u5220\u9664\u8282\u70b9\nint val = _maxHeap.removeLast();\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\n_siftDown(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n}\n[class]{MaxHeap}-[func]{siftDown}\n
    "},{"location":"chapter_heap/heap/#813","title":"8.1.3. \u00a0 \u5806\u5e38\u89c1\u5e94\u7528","text":"
    • \u4f18\u5148\u961f\u5217\uff1a\u5806\u901a\u5e38\u4f5c\u4e3a\u5b9e\u73b0\u4f18\u5148\u961f\u5217\u7684\u9996\u9009\u6570\u636e\u7ed3\u6784\uff0c\u5176\u5165\u961f\u548c\u51fa\u961f\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(\\log n)\\) \uff0c\u800c\u5efa\u961f\u64cd\u4f5c\u4e3a \\(O(n)\\) \uff0c\u8fd9\u4e9b\u64cd\u4f5c\u90fd\u975e\u5e38\u9ad8\u6548\u3002
    • \u5806\u6392\u5e8f\uff1a\u7ed9\u5b9a\u4e00\u7ec4\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u5b83\u4eec\u5efa\u7acb\u4e00\u4e2a\u5806\uff0c\u7136\u540e\u4e0d\u65ad\u5730\u6267\u884c\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\uff0c\u4ece\u800c\u5f97\u5230\u6709\u5e8f\u6570\u636e\u3002\u7136\u800c\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u4f7f\u7528\u4e00\u79cd\u66f4\u4f18\u96c5\u7684\u65b9\u5f0f\u5b9e\u73b0\u5806\u6392\u5e8f\uff0c\u8be6\u89c1\u540e\u7eed\u7684\u5806\u6392\u5e8f\u7ae0\u8282\u3002
    • \u83b7\u53d6\u6700\u5927\u7684 \\(k\\) \u4e2a\u5143\u7d20\uff1a\u8fd9\u662f\u4e00\u4e2a\u7ecf\u5178\u7684\u7b97\u6cd5\u95ee\u9898\uff0c\u540c\u65f6\u4e5f\u662f\u4e00\u79cd\u5178\u578b\u5e94\u7528\uff0c\u4f8b\u5982\u9009\u62e9\u70ed\u5ea6\u524d 10 \u7684\u65b0\u95fb\u4f5c\u4e3a\u5fae\u535a\u70ed\u641c\uff0c\u9009\u53d6\u9500\u91cf\u524d 10 \u7684\u5546\u54c1\u7b49\u3002
    "},{"location":"chapter_heap/summary/","title":"8.4. \u00a0 \u5c0f\u7ed3","text":"
    • \u5806\u662f\u4e00\u68f5\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u6839\u636e\u6210\u7acb\u6761\u4ef6\u53ef\u5206\u4e3a\u5927\u9876\u5806\u548c\u5c0f\u9876\u5806\u3002\u5927\uff08\u5c0f\uff09\u9876\u5806\u7684\u5806\u9876\u5143\u7d20\u662f\u6700\u5927\uff08\u5c0f\uff09\u7684\u3002
    • \u4f18\u5148\u961f\u5217\u7684\u5b9a\u4e49\u662f\u5177\u6709\u51fa\u961f\u4f18\u5148\u7ea7\u7684\u961f\u5217\uff0c\u901a\u5e38\u4f7f\u7528\u5806\u6765\u5b9e\u73b0\u3002
    • \u5806\u7684\u5e38\u7528\u64cd\u4f5c\u53ca\u5176\u5bf9\u5e94\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5305\u62ec\uff1a\u5143\u7d20\u5165\u5806 \\(O(\\log n)\\) \u3001\u5806\u9876\u5143\u7d20\u51fa\u5806 \\(O(\\log n)\\) \u548c\u8bbf\u95ee\u5806\u9876\u5143\u7d20 \\(O(1)\\) \u7b49\u3002
    • \u5b8c\u5168\u4e8c\u53c9\u6811\u975e\u5e38\u9002\u5408\u7528\u6570\u7ec4\u8868\u793a\uff0c\u56e0\u6b64\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u6570\u7ec4\u6765\u5b58\u50a8\u5806\u3002
    • \u5806\u5316\u64cd\u4f5c\u7528\u4e8e\u7ef4\u62a4\u5806\u7684\u6027\u8d28\uff0c\u5728\u5165\u5806\u548c\u51fa\u5806\u64cd\u4f5c\u4e2d\u90fd\u4f1a\u7528\u5230\u3002
    • \u8f93\u5165 \\(n\\) \u4e2a\u5143\u7d20\u5e76\u5efa\u5806\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u4f18\u5316\u81f3 \\(O(n)\\) \uff0c\u975e\u5e38\u9ad8\u6548\u3002
    "},{"location":"chapter_heap/top_k/","title":"8.3. \u00a0 Top-K \u95ee\u9898","text":"

    Question

    \u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \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 \\(k\\) \u8f6e\u904d\u5386\uff0c\u5206\u522b\u5728\u6bcf\u8f6e\u4e2d\u63d0\u53d6\u7b2c \\(1\\) , \\(2\\) , \\(\\cdots\\) , \\(k\\) \u5927\u7684\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nk)\\) \u3002

    \u8be5\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

    Fig. \u904d\u5386\u5bfb\u627e\u6700\u5927\u7684 k \u4e2a\u5143\u7d20

    Tip

    \u5f53 \\(k = n\\) \u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\uff0c\u7b49\u4ef7\u4e8e\u300c\u9009\u62e9\u6392\u5e8f\u300d\u7b97\u6cd5\u3002

    "},{"location":"chapter_heap/top_k/#832","title":"8.3.2. \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u6392\u5e8f","text":"

    \u6211\u4eec\u53ef\u4ee5\u5bf9\u6570\u7ec4 nums \u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\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\u8981\u627e\u51fa\u6700\u5927\u7684 \\(k\\) \u4e2a\u5143\u7d20\u5373\u53ef\uff0c\u800c\u4e0d\u9700\u8981\u6392\u5e8f\u5176\u4ed6\u5143\u7d20\u3002

    Fig. \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\u4e0b\uff1a

    1. \u521d\u59cb\u5316\u4e00\u4e2a\u5c0f\u9876\u5806\uff0c\u5176\u5806\u9876\u5143\u7d20\u6700\u5c0f\uff1b
    2. \u5148\u5c06\u6570\u7ec4\u7684\u524d \\(k\\) \u4e2a\u5143\u7d20\u4f9d\u6b21\u5165\u5806\uff1b
    3. \u4ece\u7b2c \\(k + 1\\) \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\uff0c\u5e76\u5c06\u5f53\u524d\u5143\u7d20\u5165\u5806\uff1b
    4. \u904d\u5386\u5b8c\u6210\u540e\uff0c\u5806\u4e2d\u4fdd\u5b58\u7684\u5c31\u662f\u6700\u5927\u7684 \\(k\\) \u4e2a\u5143\u7d20\uff1b
    <1><2><3><4><5><6><7><8><9>

    \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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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) {\nQueue<Integer> heap = new PriorityQueue<Integer>();\n// \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nfor (int i = 0; i < k; i++) {\nheap.add(nums[i]);\n}\n// \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\nfor (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\nif (nums[i] > heap.peek()) {\nheap.poll();\nheap.add(nums[i]);\n}\n}\nreturn heap;\n}\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) {\npriority_queue<int, vector<int>, greater<int>> heap;\n// \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nfor (int i = 0; i < k; i++) {\nheap.push(nums[i]);\n}\n// \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\nfor (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\nif (nums[i] > heap.top()) {\nheap.pop();\nheap.push(nums[i]);\n}\n}\nreturn heap;\n}\n
    top_k.py
    def 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\"\"\"\nheap = []\n# \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nfor i in range(k):\nheapq.heappush(heap, nums[i])\n# \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\nfor 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\nif nums[i] > heap[0]:\nheapq.heappop(heap)\nheapq.heappush(heap, nums[i])\nreturn heap\n
    top_k.go
    [class]{maxHeap}-[func]{topKHeap}\n
    top_k.js
    [class]{}-[func]{topKHeap}\n
    top_k.ts
    [class]{}-[func]{topKHeap}\n
    top_k.c
    [class]{maxHeap}-[func]{topKHeap}\n
    top_k.cs
    [class]{top_k}-[func]{topKHeap}\n
    top_k.swift
    [class]{}-[func]{topKHeap}\n
    top_k.zig
    [class]{}-[func]{topKHeap}\n
    top_k.dart
    [class]{}-[func]{top_k_heap}\n
    "},{"location":"chapter_introduction/","title":"1. \u00a0 \u5f15\u8a00","text":""},{"location":"chapter_introduction/algorithms_are_everywhere/","title":"1.1. \u00a0 \u7b97\u6cd5\u65e0\u5904\u4e0d\u5728","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\u4e8e\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\u5b9e\u9645\u4e0a\uff0c\u4f60\u5df2\u7ecf\u5b66\u4f1a\u4e86\u8bb8\u591a\u7b97\u6cd5\uff0c\u5e76\u4e60\u60ef\u5c06\u4ed6\u4eec\u5e94\u7528\u5230\u65e5\u5e38\u751f\u6d3b\u4e2d\u4e86\u3002\u4e0b\u9762\uff0c\u6211\u5c06\u4e3e\u4e24\u4e2a\u5177\u4f53\u4f8b\u5b50\u6765\u8bc1\u5b9e\u8fd9\u4e00\u70b9\u3002

    \u4f8b\u4e00\uff1a\u7ec4\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

    \u4ece\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u79ef\u6728\u7684\u5404\u79cd\u5f62\u72b6\u548c\u8fde\u63a5\u65b9\u5f0f\u4ee3\u8868\u6570\u636e\u7ed3\u6784\uff0c\u800c\u7ec4\u88c5\u8bf4\u660e\u4e66\u4e0a\u7684\u4e00\u7cfb\u5217\u6b65\u9aa4\u5219\u662f\u7b97\u6cd5\u3002

    Fig. \u62fc\u88c5\u79ef\u6728

    \u4f8b\u4e8c\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\u7684\u82f1\u6587\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\u8fd9\u6837\u64cd\u4f5c\uff1a

    1. \u7ffb\u5f00\u5b57\u5178\u7ea6\u4e00\u534a\u7684\u9875\u6570\uff0c\u67e5\u770b\u8be5\u9875\u9996\u5b57\u6bcd\u662f\u4ec0\u4e48\uff08\u5047\u8bbe\u4e3a \\(m\\) \uff09\uff1b
    2. \u7531\u4e8e\u5728\u82f1\u6587\u5b57\u6bcd\u8868\u4e2d \\(r\\) \u4f4d\u4e8e \\(m\\) \u4e4b\u540e\uff0c\u6240\u4ee5\u6392\u9664\u5b57\u5178\u524d\u534a\u90e8\u5206\uff0c\u67e5\u627e\u8303\u56f4\u7f29\u5c0f\u5230\u540e\u534a\u90e8\u5206\uff1b
    3. \u4e0d\u65ad\u91cd\u590d\u6b65\u9aa4 1-2 \uff0c\u76f4\u81f3\u627e\u5230\u62fc\u97f3\u9996\u5b57\u6bcd\u4e3a \\(r\\) \u7684\u9875\u7801\u4e3a\u6b62\u3002
    <1><2><3><4><5>

    \u67e5\u9605\u5b57\u5178\u8fd9\u4e2a\u5c0f\u5b66\u751f\u5fc5\u5907\u6280\u80fd\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u8457\u540d\u7684\u300c\u4e8c\u5206\u67e5\u627e\u300d\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\u300c\u6570\u7ec4\u300d\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\u662f\u300c\u4e8c\u5206\u67e5\u627e\u300d\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\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

    \u9605\u8bfb\u81f3\u6b64\uff0c\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\u90a3\u4e48\u592a\u597d\u4e86\uff01\u56e0\u4e3a\u8fd9\u6b63\u662f\u672c\u4e66\u5b58\u5728\u7684\u610f\u4e49\u3002\u63a5\u4e0b\u6765\uff0c\u8fd9\u672c\u4e66\u5c06\u4e00\u6b65\u6b65\u5f15\u5bfc\u4f60\u6df1\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":"
    • \u7b97\u6cd5\u5728\u65e5\u5e38\u751f\u6d3b\u4e2d\u65e0\u5904\u4e0d\u5728\uff0c\u5e76\u4e0d\u662f\u9065\u4e0d\u53ef\u53ca\u7684\u9ad8\u6df1\u77e5\u8bc6\u3002\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u5df2\u7ecf\u5728\u4e0d\u77e5\u4e0d\u89c9\u4e2d\u5b66\u4e60\u4e86\u8bb8\u591a\u201c\u7b97\u6cd5\u201d\uff0c\u7528\u4ee5\u89e3\u51b3\u751f\u6d3b\u4e2d\u7684\u5927\u5c0f\u95ee\u9898\u3002
    • \u67e5\u9605\u5b57\u5178\u7684\u539f\u7406\u4e0e\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u76f8\u4e00\u81f4\u3002\u4e8c\u5206\u67e5\u627e\u4f53\u73b0\u4e86\u5206\u800c\u6cbb\u4e4b\u7684\u91cd\u8981\u7b97\u6cd5\u601d\u60f3\u3002
    • \u7b97\u6cd5\u662f\u5728\u6709\u9650\u65f6\u95f4\u5185\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\u7684\u4e00\u7ec4\u6307\u4ee4\u6216\u64cd\u4f5c\u6b65\u9aa4\uff0c\u800c\u6570\u636e\u7ed3\u6784\u662f\u8ba1\u7b97\u673a\u4e2d\u7ec4\u7ec7\u548c\u5b58\u50a8\u6570\u636e\u7684\u65b9\u5f0f\u3002
    • \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7d27\u5bc6\u76f8\u8fde\u3002\u6570\u636e\u7ed3\u6784\u662f\u7b97\u6cd5\u7684\u57fa\u77f3\uff0c\u800c\u7b97\u6cd5\u5219\u662f\u53d1\u6325\u6570\u636e\u7ed3\u6784\u4f5c\u7528\u7684\u821e\u53f0\u3002
    • \u4e50\u9ad8\u79ef\u6728\u5bf9\u5e94\u4e8e\u6570\u636e\uff0c\u79ef\u6728\u5f62\u72b6\u548c\u8fde\u63a5\u65b9\u5f0f\u4ee3\u8868\u6570\u636e\u7ed3\u6784\uff0c\u62fc\u88c5\u79ef\u6728\u7684\u6b65\u9aa4\u5219\u5bf9\u5e94\u7b97\u6cd5\u3002
    "},{"location":"chapter_introduction/what_is_dsa/","title":"1.2. \u00a0 \u7b97\u6cd5\u662f\u4ec0\u4e48","text":""},{"location":"chapter_introduction/what_is_dsa/#121","title":"1.2.1. \u00a0 \u7b97\u6cd5\u5b9a\u4e49","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\u3002\u7b97\u6cd5\u5177\u6709\u4ee5\u4e0b\u7279\u6027\uff1a

    • \u95ee\u9898\u662f\u660e\u786e\u7684\uff0c\u5177\u6709\u6e05\u6670\u7684\u8f93\u5165\u548c\u8f93\u51fa\u5b9a\u4e49\u3002
    • \u89e3\u5177\u6709\u786e\u5b9a\u6027\uff0c\u5373\u7ed9\u5b9a\u76f8\u540c\u7684\u8f93\u5165\u65f6\uff0c\u8f93\u51fa\u59cb\u7ec8\u76f8\u540c\u3002
    • \u5177\u6709\u53ef\u884c\u6027\uff0c\u5728\u6709\u9650\u6b65\u9aa4\u3001\u65f6\u95f4\u548c\u5185\u5b58\u7a7a\u95f4\u4e0b\u53ef\u5b8c\u6210\u3002
    "},{"location":"chapter_introduction/what_is_dsa/#122","title":"1.2.2. \u00a0 \u6570\u636e\u7ed3\u6784\u5b9a\u4e49","text":"

    \u300c\u6570\u636e\u7ed3\u6784 Data Structure\u300d\u662f\u8ba1\u7b97\u673a\u4e2d\u7ec4\u7ec7\u548c\u5b58\u50a8\u6570\u636e\u7684\u65b9\u5f0f\u3002\u4e3a\u4e86\u63d0\u9ad8\u6570\u636e\u5b58\u50a8\u548c\u64cd\u4f5c\u6027\u80fd\uff0c\u6570\u636e\u7ed3\u6784\u7684\u8bbe\u8ba1\u76ee\u6807\u5305\u62ec\uff1a

    • \u7a7a\u95f4\u5360\u7528\u5c3d\u91cf\u51cf\u5c11\uff0c\u8282\u7701\u8ba1\u7b97\u673a\u5185\u5b58\u3002
    • \u6570\u636e\u64cd\u4f5c\u5c3d\u53ef\u80fd\u5feb\u901f\uff0c\u6db5\u76d6\u6570\u636e\u8bbf\u95ee\u3001\u6dfb\u52a0\u3001\u5220\u9664\u3001\u66f4\u65b0\u7b49\u3002
    • \u63d0\u4f9b\u7b80\u6d01\u7684\u6570\u636e\u8868\u793a\u548c\u903b\u8f91\u4fe1\u606f\uff0c\u4ee5\u5229\u4e8e\u7b97\u6cd5\u9ad8\u6548\u8fd0\u884c\u3002

    \u6570\u636e\u7ed3\u6784\u8bbe\u8ba1\u662f\u4e00\u4e2a\u5145\u6ee1\u6743\u8861\u7684\u8fc7\u7a0b\uff0c\u8fd9\u610f\u5473\u7740\u8981\u5728\u67d0\u65b9\u9762\u53d6\u5f97\u4f18\u52bf\uff0c\u5f80\u5f80\u9700\u8981\u5728\u53e6\u4e00\u65b9\u9762\u4f5c\u51fa\u59a5\u534f\u3002\u4f8b\u5982\uff0c\u94fe\u8868\u76f8\u8f83\u4e8e\u6570\u7ec4\uff0c\u5728\u6570\u636e\u6dfb\u52a0\u548c\u5220\u9664\u64cd\u4f5c\u4e0a\u66f4\u52a0\u4fbf\u6377\uff0c\u4f46\u727a\u7272\u4e86\u6570\u636e\u8bbf\u95ee\u901f\u5ea6\uff1b\u56fe\u76f8\u8f83\u4e8e\u94fe\u8868\uff0c\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684\u903b\u8f91\u4fe1\u606f\uff0c\u4f46\u9700\u8981\u5360\u7528\u66f4\u5927\u7684\u5185\u5b58\u7a7a\u95f4\u3002

    "},{"location":"chapter_introduction/what_is_dsa/#123","title":"1.2.3. \u00a0 \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u5173\u7cfb","text":"

    \u300c\u6570\u636e\u7ed3\u6784\u300d\u4e0e\u300c\u7b97\u6cd5\u300d\u9ad8\u5ea6\u76f8\u5173\u4e14\u7d27\u5bc6\u7ed3\u5408\uff0c\u5177\u4f53\u8868\u73b0\u5728\uff1a

    • \u6570\u636e\u7ed3\u6784\u662f\u7b97\u6cd5\u7684\u57fa\u77f3\u3002\u6570\u636e\u7ed3\u6784\u4e3a\u7b97\u6cd5\u63d0\u4f9b\u4e86\u7ed3\u6784\u5316\u5b58\u50a8\u7684\u6570\u636e\uff0c\u4ee5\u53ca\u7528\u4e8e\u64cd\u4f5c\u6570\u636e\u7684\u65b9\u6cd5\u3002
    • \u7b97\u6cd5\u662f\u6570\u636e\u7ed3\u6784\u53d1\u6325\u7684\u821e\u53f0\u3002\u6570\u636e\u7ed3\u6784\u672c\u8eab\u4ec5\u5b58\u50a8\u6570\u636e\u4fe1\u606f\uff0c\u901a\u8fc7\u7ed3\u5408\u7b97\u6cd5\u624d\u80fd\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\u3002
    • \u7279\u5b9a\u7b97\u6cd5\u901a\u5e38\u6709\u5bf9\u5e94\u6700\u4f18\u7684\u6570\u636e\u7ed3\u6784\u3002\u7b97\u6cd5\u901a\u5e38\u53ef\u4ee5\u57fa\u4e8e\u4e0d\u540c\u7684\u6570\u636e\u7ed3\u6784\u8fdb\u884c\u5b9e\u73b0\uff0c\u4f46\u6700\u7ec8\u6267\u884c\u6548\u7387\u53ef\u80fd\u76f8\u5dee\u5f88\u5927\u3002

    Fig. \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u5173\u7cfb

    \u7c7b\u6bd4\u300cLEGO \u4e50\u9ad8\u300d\u548c\u300c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u300d\uff0c\u5219\u5bf9\u5e94\u5173\u7cfb\u5982\u4e0b\u8868\u6240\u793a\u3002

    \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5 LEGO \u4e50\u9ad8 \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\u6ce8\u610f\u7684\u662f\uff0c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u72ec\u7acb\u4e8e\u7f16\u7a0b\u8bed\u8a00\u3002\u6b63\u56e0\u5982\u6b64\uff0c\u672c\u4e66\u5f97\u4ee5\u63d0\u4f9b\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\u300c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u300d\u7b80\u79f0\u4e3a\u300c\u7b97\u6cd5\u300d\u3002\u4f8b\u5982\uff0c\u4f17\u6240\u5468\u77e5\u7684 LeetCode \u7b97\u6cd5\u9898\u76ee\uff0c\u5b9e\u9645\u4e0a\u540c\u65f6\u8003\u5bdf\u4e86\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u4e24\u65b9\u9762\u7684\u77e5\u8bc6\u3002

    "},{"location":"chapter_preface/","title":"0. \u00a0 \u5199\u5728\u524d\u9762","text":""},{"location":"chapter_preface/about_the_book/","title":"0.1. \u00a0 \u5173\u4e8e\u672c\u4e66","text":"

    \u672c\u9879\u76ee\u65e8\u5728\u521b\u5efa\u4e00\u672c\u5f00\u6e90\u514d\u8d39\u3001\u65b0\u624b\u53cb\u597d\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u5165\u95e8\u6559\u7a0b\u3002

    • \u5168\u4e66\u91c7\u7528\u52a8\u753b\u56fe\u89e3\uff0c\u7ed3\u6784\u5316\u5730\u8bb2\u89e3\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u77e5\u8bc6\uff0c\u5185\u5bb9\u6e05\u6670\u6613\u61c2\u3001\u5b66\u4e60\u66f2\u7ebf\u5e73\u6ed1\uff1b
    • \u7b97\u6cd5\u6e90\u4ee3\u7801\u7686\u53ef\u4e00\u952e\u8fd0\u884c\uff0c\u652f\u6301 Java, C++, Python, Go, JS, TS, C#, Swift, Zig \u7b49\u8bed\u8a00\uff1b
    • \u9f13\u52b1\u8bfb\u8005\u5728\u7ae0\u8282\u8ba8\u8bba\u533a\u4e92\u5e2e\u4e92\u52a9\u3001\u5171\u540c\u8fdb\u6b65\uff0c\u63d0\u95ee\u4e0e\u8bc4\u8bba\u901a\u5e38\u53ef\u5728\u4e24\u65e5\u5185\u5f97\u5230\u56de\u590d\uff1b
    "},{"location":"chapter_preface/about_the_book/#011","title":"0.1.1. \u00a0 \u8bfb\u8005\u5bf9\u8c61","text":"

    \u82e5\u60a8\u662f\u300c\u7b97\u6cd5\u521d\u5b66\u8005\u300d\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\u8fd9\u672c\u4e66\u6b63\u662f\u4e3a\u60a8\u91cf\u8eab\u5b9a\u5236\uff01

    \u5982\u679c\u60a8\u662f\u300c\u7b97\u6cd5\u8001\u624b\u300d\uff0c\u5df2\u7ecf\u79ef\u7d2f\u4e00\u5b9a\u5237\u9898\u91cf\uff0c\u719f\u6089\u5927\u90e8\u5206\u9898\u578b\uff0c\u90a3\u4e48\u672c\u4e66\u53ef\u52a9\u60a8\u56de\u987e\u4e0e\u68b3\u7406\u7b97\u6cd5\u77e5\u8bc6\u4f53\u7cfb\uff0c\u4ed3\u5e93\u6e90\u4ee3\u7801\u53ef\u4ee5\u88ab\u5f53\u4f5c\u201c\u5237\u9898\u5de5\u5177\u5e93\u201d\u6216\u201c\u7b97\u6cd5\u5b57\u5178\u201d\u6765\u4f7f\u7528\u3002

    \u82e5\u60a8\u662f\u300c\u7b97\u6cd5\u4e13\u5bb6\u300d\uff0c\u6211\u4eec\u671f\u5f85\u6536\u5230\u60a8\u7684\u5b9d\u8d35\u5efa\u8bae\uff0c\u6216\u8005\u4e00\u8d77\u53c2\u4e0e\u521b\u4f5c\u3002

    \u524d\u7f6e\u6761\u4ef6

    \u60a8\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\u4e3b\u8981\u5185\u5bb9\u5305\u62ec\uff1a

    • \u590d\u6742\u5ea6\u5206\u6790\uff1a\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u8bc4\u4ef7\u7ef4\u5ea6\u3001\u7b97\u6cd5\u6548\u7387\u7684\u8bc4\u4f30\u65b9\u6cd5\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u3001\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u5305\u62ec\u63a8\u7b97\u65b9\u6cd5\u3001\u5e38\u89c1\u7c7b\u578b\u3001\u793a\u4f8b\u7b49\u3002
    • \u6570\u636e\u7ed3\u6784\uff1a\u5e38\u89c1\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u6570\u636e\u5728\u5185\u5b58\u4e2d\u7684\u5b58\u50a8\u5f62\u5f0f\u3001\u6570\u636e\u7ed3\u6784\u7684\u5206\u7c7b\u65b9\u6cd5\u3002\u6d89\u53ca\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u3001\u6563\u5217\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\u7b49\u6570\u636e\u7ed3\u6784\uff0c\u5185\u5bb9\u5305\u62ec\u5b9a\u4e49\u3001\u4f18\u7f3a\u70b9\u3001\u5e38\u7528\u64cd\u4f5c\u3001\u5e38\u89c1\u7c7b\u578b\u3001\u5178\u578b\u5e94\u7528\u3001\u5b9e\u73b0\u65b9\u6cd5\u7b49\u3002
    • \u7b97\u6cd5\uff1a\u67e5\u627e\u7b97\u6cd5\u3001\u6392\u5e8f\u7b97\u6cd5\u3001\u641c\u7d22\u4e0e\u56de\u6eaf\u3001\u52a8\u6001\u89c4\u5212\u3001\u5206\u6cbb\u7b97\u6cd5\u7b49\uff0c\u5185\u5bb9\u6db5\u76d6\u5b9a\u4e49\u3001\u5e94\u7528\u573a\u666f\u3001\u4f18\u7f3a\u70b9\u3001\u65f6\u7a7a\u6548\u7387\u3001\u5b9e\u73b0\u65b9\u6cd5\u3001\u793a\u4f8b\u9898\u76ee\u7b49\u3002

    Fig. Hello \u7b97\u6cd5\u5185\u5bb9\u7ed3\u6784

    "},{"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

    • \u611f\u8c22\u6211\u5728\u516c\u53f8\u7684\u5bfc\u5e08\u674e\u6c50\u535a\u58eb\uff0c\u5728\u6df1\u5165\u4ea4\u8c08\u4e2d\u60a8\u9f13\u52b1\u6211\u201c\u884c\u52a8\u8d77\u6765\u201d\uff0c\u575a\u5b9a\u4e86\u6211\u5199\u8fd9\u672c\u4e66\u7684\u51b3\u5fc3\u3002
    • \u611f\u8c22\u6211\u7684\u5973\u670b\u53cb\u6ce1\u6ce1\u4f5c\u4e3a\u672c\u4e66\u7684\u9996\u4f4d\u8bfb\u8005\uff0c\u4ece\u7b97\u6cd5\u5c0f\u767d\u7684\u89d2\u5ea6\u63d0\u51fa\u8bb8\u591a\u5b9d\u8d35\u5efa\u8bae\uff0c\u4f7f\u5f97\u672c\u4e66\u66f4\u9002\u5408\u65b0\u624b\u9605\u8bfb\u3002
    • \u611f\u8c22\u817e\u5b9d\u3001\u7426\u5b9d\u3001\u98de\u5b9d\u4e3a\u672c\u4e66\u8d77\u4e86\u4e00\u4e2a\u5bcc\u6709\u521b\u610f\u7684\u540d\u5b57\uff0c\u5524\u8d77\u5927\u5bb6\u5199\u4e0b\u7b2c\u4e00\u884c\u4ee3\u7801 \"Hello World!\" \u7684\u7f8e\u597d\u56de\u5fc6\u3002
    • \u611f\u8c22\u82cf\u6f7c\u4e3a\u672c\u4e66\u8bbe\u8ba1\u4e86\u7cbe\u7f8e\u7684\u5c01\u9762\u548c LOGO\uff0c\u5e76\u5728\u6211\u7684\u5f3a\u8feb\u75c7\u4e0b\u591a\u6b21\u8010\u5fc3\u4fee\u6539\u3002
    • \u611f\u8c22 @squidfunk \u63d0\u4f9b\u7684\u5199\u4f5c\u6392\u7248\u5efa\u8bae\uff0c\u4ee5\u53ca\u6770\u51fa\u7684\u5f00\u6e90\u9879\u76ee Material-for-MkDocs \u3002

    \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\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\u4e9b\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\u60a8\u901a\u8bfb\u672c\u8282\u5185\u5bb9\u3002

    "},{"location":"chapter_preface/suggestions/#021","title":"0.2.1. \u00a0 \u884c\u6587\u98ce\u683c\u7ea6\u5b9a","text":"

    \u6807\u9898\u540e\u6807\u6ce8 * \u7684\u662f\u9009\u8bfb\u7ae0\u8282\uff0c\u5185\u5bb9\u76f8\u5bf9\u56f0\u96be\u3002\u5982\u679c\u4f60\u7684\u65f6\u95f4\u6709\u9650\uff0c\u5efa\u8bae\u53ef\u4ee5\u5148\u8df3\u8fc7\u3002

    \u6587\u7ae0\u4e2d\u7684\u91cd\u8981\u540d\u8bcd\u4f1a\u7528 \u300c \u300d \u62ec\u53f7\u6807\u6ce8\uff0c\u4f8b\u5982 \u300c\u6570\u7ec4 Array\u300d \u3002\u8bf7\u52a1\u5fc5\u8bb0\u4f4f\u8fd9\u4e9b\u540d\u8bcd\uff0c\u5305\u62ec\u82f1\u6587\u7ffb\u8bd1\uff0c\u4ee5\u4fbf\u540e\u7eed\u9605\u8bfb\u6587\u732e\u65f6\u4f7f\u7528\u3002

    \u52a0\u7c97\u7684\u6587\u5b57 \u8868\u793a\u91cd\u70b9\u5185\u5bb9\u6216\u603b\u7ed3\u6027\u8bed\u53e5\uff0c\u8fd9\u7c7b\u6587\u5b57\u503c\u5f97\u7279\u522b\u5173\u6ce8\u3002

    \u4e13\u6709\u540d\u8bcd\u548c\u6709\u7279\u6307\u542b\u4e49\u7684\u8bcd\u53e5\u4f1a\u4f7f\u7528 \u201c\u53cc\u5f15\u53f7\u201d \u6807\u6ce8\uff0c\u4ee5\u907f\u514d\u6b67\u4e49\u3002

    \u672c\u4e66\u90e8\u5206\u653e\u5f03\u4e86\u7f16\u7a0b\u8bed\u8a00\u7684\u6ce8\u91ca\u89c4\u8303\uff0c\u4ee5\u6362\u53d6\u66f4\u52a0\u7d27\u51d1\u7684\u5185\u5bb9\u6392\u7248\u3002\u6ce8\u91ca\u4e3b\u8981\u5206\u4e3a\u4e09\u79cd\u7c7b\u578b\uff1a\u6807\u9898\u6ce8\u91ca\u3001\u5185\u5bb9\u6ce8\u91ca\u3001\u591a\u884c\u6ce8\u91ca\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\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// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\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# \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\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// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\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// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\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// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\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// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\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// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\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// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\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// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n// \u591a\u884c\n// \u6ce8\u91ca\n
    /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\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\u56e0\u6b64\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

    \u5728\u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u5982\u679c\u53d1\u73b0\u67d0\u6bb5\u5185\u5bb9\u63d0\u4f9b\u4e86\u52a8\u753b\u6216\u56fe\u89e3\uff0c\u5efa\u8bae\u4ee5\u56fe\u4e3a\u4e3b\u7ebf\uff0c\u4ee5\u6587\u5b57\uff08\u901a\u5e38\u4f4d\u4e8e\u56fe\u50cf\u4e0a\u65b9\uff09\u4e3a\u8f85\uff0c\u7efc\u5408\u4e24\u8005\u6765\u7406\u89e3\u5185\u5bb9\u3002

    Fig. \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\u5728GitHub \u4ed3\u5e93\uff0c\u6e90\u4ee3\u7801\u5305\u542b\u8be6\u7ec6\u6ce8\u91ca\uff0c\u5e76\u9644\u6709\u6d4b\u8bd5\u6837\u4f8b\uff0c\u53ef\u76f4\u63a5\u8fd0\u884c\u3002

    \u5982\u679c\u5b66\u4e60\u65f6\u95f4\u6709\u9650\uff0c\u5efa\u8bae\u4f60\u81f3\u5c11\u901a\u8bfb\u5e76\u8fd0\u884c\u6240\u6709\u4ee3\u7801\u3002\u5982\u679c\u65f6\u95f4\u5145\u88d5\uff0c\u5efa\u8bae\u53c2\u7167\u4ee3\u7801\u81ea\u884c\u6572\u4e00\u904d\u3002\u4e0e\u4ec5\u9605\u8bfb\u4ee3\u7801\u76f8\u6bd4\uff0c\u7f16\u5199\u4ee3\u7801\u7684\u8fc7\u7a0b\u5f80\u5f80\u80fd\u5e26\u6765\u66f4\u591a\u6536\u83b7\u3002

    Fig. \u8fd0\u884c\u4ee3\u7801\u793a\u4f8b

    \u7b2c\u4e00\u6b65\uff1a\u5b89\u88c5\u672c\u5730\u7f16\u7a0b\u73af\u5883\u3002\u8bf7\u53c2\u7167\u9644\u5f55\u6559\u7a0b\u8fdb\u884c\u5b89\u88c5\uff0c\u5982\u679c\u5df2\u5b89\u88c5\u5219\u53ef\u8df3\u8fc7\u6b64\u6b65\u9aa4\u3002

    \u7b2c\u4e8c\u6b65\uff1a\u4e0b\u8f7d\u4ee3\u7801\u4ed3\u3002\u5982\u679c\u5df2\u7ecf\u5b89\u88c5 Git \uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u514b\u9686\u672c\u4ed3\u5e93\u3002

    git clone https://github.com/krahets/hello-algo.git\n

    \u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u70b9\u51fb\u201cDownload ZIP\u201d\u76f4\u63a5\u4e0b\u8f7d\u4ee3\u7801\u538b\u7f29\u5305\uff0c\u7136\u540e\u5728\u672c\u5730\u89e3\u538b\u5373\u53ef\u3002

    Fig. \u514b\u9686\u4ed3\u5e93\u4e0e\u4e0b\u8f7d\u4ee3\u7801

    \u7b2c\u4e09\u6b65\uff1a\u8fd0\u884c\u6e90\u4ee3\u7801\u3002\u5982\u679c\u4ee3\u7801\u5757\u9876\u90e8\u6807\u6709\u6587\u4ef6\u540d\u79f0\uff0c\u5219\u53ef\u4ee5\u5728\u4ed3\u5e93\u7684 codes \u6587\u4ef6\u5939\u4e2d\u627e\u5230\u76f8\u5e94\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u3002\u6e90\u4ee3\u7801\u6587\u4ef6\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

    Fig. \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":"

    \u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u8bf7\u4e0d\u8981\u201c\u60ef\u7740\u201d\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\u5176\u4ed6\u5c0f\u4f19\u4f34\u4eec\u5c06\u7aed\u8bda\u4e3a\u4f60\u89e3\u7b54\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u53ef\u5728\u4e24\u5929\u5185\u5f97\u5230\u56de\u590d\u3002

    \u540c\u65f6\uff0c\u4e5f\u5e0c\u671b\u60a8\u80fd\u5728\u8bc4\u8bba\u533a\u591a\u82b1\u4e9b\u65f6\u95f4\u3002\u4e00\u65b9\u9762\uff0c\u60a8\u53ef\u4ee5\u4e86\u89e3\u5927\u5bb6\u9047\u5230\u7684\u95ee\u9898\uff0c\u4ece\u800c\u67e5\u6f0f\u8865\u7f3a\uff0c\u8fd9\u5c06\u6709\u52a9\u4e8e\u6fc0\u53d1\u66f4\u6df1\u5165\u7684\u601d\u8003\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u5e0c\u671b\u60a8\u80fd\u6177\u6168\u5730\u56de\u7b54\u5176\u4ed6\u5c0f\u4f19\u4f34\u7684\u95ee\u9898\u3001\u5206\u4eab\u60a8\u7684\u89c1\u89e3\uff0c\u8ba9\u5927\u5bb6\u5171\u540c\u5b66\u4e60\u548c\u8fdb\u6b65\u3002

    Fig. \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\uff1a

    1. \u7b97\u6cd5\u5165\u95e8\u3002\u6211\u4eec\u9700\u8981\u719f\u6089\u5404\u79cd\u6570\u636e\u7ed3\u6784\u7684\u7279\u70b9\u548c\u7528\u6cd5\uff0c\u5b66\u4e60\u4e0d\u540c\u7b97\u6cd5\u7684\u539f\u7406\u3001\u6d41\u7a0b\u3001\u7528\u9014\u548c\u6548\u7387\u7b49\u65b9\u9762\u5185\u5bb9\u3002
    2. \u5237\u7b97\u6cd5\u9898\u3002\u5efa\u8bae\u4ece\u70ed\u95e8\u9898\u76ee\u5f00\u5237\uff0c\u5982\u5251\u6307 Offer\u548cLeetCode Hot 100\uff0c\u5148\u79ef\u7d2f\u81f3\u5c11 100 \u9053\u9898\u76ee\uff0c\u719f\u6089\u4e3b\u6d41\u7684\u7b97\u6cd5\u95ee\u9898\u3002\u521d\u6b21\u5237\u9898\u65f6\uff0c\u201c\u77e5\u8bc6\u9057\u5fd8\u201d\u53ef\u80fd\u662f\u4e00\u4e2a\u6311\u6218\uff0c\u4f46\u8bf7\u653e\u5fc3\uff0c\u8fd9\u662f\u5f88\u6b63\u5e38\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u6309\u7167\u201c\u827e\u5bbe\u6d69\u65af\u9057\u5fd8\u66f2\u7ebf\u201d\u6765\u590d\u4e60\u9898\u76ee\uff0c\u901a\u5e38\u5728\u8fdb\u884c 3-5 \u8f6e\u7684\u91cd\u590d\u540e\uff0c\u5c31\u80fd\u5c06\u5176\u7262\u8bb0\u5728\u5fc3\u3002
    3. \u642d\u5efa\u77e5\u8bc6\u4f53\u7cfb\u3002\u5728\u5b66\u4e60\u65b9\u9762\uff0c\u6211\u4eec\u53ef\u4ee5\u9605\u8bfb\u7b97\u6cd5\u4e13\u680f\u6587\u7ae0\u3001\u89e3\u9898\u6846\u67b6\u548c\u7b97\u6cd5\u6559\u6750\uff0c\u4ee5\u4e0d\u65ad\u4e30\u5bcc\u77e5\u8bc6\u4f53\u7cfb\u3002\u5728\u5237\u9898\u65b9\u9762\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u91c7\u7528\u8fdb\u9636\u5237\u9898\u7b56\u7565\uff0c\u5982\u6309\u4e13\u9898\u5206\u7c7b\u3001\u4e00\u9898\u591a\u89e3\u3001\u4e00\u89e3\u591a\u9898\u7b49\uff0c\u76f8\u5173\u7684\u5237\u9898\u5fc3\u5f97\u53ef\u4ee5\u5728\u5404\u4e2a\u793e\u533a\u627e\u5230\u3002

    \u4f5c\u4e3a\u4e00\u672c\u5165\u95e8\u6559\u7a0b\uff0c\u672c\u4e66\u5185\u5bb9\u4e3b\u8981\u6db5\u76d6\u201c\u7b2c\u4e00\u9636\u6bb5\u201d\uff0c\u65e8\u5728\u5e2e\u52a9\u4f60\u66f4\u9ad8\u6548\u5730\u5c55\u5f00\u7b2c\u4e8c\u548c\u7b2c\u4e09\u9636\u6bb5\u7684\u5b66\u4e60\u3002

    Fig. \u7b97\u6cd5\u5b66\u4e60\u8def\u7ebf

    "},{"location":"chapter_preface/summary/","title":"0.3. \u00a0 \u5c0f\u7ed3","text":"
    • \u672c\u4e66\u7684\u4e3b\u8981\u53d7\u4f17\u662f\u7b97\u6cd5\u521d\u5b66\u8005\u3002\u5982\u679c\u5df2\u6709\u4e00\u5b9a\u57fa\u7840\uff0c\u672c\u4e66\u80fd\u5e2e\u52a9\u60a8\u7cfb\u7edf\u56de\u987e\u7b97\u6cd5\u77e5\u8bc6\uff0c\u4e66\u5185\u6e90\u4ee3\u7801\u4e5f\u53ef\u4f5c\u4e3a\u201c\u5237\u9898\u5de5\u5177\u5e93\u201d\u4f7f\u7528\u3002
    • \u4e66\u4e2d\u5185\u5bb9\u4e3b\u8981\u5305\u62ec\u590d\u6742\u5ea6\u5206\u6790\u3001\u6570\u636e\u7ed3\u6784\u3001\u7b97\u6cd5\u4e09\u90e8\u5206\uff0c\u6db5\u76d6\u4e86\u8be5\u9886\u57df\u7684\u5927\u90e8\u5206\u4e3b\u9898\u3002
    • \u5bf9\u4e8e\u7b97\u6cd5\u65b0\u624b\uff0c\u5728\u521d\u5b66\u9636\u6bb5\u9605\u8bfb\u4e00\u672c\u5165\u95e8\u4e66\u7c4d\u81f3\u5173\u91cd\u8981\uff0c\u53ef\u4ee5\u5c11\u8d70\u8bb8\u591a\u5f2f\u8def\u3002
    • \u4e66\u5185\u7684\u52a8\u753b\u548c\u56fe\u89e3\u901a\u5e38\u7528\u4e8e\u4ecb\u7ecd\u91cd\u70b9\u548c\u96be\u70b9\u77e5\u8bc6\u3002\u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u5e94\u7ed9\u4e88\u8fd9\u4e9b\u5185\u5bb9\u66f4\u591a\u5173\u6ce8\u3002
    • \u5b9e\u8df5\u4e43\u5b66\u4e60\u7f16\u7a0b\u4e4b\u6700\u4f73\u9014\u5f84\u3002\u5f3a\u70c8\u5efa\u8bae\u8fd0\u884c\u6e90\u4ee3\u7801\u5e76\u4eb2\u81ea\u6572\u6253\u4ee3\u7801\u3002
    • \u672c\u4e66\u7f51\u9875\u7248\u7684\u6bcf\u4e2a\u7ae0\u8282\u90fd\u8bbe\u6709\u8ba8\u8bba\u533a\uff0c\u6b22\u8fce\u968f\u65f6\u5206\u4eab\u4f60\u7684\u7591\u60d1\u4e0e\u89c1\u89e3\u3002
    "},{"location":"chapter_reference/","title":"\u53c2\u8003\u6587\u732e","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":"10. \u00a0 \u641c\u7d22","text":""},{"location":"chapter_searching/binary_search/","title":"10.1. \u00a0 \u4e8c\u5206\u67e5\u627e","text":"

    \u300c\u4e8c\u5206\u67e5\u627e Binary Search\u300d\u662f\u4e00\u79cd\u57fa\u4e8e\u5206\u6cbb\u601d\u60f3\u7684\u9ad8\u6548\u641c\u7d22\u7b97\u6cd5\u3002\u5b83\u5229\u7528\u6570\u636e\u7684\u6709\u5e8f\u6027\uff0c\u6bcf\u8f6e\u51cf\u5c11\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\uff0c\u6570\u7ec4\u4e0d\u5305\u542b\u91cd\u590d\u5143\u7d20\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

    \u5bf9\u4e8e\u4e0a\u8ff0\u95ee\u9898\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\u5176\u4e2d\uff0c\u4e2d\u62ec\u53f7\u8868\u793a\u201c\u95ed\u533a\u95f4\u201d\uff0c\u5373\u5305\u542b\u8fb9\u754c\u503c\u672c\u8eab\u3002

    \u63a5\u4e0b\u6765\uff0c\u5faa\u73af\u6267\u884c\u4ee5\u4e0b\u4e24\u4e2a\u6b65\u9aa4\uff1a

    1. \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 \\(m = \\lfloor {(i + j) / 2} \\rfloor\\) \uff0c\u5176\u4e2d \\(\\lfloor \\space \\rfloor\\) \u8868\u793a\u5411\u4e0b\u53d6\u6574\u64cd\u4f5c\u3002
    2. \u5224\u65ad nums[m] \u548c target \u7684\u5927\u5c0f\u5173\u7cfb\uff0c\u5206\u4e3a\u4e09\u79cd\u60c5\u51b5\uff1a
      1. \u5f53 nums[m] < target \u65f6\uff0c\u8bf4\u660e target \u5728\u533a\u95f4 \\([m + 1, j]\\) \u4e2d\uff0c\u56e0\u6b64\u6267\u884c \\(i = m + 1\\) \uff1b
      2. \u5f53 nums[m] > target \u65f6\uff0c\u8bf4\u660e target \u5728\u533a\u95f4 \\([i, m - 1]\\) \u4e2d\uff0c\u56e0\u6b64\u6267\u884c \\(j = m - 1\\) \uff1b
      3. \u5f53 nums[m] = target \u65f6\uff0c\u8bf4\u660e\u627e\u5230 target \uff0c\u56e0\u6b64\u8fd4\u56de\u7d22\u5f15 \\(m\\) \uff1b

    \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

    <0><1><2><3><4><5><6><7>

    \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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nint 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\nwhile (i <= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\nelse if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\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\nint 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\nwhile (i <= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)    // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\nelse if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n
    binary_search.py
    def 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\ni, 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\nwhile 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\u9700\u8003\u8651\u5927\u6570\u8d8a\u754c\u95ee\u9898\nm = (i + j) // 2  # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target:\ni = m + 1  # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelif nums[m] > target:\nj = m - 1  # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse:\nreturn m  # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn -1  # \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\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\ni, 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\nfor i <= j {\nm := i + (j-i)/2      // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1\n} else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1\n} else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -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\nlet i = 0,\nj = 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\nwhile (i <= j) {\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m \uff0c\u4f7f\u7528 parseInt() \u5411\u4e0b\u53d6\u6574\nconst m = parseInt(i + (j - i) / 2);\nif (nums[m] < target)\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\nelse if (nums[m] > target)\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\nelse 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\nreturn -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\nlet i = 0,\nj = 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\nwhile (i <= j) {\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nconst m = Math.floor(i + (j - i) / 2);\nif (nums[m] < target) {\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\n} else if (nums[m] > target) {\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -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\nint 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\nwhile (i <= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)    // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\nelse if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -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\nint 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\nwhile (i <= j) {\nint m = i + (j - i) / 2;   // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)      // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\nelse if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\nelse                       // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -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\nvar i = 0\nvar 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\nwhile i <= j {\nlet m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1\n} else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1\n} else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -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\nvar i: usize = 0;\nvar 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\nwhile (i <= j) {\nvar m = i + (j - i) / 2;                // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums.items[m] < target) {           // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\n} else if (nums.items[m] > target) {    // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\n} else {                                // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn @intCast(T, m);\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -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\nint 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\nwhile (i <= j) {\nint m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target) {\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\n} else if (nums[m] > target) {\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n

    \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u3002\u6bcf\u8f6e\u7f29\u5c0f\u4e00\u534a\u533a\u95f4\uff0c\u56e0\u6b64\u4e8c\u5206\u5faa\u73af\u6b21\u6570\u4e3a \\(\\log_2 n\\) \u3002

    \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002\u6307\u9488 i , 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\u7684\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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart binary_search.java
    /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nint binarySearchLCRO(int[] nums, int target) {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nint 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\nwhile (i < j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\nelse if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n
    binary_search.cpp
    /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nint binarySearchLCRO(vector<int> &nums, int target) {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nint 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\nwhile (i < j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)    // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\nelse if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n
    binary_search.py
    def binary_search_lcro(nums: list[int], target: int) -> int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09\"\"\"\n# \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\ni, 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\nwhile i < j:\nm = (i + j) // 2  # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target:\ni = m + 1  # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\nelif nums[m] > target:\nj = m  # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nelse:\nreturn m  # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn -1  # \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n
    binary_search.go
    /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nfunc binarySearchLCRO(nums []int, target int) int {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\ni, 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\nfor i < j {\nm := i + (j-i)/2      // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1\n} else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m\n} else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1\n}\n
    binary_search.js
    /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nfunction binarySearchLCRO(nums, target) {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nlet i = 0,\nj = nums.length;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nwhile (i < j) {\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m \uff0c\u4f7f\u7528 parseInt() \u5411\u4e0b\u53d6\u6574\nconst m = parseInt(i + (j - i) / 2);\nif (nums[m] < target)\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\nelse if (nums[m] > target)\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nelse return m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n
    binary_search.ts
    /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nfunction binarySearchLCRO(nums: number[], target: number): number {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nlet i = 0,\nj = nums.length;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nwhile (i < j) {\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nconst m = Math.floor(i + (j - i) / 2);\nif (nums[m] < target) {\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\n} else if (nums[m] > target) {\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\n
    binary_search.c
    /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nint binarySearchLCRO(int *nums, int len, int target) {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nint 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\nwhile (i < j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)    // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\nelse if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n
    binary_search.cs
    /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nint binarySearchLCRO(int[] nums, int target) {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nint 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\nwhile (i < j) {\nint m = i + (j - i) / 2;   // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)      // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\nelse if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\nelse                       // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n
    binary_search.swift
    /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nfunc binarySearchLCRO(nums: [Int], target: Int) -> Int {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nvar i = 0\nvar 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\nwhile i < j {\nlet m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1\n} else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m\n} else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1\n}\n
    binary_search.zig
    // \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09\nfn binarySearchLCRO(comptime T: type, nums: std.ArrayList(T), target: T) T {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nvar i: usize = 0;\nvar 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\nwhile (i <= j) {\nvar m = i + (j - i) / 2;                // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums.items[m] < target) {           // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\n} else if (nums.items[m] > target) {    // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\n} else {                                // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn @intCast(T, m);\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -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 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nint 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\nwhile (i < j) {\nint m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target) {\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\n} else if (nums[m] > target) {\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n

    \u5982\u4e0b\u56fe\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

    \u5728\u201c\u53cc\u95ed\u533a\u95f4\u201d\u8868\u793a\u6cd5\u4e2d\uff0c\u7531\u4e8e\u5de6\u53f3\u8fb9\u754c\u90fd\u88ab\u5b9a\u4e49\u4e3a\u95ed\u533a\u95f4\uff0c\u56e0\u6b64\u6307\u9488 \\(i\\) \u548c \\(j\\) \u7f29\u5c0f\u533a\u95f4\u64cd\u4f5c\u4e5f\u662f\u5bf9\u79f0\u7684\u3002\u8fd9\u6837\u66f4\u4e0d\u5bb9\u6613\u51fa\u9519\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u901a\u5e38\u91c7\u7528\u201c\u53cc\u95ed\u533a\u95f4\u201d\u7684\u5199\u6cd5\u3002

    Fig. \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\uff1a

    • \u4e8c\u5206\u67e5\u627e\u7684\u65f6\u95f4\u6548\u7387\u9ad8\u3002\u5728\u5927\u6570\u636e\u91cf\u4e0b\uff0c\u5bf9\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5177\u6709\u663e\u8457\u4f18\u52bf\u3002\u4f8b\u5982\uff0c\u5f53\u6570\u636e\u5927\u5c0f \\(n = 2^{20}\\) \u65f6\uff0c\u7ebf\u6027\u67e5\u627e\u9700\u8981 \\(2^{20} = 1048576\\) \u8f6e\u5faa\u73af\uff0c\u800c\u4e8c\u5206\u67e5\u627e\u4ec5\u9700 \\(\\log_2 2^{20} = 20\\) \u8f6e\u5faa\u73af\u3002
    • \u4e8c\u5206\u67e5\u627e\u65e0\u9700\u989d\u5916\u7a7a\u95f4\u3002\u76f8\u8f83\u4e8e\u9700\u8981\u501f\u52a9\u989d\u5916\u7a7a\u95f4\u7684\u641c\u7d22\u7b97\u6cd5\uff08\u4f8b\u5982\u54c8\u5e0c\u67e5\u627e\uff09\uff0c\u4e8c\u5206\u67e5\u627e\u66f4\u52a0\u8282\u7701\u7a7a\u95f4\u3002

    \u7136\u800c\uff0c\u4e8c\u5206\u67e5\u627e\u5e76\u975e\u9002\u7528\u4e8e\u6240\u6709\u60c5\u51b5\uff0c\u539f\u56e0\u5982\u4e0b\uff1a

    • \u4e8c\u5206\u67e5\u627e\u4ec5\u9002\u7528\u4e8e\u6709\u5e8f\u6570\u636e\u3002\u82e5\u8f93\u5165\u6570\u636e\u65e0\u5e8f\uff0c\u4e3a\u4e86\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u800c\u4e13\u95e8\u8fdb\u884c\u6392\u5e8f\uff0c\u5f97\u4e0d\u507f\u5931\u3002\u56e0\u4e3a\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4e3a \\(O(n \\log n)\\) \uff0c\u6bd4\u7ebf\u6027\u67e5\u627e\u548c\u4e8c\u5206\u67e5\u627e\u90fd\u66f4\u9ad8\u3002\u5bf9\u4e8e\u9891\u7e41\u63d2\u5165\u5143\u7d20\u7684\u573a\u666f\uff0c\u4e3a\u4fdd\u6301\u6570\u7ec4\u6709\u5e8f\u6027\uff0c\u9700\u8981\u5c06\u5143\u7d20\u63d2\u5165\u5230\u7279\u5b9a\u4f4d\u7f6e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u4e5f\u662f\u975e\u5e38\u6602\u8d35\u7684\u3002
    • \u4e8c\u5206\u67e5\u627e\u4ec5\u9002\u7528\u4e8e\u6570\u7ec4\u3002\u4e8c\u5206\u67e5\u627e\u9700\u8981\u8df3\u8dc3\u5f0f\uff08\u975e\u8fde\u7eed\u5730\uff09\u8bbf\u95ee\u5143\u7d20\uff0c\u800c\u5728\u94fe\u8868\u4e2d\u6267\u884c\u8df3\u8dc3\u5f0f\u8bbf\u95ee\u7684\u6548\u7387\u8f83\u4f4e\uff0c\u56e0\u6b64\u4e0d\u9002\u5408\u5e94\u7528\u5728\u94fe\u8868\u6216\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u3002
    • \u5c0f\u6570\u636e\u91cf\u4e0b\uff0c\u7ebf\u6027\u67e5\u627e\u6027\u80fd\u66f4\u4f73\u3002\u5728\u7ebf\u6027\u67e5\u627e\u4e2d\uff0c\u6bcf\u8f6e\u53ea\u9700\u8981 1 \u6b21\u5224\u65ad\u64cd\u4f5c\uff1b\u800c\u5728\u4e8c\u5206\u67e5\u627e\u4e2d\uff0c\u9700\u8981 1 \u6b21\u52a0\u6cd5\u30011 \u6b21\u9664\u6cd5\u30011 ~ 3 \u6b21\u5224\u65ad\u64cd\u4f5c\u30011 \u6b21\u52a0\u6cd5\uff08\u51cf\u6cd5\uff09\uff0c\u5171 4 ~ 6 \u4e2a\u5355\u5143\u64cd\u4f5c\uff1b\u56e0\u6b64\uff0c\u5f53\u6570\u636e\u91cf \\(n\\) \u8f83\u5c0f\u65f6\uff0c\u7ebf\u6027\u67e5\u627e\u53cd\u800c\u6bd4\u4e8c\u5206\u67e5\u627e\u66f4\u5feb\u3002
    "},{"location":"chapter_searching/binary_search_edge/","title":"10.2. \u00a0 \u4e8c\u5206\u67e5\u627e\u8fb9\u754c","text":"

    \u5728\u4e0a\u4e00\u8282\u4e2d\uff0c\u9898\u76ee\u89c4\u5b9a\u6570\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u90fd\u662f\u552f\u4e00\u7684\u3002\u5982\u679c\u76ee\u6807\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u591a\u6b21\u51fa\u73b0\uff0c\u4e0a\u8282\u4ecb\u7ecd\u7684\u65b9\u6cd5\u53ea\u80fd\u4fdd\u8bc1\u8fd4\u56de\u5176\u4e2d\u4e00\u4e2a\u76ee\u6807\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u800c\u65e0\u6cd5\u786e\u5b9a\u8be5\u7d22\u5f15\u7684\u5de6\u8fb9\u548c\u53f3\u8fb9\u8fd8\u6709\u591a\u5c11\u76ee\u6807\u5143\u7d20\u3002

    Question

    \u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6709\u5e8f\u6570\u7ec4 nums \uff0c\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\u3002\u8bf7\u67e5\u627e\u5e76\u8fd4\u56de\u5143\u7d20 target \u5728\u6570\u7ec4\u4e2d\u9996\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u3002\u82e5\u6570\u7ec4\u4e2d\u4e0d\u5305\u542b\u8be5\u5143\u7d20\uff0c\u5219\u8fd4\u56de \\(-1\\) \u3002

    "},{"location":"chapter_searching/binary_search_edge/#1021","title":"10.2.1. \u00a0 \u7b80\u5355\u65b9\u6cd5","text":"

    \u4e3a\u4e86\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5de6\u8fb9\u7684 target \uff0c\u6211\u4eec\u53ef\u4ee5\u5206\u4e3a\u4e24\u6b65\uff1a

    1. \u8fdb\u884c\u4e8c\u5206\u67e5\u627e\uff0c\u5b9a\u4f4d\u5230\u4efb\u610f\u4e00\u4e2a target \u7684\u7d22\u5f15\uff0c\u8bb0\u4e3a \\(k\\) \uff1b
    2. \u4ee5\u7d22\u5f15 \\(k\\) \u4e3a\u8d77\u59cb\u70b9\uff0c\u5411\u5de6\u8fdb\u884c\u7ebf\u6027\u904d\u5386\uff0c\u627e\u5230\u6700\u5de6\u8fb9\u7684 target \u8fd4\u56de\u5373\u53ef\u3002

    Fig. \u7ebf\u6027\u67e5\u627e\u6700\u5de6\u8fb9\u7684\u5143\u7d20

    \u8fd9\u4e2a\u65b9\u6cd5\u867d\u7136\u6709\u6548\uff0c\u4f46\u7531\u4e8e\u5305\u542b\u7ebf\u6027\u67e5\u627e\uff0c\u5176\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u80fd\u4f1a\u52a3\u5316\u81f3 \\(O(n)\\) \u3002

    "},{"location":"chapter_searching/binary_search_edge/#1022","title":"10.2.2. \u00a0 \u4e8c\u5206\u65b9\u6cd5","text":"

    \u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u4ec5\u901a\u8fc7\u4e8c\u5206\u67e5\u627e\u89e3\u51b3\u4ee5\u4e0a\u95ee\u9898\u3002\u6574\u4f53\u7b97\u6cd5\u6d41\u7a0b\u4e0d\u53d8\uff0c\u5148\u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 \\(m\\) \uff0c\u518d\u5224\u65ad target \u548c nums[m] \u5927\u5c0f\u5173\u7cfb\uff1a

    • \u5f53 nums[m] < target \u6216 nums[m] > target \u65f6\uff0c\u8bf4\u660e\u8fd8\u6ca1\u6709\u627e\u5230 target \uff0c\u56e0\u6b64\u91c7\u53d6\u4e0e\u4e0a\u8282\u4ee3\u7801\u76f8\u540c\u7684\u7f29\u5c0f\u533a\u95f4\u64cd\u4f5c\uff0c\u4ece\u800c\u4f7f\u6307\u9488 \\(i\\) \u548c \\(j\\) \u5411 target \u9760\u8fd1\u3002
    • \u5f53 nums[m] == target \u65f6\uff0c\u8bf4\u660e\u201c\u5c0f\u4e8e target \u7684\u5143\u7d20\u201d\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

    \u4e8c\u5206\u67e5\u627e\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\u8fd4\u56de\u7d22\u5f15 \\(i\\) \u5373\u53ef\u3002

    <1><2><3><4><5><6><7><8>

    \u6ce8\u610f\uff0c\u6570\u7ec4\u53ef\u80fd\u4e0d\u5305\u542b\u76ee\u6807\u5143\u7d20 target \u3002\u56e0\u6b64\u5728\u51fd\u6570\u8fd4\u56de\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u5224\u65ad nums[i] \u4e0e target \u662f\u5426\u76f8\u7b49\uff0c\u4ee5\u53ca\u7d22\u5f15 \\(i\\) \u662f\u5426\u8d8a\u754c\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart binary_search_edge.java
    /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a\u5143\u7d20 */\nint binarySearchLeftEdge(int[] nums, int target) {\nint i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelse if (nums[m] > target)\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse\nj = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n}\nif (i == nums.length || nums[i] != target)\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn i;\n}\n
    binary_search_edge.cpp
    /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a\u5143\u7d20 */\nint binarySearchLeftEdge(vector<int> &nums, int target) {\nint i = 0, j = nums.size() - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelse if (nums[m] > target)\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse\nj = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n}\nif (i == nums.size() || nums[i] != target)\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn i;\n}\n
    binary_search_edge.py
    def binary_search_left_edge(nums: list[int], target: int) -> int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a\u5143\u7d20\"\"\"\ni, j = 0, len(nums) - 1  # \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile i <= j:\nm = (i + j) // 2  # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target:\ni = m + 1  # target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelif nums[m] > target:\nj = m - 1  # target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse:\nj = m - 1  # \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\nif i == len(nums) or nums[i] != target:\nreturn -1  # \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn i\n
    binary_search_edge.go
    /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a\u5143\u7d20 */\nfunc binarySearchLeftEdge(nums []int, target int) int {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\ni, j := 0, len(nums)-1\nfor i <= j {\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nm := i + (j-i)/2\nif nums[m] < target {\n// target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1\n} else if nums[m] > target {\n// target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1\n} else {\n// \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1\n}\n}\nif i == len(nums) || nums[i] != target {\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1\n}\nreturn i\n}\n
    binary_search_edge.js
    /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a\u5143\u7d20 */\nfunction binarySearchLeftEdge(nums, target) {\nlet i = 0,\nj = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nlet m = Math.floor((i + j) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target) {\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if (nums[m] > target) {\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nj = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n}\n}\nif (i == nums.length || nums[i] != target) {\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\nreturn i;\n}\n
    binary_search_edge.ts
    /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a\u5143\u7d20 */\nfunction binarySearchLeftEdge(nums: number[], target: number): number {\nlet i = 0, j = nums.length - 1;  // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nlet m = Math.floor((i + j) / 2);  // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target) {\ni = m + 1;  // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if (nums[m] > target) {\nj = m - 1;  // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nj = m - 1;  // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n}\n}\nif (i == nums.length || nums[i] != target) {\nreturn -1;  // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\nreturn i;\n}\n
    binary_search_edge.c
    /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a\u5143\u7d20 */\nint binarySearchLeftEdge(int *nums, int size, int target) {\nint i = 0, j = size - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelse if (nums[m] > target)\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse\nj = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n}\nif (i == size || nums[i] != target)\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn i;\n}\n
    binary_search_edge.cs
    /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a\u5143\u7d20 */\nint binarySearchLeftEdge(int[] nums, int target) {\nint i = 0, j = nums.Length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelse if (nums[m] > target)\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse\nj = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n}\nif (i == nums.Length || nums[i] != target)\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn i;\n}\n
    binary_search_edge.swift
    /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a\u5143\u7d20 */\nfunc binarySearchLeftEdge(nums: [Int], target: Int) -> Int {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nvar i = 0\nvar j = nums.count - 1\nwhile i <= j {\nlet m = i + (j - 1) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target {\ni = m + 1 // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if nums[m] > target {\nj = m - 1 // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nj = m - 1 // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n}\n}\nif i == nums.count || nums[i] != target {\nreturn -1 // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\nreturn i\n}\n
    binary_search_edge.zig
    [class]{}-[func]{binarySearchLeftEdge}\n
    binary_search_edge.dart
    /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a\u5143\u7d20 */\nint binarySearchLeftEdge(List<int> nums, int target) {\nint i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nint m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u95f4\u7d22\u5f15 m\nif (nums[m] < target)\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelse if (nums[m] > target)\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse\nj = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n}\nif (i == nums.length || nums[i] != target) return -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn i;\n}\n
    "},{"location":"chapter_searching/binary_search_edge/#1023","title":"10.2.3. \u00a0 \u67e5\u627e\u53f3\u8fb9\u754c","text":"

    \u7c7b\u4f3c\u5730\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4e8c\u5206\u67e5\u627e\u6700\u53f3\u8fb9\u7684 target \u3002\u5f53 nums[m] == target \u65f6\uff0c\u8bf4\u660e\u5927\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 \\([m + 1, j]\\) \u4e2d\uff0c\u56e0\u6b64\u6267\u884c i = m + 1 \uff0c\u4f7f\u5f97\u6307\u9488 \\(i\\) \u5411\u5927\u4e8e target \u7684\u5143\u7d20\u9760\u8fd1\u3002

    \u5b8c\u6210\u4e8c\u5206\u540e\uff0c\\(i\\) \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\uff0c\\(j\\) \u6307\u5411\u6700\u53f3\u8fb9\u7684 target \uff0c\u56e0\u6b64\u8fd4\u56de\u7d22\u5f15 \\(j\\) \u5373\u53ef\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart binary_search_edge.java
    /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a\u5143\u7d20 */\nint binarySearchRightEdge(int[] nums, int target) {\nint i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelse if (nums[m] > target)\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse\ni = m + 1; // \u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [m+1, j] \u4e2d\n}\nif (j < 0 || nums[j] != target)\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn j;\n}\n
    binary_search_edge.cpp
    /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a\u5143\u7d20 */\nint binarySearchRightEdge(vector<int> &nums, int target) {\nint i = 0, j = nums.size() - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelse if (nums[m] > target)\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse\ni = m + 1; // \u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [m+1, j] \u4e2d\n}\nif (j < 0 || nums[j] != target)\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn j;\n}\n
    binary_search_edge.py
    def binary_search_right_edge(nums: list[int], target: int) -> int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a\u5143\u7d20\"\"\"\ni, j = 0, len(nums) - 1  # \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile i <= j:\nm = (i + j) // 2  # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target:\ni = m + 1  # target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelif nums[m] > target:\nj = m - 1  # target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse:\ni = m + 1  # \u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [m+1, j] \u4e2d\nif j == len(nums) or nums[j] != target:\nreturn -1  # \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn j\n
    binary_search_edge.go
    /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a\u5143\u7d20 */\nfunc binarySearchRightEdge(nums []int, target int) int {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\ni, j := 0, len(nums)-1\nfor i <= j {\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nm := i + (j-i)/2\nif nums[m] < target {\n// target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1\n} else if nums[m] > target {\n// target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1\n} else {\n// \u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1\n}\n}\nif j < 0 || nums[j] != target {\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1\n}\nreturn j\n}\n
    binary_search_edge.js
    /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a\u5143\u7d20 */\nfunction binarySearchRightEdge(nums, target) {\nlet i = 0,\nj = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nlet m = Math.floor((i + j) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target) {\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if (nums[m] > target) {\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\ni = m + 1; // \u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [m+1, j] \u4e2d\n}\n}\nif (j == nums.length || nums[j] != target) {\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\nreturn j;\n}\n
    binary_search_edge.ts
    /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a\u5143\u7d20 */\nfunction binarySearchRightEdge(nums: number[], target: number): number {\nlet i = 0, j = nums.length - 1;  // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nlet m = Math.floor((i + j) / 2);  // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target) {\ni = m + 1;  // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if (nums[m] > target) {\nj = m - 1;  // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\ni = m + 1;  // \u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [m+1, j] \u4e2d\n}\n}\nif (j == nums.length || nums[j] != target) {\nreturn -1;  // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\nreturn j;\n}\n
    binary_search_edge.c
    /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a\u5143\u7d20 */\nint binarySearchRightEdge(int *nums, int size, int target) {\nint i = 0, j = size - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelse if (nums[m] > target)\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse\ni = m + 1; // \u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [m+1, j] \u4e2d\n}\nif (j < 0 || nums[j] != target)\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn j;\n}\n
    binary_search_edge.cs
    /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a\u5143\u7d20 */\nint binarySearchRightEdge(int[] nums, int target) {\nint i = 0, j = nums.Length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelse if (nums[m] > target)\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse\ni = m + 1; // \u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [m+1, j] \u4e2d\n}\nif (j < 0 || nums[j] != target)\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn j;\n}\n
    binary_search_edge.swift
    /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a\u5143\u7d20 */\nfunc binarySearchRightEdge(nums: [Int], target: Int) -> Int {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nvar i = 0\nvar j = nums.count - 1\nwhile i <= j {\nlet m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target {\ni = m + 1 // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if nums[m] > target {\nj = m - 1 // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\ni = m + 1 // \u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [m+1, j] \u4e2d\n}\n}\nif j < 0 || nums[j] != target {\nreturn -1 // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\nreturn j\n}\n
    binary_search_edge.zig
    [class]{}-[func]{binarySearchRightEdge}\n
    binary_search_edge.dart
    /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a\u5143\u7d20 */\nint binarySearchRightEdge(List<int> nums, int target) {\nint i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nint m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u95f4\u7d22\u5f15 m\nif (nums[m] < target)\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelse if (nums[m] > target)\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse\ni = m + 1; // \u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [m+1, j] \u4e2d\n}\nif (j < 0 || nums[j] != target) return -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn j;\n}\n

    \u89c2\u5bdf\u4e0b\u56fe\uff0c\u641c\u7d22\u6700\u53f3\u8fb9\u5143\u7d20\u65f6\u6307\u9488 \\(j\\) \u7684\u4f5c\u7528\u4e0e\u641c\u7d22\u6700\u5de6\u8fb9\u5143\u7d20\u65f6\u6307\u9488 \\(i\\) \u7684\u4f5c\u7528\u4e00\u81f4\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u641c\u7d22\u6700\u5de6\u8fb9\u5143\u7d20\u548c\u6700\u53f3\u8fb9\u5143\u7d20\u7684\u5b9e\u73b0\u662f\u955c\u50cf\u5bf9\u79f0\u7684\u3002

    Fig. \u67e5\u627e\u6700\u5de6\u8fb9\u548c\u6700\u53f3\u8fb9\u5143\u7d20\u7684\u5bf9\u79f0\u6027

    Tip

    \u4ee5\u4e0a\u4ee3\u7801\u91c7\u53d6\u7684\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

    "},{"location":"chapter_searching/replace_linear_by_hashing/","title":"10.3. \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

    "},{"location":"chapter_searching/replace_linear_by_hashing/#1031","title":"10.3.1. \u00a0 \u7ebf\u6027\u67e5\u627e\uff1a\u4ee5\u65f6\u95f4\u6362\u7a7a\u95f4","text":"

    \u8003\u8651\u76f4\u63a5\u904d\u5386\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\u3002\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

    Fig. \u7ebf\u6027\u67e5\u627e\u6c42\u89e3\u4e24\u6570\u4e4b\u548c

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart two_sum.java
    /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nint[] twoSumBruteForce(int[] nums, int target) {\nint size = nums.length;\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor (int i = 0; i < size - 1; i++) {\nfor (int j = i + 1; j < size; j++) {\nif (nums[i] + nums[j] == target)\nreturn new int[] { i, j };\n}\n}\nreturn new int[0];\n}\n
    two_sum.cpp
    /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nvector<int> twoSumBruteForce(vector<int> &nums, int target) {\nint size = nums.size();\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor (int i = 0; i < size - 1; i++) {\nfor (int j = i + 1; j < size; j++) {\nif (nums[i] + nums[j] == target)\nreturn {i, j};\n}\n}\nreturn {};\n}\n
    two_sum.py
    def 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 O(n^2)\nfor i in range(len(nums) - 1):\nfor j in range(i + 1, len(nums)):\nif nums[i] + nums[j] == target:\nreturn [i, j]\nreturn []\n
    two_sum.go
    /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunc twoSumBruteForce(nums []int, target int) []int {\nsize := len(nums)\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor i := 0; i < size-1; i++ {\nfor j := i + 1; i < size; j++ {\nif nums[i]+nums[j] == target {\nreturn []int{i, j}\n}\n}\n}\nreturn nil\n}\n
    two_sum.js
    /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunction twoSumBruteForce(nums, target) {\nconst n = nums.length;\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor (let i = 0; i < n; i++) {\nfor (let j = i + 1; j < n; j++) {\nif (nums[i] + nums[j] === target) {\nreturn [i, j];\n}\n}\n}\nreturn [];\n}\n
    two_sum.ts
    /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunction twoSumBruteForce(nums: number[], target: number): number[] {\nconst n = nums.length;\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor (let i = 0; i < n; i++) {\nfor (let j = i + 1; j < n; j++) {\nif (nums[i] + nums[j] === target) {\nreturn [i, j];\n}\n}\n}\nreturn [];\n}\n
    two_sum.c
    /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nint *twoSumBruteForce(int *nums, int numsSize, int target, int *returnSize) {\nfor (int i = 0; i < numsSize; ++i) {\nfor (int j = i + 1; j < numsSize; ++j) {\nif (nums[i] + nums[j] == target) {\nint *res = malloc(sizeof(int) * 2);\nres[0] = i, res[1] = j;\n*returnSize = 2;\nreturn res;\n}\n}\n}\n*returnSize = 0;\nreturn NULL;\n}\n
    two_sum.cs
    /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nint[] twoSumBruteForce(int[] nums, int target) {\nint size = nums.Length;\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor (int i = 0; i < size - 1; i++) {\nfor (int j = i + 1; j < size; j++) {\nif (nums[i] + nums[j] == target)\nreturn new int[] { i, j };\n}\n}\nreturn new int[0];\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 O(n^2)\nfor i in nums.indices.dropLast() {\nfor j in nums.indices.dropFirst(i + 1) {\nif nums[i] + nums[j] == target {\nreturn [i, j]\n}\n}\n}\nreturn [0]\n}\n
    two_sum.zig
    // \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e\nfn twoSumBruteForce(nums: []i32, target: i32) ?[2]i32 {\nvar size: usize = nums.len;\nvar i: usize = 0;\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nwhile (i < size - 1) : (i += 1) {\nvar j = i + 1;\nwhile (j < size) : (j += 1) {\nif (nums[i] + nums[j] == target) {\nreturn [_]i32{@intCast(i32, i), @intCast(i32, j)};\n}\n}\n}\nreturn null;\n}\n
    two_sum.dart
    /* \u65b9\u6cd5\u4e00\uff1a \u66b4\u529b\u679a\u4e3e */\nList<int> twoSumBruteForce(List<int> nums, int target) {\nint size = nums.length;\nfor (var i = 0; i < size - 1; i++) {\nfor (var j = i + 1; j < size; j++) {\nif (nums[i] + nums[j] == target) return [i, j];\n}\n}\nreturn [0];\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/#1032","title":"10.3.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\uff1a

    1. \u5224\u65ad\u6570\u5b57 target - nums[i] \u662f\u5426\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u82e5\u662f\u5219\u76f4\u63a5\u8fd4\u56de\u8fd9\u4e24\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\uff1b
    2. \u5c06\u952e\u503c\u5bf9 nums[i] \u548c\u7d22\u5f15 i \u6dfb\u52a0\u8fdb\u54c8\u5e0c\u8868\uff1b
    <1><2><3>

    \u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff0c\u4ec5\u9700\u5355\u5c42\u5faa\u73af\u5373\u53ef\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart two_sum.java
    /* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nint[] twoSumHashTable(int[] nums, int target) {\nint size = nums.length;\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nMap<Integer, Integer> dic = new HashMap<>();\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor (int i = 0; i < size; i++) {\nif (dic.containsKey(target - nums[i])) {\nreturn new int[] { dic.get(target - nums[i]), i };\n}\ndic.put(nums[i], i);\n}\nreturn new int[0];\n}\n
    two_sum.cpp
    /* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nvector<int> twoSumHashTable(vector<int> &nums, int target) {\nint size = nums.size();\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nunordered_map<int, int> dic;\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor (int i = 0; i < size; i++) {\nif (dic.find(target - nums[i]) != dic.end()) {\nreturn {dic[target - nums[i]], i};\n}\ndic.emplace(nums[i], i);\n}\nreturn {};\n}\n
    two_sum.py
    def 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 O(n)\ndic = {}\n# \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor i in range(len(nums)):\nif target - nums[i] in dic:\nreturn [dic[target - nums[i]], i]\ndic[nums[i]] = i\nreturn []\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 O(n)\nhashTable := map[int]int{}\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor idx, val := range nums {\nif preIdx, ok := hashTable[target-val]; ok {\nreturn []int{preIdx, idx}\n}\nhashTable[val] = idx\n}\nreturn nil\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 O(n)\nlet m = {};\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor (let i = 0; i < nums.length; i++) {\nif (m[nums[i]] !== undefined) {\nreturn [m[nums[i]], i];\n} else {\nm[target - nums[i]] = i;\n}\n}\nreturn [];\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 O(n)\nlet m: Map<number, number> = new Map();\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor (let i = 0; i < nums.length; i++) {\nlet index = m.get(nums[i]);\nif (index !== undefined) {\nreturn [index, i];\n} else {\nm.set(target - nums[i], i);\n}\n}\nreturn [];\n}\n
    two_sum.c
    /* \u54c8\u5e0c\u8868 */\nstruct hashTable {\nint key;\nint val;\nUT_hash_handle hh; // \u57fa\u4e8e uthash.h \u5b9e\u73b0\n};\ntypedef struct hashTable hashTable;\n/* \u54c8\u5e0c\u8868\u67e5\u8be2 */\nhashTable *find(hashTable *h, int key) {\nhashTable *tmp;\nHASH_FIND_INT(h, &key, tmp);\nreturn tmp;\n}\n/* \u54c8\u5e0c\u8868\u5143\u7d20\u63d2\u5165 */\nvoid insert(hashTable *h, int key, int val) {\nhashTable *t = find(h, key);\nif (t == NULL) {\nhashTable *tmp = malloc(sizeof(hashTable));\ntmp->key = key, tmp->val = val;\nHASH_ADD_INT(h, key, tmp);\n} else {\nt->val = val;\n}\n}\n/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nint *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) {\nhashTable *hashtable = NULL;\nfor (int i = 0; i < numsSize; i++) {\nhashTable *t = find(hashtable, target - nums[i]);\nif (t != NULL) {\nint *res = malloc(sizeof(int) * 2);\nres[0] = t->val, res[1] = i;\n*returnSize = 2;\nreturn res;\n}\ninsert(hashtable, nums[i], i);\n}\n*returnSize = 0;\nreturn NULL;\n}\n
    two_sum.cs
    /* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nint[] twoSumHashTable(int[] nums, int target) {\nint size = nums.Length;\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nDictionary<int, int> dic = new();\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor (int i = 0; i < size; i++) {\nif (dic.ContainsKey(target - nums[i])) {\nreturn new int[] { dic[target - nums[i]], i };\n}\ndic.Add(nums[i], i);\n}\nreturn new int[0];\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 O(n)\nvar dic: [Int: Int] = [:]\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor i in nums.indices {\nif let j = dic[target - nums[i]] {\nreturn [j, i]\n}\ndic[nums[i]] = i\n}\nreturn [0]\n}\n
    two_sum.zig
    // \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868\nfn twoSumHashTable(nums: []i32, target: i32) !?[2]i32 {\nvar size: usize = nums.len;\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nvar dic = std.AutoHashMap(i32, i32).init(std.heap.page_allocator);\ndefer dic.deinit();\nvar i: usize = 0;\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nwhile (i < size) : (i += 1) {\nif (dic.contains(target - nums[i])) {\nreturn [_]i32{dic.get(target - nums[i]).?, @intCast(i32, i)};\n}\ntry dic.put(nums[i], @intCast(i32, i));\n}\nreturn null;\n}\n
    two_sum.dart
    /* \u65b9\u6cd5\u4e8c\uff1a \u8f85\u52a9\u54c8\u5e0c\u8868 */\nList<int> twoSumHashTable(List<int> nums, int target) {\nint size = nums.length;\nMap<int, int> dic = HashMap();\nfor (var i = 0; i < size; i++) {\nif (dic.containsKey(target - nums[i])) {\nreturn [dic[target - nums[i]]!, i];\n}\ndic.putIfAbsent(nums[i], () => i);\n}\nreturn [0];\n}\n

    \u6b64\u65b9\u6cd5\u901a\u8fc7\u54c8\u5e0c\u67e5\u627e\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n^2)\\) \u964d\u4f4e\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.4. \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

    \u6839\u636e\u5b9e\u73b0\u601d\u8def\uff0c\u641c\u7d22\u7b97\u6cd5\u603b\u4f53\u53ef\u5206\u4e3a\u4e24\u79cd\uff1a

    • \u901a\u8fc7\u904d\u5386\u6570\u636e\u7ed3\u6784\u6765\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\uff0c\u4f8b\u5982\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6811\u548c\u56fe\u7684\u904d\u5386\u7b49\u3002
    • \u5229\u7528\u6570\u636e\u7ec4\u7ec7\u7ed3\u6784\u6216\u6570\u636e\u5305\u542b\u7684\u5148\u9a8c\u4fe1\u606f\uff0c\u5b9e\u73b0\u9ad8\u6548\u5143\u7d20\u67e5\u627e\uff0c\u4f8b\u5982\u4e8c\u5206\u67e5\u627e\u3001\u54c8\u5e0c\u67e5\u627e\u548c\u4e8c\u53c9\u641c\u7d22\u6811\u67e5\u627e\u7b49\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/#1041","title":"10.4.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

    • \u300c\u7ebf\u6027\u641c\u7d22\u300d\u9002\u7528\u4e8e\u6570\u7ec4\u548c\u94fe\u8868\u7b49\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u3002\u5b83\u4ece\u6570\u636e\u7ed3\u6784\u7684\u4e00\u7aef\u5f00\u59cb\uff0c\u9010\u4e2a\u8bbf\u95ee\u5143\u7d20\uff0c\u76f4\u5230\u627e\u5230\u76ee\u6807\u5143\u7d20\u6216\u5230\u8fbe\u53e6\u4e00\u7aef\u4ecd\u6ca1\u6709\u627e\u5230\u76ee\u6807\u5143\u7d20\u4e3a\u6b62\u3002
    • \u300c\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u300d\u548c\u300c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u300d\u662f\u56fe\u548c\u6811\u7684\u4e24\u79cd\u904d\u5386\u7b56\u7565\u3002\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u4ece\u521d\u59cb\u8282\u70b9\u5f00\u59cb\u9010\u5c42\u641c\u7d22\uff0c\u7531\u8fd1\u53ca\u8fdc\u5730\u8bbf\u95ee\u5404\u4e2a\u8282\u70b9\u3002\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u662f\u4ece\u521d\u59cb\u8282\u70b9\u5f00\u59cb\uff0c\u6cbf\u7740\u4e00\u6761\u8def\u5f84\u8d70\u5230\u5934\u4e3a\u6b62\uff0c\u518d\u56de\u6eaf\u5e76\u5c1d\u8bd5\u5176\u4ed6\u8def\u5f84\uff0c\u76f4\u5230\u904d\u5386\u5b8c\u6574\u4e2a\u6570\u636e\u7ed3\u6784\u3002

    \u66b4\u529b\u641c\u7d22\u7684\u4f18\u70b9\u662f\u7b80\u5355\u4e14\u901a\u7528\u6027\u597d\uff0c\u65e0\u9700\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/#1042","title":"10.4.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

    • \u300c\u4e8c\u5206\u67e5\u627e\u300d\u5229\u7528\u6570\u636e\u7684\u6709\u5e8f\u6027\u5b9e\u73b0\u9ad8\u6548\u67e5\u627e\uff0c\u4ec5\u9002\u7528\u4e8e\u6570\u7ec4\u3002
    • \u300c\u54c8\u5e0c\u67e5\u627e\u300d\u5229\u7528\u54c8\u5e0c\u8868\u5c06\u641c\u7d22\u6570\u636e\u548c\u76ee\u6807\u6570\u636e\u5efa\u7acb\u4e3a\u952e\u503c\u5bf9\u6620\u5c04\uff0c\u4ece\u800c\u5b9e\u73b0\u67e5\u8be2\u64cd\u4f5c\u3002
    • \u300c\u6811\u67e5\u627e\u300d\u5728\u7279\u5b9a\u7684\u6811\u7ed3\u6784\uff08\u4f8b\u5982\u4e8c\u53c9\u641c\u7d22\u6811\uff09\u4e2d\uff0c\u57fa\u4e8e\u6bd4\u8f83\u8282\u70b9\u503c\u6765\u5feb\u901f\u6392\u9664\u8282\u70b9\uff0c\u4ece\u800c\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\u652f\u3002

    Note

    \u81ea\u9002\u5e94\u641c\u7d22\u7b97\u6cd5\u5e38\u88ab\u79f0\u4e3a\u67e5\u627e\u7b97\u6cd5\uff0c\u4e3b\u8981\u5173\u6ce8\u5728\u7279\u5b9a\u6570\u636e\u7ed3\u6784\u4e2d\u5feb\u901f\u68c0\u7d22\u76ee\u6807\u5143\u7d20\u3002

    "},{"location":"chapter_searching/searching_algorithm_revisited/#1043","title":"10.4.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\u5728\u8be5\u6570\u636e\u4e2d\u641c\u7d22\u76ee\u6807\u5143\u7d20\u3002\u5404\u4e2a\u65b9\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u5982\u4e0b\u56fe\u6240\u793a\u3002

    Fig. \u591a\u79cd\u641c\u7d22\u7b56\u7565

    \u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\u7684\u64cd\u4f5c\u6548\u7387\u4e0e\u7279\u6027\u5982\u4e0b\u8868\u6240\u793a\u3002

    \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

    \u9664\u4e86\u4ee5\u4e0a\u8868\u683c\u5185\u5bb9\uff0c\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

    • \u901a\u7528\u6027\u8f83\u597d\uff0c\u65e0\u9700\u4efb\u4f55\u6570\u636e\u9884\u5904\u7406\u64cd\u4f5c\u3002\u5047\u5982\u6211\u4eec\u4ec5\u9700\u67e5\u8be2\u4e00\u6b21\u6570\u636e\uff0c\u90a3\u4e48\u5176\u4ed6\u4e09\u79cd\u65b9\u6cd5\u7684\u6570\u636e\u9884\u5904\u7406\u7684\u65f6\u95f4\u6bd4\u7ebf\u6027\u641c\u7d22\u7684\u65f6\u95f4\u8fd8\u8981\u66f4\u957f\u3002
    • \u9002\u7528\u4e8e\u4f53\u91cf\u8f83\u5c0f\u7684\u6570\u636e\uff0c\u6b64\u60c5\u51b5\u4e0b\u65f6\u95f4\u590d\u6742\u5ea6\u5bf9\u6548\u7387\u5f71\u54cd\u8f83\u5c0f\u3002
    • \u9002\u7528\u4e8e\u6570\u636e\u66f4\u65b0\u9891\u7387\u8f83\u9ad8\u7684\u573a\u666f\uff0c\u56e0\u4e3a\u8be5\u65b9\u6cd5\u4e0d\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u4efb\u4f55\u989d\u5916\u7ef4\u62a4\u3002

    \u4e8c\u5206\u67e5\u627e

    • \u9002\u7528\u4e8e\u5927\u6570\u636e\u91cf\u7684\u60c5\u51b5\uff0c\u6548\u7387\u8868\u73b0\u7a33\u5b9a\uff0c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u3002
    • \u6570\u636e\u91cf\u4e0d\u80fd\u8fc7\u5927\uff0c\u56e0\u4e3a\u5b58\u50a8\u6570\u7ec4\u9700\u8981\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u3002
    • \u4e0d\u9002\u7528\u4e8e\u9ad8\u9891\u589e\u5220\u6570\u636e\u7684\u573a\u666f\uff0c\u56e0\u4e3a\u7ef4\u62a4\u6709\u5e8f\u6570\u7ec4\u7684\u5f00\u9500\u8f83\u5927\u3002

    \u54c8\u5e0c\u67e5\u627e

    • \u9002\u5408\u5bf9\u67e5\u8be2\u6027\u80fd\u8981\u6c42\u5f88\u9ad8\u7684\u573a\u666f\uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002
    • \u4e0d\u9002\u5408\u9700\u8981\u6709\u5e8f\u6570\u636e\u6216\u8303\u56f4\u67e5\u627e\u7684\u573a\u666f\uff0c\u56e0\u4e3a\u54c8\u5e0c\u8868\u65e0\u6cd5\u7ef4\u62a4\u6570\u636e\u7684\u6709\u5e8f\u6027\u3002
    • \u5bf9\u54c8\u5e0c\u51fd\u6570\u548c\u54c8\u5e0c\u51b2\u7a81\u5904\u7406\u7b56\u7565\u7684\u4f9d\u8d56\u6027\u8f83\u9ad8\uff0c\u5177\u6709\u8f83\u5927\u7684\u6027\u80fd\u52a3\u5316\u98ce\u9669\u3002
    • \u4e0d\u9002\u5408\u6570\u636e\u91cf\u8fc7\u5927\u7684\u60c5\u51b5\uff0c\u56e0\u4e3a\u54c8\u5e0c\u8868\u9700\u8981\u989d\u5916\u7a7a\u95f4\u6765\u6700\u5927\u7a0b\u5ea6\u5730\u51cf\u5c11\u51b2\u7a81\uff0c\u4ece\u800c\u63d0\u4f9b\u826f\u597d\u7684\u67e5\u8be2\u6027\u80fd\u3002

    \u6811\u67e5\u627e

    • \u9002\u7528\u4e8e\u6d77\u91cf\u6570\u636e\uff0c\u56e0\u4e3a\u6811\u8282\u70b9\u5728\u5185\u5b58\u4e2d\u662f\u79bb\u6563\u5b58\u50a8\u7684\u3002
    • \u9002\u5408\u9700\u8981\u7ef4\u62a4\u6709\u5e8f\u6570\u636e\u6216\u8303\u56f4\u67e5\u627e\u7684\u573a\u666f\u3002
    • \u5728\u6301\u7eed\u589e\u5220\u8282\u70b9\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u53ef\u80fd\u4ea7\u751f\u503e\u659c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u52a3\u5316\u81f3 \\(O(n)\\) \u3002
    • \u82e5\u4f7f\u7528 AVL \u6811\u6216\u7ea2\u9ed1\u6811\uff0c\u5219\u5404\u9879\u64cd\u4f5c\u53ef\u5728 \\(O(\\log n)\\) \u6548\u7387\u4e0b\u7a33\u5b9a\u8fd0\u884c\uff0c\u4f46\u7ef4\u62a4\u6811\u5e73\u8861\u7684\u64cd\u4f5c\u4f1a\u589e\u52a0\u989d\u5916\u5f00\u9500\u3002
    "},{"location":"chapter_searching/summary/","title":"10.5. \u00a0 \u5c0f\u7ed3","text":"
    • \u4e8c\u5206\u67e5\u627e\u4f9d\u8d56\u4e8e\u6570\u636e\u7684\u6709\u5e8f\u6027\uff0c\u901a\u8fc7\u5faa\u73af\u9010\u6b65\u7f29\u51cf\u4e00\u534a\u641c\u7d22\u533a\u95f4\u6765\u5b9e\u73b0\u67e5\u627e\u3002\u5b83\u8981\u6c42\u8f93\u5165\u6570\u636e\u6709\u5e8f\uff0c\u4e14\u4ec5\u9002\u7528\u4e8e\u6570\u7ec4\u6216\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u3002
    • \u66b4\u529b\u641c\u7d22\u901a\u8fc7\u904d\u5386\u6570\u636e\u7ed3\u6784\u6765\u5b9a\u4f4d\u6570\u636e\u3002\u7ebf\u6027\u641c\u7d22\u9002\u7528\u4e8e\u6570\u7ec4\u548c\u94fe\u8868\uff0c\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u548c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u9002\u7528\u4e8e\u56fe\u548c\u6811\u3002\u6b64\u7c7b\u7b97\u6cd5\u901a\u7528\u6027\u597d\uff0c\u65e0\u9700\u5bf9\u6570\u636e\u9884\u5904\u7406\uff0c\u4f46\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u8f83\u9ad8\u3002
    • \u54c8\u5e0c\u67e5\u627e\u3001\u6811\u67e5\u627e\u548c\u4e8c\u5206\u67e5\u627e\u5c5e\u4e8e\u9ad8\u6548\u641c\u7d22\u65b9\u6cd5\uff0c\u53ef\u5728\u7279\u5b9a\u6570\u636e\u7ed3\u6784\u4e2d\u5feb\u901f\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\u3002\u6b64\u7c7b\u7b97\u6cd5\u6548\u7387\u9ad8\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe \\(O(\\log n)\\) \u751a\u81f3 \\(O(1)\\) \uff0c\u4f46\u901a\u5e38\u9700\u8981\u501f\u52a9\u989d\u5916\u6570\u636e\u7ed3\u6784\u3002
    • \u5b9e\u9645\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u6570\u636e\u4f53\u91cf\u3001\u641c\u7d22\u6027\u80fd\u8981\u6c42\u3001\u6570\u636e\u67e5\u8be2\u548c\u66f4\u65b0\u9891\u7387\u7b49\u56e0\u7d20\u8fdb\u884c\u5177\u4f53\u5206\u6790\uff0c\u4ece\u800c\u9009\u62e9\u5408\u9002\u7684\u641c\u7d22\u65b9\u6cd5\u3002
    • \u7ebf\u6027\u641c\u7d22\u9002\u7528\u4e8e\u5c0f\u578b\u6216\u9891\u7e41\u66f4\u65b0\u7684\u6570\u636e\uff1b\u4e8c\u5206\u67e5\u627e\u9002\u7528\u4e8e\u5927\u578b\u3001\u6392\u5e8f\u7684\u6570\u636e\uff1b\u54c8\u5e0c\u67e5\u627e\u9002\u5408\u5bf9\u67e5\u8be2\u6548\u7387\u8981\u6c42\u8f83\u9ad8\u4e14\u65e0\u9700\u8303\u56f4\u67e5\u8be2\u7684\u6570\u636e\uff1b\u6811\u67e5\u627e\u9002\u7528\u4e8e\u9700\u8981\u7ef4\u62a4\u987a\u5e8f\u548c\u652f\u6301\u8303\u56f4\u67e5\u8be2\u7684\u5927\u578b\u52a8\u6001\u6570\u636e\u3002
    • \u7528\u54c8\u5e0c\u67e5\u627e\u66ff\u6362\u7ebf\u6027\u67e5\u627e\u662f\u4e00\u79cd\u5e38\u7528\u7684\u4f18\u5316\u8fd0\u884c\u65f6\u95f4\u7684\u7b56\u7565\uff0c\u53ef\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n)\\) \u964d\u4f4e\u81f3 \\(O(1)\\) \u3002
    "},{"location":"chapter_sorting/","title":"11. \u00a0 \u6392\u5e8f","text":""},{"location":"chapter_sorting/bubble_sort/","title":"11.3. \u00a0 \u5192\u6ce1\u6392\u5e8f","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

    \u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5143\u7d20\u4ea4\u6362\u64cd\u4f5c\u6a21\u62df\u4e0a\u8ff0\u8fc7\u7a0b\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\u5b83\u4fe9\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>

    "},{"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\u4e3a\uff1a

    1. \u9996\u5148\uff0c\u5bf9 \\(n\\) \u4e2a\u5143\u7d20\u6267\u884c\u201c\u5192\u6ce1\u201d\uff0c\u5c06\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u6b63\u786e\u4f4d\u7f6e\uff0c
    2. \u63a5\u4e0b\u6765\uff0c\u5bf9\u5269\u4f59 \\(n - 1\\) \u4e2a\u5143\u7d20\u6267\u884c\u201c\u5192\u6ce1\u201d\uff0c\u5c06\u7b2c\u4e8c\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u6b63\u786e\u4f4d\u7f6e\u3002
    3. \u4ee5\u6b64\u7c7b\u63a8\uff0c\u7ecf\u8fc7 \\(n - 1\\) \u8f6e\u201c\u5192\u6ce1\u201d\u540e\uff0c\u524d \\(n - 1\\) \u5927\u7684\u5143\u7d20\u90fd\u88ab\u4ea4\u6362\u81f3\u6b63\u786e\u4f4d\u7f6e\u3002
    4. \u4ec5\u5269\u7684\u4e00\u4e2a\u5143\u7d20\u5fc5\u5b9a\u662f\u6700\u5c0f\u5143\u7d20\uff0c\u65e0\u9700\u6392\u5e8f\uff0c\u56e0\u6b64\u6570\u7ec4\u6392\u5e8f\u5b8c\u6210\u3002

    Fig. \u5192\u6ce1\u6392\u5e8f\u6d41\u7a0b

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart bubble_sort.java
    /* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(int[] nums) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (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 \nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\n}\n}\n}\n}\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]\nfor (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 \nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n// \u8fd9\u91cc\u4f7f\u7528\u4e86 std::swap() \u51fd\u6570\nswap(nums[j], nums[j + 1]);\n}\n}\n}\n}\n
    bubble_sort.py
    def bubble_sort(nums: list[int]) -> None:\n\"\"\"\u5192\u6ce1\u6392\u5e8f\"\"\"\nn = len(nums)\n# \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor 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 \nfor j in range(i):\nif nums[j] > nums[j + 1]:\n# \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nnums[j], nums[j + 1] = nums[j + 1], nums[j]\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]\nfor 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\nfor j := 0; j < i; j++ {\nif nums[j] > nums[j+1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nnums[j], nums[j+1] = nums[j+1], nums[j]\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]\nfor (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 \nfor (let j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[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]\nfor (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 \nfor (let j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[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]\nfor (int i = 0; i < size - 1; 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 \nfor (int j = 0; j < size - 1 - i; j++) {\nif (nums[j] > nums[j + 1]) {\nint temp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = temp;\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]\nfor (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 \nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\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]\nfor 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 \nfor j in stride(from: 0, to: i, by: 1) {\nif nums[j] > nums[j + 1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j]\nnums[j] = nums[j + 1]\nnums[j + 1] = tmp\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]\nvar i: usize = nums.len - 1;\nwhile (i > 0) : (i -= 1) {\nvar 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 \nwhile (j < i) : (j += 1) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nvar tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[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]\nfor (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 \nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[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\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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]\nfor (int i = nums.length - 1; i > 0; i--) {\nboolean 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 \nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\nflag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif (!flag)\nbreak; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n}\n}\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]\nfor (int i = nums.size() - 1; i > 0; i--) {\nbool 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 \nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n// \u8fd9\u91cc\u4f7f\u7528\u4e86 std::swap() \u51fd\u6570\nswap(nums[j], nums[j + 1]);\nflag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif (!flag)\nbreak; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n}\n}\n
    bubble_sort.py
    def bubble_sort_with_flag(nums: list[int]) -> None:\n\"\"\"\u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09\"\"\"\nn = len(nums)\n# \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor i in range(n - 1, 0, -1):\nflag = 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 \nfor j in range(i):\nif nums[j] > nums[j + 1]:\n# \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nnums[j], nums[j + 1] = nums[j + 1], nums[j]\nflag = True  # \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\nif not flag:\nbreak  # \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\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]\nfor i := len(nums) - 1; i > 0; i-- {\nflag := 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\nfor j := 0; j < i; j++ {\nif nums[j] > nums[j+1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nnums[j], nums[j+1] = nums[j+1], nums[j]\nflag = true // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif flag == false { // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\nbreak\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]\nfor (let i = nums.length - 1; i > 0; i--) {\nlet 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 \nfor (let j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\nflag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif (!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]\nfor (let i = nums.length - 1; i > 0; i--) {\nlet 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 \nfor (let j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\nflag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif (!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]\nfor (int i = 0; i < size - 1; i++) {\nbool 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 \nfor (int j = 0; j < size - 1 - i; j++) {\nif (nums[j] > nums[j + 1]) {\nint temp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = temp;\nflag = true;\n}\n}\nif (!flag)\nbreak;\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]\nfor (int i = nums.Length - 1; i > 0; i--) {\nbool 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 \nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\nflag = true;  // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif (!flag) break;     // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\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]\nfor i in stride(from: nums.count - 1, to: 0, by: -1) {\nvar flag = false // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\nfor j in stride(from: 0, to: i, by: 1) {\nif nums[j] > nums[j + 1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j]\nnums[j] = nums[j + 1]\nnums[j + 1] = tmp\nflag = true // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif !flag { // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\nbreak\n}\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]\nvar i: usize = nums.len - 1;\nwhile (i > 0) : (i -= 1) {\nvar flag = false;   // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\nvar 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 \nwhile (j < i) : (j += 1) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nvar tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\nflag = true;\n}\n}\nif (!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]\nfor (int i = nums.length - 1; i > 0; i--) {\nbool 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 \nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\nflag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif (!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":"
    • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3001\u81ea\u9002\u5e94\u6392\u5e8f \uff1a\u5404\u8f6e\u201c\u5192\u6ce1\u201d\u904d\u5386\u7684\u6570\u7ec4\u957f\u5ea6\u4f9d\u6b21\u4e3a \\(n - 1\\) , \\(n - 2\\) , \\(\\cdots\\) , \\(2\\) , \\(1\\) \uff0c\u603b\u548c\u4e3a \\(\\frac{(n - 1) n}{2}\\) \u3002\u5728\u5f15\u5165 flag \u4f18\u5316\u540e\uff0c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe\u5230 \\(O(n)\\) \u3002
    • \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3001\u539f\u5730\u6392\u5e8f\uff1a\u6307\u9488 \\(i\\) , \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002
    • \u7a33\u5b9a\u6392\u5e8f\uff1a\u7531\u4e8e\u5728\u201c\u5192\u6ce1\u201d\u4e2d\u9047\u5230\u76f8\u7b49\u5143\u7d20\u4e0d\u4ea4\u6362\u3002
    "},{"location":"chapter_sorting/bucket_sort/","title":"11.8. \u00a0 \u6876\u6392\u5e8f","text":"

    \u524d\u8ff0\u7684\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\u6c34\u5e73\u3002

    \u300c\u6876\u6392\u5e8f Bucket Sort\u300d\u662f\u5206\u6cbb\u601d\u60f3\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\u5143\u7d20\u662f\u8303\u56f4 \\([0, 1)\\) \u7684\u6d6e\u70b9\u6570\u3002\u6876\u6392\u5e8f\u7684\u6d41\u7a0b\u5982\u4e0b\uff1a

    1. \u521d\u59cb\u5316 \\(k\\) \u4e2a\u6876\uff0c\u5c06 \\(n\\) \u4e2a\u5143\u7d20\u5206\u914d\u5230 \\(k\\) \u4e2a\u6876\u4e2d\uff1b
    2. \u5bf9\u6bcf\u4e2a\u6876\u5206\u522b\u6267\u884c\u6392\u5e8f\uff08\u672c\u6587\u91c7\u7528\u7f16\u7a0b\u8bed\u8a00\u7684\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff09\uff1b
    3. \u6309\u7167\u6876\u7684\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\uff0c\u5408\u5e76\u7ed3\u679c\uff1b

    Fig. \u6876\u6392\u5e8f\u7b97\u6cd5\u6d41\u7a0b

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nint k = nums.length / 2;\nList<List<Float>> buckets = new ArrayList<>();\nfor (int i = 0; i < k; i++) {\nbuckets.add(new ArrayList<>());\n}\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor (float num : nums) {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\nint i = (int) (num * k);\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\nbuckets.get(i).add(num);\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor (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\nCollections.sort(bucket);\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nint i = 0;\nfor (List<Float> bucket : buckets) {\nfor (float num : bucket) {\nnums[i++] = num;\n}\n}\n}\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\nint k = nums.size() / 2;\nvector<vector<float>> buckets(k);\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor (float num : nums) {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\nint i = num * k;\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 bucket_idx\nbuckets[i].push_back(num);\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor (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\nsort(bucket.begin(), bucket.end());\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nint i = 0;\nfor (vector<float> &bucket : buckets) {\nfor (float num : bucket) {\nnums[i++] = num;\n}\n}\n}\n
    bucket_sort.py
    def bucket_sort(nums: list[float]) -> None:\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\nk = len(nums) // 2\nbuckets = [[] for _ in range(k)]\n# 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor num in nums:\n# \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\ni = int(num * k)\n# \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\nbuckets[i].append(num)\n# 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f5\nfor 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\nbucket.sort()\n# 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\ni = 0\nfor bucket in buckets:\nfor num in bucket:\nnums[i] = num\ni += 1\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\nk := len(nums) / 2\nbuckets := make([][]float64, k)\nfor i := 0; i < k; i++ {\nbuckets[i] = make([]float64, 0)\n}\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor _, num := range nums {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\ni := int(num * float64(k))\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\nbuckets[i] = append(buckets[i], num)\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor 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\nsort.Float64s(buckets[i])\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\ni := 0\nfor _, bucket := range buckets {\nfor _, num := range bucket {\nnums[i] = num\ni++\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\nconst k = nums.length / 2;\nconst buckets = [];\nfor (let i = 0; i < k; i++) {\nbuckets.push([]);\n}\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor (const num of nums) {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\nconst i = Math.floor(num * k);\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\nbuckets[i].push(num);\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor (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\nbucket.sort((a, b) => a - b);\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nlet i = 0;\nfor (const bucket of buckets) {\nfor (const num of bucket) {\nnums[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\nconst k = nums.length / 2;\nconst buckets: number[][] = [];\nfor (let i = 0; i < k; i++) {\nbuckets.push([]);\n}\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor (const num of nums) {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\nconst i = Math.floor(num * k);\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\nbuckets[i].push(num);\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor (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\nbucket.sort((a, b) => a - b);\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nlet i = 0;\nfor (const bucket of buckets) {\nfor (const num of bucket) {\nnums[i++] = num;\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\nint k = size / 2;\nfloat **buckets = calloc(k, sizeof(float *));\nfor (int i = 0; i < k; i++) {\n// \u6bcf\u4e2a\u6876\u6700\u591a\u53ef\u4ee5\u5206\u914d k \u4e2a\u5143\u7d20\nbuckets[i] = calloc(ARRAY_SIZE, sizeof(float));\n}\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor (int i = 0; i < size; i++) {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\nint bucket_idx = nums[i] * k;\nint 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\nwhile (buckets[bucket_idx][j] > 0 && buckets[bucket_idx][j] < nums[i]) {\nj++;\n}\nfloat temp = nums[i];\nwhile (j < ARRAY_SIZE && buckets[bucket_idx][j] > 0) {\nswap(&temp, &buckets[bucket_idx][j]);\nj++;\n}\nbuckets[bucket_idx][j] = temp;\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor (int i = 0; i < k; i++) {\nqsort(buckets[i], ARRAY_SIZE, sizeof(float), compare_float);\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nfor (int i = 0, j = 0; j < k; j++) {\nfor (int l = 0; l < ARRAY_SIZE; l++) {\nif (buckets[j][l] > 0) {\nnums[i++] = buckets[j][l];\n}\n}\n}\n// \u91ca\u653e\u4e0a\u8ff0\u5206\u914d\u7684\u5185\u5b58\nfor (int i = 0; i < k; i++) {\nfree(buckets[i]);\n}\nfree(buckets);\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\nint k = nums.Length / 2;\nList<List<float>> buckets = new List<List<float>>();\nfor (int i = 0; i < k; i++) {\nbuckets.Add(new List<float>());\n}\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nforeach (float num in nums) {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\nint i = (int) (num * k);\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\nbuckets[i].Add(num);\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nforeach (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\nbucket.Sort();\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nint j = 0;\nforeach (List<float> bucket in buckets) {\nforeach (float num in bucket) {\nnums[j++] = num;\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\nlet k = nums.count / 2\nvar buckets = (0 ..< k).map { _ in [Double]() }\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor num in nums {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\nlet i = Int(num * k)\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\nbuckets[i].append(num)\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor 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\nbuckets[i].sort()\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nvar i = nums.startIndex\nfor bucket in buckets {\nfor num in bucket {\nnums[i] = num\nnums.formIndex(after: &i)\n}\n}\n}\n
    bucket_sort.zig
    [class]{}-[func]{bucketSort}\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\nint k = nums.length ~/ 2;\nList<List<double>> buckets = List.generate(k, (index) => []);\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor (double num in nums) {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\nint i = (num * k).toInt();\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 bucket_idx\nbuckets[i].add(num);\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor (List<double> bucket in buckets) {\nbucket.sort();\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nint i = 0;\nfor (List<double> bucket in buckets) {\nfor (double num in bucket) {\nnums[i++] = num;\n}\n}\n}\n

    \u6876\u6392\u5e8f\u7684\u9002\u7528\u573a\u666f\u662f\u4ec0\u4e48\uff1f

    \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

    "},{"location":"chapter_sorting/bucket_sort/#1182","title":"11.8.2. \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
    • \u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n + k)\\) \uff1a\u5047\u8bbe\u5143\u7d20\u5728\u5404\u4e2a\u6876\u5185\u5e73\u5747\u5206\u5e03\uff0c\u90a3\u4e48\u6bcf\u4e2a\u6876\u5185\u7684\u5143\u7d20\u6570\u91cf\u4e3a \\(\\frac{n}{k}\\) \u3002\u5047\u8bbe\u6392\u5e8f\u5355\u4e2a\u6876\u4f7f\u7528 \\(O(\\frac{n}{k} \\log\\frac{n}{k})\\) \u65f6\u95f4\uff0c\u5219\u6392\u5e8f\u6240\u6709\u6876\u4f7f\u7528 \\(O(n \\log\\frac{n}{k})\\) \u65f6\u95f4\u3002\u5f53\u6876\u6570\u91cf \\(k\\) \u6bd4\u8f83\u5927\u65f6\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5219\u8d8b\u5411\u4e8e \\(O(n)\\) \u3002\u5408\u5e76\u7ed3\u679c\u65f6\u9700\u8981\u904d\u5386 \\(n\\) \u4e2a\u6876\uff0c\u82b1\u8d39 \\(O(k)\\) \u65f6\u95f4\u3002
    • \u81ea\u9002\u5e94\u6392\u5e8f\uff1a\u5728\u6700\u574f\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u6570\u636e\u88ab\u5206\u914d\u5230\u4e00\u4e2a\u6876\u4e2d\uff0c\u4e14\u6392\u5e8f\u8be5\u6876\u4f7f\u7528 \\(O(n^2)\\) \u65f6\u95f4\u3002
    • \u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(n + k)\\) \u3001\u975e\u539f\u5730\u6392\u5e8f \uff1a\u9700\u8981\u501f\u52a9 \\(k\\) \u4e2a\u6876\u548c\u603b\u5171 \\(n\\) \u4e2a\u5143\u7d20\u7684\u989d\u5916\u7a7a\u95f4\u3002
    • \u6876\u6392\u5e8f\u662f\u5426\u7a33\u5b9a\u53d6\u51b3\u4e8e\u6392\u5e8f\u6876\u5185\u5143\u7d20\u7684\u7b97\u6cd5\u662f\u5426\u7a33\u5b9a\u3002
    "},{"location":"chapter_sorting/bucket_sort/#1183","title":"11.8.3. \u00a0 \u5982\u4f55\u5b9e\u73b0\u5e73\u5747\u5206\u914d","text":"

    \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 \u4efd\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\u4e2a\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\u8fd9\u79cd\u65b9\u6cd5\u672c\u8d28\u4e0a\u662f\u521b\u5efa\u4e00\u4e2a\u9012\u5f52\u6811\uff0c\u4f7f\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

    Fig. \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\u4e0b\u56fe\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

    Fig. \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\u3002\u8ba1\u6570\u6392\u5e8f\u7684\u6574\u4f53\u6d41\u7a0b\u5982\u4e0b\uff1a

    1. \u904d\u5386\u6570\u7ec4\uff0c\u627e\u51fa\u6570\u7ec4\u4e2d\u7684\u6700\u5927\u6570\u5b57\uff0c\u8bb0\u4e3a \\(m\\) \uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(m + 1\\) \u7684\u8f85\u52a9\u6570\u7ec4 counter \uff1b
    2. \u501f\u52a9 counter \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\u3002
    3. \u7531\u4e8e counter \u7684\u5404\u4e2a\u7d22\u5f15\u5929\u7136\u6709\u5e8f\uff0c\u56e0\u6b64\u76f8\u5f53\u4e8e\u6240\u6709\u6570\u5b57\u5df2\u7ecf\u88ab\u6392\u5e8f\u597d\u4e86\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u904d\u5386 counter \uff0c\u6839\u636e\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u5c06\u5b83\u4eec\u6309\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u586b\u5165 nums \u5373\u53ef\u3002

    Fig. \u8ba1\u6570\u6392\u5e8f\u6d41\u7a0b

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nint m = 0;\nfor (int num : nums) {\nm = 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\nint[] counter = new int[m + 1];\nfor (int num : nums) {\ncounter[num]++;\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nint i = 0;\nfor (int num = 0; num < m + 1; num++) {\nfor (int j = 0; j < counter[num]; j++, i++) {\nnums[i] = num;\n}\n}\n}\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\nint m = 0;\nfor (int num : nums) {\nm = 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\nvector<int> counter(m + 1, 0);\nfor (int num : nums) {\ncounter[num]++;\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nint i = 0;\nfor (int num = 0; num < m + 1; num++) {\nfor (int j = 0; j < counter[num]; j++, i++) {\nnums[i] = num;\n}\n}\n}\n
    counting_sort.py
    def counting_sort_naive(nums: list[int]) -> None:\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\nm = 0\nfor num in nums:\nm = 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\ncounter = [0] * (m + 1)\nfor num in nums:\ncounter[num] += 1\n# 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\ni = 0\nfor num in range(m + 1):\nfor _ in range(counter[num]):\nnums[i] = num\ni += 1\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\nm := 0\nfor _, num := range nums {\nif num > m {\nm = 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\ncounter := make([]int, m+1)\nfor _, num := range nums {\ncounter[num]++\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nfor i, num := 0, 0; num < m+1; num++ {\nfor j := 0; j < counter[num]; j++ {\nnums[i] = num\ni++\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\nlet m = 0;\nfor (const num of nums) {\nm = 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\nconst counter = new Array(m + 1).fill(0);\nfor (const num of nums) {\ncounter[num]++;\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nlet i = 0;\nfor (let num = 0; num < m + 1; num++) {\nfor (let j = 0; j < counter[num]; j++, i++) {\nnums[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\nlet m = 0;\nfor (const num of nums) {\nm = 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\nconst counter: number[] = new Array<number>(m + 1).fill(0);\nfor (const num of nums) {\ncounter[num]++;\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nlet i = 0;\nfor (let num = 0; num < m + 1; num++) {\nfor (let j = 0; j < counter[num]; j++, i++) {\nnums[i] = num;\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\nint m = 0;\nfor (int i = 0; i < size; i++) {\nif (nums[i] > m) {\nm = 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\nint *counter = malloc(sizeof(int) * m);\nfor (int i = 0; i < size; i++) {\ncounter[nums[i]]++;\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nint i = 0;\nfor (int num = 0; num < m + 1; num++) {\nfor (int j = 0; j < counter[num]; j++, i++) {\nnums[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\nint m = 0;\nforeach (int num in nums) {\nm = 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\nint[] counter = new int[m + 1];\nforeach (int num in nums) {\ncounter[num]++;\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nint i = 0;\nfor (int num = 0; num < m + 1; num++) {\nfor (int j = 0; j < counter[num]; j++, i++) {\nnums[i] = num;\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\nlet 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\nvar counter = Array(repeating: 0, count: m + 1)\nfor num in nums {\ncounter[num] += 1\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nvar i = 0\nfor num in stride(from: 0, to: m + 1, by: 1) {\nfor _ in stride(from: 0, to: counter[num], by: 1) {\nnums[i] = num\ni += 1\n}\n}\n}\n
    counting_sort.zig
    [class]{}-[func]{countingSortNaive}\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\nint m = 0;\nfor (int num in nums) {\nm = 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\nList<int> counter = List.filled(m + 1, 0);\nfor (int num in nums) {\ncounter[num]++;\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nint i = 0;\nfor (int num = 0; num < m + 1; num++) {\nfor (int j = 0; j < counter[num]; j++, i++) {\nnums[i] = num;\n}\n}\n}\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\u662f\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

    "},{"location":"chapter_sorting/counting_sort/#1192","title":"11.9.2. \u00a0 \u5b8c\u6574\u5b9e\u73b0","text":"

    \u7ec6\u5fc3\u7684\u540c\u5b66\u53ef\u80fd\u53d1\u73b0\uff0c\u5982\u679c\u8f93\u5165\u6570\u636e\u662f\u5bf9\u8c61\uff0c\u4e0a\u8ff0\u6b65\u9aa4 3. \u5c31\u5931\u6548\u4e86\u3002\u4f8b\u5982\uff0c\u8f93\u5165\u6570\u636e\u662f\u5546\u54c1\u5bf9\u8c61\uff0c\u6211\u4eec\u60f3\u8981\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\u300c\u524d\u7f00\u548c\u300d\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\uff0c\u5373

    \\[ \\text{prefix}[i] = \\sum_{j=0}^i \\text{counter[j]} \\]

    \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\uff1a

    1. \u5c06 num \u586b\u5165\u6570\u7ec4 res \u7684\u7d22\u5f15 prefix[num] - 1 \u5904\uff1b
    2. \u4ee4\u524d\u7f00\u548c prefix[num] \u51cf\u5c0f \\(1\\) \uff0c\u4ece\u800c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\uff1b

    \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

    <1><2><3><4><5><6><7><8>

    \u8ba1\u6570\u6392\u5e8f\u7684\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nint m = 0;\nfor (int num : nums) {\nm = 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\nint[] counter = new int[m + 1];\nfor (int num : nums) {\ncounter[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\nfor (int i = 0; i < m; i++) {\ncounter[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\nint n = nums.length;\nint[] res = new int[n];\nfor (int i = n - 1; i >= 0; i--) {\nint num = nums[i];\nres[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[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\nfor (int i = 0; i < n; i++) {\nnums[i] = res[i];\n}\n}\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\nint m = 0;\nfor (int num : nums) {\nm = 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\nvector<int> counter(m + 1, 0);\nfor (int num : nums) {\ncounter[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\nfor (int i = 0; i < m; i++) {\ncounter[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\nint n = nums.size();\nvector<int> res(n);\nfor (int i = n - 1; i >= 0; i--) {\nint num = nums[i];\nres[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[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\nnums = res;\n}\n
    counting_sort.py
    def counting_sort(nums: list[int]) -> None:\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\nm = 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\ncounter = [0] * (m + 1)\nfor num in nums:\ncounter[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\nfor i in range(m):\ncounter[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\nn = len(nums)\nres = [0] * n\nfor i in range(n - 1, -1, -1):\nnum = nums[i]\nres[counter[num] - 1] = num  # \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[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\nfor i in range(n):\nnums[i] = res[i]\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\nm := 0\nfor _, num := range nums {\nif num > m {\nm = 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\ncounter := make([]int, m+1)\nfor _, num := range nums {\ncounter[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\nfor i := 0; i < m; i++ {\ncounter[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\nn := len(nums)\nres := make([]int, n)\nfor i := n - 1; i >= 0; i-- {\nnum := nums[i]\n// \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\nres[counter[num]-1] = num\n// \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\ncounter[num]--\n}\n// \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\ncopy(nums, res)\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\nlet m = 0;\nfor (const num of nums) {\nm = 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\nconst counter = new Array(m + 1).fill(0);\nfor (const num of nums) {\ncounter[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\nfor (let i = 0; i < m; i++) {\ncounter[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\nconst n = nums.length;\nconst res = new Array(n);\nfor (let i = n - 1; i >= 0; i--) {\nconst num = nums[i];\nres[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[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\nfor (let i = 0; i < n; i++) {\nnums[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\nlet m = 0;\nfor (const num of nums) {\nm = 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\nconst counter: number[] = new Array<number>(m + 1).fill(0);\nfor (const num of nums) {\ncounter[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\nfor (let i = 0; i < m; i++) {\ncounter[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\nconst n = nums.length;\nconst res: number[] = new Array<number>(n);\nfor (let i = n - 1; i >= 0; i--) {\nconst num = nums[i];\nres[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[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\nfor (let i = 0; i < n; i++) {\nnums[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\nint m = 0;\nfor (int i = 0; i < size; i++) {\nif (nums[i] > m) {\nm = 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\nint *counter = malloc(sizeof(int) * m);\nfor (int i = 0; i < size; i++) {\ncounter[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\nfor (int i = 0; i < m; i++) {\ncounter[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\nint *res = malloc(sizeof(int) * size);\nfor (int i = size - 1; i >= 0; i--) {\nint num = nums[i];\nres[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[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\nmemcpy(nums, res, size * sizeof(int));\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\nint m = 0;\nforeach (int num in nums) {\nm = 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\nint[] counter = new int[m + 1];\nforeach (int num in nums) {\ncounter[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\nfor (int i = 0; i < m; i++) {\ncounter[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\nint n = nums.Length;\nint[] res = new int[n];\nfor (int i = n - 1; i >= 0; i--) {\nint num = nums[i];\nres[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[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\nfor (int i = 0; i < n; i++) {\nnums[i] = res[i];\n}\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\nlet 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\nvar counter = Array(repeating: 0, count: m + 1)\nfor num in nums {\ncounter[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\nfor i in stride(from: 0, to: m, by: 1) {\ncounter[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\nvar res = Array(repeating: 0, count: nums.count)\nfor i in stride(from: nums.count - 1, through: 0, by: -1) {\nlet num = nums[i]\nres[counter[num] - 1] = num // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[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\nfor i in stride(from: 0, to: nums.count, by: 1) {\nnums[i] = res[i]\n}\n}\n
    counting_sort.zig
    [class]{}-[func]{countingSort}\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\nint m = 0;\nfor (int num in nums) {\nm = 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\nList<int> counter = List.filled(m + 1, 0);\nfor (int num in nums) {\ncounter[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\nfor (int i = 0; i < m; i++) {\ncounter[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\nint n = nums.length;\nList<int> res = List.filled(n, 0);\nfor (int i = n - 1; i >= 0; i--) {\nint num = nums[i];\nres[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[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\nnums.setAll(0, res);\n}\n
    "},{"location":"chapter_sorting/counting_sort/#1193","title":"11.9.3. \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
    • \u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n + m)\\) \uff1a\u6d89\u53ca\u904d\u5386 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)\\) \u3002
    • \u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(n + m)\\) \u3001\u975e\u539f\u5730\u6392\u5e8f \uff1a\u501f\u52a9\u4e86\u957f\u5ea6\u5206\u522b\u4e3a \\(n\\) \u548c \\(m\\) \u7684\u6570\u7ec4 res \u548c counter \u3002
    • \u7a33\u5b9a\u6392\u5e8f\uff1a\u7531\u4e8e\u5411 res \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
    "},{"location":"chapter_sorting/counting_sort/#1194","title":"11.9.4. \u00a0 \u5c40\u9650\u6027","text":"

    \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\u5de5\u4f5c\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\u8981\u5c06\u5176\u7528\u4e8e\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u636e\uff0c\u9700\u8981\u786e\u4fdd\u8fd9\u4e9b\u6570\u636e\u53ef\u4ee5\u88ab\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\u5373\u53ef\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\u300c\u5806\u300d\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\uff1a

    1. \u8f93\u5165\u6570\u7ec4\u5e76\u5efa\u7acb\u5c0f\u9876\u5806\uff0c\u6b64\u65f6\u6700\u5c0f\u5143\u7d20\u4f4d\u4e8e\u5806\u9876\u3002
    2. \u4e0d\u65ad\u6267\u884c\u51fa\u5806\u64cd\u4f5c\uff0c\u4f9d\u6b21\u8bb0\u5f55\u51fa\u5806\u5143\u7d20\uff0c\u5373\u53ef\u5f97\u5230\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\u7684\u5e8f\u5217\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\u4e0b\uff1a

    1. \u8f93\u5165\u6570\u7ec4\u5e76\u5efa\u7acb\u5927\u9876\u5806\u3002\u5b8c\u6210\u540e\uff0c\u6700\u5927\u5143\u7d20\u4f4d\u4e8e\u5806\u9876\u3002
    2. \u5c06\u5806\u9876\u5143\u7d20\uff08\u7b2c\u4e00\u4e2a\u5143\u7d20\uff09\u4e0e\u5806\u5e95\u5143\u7d20\uff08\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff09\u4ea4\u6362\u3002\u5b8c\u6210\u4ea4\u6362\u540e\uff0c\u5806\u7684\u957f\u5ea6\u51cf \\(1\\) \uff0c\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u52a0 \\(1\\) \u3002
    3. \u4ece\u5806\u9876\u5143\u7d20\u5f00\u59cb\uff0c\u4ece\u9876\u5230\u5e95\u6267\u884c\u5806\u5316\u64cd\u4f5c\uff08Sift Down\uff09\u3002\u5b8c\u6210\u5806\u5316\u540e\uff0c\u5806\u7684\u6027\u8d28\u5f97\u5230\u4fee\u590d\u3002
    4. \u5faa\u73af\u6267\u884c\u7b2c 2. \u548c 3. \u6b65\u3002\u5faa\u73af \\(n - 1\\) \u8f6e\u540e\uff0c\u5373\u53ef\u5b8c\u6210\u6570\u7ec4\u6392\u5e8f\u3002

    \u5b9e\u9645\u4e0a\uff0c\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\u4e2d\u4e5f\u5305\u542b\u7b2c 2. \u548c 3. \u6b65\uff0c\u53ea\u662f\u591a\u4e86\u4e00\u4e2a\u5f39\u51fa\u5143\u7d20\u7684\u6b65\u9aa4\u3002

    <1><2><3><4><5><6><7><8><9><10><11><12>

    \u5728\u4ee3\u7801\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e0e\u5806\u7ae0\u8282\u76f8\u540c\u7684\u4ece\u9876\u81f3\u5e95\u5806\u5316\uff08Sift Down\uff09\u7684\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nint l = 2 * i + 1;\nint r = 2 * i + 2;\nint ma = i;\nif (l < n && nums[l] > nums[ma])\nma = l;\nif (r < n && nums[r] > nums[ma])\nma = r;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i)\nbreak;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nint temp = nums[i];\nnums[i] = nums[ma];\nnums[ma] = temp;\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\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\nfor (int i = nums.length / 2 - 1; i >= 0; i--) {\nsiftDown(nums, nums.length, i);\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor (int i = nums.length - 1; i > 0; i--) {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nint tmp = nums[0];\nnums[0] = nums[i];\nnums[i] = tmp;\n// \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\nsiftDown(nums, i, 0);\n}\n}\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) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nint l = 2 * i + 1;\nint r = 2 * i + 2;\nint ma = i;\nif (l < n && nums[l] > nums[ma])\nma = l;\nif (r < n && nums[r] > nums[ma])\nma = r;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i) {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(nums[i], nums[ma]);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\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\nfor (int i = nums.size() / 2 - 1; i >= 0; --i) {\nsiftDown(nums, nums.size(), i);\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor (int i = nums.size() - 1; i > 0; --i) {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nswap(nums[0], nums[i]);\n// \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\nsiftDown(nums, i, 0);\n}\n}\n
    heap_sort.py
    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\"\"\"\nwhile True:\n# \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nl = 2 * i + 1\nr = 2 * i + 2\nma = i\nif l < n and nums[l] > nums[ma]:\nma = l\nif r < n and nums[r] > nums[ma]:\nma = r\n# \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif ma == i:\nbreak\n# \u4ea4\u6362\u4e24\u8282\u70b9\nnums[i], nums[ma] = nums[ma], nums[i]\n# \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma\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\nfor i in range(len(nums) // 2 - 1, -1, -1):\nsift_down(nums, len(nums), i)\n# \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor i in range(len(nums) - 1, 0, -1):\n# \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nnums[0], nums[i] = nums[i], nums[0]\n# \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\nsift_down(nums, i, 0)\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) {\nfor true {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nl := 2*i + 1\nr := 2*i + 2\nma := i\nif l < n && (*nums)[l] > (*nums)[ma] {\nma = l\n}\nif r < n && (*nums)[r] > (*nums)[ma] {\nma = r\n}\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif ma == i {\nbreak\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\n(*nums)[i], (*nums)[ma] = (*nums)[ma], (*nums)[i]\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma\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\nfor i := len(*nums)/2 - 1; i >= 0; i-- {\nsiftDown(nums, len(*nums), i)\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor i := len(*nums) - 1; i > 0; i-- {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\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\nsiftDown(nums, 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) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nlet l = 2 * i + 1;\nlet r = 2 * i + 2;\nlet ma = i;\nif (l < n && nums[l] > nums[ma]) {\nma = l;\n}\nif (r < n && nums[r] > nums[ma]) {\nma = r;\n}\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma === i) {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\n[nums[i], nums[ma]] = [nums[ma], nums[i]];\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\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\nfor (let i = Math.floor(nums.length / 2) - 1; i >= 0; i--) {\nsiftDown(nums, nums.length, i);\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor (let i = nums.length - 1; i > 0; i--) {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\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\nsiftDown(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 {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nlet l = 2 * i + 1;\nlet r = 2 * i + 2;\nlet ma = i;\nif (l < n && nums[l] > nums[ma]) {\nma = l;\n}\nif (r < n && nums[r] > nums[ma]) {\nma = r;\n}\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma === i) {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\n[nums[i], nums[ma]] = [nums[ma], nums[i]];\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\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\nfor (let i = Math.floor(nums.length / 2) - 1; i >= 0; i--) {\nsiftDown(nums, nums.length, i);\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor (let i = nums.length - 1; i > 0; i--) {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\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\nsiftDown(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) {\nwhile (1) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nint l = 2 * i + 1;\nint r = 2 * i + 2;\nint ma = i;\nif (l < n && nums[l] > nums[ma])\nma = l;\nif (r < n && nums[r] > nums[ma])\nma = r;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i) {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nint temp = nums[i];\nnums[i] = nums[ma];\nnums[ma] = temp;\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\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\nfor (int i = n / 2 - 1; i >= 0; --i) {\nsiftDown(nums, n, i);\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor (int i = n - 1; i > 0; --i) {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nint tmp = nums[0];\nnums[0] = nums[i];\nnums[i] = tmp;\n// \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\nsiftDown(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) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nint l = 2 * i + 1;\nint r = 2 * i + 2;\nint ma = i;\nif (l < n && nums[l] > nums[ma])\nma = l;\nif (r < n && nums[r] > nums[ma])\nma = r;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i)\nbreak;\n// \u4ea4\u6362\u4e24\u8282\u70b9\n(nums[ma], nums[i]) = (nums[i], nums[ma]);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\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\nfor (int i = nums.Length / 2 - 1; i >= 0; i--) {\nsiftDown(nums, nums.Length, i);\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor (int i = nums.Length - 1; i > 0; i--) {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\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\nsiftDown(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) {\nvar i = i\nwhile true {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nlet l = 2 * i + 1\nlet r = 2 * i + 2\nvar ma = i\nif l < n, nums[l] > nums[ma] {\nma = l\n}\nif r < n, nums[r] > nums[ma] {\nma = r\n}\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif ma == i {\nbreak\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nnums.swapAt(i, ma)\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma\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\nfor i in stride(from: nums.count / 2 - 1, through: 0, by: -1) {\nsiftDown(nums: &nums, n: nums.count, i: i)\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor i in stride(from: nums.count - 1, to: 0, by: -1) {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nnums.swapAt(0, i)\n// \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\nsiftDown(nums: &nums, n: i, i: 0)\n}\n}\n
    heap_sort.zig
    [class]{}-[func]{siftDown}\n[class]{}-[func]{heapSort}\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) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nint l = 2 * i + 1;\nint r = 2 * i + 2;\nint ma = i;\nif (l < n && nums[l] > nums[ma]) ma = l;\nif (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\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nint temp = nums[i];\nnums[i] = nums[ma];\nnums[ma] = temp;\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\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\nfor (int i = nums.length ~/ 2 - 1; i >= 0; i--) {\nsiftDown(nums, nums.length, i);\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor (int i = nums.length - 1; i > 0; i--) {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nint tmp = nums[0];\nnums[0] = nums[i];\nnums[i] = tmp;\n// \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\nsiftDown(nums, i, 0);\n}\n}\n
    "},{"location":"chapter_sorting/heap_sort/#1172","title":"11.7.2. \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
    • \u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n \\log n)\\) \u3001\u975e\u81ea\u9002\u5e94\u6392\u5e8f \uff1a\u5efa\u5806\u64cd\u4f5c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002\u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \uff0c\u5171\u5faa\u73af \\(n - 1\\) \u8f6e\u3002
    • \u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(1)\\) \u3001\u539f\u5730\u6392\u5e8f \uff1a\u51e0\u4e2a\u6307\u9488\u53d8\u91cf\u4f7f\u7528 \\(O(1)\\) \u7a7a\u95f4\u3002\u5143\u7d20\u4ea4\u6362\u548c\u5806\u5316\u64cd\u4f5c\u90fd\u662f\u5728\u539f\u6570\u7ec4\u4e0a\u8fdb\u884c\u7684\u3002
    • \u975e\u7a33\u5b9a\u6392\u5e8f\uff1a\u5728\u4ea4\u6362\u5806\u9876\u5143\u7d20\u548c\u5806\u5e95\u5143\u7d20\u65f6\uff0c\u76f8\u7b49\u5143\u7d20\u7684\u76f8\u5bf9\u4f4d\u7f6e\u53ef\u80fd\u53d1\u751f\u53d8\u5316\u3002
    "},{"location":"chapter_sorting/insertion_sort/","title":"11.4. \u00a0 \u63d2\u5165\u6392\u5e8f","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

    \u56de\u5fc6\u6570\u7ec4\u7684\u5143\u7d20\u63d2\u5165\u64cd\u4f5c\uff0c\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\u518d\u5c06 base \u8d4b\u503c\u7ed9\u76ee\u6807\u7d22\u5f15\u3002

    Fig. \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\u4e0b\uff1a

    1. \u521d\u59cb\u72b6\u6001\u4e0b\uff0c\u6570\u7ec4\u7684\u7b2c 1 \u4e2a\u5143\u7d20\u5df2\u5b8c\u6210\u6392\u5e8f\u3002
    2. \u9009\u53d6\u6570\u7ec4\u7684\u7b2c 2 \u4e2a\u5143\u7d20\u4f5c\u4e3a base \uff0c\u5c06\u5176\u63d2\u5165\u5230\u6b63\u786e\u4f4d\u7f6e\u540e\uff0c\u6570\u7ec4\u7684\u524d 2 \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002
    3. \u9009\u53d6\u7b2c 3 \u4e2a\u5143\u7d20\u4f5c\u4e3a base \uff0c\u5c06\u5176\u63d2\u5165\u5230\u6b63\u786e\u4f4d\u7f6e\u540e\uff0c\u6570\u7ec4\u7684\u524d 3 \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002
    4. \u4ee5\u6b64\u7c7b\u63a8\uff0c\u5728\u6700\u540e\u4e00\u8f6e\u4e2d\uff0c\u9009\u53d6\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a base \uff0c\u5c06\u5176\u63d2\u5165\u5230\u6b63\u786e\u4f4d\u7f6e\u540e\uff0c\u6240\u6709\u5143\u7d20\u5747\u5df2\u6392\u5e8f\u3002

    Fig. \u63d2\u5165\u6392\u5e8f\u6d41\u7a0b

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nfor (int i = 1; i < nums.length; i++) {\nint 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\nwhile (j >= 0 && nums[j] > base) {\nnums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--;\n}\nnums[j + 1] = base;        // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n}\n}\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\nfor (int i = 1; i < nums.size(); i++) {\nint 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\nwhile (j >= 0 && nums[j] > base) {\nnums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--;\n}\nnums[j + 1] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n}\n}\n
    insertion_sort.py
    def insertion_sort(nums: list[int]) -> None:\n\"\"\"\u63d2\u5165\u6392\u5e8f\"\"\"\n# \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\nfor i in range(1, len(nums)):\nbase = nums[i]\nj = 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\nwhile j >= 0 and nums[j] > base:\nnums[j + 1] = nums[j]  # \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj -= 1\nnums[j + 1] = base  # \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\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]\nfor i := 1; i < len(nums); i++ {\nbase := nums[i]\nj := i - 1\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\nfor j >= 0 && nums[j] > base {\nnums[j+1] = nums[j] // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--\n}\nnums[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\nfor (let i = 1; i < nums.length; i++) {\nlet base = nums[i],\nj = i - 1;\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile (j >= 0 && nums[j] > base) {\nnums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--;\n}\nnums[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\nfor (let i = 1; i < nums.length; i++) {\nconst base = nums[i];\nlet j = i - 1;\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile (j >= 0 && nums[j] > base) {\nnums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--;\n}\nnums[j + 1] = 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\nfor (int i = 1; i < size; i++) {\nint 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\nwhile (j >= 0 && nums[j] > base) {\n// \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nnums[j + 1] = nums[j];\nj--;\n}\n// \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\nnums[j + 1] = base;\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\nfor (int i = 1; i < nums.Length; i++) {\nint 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\nwhile (j >= 0 && nums[j] > bas) {\nnums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--;\n}\nnums[j + 1] = bas;         // \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\nfor i in stride(from: 1, to: nums.count, by: 1) {\nlet base = nums[i]\nvar j = i - 1\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile j >= 0, nums[j] > base {\nnums[j + 1] = nums[j] // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj -= 1\n}\nnums[j + 1] = base // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\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\nvar i: usize = 1;\nwhile (i < nums.len) : (i += 1) {\nvar base = nums[i];\nvar j: usize = i;\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile (j >= 1 and nums[j - 1] > base) : (j -= 1) {\nnums[j] = nums[j - 1];  // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n}\nnums[j] = 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\nfor (int i = 1; i < nums.length; i++) {\nint 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\nwhile (j >= 0 && nums[j] > base) {\nnums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--;\n}\nnums[j + 1] = 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":"
    • \u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n^2)\\) \u3001\u81ea\u9002\u5e94\u6392\u5e8f \uff1a\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u6bcf\u6b21\u63d2\u5165\u64cd\u4f5c\u5206\u522b\u9700\u8981\u5faa\u73af \\(n - 1\\) , \\(n-2\\) , \\(\\cdots\\) , \\(2\\) , \\(1\\) \u6b21\uff0c\u6c42\u548c\u5f97\u5230 \\(\\frac{(n - 1) n}{2}\\) \uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002\u5728\u9047\u5230\u6709\u5e8f\u6570\u636e\u65f6\uff0c\u63d2\u5165\u64cd\u4f5c\u4f1a\u63d0\u524d\u7ec8\u6b62\u3002\u5f53\u8f93\u5165\u6570\u7ec4\u5b8c\u5168\u6709\u5e8f\u65f6\uff0c\u63d2\u5165\u6392\u5e8f\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u3002
    • \u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(1)\\) \u3001\u539f\u5730\u6392\u5e8f \uff1a\u6307\u9488 \\(i\\) , \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002
    • \u7a33\u5b9a\u6392\u5e8f\uff1a\u5728\u63d2\u5165\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f1a\u5c06\u5143\u7d20\u63d2\u5165\u5230\u76f8\u7b49\u5143\u7d20\u7684\u53f3\u4fa7\uff0c\u4e0d\u4f1a\u6539\u53d8\u5b83\u4eec\u7684\u987a\u5e8f\u3002
    "},{"location":"chapter_sorting/insertion_sort/#1143","title":"11.4.3. \u00a0 \u63d2\u5165\u6392\u5e8f\u4f18\u52bf","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\u76f8\u6bd4\u5feb\u901f\u6392\u5e8f\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\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\u4f5c\u7528\uff1b\u6bcf\u8f6e\u4e2d\u7684\u5355\u5143\u8ba1\u7b97\u64cd\u4f5c\u6570\u91cf\u8d77\u5230\u51b3\u5b9a\u6027\u56e0\u7d20\u3002

    \u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\uff08\u4f8b\u5982 Java\uff09\u7684\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\u90fd\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\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\u3002\u8fd9\u662f\u56e0\u4e3a\uff1a

    • \u5192\u6ce1\u6392\u5e8f\u57fa\u4e8e\u5143\u7d20\u4ea4\u6362\u5b9e\u73b0\uff0c\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf\uff0c\u5171\u6d89\u53ca 3 \u4e2a\u5355\u5143\u64cd\u4f5c\uff1b\u63d2\u5165\u6392\u5e8f\u57fa\u4e8e\u5143\u7d20\u8d4b\u503c\u5b9e\u73b0\uff0c\u4ec5\u9700 1 \u4e2a\u5355\u5143\u64cd\u4f5c\u3002\u56e0\u6b64\uff0c\u5192\u6ce1\u6392\u5e8f\u7684\u8ba1\u7b97\u5f00\u9500\u901a\u5e38\u6bd4\u63d2\u5165\u6392\u5e8f\u66f4\u9ad8\u3002
    • \u9009\u62e9\u6392\u5e8f\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n^2)\\) \u3002\u5982\u679c\u7ed9\u5b9a\u4e00\u7ec4\u90e8\u5206\u6709\u5e8f\u7684\u6570\u636e\uff0c\u63d2\u5165\u6392\u5e8f\u901a\u5e38\u6bd4\u9009\u62e9\u6392\u5e8f\u6548\u7387\u66f4\u9ad8\u3002
    • \u9009\u62e9\u6392\u5e8f\u4e0d\u7a33\u5b9a\uff0c\u65e0\u6cd5\u5e94\u7528\u4e8e\u591a\u7ea7\u6392\u5e8f\u3002
    "},{"location":"chapter_sorting/merge_sort/","title":"11.6. \u00a0 \u5f52\u5e76\u6392\u5e8f","text":"

    \u300c\u5f52\u5e76\u6392\u5e8f Merge Sort\u300d\u57fa\u4e8e\u5206\u6cbb\u601d\u60f3\u5b9e\u73b0\u6392\u5e8f\uff0c\u5305\u542b\u201c\u5212\u5206\u201d\u548c\u201c\u5408\u5e76\u201d\u4e24\u4e2a\u9636\u6bb5\uff1a

    1. \u5212\u5206\u9636\u6bb5\uff1a\u901a\u8fc7\u9012\u5f52\u4e0d\u65ad\u5730\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5904\u5206\u5f00\uff0c\u5c06\u957f\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\u8f6c\u6362\u4e3a\u77ed\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\uff1b
    2. \u5408\u5e76\u9636\u6bb5\uff1a\u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u5212\u5206\uff0c\u5f00\u59cb\u5408\u5e76\uff0c\u6301\u7eed\u5730\u5c06\u5de6\u53f3\u4e24\u4e2a\u8f83\u77ed\u7684\u6709\u5e8f\u6570\u7ec4\u5408\u5e76\u4e3a\u4e00\u4e2a\u8f83\u957f\u7684\u6709\u5e8f\u6570\u7ec4\uff0c\u76f4\u81f3\u7ed3\u675f\uff1b

    Fig. \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":"

    \u201c\u5212\u5206\u9636\u6bb5\u201d\u4ece\u9876\u81f3\u5e95\u9012\u5f52\u5730\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5207\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u76f4\u81f3\u957f\u5ea6\u4e3a 1 \uff1b

    1. \u8ba1\u7b97\u6570\u7ec4\u4e2d\u70b9 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\uff1b
    2. \u9012\u5f52\u6267\u884c\u6b65\u9aa4 1. \uff0c\u76f4\u81f3\u5b50\u6570\u7ec4\u533a\u95f4\u957f\u5ea6\u4e3a 1 \u65f6\uff0c\u7ec8\u6b62\u9012\u5f52\u5212\u5206\uff1b

    \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>

    \u89c2\u5bdf\u53d1\u73b0\uff0c\u5f52\u5e76\u6392\u5e8f\u7684\u9012\u5f52\u987a\u5e8f\u4e0e\u4e8c\u53c9\u6811\u7684\u540e\u5e8f\u904d\u5386\u76f8\u540c\uff0c\u5177\u4f53\u6765\u770b\uff1a

    • \u540e\u5e8f\u904d\u5386\uff1a\u5148\u9012\u5f52\u5de6\u5b50\u6811\uff0c\u518d\u9012\u5f52\u53f3\u5b50\u6811\uff0c\u6700\u540e\u5904\u7406\u6839\u8282\u70b9\u3002
    • \u5f52\u5e76\u6392\u5e8f\uff1a\u5148\u9012\u5f52\u5de6\u5b50\u6570\u7ec4\uff0c\u518d\u9012\u5f52\u53f3\u5b50\u6570\u7ec4\uff0c\u6700\u540e\u5904\u7406\u5408\u5e76\u3002
    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart merge_sort.java
    /* \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]\nvoid merge(int[] nums, int left, int mid, int right) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nint[] tmp = Arrays.copyOfRange(nums, left, right + 1);\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nint leftStart = left - left, leftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nint rightStart = mid + 1 - left, rightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nint i = leftStart, j = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (int k = left; k <= right; k++) {\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++\nif (i > leftEnd)\nnums[k] = tmp[j++];\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++\nelse if (j > rightEnd || tmp[i] <= tmp[j])\nnums[k] = tmp[i++];\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++\nelse\nnums[k] = tmp[j++];\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(int[] nums, int left, int right) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left >= right)\nreturn;                      // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nint mid = (left + right) / 2;    // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid);      // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n
    merge_sort.cpp
    /* \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]\nvoid merge(vector<int> &nums, int left, int mid, int right) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nvector<int> tmp(nums.begin() + left, nums.begin() + right + 1);\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nint leftStart = left - left, leftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nint rightStart = mid + 1 - left, rightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nint i = leftStart, j = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (int k = left; k <= right; k++) {\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++\nif (i > leftEnd)\nnums[k] = tmp[j++];\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++\nelse if (j > rightEnd || tmp[i] <= tmp[j])\nnums[k] = tmp[i++];\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++\nelse\nnums[k] = tmp[j++];\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(vector<int> &nums, int left, int right) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left >= right)\nreturn; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nint mid = (left + right) / 2;    // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid);      // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n
    merge_sort.py
    def merge(nums: list[int], left: int, mid: int, right: int) -> None:\n\"\"\"\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]\n# \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\ntmp = list(nums[left : right + 1])\n# \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nleft_start = 0\nleft_end = mid - left\n# \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nright_start = mid + 1 - left\nright_end = right - left\n# i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\ni = left_start\nj = right_start\n# \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor k in range(left, right + 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++\nif i > left_end:\nnums[k] = tmp[j]\nj += 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++\nelif j > right_end or tmp[i] <= tmp[j]:\nnums[k] = tmp[i]\ni += 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++\nelse:\nnums[k] = tmp[j]\nj += 1\ndef merge_sort(nums: list[int], left: int, right: int) -> None:\n\"\"\"\u5f52\u5e76\u6392\u5e8f\"\"\"\n# \u7ec8\u6b62\u6761\u4ef6\nif left >= right:\nreturn  # \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n# \u5212\u5206\u9636\u6bb5\nmid = (left + right) // 2  # \u8ba1\u7b97\u4e2d\u70b9\nmerge_sort(nums, left, mid)  # \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmerge_sort(nums, mid + 1, right)  # \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n# \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right)\n
    merge_sort.go
    /* \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]\nfunc merge(nums []int, left, mid, right int) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4 \u501f\u52a9 copy \u6a21\u5757\ntmp := make([]int, right-left+1)\nfor i := left; i <= right; i++ {\ntmp[i-left] = nums[i]\n}\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nleftStart, leftEnd := left-left, mid-left\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nrightStart, rightEnd := mid+1-left, right-left\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\ni, j := leftStart, rightStart\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor k := left; k <= right; k++ {\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++\nif i > leftEnd {\nnums[k] = tmp[j]\nj++\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 || tmp[i] <= tmp[j] {\nnums[k] = tmp[i]\ni++\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 {\nnums[k] = tmp[j]\nj++\n}\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nfunc mergeSort(nums []int, left, right int) {\n// \u7ec8\u6b62\u6761\u4ef6\nif left >= right {\nreturn\n}\n// \u5212\u5206\u9636\u6bb5\nmid := (left + right) / 2\nmergeSort(nums, left, mid)\nmergeSort(nums, mid+1, right)\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right)\n}\n
    merge_sort.js
    /* \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]\nfunction merge(nums, left, mid, right) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nlet tmp = nums.slice(left, right + 1);\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet leftStart = left - left,\nleftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet rightStart = mid + 1 - left,\nrightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nlet i = leftStart,\nj = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (let k = left; k <= right; k++) {\nif (i > leftEnd) {\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++\nnums[k] = tmp[j++];\n} else if (j > rightEnd || tmp[i] <= tmp[j]) {\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++\nnums[k] = tmp[i++];\n} else {\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++\nnums[k] = tmp[j++];\n}\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nfunction mergeSort(nums, left, right) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nlet mid = Math.floor((left + right) / 2); // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n
    merge_sort.ts
    /* \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]\nfunction merge(nums: number[], left: number, mid: number, right: number): void {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nlet tmp = nums.slice(left, right + 1);\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet leftStart = left - left,\nleftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet rightStart = mid + 1 - left,\nrightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nlet i = leftStart,\nj = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (let k = left; k <= right; k++) {\nif (i > leftEnd) {\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++\nnums[k] = tmp[j++];\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 || tmp[i] <= tmp[j]) {\nnums[k] = tmp[i++];\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 {\nnums[k] = tmp[j++];\n}\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nfunction mergeSort(nums: number[], left: number, right: number): void {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nlet mid = Math.floor((left + right) / 2); // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n
    merge_sort.c
    /* \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]\nvoid merge(int *nums, int left, int mid, int right) {\nint index;\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nint tmp[right + 1 - left];\nfor (index = left; index < right + 1; index++) {\ntmp[index - left] = nums[index];\n}\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nint leftStart = left - left, leftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nint rightStart = mid + 1 - left, rightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nint i = leftStart, j = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (int k = left; k <= right; k++) {\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++\nif (i > leftEnd)\nnums[k] = tmp[j++];\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++\nelse if (j > rightEnd || tmp[i] <= tmp[j])\nnums[k] = tmp[i++];\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++\nelse\nnums[k] = tmp[j++];\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(int *nums, int left, int right) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left >= right)\nreturn; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nint mid = (left + right) / 2;    // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid);      // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n
    merge_sort.cs
    /* \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]\nvoid merge(int[] nums, int left, int mid, int right) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nint[] tmp = nums[left..(right + 1)];\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15  \nint leftStart = left - left, leftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15       \nint rightStart = mid + 1 - left, rightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nint i = leftStart, j = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (int k = left; k <= right; k++) {\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++\nif (i > leftEnd)\nnums[k] = tmp[j++];\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++\nelse if (j > rightEnd || tmp[i] <= tmp[j])\nnums[k] = tmp[i++];\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++\nelse\nnums[k] = tmp[j++];\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(int[] nums, int left, int right) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left >= right) return;       // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nint mid = (left + right) / 2;    // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid);      // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n
    merge_sort.swift
    /* \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]\nfunc merge(nums: inout [Int], left: Int, mid: Int, right: Int) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nlet tmp = Array(nums[left ..< (right + 1)])\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet leftStart = left - left\nlet leftEnd = mid - left\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet rightStart = mid + 1 - left\nlet rightEnd = right - left\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nvar i = leftStart\nvar j = rightStart\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor k in left ... right {\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++\nif i > leftEnd {\nnums[k] = tmp[j]\nj += 1\n}\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++\nelse if j > rightEnd || tmp[i] <= tmp[j] {\nnums[k] = tmp[i]\ni += 1\n}\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++\nelse {\nnums[k] = tmp[j]\nj += 1\n}\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nfunc mergeSort(nums: inout [Int], left: Int, right: Int) {\n// \u7ec8\u6b62\u6761\u4ef6\nif left >= right { // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nreturn\n}\n// \u5212\u5206\u9636\u6bb5\nlet mid = (left + right) / 2 // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums: &nums, left: left, right: mid) // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums: &nums, left: mid + 1, right: right) // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums: &nums, left: left, mid: mid, right: 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\nvar mem_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);\ndefer mem_arena.deinit();\nconst mem_allocator = mem_arena.allocator();\nvar tmp = try mem_allocator.alloc(i32, right + 1 - left);\nstd.mem.copy(i32, tmp, nums[left..right+1]);\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15  \nvar leftStart = left - left;\nvar leftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15       \nvar rightStart = mid + 1 - left;\nvar rightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nvar i = leftStart;\nvar j = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nvar k = left;\nwhile (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++\nif (i > leftEnd) {\nnums[k] = tmp[j];\nj += 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]) {\nnums[k] = tmp[i];\ni += 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 {\nnums[k] = tmp[j];\nj += 1;\n}\n}\n}\n// \u5f52\u5e76\u6392\u5e8f\nfn mergeSort(nums: []i32, left: usize, right: usize) !void {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left >= right) return;              // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nvar mid = (left + right) / 2;           // \u8ba1\u7b97\u4e2d\u70b9\ntry mergeSort(nums, left, mid);         // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\ntry mergeSort(nums, mid + 1, right);    // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\ntry merge(nums, left, mid, right);\n}\n
    merge_sort.dart
    /* \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]\nvoid merge(List<int> nums, int left, int mid, int right) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nList<int> tmp = nums.sublist(left, right + 1);\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nint leftStart = left - left, leftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nint rightStart = mid + 1 - left, rightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nint i = leftStart, j = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (int k = left; k <= right; k++) {\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++\nif (i > leftEnd)\nnums[k] = tmp[j++];\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++\nelse if (j > rightEnd || tmp[i] <= tmp[j])\nnums[k] = tmp[i++];\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++\nelse\nnums[k] = tmp[j++];\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(List<int> nums, int left, int right) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nint mid = (left + right) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n

    \u5408\u5e76\u65b9\u6cd5 merge() \u4ee3\u7801\u4e2d\u7684\u96be\u70b9\u5305\u62ec\uff1a

    • \u5728\u9605\u8bfb\u4ee3\u7801\u65f6\uff0c\u9700\u8981\u7279\u522b\u6ce8\u610f\u5404\u4e2a\u53d8\u91cf\u7684\u542b\u4e49\u3002nums \u7684\u5f85\u5408\u5e76\u533a\u95f4\u4e3a [left, right] \uff0c\u4f46\u7531\u4e8e tmp \u4ec5\u590d\u5236\u4e86 nums \u8be5\u533a\u95f4\u7684\u5143\u7d20\uff0c\u56e0\u6b64 tmp \u5bf9\u5e94\u533a\u95f4\u4e3a [0, right - left] \u3002
    • \u5728\u6bd4\u8f83 tmp[i] \u548c tmp[j] \u7684\u5927\u5c0f\u65f6\uff0c\u8fd8\u9700\u8003\u8651\u5b50\u6570\u7ec4\u904d\u5386\u5b8c\u6210\u540e\u7684\u7d22\u5f15\u8d8a\u754c\u95ee\u9898\uff0c\u5373 i > leftEnd \u548c j > rightEnd \u7684\u60c5\u51b5\u3002\u7d22\u5f15\u8d8a\u754c\u7684\u4f18\u5148\u7ea7\u662f\u6700\u9ad8\u7684\uff0c\u5982\u679c\u5de6\u5b50\u6570\u7ec4\u5df2\u7ecf\u88ab\u5408\u5e76\u5b8c\u4e86\uff0c\u90a3\u4e48\u4e0d\u9700\u8981\u7ee7\u7eed\u6bd4\u8f83\uff0c\u76f4\u63a5\u5408\u5e76\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u5373\u53ef\u3002
    "},{"location":"chapter_sorting/merge_sort/#1162","title":"11.6.2. \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
    • \u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n \\log n)\\) \u3001\u975e\u81ea\u9002\u5e94\u6392\u5e8f \uff1a\u5212\u5206\u4ea7\u751f\u9ad8\u5ea6\u4e3a \\(\\log n\\) \u7684\u9012\u5f52\u6811\uff0c\u6bcf\u5c42\u5408\u5e76\u7684\u603b\u64cd\u4f5c\u6570\u91cf\u4e3a \\(n\\) \uff0c\u56e0\u6b64\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002
    • \u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u3001\u975e\u539f\u5730\u6392\u5e8f \uff1a\u9012\u5f52\u6df1\u5ea6\u4e3a \\(\\log n\\) \uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u5927\u5c0f\u7684\u6808\u5e27\u7a7a\u95f4\u3002\u5408\u5e76\u64cd\u4f5c\u9700\u8981\u501f\u52a9\u8f85\u52a9\u6570\u7ec4\u5b9e\u73b0\uff0c\u4f7f\u7528 \\(O(n)\\) \u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002
    • \u7a33\u5b9a\u6392\u5e8f\uff1a\u5728\u5408\u5e76\u8fc7\u7a0b\u4e2d\uff0c\u76f8\u7b49\u5143\u7d20\u7684\u6b21\u5e8f\u4fdd\u6301\u4e0d\u53d8\u3002
    "},{"location":"chapter_sorting/merge_sort/#1163","title":"11.6.3. \u00a0 \u94fe\u8868\u6392\u5e8f *","text":"

    \u5f52\u5e76\u6392\u5e8f\u5728\u6392\u5e8f\u94fe\u8868\u65f6\u5177\u6709\u663e\u8457\u4f18\u52bf\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u4f18\u5316\u81f3 \\(O(1)\\) \uff0c\u539f\u56e0\u5982\u4e0b\uff1a

    • \u7531\u4e8e\u94fe\u8868\u4ec5\u9700\u6539\u53d8\u6307\u9488\u5c31\u53ef\u5b9e\u73b0\u8282\u70b9\u7684\u589e\u5220\u64cd\u4f5c\uff0c\u56e0\u6b64\u5408\u5e76\u9636\u6bb5\uff08\u5c06\u4e24\u4e2a\u77ed\u6709\u5e8f\u94fe\u8868\u5408\u5e76\u4e3a\u4e00\u4e2a\u957f\u6709\u5e8f\u94fe\u8868\uff09\u65e0\u9700\u521b\u5efa\u8f85\u52a9\u94fe\u8868\u3002
    • \u901a\u8fc7\u4f7f\u7528\u201c\u8fed\u4ee3\u5212\u5206\u201d\u66ff\u4ee3\u201c\u9012\u5f52\u5212\u5206\u201d\uff0c\u53ef\u7701\u53bb\u9012\u5f52\u4f7f\u7528\u7684\u6808\u5e27\u7a7a\u95f4\uff1b

    \u5177\u4f53\u5b9e\u73b0\u7ec6\u8282\u6bd4\u8f83\u590d\u6742\uff0c\u6709\u5174\u8da3\u7684\u540c\u5b66\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\u601d\u60f3\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\u300c\u54e8\u5175\u5212\u5206\u300d\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\u4e3a\uff1a

    1. \u9009\u53d6\u6570\u7ec4\u6700\u5de6\u7aef\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6\u6570\uff0c\u521d\u59cb\u5316\u4e24\u4e2a\u6307\u9488 i \u548c j \u5206\u522b\u6307\u5411\u6570\u7ec4\u7684\u4e24\u7aef\uff1b
    2. \u8bbe\u7f6e\u4e00\u4e2a\u5faa\u73af\uff0c\u5728\u6bcf\u8f6e\u4e2d\u4f7f\u7528 i\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\uff1b
    3. \u5faa\u73af\u6267\u884c\u6b65\u9aa4 2. \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\uff1b

    \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

    <1><2><3><4><5><6><7><8><9>

    \u5feb\u901f\u6392\u5e8f\u7684\u5206\u6cbb\u601d\u60f3

    \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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart quick_sort.java
    /* \u5143\u7d20\u4ea4\u6362 */\nvoid swap(int[] nums, int i, int j) {\nint tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n/* \u54e8\u5175\u5212\u5206 */\nint partition(int[] nums, int left, int right) {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left])\nj--;          // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j && nums[i] <= nums[left])\ni++;          // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left);  // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i;             // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
    quick_sort.cpp
    /* \u5143\u7d20\u4ea4\u6362 */\nvoid swap(vector<int> &nums, int i, int j) {\nint tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n/* \u54e8\u5175\u5212\u5206 */\nint partition(vector<int> &nums, int left, int right) {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left])\nj--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j && nums[i] <= nums[left])\ni++;          // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i;            // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
    quick_sort.py
    def partition(self, nums: list[int], left: int, right: int) -> int:\n\"\"\"\u54e8\u5175\u5212\u5206\"\"\"\n# \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\ni, j = left, right\nwhile i < j:\nwhile i < j and nums[j] >= nums[left]:\nj -= 1  # \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile i < j and nums[i] <= nums[left]:\ni += 1  # \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n# \u5143\u7d20\u4ea4\u6362\nnums[i], nums[j] = nums[j], nums[i]\n# \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nnums[i], nums[left] = nums[left], nums[i]\nreturn i  # \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n
    quick_sort.go
    /* \u54e8\u5175\u5212\u5206 */\nfunc (q *quickSort) partition(nums []int, left, right int) int {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\ni, j := left, right\nfor i < j {\nfor i < j && nums[j] >= nums[left] {\nj-- // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nfor i < j && nums[i] <= nums[left] {\ni++ // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\n// \u5143\u7d20\u4ea4\u6362\nnums[i], nums[j] = nums[j], nums[i]\n}\n// \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nnums[i], nums[left] = nums[left], nums[i]\nreturn i // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
    quick_sort.js
    /* \u5143\u7d20\u4ea4\u6362 */\nswap(nums, i, j) {\nlet tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n/* \u54e8\u5175\u5212\u5206 */\npartition(nums, left, right) {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nlet i = left,\nj = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left]) {\nj -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nwhile (i < j && nums[i] <= nums[left]) {\ni += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\n// \u5143\u7d20\u4ea4\u6362\nthis.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nthis.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn 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 {\nlet tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n/* \u54e8\u5175\u5212\u5206 */\npartition(nums: number[], left: number, right: number): number {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nlet i = left,\nj = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left]) {\nj -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nwhile (i < j && nums[i] <= nums[left]) {\ni += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\n// \u5143\u7d20\u4ea4\u6362\nthis.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nthis.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn 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) {\nint tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\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] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left]) {\n// \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nj--;\n}\nwhile (i < j && nums[i] <= nums[left]) {\n// \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\ni++;\n}\n// \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\nswap(nums, i, j);\n}\n// \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nswap(nums, i, left);\n// \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\nreturn i;\n}\n
    quick_sort.cs
    /* \u5143\u7d20\u4ea4\u6362 */\nvoid swap(int[] nums, int i, int j) {\nint tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n/* \u54e8\u5175\u5212\u5206 */\nint partition(int[] nums, int left, int right) {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left])\nj--;          // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j && nums[i] <= nums[left])\ni++;          // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left);  // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn 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) {\nlet tmp = nums[i]\nnums[i] = nums[j]\nnums[j] = tmp\n}\n/* \u54e8\u5175\u5212\u5206 */\nfunc partition(nums: inout [Int], left: Int, right: Int) -> Int {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nvar i = left\nvar j = right\nwhile i < j {\nwhile i < j, nums[j] >= nums[left] {\nj -= 1 // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nwhile i < j, nums[i] <= nums[left] {\ni += 1 // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nswap(nums: &nums, i: i, j: j) // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums: &nums, i: i, j: left) // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
    quick_sort.zig
    // \u5143\u7d20\u4ea4\u6362\nfn swap(nums: []i32, i: usize, j: usize) void {\nvar tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n// \u54e8\u5175\u5212\u5206\nfn partition(nums: []i32, left: usize, right: usize) usize {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nvar i = left;\nvar j = right;\nwhile (i < j) {\nwhile (i < j and nums[j] >= nums[left]) j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j and nums[i] <= nums[left]) i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j);   // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left);    // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn 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) {\nint tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n/* \u54e8\u5175\u5212\u5206 */\nint _partition(List<int> nums, int left, int right) {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left]) j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (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\nreturn 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":"
    1. \u9996\u5148\uff0c\u5bf9\u539f\u6570\u7ec4\u6267\u884c\u4e00\u6b21\u300c\u54e8\u5175\u5212\u5206\u300d\uff0c\u5f97\u5230\u672a\u6392\u5e8f\u7684\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\uff1b
    2. \u7136\u540e\uff0c\u5bf9\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u5206\u522b\u9012\u5f52\u6267\u884c\u300c\u54e8\u5175\u5212\u5206\u300d\uff1b
    3. \u6301\u7eed\u9012\u5f52\uff0c\u76f4\u81f3\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\uff0c\u4ece\u800c\u5b8c\u6210\u6574\u4e2a\u6570\u7ec4\u7684\u6392\u5e8f\uff1b

    Fig. \u5feb\u901f\u6392\u5e8f\u6d41\u7a0b

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nif (left >= right)\nreturn;\n// \u54e8\u5175\u5212\u5206\nint pivot = partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums, left, pivot - 1);\nquickSort(nums, pivot + 1, right);\n}\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\nif (left >= right)\nreturn;\n// \u54e8\u5175\u5212\u5206\nint pivot = partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums, left, pivot - 1);\nquickSort(nums, pivot + 1, right);\n}\n
    quick_sort.py
    def quick_sort(self, nums: list[int], left: int, right: int) -> None:\n\"\"\"\u5feb\u901f\u6392\u5e8f\"\"\"\n# \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nif left >= right:\nreturn\n# \u54e8\u5175\u5212\u5206\npivot = self.partition(nums, left, right)\n# \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nself.quick_sort(nums, left, pivot - 1)\nself.quick_sort(nums, pivot + 1, right)\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\nif left >= right {\nreturn\n}\n// \u54e8\u5175\u5212\u5206\npivot := q.partition(nums, left, right)\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nq.quickSort(nums, left, pivot-1)\nq.quickSort(nums, pivot+1, 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\nif (left >= right) return;\n// \u54e8\u5175\u5212\u5206\nconst pivot = this.partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nthis.quickSort(nums, left, pivot - 1);\nthis.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\nif (left >= right) {\nreturn;\n}\n// \u54e8\u5175\u5212\u5206\nconst pivot = this.partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nthis.quickSort(nums, left, pivot - 1);\nthis.quickSort(nums, pivot + 1, right);\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] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left]) {\n// \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nj--;\n}\nwhile (i < j && nums[i] <= nums[left]) {\n// \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\ni++;\n}\n// \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\nswap(nums, i, j);\n}\n// \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nswap(nums, i, left);\n// \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\nreturn i;\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\nif (left >= right) {\nreturn;\n}\n// \u54e8\u5175\u5212\u5206\nint pivot = partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums, left, pivot - 1);\nquickSort(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\nif (left >= right)\nreturn;\n// \u54e8\u5175\u5212\u5206\nint pivot = partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums, left, pivot - 1);\nquickSort(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\nif left >= right {\nreturn\n}\n// \u54e8\u5175\u5212\u5206\nlet pivot = partition(nums: &nums, left: left, right: right)\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums: &nums, left: left, right: pivot - 1)\nquickSort(nums: &nums, left: pivot + 1, right: 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\nif (left >= right) return;\n// \u54e8\u5175\u5212\u5206\nvar pivot = partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums, left, pivot - 1);\nquickSort(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\nif (left >= right) return;\n// \u54e8\u5175\u5212\u5206\nint pivot = _partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums, left, pivot - 1);\nquickSort(nums, pivot + 1, right);\n}\n
    "},{"location":"chapter_sorting/quick_sort/#1152","title":"11.5.2. \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
    • \u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n \\log n)\\) \u3001\u81ea\u9002\u5e94\u6392\u5e8f \uff1a\u5728\u5e73\u5747\u60c5\u51b5\u4e0b\uff0c\u54e8\u5175\u5212\u5206\u7684\u9012\u5f52\u5c42\u6570\u4e3a \\(\\log n\\) \uff0c\u6bcf\u5c42\u4e2d\u7684\u603b\u5faa\u73af\u6570\u4e3a \\(n\\) \uff0c\u603b\u4f53\u4f7f\u7528 \\(O(n \\log n)\\) \u65f6\u95f4\u3002\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u6bcf\u8f6e\u54e8\u5175\u5212\u5206\u64cd\u4f5c\u90fd\u5c06\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\u5212\u5206\u4e3a\u957f\u5ea6\u4e3a \\(0\\) \u548c \\(n - 1\\) \u7684\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u6b64\u65f6\u9012\u5f52\u5c42\u6570\u8fbe\u5230 \\(n\\) \u5c42\uff0c\u6bcf\u5c42\u4e2d\u7684\u5faa\u73af\u6570\u4e3a \\(n\\) \uff0c\u603b\u4f53\u4f7f\u7528 \\(O(n^2)\\) \u65f6\u95f4\u3002
    • \u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u3001\u539f\u5730\u6392\u5e8f \uff1a\u5728\u8f93\u5165\u6570\u7ec4\u5b8c\u5168\u5012\u5e8f\u7684\u60c5\u51b5\u4e0b\uff0c\u8fbe\u5230\u6700\u5dee\u9012\u5f52\u6df1\u5ea6 \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002\u6392\u5e8f\u64cd\u4f5c\u662f\u5728\u539f\u6570\u7ec4\u4e0a\u8fdb\u884c\u7684\uff0c\u672a\u501f\u52a9\u989d\u5916\u6570\u7ec4\u3002
    • \u975e\u7a33\u5b9a\u6392\u5e8f\uff1a\u5728\u54e8\u5175\u5212\u5206\u7684\u6700\u540e\u4e00\u6b65\uff0c\u57fa\u51c6\u6570\u53ef\u80fd\u4f1a\u88ab\u4ea4\u6362\u81f3\u76f8\u7b49\u5143\u7d20\u7684\u53f3\u4fa7\u3002
    "},{"location":"chapter_sorting/quick_sort/#1153","title":"11.5.3. \u00a0 \u5feb\u6392\u4e3a\u4ec0\u4e48\u5feb\uff1f","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\u300c\u5f52\u5e76\u6392\u5e8f\u300d\u548c\u300c\u5806\u6392\u5e8f\u300d\u76f8\u540c\uff0c\u4f46\u901a\u5e38\u5feb\u901f\u6392\u5e8f\u7684\u6548\u7387\u66f4\u9ad8\uff0c\u539f\u56e0\u5982\u4e0b\uff1a

    • \u51fa\u73b0\u6700\u5dee\u60c5\u51b5\u7684\u6982\u7387\u5f88\u4f4e\uff1a\u867d\u7136\u5feb\u901f\u6392\u5e8f\u7684\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u6ca1\u6709\u5f52\u5e76\u6392\u5e8f\u7a33\u5b9a\uff0c\u4f46\u5728\u7edd\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5feb\u901f\u6392\u5e8f\u80fd\u5728 \\(O(n \\log n)\\) \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e0b\u8fd0\u884c\u3002
    • \u7f13\u5b58\u4f7f\u7528\u6548\u7387\u9ad8\uff1a\u5728\u6267\u884c\u54e8\u5175\u5212\u5206\u64cd\u4f5c\u65f6\uff0c\u7cfb\u7edf\u53ef\u5c06\u6574\u4e2a\u5b50\u6570\u7ec4\u52a0\u8f7d\u5230\u7f13\u5b58\uff0c\u56e0\u6b64\u8bbf\u95ee\u5143\u7d20\u7684\u6548\u7387\u8f83\u9ad8\u3002\u800c\u50cf\u300c\u5806\u6392\u5e8f\u300d\u8fd9\u7c7b\u7b97\u6cd5\u9700\u8981\u8df3\u8dc3\u5f0f\u8bbf\u95ee\u5143\u7d20\uff0c\u4ece\u800c\u7f3a\u4e4f\u8fd9\u4e00\u7279\u6027\u3002
    • \u590d\u6742\u5ea6\u7684\u5e38\u6570\u7cfb\u6570\u4f4e\uff1a\u5728\u4e0a\u8ff0\u4e09\u79cd\u7b97\u6cd5\u4e2d\uff0c\u5feb\u901f\u6392\u5e8f\u7684\u6bd4\u8f83\u3001\u8d4b\u503c\u3001\u4ea4\u6362\u7b49\u64cd\u4f5c\u7684\u603b\u6570\u91cf\u6700\u5c11\u3002\u8fd9\u4e0e\u300c\u63d2\u5165\u6392\u5e8f\u300d\u6bd4\u300c\u5192\u6ce1\u6392\u5e8f\u300d\u66f4\u5feb\u7684\u539f\u56e0\u7c7b\u4f3c\u3002
    "},{"location":"chapter_sorting/quick_sort/#1154","title":"11.5.4. \u00a0 \u57fa\u51c6\u6570\u4f18\u5316","text":"

    \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\u300c\u5192\u6ce1\u6392\u5e8f\u300d\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nif ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))\nreturn left;\nelse if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\nreturn mid;\nelse\nreturn right;\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\nint med = medianThree(nums, left, (left + right) / 2, right);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nswap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left])\nj--;          // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j && nums[i] <= nums[left])\ni++;          // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left);  // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i;             // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\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\nif ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))\nreturn left;\nelse if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\nreturn mid;\nelse\nreturn right;\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\nint med = medianThree(nums, left, (left + right) / 2, right);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nswap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left])\nj--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j && nums[i] <= nums[left])\ni++;          // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i;            // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
    quick_sort.py
    def 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\nif (nums[left] < nums[mid]) ^ (nums[left] < nums[right]):\nreturn left\nelif (nums[mid] < nums[left]) ^ (nums[mid] < nums[right]):\nreturn mid\nreturn right\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] \u4f5c\u4e3a\u57fa\u51c6\u6570\nmed = self.median_three(nums, left, (left + right) // 2, right)\n# \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nnums[left], nums[med] = nums[med], nums[left]\n# \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\ni, j = left, right\nwhile i < j:\nwhile i < j and nums[j] >= nums[left]:\nj -= 1  # \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile i < j and nums[i] <= nums[left]:\ni += 1  # \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n# \u5143\u7d20\u4ea4\u6362\nnums[i], nums[j] = nums[j], nums[i]\n# \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nnums[i], nums[left] = nums[left], nums[i]\nreturn i  # \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\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\nif (nums[left] < nums[mid]) != (nums[left] < nums[right]) {\nreturn left\n} else if (nums[mid] < nums[left]) != (nums[mid] < nums[right]) {\nreturn mid\n}\nreturn right\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] \u4f5c\u4e3a\u57fa\u51c6\u6570\nmed := q.medianThree(nums, left, (left+right)/2, right)\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nnums[left], nums[med] = nums[med], nums[left]\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\ni, j := left, right\nfor i < j {\nfor i < j && nums[j] >= nums[left] {\nj-- //\u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nfor i < j && nums[i] <= nums[left] {\ni++ //\u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\n//\u5143\u7d20\u4ea4\u6362\nnums[i], nums[j] = nums[j], nums[i]\n}\n//\u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nnums[i], nums[left] = nums[left], nums[i]\nreturn i //\u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\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\nif ((nums[left] < nums[mid]) ^ (nums[left] < nums[right])) return left;\nelse if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\nreturn mid;\nelse return right;\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\nlet med = this.medianThree(\nnums,\nleft,\nMath.floor((left + right) / 2),\nright\n);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nthis.swap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nlet i = left,\nj = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left]) j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j && nums[i] <= nums[left]) i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nthis.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nthis.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn 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(\nnums: number[],\nleft: number,\nmid: number,\nright: 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\nif (Number(nums[left] < nums[mid]) ^ Number(nums[left] < nums[right])) {\nreturn left;\n} else if (\nNumber(nums[mid] < nums[left]) ^ Number(nums[mid] < nums[right])\n) {\nreturn mid;\n} else {\nreturn right;\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\nlet med = this.medianThree(\nnums,\nleft,\nMath.floor((left + right) / 2),\nright\n);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nthis.swap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nlet i = left,\nj = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left]) {\nj--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nwhile (i < j && nums[i] <= nums[left]) {\ni++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nthis.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nthis.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn 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\nif ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))\nreturn left;\nelse if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\nreturn mid;\nelse\nreturn right;\n}\n/* \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\nif ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))\nreturn left;\nelse if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\nreturn mid;\nelse\nreturn right;\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\nint med = medianThree(nums, left, (left + right) / 2, right);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nswap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left])\nj--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j && nums[i] <= nums[left])\ni++;          // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn 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\nif ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))\nreturn left;\nelse if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\nreturn mid;\nelse\nreturn right;\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\nint med = medianThree(nums, left, (left + right) / 2, right);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nswap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left])\nj--;          // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j && nums[i] <= nums[left])\ni++;          // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left);  // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn 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 {\nif (nums[left] < nums[mid]) != (nums[left] < nums[right]) {\nreturn left\n} else if (nums[mid] < nums[left]) != (nums[mid] < nums[right]) {\nreturn mid\n} else {\nreturn right\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\nlet 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\nswap(nums: &nums, i: left, j: med)\nreturn partition(nums: &nums, left: left, right: right)\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\nif ((nums[left] < nums[mid]) != (nums[left] < nums[right])) {\nreturn left;\n} else if ((nums[mid] < nums[left]) != (nums[mid] < nums[right])) {\nreturn mid;\n} else {\nreturn right;\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\nvar med = medianThree(nums, left, (left + right) / 2, right);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nswap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nvar i = left;\nvar j = right;\nwhile (i < j) {\nwhile (i < j and nums[j] >= nums[left]) j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j and nums[i] <= nums[left]) i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j);   // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left);    // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn 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\nif ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))\nreturn left;\nelse if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\nreturn mid;\nelse\nreturn right;\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\nint 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] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left]) j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (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\nreturn 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\u5012\u5e8f\u7684\u8f93\u5165\u6570\u7ec4\u4e3a\u4f8b\uff0c\u7531\u4e8e\u6bcf\u8f6e\u54e8\u5175\u5212\u5206\u540e\u53f3\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a \\(0\\) \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 \\(\\frac{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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nwhile (left < right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nint pivot = partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left < right - pivot) {\nquickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nquickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n}\n}\n}\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\nwhile (left < right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nint pivot = partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left < right - pivot) {\nquickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1;                 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nquickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1;                 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n}\n}\n}\n
    quick_sort.py
    def quick_sort(self, nums: list[int], left: int, right: int) -> None:\n\"\"\"\u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09\"\"\"\n# \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\nwhile left < right:\n# \u54e8\u5175\u5212\u5206\u64cd\u4f5c\npivot = self.partition(nums, left, right)\n# \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif pivot - left < right - pivot:\nself.quick_sort(nums, left, pivot - 1)  # \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1  # \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\nelse:\nself.quick_sort(nums, pivot + 1, right)  # \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1  # \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\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\nfor left < right {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\npivot := q.partition(nums, left, right)\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif pivot-left < right-pivot {\nq.quickSort(nums, left, pivot-1) // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1                 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nq.quickSort(nums, pivot+1, right) // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = 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\nwhile (left < right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nlet pivot = this.partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left < right - pivot) {\nthis.quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nthis.quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = 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\nwhile (left < right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nlet pivot = this.partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left < right - pivot) {\nthis.quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nthis.quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = 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\nwhile (left < right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nint pivot = partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left < right - pivot) {\nquickSortTailCall(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1;                         // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nquickSortTailCall(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = 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\nwhile (left < right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nint pivot = partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left < right - pivot) {\nquickSort(nums, left, pivot - 1);  // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1;  // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nquickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = 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) {\nvar left = left\nvar right = right\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\nwhile left < right {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nlet pivot = partition(nums: &nums, left: left, right: right)\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left) < (right - pivot) {\nquickSortTailCall(nums: &nums, left: left, right: pivot - 1) // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nquickSortTailCall(nums: &nums, left: pivot + 1, right: right) // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = 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 {\nvar left = left_;\nvar right = right_;\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nwhile (left < right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nvar pivot = partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left < right - pivot) {\nquickSort(nums, left, pivot - 1);   // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1;                   // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nquickSort(nums, pivot + 1, right);  // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = 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\nwhile (left < right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nint pivot = _partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left < right - pivot) {\nquickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nquickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = 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\u6211\u4eec\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\u6b65\u9aa4\u5982\u4e0b\uff1a

    1. \u521d\u59cb\u5316\u4f4d\u6570 \\(k = 1\\) \uff1b
    2. \u5bf9\u5b66\u53f7\u7684\u7b2c \\(k\\) \u4f4d\u6267\u884c\u300c\u8ba1\u6570\u6392\u5e8f\u300d\u3002\u5b8c\u6210\u540e\uff0c\u6570\u636e\u4f1a\u6839\u636e\u7b2c \\(k\\) \u4f4d\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\uff1b
    3. \u5c06 \\(k\\) \u589e\u52a0 \\(1\\) \uff0c\u7136\u540e\u8fd4\u56de\u6b65\u9aa4 2. \u7ee7\u7eed\u8fed\u4ee3\uff0c\u76f4\u5230\u6240\u6709\u4f4d\u90fd\u6392\u5e8f\u5b8c\u6210\u540e\u7ed3\u675f\uff1b

    Fig. \u57fa\u6570\u6392\u5e8f\u7b97\u6cd5\u6d41\u7a0b

    \u4e0b\u9762\u6765\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 \\space 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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nreturn (num / exp) % 10;\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\nint[] counter = new int[10];\nint n = nums.length;\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (int i = 0; i < n; i++) {\nint d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[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\nfor (int i = 1; i < 10; i++) {\ncounter[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\nint[] res = new int[n];\nfor (int i = n - 1; i >= 0; i--) {\nint d = digit(nums[i], exp);\nint j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i];       // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d]--;           // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor (int i = 0; i < n; i++)\nnums[i] = res[i];\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\nint m = Integer.MIN_VALUE;\nfor (int num : nums)\nif (num > m)\nm = num;\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor (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)\ncountingSortDigit(nums, exp);\n}\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\nreturn (num / exp) % 10;\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\nvector<int> counter(10, 0);\nint n = nums.size();\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (int i = 0; i < n; i++) {\nint d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[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\nfor (int i = 1; i < 10; i++) {\ncounter[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\nvector<int> res(n, 0);\nfor (int i = n - 1; i >= 0; i--) {\nint d = digit(nums[i], exp);\nint j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i];       // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d]--;           // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor (int i = 0; i < n; i++)\nnums[i] = res[i];\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\nint m = *max_element(nums.begin(), nums.end());\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor (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)\ncountingSortDigit(nums, exp);\n}\n
    radix_sort.py
    def 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\nreturn (num // exp) % 10\ndef counting_sort_digit(nums: list[int], exp: int) -> None:\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\ncounter = [0] * 10\nn = len(nums)\n# \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor i in range(n):\nd = digit(nums[i], exp)  # \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[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\nfor i in range(1, 10):\ncounter[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\nres = [0] * n\nfor i in range(n - 1, -1, -1):\nd = digit(nums[i], exp)\nj = counter[d] - 1  # \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i]  # \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d] -= 1  # \u5c06 d \u7684\u6570\u91cf\u51cf 1\n# \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor i in range(n):\nnums[i] = res[i]\ndef radix_sort(nums: list[int]) -> None:\n\"\"\"\u57fa\u6570\u6392\u5e8f\"\"\"\n# \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\nm = max(nums)\n# \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nexp = 1\nwhile 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)\ncounting_sort_digit(nums, exp)\nexp *= 10\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\nreturn (num / exp) % 10\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\ncounter := make([]int, 10)\nn := len(nums)\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor i := 0; i < n; i++ {\nd := digit(nums[i], exp) // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[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\nfor i := 1; i < 10; i++ {\ncounter[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\nres := make([]int, n)\nfor i := n - 1; i >= 0; i-- {\nd := digit(nums[i], exp)\nj := counter[d] - 1 // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i]    // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d]--        // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor i := 0; i < n; i++ {\nnums[i] = res[i]\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\nmax := math.MinInt\nfor _, num := range nums {\nif num > max {\nmax = num\n}\n}\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor 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)\ncountingSortDigit(nums, 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\nreturn Math.floor(num / exp) % 10;\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\nconst counter = new Array(10).fill(0);\nconst n = nums.length;\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (let i = 0; i < n; i++) {\nconst d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[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\nfor (let i = 1; i < 10; i++) {\ncounter[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\nconst res = new Array(n).fill(0);\nfor (let i = n - 1; i >= 0; i--) {\nconst d = digit(nums[i], exp);\nconst j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor (let i = 0; i < n; i++) {\nnums[i] = res[i];\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\nlet m = Number.MIN_VALUE;\nfor (const num of nums) {\nif (num > m) {\nm = num;\n}\n}\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor (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)\ncountingSortDigit(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\nreturn Math.floor(num / exp) % 10;\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\nconst counter = new Array(10).fill(0);\nconst n = nums.length;\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (let i = 0; i < n; i++) {\nconst d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[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\nfor (let i = 1; i < 10; i++) {\ncounter[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\nconst res = new Array(n).fill(0);\nfor (let i = n - 1; i >= 0; i--) {\nconst d = digit(nums[i], exp);\nconst j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor (let i = 0; i < n; i++) {\nnums[i] = res[i];\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\nlet m = Number.MIN_VALUE;\nfor (const num of nums) {\nif (num > m) {\nm = num;\n}\n}\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor (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)\ncountingSortDigit(nums, exp);\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\nreturn (num / exp) % 10;\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\nint *counter = (int *)malloc((sizeof(int) * 10));\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (int i = 0; i < size; i++) {\n// \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\nint d = digit(nums[i], exp);\n// \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\ncounter[d]++;\n}\n// \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\nfor (int i = 1; i < 10; i++) {\ncounter[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\nint *res = (int *)malloc(sizeof(int) * size);\nfor (int i = size - 1; i >= 0; i--) {\nint d = digit(nums[i], exp);\nint j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i];       // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d]--;           // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor (int i = 0; i < size; i++) {\nnums[i] = res[i];\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\nint max = INT32_MIN;\nfor (size_t i = 0; i < size - 1; i++) {\nif (nums[i] > max) {\nmax = nums[i];\n}\n}\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor (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)\ncountingSortDigit(nums, size, 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\nreturn (num / exp) % 10;\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\nint[] counter = new int[10];\nint n = nums.Length;\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (int i = 0; i < n; i++) {\nint d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[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\nfor (int i = 1; i < 10; i++) {\ncounter[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\nint[] res = new int[n];\nfor (int i = n - 1; i >= 0; i--) {\nint d = digit(nums[i], exp);\nint j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i];       // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d]--;           // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor (int i = 0; i < n; i++) {\nnums[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\nint m = int.MinValue;\nforeach (int num in nums) {\nif (num > m) m = num;\n}\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor (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)\ncountingSortDigit(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/* \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\nvar counter = Array(repeating: 0, count: 10)\nlet n = nums.count\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor i in nums.indices {\nlet d = digit(num: nums[i], exp: exp) // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[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\nfor i in 1 ..< 10 {\ncounter[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\nvar res = Array(repeating: 0, count: n)\nfor i in stride(from: n - 1, through: 0, by: -1) {\nlet d = digit(num: nums[i], exp: exp)\nlet j = counter[d] - 1 // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i] // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d] -= 1 // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor i in nums.indices {\nnums[i] = res[i]\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\nvar m = Int.min\nfor num in nums {\nif num > m {\nm = num\n}\n}\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor 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)\ncountingSortDigit(nums: &nums, exp: exp)\n}\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\nreturn @mod(@divFloor(num, exp), 10);\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\nvar mem_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);\n// defer mem_arena.deinit();\nconst mem_allocator = mem_arena.allocator();\nvar counter = try mem_allocator.alloc(usize, 10);\nstd.mem.set(usize, counter, 0);\nvar n = nums.len;\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (nums) |num| {\nvar d = @bitCast(u32, digit(num, exp)); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[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\nvar i: usize = 1;\nwhile (i < 10) : (i += 1) {\ncounter[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\nvar res = try mem_allocator.alloc(i32, n);\ni = n - 1;\nwhile (i >= 0) : (i -= 1) {\nvar d = @bitCast(u32, digit(nums[i], exp));\nvar j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i];       // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d] -= 1;        // \u5c06 d \u7684\u6570\u91cf\u51cf 1\nif (i == 0) break;\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\ni = 0;\nwhile (i < n) : (i += 1) {\nnums[i] = res[i];\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\nvar m: i32 = std.math.minInt(i32);\nfor (nums) |num| {\nif (num > m) m = num;\n}\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nvar exp: i32 = 1;\nwhile (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)\ntry countingSortDigit(nums, exp);    }\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\nreturn (num ~/ exp) % 10;\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\nList<int> counter = List<int>.filled(10, 0);\nint n = nums.length;\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (int i = 0; i < n; i++) {\nint d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[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\nfor (int i = 1; i < 10; i++) {\ncounter[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\nList<int> res = List<int>.filled(n, 0);\nfor (int i = n - 1; i >= 0; i--) {\nint d = digit(nums[i], exp);\nint j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor (int i = 0; i < n; i++) nums[i] = res[i];\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\nint m = -1 << 63; for (int num in nums) if (num > m) m = num;\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor (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)\ncountingSortDigit(nums, exp);\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\u6211\u4eec\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

    • \u65f6\u95f4\u590d\u6742\u5ea6 \\(O(nk)\\) \uff1a\u8bbe\u6570\u636e\u91cf\u4e3a \\(n\\) \u3001\u6570\u636e\u4e3a \\(d\\) \u8fdb\u5236\u3001\u6700\u5927\u4f4d\u6570\u4e3a \\(k\\) \uff0c\u5219\u5bf9\u67d0\u4e00\u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\u4f7f\u7528 \\(O(n + d)\\) \u65f6\u95f4\uff0c\u6392\u5e8f\u6240\u6709 \\(k\\) \u4f4d\u4f7f\u7528 \\(O((n + d)k)\\) \u65f6\u95f4\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\\(d\\) \u548c \\(k\\) \u90fd\u76f8\u5bf9\u8f83\u5c0f\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u8d8b\u5411 \\(O(n)\\) \u3002
    • \u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(n + d)\\) \u3001\u975e\u539f\u5730\u6392\u5e8f \uff1a\u4e0e\u8ba1\u6570\u6392\u5e8f\u76f8\u540c\uff0c\u57fa\u6570\u6392\u5e8f\u9700\u8981\u501f\u52a9\u957f\u5ea6\u4e3a \\(n\\) \u548c \\(d\\) \u7684\u6570\u7ec4 res \u548c counter \u3002
    • \u7a33\u5b9a\u6392\u5e8f\uff1a\u4e0e\u8ba1\u6570\u6392\u5e8f\u76f8\u540c\u3002
    "},{"location":"chapter_sorting/selection_sort/","title":"11.2. \u00a0 \u9009\u62e9\u6392\u5e8f","text":"

    \u300c\u9009\u62e9\u6392\u5e8f Selection Sort\u300d\u7684\u5de5\u4f5c\u539f\u7406\u975e\u5e38\u76f4\u63a5\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\u4e0b\uff1a

    1. \u521d\u59cb\u72b6\u6001\u4e0b\uff0c\u6240\u6709\u5143\u7d20\u672a\u6392\u5e8f\uff0c\u5373\u672a\u6392\u5e8f\uff08\u7d22\u5f15\uff09\u533a\u95f4\u4e3a \\([0, n-1]\\) \u3002
    2. \u9009\u53d6\u533a\u95f4 \\([0, n-1]\\) \u4e2d\u7684\u6700\u5c0f\u5143\u7d20\uff0c\u5c06\u5176\u4e0e\u7d22\u5f15 \\(0\\) \u5904\u5143\u7d20\u4ea4\u6362\u3002\u5b8c\u6210\u540e\uff0c\u6570\u7ec4\u524d 1 \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002
    3. \u9009\u53d6\u533a\u95f4 \\([1, n-1]\\) \u4e2d\u7684\u6700\u5c0f\u5143\u7d20\uff0c\u5c06\u5176\u4e0e\u7d22\u5f15 \\(1\\) \u5904\u5143\u7d20\u4ea4\u6362\u3002\u5b8c\u6210\u540e\uff0c\u6570\u7ec4\u524d 2 \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002
    4. \u4ee5\u6b64\u7c7b\u63a8\u3002\u7ecf\u8fc7 \\(n - 1\\) \u8f6e\u9009\u62e9\u4e0e\u4ea4\u6362\u540e\uff0c\u6570\u7ec4\u524d \\(n - 1\\) \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002
    5. \u4ec5\u5269\u7684\u4e00\u4e2a\u5143\u7d20\u5fc5\u5b9a\u662f\u6700\u5927\u5143\u7d20\uff0c\u65e0\u9700\u6392\u5e8f\uff0c\u56e0\u6b64\u6570\u7ec4\u6392\u5e8f\u5b8c\u6210\u3002
    <1><2><3><4><5><6><7><8><9><10><11>

    \u5728\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u7528 \\(k\\) \u6765\u8bb0\u5f55\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart selection_sort.java
    /* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(int[] nums) {\nint n = nums.length;\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor (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\nint k = i;\nfor (int j = i + 1; j < n; j++) {\nif (nums[j] < nums[k])\nk = 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\nint temp = nums[i];\nnums[i] = nums[k];\nnums[k] = temp;\n}\n}\n
    selection_sort.cpp
    /* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(vector<int> &nums) {\nint n = nums.size();\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor (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\nint k = i;\nfor (int j = i + 1; j < n; j++) {\nif (nums[j] < nums[k])\nk = 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\nswap(nums[i], nums[k]);\n}\n}\n
    selection_sort.py
    def selection_sort(nums: list[int]):\n\"\"\"\u9009\u62e9\u6392\u5e8f\"\"\"\nn = len(nums)\n# \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor i in range(n - 1):\n# \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\nk = i\nfor j in range(i + 1, n):\nif nums[j] < nums[k]:\nk = 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\nnums[i], nums[k] = nums[k], nums[i]\n
    selection_sort.go
    /* \u9009\u62e9\u6392\u5e8f */\nfunc selectionSort(nums []int) {\nn := len(nums)\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor i := 0; i < n-1; i++ {\n// \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\nk := i\nfor j := i + 1; j < n; j++ {\nif nums[j] < nums[k] {\n// \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\nk = j\n}\n}\n// \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\nnums[i], nums[k] = nums[k], nums[i]\n}\n}\n
    selection_sort.js
    /* \u9009\u62e9\u6392\u5e8f */\nfunction selectionSort(nums) {\nlet n = nums.length;\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor (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\nlet k = i;\nfor (let j = i + 1; j < n; j++) {\nif (nums[j] < nums[k]) {\nk = 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 {\nlet n = nums.length;\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor (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\nlet k = i;\nfor (let j = i + 1; j < n; j++) {\nif (nums[j] < nums[k]) {\nk = 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.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]\nfor (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\nint k = i;\nfor (int j = i + 1; j < n; j++) {\nif (nums[j] < nums[k])\nk = 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\nint temp = nums[i];\nnums[i] = nums[k];\nnums[k] = temp;\n}\n}\n
    selection_sort.cs
    /* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(int[] nums) {\nint n = nums.Length;\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor (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\nint k = i;\nfor (int j = i + 1; j < n; j++) {\nif (nums[j] < nums[k])\nk = 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.swift
    /* \u9009\u62e9\u6392\u5e8f */\nfunc selectionSort(nums: inout [Int]) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor i in nums.indices.dropLast() {\n// \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\nvar k = i\nfor j in nums.indices.dropFirst(i + 1) {\nif nums[j] < nums[k] {\nk = 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\nnums.swapAt(i, k)\n}\n}\n
    selection_sort.zig
    [class]{}-[func]{selectionSort}\n
    selection_sort.dart
    /* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(List<int> nums) {\nint n = nums.length;\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor (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\nint k = i;\nfor (int j = i + 1; j < n; j++) {\nif (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\nint temp = nums[i];\nnums[i] = nums[k];\nnums[k] = temp;\n}\n}\n
    "},{"location":"chapter_sorting/selection_sort/#1121","title":"11.2.1. \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
    • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3001\u975e\u81ea\u9002\u5e94\u6392\u5e8f\uff1a\u5916\u5faa\u73af\u5171 \\(n - 1\\) \u8f6e\uff0c\u7b2c\u4e00\u8f6e\u7684\u672a\u6392\u5e8f\u533a\u95f4\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u6700\u540e\u4e00\u8f6e\u7684\u672a\u6392\u5e8f\u533a\u95f4\u957f\u5ea6\u4e3a \\(2\\) \uff0c\u5373\u5404\u8f6e\u5916\u5faa\u73af\u5206\u522b\u5305\u542b \\(n\\) , \\(n - 1\\) , \\(\\cdots\\) , \\(2\\) \u8f6e\u5185\u5faa\u73af\uff0c\u6c42\u548c\u4e3a \\(\\frac{(n - 1)(n + 2)}{2}\\) \u3002
    • \u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(1)\\) \u3001\u539f\u5730\u6392\u5e8f\uff1a\u6307\u9488 \\(i\\) , \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002
    • \u975e\u7a33\u5b9a\u6392\u5e8f\uff1a\u5728\u4ea4\u6362\u5143\u7d20\u65f6\uff0c\u6709\u53ef\u80fd\u5c06 nums[i] \u4ea4\u6362\u81f3\u5176\u76f8\u7b49\u5143\u7d20\u7684\u53f3\u8fb9\uff0c\u5bfc\u81f4\u4e24\u8005\u7684\u76f8\u5bf9\u987a\u5e8f\u53d1\u751f\u6539\u53d8\u3002

    Fig. \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\u6709\u6548\u5730\u67e5\u627e\u3001\u5206\u6790\u548c\u5904\u7406\u3002

    \u5728\u6392\u5e8f\u7b97\u6cd5\u4e2d\uff0c\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u662f\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\u7b49\uff1b\u987a\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

    Fig. \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\u5373\u65f6\u95f4\u590d\u6742\u5ea6\u4e2d\u7684\u5e38\u6570\u9879\u964d\u4f4e\uff09\u3002\u5bf9\u4e8e\u5927\u6570\u636e\u91cf\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\u9700\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\u4f18\u826f\u7279\u6027\uff0c\u4e5f\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, 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\u3002

    # \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# \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\u3001\u6700\u5dee\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\u4e8e\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff08\\(<\\) , \\(=\\) , \\(>\\)\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":"
    • \u5192\u6ce1\u6392\u5e8f\u901a\u8fc7\u4ea4\u6362\u76f8\u90bb\u5143\u7d20\u6765\u5b9e\u73b0\u6392\u5e8f\u3002\u901a\u8fc7\u6dfb\u52a0\u4e00\u4e2a\u6807\u5fd7\u4f4d\u6765\u5b9e\u73b0\u63d0\u524d\u8fd4\u56de\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5192\u6ce1\u6392\u5e8f\u7684\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u5230 \\(O(n)\\) \u3002
    • \u63d2\u5165\u6392\u5e8f\u6bcf\u8f6e\u5c06\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u5143\u7d20\u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4\u7684\u6b63\u786e\u4f4d\u7f6e\uff0c\u4ece\u800c\u5b8c\u6210\u6392\u5e8f\u3002\u867d\u7136\u63d2\u5165\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u4f46\u7531\u4e8e\u5355\u5143\u64cd\u4f5c\u76f8\u5bf9\u8f83\u5c11\uff0c\u5b83\u5728\u5c0f\u6570\u636e\u91cf\u7684\u6392\u5e8f\u4efb\u52a1\u4e2d\u975e\u5e38\u53d7\u6b22\u8fce\u3002
    • \u5feb\u901f\u6392\u5e8f\u57fa\u4e8e\u54e8\u5175\u5212\u5206\u64cd\u4f5c\u5b9e\u73b0\u6392\u5e8f\u3002\u5728\u54e8\u5175\u5212\u5206\u4e2d\uff0c\u6709\u53ef\u80fd\u6bcf\u6b21\u90fd\u9009\u53d6\u5230\u6700\u5dee\u7684\u57fa\u51c6\u6570\uff0c\u5bfc\u81f4\u65f6\u95f4\u590d\u6742\u5ea6\u52a3\u5316\u81f3 \\(O(n^2)\\) \u3002\u5f15\u5165\u4e2d\u4f4d\u6570\u57fa\u51c6\u6570\u6216\u968f\u673a\u57fa\u51c6\u6570\u53ef\u4ee5\u964d\u4f4e\u8fd9\u79cd\u52a3\u5316\u7684\u6982\u7387\u3002\u5c3e\u9012\u5f52\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5730\u51cf\u5c11\u9012\u5f52\u6df1\u5ea6\uff0c\u5c06\u7a7a\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u5230 \\(O(\\log n)\\) \u3002
    • \u5f52\u5e76\u6392\u5e8f\u5305\u62ec\u5212\u5206\u548c\u5408\u5e76\u4e24\u4e2a\u9636\u6bb5\uff0c\u5178\u578b\u5730\u4f53\u73b0\u4e86\u5206\u6cbb\u7b56\u7565\u3002\u5728\u5f52\u5e76\u6392\u5e8f\u4e2d\uff0c\u6392\u5e8f\u6570\u7ec4\u9700\u8981\u521b\u5efa\u8f85\u52a9\u6570\u7ec4\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff1b\u7136\u800c\u6392\u5e8f\u94fe\u8868\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u4f18\u5316\u81f3 \\(O(1)\\) \u3002
    • \u6876\u6392\u5e8f\u5305\u542b\u4e09\u4e2a\u6b65\u9aa4\uff1a\u6570\u636e\u5206\u6876\u3001\u6876\u5185\u6392\u5e8f\u548c\u5408\u5e76\u7ed3\u679c\u3002\u5b83\u540c\u6837\u4f53\u73b0\u4e86\u5206\u6cbb\u7b56\u7565\uff0c\u9002\u7528\u4e8e\u6570\u636e\u4f53\u91cf\u5f88\u5927\u7684\u60c5\u51b5\u3002\u6876\u6392\u5e8f\u7684\u5173\u952e\u5728\u4e8e\u5bf9\u6570\u636e\u8fdb\u884c\u5e73\u5747\u5206\u914d\u3002
    • \u8ba1\u6570\u6392\u5e8f\u662f\u6876\u6392\u5e8f\u7684\u4e00\u4e2a\u7279\u4f8b\uff0c\u5b83\u901a\u8fc7\u7edf\u8ba1\u6570\u636e\u51fa\u73b0\u7684\u6b21\u6570\u6765\u5b9e\u73b0\u6392\u5e8f\u3002\u8ba1\u6570\u6392\u5e8f\u9002\u7528\u4e8e\u6570\u636e\u91cf\u5927\u4f46\u6570\u636e\u8303\u56f4\u6709\u9650\u7684\u60c5\u51b5\uff0c\u5e76\u4e14\u8981\u6c42\u6570\u636e\u80fd\u591f\u8f6c\u6362\u4e3a\u6b63\u6574\u6570\u3002
    • \u57fa\u6570\u6392\u5e8f\u901a\u8fc7\u9010\u4f4d\u6392\u5e8f\u6765\u5b9e\u73b0\u6570\u636e\u6392\u5e8f\uff0c\u8981\u6c42\u6570\u636e\u80fd\u591f\u8868\u793a\u4e3a\u56fa\u5b9a\u4f4d\u6570\u7684\u6570\u5b57\u3002

    Fig. \u6392\u5e8f\u7b97\u6cd5\u5bf9\u6bd4

    • \u603b\u7684\u6765\u8bf4\uff0c\u6211\u4eec\u5e0c\u671b\u627e\u5230\u4e00\u79cd\u6392\u5e8f\u7b97\u6cd5\uff0c\u5177\u6709\u9ad8\u6548\u7387\u3001\u7a33\u5b9a\u3001\u539f\u5730\u4ee5\u53ca\u6b63\u5411\u81ea\u9002\u5e94\u6027\u7b49\u4f18\u70b9\u3002\u7136\u800c\uff0c\u6b63\u5982\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u4e00\u6837\uff0c\u6ca1\u6709\u4e00\u79cd\u6392\u5e8f\u7b97\u6cd5\u80fd\u591f\u540c\u65f6\u6ee1\u8db3\u6240\u6709\u8fd9\u4e9b\u6761\u4ef6\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u6570\u636e\u7684\u7279\u6027\u6765\u9009\u62e9\u5408\u9002\u7684\u6392\u5e8f\u7b97\u6cd5\u3002
    "},{"location":"chapter_sorting/summary/#11111-q-a","title":"11.11.1. \u00a0 Q & A","text":"

    \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\u5c0f\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

    "},{"location":"chapter_stack_and_queue/","title":"5. \u00a0 \u6808\u4e0e\u961f\u5217","text":""},{"location":"chapter_stack_and_queue/deque/","title":"5.3. \u00a0 \u53cc\u5411\u961f\u5217","text":"

    \u5bf9\u4e8e\u961f\u5217\uff0c\u6211\u4eec\u4ec5\u80fd\u5728\u5934\u90e8\u5220\u9664\u6216\u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\u3002\u7136\u800c\uff0c\u300c\u53cc\u5411\u961f\u5217 Deque\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

    Fig. \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\u4e0b\u8868\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

    \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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart deque.java
    /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\nDeque<Integer> deque = new LinkedList<>();\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/* \u8bbf\u95ee\u5143\u7d20 */\nint peekFirst = deque.peekFirst();  // \u961f\u9996\u5143\u7d20\nint peekLast = deque.peekLast();    // \u961f\u5c3e\u5143\u7d20\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/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.size();\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = deque.isEmpty();\n
    deque.cpp
    /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\ndeque<int> deque;\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/* \u8bbf\u95ee\u5143\u7d20 */\nint front = deque.front(); // \u961f\u9996\u5143\u7d20\nint back = deque.back();   // \u961f\u5c3e\u5143\u7d20\n/* \u5143\u7d20\u51fa\u961f */\ndeque.pop_front();  // \u961f\u9996\u5143\u7d20\u51fa\u961f\ndeque.pop_back();   // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.size();\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty = deque.empty();\n
    deque.py
    # \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217\ndeque: Deque[int] = collections.deque()\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# \u8bbf\u95ee\u5143\u7d20\nfront: int = deque[0]  # \u961f\u9996\u5143\u7d20\nrear: int = deque[-1]  # \u961f\u5c3e\u5143\u7d20\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# \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\nsize: int = len(deque)\n# \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = len(deque) == 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/* \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/* \u8bbf\u95ee\u5143\u7d20 */\nfront := deque.Front() // \u961f\u9996\u5143\u7d20\nrear := deque.Back()   // \u961f\u5c3e\u5143\u7d20\n/* \u5143\u7d20\u51fa\u961f */\ndeque.Remove(front)    // \u961f\u9996\u5143\u7d20\u51fa\u961f\ndeque.Remove(rear)     // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nsize := deque.Len()\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty := deque.Len() == 0\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/* \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/* \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/* \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/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nconst size = deque.length;\nconsole.log(\"\u53cc\u5411\u961f\u5217\u957f\u5ea6 size = \" + size);\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/* \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/* \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/* \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/* \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/* \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.c
    // C \u672a\u63d0\u4f9b\u5185\u7f6e\u53cc\u5411\u961f\u5217\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 LinkedList<int>();\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/* \u8bbf\u95ee\u5143\u7d20 */\nint peekFirst = deque.First.Value;  // \u961f\u9996\u5143\u7d20\nint peekLast = deque.Last.Value;    // \u961f\u5c3e\u5143\u7d20\n/* \u5143\u7d20\u51fa\u961f */\ndeque.RemoveFirst();  // \u961f\u9996\u5143\u7d20\u51fa\u961f\ndeque.RemoveLast();   // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.Count;\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = deque.Count == 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/* \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/* \u8bbf\u95ee\u5143\u7d20 */\nlet peekFirst = deque.first! // \u961f\u9996\u5143\u7d20\nlet peekLast = deque.last! // \u961f\u5c3e\u5143\u7d20\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/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = deque.count\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = deque.isEmpty\n
    deque.zig
    \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/* \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/* \u8bbf\u95ee\u5143\u7d20 */\nint peekFirst = deque.first; // \u961f\u9996\u5143\u7d20\nint peekLast = deque.last;   // \u961f\u5c3e\u5143\u7d20\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/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.length;\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = deque.isEmpty;W\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":"\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\u300c\u53cc\u5411\u94fe\u8868\u300d\u4f5c\u4e3a\u53cc\u5411\u961f\u5217\u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002

    \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()

    \u4ee5\u4e0b\u662f\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart linkedlist_deque.java
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\nint val; // \u8282\u70b9\u503c\nListNode next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\nListNode prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\nListNode(int val) {\nthis.val = val;\nprev = next = null;\n}\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\nprivate ListNode front, rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\nprivate int queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\npublic LinkedListDeque() {\nfront = rear = null;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic boolean isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u961f\u64cd\u4f5c */\nprivate void push(int num, boolean isFront) {\nListNode node = new ListNode(num);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (isEmpty())\nfront = rear = node;\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\nelse if (isFront) {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nfront.prev = node;\nnode.next = front;\nfront = 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\nrear.next = node;\nnode.prev = rear;\nrear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nqueSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n/* \u961f\u9996\u5165\u961f */\npublic void pushFirst(int num) {\npush(num, true);\n}\n/* \u961f\u5c3e\u5165\u961f */\npublic void pushLast(int num) {\npush(num, false);\n}\n/* \u51fa\u961f\u64cd\u4f5c */\nprivate Integer pop(boolean isFront) {\n// \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de null\nif (isEmpty())\nreturn null;\nint val;\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif (isFront) {\nval = front.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n// \u5220\u9664\u5934\u8282\u70b9\nListNode fNext = front.next;\nif (fNext != null) {\nfNext.prev = null;\nfront.next = null;\n}\nfront = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n// \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n} else {\nval = rear.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5c3e\u8282\u70b9\nListNode rPrev = rear.prev;\nif (rPrev != null) {\nrPrev.next = null;\nrear.prev = null;\n}\nrear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nqueSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val;\n}\n/* \u961f\u9996\u51fa\u961f */\npublic Integer popFirst() {\nreturn pop(true);\n}\n/* \u961f\u5c3e\u51fa\u961f */\npublic Integer popLast() {\nreturn pop(false);\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic Integer peekFirst() {\nreturn isEmpty() ? null : front.val;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npublic Integer peekLast() {\nreturn isEmpty() ? null : rear.val;\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\npublic int[] toArray() {\nListNode node = front;\nint[] res = new int[size()];\nfor (int i = 0; i < res.length; i++) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n
    linkedlist_deque.cpp
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nstruct DoublyListNode {\nint val;              // \u8282\u70b9\u503c\nDoublyListNode *next; // \u540e\u7ee7\u8282\u70b9\u6307\u9488\nDoublyListNode *prev; // \u524d\u9a71\u8282\u70b9\u6307\u9488\nDoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {\n}\n};\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\nprivate:\nDoublyListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\nint queSize = 0;              // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nLinkedListDeque() : front(nullptr), rear(nullptr) {\n}\n/* \u6790\u6784\u65b9\u6cd5 */\n~LinkedListDeque() {\n// \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\nDoublyListNode *pre, *cur = front;\nwhile (cur != nullptr) {\npre = cur;\ncur = cur->next;\ndelete pre;\n}\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u961f\u64cd\u4f5c */\nvoid push(int num, bool isFront) {\nDoublyListNode *node = new DoublyListNode(num);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (isEmpty())\nfront = rear = node;\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\nelse if (isFront) {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nfront->prev = node;\nnode->next = front;\nfront = 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\nrear->next = node;\nnode->prev = rear;\nrear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nqueSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(int num) {\npush(num, true);\n}\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(int num) {\npush(num, false);\n}\n/* \u51fa\u961f\u64cd\u4f5c */\nint pop(bool isFront) {\n// \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de -1\nif (isEmpty())\nreturn -1;\nint val;\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif (isFront) {\nval = front->val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n// \u5220\u9664\u5934\u8282\u70b9\nDoublyListNode *fNext = front->next;\nif (fNext != nullptr) {\nfNext->prev = nullptr;\nfront->next = nullptr;\ndelete front;\n}\nfront = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n// \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n} else {\nval = rear->val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5c3e\u8282\u70b9\nDoublyListNode *rPrev = rear->prev;\nif (rPrev != nullptr) {\nrPrev->next = nullptr;\nrear->prev = nullptr;\ndelete rear;\n}\nrear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nqueSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val;\n}\n/* \u961f\u9996\u51fa\u961f */\nint popFirst() {\nreturn pop(true);\n}\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast() {\nreturn pop(false);\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst() {\nreturn isEmpty() ? -1 : front->val;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast() {\nreturn isEmpty() ? -1 : rear->val;\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nvector<int> toVector() {\nDoublyListNode *node = front;\nvector<int> res(size());\nfor (int i = 0; i < res.size(); i++) {\nres[i] = node->val;\nnode = node->next;\n}\nreturn res;\n}\n};\n
    linkedlist_deque.py
    class ListNode:\n\"\"\"\u53cc\u5411\u94fe\u8868\u8282\u70b9\"\"\"\ndef __init__(self, val: int) -> None:\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.val: int = val\nself.next: ListNode | None = None  # \u540e\u7ee7\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\nself.prev: ListNode | None = None  # \u524d\u9a71\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\nclass LinkedListDeque:\n\"\"\"\u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\"\"\"\ndef __init__(self) -> None:\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.front: ListNode | None = None  # \u5934\u8282\u70b9 front\nself.rear: ListNode | None = None  # \u5c3e\u8282\u70b9 rear\nself.__size: int = 0  # \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\ndef size(self) -> int:\n\"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\"\"\"\nreturn self.__size\ndef is_empty(self) -> bool:\n\"\"\"\u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\nreturn self.size() == 0\ndef push(self, num: int, is_front: bool) -> None:\n\"\"\"\u5165\u961f\u64cd\u4f5c\"\"\"\nnode = ListNode(num)\n# \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif self.is_empty():\nself.front = self.rear = node\n# \u961f\u9996\u5165\u961f\u64cd\u4f5c\nelif is_front:\n# \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nself.front.prev = node\nnode.next = self.front\nself.front = node  # \u66f4\u65b0\u5934\u8282\u70b9\n# \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\nelse:\n# \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\nself.rear.next = node\nnode.prev = self.rear\nself.rear = node  # \u66f4\u65b0\u5c3e\u8282\u70b9\nself.__size += 1  # \u66f4\u65b0\u961f\u5217\u957f\u5ea6\ndef push_first(self, num: int) -> None:\n\"\"\"\u961f\u9996\u5165\u961f\"\"\"\nself.push(num, True)\ndef push_last(self, num: int) -> None:\n\"\"\"\u961f\u5c3e\u5165\u961f\"\"\"\nself.push(num, False)\ndef pop(self, is_front: bool) -> int:\n\"\"\"\u51fa\u961f\u64cd\u4f5c\"\"\"\n# \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de None\nif self.is_empty():\nreturn None\n# \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif is_front:\nval: int = self.front.val  # \u6682\u5b58\u5934\u8282\u70b9\u503c\n# \u5220\u9664\u5934\u8282\u70b9\nfnext: ListNode | None = self.front.next\nif fnext != None:\nfnext.prev = None\nself.front.next = None\nself.front = fnext  # \u66f4\u65b0\u5934\u8282\u70b9\n# \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\nelse:\nval: int = self.rear.val  # \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n# \u5220\u9664\u5c3e\u8282\u70b9\nrprev: ListNode | None = self.rear.prev\nif rprev != None:\nrprev.next = None\nself.rear.prev = None\nself.rear = rprev  # \u66f4\u65b0\u5c3e\u8282\u70b9\nself.__size -= 1  # \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val\ndef pop_first(self) -> int:\n\"\"\"\u961f\u9996\u51fa\u961f\"\"\"\nreturn self.pop(True)\ndef pop_last(self) -> int:\n\"\"\"\u961f\u5c3e\u51fa\u961f\"\"\"\nreturn self.pop(False)\ndef peek_first(self) -> int:\n\"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\nreturn None if self.is_empty() else self.front.val\ndef peek_last(self) -> int:\n\"\"\"\u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\"\"\"\nreturn None if self.is_empty() else self.rear.val\ndef to_array(self) -> list[int]:\n\"\"\"\u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370\"\"\"\nnode = self.front\nres = [0] * self.size()\nfor i in range(self.size()):\nres[i] = node.val\nnode = node.next\nreturn res\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\ndata *list.List\n}\n/* \u521d\u59cb\u5316\u53cc\u7aef\u961f\u5217 */\nfunc newLinkedListDeque() *linkedListDeque {\nreturn &linkedListDeque{\ndata: list.New(),\n}\n}\n/* \u961f\u9996\u5143\u7d20\u5165\u961f */\nfunc (s *linkedListDeque) pushFirst(value any) {\ns.data.PushFront(value)\n}\n/* \u961f\u5c3e\u5143\u7d20\u5165\u961f */\nfunc (s *linkedListDeque) pushLast(value any) {\ns.data.PushBack(value)\n}\n/* \u961f\u9996\u5143\u7d20\u51fa\u961f */\nfunc (s *linkedListDeque) popFirst() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Front()\ns.data.Remove(e)\nreturn e.Value\n}\n/* \u961f\u5c3e\u5143\u7d20\u51fa\u961f */\nfunc (s *linkedListDeque) popLast() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Back()\ns.data.Remove(e)\nreturn e.Value\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (s *linkedListDeque) peekFirst() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Front()\nreturn e.Value\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc (s *linkedListDeque) peekLast() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Back()\nreturn e.Value\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (s *linkedListDeque) size() int {\nreturn s.data.Len()\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListDeque) isEmpty() bool {\nreturn s.data.Len() == 0\n}\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListDeque) toList() *list.List {\nreturn s.data\n}\n
    linkedlist_deque.js
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\nprev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\nnext; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\nval; // \u8282\u70b9\u503c\nconstructor(val) {\nthis.val = val;\nthis.next = null;\nthis.prev = null;\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\nconstructor() {\nthis.#front = null;\nthis.#rear = null;\nthis.#queSize = 0;\n}\n/* \u961f\u5c3e\u5165\u961f\u64cd\u4f5c */\npushLast(val) {\nconst node = new ListNode(val);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (this.#queSize === 0) {\nthis.#front = node;\nthis.#rear = node;\n} else {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\nthis.#rear.next = node;\nnode.prev = this.#rear;\nthis.#rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nthis.#queSize++;\n}\n/* \u961f\u9996\u5165\u961f\u64cd\u4f5c */\npushFirst(val) {\nconst node = new ListNode(val);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (this.#queSize === 0) {\nthis.#front = node;\nthis.#rear = node;\n} else {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nthis.#front.prev = node;\nnode.next = this.#front;\nthis.#front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n}\nthis.#queSize++;\n}\n/* \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c */\npopLast() {\nif (this.#queSize === 0) {\nreturn null;\n}\nconst value = this.#rear.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5c3e\u8282\u70b9\nlet temp = this.#rear.prev;\nif (temp !== null) {\ntemp.next = null;\nthis.#rear.prev = null;\n}\nthis.#rear = temp; // \u66f4\u65b0\u5c3e\u8282\u70b9\nthis.#queSize--;\nreturn value;\n}\n/* \u961f\u9996\u51fa\u961f\u64cd\u4f5c */\npopFirst() {\nif (this.#queSize === 0) {\nreturn null;\n}\nconst value = this.#front.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5934\u8282\u70b9\nlet temp = this.#front.next;\nif (temp !== null) {\ntemp.prev = null;\nthis.#front.next = null;\n}\nthis.#front = temp; // \u66f4\u65b0\u5934\u8282\u70b9\nthis.#queSize--;\nreturn value;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npeekLast() {\nreturn this.#queSize === 0 ? null : this.#rear.val;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeekFirst() {\nreturn this.#queSize === 0 ? null : this.#front.val;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nsize() {\nreturn this.#queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty() {\nreturn this.#queSize === 0;\n}\n/* \u6253\u5370\u53cc\u5411\u961f\u5217 */\nprint() {\nconst arr = [];\nlet temp = this.#front;\nwhile (temp !== null) {\narr.push(temp.val);\ntemp = temp.next;\n}\nconsole.log('[' + arr.join(', ') + ']');\n}\n}\n
    linkedlist_deque.ts
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\nprev: ListNode; // \u524d\u9a71\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\nnext: ListNode; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\nval: number; // \u8282\u70b9\u503c\nconstructor(val: number) {\nthis.val = val;\nthis.next = null;\nthis.prev = null;\n}\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\nprivate front: ListNode; // \u5934\u8282\u70b9 front\nprivate rear: ListNode; // \u5c3e\u8282\u70b9 rear\nprivate queSize: number; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\nconstructor() {\nthis.front = null;\nthis.rear = null;\nthis.queSize = 0;\n}\n/* \u961f\u5c3e\u5165\u961f\u64cd\u4f5c */\npushLast(val: number): void {\nconst node: ListNode = new ListNode(val);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (this.queSize === 0) {\nthis.front = node;\nthis.rear = node;\n} else {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\nthis.rear.next = node;\nnode.prev = this.rear;\nthis.rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nthis.queSize++;\n}\n/* \u961f\u9996\u5165\u961f\u64cd\u4f5c */\npushFirst(val: number): void {\nconst node: ListNode = new ListNode(val);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (this.queSize === 0) {\nthis.front = node;\nthis.rear = node;\n} else {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nthis.front.prev = node;\nnode.next = this.front;\nthis.front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n}\nthis.queSize++;\n}\n/* \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c */\npopLast(): number {\nif (this.queSize === 0) {\nreturn null;\n}\nconst value: number = this.rear.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5c3e\u8282\u70b9\nlet temp: ListNode = this.rear.prev;\nif (temp !== null) {\ntemp.next = null;\nthis.rear.prev = null;\n}\nthis.rear = temp; // \u66f4\u65b0\u5c3e\u8282\u70b9\nthis.queSize--;\nreturn value;\n}\n/* \u961f\u9996\u51fa\u961f\u64cd\u4f5c */\npopFirst(): number {\nif (this.queSize === 0) {\nreturn null;\n}\nconst value: number = this.front.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5934\u8282\u70b9\nlet temp: ListNode = this.front.next;\nif (temp !== null) {\ntemp.prev = null;\nthis.front.next = null;\n}\nthis.front = temp; // \u66f4\u65b0\u5934\u8282\u70b9\nthis.queSize--;\nreturn value;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npeekLast(): number {\nreturn this.queSize === 0 ? null : this.rear.val;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeekFirst(): number {\nreturn this.queSize === 0 ? null : this.front.val;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nsize(): number {\nreturn this.queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty(): boolean {\nreturn this.queSize === 0;\n}\n/* \u6253\u5370\u53cc\u5411\u961f\u5217 */\nprint(): void {\nconst arr: number[] = [];\nlet temp: ListNode = this.front;\nwhile (temp !== null) {\narr.push(temp.val);\ntemp = temp.next;\n}\nconsole.log('[' + arr.join(', ') + ']');\n}\n}\n
    linkedlist_deque.c
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nstruct doublyListNode {\nint val;                     // \u8282\u70b9\u503c\nstruct doublyListNode *next; // \u540e\u7ee7\u8282\u70b9\nstruct doublyListNode *prev; // \u524d\u9a71\u8282\u70b9\n};\ntypedef struct doublyListNode doublyListNode;\n/* \u6784\u9020\u51fd\u6570 */\ndoublyListNode *newDoublyListNode(int num) {\ndoublyListNode *new = (doublyListNode *)malloc(sizeof(doublyListNode));\nnew->val = num;\nnew->next = NULL;\nnew->prev = NULL;\nreturn new;\n}\n/* \u6790\u6784\u51fd\u6570 */\nvoid delDoublyListNode(doublyListNode *node) {\nfree(node);\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nstruct linkedListDeque {\ndoublyListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\nint queSize;                  // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n};\ntypedef struct linkedListDeque linkedListDeque;\n/* \u6784\u9020\u51fd\u6570 */\nlinkedListDeque *newLinkedListDeque() {\nlinkedListDeque *deque = (linkedListDeque *)malloc(sizeof(linkedListDeque));\ndeque->front = NULL;\ndeque->rear = NULL;\ndeque->queSize = 0;\nreturn deque;\n}\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListdeque(linkedListDeque *deque) {\n// \u91ca\u653e\u6240\u6709\u8282\u70b9\nfor (int i = 0; i < deque->queSize && deque->front != NULL; i++) {\ndoublyListNode *tmp = deque->front;\ndeque->front = deque->front->next;\nfree(tmp);\n}\n// \u91ca\u653e deque \u7ed3\u6784\u4f53\nfree(deque);\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(linkedListDeque *deque) {\nreturn deque->queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(linkedListDeque *deque) {\nreturn (size(deque) == 0);\n}\n/* \u5165\u961f */\nvoid push(linkedListDeque *deque, int num, bool isFront) {\ndoublyListNode *node = newDoublyListNode(num);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411node\nif (empty(deque)) {\ndeque->front = deque->rear = node;\n}\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\nelse if (isFront) {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\ndeque->front->prev = node;\nnode->next = deque->front;\ndeque->front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n}\n// \u5bf9\u5c3e\u5165\u961f\u64cd\u4f5c\nelse {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\ndeque->rear->next = node;\nnode->prev = deque->rear;\ndeque->rear = node;\n}\ndeque->queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(linkedListDeque *deque, int num) {\npush(deque, num, true);\n}\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(linkedListDeque *deque, int num) {\npush(deque, num, false);\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst(linkedListDeque *deque) {\nassert(size(deque) && deque->front);\nreturn deque->front->val;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast(linkedListDeque *deque) {\nassert(size(deque) && deque->rear);\nreturn deque->rear->val;\n}\n/* \u51fa\u961f */\nint pop(linkedListDeque *deque, bool isFront) {\nif (empty(deque))\nreturn -1;\nint val;\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif (isFront) {\nval = peekFirst(deque); // \u6682\u5b58\u5934\u8282\u70b9\u503c\ndoublyListNode *fNext = deque->front->next;\nif (fNext) {\nfNext->prev = NULL;\ndeque->front->next = NULL;\ndelDoublyListNode(deque->front);\n}\ndeque->front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n}\n// \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\nelse {\nval = peekLast(deque); // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\ndoublyListNode *rPrev = deque->rear->prev;\nif (rPrev) {\nrPrev->next = NULL;\ndeque->rear->prev = NULL;\ndelDoublyListNode(deque->rear);\n}\ndeque->rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\ndeque->queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val;\n}\n/* \u961f\u9996\u51fa\u961f */\nint popFirst(linkedListDeque *deque) {\nreturn pop(deque, true);\n}\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast(linkedListDeque *deque) {\nreturn pop(deque, false);\n}\n/* \u6253\u5370\u961f\u5217 */\nvoid printLinkedListDeque(linkedListDeque *deque) {\nint arr[deque->queSize];\n// \u62f7\u8d1d\u94fe\u8868\u4e2d\u7684\u6570\u636e\u5230\u6570\u7ec4\nint i;\ndoublyListNode *node;\nfor (i = 0, node = deque->front; i < deque->queSize; i++) {\narr[i] = node->val;\nnode = node->next;\n}\nprintArray(arr, deque->queSize);\n}\n
    linkedlist_deque.cs
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\npublic int val;       // \u8282\u70b9\u503c\npublic ListNode? next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\npublic ListNode? prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\npublic ListNode(int val) {\nthis.val = val;\nprev = null;\nnext = null;\n}\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\nprivate ListNode? front, rear; // \u5934\u8282\u70b9 front, \u5c3e\u8282\u70b9 rear\nprivate int queSize = 0;      // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\npublic LinkedListDeque() {\nfront = null;\nrear = null;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic bool isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u961f\u64cd\u4f5c */\nprivate void push(int num, bool isFront) {\nListNode node = new ListNode(num);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (isEmpty()) {\nfront = node;\nrear = node;\n}\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\nelse if (isFront) {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nfront.prev = node;\nnode.next = front;\nfront = node; // \u66f4\u65b0\u5934\u8282\u70b9                           \n}\n// \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\nelse {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\nrear.next = node;\nnode.prev = rear;\nrear = node;  // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nqueSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n/* \u961f\u9996\u5165\u961f */\npublic void pushFirst(int num) {\npush(num, true);\n}\n/* \u961f\u5c3e\u5165\u961f */\npublic void pushLast(int num) {\npush(num, false);\n}\n/* \u51fa\u961f\u64cd\u4f5c */\nprivate int? pop(bool isFront) {\n// \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de null\nif (isEmpty()) {\nreturn null;\n}\nint val;\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif (isFront) {\nval = front.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n// \u5220\u9664\u5934\u8282\u70b9\nListNode fNext = front.next;\nif (fNext != null) {\nfNext.prev = null;\nfront.next = null;\n}\nfront = fNext;   // \u66f4\u65b0\u5934\u8282\u70b9\n}\n// \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\nelse {\nval = rear.val;  // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5c3e\u8282\u70b9\nListNode rPrev = rear.prev;\nif (rPrev != null) {\nrPrev.next = null;\nrear.prev = null;\n}\nrear = rPrev;    // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nqueSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val;\n}\n/* \u961f\u9996\u51fa\u961f */\npublic int? popFirst() {\nreturn pop(true);\n}\n/* \u961f\u5c3e\u51fa\u961f */\npublic int? popLast() {\nreturn pop(false);\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int? peekFirst() {\nreturn isEmpty() ? null : front.val;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npublic int? peekLast() {\nreturn isEmpty() ? null : rear.val;\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\npublic int[] toArray() {\nListNode node = front;\nint[] res = new int[size()];\nfor (int i = 0; i < res.Length; i++) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n
    linkedlist_deque.swift
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\nvar val: Int // \u8282\u70b9\u503c\nvar next: ListNode? // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\nvar prev: ListNode? // \u524d\u9a71\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\ninit(val: Int) {\nself.val = val\n}\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\nprivate var front: ListNode? // \u5934\u8282\u70b9 front\nprivate var rear: ListNode? // \u5c3e\u8282\u70b9 rear\nprivate var queSize: Int // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\ninit() {\nqueSize = 0\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nfunc size() -> Int {\nqueSize\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc isEmpty() -> Bool {\nsize() == 0\n}\n/* \u5165\u961f\u64cd\u4f5c */\nprivate func push(num: Int, isFront: Bool) {\nlet node = ListNode(val: num)\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif isEmpty() {\nfront = node\nrear = node\n}\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\nelse if isFront {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nfront?.prev = node\nnode.next = front\nfront = node // \u66f4\u65b0\u5934\u8282\u70b9\n}\n// \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\nelse {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\nrear?.next = node\nnode.prev = rear\nrear = node // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nqueSize += 1 // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n/* \u961f\u9996\u5165\u961f */\nfunc pushFirst(num: Int) {\npush(num: num, isFront: true)\n}\n/* \u961f\u5c3e\u5165\u961f */\nfunc pushLast(num: Int) {\npush(num: num, isFront: false)\n}\n/* \u51fa\u961f\u64cd\u4f5c */\nprivate func pop(isFront: Bool) -> Int {\nif isEmpty() {\nfatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n}\nlet val: Int\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif isFront {\nval = front!.val // \u6682\u5b58\u5934\u8282\u70b9\u503c\n// \u5220\u9664\u5934\u8282\u70b9\nlet fNext = front?.next\nif fNext != nil {\nfNext?.prev = nil\nfront?.next = nil\n}\nfront = fNext // \u66f4\u65b0\u5934\u8282\u70b9\n}\n// \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\nelse {\nval = rear!.val // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5c3e\u8282\u70b9\nlet rPrev = rear?.prev\nif rPrev != nil {\nrPrev?.next = nil\nrear?.prev = nil\n}\nrear = rPrev // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nqueSize -= 1 // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val\n}\n/* \u961f\u9996\u51fa\u961f */\nfunc popFirst() -> Int {\npop(isFront: true)\n}\n/* \u961f\u5c3e\u51fa\u961f */\nfunc popLast() -> Int {\npop(isFront: false)\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc peekFirst() -> Int? {\nisEmpty() ? nil : front?.val\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc peekLast() -> Int? {\nisEmpty() ? nil : rear?.val\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nfunc toArray() -> [Int] {\nvar node = front\nvar res = Array(repeating: 0, count: size())\nfor i in res.indices {\nres[i] = node!.val\nnode = node?.next\n}\nreturn res\n}\n}\n
    linkedlist_deque.zig
    // \u53cc\u5411\u94fe\u8868\u8282\u70b9\nfn ListNode(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nval: T = undefined,     // \u8282\u70b9\u503c\nnext: ?*Self = null,    // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\nprev: ?*Self = null,    // \u524d\u9a71\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\n// Initialize a list node with specific value\npub fn init(self: *Self, x: i32) void {\nself.val = x;\nself.next = null;\nself.prev = null;\n}\n};\n}\n// \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\nfn LinkedListDeque(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nfront: ?*ListNode(T) = null,                    // \u5934\u8282\u70b9 front\nrear: ?*ListNode(T) = null,                     // \u5c3e\u8282\u70b9 rear\nque_size: usize = 0,                             // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\nmem_arena: ?std.heap.ArenaAllocator = null,\nmem_allocator: std.mem.Allocator = undefined,   // \u5185\u5b58\u5206\u914d\u5668\n// \u6784\u9020\u65b9\u6cd5\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u961f\u5217\uff09\npub fn init(self: *Self, allocator: std.mem.Allocator) !void {\nif (self.mem_arena == null) {\nself.mem_arena = std.heap.ArenaAllocator.init(allocator);\nself.mem_allocator = self.mem_arena.?.allocator();\n}\nself.front = null;\nself.rear = null;\nself.que_size = 0;\n}\n// \u6790\u6784\u65b9\u6cd5\uff08\u91ca\u653e\u5185\u5b58\uff09\npub fn deinit(self: *Self) void {\nif (self.mem_arena == null) return;\nself.mem_arena.?.deinit();\n}\n// \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\npub fn size(self: *Self) usize {\nreturn self.que_size;\n}\n// \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\npub fn isEmpty(self: *Self) bool {\nreturn self.size() == 0;\n}\n// \u5165\u961f\u64cd\u4f5c\npub fn push(self: *Self, num: T, is_front: bool) !void {\nvar node = try self.mem_allocator.create(ListNode(T));\nnode.init(num);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (self.isEmpty()) {\nself.front = node;\nself.rear = node;\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\n} else if (is_front) {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nself.front.?.prev = node;\nnode.next = self.front;\nself.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\nself.rear.?.next = node;\nnode.prev = self.rear;\nself.rear = node;   // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nself.que_size += 1;      // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n} // \u961f\u9996\u5165\u961f\npub fn pushFirst(self: *Self, num: T) !void {\ntry self.push(num, true);\n} // \u961f\u5c3e\u5165\u961f\npub fn pushLast(self: *Self, num: T) !void {\ntry self.push(num, false);\n} // \u51fa\u961f\u64cd\u4f5c\npub fn pop(self: *Self, is_front: bool) T {\nif (self.isEmpty()) @panic(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\nvar val: T = undefined;\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif (is_front) {\nval = self.front.?.val;     // \u6682\u5b58\u5934\u8282\u70b9\u503c\n// \u5220\u9664\u5934\u8282\u70b9\nvar fNext = self.front.?.next;\nif (fNext != null) {\nfNext.?.prev = null;\nself.front.?.next = null;\n}\nself.front = fNext;         // \u66f4\u65b0\u5934\u8282\u70b9\n// \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n} else {\nval = self.rear.?.val;      // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5c3e\u8282\u70b9\nvar rPrev = self.rear.?.prev;\nif (rPrev != null) {\nrPrev.?.next = null;\nself.rear.?.prev = null;\n}\nself.rear = rPrev;          // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nself.que_size -= 1;              // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val;\n} // \u961f\u9996\u51fa\u961f\npub fn popFirst(self: *Self) T {\nreturn self.pop(true);\n} // \u961f\u5c3e\u51fa\u961f\npub fn popLast(self: *Self) T {\nreturn self.pop(false);\n} // \u8bbf\u95ee\u961f\u9996\u5143\u7d20\npub fn peekFirst(self: *Self) T {\nif (self.isEmpty()) @panic(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\nreturn self.front.?.val;\n}  // \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\npub fn peekLast(self: *Self) T {\nif (self.isEmpty()) @panic(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\nreturn self.rear.?.val;\n}\n// \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370\npub fn toArray(self: *Self) ![]T {\nvar node = self.front;\nvar res = try self.mem_allocator.alloc(T, self.size());\nstd.mem.set(T, res, @as(T, 0));\nvar i: usize = 0;\nwhile (i < res.len) : (i += 1) {\nres[i] = node.?.val;\nnode = node.?.next;\n}\nreturn res;\n}\n};\n}\n
    linkedlist_deque.dart
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\nint val; // \u8282\u70b9\u503c\nListNode? next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\nListNode? prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\nListNode(this.val, {this.next, this.prev});\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u5bf9\u5217 */\nclass LinkedListDeque {\nlate ListNode? _front; // \u5934\u8282\u70b9 _front\nlate ListNode? _rear; // \u5c3e\u8282\u70b9 _rear\nint _queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\nLinkedListDeque() {\nthis._front = null;\nthis._rear = null;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u957f\u5ea6 */\nint size() {\nreturn this._queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u961f\u64cd\u4f5c */\nvoid push(int num, bool isFront) {\nfinal ListNode node = ListNode(num);\nif (isEmpty()) {\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 _front\uff0c_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;\nnode.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;\nnode.prev = _rear;\n_rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\n_queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(int num) {\npush(num, true);\n}\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(int num) {\npush(num, false);\n}\n/* \u51fa\u961f\u64cd\u4f5c */\nint? pop(bool isFront) {\n// \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de null\nif (isEmpty()) {\nreturn null;\n}\nfinal int val;\nif (isFront) {\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nval = _front!.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n// \u5220\u9664\u5934\u8282\u70b9\nListNode? fNext = _front!.next;\nif (fNext != null) {\nfNext.prev = null;\n_front!.next = null;\n}\n_front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n} else {\n// \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\nval = _rear!.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5c3e\u8282\u70b9\nListNode? rPrev = _rear!.prev;\nif (rPrev != null) {\nrPrev.next = null;\n_rear!.prev = null;\n}\n_rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\n_queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val;\n}\n/* \u961f\u9996\u51fa\u961f */\nint? popFirst() {\nreturn pop(true);\n}\n/* \u961f\u5c3e\u51fa\u961f */\nint? popLast() {\nreturn pop(false);\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint? peekFirst() {\nreturn _front?.val;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint? peekLast() {\nreturn _rear?.val;\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nList<int> toArray() {\nListNode? node = _front;\nfinal List<int> res = [];\nfor (int i = 0; i < _queSize; i++) {\nres.add(node!.val);\nnode = node.next;\n}\nreturn res;\n}\n}\n
    "},{"location":"chapter_stack_and_queue/deque/#_2","title":"\u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"

    \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\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\u3002

    ArrayDequepushLast()pushFirst()popLast()popFirst()

    \u4ee5\u4e0b\u662f\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart array_deque.java
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\nprivate int[] nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate int queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n/* \u6784\u9020\u65b9\u6cd5 */\npublic ArrayDeque(int capacity) {\nthis.nums = new int[capacity];\nfront = queSize = 0;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\npublic int capacity() {\nreturn nums.length;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic boolean isEmpty() {\nreturn queSize == 0;\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nprivate 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\nreturn (i + capacity()) % capacity();\n}\n/* \u961f\u9996\u5165\u961f */\npublic void pushFirst(int num) {\nif (queSize == capacity()) {\nSystem.out.println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\nreturn;\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\nfront = index(front - 1);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nnums[front] = num;\nqueSize++;\n}\n/* \u961f\u5c3e\u5165\u961f */\npublic void pushLast(int num) {\nif (queSize == capacity()) {\nSystem.out.println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nint rear = index(front + queSize);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num;\nqueSize++;\n}\n/* \u961f\u9996\u51fa\u961f */\npublic int popFirst() {\nint num = peekFirst();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfront = index(front + 1);\nqueSize--;\nreturn num;\n}\n/* \u961f\u5c3e\u51fa\u961f */\npublic int popLast() {\nint num = peekLast();\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int peekFirst() {\nif (isEmpty())\nthrow new IndexOutOfBoundsException();\nreturn nums[front];\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npublic int peekLast() {\nif (isEmpty())\nthrow new IndexOutOfBoundsException();\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nint last = index(front + queSize - 1);\nreturn nums[last];\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\npublic int[] toArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nint[] res = new int[queSize];\nfor (int i = 0, j = front; i < queSize; i++, j++) {\nres[i] = nums[index(j)];\n}\nreturn res;\n}\n}\n
    array_deque.cpp
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\nprivate:\nvector<int> nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nint front;        // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nint queSize;      // \u53cc\u5411\u961f\u5217\u957f\u5ea6\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nArrayDeque(int capacity) {\nnums.resize(capacity);\nfront = queSize = 0;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity() {\nreturn nums.size();\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty() {\nreturn queSize == 0;\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nint 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\nreturn (i + capacity()) % capacity();\n}\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(int num) {\nif (queSize == capacity()) {\ncout << \"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\" << endl;\nreturn;\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\nfront = index(front - 1);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nnums[front] = num;\nqueSize++;\n}\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(int num) {\nif (queSize == capacity()) {\ncout << \"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\" << endl;\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nint rear = index(front + queSize);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num;\nqueSize++;\n}\n/* \u961f\u9996\u51fa\u961f */\nint popFirst() {\nint num = peekFirst();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfront = index(front + 1);\nqueSize--;\nreturn num;\n}\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast() {\nint num = peekLast();\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst() {\nif (isEmpty())\nthrow out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\nreturn nums[front];\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast() {\nif (isEmpty())\nthrow out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nint last = index(front + queSize - 1);\nreturn nums[last];\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nvector<int> toVector() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvector<int> res(queSize);\nfor (int i = 0, j = front; i < queSize; i++, j++) {\nres[i] = nums[index(j)];\n}\nreturn res;\n}\n};\n
    array_deque.py
    class ArrayDeque:\n\"\"\"\u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\"\"\"\ndef __init__(self, capacity: int) -> None:\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.__nums: list[int] = [0] * capacity\nself.__front: int = 0\nself.__size: int = 0\ndef capacity(self) -> int:\n\"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf\"\"\"\nreturn len(self.__nums)\ndef size(self) -> int:\n\"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\"\"\"\nreturn self.__size\ndef is_empty(self) -> bool:\n\"\"\"\u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\nreturn self.__size == 0\ndef 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\nreturn (i + self.capacity()) % self.capacity()\ndef push_first(self, num: int) -> None:\n\"\"\"\u961f\u9996\u5165\u961f\"\"\"\nif self.__size == self.capacity():\nprint(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\nreturn\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\nself.__front = self.index(self.__front - 1)\n# \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nself.__nums[self.__front] = num\nself.__size += 1\ndef push_last(self, num: int) -> None:\n\"\"\"\u961f\u5c3e\u5165\u961f\"\"\"\nif self.__size == self.capacity():\nprint(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\nreturn\n# \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nrear = self.index(self.__front + self.__size)\n# \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nself.__nums[rear] = num\nself.__size += 1\ndef pop_first(self) -> int:\n\"\"\"\u961f\u9996\u51fa\u961f\"\"\"\nnum = self.peek_first()\n# \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nself.__front = self.index(self.__front + 1)\nself.__size -= 1\nreturn num\ndef pop_last(self) -> int:\n\"\"\"\u961f\u5c3e\u51fa\u961f\"\"\"\nnum = self.peek_last()\nself.__size -= 1\nreturn num\ndef peek_first(self) -> int:\n\"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\nif self.is_empty():\nraise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\nreturn self.__nums[self.__front]\ndef peek_last(self) -> int:\n\"\"\"\u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\"\"\"\nif self.is_empty():\nraise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n# \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nlast = self.index(self.__front + self.__size - 1)\nreturn self.__nums[last]\ndef 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\nres = []\nfor i in range(self.__size):\nres.append(self.__nums[self.index(self.__front + i)])\nreturn res\n
    array_deque.go
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntype arrayDeque struct {\nnums        []int // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nfront       int   // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nqueSize     int   // \u53cc\u5411\u961f\u5217\u957f\u5ea6\nqueCapacity int   // \u961f\u5217\u5bb9\u91cf\uff08\u5373\u6700\u5927\u5bb9\u7eb3\u5143\u7d20\u6570\u91cf\uff09\n}\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newArrayDeque(queCapacity int) *arrayDeque {\nreturn &arrayDeque{\nnums:        make([]int, queCapacity),\nqueCapacity: queCapacity,\nfront:       0,\nqueSize:     0,\n}\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (q *arrayDeque) size() int {\nreturn q.queSize\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (q *arrayDeque) isEmpty() bool {\nreturn q.queSize == 0\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\nreturn (i + q.queCapacity) % q.queCapacity\n}\n/* \u961f\u9996\u5165\u961f */\nfunc (q *arrayDeque) pushFirst(num int) {\nif q.queSize == q.queCapacity {\nfmt.Println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\nreturn\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\nq.front = q.index(q.front - 1)\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nq.nums[q.front] = num\nq.queSize++\n}\n/* \u961f\u5c3e\u5165\u961f */\nfunc (q *arrayDeque) pushLast(num int) {\nif q.queSize == q.queCapacity {\nfmt.Println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\nreturn\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nrear := q.index(q.front + q.queSize)\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nq.nums[rear] = num\nq.queSize++\n}\n/* \u961f\u9996\u51fa\u961f */\nfunc (q *arrayDeque) popFirst() any {\nnum := q.peekFirst()\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nq.front = q.index(q.front + 1)\nq.queSize--\nreturn num\n}\n/* \u961f\u5c3e\u51fa\u961f */\nfunc (q *arrayDeque) popLast() any {\nnum := q.peekLast()\nq.queSize--\nreturn num\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (q *arrayDeque) peekFirst() any {\nif q.isEmpty() {\nreturn nil\n}\nreturn q.nums[q.front]\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc (q *arrayDeque) peekLast() any {\nif q.isEmpty() {\nreturn nil\n}\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nlast := q.index(q.front + q.queSize - 1)\nreturn q.nums[last]\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\nres := make([]int, q.queSize)\nfor i, j := 0, q.front; i < q.queSize; i++ {\nres[i] = q.nums[q.index(j)]\nj++\n}\nreturn res\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/* \u6784\u9020\u65b9\u6cd5 */\nconstructor(capacity) {\nthis.#nums = new Array(capacity);\nthis.#front = 0;\nthis.#queSize = 0;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\ncapacity() {\nreturn this.#nums.length;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nsize() {\nreturn this.#queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty() {\nreturn this.#queSize === 0;\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nindex(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\nreturn (i + this.capacity()) % this.capacity();\n}\n/* \u961f\u9996\u5165\u961f */\npushFirst(num) {\nif (this.#queSize === this.capacity()) {\nconsole.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\nreturn;\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\nthis.#front = this.index(this.#front - 1);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nthis.#nums[this.#front] = num;\nthis.#queSize++;\n}\n/* \u961f\u5c3e\u5165\u961f */\npushLast(num) {\nif (this.#queSize === this.capacity()) {\nconsole.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nconst rear = this.index(this.#front + this.#queSize);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nthis.#nums[rear] = num;\nthis.#queSize++;\n}\n/* \u961f\u9996\u51fa\u961f */\npopFirst() {\nconst num = this.peekFirst();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nthis.#front = this.index(this.#front + 1);\nthis.#queSize--;\nreturn num;\n}\n/* \u961f\u5c3e\u51fa\u961f */\npopLast() {\nconst num = this.peekLast();\nthis.#queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeekFirst() {\nif (this.isEmpty()) throw new Error('The Deque Is Empty.');\nreturn this.#nums[this.#front];\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npeekLast() {\nif (this.isEmpty()) throw new Error('The Deque Is Empty.');\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nconst last = this.index(this.#front + this.#queSize - 1);\nreturn this.#nums[last];\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\ntoArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nconst res = [];\nfor (let i = 0, j = this.#front; i < this.#queSize; i++, j++) {\nres[i] = this.#nums[this.index(j)];\n}\nreturn res;\n}\n}\n
    array_deque.ts
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\nprivate nums: number[]; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate front: number; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate queSize: number; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor(capacity: number) {\nthis.nums = new Array(capacity);\nthis.front = 0;\nthis.queSize = 0;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\ncapacity(): number {\nreturn this.nums.length;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nsize(): number {\nreturn this.queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty(): boolean {\nreturn this.queSize === 0;\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nindex(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\nreturn (i + this.capacity()) % this.capacity();\n}\n/* \u961f\u9996\u5165\u961f */\npushFirst(num: number): void {\nif (this.queSize === this.capacity()) {\nconsole.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\nreturn;\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\nthis.front = this.index(this.front - 1);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nthis.nums[this.front] = num;\nthis.queSize++;\n}\n/* \u961f\u5c3e\u5165\u961f */\npushLast(num: number): void {\nif (this.queSize === this.capacity()) {\nconsole.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nconst rear: number = this.index(this.front + this.queSize);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nthis.nums[rear] = num;\nthis.queSize++;\n}\n/* \u961f\u9996\u51fa\u961f */\npopFirst(): number {\nconst num: number = this.peekFirst();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nthis.front = this.index(this.front + 1);\nthis.queSize--;\nreturn num;\n}\n/* \u961f\u5c3e\u51fa\u961f */\npopLast(): number {\nconst num: number = this.peekLast();\nthis.queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeekFirst(): number {\nif (this.isEmpty()) throw new Error('The Deque Is Empty.');\nreturn this.nums[this.front];\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npeekLast(): number {\nif (this.isEmpty()) throw new Error('The Deque Is Empty.');\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nconst last = this.index(this.front + this.queSize - 1);\nreturn this.nums[last];\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\ntoArray(): number[] {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nconst res: number[] = [];\nfor (let i = 0, j = this.front; i < this.queSize; i++, j++) {\nres[i] = this.nums[this.index(j)];\n}\nreturn res;\n}\n}\n
    array_deque.c
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nstruct arrayDeque {\nint *nums;       // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nint front;       // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nint queSize;     // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\nint queCapacity; // \u961f\u5217\u5bb9\u91cf\n};\ntypedef struct arrayDeque arrayDeque;\n/* \u6784\u9020\u51fd\u6570 */\narrayDeque *newArrayDeque(int capacity) {\narrayDeque *deque = (arrayDeque *)malloc(sizeof(arrayDeque));\n// \u521d\u59cb\u5316\u6570\u7ec4\ndeque->queCapacity = capacity;\ndeque->nums = (int *)malloc(sizeof(int) * deque->queCapacity);\ndeque->front = deque->queSize = 0;\nreturn deque;\n}\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayDeque(arrayDeque *deque) {\nfree(deque->nums);\ndeque->queCapacity = 0;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity(arrayDeque *deque) {\nreturn deque->queCapacity;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size(arrayDeque *deque) {\nreturn deque->queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(arrayDeque *deque) {\nreturn deque->queSize == 0;\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\nreturn ((i + capacity(deque)) % capacity(deque));\n}\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(arrayDeque *deque, int num) {\nif (deque->queSize == capacity(deque)) {\nprintf(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\\r\\n\");\nreturn;\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\ndeque->front = dequeIndex(deque, deque->front - 1);\n// \u5c06 num \u6dfb\u52a0\u5230\u961f\u9996\ndeque->nums[deque->front] = num;\ndeque->queSize++;\n}\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(arrayDeque *deque, int num) {\nif (deque->queSize == capacity(deque)) {\nprintf(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\\r\\n\");\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nint rear = dequeIndex(deque, deque->front + deque->queSize);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque->nums[rear] = num;\ndeque->queSize++;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst(arrayDeque *deque) {\n// \u8bbf\u95ee\u5f02\u5e38\uff1a\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\nassert(empty(deque) == 0);\nreturn deque->nums[deque->front];\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast(arrayDeque *deque) {\n// \u8bbf\u95ee\u5f02\u5e38\uff1a\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\nassert(empty(deque) == 0);\nint last = dequeIndex(deque, deque->front + deque->queSize - 1);\nreturn deque->nums[last];\n}\n/* \u961f\u9996\u51fa\u961f */\nint popFirst(arrayDeque *deque) {\nint num = peekFirst(deque);\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\ndeque->front = dequeIndex(deque, deque->front + 1);\ndeque->queSize--;\nreturn num;\n}\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast(arrayDeque *deque) {\nint num = peekLast(deque);\ndeque->queSize--;\nreturn num;\n}\n/* \u6253\u5370\u961f\u5217 */\nvoid printArrayDeque(arrayDeque *deque) {\nint arr[deque->queSize];\n// \u62f7\u8d1d\nfor (int i = 0, j = deque->front; i < deque->queSize; i++, j++) {\narr[i] = deque->nums[j % deque->queCapacity];\n}\nprintArray(arr, deque->queSize);\n}\n
    array_deque.cs
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\nprivate readonly int[] nums;  // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate int front;   // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate int queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n/* \u6784\u9020\u65b9\u6cd5 */\npublic ArrayDeque(int capacity) {\nthis.nums = new int[capacity];\nfront = queSize = 0;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\npublic int capacity() {\nreturn nums.Length;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic bool isEmpty() {\nreturn queSize == 0;\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nprivate 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\nreturn (i + capacity()) % capacity();\n}\n/* \u961f\u9996\u5165\u961f */\npublic void pushFirst(int num) {\nif (queSize == capacity()) {\nConsole.WriteLine(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\nreturn;\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\nfront = index(front - 1);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nnums[front] = num;\nqueSize++;\n}\n/* \u961f\u5c3e\u5165\u961f */\npublic void pushLast(int num) {\nif (queSize == capacity()) {\nConsole.WriteLine(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nint rear = index(front + queSize);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num;\nqueSize++;\n}\n/* \u961f\u9996\u51fa\u961f */\npublic int popFirst() {\nint num = peekFirst();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfront = index(front + 1);\nqueSize--;\nreturn num;\n}\n/* \u961f\u5c3e\u51fa\u961f */\npublic int popLast() {\nint num = peekLast();\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int peekFirst() {\nif (isEmpty()) {\nthrow new InvalidOperationException();\n}\nreturn nums[front];\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npublic int peekLast() {\nif (isEmpty()) {\nthrow new InvalidOperationException();\n}\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nint last = index(front + queSize - 1);\nreturn nums[last];\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\npublic int[] toArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nint[] res = new int[queSize];\nfor (int i = 0, j = front; i < queSize; i++, j++) {\nres[i] = nums[index(j)];\n}\nreturn res;\n}\n}\n
    array_deque.swift
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\nprivate var nums: [Int] // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate var front: Int // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate var queSize: Int // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n/* \u6784\u9020\u65b9\u6cd5 */\ninit(capacity: Int) {\nnums = Array(repeating: 0, count: capacity)\nfront = 0\nqueSize = 0\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\nfunc capacity() -> Int {\nnums.count\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nfunc size() -> Int {\nqueSize\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc isEmpty() -> Bool {\nsize() == 0\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nprivate 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/* \u961f\u9996\u5165\u961f */\nfunc pushFirst(num: Int) {\nif size() == capacity() {\nprint(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\nreturn\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\nfront = index(i: front - 1)\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nnums[front] = num\nqueSize += 1\n}\n/* \u961f\u5c3e\u5165\u961f */\nfunc pushLast(num: Int) {\nif size() == capacity() {\nprint(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\nreturn\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nlet rear = index(i: front + size())\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num\nqueSize += 1\n}\n/* \u961f\u9996\u51fa\u961f */\nfunc popFirst() -> Int {\nlet num = peekFirst()\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfront = index(i: front + 1)\nqueSize -= 1\nreturn num\n}\n/* \u961f\u5c3e\u51fa\u961f */\nfunc popLast() -> Int {\nlet num = peekLast()\nqueSize -= 1\nreturn num\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc peekFirst() -> Int {\nif isEmpty() {\nfatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n}\nreturn nums[front]\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc peekLast() -> Int {\nif isEmpty() {\nfatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n}\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nlet last = index(i: front + size() - 1)\nreturn nums[last]\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nfunc toArray() -> [Int] {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvar res = Array(repeating: 0, count: size())\nfor (i, j) in sequence(first: (0, front), next: { $0 < self.size() - 1 ? ($0 + 1, $1 + 1) : nil }) {\nres[i] = nums[index(i: j)]\n}\nreturn res\n}\n}\n
    array_deque.zig
    [class]{ArrayDeque}-[func]{}\n
    array_deque.dart
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\nlate List<int> _nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nlate int _front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nlate int _queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n/* \u6784\u9020\u65b9\u6cd5 */\nArrayDeque(int capacity) {\nthis._nums = List.filled(capacity, 0);\nthis._front = this._queSize = 0;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity() {\nreturn _nums.length;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size() {\nreturn _queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty() {\nreturn _queSize == 0;\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nint 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\nreturn (i + capacity()) % capacity();\n}\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(int num) {\nif (_queSize == capacity()) {\nthrow 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/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(int num) {\nif (_queSize == capacity()) {\nthrow Exception(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nint rear = index(_front + _queSize);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n_nums[rear] = num;\n_queSize++;\n}\n/* \u961f\u9996\u51fa\u961f */\nint popFirst() {\nint num = peekFirst();\n// \u961f\u9996\u6307\u9488\u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n_front = index(_front + 1);\n_queSize--;\nreturn num;\n}\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast() {\nint num = peekLast();\n_queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst() {\nif (isEmpty()) {\nthrow Exception(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n}\nreturn _nums[_front];\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast() {\nif (isEmpty()) {\nthrow Exception(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n}\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nint last = index(_front + _queSize - 1);\nreturn _nums[last];\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nList<int> toArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nList<int> res = List.filled(_queSize, 0);\nfor (int i = 0, j = _front; i < _queSize; i++, j++) {\nres[i] = _nums[index(j)];\n}\nreturn res;\n}\n}\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\u5373\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

    "},{"location":"chapter_stack_and_queue/queue/","title":"5.2. \u00a0 \u961f\u5217","text":"

    \u300c\u961f\u5217 Queue\u300d\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u5148\u51fa\uff08First In, First Out\uff09\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\u7684\u5c3e\u90e8\uff0c\u800c\u4f4d\u4e8e\u961f\u5217\u5934\u90e8\u7684\u4eba\u9010\u4e2a\u79bb\u5f00\u3002

    \u6211\u4eec\u628a\u961f\u5217\u7684\u5934\u90e8\u79f0\u4e3a\u300c\u961f\u9996\u300d\uff0c\u5c3e\u90e8\u79f0\u4e3a\u300c\u961f\u5c3e\u300d\uff0c\u628a\u5c06\u5143\u7d20\u52a0\u5165\u961f\u5c3e\u7684\u64cd\u4f5c\u79f0\u4e3a\u300c\u5165\u961f\u300d\uff0c\u5220\u9664\u961f\u9996\u5143\u7d20\u7684\u64cd\u4f5c\u79f0\u4e3a\u300c\u51fa\u961f\u300d\u3002

    Fig. \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\u4e0b\u8868\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

    \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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart queue.java
    /* \u521d\u59cb\u5316\u961f\u5217 */\nQueue<Integer> queue = new LinkedList<>();\n/* \u5143\u7d20\u5165\u961f */\nqueue.offer(1);\nqueue.offer(3);\nqueue.offer(2);\nqueue.offer(5);\nqueue.offer(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek = queue.peek();\n/* \u5143\u7d20\u51fa\u961f */\nint pop = queue.poll();\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.size();\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = queue.isEmpty();\n
    queue.cpp
    /* \u521d\u59cb\u5316\u961f\u5217 */\nqueue<int> queue;\n/* \u5143\u7d20\u5165\u961f */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint front = queue.front();\n/* \u5143\u7d20\u51fa\u961f */\nqueue.pop();\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.size();\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty = queue.empty();\n
    queue.py
    # \u521d\u59cb\u5316\u961f\u5217\n# \u5728 Python \u4e2d\uff0c\u6211\u4eec\u4e00\u822c\u5c06\u53cc\u5411\u961f\u5217\u7c7b deque \u770b\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\u5efa\u8bae\nque: Deque[int] = collections.deque()\n# \u5143\u7d20\u5165\u961f\nque.append(1)\nque.append(3)\nque.append(2)\nque.append(5)\nque.append(4)\n# \u8bbf\u95ee\u961f\u9996\u5143\u7d20\nfront: int = que[0];\n# \u5143\u7d20\u51fa\u961f\npop: int = que.popleft()\n# \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\nsize: int = len(que)\n# \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = len(que) == 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/* \u5143\u7d20\u5165\u961f */\nqueue.PushBack(1)\nqueue.PushBack(3)\nqueue.PushBack(2)\nqueue.PushBack(5)\nqueue.PushBack(4)\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeek := queue.Front()\n/* \u5143\u7d20\u51fa\u961f */\npop := queue.Front()\nqueue.Remove(pop)\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nsize := queue.Len()\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty := queue.Len() == 0\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/* \u5143\u7d20\u5165\u961f */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nconst peek = queue[0];\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/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nconst size = queue.length;\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/* \u5143\u7d20\u5165\u961f */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nconst peek = queue[0];\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/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nconst size = queue.length;\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nconst empty = queue.length === 0;\n
    queue.c
    // C \u672a\u63d0\u4f9b\u5185\u7f6e\u961f\u5217\n
    queue.cs
    /* \u521d\u59cb\u5316\u961f\u5217 */\nQueue<int> queue = new();\n/* \u5143\u7d20\u5165\u961f */\nqueue.Enqueue(1);\nqueue.Enqueue(3);\nqueue.Enqueue(2);\nqueue.Enqueue(5);\nqueue.Enqueue(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek = queue.Peek();\n/* \u5143\u7d20\u51fa\u961f */\nint pop = queue.Dequeue();\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.Count();\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = queue.Count() == 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/* \u5143\u7d20\u5165\u961f */\nqueue.append(1)\nqueue.append(3)\nqueue.append(2)\nqueue.append(5)\nqueue.append(4)\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nlet peek = queue.first!\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/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = queue.count\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = queue.isEmpty\n
    queue.zig
    \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/* \u5143\u7d20\u5165\u961f */\nqueue.add(1);\nqueue.add(3);\nqueue.add(2);\nqueue.add(5);\nqueue.add(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek = queue.first;\n/* \u5143\u7d20\u51fa\u961f */\nint pop = queue.removeFirst();\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.length;\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = queue.isEmpty;\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\u56e0\u6b64\uff0c\u94fe\u8868\u548c\u6570\u7ec4\u90fd\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u961f\u5217\u3002

    "},{"location":"chapter_stack_and_queue/queue/#_1","title":"\u57fa\u4e8e\u94fe\u8868\u7684\u5b9e\u73b0","text":"

    \u5bf9\u4e8e\u94fe\u8868\u5b9e\u73b0\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u94fe\u8868\u7684\u300c\u5934\u8282\u70b9\u300d\u548c\u300c\u5c3e\u8282\u70b9\u300d\u5206\u522b\u89c6\u4e3a\u961f\u9996\u548c\u961f\u5c3e\uff0c\u89c4\u5b9a\u961f\u5c3e\u4ec5\u53ef\u6dfb\u52a0\u8282\u70b9\uff0c\u800c\u961f\u9996\u4ec5\u53ef\u5220\u9664\u8282\u70b9\u3002

    LinkedListQueuepush()pop()

    \u4ee5\u4e0b\u662f\u7528\u94fe\u8868\u5b9e\u73b0\u961f\u5217\u7684\u793a\u4f8b\u4ee3\u7801\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart linkedlist_queue.java
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\nprivate ListNode front, rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\nprivate int queSize = 0;\npublic LinkedListQueue() {\nfront = null;\nrear = null;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic boolean isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u961f */\npublic void push(int num) {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nListNode 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\nif (front == null) {\nfront = node;\nrear = node;\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n} else {\nrear.next = node;\nrear = node;\n}\nqueSize++;\n}\n/* \u51fa\u961f */\npublic int pop() {\nint num = peek();\n// \u5220\u9664\u5934\u8282\u70b9\nfront = front.next;\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int peek() {\nif (size() == 0)\nthrow new IndexOutOfBoundsException();\nreturn front.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npublic int[] toArray() {\nListNode node = front;\nint[] res = new int[size()];\nfor (int i = 0; i < res.length; i++) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n
    linkedlist_queue.cpp
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\nprivate:\nListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\nint queSize;\npublic:\nLinkedListQueue() {\nfront = nullptr;\nrear = nullptr;\nqueSize = 0;\n}\n~LinkedListQueue() {\n// \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\nfreeMemoryLinkedList(front);\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty() {\nreturn queSize == 0;\n}\n/* \u5165\u961f */\nvoid push(int num) {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nListNode *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\nif (front == nullptr) {\nfront = node;\nrear = node;\n}\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\nelse {\nrear->next = node;\nrear = node;\n}\nqueSize++;\n}\n/* \u51fa\u961f */\nvoid pop() {\nint num = peek();\n// \u5220\u9664\u5934\u8282\u70b9\nListNode *tmp = front;\nfront = front->next;\n// \u91ca\u653e\u5185\u5b58\ndelete tmp;\nqueSize--;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek() {\nif (size() == 0)\nthrow out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\nreturn front->val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Vector \u5e76\u8fd4\u56de */\nvector<int> toVector() {\nListNode *node = front;\nvector<int> res(size());\nfor (int i = 0; i < res.size(); i++) {\nres[i] = node->val;\nnode = node->next;\n}\nreturn res;\n}\n};\n
    linkedlist_queue.py
    class LinkedListQueue:\n\"\"\"\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217\"\"\"\ndef __init__(self):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.__front: ListNode | None = None  # \u5934\u8282\u70b9 front\nself.__rear: ListNode | None = None  # \u5c3e\u8282\u70b9 rear\nself.__size: int = 0\ndef size(self) -> int:\n\"\"\"\u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\"\"\"\nreturn self.__size\ndef is_empty(self) -> bool:\n\"\"\"\u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\nreturn not self.__front\ndef push(self, num: int) -> None:\n\"\"\"\u5165\u961f\"\"\"\n# \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nnode = ListNode(num)\n# \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\nif self.__front is None:\nself.__front = node\nself.__rear = node\n# \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\nelse:\nself.__rear.next = node\nself.__rear = node\nself.__size += 1\ndef pop(self) -> int:\n\"\"\"\u51fa\u961f\"\"\"\nnum = self.peek()\n# \u5220\u9664\u5934\u8282\u70b9\nself.__front = self.__front.next\nself.__size -= 1\nreturn num\ndef peek(self) -> int:\n\"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\nif self.size() == 0:\nprint(\"\u961f\u5217\u4e3a\u7a7a\")\nreturn False\nreturn self.__front.val\ndef to_list(self) -> list[int]:\n\"\"\"\u8f6c\u5316\u4e3a\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\nqueue = []\ntemp = self.__front\nwhile temp:\nqueue.append(temp.val)\ntemp = temp.next\nreturn queue\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\ndata *list.List\n}\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newLinkedListQueue() *linkedListQueue {\nreturn &linkedListQueue{\ndata: list.New(),\n}\n}\n/* \u5165\u961f */\nfunc (s *linkedListQueue) push(value any) {\ns.data.PushBack(value)\n}\n/* \u51fa\u961f */\nfunc (s *linkedListQueue) pop() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Front()\ns.data.Remove(e)\nreturn e.Value\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (s *linkedListQueue) peek() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Front()\nreturn e.Value\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (s *linkedListQueue) size() int {\nreturn s.data.Len()\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListQueue) isEmpty() bool {\nreturn s.data.Len() == 0\n}\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListQueue) toList() *list.List {\nreturn s.data\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;\nconstructor() {\nthis.#front = null;\nthis.#rear = null;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nget size() {\nreturn this.#queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty() {\nreturn this.size === 0;\n}\n/* \u5165\u961f */\npush(num) {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nconst 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\nif (!this.#front) {\nthis.#front = node;\nthis.#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 {\nthis.#rear.next = node;\nthis.#rear = node;\n}\nthis.#queSize++;\n}\n/* \u51fa\u961f */\npop() {\nconst num = this.peek();\n// \u5220\u9664\u5934\u8282\u70b9\nthis.#front = this.#front.next;\nthis.#queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeek() {\nif (this.size === 0) throw new Error('\u961f\u5217\u4e3a\u7a7a');\nreturn this.#front.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\ntoArray() {\nlet node = this.#front;\nconst res = new Array(this.size);\nfor (let i = 0; i < res.length; i++) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n
    linkedlist_queue.ts
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\nprivate front: ListNode | null; // \u5934\u8282\u70b9 front\nprivate rear: ListNode | null; // \u5c3e\u8282\u70b9 rear\nprivate queSize: number = 0;\nconstructor() {\nthis.front = null;\nthis.rear = null;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nget size(): number {\nreturn this.queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty(): boolean {\nreturn this.size === 0;\n}\n/* \u5165\u961f */\npush(num: number): void {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nconst 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\nif (!this.front) {\nthis.front = node;\nthis.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 {\nthis.rear!.next = node;\nthis.rear = node;\n}\nthis.queSize++;\n}\n/* \u51fa\u961f */\npop(): number {\nconst num = this.peek();\nif (!this.front) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n// \u5220\u9664\u5934\u8282\u70b9\nthis.front = this.front.next;\nthis.queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeek(): number {\nif (this.size === 0) throw new Error('\u961f\u5217\u4e3a\u7a7a');\nreturn this.front!.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\ntoArray(): number[] {\nlet node = this.front;\nconst res = new Array<number>(this.size);\nfor (let i = 0; i < res.length; i++) {\nres[i] = node!.val;\nnode = node!.next;\n}\nreturn res;\n}\n}\n
    linkedlist_queue.c
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nstruct linkedListQueue {\nListNode *front, *rear;\nint queSize;\n};\ntypedef struct linkedListQueue linkedListQueue;\n/* \u6784\u9020\u51fd\u6570 */\nlinkedListQueue *newLinkedListQueue() {\nlinkedListQueue *queue = (linkedListQueue *)malloc(sizeof(linkedListQueue));\nqueue->front = NULL;\nqueue->rear = NULL;\nqueue->queSize = 0;\nreturn queue;\n}\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListQueue(linkedListQueue *queue) {\n// \u91ca\u653e\u6240\u6709\u8282\u70b9\nfor (int i = 0; i < queue->queSize && queue->front != NULL; i++) {\nListNode *tmp = queue->front;\nqueue->front = queue->front->next;\nfree(tmp);\n}\n// \u91ca\u653e queue \u7ed3\u6784\u4f53\nfree(queue);\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(linkedListQueue *queue) {\nreturn queue->queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(linkedListQueue *queue) {\nreturn (size(queue) == 0);\n}\n/* \u5165\u961f */\nvoid push(linkedListQueue *queue, int num) {\n// \u5c3e\u8282\u70b9\u5904\u6dfb\u52a0 node\nListNode *node = newListNode(num);\n// \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\nif (queue->front == NULL) {\nqueue->front = node;\nqueue->rear = node;\n}\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\nelse {\nqueue->rear->next = node;\nqueue->rear = node;\n}\nqueue->queSize++;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek(linkedListQueue *queue) {\nassert(size(queue) && queue->front);\nreturn queue->front->val;\n}\n/* \u51fa\u961f */\nvoid pop(linkedListQueue *queue) {\nint num = peek(queue);\nListNode *tmp = queue->front;\nqueue->front = queue->front->next;\nfree(tmp);\nqueue->queSize--;\n}\n/* \u6253\u5370\u961f\u5217 */\nvoid printLinkedListQueue(linkedListQueue *queue) {\nint arr[queue->queSize];\n// \u62f7\u8d1d\u94fe\u8868\u4e2d\u7684\u6570\u636e\u5230\u6570\u7ec4\nint i;\nListNode *node;\nfor (i = 0, node = queue->front; i < queue->queSize && queue->front != queue->rear; i++) {\narr[i] = node->val;\nnode = node->next;\n}\nprintArray(arr, queue->queSize);\n}\n
    linkedlist_queue.cs
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\nprivate ListNode? front, rear;  // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear \nprivate int queSize = 0;\npublic LinkedListQueue() {\nfront = null;\nrear = null;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic bool isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u961f */\npublic void push(int num) {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nListNode 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\nif (front == null) {\nfront = node;\nrear = 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) {\nrear.next = node;\nrear = node;\n}\nqueSize++;\n}\n/* \u51fa\u961f */\npublic int pop() {\nint num = peek();\n// \u5220\u9664\u5934\u8282\u70b9\nfront = front?.next;\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int peek() {\nif (size() == 0 || front == null)\nthrow new Exception();\nreturn front.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npublic int[] toArray() {\nif (front == null)\nreturn Array.Empty<int>();\nListNode node = front;\nint[] res = new int[size()];\nfor (int i = 0; i < res.Length; i++) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n
    linkedlist_queue.swift
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\nprivate var front: ListNode? // \u5934\u8282\u70b9\nprivate var rear: ListNode? // \u5c3e\u8282\u70b9\nprivate var _size = 0\ninit() {}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc size() -> Int {\n_size\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc isEmpty() -> Bool {\nsize() == 0\n}\n/* \u5165\u961f */\nfunc push(num: Int) {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nlet 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\nif front == nil {\nfront = node\nrear = node\n}\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\nelse {\nrear?.next = node\nrear = node\n}\n_size += 1\n}\n/* \u51fa\u961f */\n@discardableResult\nfunc pop() -> Int {\nlet num = peek()\n// \u5220\u9664\u5934\u8282\u70b9\nfront = front?.next\n_size -= 1\nreturn num\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc peek() -> Int {\nif isEmpty() {\nfatalError(\"\u961f\u5217\u4e3a\u7a7a\")\n}\nreturn front!.val\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\nfunc toArray() -> [Int] {\nvar node = front\nvar res = Array(repeating: 0, count: size())\nfor i in res.indices {\nres[i] = node!.val\nnode = node?.next\n}\nreturn res\n}\n}\n
    linkedlist_queue.zig
    // \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217\nfn LinkedListQueue(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nfront: ?*inc.ListNode(T) = null,                // \u5934\u8282\u70b9 front\nrear: ?*inc.ListNode(T) = null,                 // \u5c3e\u8282\u70b9 rear\nque_size: usize = 0,                             // \u961f\u5217\u7684\u957f\u5ea6\nmem_arena: ?std.heap.ArenaAllocator = null,\nmem_allocator: std.mem.Allocator = undefined,   // \u5185\u5b58\u5206\u914d\u5668\n// \u6784\u9020\u65b9\u6cd5\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u961f\u5217\uff09\npub fn init(self: *Self, allocator: std.mem.Allocator) !void {\nif (self.mem_arena == null) {\nself.mem_arena = std.heap.ArenaAllocator.init(allocator);\nself.mem_allocator = self.mem_arena.?.allocator();\n}\nself.front = null;\nself.rear = null;\nself.que_size = 0;\n}\n// \u6790\u6784\u65b9\u6cd5\uff08\u91ca\u653e\u5185\u5b58\uff09\npub fn deinit(self: *Self) void {\nif (self.mem_arena == null) return;\nself.mem_arena.?.deinit();\n}\n// \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\npub fn size(self: *Self) usize {\nreturn self.que_size;\n}\n// \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\npub fn isEmpty(self: *Self) bool {\nreturn self.size() == 0;\n}\n// \u8bbf\u95ee\u961f\u9996\u5143\u7d20\npub fn peek(self: *Self) T {\nif (self.size() == 0) @panic(\"\u961f\u5217\u4e3a\u7a7a\");\nreturn self.front.?.val;\n}  // \u5165\u961f\npub fn push(self: *Self, num: T) !void {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nvar node = try self.mem_allocator.create(inc.ListNode(T));\nnode.init(num);\n// \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\nif (self.front == null) {\nself.front = node;\nself.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 {\nself.rear.?.next = node;\nself.rear = node;\n}\nself.que_size += 1;\n} // \u51fa\u961f\npub fn pop(self: *Self) T {\nvar num = self.peek();\n// \u5220\u9664\u5934\u8282\u70b9\nself.front = self.front.?.next;\nself.que_size -= 1;\nreturn num;\n} // \u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\npub fn toArray(self: *Self) ![]T {\nvar node = self.front;\nvar res = try self.mem_allocator.alloc(T, self.size());\nstd.mem.set(T, res, @as(T, 0));\nvar i: usize = 0;\nwhile (i < res.len) : (i += 1) {\nres[i] = node.?.val;\nnode = node.?.next;\n}\nreturn res;\n}\n};\n}\n
    linkedlist_queue.dart
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\nListNode? _front; // \u5934\u8282\u70b9 _front\nListNode? _rear; // \u5c3e\u8282\u70b9 _rear\nint _queSize = 0; // \u961f\u5217\u957f\u5ea6\nLinkedListQueue() {\n_front = null;\n_rear = null;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size() {\nreturn _queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty() {\nreturn _queSize == 0;\n}\n/* \u5165\u961f */\nvoid push(int num) {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nfinal node = ListNode(num);\n// \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\nif (_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/* \u51fa\u961f */\nint pop() {\nfinal int num = peek();\n// \u5220\u9664\u5934\u8282\u70b9\n_front = _front!.next;\n_queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek() {\nif (_queSize == 0) {\nthrow Exception('\u961f\u5217\u4e3a\u7a7a');\n}\nreturn _front!.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\nList<int> toArray() {\nListNode? node = _front;\nfinal List<int> queue = [];\nwhile (node != null) {\nqueue.add(node.val);\nnode = node.next;\n}\nreturn queue;\n}\n}\n
    "},{"location":"chapter_stack_and_queue/queue/#_2","title":"\u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"

    \u7531\u4e8e\u6570\u7ec4\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 queSize \u7528\u4e8e\u8bb0\u5f55\u961f\u5217\u957f\u5ea6\u3002\u5b9a\u4e49 rear = front + queSize \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\u8fdb\u800c\uff1a

    • \u5bf9\u4e8e\u5165\u961f\u64cd\u4f5c\uff0c\u5c06\u8f93\u5165\u5143\u7d20\u8d4b\u503c\u7ed9 rear \u7d22\u5f15\u5904\uff0c\u5e76\u5c06 queSize \u589e\u52a0 1 \uff1b
    • \u5bf9\u4e8e\u51fa\u961f\u64cd\u4f5c\uff0c\u53ea\u9700\u5c06 front \u589e\u52a0 1 \uff0c\u5e76\u5c06 queSize \u51cf\u5c11 1 \uff1b

    \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()

    \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\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u89c6\u4e3a\u9996\u5c3e\u76f8\u63a5\u7684\u300c\u73af\u5f62\u6570\u7ec4\u300d\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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart array_queue.java
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\nprivate int[] nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate int queSize; // \u961f\u5217\u957f\u5ea6\npublic ArrayQueue(int capacity) {\nnums = new int[capacity];\nfront = queSize = 0;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\npublic int capacity() {\nreturn nums.length;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic boolean isEmpty() {\nreturn queSize == 0;\n}\n/* \u5165\u961f */\npublic void push(int num) {\nif (queSize == capacity()) {\nSystem.out.println(\"\u961f\u5217\u5df2\u6ee1\");\nreturn;\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\nint rear = (front + queSize) % capacity();\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num;\nqueSize++;\n}\n/* \u51fa\u961f */\npublic int pop() {\nint 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\nfront = (front + 1) % capacity();\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int peek() {\nif (isEmpty())\nthrow new IndexOutOfBoundsException();\nreturn nums[front];\n}\n/* \u8fd4\u56de\u6570\u7ec4 */\npublic int[] toArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nint[] res = new int[queSize];\nfor (int i = 0, j = front; i < queSize; i++, j++) {\nres[i] = nums[j % capacity()];\n}\nreturn res;\n}\n}\n
    array_queue.cpp
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\nprivate:\nint *nums;       // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nint front;       // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nint queSize;     // \u961f\u5217\u957f\u5ea6\nint queCapacity; // \u961f\u5217\u5bb9\u91cf\npublic:\nArrayQueue(int capacity) {\n// \u521d\u59cb\u5316\u6570\u7ec4\nnums = new int[capacity];\nqueCapacity = capacity;\nfront = queSize = 0;\n}\n~ArrayQueue() {\ndelete[] nums;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity() {\nreturn queCapacity;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty() {\nreturn size() == 0;\n}\n/* \u5165\u961f */\nvoid push(int num) {\nif (queSize == queCapacity) {\ncout << \"\u961f\u5217\u5df2\u6ee1\" << endl;\nreturn;\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\nint rear = (front + queSize) % queCapacity;\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num;\nqueSize++;\n}\n/* \u51fa\u961f */\nvoid pop() {\nint 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\nfront = (front + 1) % queCapacity;\nqueSize--;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek() {\nif (empty())\nthrow out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\nreturn nums[front];\n}\n/* \u5c06\u6570\u7ec4\u8f6c\u5316\u4e3a Vector \u5e76\u8fd4\u56de */\nvector<int> toVector() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvector<int> arr(queSize);\nfor (int i = 0, j = front; i < queSize; i++, j++) {\narr[i] = nums[j % queCapacity];\n}\nreturn arr;\n}\n};\n
    array_queue.py
    class ArrayQueue:\n\"\"\"\u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217\"\"\"\ndef __init__(self, size: int) -> None:\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.__nums: list[int] = [0] * size  # \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nself.__front: int = 0  # \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nself.__size: int = 0  # \u961f\u5217\u957f\u5ea6\ndef capacity(self) -> int:\n\"\"\"\u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf\"\"\"\nreturn len(self.__nums)\ndef size(self) -> int:\n\"\"\"\u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\"\"\"\nreturn self.__size\ndef is_empty(self) -> bool:\n\"\"\"\u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\nreturn self.__size == 0\ndef push(self, num: int) -> None:\n\"\"\"\u5165\u961f\"\"\"\nif self.__size == self.capacity():\nraise 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\u90e8F\nrear: int = (self.__front + self.__size) % self.capacity()\n# \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nself.__nums[rear] = num\nself.__size += 1\ndef pop(self) -> int:\n\"\"\"\u51fa\u961f\"\"\"\nnum: 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\nself.__front = (self.__front + 1) % self.capacity()\nself.__size -= 1\nreturn num\ndef peek(self) -> int:\n\"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\nif self.is_empty():\nraise IndexError(\"\u961f\u5217\u4e3a\u7a7a\")\nreturn self.__nums[self.__front]\ndef to_list(self) -> list[int]:\n\"\"\"\u8fd4\u56de\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\nres = [0] * self.size()\nj: int = self.__front\nfor i in range(self.size()):\nres[i] = self.__nums[(j % self.capacity())]\nj += 1\nreturn res\n
    array_queue.go
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\ntype arrayQueue struct {\nnums        []int // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nfront       int   // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nqueSize     int   // \u961f\u5217\u957f\u5ea6\nqueCapacity int   // \u961f\u5217\u5bb9\u91cf\uff08\u5373\u6700\u5927\u5bb9\u7eb3\u5143\u7d20\u6570\u91cf\uff09\n}\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newArrayQueue(queCapacity int) *arrayQueue {\nreturn &arrayQueue{\nnums:        make([]int, queCapacity),\nqueCapacity: queCapacity,\nfront:       0,\nqueSize:     0,\n}\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (q *arrayQueue) size() int {\nreturn q.queSize\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (q *arrayQueue) isEmpty() bool {\nreturn q.queSize == 0\n}\n/* \u5165\u961f */\nfunc (q *arrayQueue) push(num int) {\n// \u5f53 rear == queCapacity \u8868\u793a\u961f\u5217\u5df2\u6ee1\nif q.queSize == q.queCapacity {\nreturn\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\nrear := (q.front + q.queSize) % q.queCapacity\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nq.nums[rear] = num\nq.queSize++\n}\n/* \u51fa\u961f */\nfunc (q *arrayQueue) pop() any {\nnum := 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\nq.front = (q.front + 1) % q.queCapacity\nq.queSize--\nreturn num\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (q *arrayQueue) peek() any {\nif q.isEmpty() {\nreturn nil\n}\nreturn q.nums[q.front]\n}\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (q *arrayQueue) toSlice() []int {\nrear := (q.front + q.queSize)\nif rear >= q.queCapacity {\nrear %= q.queCapacity\nreturn append(q.nums[q.front:], q.nums[:rear]...)\n}\nreturn q.nums[q.front:rear]\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\nconstructor(capacity) {\nthis.#nums = new Array(capacity);\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nget capacity() {\nreturn this.#nums.length;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nget size() {\nreturn this.#queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nempty() {\nreturn this.#queSize == 0;\n}\n/* \u5165\u961f */\npush(num) {\nif (this.size == this.capacity) {\nconsole.log('\u961f\u5217\u5df2\u6ee1');\nreturn;\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\nconst rear = (this.#front + this.size) % this.capacity;\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nthis.#nums[rear] = num;\nthis.#queSize++;\n}\n/* \u51fa\u961f */\npop() {\nconst 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\nthis.#front = (this.#front + 1) % this.capacity;\nthis.#queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeek() {\nif (this.empty()) throw new Error('\u961f\u5217\u4e3a\u7a7a');\nreturn this.#nums[this.#front];\n}\n/* \u8fd4\u56de Array */\ntoArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nconst arr = new Array(this.size);\nfor (let i = 0, j = this.#front; i < this.size; i++, j++) {\narr[i] = this.#nums[j % this.capacity];\n}\nreturn arr;\n}\n}\n
    array_queue.ts
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\nprivate nums: number[]; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate front: number; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate queSize: number; // \u961f\u5217\u957f\u5ea6\nconstructor(capacity: number) {\nthis.nums = new Array(capacity);\nthis.front = this.queSize = 0;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nget capacity(): number {\nreturn this.nums.length;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nget size(): number {\nreturn this.queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nempty(): boolean {\nreturn this.queSize == 0;\n}\n/* \u5165\u961f */\npush(num: number): void {\nif (this.size == this.capacity) {\nconsole.log('\u961f\u5217\u5df2\u6ee1');\nreturn;\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\nconst rear = (this.front + this.queSize) % this.capacity;\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nthis.nums[rear] = num;\nthis.queSize++;\n}\n/* \u51fa\u961f */\npop(): number {\nconst 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\nthis.front = (this.front + 1) % this.capacity;\nthis.queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeek(): number {\nif (this.empty()) throw new Error('\u961f\u5217\u4e3a\u7a7a');\nreturn this.nums[this.front];\n}\n/* \u8fd4\u56de Array */\ntoArray(): number[] {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nconst arr = new Array(this.size);\nfor (let i = 0, j = this.front; i < this.size; i++, j++) {\narr[i] = this.nums[j % this.capacity];\n}\nreturn arr;\n}\n}\n
    array_queue.c
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nstruct arrayQueue {\nint *nums;       // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nint front;       // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nint queSize;     // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\nint queCapacity; // \u961f\u5217\u5bb9\u91cf\n};\ntypedef struct arrayQueue arrayQueue;\n/* \u6784\u9020\u51fd\u6570 */\narrayQueue *newArrayQueue(int capacity) {\narrayQueue *queue = (arrayQueue *)malloc(sizeof(arrayQueue));\n// \u521d\u59cb\u5316\u6570\u7ec4\nqueue->queCapacity = capacity;\nqueue->nums = (int *)malloc(sizeof(int) * queue->queCapacity);\nqueue->front = queue->queSize = 0;\nreturn queue;\n}\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayQueue(arrayQueue *queue) {\nfree(queue->nums);\nqueue->queCapacity = 0;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity(arrayQueue *queue) {\nreturn queue->queCapacity;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(arrayQueue *queue) {\nreturn queue->queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(arrayQueue *queue) {\nreturn queue->queSize == 0;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek(arrayQueue *queue) {\nassert(size(queue) != 0);\nreturn queue->nums[queue->front];\n}\n/* \u5165\u961f */\nvoid push(arrayQueue *queue, int num) {\nif (size(queue) == capacity(queue)) {\nprintf(\"\u961f\u5217\u5df2\u6ee1\\r\\n\");\nreturn;\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\nint rear = (queue->front + queue->queSize) % queue->queCapacity;\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nqueue->nums[rear] = num;\nqueue->queSize++;\n}\n/* \u51fa\u961f */\nvoid pop(arrayQueue *queue) {\nint 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\nqueue->front = (queue->front + 1) % queue->queCapacity;\nqueue->queSize--;\n}\n/* \u6253\u5370\u961f\u5217 */\nvoid printArrayQueue(arrayQueue *queue) {\nint arr[queue->queSize];\n// \u62f7\u8d1d\nfor (int i = 0, j = queue->front; i < queue->queSize; i++, j++) {\narr[i] = queue->nums[j % queue->queCapacity];\n}\nprintArray(arr, queue->queSize);\n}\n
    array_queue.cs
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\nprivate int[] nums;  // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate int front;   // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate int queSize; // \u961f\u5217\u957f\u5ea6\npublic ArrayQueue(int capacity) {\nnums = new int[capacity];\nfront = queSize = 0;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\npublic int capacity() {\nreturn nums.Length;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic bool isEmpty() {\nreturn queSize == 0;\n}\n/* \u5165\u961f */\npublic void push(int num) {\nif (queSize == capacity()) {\nConsole.WriteLine(\"\u961f\u5217\u5df2\u6ee1\");\nreturn;\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\nint rear = (front + queSize) % capacity();\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num;\nqueSize++;\n}\n/* \u51fa\u961f */\npublic int pop() {\nint 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\nfront = (front + 1) % capacity();\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int peek() {\nif (isEmpty())\nthrow new Exception();\nreturn nums[front];\n}\n/* \u8fd4\u56de\u6570\u7ec4 */\npublic int[] toArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nint[] res = new int[queSize];\nfor (int i = 0, j = front; i < queSize; i++, j++) {\nres[i] = nums[j % this.capacity()];\n}\nreturn res;\n}\n}\n
    array_queue.swift
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\nprivate var nums: [Int] // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate var front = 0 // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate var queSize = 0 // \u961f\u5217\u957f\u5ea6\ninit(capacity: Int) {\n// \u521d\u59cb\u5316\u6570\u7ec4\nnums = Array(repeating: 0, count: capacity)\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nfunc capacity() -> Int {\nnums.count\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc size() -> Int {\nqueSize\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc isEmpty() -> Bool {\nqueSize == 0\n}\n/* \u5165\u961f */\nfunc push(num: Int) {\nif size() == capacity() {\nprint(\"\u961f\u5217\u5df2\u6ee1\")\nreturn\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\nlet rear = (front + queSize) % capacity()\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num\nqueSize += 1\n}\n/* \u51fa\u961f */\n@discardableResult\nfunc pop() -> Int {\nlet 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\nfront = (front + 1) % capacity()\nqueSize -= 1\nreturn num\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc peek() -> Int {\nif isEmpty() {\nfatalError(\"\u961f\u5217\u4e3a\u7a7a\")\n}\nreturn nums[front]\n}\n/* \u8fd4\u56de\u6570\u7ec4 */\nfunc toArray() -> [Int] {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvar res = Array(repeating: 0, count: queSize)\nfor (i, j) in sequence(first: (0, front), next: { $0 < self.queSize - 1 ? ($0 + 1, $1 + 1) : nil }) {\nres[i] = nums[j % capacity()]\n}\nreturn res\n}\n}\n
    array_queue.zig
    // \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217\nfn ArrayQueue(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nnums: []T = undefined,                          // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4     \ncap: usize = 0,                                 // \u961f\u5217\u5bb9\u91cf\nfront: usize = 0,                               // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nque_size: usize = 0,                             // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\nmem_arena: ?std.heap.ArenaAllocator = null,\nmem_allocator: std.mem.Allocator = undefined,   // \u5185\u5b58\u5206\u914d\u5668\n// \u6784\u9020\u65b9\u6cd5\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u6570\u7ec4\uff09\npub fn init(self: *Self, allocator: std.mem.Allocator, cap: usize) !void {\nif (self.mem_arena == null) {\nself.mem_arena = std.heap.ArenaAllocator.init(allocator);\nself.mem_allocator = self.mem_arena.?.allocator();\n}\nself.cap = cap;\nself.nums = try self.mem_allocator.alloc(T, self.cap);\nstd.mem.set(T, self.nums, @as(T, 0));\n}\n// \u6790\u6784\u65b9\u6cd5\uff08\u91ca\u653e\u5185\u5b58\uff09\npub fn deinit(self: *Self) void {\nif (self.mem_arena == null) return;\nself.mem_arena.?.deinit();\n}\n// \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf\npub fn capacity(self: *Self) usize {\nreturn self.cap;\n}\n// \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\npub fn size(self: *Self) usize {\nreturn self.que_size;\n}\n// \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\npub fn isEmpty(self: *Self) bool {\nreturn self.que_size == 0;\n}\n// \u5165\u961f\npub fn push(self: *Self, num: T) !void {\nif (self.size() == self.capacity()) {\nstd.debug.print(\"\u961f\u5217\u5df2\u6ee1\\n\", .{});\nreturn;\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\nvar rear = (self.front + self.que_size) % self.capacity();\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nself.nums[rear] = num;\nself.que_size += 1;\n} // \u51fa\u961f\npub fn pop(self: *Self) T {\nvar 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\nself.front = (self.front + 1) % self.capacity();\nself.que_size -= 1;\nreturn num;\n} // \u8bbf\u95ee\u961f\u9996\u5143\u7d20\npub fn peek(self: *Self) T {\nif (self.isEmpty()) @panic(\"\u961f\u5217\u4e3a\u7a7a\");\nreturn self.nums[self.front];\n} // \u8fd4\u56de\u6570\u7ec4\npub fn toArray(self: *Self) ![]T {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvar res = try self.mem_allocator.alloc(T, self.size());\nstd.mem.set(T, res, @as(T, 0));\nvar i: usize = 0;\nvar j: usize = self.front;\nwhile (i < self.size()) : ({ i += 1; j += 1; }) {\nres[i] = self.nums[j % self.capacity()];\n}\nreturn res;\n}\n};\n}\n
    array_queue.dart
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\nlate List<int> _nums; // \u7528\u4e8e\u50a8\u5b58\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nlate int _front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nlate int _queSize; // \u961f\u5217\u957f\u5ea6\nArrayQueue(int capacity) {\n_nums = List.filled(capacity, 0);\n_front = _queSize = 0;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nint capaCity() {\nreturn _nums.length;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size() {\nreturn _queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty() {\nreturn _queSize == 0;\n}\n/* \u5165\u961f */\nvoid push(int num) {\nif (_queSize == capaCity()) {\nthrow 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\nint rear = (_front + _queSize) % capaCity();\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n_nums[rear] = num;\n_queSize++;\n}\n/* \u51fa\u961f */\nint pop() {\nint 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--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek() {\nif (isEmpty()) {\nthrow Exception(\"\u961f\u5217\u4e3a\u7a7a\");\n}\nreturn _nums[_front];\n}\n/* \u8fd4\u56de Array */\nList<int> toArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nfinal List<int> res = List.filled(_queSize, 0);\nfor (int i = 0, j = _front; i < _queSize; i++, j++) {\nres[i] = _nums[j % capaCity()];\n}\nreturn res;\n}\n}\n

    \u4ee5\u4e0a\u5b9e\u73b0\u7684\u961f\u5217\u4ecd\u7136\u5177\u6709\u5c40\u9650\u6027\uff0c\u5373\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\u540c\u5b66\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":"
    • \u6dd8\u5b9d\u8ba2\u5355\u3002\u8d2d\u7269\u8005\u4e0b\u5355\u540e\uff0c\u8ba2\u5355\u5c06\u52a0\u5165\u961f\u5217\u4e2d\uff0c\u7cfb\u7edf\u968f\u540e\u4f1a\u6839\u636e\u987a\u5e8f\u4f9d\u6b21\u5904\u7406\u961f\u5217\u4e2d\u7684\u8ba2\u5355\u3002\u5728\u53cc\u5341\u4e00\u671f\u95f4\uff0c\u77ed\u65f6\u95f4\u5185\u4f1a\u4ea7\u751f\u6d77\u91cf\u8ba2\u5355\uff0c\u9ad8\u5e76\u53d1\u6210\u4e3a\u5de5\u7a0b\u5e08\u4eec\u9700\u8981\u91cd\u70b9\u653b\u514b\u7684\u95ee\u9898\u3002
    • \u5404\u7c7b\u5f85\u529e\u4e8b\u9879\u3002\u4efb\u4f55\u9700\u8981\u5b9e\u73b0\u201c\u5148\u6765\u540e\u5230\u201d\u529f\u80fd\u7684\u573a\u666f\uff0c\u4f8b\u5982\u6253\u5370\u673a\u7684\u4efb\u52a1\u961f\u5217\u3001\u9910\u5385\u7684\u51fa\u9910\u961f\u5217\u7b49\u3002\u961f\u5217\u5728\u8fd9\u4e9b\u573a\u666f\u4e2d\u53ef\u4ee5\u6709\u6548\u5730\u7ef4\u62a4\u5904\u7406\u987a\u5e8f\u3002
    "},{"location":"chapter_stack_and_queue/stack/","title":"5.1. \u00a0 \u6808","text":"

    \u300c\u6808 Stack\u300d\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u540e\u51fa\uff08First In, Last Out\uff09\u539f\u5219\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\u9700\u8981\u62ff\u51fa\u5e95\u90e8\u7684\u76d8\u5b50\uff0c\u5219\u9700\u8981\u5148\u5c06\u4e0a\u9762\u7684\u76d8\u5b50\u4f9d\u6b21\u53d6\u51fa\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\u6570\u636e\u7ed3\u6784\u3002

    \u5728\u6808\u4e2d\uff0c\u6211\u4eec\u628a\u5806\u53e0\u5143\u7d20\u7684\u9876\u90e8\u79f0\u4e3a\u300c\u6808\u9876\u300d\uff0c\u5e95\u90e8\u79f0\u4e3a\u300c\u6808\u5e95\u300d\u3002\u5c06\u628a\u5143\u7d20\u6dfb\u52a0\u5230\u6808\u9876\u7684\u64cd\u4f5c\u53eb\u505a\u300c\u5165\u6808\u300d\uff0c\u800c\u5220\u9664\u6808\u9876\u5143\u7d20\u7684\u64cd\u4f5c\u53eb\u505a\u300c\u51fa\u6808\u300d\u3002

    Fig. \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\u4e0b\u8868\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() , pop() , peek() \u547d\u540d\u4e3a\u4f8b\u3002

    \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\u300c\u6570\u7ec4\u300d\u6216\u300c\u94fe\u8868\u300d\u89c6\u4f5c\u6808\u6765\u4f7f\u7528\uff0c\u5e76\u901a\u8fc7\u201c\u8111\u8865\u201d\u6765\u5ffd\u7565\u4e0e\u6808\u65e0\u5173\u7684\u64cd\u4f5c\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart stack.java
    /* \u521d\u59cb\u5316\u6808 */\nStack<Integer> stack = new Stack<>();\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek = stack.peek();\n/* \u5143\u7d20\u51fa\u6808 */\nint pop = stack.pop();\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.size();\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = stack.isEmpty();\n
    stack.cpp
    /* \u521d\u59cb\u5316\u6808 */\nstack<int> stack;\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint top = stack.top();\n/* \u5143\u7d20\u51fa\u6808 */\nstack.pop(); // \u65e0\u8fd4\u56de\u503c\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.size();\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nbool empty = stack.empty();\n
    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# \u5143\u7d20\u5165\u6808\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n# \u8bbf\u95ee\u6808\u9876\u5143\u7d20\npeek: int = stack[-1]\n# \u5143\u7d20\u51fa\u6808\npop: int = stack.pop()\n# \u83b7\u53d6\u6808\u7684\u957f\u5ea6\nsize: int = len(stack)\n# \u5224\u65ad\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = len(stack) == 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/* \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/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npeek := stack[len(stack)-1]\n/* \u5143\u7d20\u51fa\u6808 */\npop := stack[len(stack)-1]\nstack = stack[:len(stack)-1]\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nsize := len(stack)\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nisEmpty := len(stack) == 0\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/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nconst peek = stack[stack.length-1];\n/* \u5143\u7d20\u51fa\u6808 */\nconst pop = stack.pop();\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nconst size = stack.length;\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/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nconst peek = stack[stack.length - 1];\n/* \u5143\u7d20\u51fa\u6808 */\nconst pop = stack.pop();\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nconst size = stack.length;\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nconst is_empty = stack.length === 0;\n
    stack.c
    // C \u672a\u63d0\u4f9b\u5185\u7f6e\u6808\n
    stack.cs
    /* \u521d\u59cb\u5316\u6808 */\nStack<int> stack = new ();\n/* \u5143\u7d20\u5165\u6808 */\nstack.Push(1);\nstack.Push(3);\nstack.Push(2);\nstack.Push(5);\nstack.Push(4);\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek = stack.Peek();\n/* \u5143\u7d20\u51fa\u6808 */\nint pop = stack.Pop();\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.Count();\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = stack.Count()==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/* \u5143\u7d20\u5165\u6808 */\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nlet peek = stack.last!\n/* \u5143\u7d20\u51fa\u6808 */\nlet pop = stack.removeLast()\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nlet size = stack.count\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = stack.isEmpty\n
    stack.zig
    \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/* \u5143\u7d20\u5165\u6808 */\nstack.add(1);\nstack.add(3);\nstack.add(2);\nstack.add(5);\nstack.add(4);\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek = stack.last;\n/* \u5143\u7d20\u51fa\u6808 */\nint pop = stack.removeLast();\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.length;\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = stack.isEmpty;\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\u88ab\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":"\u57fa\u4e8e\u94fe\u8868\u7684\u5b9e\u73b0","text":"

    \u4f7f\u7528\u94fe\u8868\u6765\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

    \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()

    \u4ee5\u4e0b\u662f\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u6808\u7684\u793a\u4f8b\u4ee3\u7801\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart linkedlist_stack.java
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\nprivate ListNode stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nprivate int stkSize = 0; // \u6808\u7684\u957f\u5ea6\npublic LinkedListStack() {\nstackPeek = null;\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\npublic int size() {\nreturn stkSize;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\npublic boolean isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u6808 */\npublic void push(int num) {\nListNode node = new ListNode(num);\nnode.next = stackPeek;\nstackPeek = node;\nstkSize++;\n}\n/* \u51fa\u6808 */\npublic int pop() {\nint num = peek();\nstackPeek = stackPeek.next;\nstkSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npublic int peek() {\nif (size() == 0)\nthrow new IndexOutOfBoundsException();\nreturn stackPeek.val;\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npublic int[] toArray() {\nListNode node = stackPeek;\nint[] res = new int[size()];\nfor (int i = res.length - 1; i >= 0; i--) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n
    linkedlist_stack.cpp
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\nprivate:\nListNode *stackTop; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nint stkSize;        // \u6808\u7684\u957f\u5ea6\npublic:\nLinkedListStack() {\nstackTop = nullptr;\nstkSize = 0;\n}\n~LinkedListStack() {\n// \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\nfreeMemoryLinkedList(stackTop);\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size() {\nreturn stkSize;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool empty() {\nreturn size() == 0;\n}\n/* \u5165\u6808 */\nvoid push(int num) {\nListNode *node = new ListNode(num);\nnode->next = stackTop;\nstackTop = node;\nstkSize++;\n}\n/* \u51fa\u6808 */\nvoid pop() {\nint num = top();\nListNode *tmp = stackTop;\nstackTop = stackTop->next;\n// \u91ca\u653e\u5185\u5b58\ndelete tmp;\nstkSize--;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint top() {\nif (size() == 0)\nthrow out_of_range(\"\u6808\u4e3a\u7a7a\");\nreturn stackTop->val;\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\nvector<int> toVector() {\nListNode *node = stackTop;\nvector<int> res(size());\nfor (int i = res.size() - 1; i >= 0; i--) {\nres[i] = node->val;\nnode = node->next;\n}\nreturn res;\n}\n};\n
    linkedlist_stack.py
    class LinkedListStack:\n\"\"\"\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\"\"\"\ndef __init__(self):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.__peek: ListNode | None = None\nself.__size: int = 0\ndef size(self) -> int:\n\"\"\"\u83b7\u53d6\u6808\u7684\u957f\u5ea6\"\"\"\nreturn self.__size\ndef is_empty(self) -> bool:\n\"\"\"\u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\"\"\"\nreturn not self.__peek\ndef push(self, val: int) -> None:\n\"\"\"\u5165\u6808\"\"\"\nnode = ListNode(val)\nnode.next = self.__peek\nself.__peek = node\nself.__size += 1\ndef pop(self) -> int:\n\"\"\"\u51fa\u6808\"\"\"\nnum: int = self.peek()\nself.__peek = self.__peek.next\nself.__size -= 1\nreturn num\ndef peek(self) -> int:\n\"\"\"\u8bbf\u95ee\u6808\u9876\u5143\u7d20\"\"\"\n# \u5224\u7a7a\u5904\u7406\nif not self.__peek:\nreturn None\nreturn self.__peek.val\ndef to_list(self) -> list[int]:\n\"\"\"\u8f6c\u5316\u4e3a\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\narr = []\nnode = self.__peek\nwhile node:\narr.append(node.val)\nnode = node.next\narr.reverse()\nreturn arr\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\ndata *list.List\n}\n/* \u521d\u59cb\u5316\u6808 */\nfunc newLinkedListStack() *linkedListStack {\nreturn &linkedListStack{\ndata: list.New(),\n}\n}\n/* \u5165\u6808 */\nfunc (s *linkedListStack) push(value int) {\ns.data.PushBack(value)\n}\n/* \u51fa\u6808 */\nfunc (s *linkedListStack) pop() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Back()\ns.data.Remove(e)\nreturn e.Value\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nfunc (s *linkedListStack) peek() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Back()\nreturn e.Value\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nfunc (s *linkedListStack) size() int {\nreturn s.data.Len()\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListStack) isEmpty() bool {\nreturn s.data.Len() == 0\n}\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListStack) toList() *list.List {\nreturn s.data\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\nconstructor() {\nthis.#stackPeek = null;\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nget size() {\nreturn this.#stkSize;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nisEmpty() {\nreturn this.size == 0;\n}\n/* \u5165\u6808 */\npush(num) {\nconst node = new ListNode(num);\nnode.next = this.#stackPeek;\nthis.#stackPeek = node;\nthis.#stkSize++;\n}\n/* \u51fa\u6808 */\npop() {\nconst num = this.peek();\nthis.#stackPeek = this.#stackPeek.next;\nthis.#stkSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npeek() {\nif (!this.#stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\nreturn this.#stackPeek.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\ntoArray() {\nlet node = this.#stackPeek;\nconst res = new Array(this.size);\nfor (let i = res.length - 1; i >= 0; i--) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n
    linkedlist_stack.ts
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\nprivate stackPeek: ListNode | null; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nprivate stkSize: number = 0; // \u6808\u7684\u957f\u5ea6\nconstructor() {\nthis.stackPeek = null;\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nget size(): number {\nreturn this.stkSize;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nisEmpty(): boolean {\nreturn this.size == 0;\n}\n/* \u5165\u6808 */\npush(num: number): void {\nconst node = new ListNode(num);\nnode.next = this.stackPeek;\nthis.stackPeek = node;\nthis.stkSize++;\n}\n/* \u51fa\u6808 */\npop(): number {\nconst num = this.peek();\nif (!this.stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\nthis.stackPeek = this.stackPeek.next;\nthis.stkSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npeek(): number {\nif (!this.stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\nreturn this.stackPeek.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\ntoArray(): number[] {\nlet node = this.stackPeek;\nconst res = new Array<number>(this.size);\nfor (let i = res.length - 1; i >= 0; i--) {\nres[i] = node!.val;\nnode = node!.next;\n}\nreturn res;\n}\n}\n
    linkedlist_stack.c
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nstruct linkedListStack {\nListNode *top; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nint size;      // \u6808\u7684\u957f\u5ea6\n};\ntypedef struct linkedListStack linkedListStack;\n/* \u6784\u9020\u51fd\u6570 */\nlinkedListStack *newLinkedListStack() {\nlinkedListStack *s = malloc(sizeof(linkedListStack));\ns->top = NULL;\ns->size = 0;\nreturn s;\n}\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListStack(linkedListStack *s) {\nwhile (s->top) {\nListNode *n = s->top->next;\nfree(s->top);\ns->top = n;\n}\nfree(s);\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size(linkedListStack *s) {\nassert(s);\nreturn s->size;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty(linkedListStack *s) {\nassert(s);\nreturn size(s) == 0;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek(linkedListStack *s) {\nassert(s);\nassert(size(s) != 0);\nreturn s->top->val;\n}\n/* \u5165\u6808 */\nvoid push(linkedListStack *s, int num) {\nassert(s);\nListNode *node = (ListNode *)malloc(sizeof(ListNode));\nnode->next = s->top; // \u66f4\u65b0\u65b0\u52a0\u8282\u70b9\u6307\u9488\u57df\nnode->val = num;     // \u66f4\u65b0\u65b0\u52a0\u8282\u70b9\u6570\u636e\u57df\ns->top = node;       // \u66f4\u65b0\u6808\u9876\ns->size++;           // \u66f4\u65b0\u6808\u5927\u5c0f\n}\n/* \u51fa\u6808 */\nint pop(linkedListStack *s) {\nif (s->size == 0) {\nprintf(\"stack is empty.\\n\");\nreturn INT_MAX;\n}\nassert(s);\nint val = peek(s);\nListNode *tmp = s->top;\ns->top = s->top->next;\n// \u91ca\u653e\u5185\u5b58\nfree(tmp);\ns->size--;\nreturn val;\n}\n
    linkedlist_stack.cs
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\nprivate ListNode? stackPeek;  // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nprivate int stkSize = 0;   // \u6808\u7684\u957f\u5ea6\npublic LinkedListStack() {\nstackPeek = null;\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\npublic int size() {\nreturn stkSize;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\npublic bool isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u6808 */\npublic void push(int num) {\nListNode node = new ListNode(num);\nnode.next = stackPeek;\nstackPeek = node;\nstkSize++;\n}\n/* \u51fa\u6808 */\npublic int pop() {\nif (stackPeek == null)\nthrow new Exception();\nint num = peek();\nstackPeek = stackPeek.next;\nstkSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npublic int peek() {\nif (size() == 0 || stackPeek == null)\nthrow new Exception();\nreturn stackPeek.val;\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npublic int[] toArray() {\nif (stackPeek == null)\nreturn Array.Empty<int>();\nListNode node = stackPeek;\nint[] res = new int[size()];\nfor (int i = res.Length - 1; i >= 0; i--) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n
    linkedlist_stack.swift
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\nprivate var _peek: ListNode? // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nprivate var _size = 0 // \u6808\u7684\u957f\u5ea6\ninit() {}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nfunc size() -> Int {\n_size\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nfunc isEmpty() -> Bool {\nsize() == 0\n}\n/* \u5165\u6808 */\nfunc push(num: Int) {\nlet node = ListNode(x: num)\nnode.next = _peek\n_peek = node\n_size += 1\n}\n/* \u51fa\u6808 */\n@discardableResult\nfunc pop() -> Int {\nlet num = peek()\n_peek = _peek?.next\n_size -= 1\nreturn num\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nfunc peek() -> Int {\nif isEmpty() {\nfatalError(\"\u6808\u4e3a\u7a7a\")\n}\nreturn _peek!.val\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\nfunc toArray() -> [Int] {\nvar node = _peek\nvar res = Array(repeating: 0, count: _size)\nfor i in sequence(first: res.count - 1, next: { $0 >= 0 + 1 ? $0 - 1 : nil }) {\nres[i] = node!.val\nnode = node?.next\n}\nreturn res\n}\n}\n
    linkedlist_stack.zig
    // \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\nfn LinkedListStack(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nstack_top: ?*inc.ListNode(T) = null,             // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nstk_size: usize = 0,                             // \u6808\u7684\u957f\u5ea6\nmem_arena: ?std.heap.ArenaAllocator = null,\nmem_allocator: std.mem.Allocator = undefined,   // \u5185\u5b58\u5206\u914d\u5668\n// \u6784\u9020\u65b9\u6cd5\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u6808\uff09\npub fn init(self: *Self, allocator: std.mem.Allocator) !void {\nif (self.mem_arena == null) {\nself.mem_arena = std.heap.ArenaAllocator.init(allocator);\nself.mem_allocator = self.mem_arena.?.allocator();\n}\nself.stack_top = null;\nself.stk_size = 0;\n}\n// \u6790\u6784\u65b9\u6cd5\uff08\u91ca\u653e\u5185\u5b58\uff09\npub fn deinit(self: *Self) void {\nif (self.mem_arena == null) return;\nself.mem_arena.?.deinit();\n}\n// \u83b7\u53d6\u6808\u7684\u957f\u5ea6\npub fn size(self: *Self) usize {\nreturn self.stk_size;\n}\n// \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\npub fn isEmpty(self: *Self) bool {\nreturn self.size() == 0;\n}\n// \u8bbf\u95ee\u6808\u9876\u5143\u7d20\npub fn peek(self: *Self) T {\nif (self.size() == 0) @panic(\"\u6808\u4e3a\u7a7a\");\nreturn self.stack_top.?.val;\n}  // \u5165\u6808\npub fn push(self: *Self, num: T) !void {\nvar node = try self.mem_allocator.create(inc.ListNode(T));\nnode.init(num);\nnode.next = self.stack_top;\nself.stack_top = node;\nself.stk_size += 1;\n} // \u51fa\u6808\npub fn pop(self: *Self) T {\nvar num = self.peek();\nself.stack_top = self.stack_top.?.next;\nself.stk_size -= 1;\nreturn num;\n} // \u5c06\u6808\u8f6c\u6362\u4e3a\u6570\u7ec4\npub fn toArray(self: *Self) ![]T {\nvar node = self.stack_top;\nvar res = try self.mem_allocator.alloc(T, self.size());\nstd.mem.set(T, res, @as(T, 0));\nvar i: usize = 0;\nwhile (i < res.len) : (i += 1) {\nres[res.len - i - 1] = node.?.val;\nnode = node.?.next;\n}\nreturn res;\n}\n};\n}\n
    linkedlist_stack.dart
    /* \u57fa\u4e8e\u94fe\u8868\u7c7b\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\nListNode? _stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nint _stkSize = 0; // \u6808\u7684\u957f\u5ea6\nLinkedListStack() {\n_stackPeek = null;\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size() {\nreturn _stkSize;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty() {\nreturn _stkSize == 0;\n}\n/* \u5165\u6808 */\nvoid push(int num) {\nfinal ListNode node = ListNode(num);\nnode.next = _stackPeek;\n_stackPeek = node;\n_stkSize++;\n}\n/* \u51fa\u6808 */\nint pop() {\nfinal int num = peek();\n_stackPeek = _stackPeek!.next;\n_stkSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek() {\nif (_stackPeek == null) {\nthrow Exception(\"\u6808\u4e3a\u7a7a\");\n}\nreturn _stackPeek!.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a List \u5e76\u8fd4\u56de */\nList<int> toList() {\nListNode? node = _stackPeek;\nList<int> list = [];\nwhile (node != null) {\nlist.add(node.val);\nnode = node.next;\n}\nlist = list.reversed.toList();\nreturn list;\n}\n}\n
    "},{"location":"chapter_stack_and_queue/stack/#_2","title":"\u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"

    \u5728\u57fa\u4e8e\u300c\u6570\u7ec4\u300d\u5b9e\u73b0\u6808\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u7684\u5c3e\u90e8\u4f5c\u4e3a\u6808\u9876\u3002\u5728\u8fd9\u6837\u7684\u8bbe\u8ba1\u4e0b\uff0c\u5165\u6808\u4e0e\u51fa\u6808\u64cd\u4f5c\u5c31\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()

    \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\u9700\u81ea\u884c\u5904\u7406\u6570\u7ec4\u6269\u5bb9\u95ee\u9898\u3002\u4ee5\u4e0b\u4e3a\u793a\u4f8b\u4ee3\u7801\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart array_stack.java
    /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\nprivate ArrayList<Integer> stack;\npublic ArrayStack() {\n// \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\nstack = new ArrayList<>();\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\npublic int size() {\nreturn stack.size();\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\npublic boolean isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u6808 */\npublic void push(int num) {\nstack.add(num);\n}\n/* \u51fa\u6808 */\npublic int pop() {\nif (isEmpty())\nthrow new IndexOutOfBoundsException();\nreturn stack.remove(size() - 1);\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npublic int peek() {\nif (isEmpty())\nthrow new IndexOutOfBoundsException();\nreturn stack.get(size() - 1);\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npublic Object[] toArray() {\nreturn stack.toArray();\n}\n}\n
    array_stack.cpp
    /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\nprivate:\nvector<int> stack;\npublic:\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size() {\nreturn stack.size();\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool empty() {\nreturn stack.empty();\n}\n/* \u5165\u6808 */\nvoid push(int num) {\nstack.push_back(num);\n}\n/* \u51fa\u6808 */\nvoid pop() {\nint oldTop = top();\nstack.pop_back();\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint top() {\nif (empty())\nthrow out_of_range(\"\u6808\u4e3a\u7a7a\");\nreturn stack.back();\n}\n/* \u8fd4\u56de Vector */\nvector<int> toVector() {\nreturn stack;\n}\n};\n
    array_stack.py
    class ArrayStack:\n\"\"\"\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\"\"\"\ndef __init__(self) -> None:\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.__stack: list[int] = []\ndef size(self) -> int:\n\"\"\"\u83b7\u53d6\u6808\u7684\u957f\u5ea6\"\"\"\nreturn len(self.__stack)\ndef is_empty(self) -> bool:\n\"\"\"\u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\"\"\"\nreturn self.__stack == []\ndef push(self, item: int) -> None:\n\"\"\"\u5165\u6808\"\"\"\nself.__stack.append(item)\ndef pop(self) -> int:\n\"\"\"\u51fa\u6808\"\"\"\nif self.is_empty():\nraise IndexError(\"\u6808\u4e3a\u7a7a\")\nreturn self.__stack.pop()\ndef peek(self) -> int:\n\"\"\"\u8bbf\u95ee\u6808\u9876\u5143\u7d20\"\"\"\nif self.is_empty():\nraise IndexError(\"\u6808\u4e3a\u7a7a\")\nreturn self.__stack[-1]\ndef to_list(self) -> list[int]:\n\"\"\"\u8fd4\u56de\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\nreturn self.__stack\n
    array_stack.go
    /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\ntype arrayStack struct {\ndata []int // \u6570\u636e\n}\n/* \u521d\u59cb\u5316\u6808 */\nfunc newArrayStack() *arrayStack {\nreturn &arrayStack{\n// \u8bbe\u7f6e\u6808\u7684\u957f\u5ea6\u4e3a 0\uff0c\u5bb9\u91cf\u4e3a 16\ndata: make([]int, 0, 16),\n}\n}\n/* \u6808\u7684\u957f\u5ea6 */\nfunc (s *arrayStack) size() int {\nreturn len(s.data)\n}\n/* \u6808\u662f\u5426\u4e3a\u7a7a */\nfunc (s *arrayStack) isEmpty() bool {\nreturn s.size() == 0\n}\n/* \u5165\u6808 */\nfunc (s *arrayStack) push(v int) {\n// \u5207\u7247\u4f1a\u81ea\u52a8\u6269\u5bb9\ns.data = append(s.data, v)\n}\n/* \u51fa\u6808 */\nfunc (s *arrayStack) pop() any {\nval := s.peek()\ns.data = s.data[:len(s.data)-1]\nreturn val\n}\n/* \u83b7\u53d6\u6808\u9876\u5143\u7d20 */\nfunc (s *arrayStack) peek() any {\nif s.isEmpty() {\nreturn nil\n}\nval := s.data[len(s.data)-1]\nreturn val\n}\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (s *arrayStack) toSlice() []int {\nreturn s.data\n}\n
    array_stack.js
    /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n#stack;\nconstructor() {\nthis.#stack = [];\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nget size() {\nreturn this.#stack.length;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nempty() {\nreturn this.#stack.length === 0;\n}\n/* \u5165\u6808 */\npush(num) {\nthis.#stack.push(num);\n}\n/* \u51fa\u6808 */\npop() {\nif (this.empty()) throw new Error('\u6808\u4e3a\u7a7a');\nreturn this.#stack.pop();\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\ntop() {\nif (this.empty()) throw new Error('\u6808\u4e3a\u7a7a');\nreturn this.#stack[this.#stack.length - 1];\n}\n/* \u8fd4\u56de Array */\ntoArray() {\nreturn this.#stack;\n}\n}\n
    array_stack.ts
    /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\nprivate stack: number[];\nconstructor() {\nthis.stack = [];\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nget size(): number {\nreturn this.stack.length;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nempty(): boolean {\nreturn this.stack.length === 0;\n}\n/* \u5165\u6808 */\npush(num: number): void {\nthis.stack.push(num);\n}\n/* \u51fa\u6808 */\npop(): number | undefined {\nif (this.empty()) throw new Error('\u6808\u4e3a\u7a7a');\nreturn this.stack.pop();\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\ntop(): number | undefined {\nif (this.empty()) throw new Error('\u6808\u4e3a\u7a7a');\nreturn this.stack[this.stack.length - 1];\n}\n/* \u8fd4\u56de Array */\ntoArray() {\nreturn this.stack;\n}\n}\n
    array_stack.c
    /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nstruct arrayStack {\nint *data;\nint size;\n};\ntypedef struct arrayStack arrayStack;\n/* \u6784\u9020\u51fd\u6570 */\narrayStack *newArrayStack() {\narrayStack *s = malloc(sizeof(arrayStack));\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5927\u5bb9\u91cf\uff0c\u907f\u514d\u6269\u5bb9\ns->data = malloc(sizeof(int) * MAX_SIZE);\ns->size = 0;\nreturn s;\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size(arrayStack *s) {\nreturn s->size;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty(arrayStack *s) {\nreturn s->size == 0;\n}\n/* \u5165\u6808 */\nvoid push(arrayStack *s, int num) {\nif (s->size == MAX_SIZE) {\nprintf(\"stack is full.\\n\");\nreturn;\n}\ns->data[s->size] = num;\ns->size++;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek(arrayStack *s) {\nif (s->size == 0) {\nprintf(\"stack is empty.\\n\");\nreturn INT_MAX;\n}\nreturn s->data[s->size - 1];\n}\n/* \u51fa\u6808 */\nint pop(arrayStack *s) {\nif (s->size == 0) {\nprintf(\"stack is empty.\\n\");\nreturn INT_MAX;\n}\nint val = peek(s);\ns->size--;\nreturn val;\n}\n
    array_stack.cs
    /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\nprivate List<int> stack;\npublic ArrayStack() {\n// \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\nstack = new();\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\npublic int size() {\nreturn stack.Count();\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\npublic bool isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u6808 */\npublic void push(int num) {\nstack.Add(num);\n}\n/* \u51fa\u6808 */\npublic int pop() {\nif (isEmpty())\nthrow new Exception();\nvar val = peek();\nstack.RemoveAt(size() - 1);\nreturn val;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npublic int peek() {\nif (isEmpty())\nthrow new Exception();\nreturn stack[size() - 1];\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npublic int[] toArray() {\nreturn stack.ToArray();\n}\n}\n
    array_stack.swift
    /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\nprivate var stack: [Int]\ninit() {\n// \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\nstack = []\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nfunc size() -> Int {\nstack.count\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nfunc isEmpty() -> Bool {\nstack.isEmpty\n}\n/* \u5165\u6808 */\nfunc push(num: Int) {\nstack.append(num)\n}\n/* \u51fa\u6808 */\n@discardableResult\nfunc pop() -> Int {\nif isEmpty() {\nfatalError(\"\u6808\u4e3a\u7a7a\")\n}\nreturn stack.removeLast()\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nfunc peek() -> Int {\nif isEmpty() {\nfatalError(\"\u6808\u4e3a\u7a7a\")\n}\nreturn stack.last!\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\nfunc toArray() -> [Int] {\nstack\n}\n}\n
    array_stack.zig
    // \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\nfn ArrayStack(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nstack: ?std.ArrayList(T) = null,     // \u6784\u9020\u65b9\u6cd5\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u6808\uff09\npub fn init(self: *Self, allocator: std.mem.Allocator) void {\nif (self.stack == null) {\nself.stack = std.ArrayList(T).init(allocator);\n}\n}\n// \u6790\u6784\u65b9\u6cd5\uff08\u91ca\u653e\u5185\u5b58\uff09\npub fn deinit(self: *Self) void {\nif (self.stack == null) return;\nself.stack.?.deinit();\n}\n// \u83b7\u53d6\u6808\u7684\u957f\u5ea6\npub fn size(self: *Self) usize {\nreturn self.stack.?.items.len;\n}\n// \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\npub fn isEmpty(self: *Self) bool {\nreturn self.size() == 0;\n}\n// \u8bbf\u95ee\u6808\u9876\u5143\u7d20\npub fn peek(self: *Self) T {\nif (self.isEmpty()) @panic(\"\u6808\u4e3a\u7a7a\");\nreturn self.stack.?.items[self.size() - 1];\n}  // \u5165\u6808\npub fn push(self: *Self, num: T) !void {\ntry self.stack.?.append(num);\n} // \u51fa\u6808\npub fn pop(self: *Self) T {\nvar num = self.stack.?.pop();\nreturn num;\n} // \u8fd4\u56de ArrayList\npub fn toList(self: *Self) std.ArrayList(T) {\nreturn self.stack.?;\n}\n};\n}\n
    array_stack.dart
    /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\nlate List<int> _stack;\nArrayStack() {\n_stack = [];\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size() {\nreturn _stack.length;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty() {\nreturn _stack.isEmpty;\n}\n/* \u5165\u6808 */\nvoid push(int num) {\n_stack.add(num);\n}\n/* \u51fa\u6808 */\nint pop() {\nif (isEmpty()) {\nthrow Exception(\"\u6808\u4e3a\u7a7a\");\n}\nreturn _stack.removeLast();\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek() {\nif (isEmpty()) {\nthrow Exception(\"\u6808\u4e3a\u7a7a\");\n}\nreturn _stack.last;\n}\n/* \u5c06\u6808\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\nList<int> toArray() => _stack;\n}\n
    "},{"location":"chapter_stack_and_queue/stack/#513","title":"5.1.3. \u00a0 \u4e24\u79cd\u5b9e\u73b0\u5bf9\u6bd4","text":""},{"location":"chapter_stack_and_queue/stack/#_3","title":"\u652f\u6301\u64cd\u4f5c","text":"

    \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

    "},{"location":"chapter_stack_and_queue/stack/#_4","title":"\u65f6\u95f4\u6548\u7387","text":"

    \u5728\u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0\u4e2d\uff0c\u5165\u6808\u548c\u51fa\u6808\u64cd\u4f5c\u90fd\u662f\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\u94fe\u8868\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\uff08\u5982 int , double \uff09\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\uff1a

    • \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\u5728\u89e6\u53d1\u6269\u5bb9\u65f6\u6548\u7387\u4f1a\u964d\u4f4e\uff0c\u4f46\u7531\u4e8e\u6269\u5bb9\u662f\u4f4e\u9891\u64cd\u4f5c\uff0c\u56e0\u6b64\u5e73\u5747\u6548\u7387\u66f4\u9ad8\uff1b
    • \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\u53ef\u4ee5\u63d0\u4f9b\u66f4\u52a0\u7a33\u5b9a\u7684\u6548\u7387\u8868\u73b0\uff1b
    "},{"location":"chapter_stack_and_queue/stack/#_5","title":"\u7a7a\u95f4\u6548\u7387","text":"

    \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\u8fc7\u5b9e\u9645\u9700\u6c42\u3002\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\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\u4e2d\u7684\u540e\u9000\u4e0e\u524d\u8fdb\u3001\u8f6f\u4ef6\u4e2d\u7684\u64a4\u9500\u4e0e\u53cd\u64a4\u9500\u3002\u6bcf\u5f53\u6211\u4eec\u6253\u5f00\u65b0\u7684\u7f51\u9875\uff0c\u6d4f\u89c8\u5668\u5c31\u4f1a\u5c06\u4e0a\u4e00\u4e2a\u7f51\u9875\u6267\u884c\u5165\u6808\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7\u300c\u540e\u9000\u300d\u64cd\u4f5c\u56de\u5230\u4e0a\u4e00\u9875\u9762\u3002\u540e\u9000\u64cd\u4f5c\u5b9e\u9645\u4e0a\u662f\u5728\u6267\u884c\u51fa\u6808\u3002\u5982\u679c\u8981\u540c\u65f6\u652f\u6301\u540e\u9000\u548c\u524d\u8fdb\uff0c\u90a3\u4e48\u9700\u8981\u4e24\u4e2a\u6808\u6765\u914d\u5408\u5b9e\u73b0\u3002
    • \u7a0b\u5e8f\u5185\u5b58\u7ba1\u7406\u3002\u6bcf\u6b21\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u7cfb\u7edf\u90fd\u4f1a\u5728\u6808\u9876\u6dfb\u52a0\u4e00\u4e2a\u6808\u5e27\uff0c\u7528\u4e8e\u8bb0\u5f55\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002\u5728\u9012\u5f52\u51fd\u6570\u4e2d\uff0c\u5411\u4e0b\u9012\u63a8\u9636\u6bb5\u4f1a\u4e0d\u65ad\u6267\u884c\u5165\u6808\u64cd\u4f5c\uff0c\u800c\u5411\u4e0a\u56de\u6eaf\u9636\u6bb5\u5219\u4f1a\u6267\u884c\u51fa\u6808\u64cd\u4f5c\u3002
    "},{"location":"chapter_stack_and_queue/summary/","title":"5.4. \u00a0 \u5c0f\u7ed3","text":"
    • \u6808\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u540e\u51fa\u539f\u5219\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u901a\u8fc7\u6570\u7ec4\u6216\u94fe\u8868\u6765\u5b9e\u73b0\u3002
    • \u4ece\u65f6\u95f4\u6548\u7387\u89d2\u5ea6\u770b\uff0c\u6808\u7684\u6570\u7ec4\u5b9e\u73b0\u5177\u6709\u8f83\u9ad8\u7684\u5e73\u5747\u6548\u7387\uff0c\u4f46\u5728\u6269\u5bb9\u8fc7\u7a0b\u4e2d\uff0c\u5355\u6b21\u5165\u6808\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f1a\u964d\u4f4e\u81f3 \\(O(n)\\) \u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\u5177\u6709\u66f4\u4e3a\u7a33\u5b9a\u7684\u6548\u7387\u8868\u73b0\u3002
    • \u5728\u7a7a\u95f4\u6548\u7387\u65b9\u9762\uff0c\u6808\u7684\u6570\u7ec4\u5b9e\u73b0\u53ef\u80fd\u5bfc\u81f4\u4e00\u5b9a\u7a0b\u5ea6\u7684\u7a7a\u95f4\u6d6a\u8d39\u3002\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u94fe\u8868\u8282\u70b9\u6240\u5360\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u6bd4\u6570\u7ec4\u5143\u7d20\u66f4\u5927\u3002
    • \u961f\u5217\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u5148\u51fa\u539f\u5219\u7684\u6570\u636e\u7ed3\u6784\uff0c\u540c\u6837\u53ef\u4ee5\u901a\u8fc7\u6570\u7ec4\u6216\u94fe\u8868\u6765\u5b9e\u73b0\u3002\u5728\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u7684\u5bf9\u6bd4\u4e0a\uff0c\u961f\u5217\u7684\u7ed3\u8bba\u4e0e\u524d\u8ff0\u6808\u7684\u7ed3\u8bba\u76f8\u4f3c\u3002
    • \u53cc\u5411\u961f\u5217\u662f\u4e00\u79cd\u5177\u6709\u66f4\u9ad8\u81ea\u7531\u5ea6\u7684\u961f\u5217\uff0c\u5b83\u5141\u8bb8\u5728\u4e24\u7aef\u8fdb\u884c\u5143\u7d20\u7684\u6dfb\u52a0\u548c\u5220\u9664\u64cd\u4f5c\u3002
    "},{"location":"chapter_stack_and_queue/summary/#541-q-a","title":"5.4.1. \u00a0 Q & A","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\u5b9e\u73b0\u4e00\u4e9b\u989d\u5916\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u5728\u53cc\u5411\u961f\u5217\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\u8005\u662f\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

    "},{"location":"chapter_tree/","title":"7. \u00a0 \u6811","text":""},{"location":"chapter_tree/array_representation_of_tree/","title":"7.3. \u00a0 \u4e8c\u53c9\u6811\u6570\u7ec4\u8868\u793a","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\u5728\u4e0a\u8282\u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u5728\u94fe\u8868\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7684\u5404\u9879\u57fa\u672c\u64cd\u4f5c\u3002

    \u90a3\u4e48\uff0c\u80fd\u5426\u7528\u300c\u6570\u7ec4\u300d\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\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u5b8c\u7f8e\u4e8c\u53c9\u6811\uff0c\u6211\u4eec\u5c06\u8282\u70b9\u6309\u7167\u5c42\u5e8f\u904d\u5386\u7684\u987a\u5e8f\u7f16\u53f7\uff08\u4ece \\(0\\) \u5f00\u59cb\uff09\uff0c\u6b64\u65f6\u6bcf\u4e2a\u8282\u70b9\u90fd\u5bf9\u5e94\u552f\u4e00\u7684\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\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

    Fig. \u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a

    \u6620\u5c04\u516c\u5f0f\u7684\u4f5c\u7528\u76f8\u5f53\u4e8e\u94fe\u8868\u4e2d\u7684\u6307\u9488\u3002\u5982\u679c\u6211\u4eec\u5c06\u8282\u70b9\u6309\u7167\u5c42\u5e8f\u904d\u5386\u7684\u987a\u5e8f\u5b58\u50a8\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\uff0c\u90a3\u4e48\u5bf9\u4e8e\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u8282\u70b9\uff0c\u6211\u4eec\u90fd\u53ef\u4ee5\u901a\u8fc7\u6620\u5c04\u516c\u5f0f\u6765\u8bbf\u95ee\u5176\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":"

    \u7136\u800c\uff0c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u53ea\u662f\u4e00\u4e2a\u7279\u4f8b\u3002\u5728\u4e8c\u53c9\u6811\u7684\u4e2d\u95f4\u5c42\uff0c\u901a\u5e38\u5b58\u5728\u8bb8\u591a \\(\\text{null}\\) \uff0c\u800c\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5e76\u4e0d\u5305\u542b\u8fd9\u4e9b \\(\\text{null}\\) \u3002\u6211\u4eec\u65e0\u6cd5\u4ec5\u51ed\u8be5\u5e8f\u5217\u6765\u63a8\u6d4b \\(\\text{null}\\) \u7684\u6570\u91cf\u548c\u5206\u5e03\u4f4d\u7f6e\uff0c\u8fd9\u610f\u5473\u7740\u5b58\u5728\u591a\u79cd\u4e8c\u53c9\u6811\u7ed3\u6784\u90fd\u7b26\u5408\u8be5\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u3002\u663e\u7136\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e0a\u8ff0\u7684\u6570\u7ec4\u8868\u793a\u65b9\u6cd5\u5df2\u7ecf\u5931\u6548\u3002

    Fig. \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{null}\\)\u3002\u5982\u4e0b\u56fe\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    /* \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 \u6700\u5927\u503c\u6807\u8bb0\u7a7a\u4f4d\uff0c\u56e0\u6b64\u8981\u6c42\u8282\u70b9\u503c\u4e0d\u80fd\u4e3a INT_MAX\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# \u76f4\u63a5\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 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// \u76f4\u63a5\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// \u76f4\u63a5\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 \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
    /* \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 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
    \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

    Fig. \u4efb\u610f\u7c7b\u578b\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a

    "},{"location":"chapter_tree/array_representation_of_tree/#733","title":"7.3.3. \u00a0 \u4f18\u52bf\u4e0e\u5c40\u9650\u6027","text":"

    \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a\u5b58\u5728\u4ee5\u4e0b\u4f18\u70b9\uff1a

    • \u6570\u7ec4\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\uff0c\u7f13\u5b58\u53cb\u597d\uff0c\u8bbf\u95ee\u4e0e\u904d\u5386\u901f\u5ea6\u8f83\u5feb\uff1b
    • \u4e0d\u9700\u8981\u5b58\u50a8\u6307\u9488\uff0c\u6bd4\u8f83\u8282\u7701\u7a7a\u95f4\uff1b
    • \u5141\u8bb8\u968f\u673a\u8bbf\u95ee\u8282\u70b9\uff1b

    \u7136\u800c\uff0c\u6570\u7ec4\u8868\u793a\u4e5f\u5177\u6709\u4e00\u4e9b\u5c40\u9650\u6027\uff1a

    • \u6570\u7ec4\u5b58\u50a8\u9700\u8981\u8fde\u7eed\u5185\u5b58\u7a7a\u95f4\uff0c\u56e0\u6b64\u4e0d\u9002\u5408\u5b58\u50a8\u6570\u636e\u91cf\u8fc7\u5927\u7684\u6811\u3002
    • \u589e\u5220\u8282\u70b9\u9700\u8981\u901a\u8fc7\u6570\u7ec4\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u5b9e\u73b0\uff0c\u6548\u7387\u8f83\u4f4e\uff1b
    • \u5f53\u4e8c\u53c9\u6811\u4e2d\u5b58\u5728\u5927\u91cf \\(\\text{null}\\) \u65f6\uff0c\u6570\u7ec4\u4e2d\u5305\u542b\u7684\u8282\u70b9\u6570\u636e\u6bd4\u91cd\u8f83\u4f4e\uff0c\u7a7a\u95f4\u5229\u7528\u7387\u8f83\u4f4e\u3002

    \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{null}\\) \u53ea\u51fa\u73b0\u5728\u6700\u5e95\u5c42\u4e14\u9760\u53f3\u7684\u4f4d\u7f6e\uff0c\u8fd9\u610f\u5473\u7740\u6240\u6709 \\(\\text{null}\\) \u4e00\u5b9a\u51fa\u73b0\u5728\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u7684\u672b\u5c3e\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528\u6570\u7ec4\u8868\u793a\u5b8c\u5168\u4e8c\u53c9\u6811\u65f6\uff0c\u53ef\u4ee5\u7701\u7565\u5b58\u50a8\u6240\u6709 \\(\\text{null}\\) \u3002

    Fig. \u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a

    "},{"location":"chapter_tree/avl_tree/","title":"7.5. \u00a0 AVL \u6811 *","text":"

    \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u63d0\u5230\u4e86\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\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\u4e0b\u56fe\u6240\u793a\uff0c\u7ecf\u8fc7\u4e24\u6b21\u5220\u9664\u8282\u70b9\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u4e8c\u53c9\u641c\u7d22\u6811\u4fbf\u4f1a\u9000\u5316\u4e3a\u94fe\u8868\u3002

    Fig. AVL \u6811\u5728\u5220\u9664\u8282\u70b9\u540e\u53d1\u751f\u9000\u5316

    \u518d\u4f8b\u5982\uff0c\u5728\u4ee5\u4e0b\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

    Fig. AVL \u6811\u5728\u63d2\u5165\u8282\u70b9\u540e\u53d1\u751f\u9000\u5316

    G. M. Adelson-Velsky \u548c E. M. Landis \u5728\u5176 1962 \u5e74\u53d1\u8868\u7684\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":"

    \u300cAVL \u6811\u300d\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\u300d\u3002

    "},{"location":"chapter_tree/avl_tree/#_1","title":"\u8282\u70b9\u9ad8\u5ea6","text":"

    \u5728\u64cd\u4f5c AVL \u6811\u65f6\uff0c\u6211\u4eec\u9700\u8981\u83b7\u53d6\u8282\u70b9\u7684\u9ad8\u5ea6\uff0c\u56e0\u6b64\u9700\u8981\u4e3a AVL \u6811\u7684\u8282\u70b9\u7c7b\u6dfb\u52a0 height \u53d8\u91cf\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\npublic int val;        // \u8282\u70b9\u503c\npublic int height;     // \u8282\u70b9\u9ad8\u5ea6\npublic TreeNode left;  // \u5de6\u5b50\u8282\u70b9\npublic TreeNode right; // \u53f3\u5b50\u8282\u70b9\npublic TreeNode(int x) { val = x; }\n}\n
    /* AVL \u6811\u8282\u70b9\u7c7b */\nstruct TreeNode {\nint val{};          // \u8282\u70b9\u503c\nint height = 0;     // \u8282\u70b9\u9ad8\u5ea6\nTreeNode *left{};   // \u5de6\u5b50\u8282\u70b9\nTreeNode *right{};  // \u53f3\u5b50\u8282\u70b9\nTreeNode() = default;\nexplicit TreeNode(int x) : val(x){}\n};\n
    class TreeNode:\n\"\"\"AVL \u6811\u8282\u70b9\u7c7b\"\"\"\ndef __init__(self, val: int):\nself.val: int = val                    # \u8282\u70b9\u503c\nself.height: int = 0                   # \u8282\u70b9\u9ad8\u5ea6\nself.left: Optional[TreeNode] = None   # \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nself.right: Optional[TreeNode] = None  # \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n
    /* AVL \u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype TreeNode struct {\nVal    int       // \u8282\u70b9\u503c\nHeight int       // \u8282\u70b9\u9ad8\u5ea6\nLeft   *TreeNode // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nRight  *TreeNode // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\n
    /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nval; // \u8282\u70b9\u503c\nheight; //\u8282\u70b9\u9ad8\u5ea6\nleft; // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nright; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\nconstructor(val, left, right, height) {\nthis.val = val === undefined ? 0 : val;\nthis.height = height === undefined ? 0 : height;\nthis.left = left === undefined ? null : left;\nthis.right = right === undefined ? null : right;\n}\n}\n
    /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nval: number;            // \u8282\u70b9\u503c\nheight: number;         // \u8282\u70b9\u9ad8\u5ea6\nleft: TreeNode | null;  // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nright: TreeNode | null; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\nconstructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {\nthis.val = val === undefined ? 0 : val;\nthis.height = height === undefined ? 0 : height; this.left = left === undefined ? null : left; this.right = right === undefined ? null : right; }\n}\n
    /* AVL \u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\nint val;\nint height;\nstruct TreeNode *left;\nstruct TreeNode *right;\n};\ntypedef struct TreeNode TreeNode;\n/* \u6784\u9020\u51fd\u6570 */\nTreeNode *newTreeNode(int val) {\nTreeNode *node;\nnode = (TreeNode *)malloc(sizeof(TreeNode));\nnode->val = val;\nnode->height = 0;\nnode->left = NULL;\nnode->right = NULL;\nreturn node;\n}\n
    /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\npublic int val;          // \u8282\u70b9\u503c\npublic int height;       // \u8282\u70b9\u9ad8\u5ea6\npublic TreeNode? left;   // \u5de6\u5b50\u8282\u70b9\npublic TreeNode? right;  // \u53f3\u5b50\u8282\u70b9\npublic TreeNode(int x) { val = x; }\n}\n
    /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nvar val: Int // \u8282\u70b9\u503c\nvar height: Int // \u8282\u70b9\u9ad8\u5ea6\nvar left: TreeNode? // \u5de6\u5b50\u8282\u70b9\nvar right: TreeNode? // \u53f3\u5b50\u8282\u70b9\ninit(x: Int) {\nval = x\nheight = 0\n}\n}\n
    \n
    /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nint val;         // \u8282\u70b9\u503c\nint height;      // \u8282\u70b9\u9ad8\u5ea6\nTreeNode? left;  // \u5de6\u5b50\u8282\u70b9\nTreeNode? right; // \u53f3\u5b50\u8282\u70b9\nTreeNode(this.val, [this.height = 0, this.left, this.right]);\n}\n

    \u300c\u8282\u70b9\u9ad8\u5ea6\u300d\u662f\u6307\u4ece\u8be5\u8282\u70b9\u5230\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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nreturn node == null ? -1 : node.height;\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\nnode.height = Math.max(height(node.left), height(node.right)) + 1;\n}\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\nreturn node == nullptr ? -1 : node->height;\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\nnode->height = max(height(node->left), height(node->right)) + 1;\n}\n
    avl_tree.py
    def 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\nif node is not None:\nreturn node.height\nreturn -1\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\nnode.height = max([self.height(node.left), self.height(node.right)]) + 1\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\nif node != nil {\nreturn node.Height\n}\nreturn -1\n}\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfunc (t *aVLTree) updateHeight(node *TreeNode) {\nlh := t.height(node.Left)\nrh := t.height(node.Right)\n// \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nif lh > rh {\nnode.Height = lh + 1\n} else {\nnode.Height = rh + 1\n}\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\nreturn node === null ? -1 : node.height;\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\nnode.height =\nMath.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\nreturn node === null ? -1 : node.height;\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\nnode.height =\nMath.max(this.height(node.left), this.height(node.right)) + 1;\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\nif (node != NULL) {\nreturn node->height;\n}\nreturn -1;\n}\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode *node) {\nint lh = height(node->left);\nint rh = height(node->right);\n// \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nif (lh > rh) {\nnode->height = lh + 1;\n} else {\nnode->height = rh + 1;\n}\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\nreturn node == null ? -1 : node.height;\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\nnode.height = Math.Max(height(node.left), height(node.right)) + 1;\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\nnode == nil ? -1 : node!.height\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\nnode?.height = max(height(node: node?.left), height(node: node?.right)) + 1\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\nreturn if (node == null) -1 else node.?.height;\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\nnode.?.height = std.math.max(self.height(node.?.left), self.height(node.?.right)) + 1;\n}\n
    avl_tree.dart
    /* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode? node) {\nreturn node == null ? -1 : node.height;\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\nnode!.height = max(height(node.left), height(node.right)) + 1;\n}\n
    "},{"location":"chapter_tree/avl_tree/#_2","title":"\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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart avl_tree.java
    /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode node) {\n// \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nif (node == null)\nreturn 0;\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn height(node.left) - height(node.right);\n}\n
    avl_tree.cpp
    /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode *node) {\n// \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nif (node == nullptr)\nreturn 0;\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn height(node->left) - height(node->right);\n}\n
    avl_tree.py
    def balance_factor(self, node: TreeNode | None) -> int:\n\"\"\"\u83b7\u53d6\u5e73\u8861\u56e0\u5b50\"\"\"\n# \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nif node is None:\nreturn 0\n# \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn self.height(node.left) - self.height(node.right)\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\nif node == nil {\nreturn 0\n}\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn t.height(node.Left) - t.height(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\nif (node === null) return 0;\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn 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\nif (node === null) return 0;\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn this.height(node.left) - this.height(node.right);\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\nif (node == NULL) {\nreturn 0;\n}\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn 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\nif (node == null) return 0;\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn height(node.left) - 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\nguard let node = node else { return 0 }\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn height(node: node.left) - height(node: 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\nif (node == null) return 0;\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn self.height(node.?.left) - self.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\nif (node == null) return 0;\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn height(node.left) - 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\u300c\u65cb\u8f6c Rotation\u300d\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\u6811\u7684\u300c\u4e8c\u53c9\u641c\u7d22\u6811\u300d\u5c5e\u6027\uff0c\u4e5f\u80fd\u4f7f\u6811\u91cd\u65b0\u53d8\u4e3a\u300c\u5e73\u8861\u4e8c\u53c9\u6811\u300d\u3002

    \u6211\u4eec\u5c06\u5e73\u8861\u56e0\u5b50\u7edd\u5bf9\u503c \\(> 1\\) \u7684\u8282\u70b9\u79f0\u4e3a\u300c\u5931\u8861\u8282\u70b9\u300d\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\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65cb\u8f6c\u64cd\u4f5c\u3002

    "},{"location":"chapter_tree/avl_tree/#_3","title":"\u53f3\u65cb","text":"

    \u5982\u4e0b\u56fe\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\u300c\u53f3\u65cb\u300d\u64cd\u4f5c\u3002\u5b8c\u6210\u53f3\u65cb\u540e\uff0c\u5b50\u6811\u5df2\u7ecf\u6062\u590d\u5e73\u8861\uff0c\u5e76\u4e14\u4ecd\u7136\u4fdd\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u7279\u6027\u3002

    <1><2><3><4>

    \u6b64\u5916\uff0c\u5982\u679c\u8282\u70b9 child \u672c\u8eab\u6709\u53f3\u5b50\u8282\u70b9\uff08\u8bb0\u4e3a grandChild \uff09\uff0c\u5219\u9700\u8981\u5728\u300c\u53f3\u65cb\u300d\u4e2d\u6dfb\u52a0\u4e00\u6b65\uff1a\u5c06 grandChild \u4f5c\u4e3a node \u7684\u5de6\u5b50\u8282\u70b9\u3002

    Fig. \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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart avl_tree.java
    /* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode rightRotate(TreeNode node) {\nTreeNode child = node.left;\nTreeNode grandChild = child.right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.right = node;\nnode.left = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.cpp
    /* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode *rightRotate(TreeNode *node) {\nTreeNode *child = node->left;\nTreeNode *grandChild = child->right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild->right = node;\nnode->left = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.py
    def __right_rotate(self, node: TreeNode | None) -> TreeNode | None:\n\"\"\"\u53f3\u65cb\u64cd\u4f5c\"\"\"\nchild = node.left\ngrand_child = child.right\n# \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.right = node\nnode.left = grand_child\n# \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nself.__update_height(node)\nself.__update_height(child)\n# \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child\n
    avl_tree.go
    /* \u53f3\u65cb\u64cd\u4f5c */\nfunc (t *aVLTree) rightRotate(node *TreeNode) *TreeNode {\nchild := node.Left\ngrandChild := child.Right\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.Right = node\nnode.Left = grandChild\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nt.updateHeight(node)\nt.updateHeight(child)\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child\n}\n
    avl_tree.js
    /* \u53f3\u65cb\u64cd\u4f5c */\n#rightRotate(node) {\nconst child = node.left;\nconst grandChild = child.right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.right = node;\nnode.left = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nthis.#updateHeight(node);\nthis.#updateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.ts
    /* \u53f3\u65cb\u64cd\u4f5c */\nrightRotate(node: TreeNode): TreeNode {\nconst child = node.left;\nconst grandChild = child.right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.right = node;\nnode.left = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nthis.updateHeight(node);\nthis.updateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.c
    /* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode *rightRotate(TreeNode *node) {\nTreeNode *child, *grandChild;\nchild = node->left;\ngrandChild = child->right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild->right = node;\nnode->left = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.cs
    /* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode? rightRotate(TreeNode? node) {\nTreeNode? child = node.left;\nTreeNode? grandChild = child?.right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.right = node;\nnode.left = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.swift
    /* \u53f3\u65cb\u64cd\u4f5c */\nfunc rightRotate(node: TreeNode?) -> TreeNode? {\nlet child = node?.left\nlet grandChild = child?.right\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild?.right = node\nnode?.left = grandChild\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node: node)\nupdateHeight(node: child)\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child\n}\n
    avl_tree.zig
    // \u53f3\u65cb\u64cd\u4f5c\nfn rightRotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\nvar child = node.?.left;\nvar grandChild = child.?.right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.?.right = node;\nnode.?.left = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nself.updateHeight(node);\nself.updateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.dart
    /* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode? rightRotate(TreeNode? node) {\nTreeNode? child = node!.left;\nTreeNode? grandChild = child!.right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.right = node;\nnode.left = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    "},{"location":"chapter_tree/avl_tree/#_4","title":"\u5de6\u65cb","text":"

    \u76f8\u5e94\u7684\uff0c\u5982\u679c\u8003\u8651\u4e0a\u8ff0\u5931\u8861\u4e8c\u53c9\u6811\u7684\u201c\u955c\u50cf\u201d\uff0c\u5219\u9700\u8981\u6267\u884c\u300c\u5de6\u65cb\u300d\u64cd\u4f5c\u3002

    Fig. \u5de6\u65cb\u64cd\u4f5c

    \u540c\u7406\uff0c\u82e5\u8282\u70b9 child \u672c\u8eab\u6709\u5de6\u5b50\u8282\u70b9\uff08\u8bb0\u4e3a grandChild \uff09\uff0c\u5219\u9700\u8981\u5728\u300c\u5de6\u65cb\u300d\u4e2d\u6dfb\u52a0\u4e00\u6b65\uff1a\u5c06 grandChild \u4f5c\u4e3a node \u7684\u53f3\u5b50\u8282\u70b9\u3002

    Fig. \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\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece\u53f3\u65cb\u7684\u4ee3\u7801\u63a8\u5bfc\u51fa\u5de6\u65cb\u7684\u4ee3\u7801\u3002\u5177\u4f53\u5730\uff0c\u53ea\u9700\u5c06\u300c\u53f3\u65cb\u300d\u4ee3\u7801\u4e2d\u7684\u628a\u6240\u6709\u7684 left \u66ff\u6362\u4e3a right \uff0c\u5c06\u6240\u6709\u7684 right \u66ff\u6362\u4e3a left \uff0c\u5373\u53ef\u5f97\u5230\u300c\u5de6\u65cb\u300d\u4ee3\u7801\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart avl_tree.java
    /* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode leftRotate(TreeNode node) {\nTreeNode child = node.right;\nTreeNode grandChild = child.left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.left = node;\nnode.right = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.cpp
    /* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode *leftRotate(TreeNode *node) {\nTreeNode *child = node->right;\nTreeNode *grandChild = child->left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild->left = node;\nnode->right = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.py
    def __left_rotate(self, node: TreeNode | None) -> TreeNode | None:\n\"\"\"\u5de6\u65cb\u64cd\u4f5c\"\"\"\nchild = node.right\ngrand_child = child.left\n# \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.left = node\nnode.right = grand_child\n# \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nself.__update_height(node)\nself.__update_height(child)\n# \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child\n
    avl_tree.go
    /* \u5de6\u65cb\u64cd\u4f5c */\nfunc (t *aVLTree) leftRotate(node *TreeNode) *TreeNode {\nchild := node.Right\ngrandChild := child.Left\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.Left = node\nnode.Right = grandChild\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nt.updateHeight(node)\nt.updateHeight(child)\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child\n}\n
    avl_tree.js
    /* \u5de6\u65cb\u64cd\u4f5c */\n#leftRotate(node) {\nconst child = node.right;\nconst grandChild = child.left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.left = node;\nnode.right = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nthis.#updateHeight(node);\nthis.#updateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.ts
    /* \u5de6\u65cb\u64cd\u4f5c */\nleftRotate(node: TreeNode): TreeNode {\nconst child = node.right;\nconst grandChild = child.left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.left = node;\nnode.right = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nthis.updateHeight(node);\nthis.updateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.c
    /* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode *leftRotate(TreeNode *node) {\nTreeNode *child, *grandChild;\nchild = node->right;\ngrandChild = child->left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild->left = node;\nnode->right = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.cs
    /* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode? leftRotate(TreeNode? node) {\nTreeNode? child = node.right;\nTreeNode? grandChild = child?.left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.left = node;\nnode.right = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.swift
    /* \u5de6\u65cb\u64cd\u4f5c */\nfunc leftRotate(node: TreeNode?) -> TreeNode? {\nlet child = node?.right\nlet grandChild = child?.left\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild?.left = node\nnode?.right = grandChild\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node: node)\nupdateHeight(node: child)\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child\n}\n
    avl_tree.zig
    // \u5de6\u65cb\u64cd\u4f5c\nfn leftRotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\nvar child = node.?.right;\nvar grandChild = child.?.left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.?.left = node;\nnode.?.right = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nself.updateHeight(node);\nself.updateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.dart
    /* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode? leftRotate(TreeNode? node) {\nTreeNode? child = node!.right;\nTreeNode? grandChild = child!.left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.left = node;\nnode.right = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    "},{"location":"chapter_tree/avl_tree/#_5","title":"\u5148\u5de6\u65cb\u540e\u53f3\u65cb","text":"

    \u5bf9\u4e8e\u4e0b\u56fe\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\u5de6\u65cb\u540e\u53f3\u65cb\uff0c\u5373\u5148\u5bf9 child \u6267\u884c\u300c\u5de6\u65cb\u300d\uff0c\u518d\u5bf9 node \u6267\u884c\u300c\u53f3\u65cb\u300d\u3002

    Fig. \u5148\u5de6\u65cb\u540e\u53f3\u65cb

    "},{"location":"chapter_tree/avl_tree/#_6","title":"\u5148\u53f3\u65cb\u540e\u5de6\u65cb","text":"

    \u540c\u7406\uff0c\u5bf9\u4e8e\u4e0a\u8ff0\u5931\u8861\u4e8c\u53c9\u6811\u7684\u955c\u50cf\u60c5\u51b5\uff0c\u9700\u8981\u5148\u53f3\u65cb\u540e\u5de6\u65cb\uff0c\u5373\u5148\u5bf9 child \u6267\u884c\u300c\u53f3\u65cb\u300d\uff0c\u7136\u540e\u5bf9 node \u6267\u884c\u300c\u5de6\u65cb\u300d\u3002

    Fig. \u5148\u53f3\u65cb\u540e\u5de6\u65cb

    "},{"location":"chapter_tree/avl_tree/#_7","title":"\u65cb\u8f6c\u7684\u9009\u62e9","text":"

    \u4e0b\u56fe\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\u5de6\u65cb\u3001\u5148\u53f3\u540e\u5de6\u3001\u5148\u5de6\u540e\u53f3\u7684\u65cb\u8f6c\u64cd\u4f5c\u3002

    Fig. AVL \u6811\u7684\u56db\u79cd\u65cb\u8f6c\u60c5\u51b5

    \u5728\u4ee3\u7801\u4e2d\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\u4e0a\u56fe\u4e2d\u7684\u54ea\u79cd\u60c5\u51b5\u3002

    \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 \\(>0\\) \uff08\u5373\u5de6\u504f\u6811\uff09 \\(\\geq 0\\) \u53f3\u65cb \\(>0\\) \uff08\u5373\u5de6\u504f\u6811\uff09 \\(<0\\) \u5148\u5de6\u65cb\u540e\u53f3\u65cb \\(<0\\) \uff08\u5373\u53f3\u504f\u6811\uff09 \\(\\leq 0\\) \u5de6\u65cb \\(<0\\) \uff08\u5373\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nint balanceFactor = balanceFactor(node);\n// \u5de6\u504f\u6811\nif (balanceFactor > 1) {\nif (balanceFactor(node.left) >= 0) {\n// \u53f3\u65cb\nreturn rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.left = leftRotate(node.left);\nreturn rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (balanceFactor < -1) {\nif (balanceFactor(node.right) <= 0) {\n// \u5de6\u65cb\nreturn leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.right = rightRotate(node.right);\nreturn leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\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\nint _balanceFactor = balanceFactor(node);\n// \u5de6\u504f\u6811\nif (_balanceFactor > 1) {\nif (balanceFactor(node->left) >= 0) {\n// \u53f3\u65cb\nreturn rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode->left = leftRotate(node->left);\nreturn rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (_balanceFactor < -1) {\nif (balanceFactor(node->right) <= 0) {\n// \u5de6\u65cb\nreturn leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode->right = rightRotate(node->right);\nreturn leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\n
    avl_tree.py
    def __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\nbalance_factor = self.balance_factor(node)\n# \u5de6\u504f\u6811\nif balance_factor > 1:\nif self.balance_factor(node.left) >= 0:\n# \u53f3\u65cb\nreturn self.__right_rotate(node)\nelse:\n# \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.left = self.__left_rotate(node.left)\nreturn self.__right_rotate(node)\n# \u53f3\u504f\u6811\nelif balance_factor < -1:\nif self.balance_factor(node.right) <= 0:\n# \u5de6\u65cb\nreturn self.__left_rotate(node)\nelse:\n# \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.right = self.__right_rotate(node.right)\nreturn self.__left_rotate(node)\n# \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node\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\nbf := t.balanceFactor(node)\n// \u5de6\u504f\u6811\nif bf > 1 {\nif t.balanceFactor(node.Left) >= 0 {\n// \u53f3\u65cb\nreturn t.rightRotate(node)\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.Left = t.leftRotate(node.Left)\nreturn t.rightRotate(node)\n}\n}\n// \u53f3\u504f\u6811\nif bf < -1 {\nif t.balanceFactor(node.Right) <= 0 {\n// \u5de6\u65cb\nreturn t.leftRotate(node)\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.Right = t.rightRotate(node.Right)\nreturn t.leftRotate(node)\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn 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\nconst balanceFactor = this.balanceFactor(node);\n// \u5de6\u504f\u6811\nif (balanceFactor > 1) {\nif (this.balanceFactor(node.left) >= 0) {\n// \u53f3\u65cb\nreturn this.#rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.left = this.#leftRotate(node.left);\nreturn this.#rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (balanceFactor < -1) {\nif (this.balanceFactor(node.right) <= 0) {\n// \u5de6\u65cb\nreturn this.#leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.right = this.#rightRotate(node.right);\nreturn this.#leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn 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\nconst balanceFactor = this.balanceFactor(node);\n// \u5de6\u504f\u6811\nif (balanceFactor > 1) {\nif (this.balanceFactor(node.left) >= 0) {\n// \u53f3\u65cb\nreturn this.rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.left = this.leftRotate(node.left);\nreturn this.rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (balanceFactor < -1) {\nif (this.balanceFactor(node.right) <= 0) {\n// \u5de6\u65cb\nreturn this.leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.right = this.rightRotate(node.right);\nreturn this.leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\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\nint bf = balanceFactor(node);\n// \u5de6\u504f\u6811\nif (bf > 1) {\nif (balanceFactor(node->left) >= 0) {\n// \u53f3\u65cb\nreturn rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode->left = leftRotate(node->left);\nreturn rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (bf < -1) {\nif (balanceFactor(node->right) <= 0) {\n// \u5de6\u65cb\nreturn leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode->right = rightRotate(node->right);\nreturn leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn 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\nint balanceFactorInt = balanceFactor(node);\n// \u5de6\u504f\u6811\nif (balanceFactorInt > 1) {\nif (balanceFactor(node.left) >= 0) {\n// \u53f3\u65cb\nreturn rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.left = leftRotate(node?.left);\nreturn rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (balanceFactorInt < -1) {\nif (balanceFactor(node.right) <= 0) {\n// \u5de6\u65cb\nreturn leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.right = rightRotate(node?.right);\nreturn leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn 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\nlet balanceFactor = balanceFactor(node: node)\n// \u5de6\u504f\u6811\nif balanceFactor > 1 {\nif self.balanceFactor(node: node?.left) >= 0 {\n// \u53f3\u65cb\nreturn rightRotate(node: node)\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode?.left = leftRotate(node: node?.left)\nreturn rightRotate(node: node)\n}\n}\n// \u53f3\u504f\u6811\nif balanceFactor < -1 {\nif self.balanceFactor(node: node?.right) <= 0 {\n// \u5de6\u65cb\nreturn leftRotate(node: node)\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode?.right = rightRotate(node: node?.right)\nreturn leftRotate(node: node)\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn 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\nvar balance_factor = self.balanceFactor(node);\n// \u5de6\u504f\u6811\nif (balance_factor > 1) {\nif (self.balanceFactor(node.?.left) >= 0) {\n// \u53f3\u65cb\nreturn self.rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.?.left = self.leftRotate(node.?.left);\nreturn self.rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (balance_factor < -1) {\nif (self.balanceFactor(node.?.right) <= 0) {\n// \u5de6\u65cb\nreturn self.leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.?.right = self.rightRotate(node.?.right);\nreturn self.leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn 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\nint factor = balanceFactor(node);\n// \u5de6\u504f\u6811\nif (factor > 1) {\nif (balanceFactor(node!.left) >= 0) {\n// \u53f3\u65cb\nreturn rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.left = leftRotate(node.left);\nreturn rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (factor < -1) {\nif (balanceFactor(node!.right) <= 0) {\n// \u5de6\u65cb\nreturn leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.right = rightRotate(node.right);\nreturn leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn 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/#_8","title":"\u63d2\u5165\u8282\u70b9","text":"

    \u300cAVL \u6811\u300d\u7684\u8282\u70b9\u63d2\u5165\u64cd\u4f5c\u4e0e\u300c\u4e8c\u53c9\u641c\u7d22\u6811\u300d\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart avl_tree.java
    /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\nroot = insertHelper(root, val);\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode insertHelper(TreeNode node, int val) {\nif (node == null)\nreturn new TreeNode(val);\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif (val < node.val)\nnode.left = insertHelper(node.left, val);\nelse if (val > node.val)\nnode.right = insertHelper(node.right, val);\nelse\nreturn node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nupdateHeight(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 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.cpp
    /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\nroot = insertHelper(root, val);\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode *insertHelper(TreeNode *node, int val) {\nif (node == nullptr)\nreturn new TreeNode(val);\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif (val < node->val)\nnode->left = insertHelper(node->left, val);\nelse if (val > node->val)\nnode->right = insertHelper(node->right, val);\nelse\nreturn node;    // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nupdateHeight(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 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.py
    def insert(self, val) -> None:\n\"\"\"\u63d2\u5165\u8282\u70b9\"\"\"\nself.__root = self.__insert_helper(self.__root, val)\ndef __insert_helper(self, node: TreeNode | None, val: int) -> TreeNode:\n\"\"\"\u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\"\"\"\nif node is None:\nreturn TreeNode(val)\n# 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9\nif val < node.val:\nnode.left = self.__insert_helper(node.left, val)\nelif val > node.val:\nnode.right = self.__insert_helper(node.right, val)\nelse:\n# \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node\n# \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nself.__update_height(node)\n# 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\nreturn self.__rotate(node)\n
    avl_tree.go
    /* \u63d2\u5165\u8282\u70b9 */\nfunc (t *aVLTree) insert(val int) {\nt.root = t.insertHelper(t.root, val)\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc (t *aVLTree) insertHelper(node *TreeNode, val int) *TreeNode {\nif node == nil {\nreturn NewTreeNode(val)\n}\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif val < node.Val {\nnode.Left = t.insertHelper(node.Left, val)\n} else if val > node.Val {\nnode.Right = t.insertHelper(node.Right, val)\n} else {\n// \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node\n}\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nt.updateHeight(node)\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = t.rotate(node)\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node\n}\n
    avl_tree.js
    /* \u63d2\u5165\u8282\u70b9 */\ninsert(val) {\nthis.root = this.#insertHelper(this.root, val);\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\n#insertHelper(node, val) {\nif (node === null) return new TreeNode(val);\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif (val < node.val) node.left = this.#insertHelper(node.left, val);\nelse if (val > node.val)\nnode.right = this.#insertHelper(node.right, val);\nelse return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nthis.#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 */\nnode = this.#rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.ts
    /* \u63d2\u5165\u8282\u70b9 */\ninsert(val: number): void {\nthis.root = this.insertHelper(this.root, val);\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\ninsertHelper(node: TreeNode, val: number): TreeNode {\nif (node === null) return new TreeNode(val);\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif (val < node.val) {\nnode.left = this.insertHelper(node.left, val);\n} else if (val > node.val) {\nnode.right = this.insertHelper(node.right, val);\n} else {\nreturn node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n}\nthis.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 */\nnode = this.rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.c
    /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(aVLTree *tree, int val) {\ntree->root = insertHelper(tree->root, val);\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode *insertHelper(TreeNode *node, int val) {\nif (node == NULL) {\nreturn newTreeNode(val);\n}\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif (val < node->val) {\nnode->left = insertHelper(node->left, val);\n} else if (val > node->val) {\nnode->right = insertHelper(node->right, val);\n} else {\n// \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.cs
    /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\nroot = insertHelper(root, val);\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? insertHelper(TreeNode? node, int val) {\nif (node == null) return new TreeNode(val);\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif (val < node.val)\nnode.left = insertHelper(node.left, val);\nelse if (val > node.val)\nnode.right = insertHelper(node.right, val);\nelse\nreturn node;     // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nupdateHeight(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 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.swift
    /* \u63d2\u5165\u8282\u70b9 */\n@discardableResult\nfunc insert(val: Int) {\nroot = insertHelper(node: root, val: val)\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc insertHelper(node: TreeNode?, val: Int) -> TreeNode? {\nvar node = node\nif node == nil {\nreturn TreeNode(x: val)\n}\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif val < node!.val {\nnode?.left = insertHelper(node: node?.left, val: val)\n} else if val > node!.val {\nnode?.right = insertHelper(node: node?.right, val: val)\n} else {\nreturn node // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n}\nupdateHeight(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 */\nnode = rotate(node: node)\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node\n}\n
    avl_tree.zig
    // \u63d2\u5165\u8282\u70b9\nfn insert(self: *Self, val: T) void {\nself.root = try self.insertHelper(self.root, val);\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) {\nvar node = node_;\nif (node == null) {\nvar tmp_node = try self.mem_allocator.create(inc.TreeNode(T));\ntmp_node.init(val);\nreturn tmp_node;\n}\n// 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9\nif (val < node.?.val) {\nnode.?.left = try self.insertHelper(node.?.left, val);\n} else if (val > node.?.val) {\nnode.?.right = try self.insertHelper(node.?.right, val);\n} else {\nreturn node;            // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n}\nself.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\nnode = self.rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.dart
    /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\nroot = insertHelper(root, val);\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? insertHelper(TreeNode? node, int val) {\nif (node == null) return TreeNode(val);\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif (val < node.val)\nnode.left = insertHelper(node.left, val);\nelse if (val > node.val)\nnode.right = insertHelper(node.right, val);\nelse\nreturn node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nupdateHeight(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 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    "},{"location":"chapter_tree/avl_tree/#_9","title":"\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\u5730\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u6240\u6709\u5931\u8861\u8282\u70b9\u6062\u590d\u5e73\u8861\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart avl_tree.java
    /* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\nroot = removeHelper(root, val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode removeHelper(TreeNode node, int val) {\nif (node == null)\nreturn null;\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val < node.val)\nnode.left = removeHelper(node.left, val);\nelse if (val > node.val)\nnode.right = removeHelper(node.right, val);\nelse {\nif (node.left == null || node.right == null) {\nTreeNode child = node.left != null ? node.left : node.right;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child == null)\nreturn null;\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse\nnode = 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\nTreeNode temp = node.right;\nwhile (temp.left != null) {\ntemp = temp.left;\n}\nnode.right = removeHelper(node.right, temp.val);\nnode.val = temp.val;\n}\n}\nupdateHeight(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 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.cpp
    /* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\nroot = removeHelper(root, val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode *removeHelper(TreeNode *node, int val) {\nif (node == nullptr)\nreturn nullptr;\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val < node->val)\nnode->left = removeHelper(node->left, val);\nelse if (val > node->val)\nnode->right = removeHelper(node->right, val);\nelse {\nif (node->left == nullptr || node->right == nullptr) {\nTreeNode *child = node->left != nullptr ? node->left : node->right;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child == nullptr) {\ndelete node;\nreturn nullptr;\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse {\ndelete node;\nnode = 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\nTreeNode *temp = node->right;\nwhile (temp->left != nullptr) {\ntemp = temp->left;\n}\nint tempVal = temp->val;\nnode->right = removeHelper(node->right, temp->val);\nnode->val = tempVal;\n}\n}\nupdateHeight(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 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.py
    def remove(self, val: int) -> None:\n\"\"\"\u5220\u9664\u8282\u70b9\"\"\"\nself.__root = self.__remove_helper(self.__root, val)\ndef __remove_helper(self, node: TreeNode | None, val: int) -> TreeNode | None:\n\"\"\"\u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\"\"\"\nif node is None:\nreturn None\n# 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b\nif val < node.val:\nnode.left = self.__remove_helper(node.left, val)\nelif val > node.val:\nnode.right = self.__remove_helper(node.right, val)\nelse:\nif node.left is None or node.right is None:\nchild = node.left or node.right\n# \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif child is None:\nreturn None\n# \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse:\nnode = child\nelse:\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\ntemp = node.right\nwhile temp.left is not None:\ntemp = temp.left\nnode.right = self.__remove_helper(node.right, temp.val)\nnode.val = temp.val\n# \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nself.__update_height(node)\n# 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\nreturn self.__rotate(node)\n
    avl_tree.go
    /* \u5220\u9664\u8282\u70b9 */\nfunc (t *aVLTree) remove(val int) {\nt.root = t.removeHelper(t.root, val)\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode {\nif node == nil {\nreturn nil\n}\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif val < node.Val {\nnode.Left = t.removeHelper(node.Left, val)\n} else if val > node.Val {\nnode.Right = t.removeHelper(node.Right, val)\n} else {\nif node.Left == nil || node.Right == nil {\nchild := node.Left\nif node.Right != nil {\nchild = node.Right\n}\nif child == nil {\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nreturn nil\n} else {\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nnode = 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\ntemp := node.Right\nfor temp.Left != nil {\ntemp = temp.Left\n}\nnode.Right = t.removeHelper(node.Right, temp.Val)\nnode.Val = temp.Val\n}\n}\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nt.updateHeight(node)\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = t.rotate(node)\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node\n}\n
    avl_tree.js
    /* \u5220\u9664\u8282\u70b9 */\nremove(val) {\nthis.root = this.#removeHelper(this.root, val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\n#removeHelper(node, val) {\nif (node === null) return null;\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val < node.val) node.left = this.#removeHelper(node.left, val);\nelse if (val > node.val)\nnode.right = this.#removeHelper(node.right, val);\nelse {\nif (node.left === null || node.right === null) {\nconst child = node.left !== null ? node.left : node.right;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child === null) return null;\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse 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\nlet temp = node.right;\nwhile (temp.left !== null) {\ntemp = temp.left;\n}\nnode.right = this.#removeHelper(node.right, temp.val);\nnode.val = temp.val;\n}\n}\nthis.#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 */\nnode = this.#rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.ts
    /* \u5220\u9664\u8282\u70b9 */\nremove(val: number): void {\nthis.root = this.removeHelper(this.root, val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nremoveHelper(node: TreeNode, val: number): TreeNode {\nif (node === null) return null;\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val < node.val) {\nnode.left = this.removeHelper(node.left, val);\n} else if (val > node.val) {\nnode.right = this.removeHelper(node.right, val);\n} else {\nif (node.left === null || node.right === null) {\nconst child = node.left !== null ? node.left : node.right;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child === null) {\nreturn null;\n} else {\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nnode = 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\nlet temp = node.right;\nwhile (temp.left !== null) {\ntemp = temp.left;\n}\nnode.right = this.removeHelper(node.right, temp.val);\nnode.val = temp.val;\n}\n}\nthis.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 */\nnode = this.rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\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 removeNode(aVLTree *tree, int val) {\nTreeNode *root = removeHelper(tree->root, val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode *removeHelper(TreeNode *node, int val) {\nTreeNode *child, *grandChild;\nif (node == NULL) {\nreturn NULL;\n}\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val < node->val) {\nnode->left = removeHelper(node->left, val);\n} else if (val > node->val) {\nnode->right = removeHelper(node->right, val);\n} else {\nif (node->left == NULL || node->right == NULL) {\nchild = node->left;\nif (node->right != NULL) {\nchild = node->right;\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child == NULL) {\nreturn NULL;\n} else {\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nnode = 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\nTreeNode *temp = node->right;\nwhile (temp->left != NULL) {\ntemp = temp->left;\n}\nint tempVal = temp->val;\nnode->right = removeHelper(node->right, temp->val);\nnode->val = tempVal;\n}\n}\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.cs
    /* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\nroot = removeHelper(root, val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? removeHelper(TreeNode? node, int val) {\nif (node == null) return null;\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val < node.val)\nnode.left = removeHelper(node.left, val);\nelse if (val > node.val)\nnode.right = removeHelper(node.right, val);\nelse {\nif (node.left == null || node.right == null) {\nTreeNode? child = node.left != null ? node.left : node.right;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child == null)\nreturn null;\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse\nnode = 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\nTreeNode? temp = node.right;\nwhile (temp.left != null) {\ntemp = temp.left;\n}\nnode.right = removeHelper(node.right, temp.val);\nnode.val = temp.val;\n}\n}\nupdateHeight(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 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.swift
    /* \u5220\u9664\u8282\u70b9 */\n@discardableResult\nfunc remove(val: Int) {\nroot = removeHelper(node: root, val: val)\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc removeHelper(node: TreeNode?, val: Int) -> TreeNode? {\nvar node = node\nif node == nil {\nreturn nil\n}\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif val < node!.val {\nnode?.left = removeHelper(node: node?.left, val: val)\n} else if val > node!.val {\nnode?.right = removeHelper(node: node?.right, val: val)\n} else {\nif node?.left == nil || node?.right == nil {\nlet child = node?.left != nil ? node?.left : node?.right\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif child == nil {\nreturn nil\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse {\nnode = 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\nvar temp = node?.right\nwhile temp?.left != nil {\ntemp = temp?.left\n}\nnode?.right = removeHelper(node: node?.right, val: temp!.val)\nnode?.val = temp!.val\n}\n}\nupdateHeight(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 */\nnode = rotate(node: node)\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node\n}\n
    avl_tree.zig
    // \u5220\u9664\u8282\u70b9\nfn remove(self: *Self, val: T) void {\nself.root = self.removeHelper(self.root, val);\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) {\nvar node = node_;\nif (node == null) return null;\n// 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b\nif (val < node.?.val) {\nnode.?.left = self.removeHelper(node.?.left, val);\n} else if (val > node.?.val) {\nnode.?.right = self.removeHelper(node.?.right, val);\n} else {\nif (node.?.left == null or node.?.right == null) {\nvar 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\nif (child == null) {\nreturn null;\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n} else {\nnode = 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\nvar temp = node.?.right;\nwhile (temp.?.left != null) {\ntemp = temp.?.left;\n}\nnode.?.right = self.removeHelper(node.?.right, temp.?.val);\nnode.?.val = temp.?.val;\n}\n}\nself.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\nnode = self.rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.dart
    /* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\nroot = removeHelper(root, val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? removeHelper(TreeNode? node, int val) {\nif (node == null) return null;\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val < node.val)\nnode.left = removeHelper(node.left, val);\nelse if (val > node.val)\nnode.right = removeHelper(node.right, val);\nelse {\nif (node.left == null || node.right == null) {\nTreeNode? child = node.left ?? node.right;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child == null)\nreturn null;\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse\nnode = 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\nTreeNode? temp = node.right;\nwhile (temp!.left != null) {\ntemp = temp.left;\n}\nnode.right = removeHelper(node.right, temp.val);\nnode.val = temp.val;\n}\n}\nupdateHeight(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 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    "},{"location":"chapter_tree/avl_tree/#_10","title":"\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":"
    • \u7ec4\u7ec7\u548c\u5b58\u50a8\u5927\u578b\u6570\u636e\uff0c\u9002\u7528\u4e8e\u9ad8\u9891\u67e5\u627e\u3001\u4f4e\u9891\u589e\u5220\u7684\u573a\u666f\uff1b
    • \u7528\u4e8e\u6784\u5efa\u6570\u636e\u5e93\u4e2d\u7684\u7d22\u5f15\u7cfb\u7edf\uff1b

    \u4e3a\u4ec0\u4e48\u7ea2\u9ed1\u6811\u6bd4 AVL \u6811\u66f4\u53d7\u6b22\u8fce\uff1f

    \u7ea2\u9ed1\u6811\u7684\u5e73\u8861\u6761\u4ef6\u76f8\u5bf9\u5bbd\u677e\uff0c\u56e0\u6b64\u5728\u7ea2\u9ed1\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\u6240\u9700\u7684\u65cb\u8f6c\u64cd\u4f5c\u76f8\u5bf9\u8f83\u5c11\uff0c\u5728\u8282\u70b9\u589e\u5220\u64cd\u4f5c\u4e0a\u7684\u5e73\u5747\u6548\u7387\u9ad8\u4e8e AVL \u6811\u3002

    "},{"location":"chapter_tree/binary_search_tree/","title":"7.4. \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811","text":"

    \u300c\u4e8c\u53c9\u641c\u7d22\u6811 Binary Search Tree\u300d\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\uff1a

    1. \u5bf9\u4e8e\u6839\u8282\u70b9\uff0c\u5de6\u5b50\u6811\u4e2d\u6240\u6709\u8282\u70b9\u7684\u503c \\(<\\) \u6839\u8282\u70b9\u7684\u503c \\(<\\) \u53f3\u5b50\u6811\u4e2d\u6240\u6709\u8282\u70b9\u7684\u503c\uff1b
    2. \u4efb\u610f\u8282\u70b9\u7684\u5de6\u3001\u53f3\u5b50\u6811\u4e5f\u662f\u4e8c\u53c9\u641c\u7d22\u6811\uff0c\u5373\u540c\u6837\u6ee1\u8db3\u6761\u4ef6 1. \uff1b

    Fig. \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":""},{"location":"chapter_tree/binary_search_tree/#_1","title":"\u67e5\u627e\u8282\u70b9","text":"

    \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\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

    • \u82e5 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 \uff1b
    • \u82e5 cur.val > num \uff0c\u8bf4\u660e\u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\uff0c\u56e0\u6b64\u6267\u884c cur = cur.left \uff1b
    • \u82e5 cur.val = num \uff0c\u8bf4\u660e\u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\u5e76\u8fd4\u56de\u8be5\u8282\u70b9\uff1b
    <1><2><3><4>

    \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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart binary_search_tree.java
    /* \u67e5\u627e\u8282\u70b9 */\nTreeNode search(int num) {\nTreeNode cur = root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num)\ncur = cur.right;\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse if (cur.val > num)\ncur = cur.left;\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse\nbreak;\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn cur;\n}\n
    binary_search_tree.cpp
    /* \u67e5\u627e\u8282\u70b9 */\nTreeNode *search(int num) {\nTreeNode *cur = root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != nullptr) {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur->val < num)\ncur = cur->right;\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse if (cur->val > num)\ncur = cur->left;\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse\nbreak;\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn cur;\n}\n
    binary_search_tree.py
    def search(self, num: int) -> TreeNode | None:\n\"\"\"\u67e5\u627e\u8282\u70b9\"\"\"\ncur: TreeNode | None = self.root\n# \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile cur is not None:\n# \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif cur.val < num:\ncur = cur.right\n# \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelif cur.val > num:\ncur = cur.left\n# \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse:\nbreak\nreturn cur\n
    binary_search_tree.go
    /* \u67e5\u627e\u8282\u70b9 */\nfunc (bst *binarySearchTree) search(num int) *TreeNode {\nnode := bst.root\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nfor node != nil {\nif node.Val < num {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nnode = node.Right\n} else if node.Val > num {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nnode = node.Left\n} else {\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nbreak\n}\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn node\n}\n
    binary_search_tree.js
    /* \u67e5\u627e\u8282\u70b9 */\nfunction search(num) {\nlet cur = root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur !== null) {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num) cur = cur.right;\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse if (cur.val > num) cur = cur.left;\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse break;\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn cur;\n}\n
    binary_search_tree.ts
    /* \u67e5\u627e\u8282\u70b9 */\nfunction search(num: number): TreeNode | null {\nlet cur = root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur !== null) {\nif (cur.val < num) {\ncur = cur.right; // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n} else if (cur.val > num) {\ncur = cur.left; // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n} else {\nbreak; // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n}\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn cur;\n}\n
    binary_search_tree.c
    /* \u67e5\u627e\u8282\u70b9 */\nTreeNode *search(binarySearchTree *bst, int num) {\nTreeNode *cur = bst->root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != NULL) {\nif (cur->val < num) {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\ncur = cur->right;\n} else if (cur->val > num) {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\ncur = cur->left;\n} else {\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nbreak;\n}\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn cur;\n}\n
    binary_search_tree.cs
    /* \u67e5\u627e\u8282\u70b9 */\nTreeNode? search(int num) {\nTreeNode? cur = root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num) cur =\ncur.right;\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse if (cur.val > num)\ncur = cur.left;\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse\nbreak;\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn cur;\n}\n
    binary_search_tree.swift
    /* \u67e5\u627e\u8282\u70b9 */\nfunc search(num: Int) -> TreeNode? {\nvar cur = root\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile cur != nil {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif cur!.val < num {\ncur = cur?.right\n}\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse if cur!.val > num {\ncur = cur?.left\n}\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse {\nbreak\n}\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn cur\n}\n
    binary_search_tree.zig
    // \u67e5\u627e\u8282\u70b9\nfn search(self: *Self, num: T) ?*inc.TreeNode(T) {\nvar cur = self.root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.?.val < num) {\ncur = cur.?.right;\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n} else if (cur.?.val > num) {\ncur = cur.?.left;\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n} else {\nbreak;\n}\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn cur;\n}\n
    binary_search_tree.dart
    [class]{BinarySearchTree}-[func]{search}\n
    "},{"location":"chapter_tree/binary_search_tree/#_2","title":"\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\u5206\u4e3a\u4e24\u6b65\uff1a

    1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff1a\u4e0e\u67e5\u627e\u64cd\u4f5c\u76f8\u4f3c\uff0c\u4ece\u6839\u8282\u70b9\u51fa\u53d1\uff0c\u6839\u636e\u5f53\u524d\u8282\u70b9\u503c\u548c 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{null}\\) \uff09\u65f6\u8df3\u51fa\u5faa\u73af\uff1b
    2. \u5728\u8be5\u4f4d\u7f6e\u63d2\u5165\u8282\u70b9\uff1a\u521d\u59cb\u5316\u8282\u70b9 num \uff0c\u5c06\u8be5\u8282\u70b9\u7f6e\u4e8e \\(\\text{null}\\) \u7684\u4f4d\u7f6e\uff1b

    \u4e8c\u53c9\u641c\u7d22\u6811\u4e0d\u5141\u8bb8\u5b58\u5728\u91cd\u590d\u8282\u70b9\uff0c\u5426\u5219\u5c06\u8fdd\u53cd\u5176\u5b9a\u4e49\u3002\u56e0\u6b64\uff0c\u82e5\u5f85\u63d2\u5165\u8282\u70b9\u5728\u6811\u4e2d\u5df2\u5b58\u5728\uff0c\u5219\u4e0d\u6267\u884c\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\u3002

    Fig. \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u63d2\u5165\u8282\u70b9

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart binary_search_tree.java
    /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root == null)\nreturn;\nTreeNode cur = root, pre = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif (cur.val == num)\nreturn;\npre = cur;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num)\ncur = cur.right;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse\ncur = cur.left;\n}\n// \u63d2\u5165\u8282\u70b9\nTreeNode node = new TreeNode(num);\nif (pre.val < num)\npre.right = node;\nelse\npre.left = node;\n}\n
    binary_search_tree.cpp
    /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root == nullptr)\nreturn;\nTreeNode *cur = root, *pre = nullptr;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != nullptr) {\n// \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif (cur->val == num)\nreturn;\npre = cur;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur->val < num)\ncur = cur->right;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse\ncur = cur->left;\n}\n// \u63d2\u5165\u8282\u70b9\nTreeNode *node = new TreeNode(num);\nif (pre->val < num)\npre->right = node;\nelse\npre->left = node;\n}\n
    binary_search_tree.py
    def insert(self, num: int) -> None:\n\"\"\"\u63d2\u5165\u8282\u70b9\"\"\"\n# \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif self.root is None:\nreturn\n# \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\ncur, pre = self.root, None\nwhile cur is not None:\n# \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif cur.val == num:\nreturn\npre = cur\n# \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif cur.val < num:\ncur = cur.right\n# \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse:\ncur = cur.left\n# \u63d2\u5165\u8282\u70b9\nnode = TreeNode(num)\nif pre.val < num:\npre.right = node\nelse:\npre.left = node\n
    binary_search_tree.go
    /* \u63d2\u5165\u8282\u70b9 */\nfunc (bst *binarySearchTree) insert(num int) {\ncur := bst.root\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif cur == nil {\nreturn\n}\n// \u5f85\u63d2\u5165\u8282\u70b9\u4e4b\u524d\u7684\u8282\u70b9\u4f4d\u7f6e\nvar pre *TreeNode = nil\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nfor cur != nil {\nif cur.Val == num {\nreturn\n}\npre = cur\nif cur.Val < num {\ncur = cur.Right\n} else {\ncur = cur.Left\n}\n}\n// \u63d2\u5165\u8282\u70b9\nnode := NewTreeNode(num)\nif pre.Val < num {\npre.Right = node\n} else {\npre.Left = node\n}\n}\n
    binary_search_tree.js
    /* \u63d2\u5165\u8282\u70b9 */\nfunction insert(num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root === null) return;\nlet cur = root,\npre = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur !== null) {\n// \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif (cur.val === num) return;\npre = cur;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num) cur = cur.right;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse cur = cur.left;\n}\n// \u63d2\u5165\u8282\u70b9\nlet node = new TreeNode(num);\nif (pre.val < num) pre.right = node;\nelse pre.left = node;\n}\n
    binary_search_tree.ts
    /* \u63d2\u5165\u8282\u70b9 */\nfunction insert(num: number): void {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root === null) {\nreturn;\n}\nlet cur = root,\npre: TreeNode | null = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur !== null) {\nif (cur.val === num) {\nreturn; // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n}\npre = cur;\nif (cur.val < num) {\ncur = cur.right as TreeNode; // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n} else {\ncur = cur.left as TreeNode; // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n}\n}\n// \u63d2\u5165\u8282\u70b9\nlet node = new TreeNode(num);\nif (pre!.val < num) {\npre!.right = node;\n} else {\npre!.left = node;\n}\n}\n
    binary_search_tree.c
    /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(binarySearchTree *bst, int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (bst->root == NULL)\nreturn;\nTreeNode *cur = bst->root, *pre = NULL;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != NULL) {\n// \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif (cur->val == num) {\nreturn;\n}\npre = cur;\nif (cur->val < num) {\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\ncur = cur->right;\n} else {\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\ncur = cur->left;\n}\n}\n// \u63d2\u5165\u8282\u70b9\nTreeNode *node = newTreeNode(num);\nif (pre->val < num) {\npre->right = node;\n} else {\npre->left = node;\n}\n}\n
    binary_search_tree.cs
    /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root == null)\nreturn;\nTreeNode? cur = root, pre = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif (cur.val == num)\nreturn;\npre = cur;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num)\ncur = cur.right;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse\ncur = cur.left;\n}\n// \u63d2\u5165\u8282\u70b9\nTreeNode node = new TreeNode(num);\nif (pre != null) {\nif (pre.val < num)\npre.right = node;\nelse\npre.left = node;\n}\n}\n
    binary_search_tree.swift
    /* \u63d2\u5165\u8282\u70b9 */\nfunc insert(num: Int) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif root == nil {\nreturn\n}\nvar cur = root\nvar pre: TreeNode?\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile cur != nil {\n// \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif cur!.val == num {\nreturn\n}\npre = cur\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif cur!.val < num {\ncur = cur?.right\n}\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse {\ncur = cur?.left\n}\n}\n// \u63d2\u5165\u8282\u70b9\nlet node = TreeNode(x: num)\nif pre!.val < num {\npre?.right = node\n} else {\npre?.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\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (self.root == null) return;\nvar cur = self.root;\nvar pre: ?*inc.TreeNode(T) = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif (cur.?.val == num) return;\npre = cur;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.?.val < num) {\ncur = cur.?.right;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n} else {\ncur = cur.?.left;\n}\n}\n// \u63d2\u5165\u8282\u70b9\nvar node = try self.mem_allocator.create(inc.TreeNode(T));\nnode.init(num);\nif (pre.?.val < num) {\npre.?.right = node;\n} else {\npre.?.left = node;\n}\n}\n
    binary_search_tree.dart
    [class]{BinarySearchTree}-[func]{insert}\n

    \u4e3a\u4e86\u63d2\u5165\u8282\u70b9\uff0c\u6211\u4eec\u9700\u8981\u5229\u7528\u8f85\u52a9\u8282\u70b9 pre \u4fdd\u5b58\u4e0a\u4e00\u8f6e\u5faa\u73af\u7684\u8282\u70b9\uff0c\u8fd9\u6837\u5728\u904d\u5386\u81f3 \\(\\text{null}\\) \u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u5230\u5176\u7236\u8282\u70b9\uff0c\u4ece\u800c\u5b8c\u6210\u8282\u70b9\u63d2\u5165\u64cd\u4f5c\u3002

    \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":"\u5220\u9664\u8282\u70b9","text":"

    \u4e0e\u63d2\u5165\u8282\u70b9\u7c7b\u4f3c\uff0c\u6211\u4eec\u9700\u8981\u5728\u5220\u9664\u64cd\u4f5c\u540e\u7ef4\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u201c\u5de6\u5b50\u6811 < \u6839\u8282\u70b9 < \u53f3\u5b50\u6811\u201d\u7684\u6027\u8d28\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u4e8c\u53c9\u6811\u4e2d\u6267\u884c\u67e5\u627e\u64cd\u4f5c\uff0c\u83b7\u53d6\u5f85\u5220\u9664\u8282\u70b9\u3002\u63a5\u4e0b\u6765\uff0c\u6839\u636e\u5f85\u5220\u9664\u8282\u70b9\u7684\u5b50\u8282\u70b9\u6570\u91cf\uff0c\u5220\u9664\u64cd\u4f5c\u9700\u5206\u4e3a\u4e09\u79cd\u60c5\u51b5\uff1a

    \u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5b50\u8282\u70b9\u6570\u91cf \\(= 0\\) \u65f6\uff0c\u8868\u793a\u5f85\u5220\u9664\u8282\u70b9\u662f\u53f6\u8282\u70b9\uff0c\u53ef\u4ee5\u76f4\u63a5\u5220\u9664\u3002

    Fig. \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 0\uff09

    \u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5b50\u8282\u70b9\u6570\u91cf \\(= 1\\) \u65f6\uff0c\u5c06\u5f85\u5220\u9664\u8282\u70b9\u66ff\u6362\u4e3a\u5176\u5b50\u8282\u70b9\u5373\u53ef\u3002

    Fig. \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 1\uff09

    \u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5b50\u8282\u70b9\u6570\u91cf \\(= 2\\) \u65f6\uff0c\u5220\u9664\u64cd\u4f5c\u5206\u4e3a\u4e09\u6b65\uff1a

    1. \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\u5728\u201c\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u201d\u4e2d\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\uff0c\u8bb0\u4e3a tmp \uff1b
    2. \u5728\u6811\u4e2d\u9012\u5f52\u5220\u9664\u8282\u70b9 tmp \uff1b
    3. \u7528 tmp \u7684\u503c\u8986\u76d6\u5f85\u5220\u9664\u8282\u70b9\u7684\u503c\uff1b
    <1><2><3><4>

    \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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart binary_search_tree.java
    /* \u5220\u9664\u8282\u70b9 */\nvoid remove(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root == null)\nreturn;\nTreeNode cur = root, pre = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur.val == num)\nbreak;\npre = cur;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num)\ncur = cur.right;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse\ncur = cur.left;\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur == null)\nreturn;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif (cur.left == null || cur.right == null) {\n// \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\nTreeNode child = cur.left != null ? cur.left : cur.right;\n// \u5220\u9664\u8282\u70b9 cur\nif (cur != root) {\nif (pre.left == cur)\npre.left = child;\nelse\npre.right = child;\n} else {\n// \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nroot = child;\n}\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 2\nelse {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\nTreeNode tmp = cur.right;\nwhile (tmp.left != null) {\ntmp = tmp.left;\n}\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nremove(tmp.val);\n// \u7528 tmp \u8986\u76d6 cur\ncur.val = tmp.val;\n}\n}\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\nif (root == nullptr)\nreturn;\nTreeNode *cur = root, *pre = nullptr;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != nullptr) {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur->val == num)\nbreak;\npre = cur;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur->val < num)\ncur = cur->right;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse\ncur = cur->left;\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur == nullptr)\nreturn;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif (cur->left == nullptr || cur->right == nullptr) {\n// \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u8282\u70b9\nTreeNode *child = cur->left != nullptr ? cur->left : cur->right;\n// \u5220\u9664\u8282\u70b9 cur\nif (cur != root) {\nif (pre->left == cur)\npre->left = child;\nelse\npre->right = child;\n} else {\n// \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nroot = child;\n}\n// \u91ca\u653e\u5185\u5b58\ndelete cur;\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 2\nelse {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\nTreeNode *tmp = cur->right;\nwhile (tmp->left != nullptr) {\ntmp = tmp->left;\n}\nint tmpVal = tmp->val;\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nremove(tmp->val);\n// \u7528 tmp \u8986\u76d6 cur\ncur->val = tmpVal;\n}\n}\n
    binary_search_tree.py
    def remove(self, num: int) -> None:\n\"\"\"\u5220\u9664\u8282\u70b9\"\"\"\n# \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif self.root is None:\nreturn\n# \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\ncur, pre = self.root, None\nwhile cur is not None:\n# \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif cur.val == num:\nbreak\npre = cur\n# \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif cur.val < num:\ncur = cur.right\n# \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse:\ncur = cur.left\n# \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif cur is None:\nreturn\n# \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif 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\nchild = cur.left or cur.right\n# \u5220\u9664\u8282\u70b9 cur\nif cur != self.root:\nif pre.left == cur:\npre.left = child\nelse:\npre.right = child\nelse:\n# \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nself.__root = cur\n# \u5b50\u8282\u70b9\u6570\u91cf = 2\nelse:\n# \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\ntmp: TreeNode = cur.right\nwhile tmp.left is not None:\ntmp = tmp.left\n# \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nself.remove(tmp.val)\n# \u7528 tmp \u8986\u76d6 cur\ncur.val = tmp.val\n
    binary_search_tree.go
    /* \u5220\u9664\u8282\u70b9 */\nfunc (bst *binarySearchTree) remove(num int) {\ncur := bst.root\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif cur == nil {\nreturn\n}\n// \u5f85\u5220\u9664\u8282\u70b9\u4e4b\u524d\u7684\u8282\u70b9\u4f4d\u7f6e\nvar pre *TreeNode = nil\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nfor cur != nil {\nif cur.Val == num {\nbreak\n}\npre = cur\nif cur.Val < num {\n// \u5f85\u5220\u9664\u8282\u70b9\u5728\u53f3\u5b50\u6811\u4e2d\ncur = cur.Right\n} else {\n// \u5f85\u5220\u9664\u8282\u70b9\u5728\u5de6\u5b50\u6811\u4e2d\ncur = cur.Left\n}\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif cur == nil {\nreturn\n}\n// \u5b50\u8282\u70b9\u6570\u4e3a 0 \u6216 1\nif cur.Left == nil || cur.Right == nil {\nvar child *TreeNode = nil\n// \u53d6\u51fa\u5f85\u5220\u9664\u8282\u70b9\u7684\u5b50\u8282\u70b9\nif cur.Left != nil {\nchild = cur.Left\n} else {\nchild = cur.Right\n}\n// \u5220\u9664\u8282\u70b9 cur\nif cur != bst.root {\nif pre.Left == cur {\npre.Left = child\n} else {\npre.Right = child\n}\n} else {\n// \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nbst.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\ntmp := cur.Right\nfor tmp.Left != nil {\ntmp = tmp.Left\n}\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nbst.remove(tmp.Val)\n// \u7528 tmp \u8986\u76d6 cur\ncur.Val = tmp.Val\n}\n}\n
    binary_search_tree.js
    /* \u5220\u9664\u8282\u70b9 */\nfunction remove(num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root === null) return;\nlet cur = root,\npre = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur !== null) {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur.val === num) break;\npre = cur;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num) cur = cur.right;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse cur = cur.left;\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur === null) return;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif (cur.left === null || cur.right === null) {\n// \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\nlet child = cur.left !== null ? cur.left : cur.right;\n// \u5220\u9664\u8282\u70b9 cur\nif (cur != root) {\nif (pre.left === cur) pre.left = child;\nelse pre.right = child;\n} else {\n// \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nroot = child;\n}\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 2\nelse {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\nlet tmp = cur.right;\nwhile (tmp.left !== null) {\ntmp = tmp.left;\n}\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nremove(tmp.val);\n// \u7528 tmp \u8986\u76d6 cur\ncur.val = tmp.val;\n}\n}\n
    binary_search_tree.ts
    /* \u5220\u9664\u8282\u70b9 */\nfunction remove(num: number): void {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root === null) {\nreturn;\n}\nlet cur = root,\npre: TreeNode | null = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur !== null) {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur.val === num) {\nbreak;\n}\npre = cur;\nif (cur.val < num) {\ncur = cur.right as TreeNode; // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n} else {\ncur = cur.left as TreeNode; // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n}\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur === null) {\nreturn;\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif (cur.left === null || cur.right === null) {\n// \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\nlet child = cur.left !== null ? cur.left : cur.right;\n// \u5220\u9664\u8282\u70b9 cur\nif (cur != root) {\nif (pre!.left === cur) {\npre!.left = child;\n} else {\npre!.right = child;\n}\n} else {\n// \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nroot = child;\n}\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 2\nelse {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\nlet tmp = cur.right;\nwhile (tmp.left !== null) {\ntmp = tmp.left;\n}\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nremove(tmp!.val);\n// \u7528 tmp \u8986\u76d6 cur\ncur.val = tmp.val;\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 removeNode(binarySearchTree *bst, int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (bst->root == NULL)\nreturn;\nTreeNode *cur = bst->root, *pre = NULL;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != NULL) {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur->val == num)\nbreak;\npre = cur;\nif (cur->val < num) {\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 root \u7684\u53f3\u5b50\u6811\u4e2d\ncur = cur->right;\n} else {\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 root \u7684\u5de6\u5b50\u6811\u4e2d\ncur = cur->left;\n}\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur == NULL)\nreturn;\n// \u5224\u65ad\u5f85\u5220\u9664\u8282\u70b9\u662f\u5426\u5b58\u5728\u5b50\u8282\u70b9\nif (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\nTreeNode *child = cur->left != NULL ? cur->left : cur->right;\n// \u5220\u9664\u8282\u70b9 cur\nif (pre->left == cur) {\npre->left = child;\n} else {\npre->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\nTreeNode *tmp = cur->right;\nwhile (tmp->left != NULL) {\ntmp = tmp->left;\n}\nint tmpVal = tmp->val;\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nremoveNode(bst, tmp->val);\n// \u7528 tmp \u8986\u76d6 cur\ncur->val = tmpVal;\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\nif (root == null)\nreturn;\nTreeNode? cur = root, pre = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur.val == num)\nbreak;\npre = cur;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num)\ncur = cur.right;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse\ncur = cur.left;\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur == null || pre == null)\nreturn;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif (cur.left == null || cur.right == null) {\n// \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\nTreeNode? child = cur.left != null ? cur.left : cur.right;\n// \u5220\u9664\u8282\u70b9 cur\nif (cur != root) {\nif (pre.left == cur)\npre.left = child;\nelse\npre.right = child;\n} else {\n// \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nroot = child;\n}\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 2\nelse {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\nTreeNode? tmp = cur.right;\nwhile (tmp.left != null) {\ntmp = tmp.left;\n}\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nremove(tmp.val);\n// \u7528 tmp \u8986\u76d6 cur\ncur.val = tmp.val;\n}\n}\n
    binary_search_tree.swift
    /* \u5220\u9664\u8282\u70b9 */\n@discardableResult\nfunc remove(num: Int) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif root == nil {\nreturn\n}\nvar cur = root\nvar pre: TreeNode?\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile cur != nil {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif cur!.val == num {\nbreak\n}\npre = cur\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif cur!.val < num {\ncur = cur?.right\n}\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse {\ncur = cur?.left\n}\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif cur == nil {\nreturn\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif cur?.left == nil || cur?.right == nil {\n// \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\nlet child = cur?.left != nil ? cur?.left : cur?.right\n// \u5220\u9664\u8282\u70b9 cur\nif cur != root {\nif pre?.left === cur {\npre?.left = child\n} else {\npre?.right = child\n}\n} else {\n// \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nroot = cur;\n}\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 2\nelse {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\nvar tmp = cur?.right\nwhile tmp?.left != nil {\ntmp = tmp?.left\n}\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nremove(num: tmp!.val)\n// \u7528 tmp \u8986\u76d6 cur\ncur?.val = tmp!.val\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\nif (self.root == null) return;\nvar cur = self.root;\nvar pre: ?*inc.TreeNode(T) = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur.?.val == num) break;\npre = cur;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.?.val < num) {\ncur = cur.?.right;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n} else {\ncur = cur.?.left;\n}\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur == null) return;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif (cur.?.left == null or cur.?.right == null) {\n// \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\nvar child = if (cur.?.left != null) cur.?.left else cur.?.right;\n// \u5220\u9664\u8282\u70b9 cur\nif (pre.?.left == cur) {\npre.?.left = child;\n} else {\npre.?.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\nvar tmp = cur.?.right;\nwhile (tmp.?.left != null) {\ntmp = tmp.?.left;\n}\nvar tmpVal = tmp.?.val;\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n_ = self.remove(tmp.?.val);\n// \u7528 tmp \u8986\u76d6 cur\ncur.?.val = tmpVal;\n}\n}\n
    binary_search_tree.dart
    [class]{BinarySearchTree}-[func]{remove}\n
    "},{"location":"chapter_tree/binary_search_tree/#_4","title":"\u6392\u5e8f","text":"

    \u6211\u4eec\u77e5\u9053\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\u56e0\u6b64\uff0c\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\u9700\u989d\u5916\u6392\u5e8f\uff0c\u975e\u5e38\u9ad8\u6548\u3002

    Fig. \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\u53ef\u77e5\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\u8868\u73b0\u3002\u53ea\u6709\u5728\u9ad8\u9891\u6dfb\u52a0\u3001\u4f4e\u9891\u67e5\u627e\u5220\u9664\u7684\u6570\u636e\u9002\u7528\u573a\u666f\u4e0b\uff0c\u6570\u7ec4\u6bd4\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6548\u7387\u66f4\u9ad8\u3002

    \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\u94fe\u8868\uff0c\u8fd9\u65f6\u5404\u79cd\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u4f1a\u9000\u5316\u4e3a \\(O(n)\\) \u3002

    Fig. \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u5e73\u8861\u4e0e\u9000\u5316

    "},{"location":"chapter_tree/binary_search_tree/#743","title":"7.4.3. \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u5e38\u89c1\u5e94\u7528","text":"
    • \u7528\u4f5c\u7cfb\u7edf\u4e2d\u7684\u591a\u7ea7\u7d22\u5f15\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684\u67e5\u627e\u3001\u63d2\u5165\u3001\u5220\u9664\u64cd\u4f5c\u3002
    • \u4f5c\u4e3a\u67d0\u4e9b\u641c\u7d22\u7b97\u6cd5\u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002
    • \u7528\u4e8e\u5b58\u50a8\u6570\u636e\u6d41\uff0c\u4ee5\u4fdd\u6301\u5176\u6709\u5e8f\u72b6\u6001\u3002
    "},{"location":"chapter_tree/binary_tree/","title":"7.1. \u00a0 \u4e8c\u53c9\u6811","text":"

    \u300c\u4e8c\u53c9\u6811 Binary Tree\u300d\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u4ee3\u8868\u7740\u7956\u5148\u4e0e\u540e\u4ee3\u4e4b\u95f4\u7684\u6d3e\u751f\u5173\u7cfb\uff0c\u4f53\u73b0\u7740\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\u4e00\u4e2a\u300c\u503c\u300d\u548c\u4e24\u4e2a\u300c\u6307\u9488\u300d\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nint val;         // \u8282\u70b9\u503c\nTreeNode left;   // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nTreeNode right;  // \u53f3\u5b50\u8282\u70b9\u6307\u9488\nTreeNode(int x) { val = x; }\n}\n
    /* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\nint val;          // \u8282\u70b9\u503c\nTreeNode *left;   // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nTreeNode *right;  // \u53f3\u5b50\u8282\u70b9\u6307\u9488\nTreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n};\n
    class TreeNode:\n\"\"\"\u4e8c\u53c9\u6811\u8282\u70b9\u7c7b\"\"\"\ndef __init__(self, val: int):\nself.val: int = val                   # \u8282\u70b9\u503c\nself.left: Optional[TreeNode] = None  # \u5de6\u5b50\u8282\u70b9\u6307\u9488\nself.right: Optional[TreeNode] = None # \u53f3\u5b50\u8282\u70b9\u6307\u9488\n
    /* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype TreeNode struct {\nVal   int\nLeft  *TreeNode\nRight *TreeNode\n}\n/* \u8282\u70b9\u521d\u59cb\u5316\u65b9\u6cd5 */\nfunc NewTreeNode(v int) *TreeNode {\nreturn &TreeNode{\nLeft:  nil,\nRight: nil,\nVal:   v,\n}\n}\n
    /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nfunction TreeNode(val, left, right) {\nthis.val = (val === undefined ? 0 : val); // \u8282\u70b9\u503c\nthis.left = (left === undefined ? null : left); // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nthis.right = (right === undefined ? null : right); // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n}\n
    /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nval: number;\nleft: TreeNode | null;\nright: TreeNode | null;\nconstructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {\nthis.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\nthis.left = left === undefined ? null : left; // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nthis.right = right === undefined ? null : right; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n}\n}\n
    /* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\nint val;                // \u8282\u70b9\u503c\nint height;             // \u8282\u70b9\u9ad8\u5ea6\nstruct TreeNode *left;  // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nstruct TreeNode *right; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n};\ntypedef struct TreeNode TreeNode;\n/* \u6784\u9020\u51fd\u6570 */\nTreeNode *newTreeNode(int val) {\nTreeNode *node;\nnode = (TreeNode *)malloc(sizeof(TreeNode));\nnode->val = val;\nnode->height = 0;\nnode->left = NULL;\nnode->right = NULL;\nreturn node;\n}\n
    /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nint val;          // \u8282\u70b9\u503c\nTreeNode? left;   // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nTreeNode? right;  // \u53f3\u5b50\u8282\u70b9\u6307\u9488\nTreeNode(int x) { val = x; }\n}\n
    /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nvar val: Int // \u8282\u70b9\u503c\nvar left: TreeNode? // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nvar right: TreeNode? // \u53f3\u5b50\u8282\u70b9\u6307\u9488\ninit(x: Int) {\nval = x\n}\n}\n
    \n
    /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nint val;         // \u8282\u70b9\u503c\nTreeNode? left;  // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nTreeNode? right; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\nTreeNode(this.val, [this.left, this.right]);\n}\n

    \u8282\u70b9\u7684\u4e24\u4e2a\u6307\u9488\u5206\u522b\u6307\u5411\u300c\u5de6\u5b50\u8282\u70b9\u300d\u548c\u300c\u53f3\u5b50\u8282\u70b9\u300d\uff0c\u540c\u65f6\u8be5\u8282\u70b9\u88ab\u79f0\u4e3a\u8fd9\u4e24\u4e2a\u5b50\u8282\u70b9\u7684\u300c\u7236\u8282\u70b9\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\u300d\uff0c\u540c\u7406\u53ef\u5f97\u300c\u53f3\u5b50\u6811\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\u4f8b\u5982\uff0c\u5728\u4ee5\u4e0b\u793a\u4f8b\u4e2d\uff0c\u82e5\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

    Fig. \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\u6d89\u53ca\u7684\u672f\u8bed\u8f83\u591a\uff0c\u5efa\u8bae\u5c3d\u91cf\u7406\u89e3\u5e76\u8bb0\u4f4f\u3002

    • \u300c\u6839\u8282\u70b9 Root Node\u300d\uff1a\u4f4d\u4e8e\u4e8c\u53c9\u6811\u9876\u5c42\u7684\u8282\u70b9\uff0c\u6ca1\u6709\u7236\u8282\u70b9\uff1b
    • \u300c\u53f6\u8282\u70b9 Leaf Node\u300d\uff1a\u6ca1\u6709\u5b50\u8282\u70b9\u7684\u8282\u70b9\uff0c\u5176\u4e24\u4e2a\u6307\u9488\u5747\u6307\u5411 \\(\\text{null}\\) \uff1b
    • \u8282\u70b9\u7684\u300c\u5c42 Level\u300d\uff1a\u4ece\u9876\u81f3\u5e95\u9012\u589e\uff0c\u6839\u8282\u70b9\u6240\u5728\u5c42\u4e3a 1 \uff1b
    • \u8282\u70b9\u7684\u300c\u5ea6 Degree\u300d\uff1a\u8282\u70b9\u7684\u5b50\u8282\u70b9\u7684\u6570\u91cf\u3002\u5728\u4e8c\u53c9\u6811\u4e2d\uff0c\u5ea6\u7684\u8303\u56f4\u662f 0, 1, 2 \uff1b
    • \u300c\u8fb9 Edge\u300d\uff1a\u8fde\u63a5\u4e24\u4e2a\u8282\u70b9\u7684\u7ebf\u6bb5\uff0c\u5373\u8282\u70b9\u6307\u9488\uff1b
    • \u4e8c\u53c9\u6811\u7684\u300c\u9ad8\u5ea6\u300d\uff1a\u4ece\u6839\u8282\u70b9\u5230\u6700\u8fdc\u53f6\u8282\u70b9\u6240\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\uff1b
    • \u8282\u70b9\u7684\u300c\u6df1\u5ea6 Depth\u300d \uff1a\u4ece\u6839\u8282\u70b9\u5230\u8be5\u8282\u70b9\u6240\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\uff1b
    • \u8282\u70b9\u7684\u300c\u9ad8\u5ea6 Height\u300d\uff1a\u4ece\u6700\u8fdc\u53f6\u8282\u70b9\u5230\u8be5\u8282\u70b9\u6240\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\uff1b

    Fig. \u4e8c\u53c9\u6811\u7684\u5e38\u7528\u672f\u8bed

    \u9ad8\u5ea6\u4e0e\u6df1\u5ea6\u7684\u5b9a\u4e49

    \u8bf7\u6ce8\u610f\uff0c\u6211\u4eec\u901a\u5e38\u5c06\u300c\u9ad8\u5ea6\u300d\u548c\u300c\u6df1\u5ea6\u300d\u5b9a\u4e49\u4e3a\u201c\u8d70\u8fc7\u8fb9\u7684\u6570\u91cf\u201d\uff0c\u4f46\u6709\u4e9b\u9898\u76ee\u6216\u6559\u6750\u53ef\u80fd\u4f1a\u5c06\u5176\u5b9a\u4e49\u4e3a\u201c\u8d70\u8fc7\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":"

    \u521d\u59cb\u5316\u4e8c\u53c9\u6811\u3002\u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u9996\u5148\u521d\u59cb\u5316\u8282\u70b9\uff0c\u7136\u540e\u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\u5f15\u7528\u6307\u5411\uff08\u5373\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\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n
    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\u5f15\u7528\u6307\u5411\uff08\u5373\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\u5f15\u7528\u6307\u5411\uff08\u5373\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),\nn2 = new TreeNode(2),\nn3 = new TreeNode(3),\nn4 = new TreeNode(4),\nn5 = new TreeNode(5);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\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),\nn2 = new TreeNode(2),\nn3 = new TreeNode(3),\nn4 = new TreeNode(4),\nn5 = new TreeNode(5);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = 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\u5f15\u7528\u6307\u5411\uff08\u5373\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 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\u5f15\u7528\u6307\u5411\uff08\u5373\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\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
    binary_tree.zig
    \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\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n

    \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\u3002\u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u901a\u8fc7\u4fee\u6539\u6307\u9488\u6765\u5b9e\u73b0\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\u3002

    Fig. \u5728\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart binary_tree.java
    TreeNode 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.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.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.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.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.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.cs
    /* \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.swift
    let 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.zig
    \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

    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\u64cd\u4f5c\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","title":"\u5b8c\u7f8e\u4e8c\u53c9\u6811","text":"

    \u300c\u5b8c\u7f8e\u4e8c\u53c9\u6811 Perfect Binary Tree\u300d\u9664\u4e86\u6700\u5e95\u5c42\u5916\uff0c\u5176\u4f59\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\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

    \u5728\u4e2d\u6587\u793e\u533a\u4e2d\uff0c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u5e38\u88ab\u79f0\u4e3a\u300c\u6ee1\u4e8c\u53c9\u6811\u300d\uff0c\u8bf7\u6ce8\u610f\u533a\u5206\u3002

    Fig. \u5b8c\u7f8e\u4e8c\u53c9\u6811

    "},{"location":"chapter_tree/binary_tree/#_2","title":"\u5b8c\u5168\u4e8c\u53c9\u6811","text":"

    \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

    Fig. \u5b8c\u5168\u4e8c\u53c9\u6811

    "},{"location":"chapter_tree/binary_tree/#_3","title":"\u5b8c\u6ee1\u4e8c\u53c9\u6811","text":"

    \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

    Fig. \u5b8c\u6ee1\u4e8c\u53c9\u6811

    "},{"location":"chapter_tree/binary_tree/#_4","title":"\u5e73\u8861\u4e8c\u53c9\u6811","text":"

    \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

    Fig. \u5e73\u8861\u4e8c\u53c9\u6811

    "},{"location":"chapter_tree/binary_tree/#714","title":"7.1.4. \u00a0 \u4e8c\u53c9\u6811\u7684\u9000\u5316","text":"

    \u5f53\u4e8c\u53c9\u6811\u7684\u6bcf\u5c42\u8282\u70b9\u90fd\u88ab\u586b\u6ee1\u65f6\uff0c\u8fbe\u5230\u300c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u300d\uff1b\u800c\u5f53\u6240\u6709\u8282\u70b9\u90fd\u504f\u5411\u4e00\u4fa7\u65f6\uff0c\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u300c\u94fe\u8868\u300d\u3002

    • \u5b8c\u7f8e\u4e8c\u53c9\u6811\u662f\u7406\u60f3\u60c5\u51b5\uff0c\u53ef\u4ee5\u5145\u5206\u53d1\u6325\u4e8c\u53c9\u6811\u201c\u5206\u6cbb\u201d\u7684\u4f18\u52bf\uff1b
    • \u94fe\u8868\u5219\u662f\u53e6\u4e00\u4e2a\u6781\u7aef\uff0c\u5404\u9879\u64cd\u4f5c\u90fd\u53d8\u4e3a\u7ebf\u6027\u64cd\u4f5c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u9000\u5316\u81f3 \\(O(n)\\) \uff1b

    Fig. \u4e8c\u53c9\u6811\u7684\u6700\u4f73\u4e0e\u6700\u5dee\u7ed3\u6784

    \u5982\u4e0b\u8868\u6240\u793a\uff0c\u5728\u6700\u4f73\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\u6216\u6781\u5c0f\u503c\u3002

    \u5b8c\u7f8e\u4e8c\u53c9\u6811 \u94fe\u8868 \u7b2c \\(i\\) \u5c42\u7684\u8282\u70b9\u6570\u91cf \\(2^{i-1}\\) \\(1\\) \u6811\u7684\u9ad8\u5ea6\u4e3a \\(h\\) \u65f6\u7684\u53f6\u8282\u70b9\u6570\u91cf \\(2^h\\) \\(1\\) \u6811\u7684\u9ad8\u5ea6\u4e3a \\(h\\) \u65f6\u7684\u8282\u70b9\u603b\u6570 \\(2^{h+1} - 1\\) \\(h + 1\\) \u6811\u7684\u8282\u70b9\u603b\u6570\u4e3a \\(n\\) \u65f6\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":"

    \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\u641c\u7d22 Breadth-First Traversal\u300d\uff0c\u5b83\u4f53\u73b0\u4e86\u4e00\u79cd\u201c\u4e00\u5708\u4e00\u5708\u5411\u5916\u6269\u5c55\u201d\u7684\u9010\u5c42\u641c\u7d22\u65b9\u5f0f\u3002

    Fig. \u4e8c\u53c9\u6811\u7684\u5c42\u5e8f\u904d\u5386

    "},{"location":"chapter_tree/binary_tree_traversal/#_1","title":"\u7b97\u6cd5\u5b9e\u73b0","text":"

    \u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u901a\u5e38\u501f\u52a9\u300c\u961f\u5217\u300d\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nQueue<TreeNode> queue = new LinkedList<>() {{ add(root); }};\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nList<Integer> list = new ArrayList<>();\nwhile (!queue.isEmpty()) {\nTreeNode node = queue.poll(); // \u961f\u5217\u51fa\u961f\nlist.add(node.val);           // \u4fdd\u5b58\u8282\u70b9\u503c\nif (node.left != null)\nqueue.offer(node.left);   // \u5de6\u5b50\u8282\u70b9\u5165\u961f\nif (node.right != null)\nqueue.offer(node.right);  // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n}\nreturn list;\n}\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\nqueue<TreeNode *> queue;\nqueue.push(root);\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nvector<int> vec;\nwhile (!queue.empty()) {\nTreeNode *node = queue.front();\nqueue.pop();              // \u961f\u5217\u51fa\u961f\nvec.push_back(node->val); // \u4fdd\u5b58\u8282\u70b9\u503c\nif (node->left != nullptr)\nqueue.push(node->left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\nif (node->right != nullptr)\nqueue.push(node->right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n}\nreturn vec;\n}\n
    binary_tree_bfs.py
    def level_order(root: TreeNode | None) -> list[int]:\n\"\"\"\u5c42\u5e8f\u904d\u5386\"\"\"\n# \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\nqueue: deque[TreeNode] = deque()\nqueue.append(root)\n# \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nres = []\nwhile queue:\nnode: TreeNode = queue.popleft()  # \u961f\u5217\u51fa\u961f\nres.append(node.val)  # \u4fdd\u5b58\u8282\u70b9\u503c\nif node.left is not None:\nqueue.append(node.left)  # \u5de6\u5b50\u8282\u70b9\u5165\u961f\nif node.right is not None:\nqueue.append(node.right)  # \u53f3\u5b50\u8282\u70b9\u5165\u961f\nreturn res\n
    binary_tree_bfs.go
    /* \u5c42\u5e8f\u904d\u5386 */\nfunc levelOrder(root *TreeNode) []int {\n// \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\nqueue := list.New()\nqueue.PushBack(root)\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5207\u7247\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nnums := make([]int, 0)\nfor queue.Len() > 0 {\n// \u961f\u5217\u51fa\u961f\nnode := queue.Remove(queue.Front()).(*TreeNode)\n// \u4fdd\u5b58\u8282\u70b9\u503c\nnums = append(nums, node.Val)\nif node.Left != nil {\n// \u5de6\u5b50\u8282\u70b9\u5165\u961f\nqueue.PushBack(node.Left)\n}\nif node.Right != nil {\n// \u53f3\u5b50\u8282\u70b9\u5165\u961f\nqueue.PushBack(node.Right)\n}\n}\nreturn nums\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\nconst queue = [root];\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nconst list = [];\nwhile (queue.length) {\nlet node = queue.shift(); // \u961f\u5217\u51fa\u961f\nlist.push(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\nif (node.left) queue.push(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\nif (node.right) queue.push(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n}\nreturn 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\nconst queue = [root];\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nconst list: number[] = [];\nwhile (queue.length) {\nlet node = queue.shift() as TreeNode; // \u961f\u5217\u51fa\u961f\nlist.push(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\nif (node.left) {\nqueue.push(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n}\nif (node.right) {\nqueue.push(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n}\n}\nreturn list;\n}\n
    binary_tree_bfs.c
    /* \u5c42\u5e8f\u904d\u5386 */\nint *levelOrder(TreeNode *root, int *size) {\n/* \u8f85\u52a9\u961f\u5217 */\nint front, rear;\nint index, *arr;\nTreeNode *node;\nTreeNode **queue;\n/* \u8f85\u52a9\u961f\u5217 */\nqueue = (TreeNode **)malloc(sizeof(TreeNode *) * MAX_NODE_SIZE);\n// \u961f\u5217\u6307\u9488\nfront = 0, rear = 0;\n// \u52a0\u5165\u6839\u8282\u70b9\nqueue[rear++] = root;\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n/* \u8f85\u52a9\u6570\u7ec4 */\narr = (int *)malloc(sizeof(int) * MAX_NODE_SIZE);\n// \u6570\u7ec4\u6307\u9488\nindex = 0;\nwhile (front < rear) {\n// \u961f\u5217\u51fa\u961f\nnode = queue[front++];\n// \u4fdd\u5b58\u8282\u70b9\u503c\narr[index++] = node->val;\nif (node->left != NULL) {\n// \u5de6\u5b50\u8282\u70b9\u5165\u961f\nqueue[rear++] = node->left;\n}\nif (node->right != NULL) {\n// \u53f3\u5b50\u8282\u70b9\u5165\u961f\nqueue[rear++] = node->right;\n}\n}\n// \u66f4\u65b0\u6570\u7ec4\u957f\u5ea6\u7684\u503c\n*size = index;\narr = realloc(arr, sizeof(int) * (*size));\n// \u91ca\u653e\u8f85\u52a9\u6570\u7ec4\u7a7a\u95f4\nfree(queue);\nreturn arr;\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\nQueue<TreeNode> queue = new();\nqueue.Enqueue(root);\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nList<int> list = new();\nwhile (queue.Count != 0) {\nTreeNode node = queue.Dequeue(); // \u961f\u5217\u51fa\u961f\nlist.Add(node.val);              // \u4fdd\u5b58\u8282\u70b9\u503c\nif (node.left != null)\nqueue.Enqueue(node.left);    // \u5de6\u5b50\u8282\u70b9\u5165\u961f\nif (node.right != null)\nqueue.Enqueue(node.right);   // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n}\nreturn list;\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\nvar queue: [TreeNode] = [root]\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nvar list: [Int] = []\nwhile !queue.isEmpty {\nlet node = queue.removeFirst() // \u961f\u5217\u51fa\u961f\nlist.append(node.val) // \u4fdd\u5b58\u8282\u70b9\u503c\nif let left = node.left {\nqueue.append(left) // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n}\nif let right = node.right {\nqueue.append(right) // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n}\n}\nreturn list\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\nconst L = std.TailQueue(*inc.TreeNode(T));\nvar queue = L{};\nvar root_node = try mem_allocator.create(L.Node);\nroot_node.data = root;\nqueue.append(root_node); // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nvar list = std.ArrayList(T).init(std.heap.page_allocator);\nwhile (queue.len > 0) {\nvar queue_node = queue.popFirst().?;    // \u961f\u5217\u51fa\u961f\nvar node = queue_node.data;\ntry list.append(node.val);              // \u4fdd\u5b58\u8282\u70b9\u503c\nif (node.left != null) {\nvar tmp_node = try mem_allocator.create(L.Node);\ntmp_node.data = node.left.?;\nqueue.append(tmp_node);             // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n}\nif (node.right != null) {\nvar tmp_node = try mem_allocator.create(L.Node);\ntmp_node.data = node.right.?;\nqueue.append(tmp_node);             // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n}        }\nreturn 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\nQueue<TreeNode?> queue = Queue();\nqueue.add(root);\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nList<int> res = [];\nwhile (queue.isNotEmpty) {\nTreeNode? node = queue.removeFirst(); // \u961f\u5217\u51fa\u961f\nres.add(node!.val); // \u4fdd\u5b58\u8282\u70b9\u503c\nif (node.left != null) queue.add(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\nif (node.right != null) queue.add(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n}\nreturn res;\n}\n
    "},{"location":"chapter_tree/binary_tree_traversal/#_2","title":"\u590d\u6742\u5ea6\u5206\u6790","text":"

    \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u6240\u6709\u8282\u70b9\u88ab\u8bbf\u95ee\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff0c\u5176\u4e2d \\(n\\) \u4e3a\u8282\u70b9\u6570\u91cf\u3002

    \u7a7a\u95f4\u590d\u6742\u5ea6\uff1a\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u5373\u6ee1\u4e8c\u53c9\u6811\u65f6\uff0c\u904d\u5386\u5230\u6700\u5e95\u5c42\u4e4b\u524d\uff0c\u961f\u5217\u4e2d\u6700\u591a\u540c\u65f6\u5b58\u5728 \\(\\frac{n + 1}{2}\\) \u4e2a\u8282\u70b9\uff0c\u5360\u7528 \\(O(n)\\) \u7a7a\u95f4\u3002

    "},{"location":"chapter_tree/binary_tree_traversal/#722","title":"7.2.2. \u00a0 \u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386","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\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

    \u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5de6\u4fa7\u662f\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u793a\u610f\u56fe\uff0c\u53f3\u4e0a\u65b9\u662f\u5bf9\u5e94\u7684\u9012\u5f52\u5b9e\u73b0\u4ee3\u7801\u3002\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u5c31\u50cf\u662f\u7ed5\u7740\u6574\u4e2a\u4e8c\u53c9\u6811\u7684\u5916\u56f4\u201c\u8d70\u201d\u4e00\u5708\uff0c\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\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

    Fig. \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u524d\u3001\u4e2d\u3001\u540e\u5e8f\u904d\u5386

    \u4f4d\u7f6e \u542b\u4e49 \u6b64\u5904\u8bbf\u95ee\u8282\u70b9\u65f6\u5bf9\u5e94 \u6a59\u8272\u5706\u5708\u5904 \u521a\u8fdb\u5165\u6b64\u8282\u70b9\uff0c\u5373\u5c06\u8bbf\u95ee\u8be5\u8282\u70b9\u7684\u5de6\u5b50\u6811 \u524d\u5e8f\u904d\u5386 Pre-Order Traversal \u84dd\u8272\u5706\u5708\u5904 \u5df2\u8bbf\u95ee\u5b8c\u5de6\u5b50\u6811\uff0c\u5373\u5c06\u8bbf\u95ee\u53f3\u5b50\u6811 \u4e2d\u5e8f\u904d\u5386 In-Order Traversal \u7d2b\u8272\u5706\u5708\u5904 \u5df2\u8bbf\u95ee\u5b8c\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\uff0c\u5373\u5c06\u8fd4\u56de \u540e\u5e8f\u904d\u5386 Post-Order Traversal"},{"location":"chapter_tree/binary_tree_traversal/#_3","title":"\u7b97\u6cd5\u5b9e\u73b0","text":"JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart binary_tree_dfs.java
    /* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode root) {\nif (root == null)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nlist.add(root.val);\npreOrder(root.left);\npreOrder(root.right);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode root) {\nif (root == null)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\ninOrder(root.left);\nlist.add(root.val);\ninOrder(root.right);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode root) {\nif (root == null)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\npostOrder(root.left);\npostOrder(root.right);\nlist.add(root.val);\n}\n
    binary_tree_dfs.cpp
    /* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode *root) {\nif (root == nullptr)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nvec.push_back(root->val);\npreOrder(root->left);\npreOrder(root->right);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode *root) {\nif (root == nullptr)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\ninOrder(root->left);\nvec.push_back(root->val);\ninOrder(root->right);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode *root) {\nif (root == nullptr)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\npostOrder(root->left);\npostOrder(root->right);\nvec.push_back(root->val);\n}\n
    binary_tree_dfs.py
    def pre_order(root: TreeNode | None) -> None:\n\"\"\"\u524d\u5e8f\u904d\u5386\"\"\"\nif root is None:\nreturn\n# \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nres.append(root.val)\npre_order(root=root.left)\npre_order(root=root.right)\ndef in_order(root: TreeNode | None) -> None:\n\"\"\"\u4e2d\u5e8f\u904d\u5386\"\"\"\nif root is None:\nreturn\n# \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\nin_order(root=root.left)\nres.append(root.val)\nin_order(root=root.right)\ndef post_order(root: TreeNode | None) -> None:\n\"\"\"\u540e\u5e8f\u904d\u5386\"\"\"\nif root is None:\nreturn\n# \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\npost_order(root=root.left)\npost_order(root=root.right)\nres.append(root.val)\n
    binary_tree_dfs.go
    /* \u524d\u5e8f\u904d\u5386 */\nfunc preOrder(node *TreeNode) {\nif node == nil {\nreturn\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nnums = append(nums, node.Val)\npreOrder(node.Left)\npreOrder(node.Right)\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc inOrder(node *TreeNode) {\nif node == nil {\nreturn\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\ninOrder(node.Left)\nnums = append(nums, node.Val)\ninOrder(node.Right)\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfunc postOrder(node *TreeNode) {\nif node == nil {\nreturn\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\npostOrder(node.Left)\npostOrder(node.Right)\nnums = append(nums, node.Val)\n}\n
    binary_tree_dfs.js
    /* \u524d\u5e8f\u904d\u5386 */\nfunction preOrder(root) {\nif (root === null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nlist.push(root.val);\npreOrder(root.left);\npreOrder(root.right);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunction inOrder(root) {\nif (root === null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\ninOrder(root.left);\nlist.push(root.val);\ninOrder(root.right);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfunction postOrder(root) {\nif (root === null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\npostOrder(root.left);\npostOrder(root.right);\nlist.push(root.val);\n}\n
    binary_tree_dfs.ts
    /* \u524d\u5e8f\u904d\u5386 */\nfunction preOrder(root: TreeNode | null): void {\nif (root === null) {\nreturn;\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nlist.push(root.val);\npreOrder(root.left);\npreOrder(root.right);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunction inOrder(root: TreeNode | null): void {\nif (root === null) {\nreturn;\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\ninOrder(root.left);\nlist.push(root.val);\ninOrder(root.right);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfunction postOrder(root: TreeNode | null): void {\nif (root === null) {\nreturn;\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\npostOrder(root.left);\npostOrder(root.right);\nlist.push(root.val);\n}\n
    binary_tree_dfs.c
    /* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode *root, int *size) {\nif (root == NULL)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\narr[(*size)++] = root->val;\npreOrder(root->left, size);\npreOrder(root->right, size);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode *root, int *size) {\nif (root == NULL)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\ninOrder(root->left, size);\narr[(*size)++] = root->val;\ninOrder(root->right, size);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode *root, int *size) {\nif (root == NULL)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\npostOrder(root->left, size);\npostOrder(root->right, size);\narr[(*size)++] = root->val;\n}\n
    binary_tree_dfs.cs
    /* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode? root) {\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nlist.Add(root.val);\npreOrder(root.left);\npreOrder(root.right);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode? root) {\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\ninOrder(root.left);\nlist.Add(root.val);\ninOrder(root.right);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode? root) {\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\npostOrder(root.left);\npostOrder(root.right);\nlist.Add(root.val);\n}\n
    binary_tree_dfs.swift
    /* \u524d\u5e8f\u904d\u5386 */\nfunc preOrder(root: TreeNode?) {\nguard let root = root else {\nreturn\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nlist.append(root.val)\npreOrder(root: root.left)\npreOrder(root: root.right)\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc inOrder(root: TreeNode?) {\nguard let root = root else {\nreturn\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\ninOrder(root: root.left)\nlist.append(root.val)\ninOrder(root: root.right)\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfunc postOrder(root: TreeNode?) {\nguard let root = root else {\nreturn\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\npostOrder(root: root.left)\npostOrder(root: root.right)\nlist.append(root.val)\n}\n
    binary_tree_dfs.zig
    // \u524d\u5e8f\u904d\u5386\nfn preOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\ntry list.append(root.?.val);\ntry preOrder(T, root.?.left);\ntry preOrder(T, root.?.right);\n}\n// \u4e2d\u5e8f\u904d\u5386\nfn inOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\ntry inOrder(T, root.?.left);\ntry list.append(root.?.val);\ntry inOrder(T, root.?.right);\n}\n// \u540e\u5e8f\u904d\u5386\nfn postOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\ntry postOrder(T, root.?.left);\ntry postOrder(T, root.?.right);\ntry list.append(root.?.val);\n}\n
    binary_tree_dfs.dart
    /* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode? node) {\nif (node == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nlist.add(node.val);\npreOrder(node.left);\npreOrder(node.right);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode? node) {\nif (node == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\ninOrder(node.left);\nlist.add(node.val);\ninOrder(node.right);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode? node) {\nif (node == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\npostOrder(node.left);\npostOrder(node.right);\nlist.add(node.val);\n}\n

    Note

    \u6211\u4eec\u4e5f\u53ef\u4ee5\u4ec5\u57fa\u4e8e\u5faa\u73af\u5b9e\u73b0\u524d\u3001\u4e2d\u3001\u540e\u5e8f\u904d\u5386\uff0c\u6709\u5174\u8da3\u7684\u540c\u5b66\u53ef\u4ee5\u81ea\u884c\u5b9e\u73b0\u3002

    \u9012\u5f52\u8fc7\u7a0b\u53ef\u5206\u4e3a\u201c\u9012\u201d\u548c\u201c\u5f52\u201d\u4e24\u4e2a\u76f8\u53cd\u7684\u90e8\u5206\u3002\u201c\u9012\u201d\u8868\u793a\u5f00\u542f\u65b0\u65b9\u6cd5\uff0c\u7a0b\u5e8f\u5728\u6b64\u8fc7\u7a0b\u4e2d\u8bbf\u95ee\u4e0b\u4e00\u4e2a\u8282\u70b9\uff1b\u201c\u5f52\u201d\u8868\u793a\u51fd\u6570\u8fd4\u56de\uff0c\u4ee3\u8868\u8be5\u8282\u70b9\u5df2\u7ecf\u8bbf\u95ee\u5b8c\u6bd5\u3002\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u4e3a\u524d\u5e8f\u904d\u5386\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b\u3002

    <1><2><3><4><5><6><7><8><9><10><11>

    "},{"location":"chapter_tree/binary_tree_traversal/#_4","title":"\u590d\u6742\u5ea6\u5206\u6790","text":"

    \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u6240\u6709\u8282\u70b9\u88ab\u8bbf\u95ee\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff0c\u5176\u4e2d \\(n\\) \u4e3a\u8282\u70b9\u6570\u91cf\u3002

    \u7a7a\u95f4\u590d\u6742\u5ea6\uff1a\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u5373\u6811\u9000\u5316\u4e3a\u94fe\u8868\u65f6\uff0c\u9012\u5f52\u6df1\u5ea6\u8fbe\u5230 \\(n\\) \uff0c\u7cfb\u7edf\u5360\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002

    "},{"location":"chapter_tree/summary/","title":"7.6. \u00a0 \u5c0f\u7ed3","text":"
    • \u4e8c\u53c9\u6811\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u4f53\u73b0\u201c\u4e00\u5206\u4e3a\u4e8c\u201d\u7684\u5206\u6cbb\u903b\u8f91\u3002\u6bcf\u4e2a\u4e8c\u53c9\u6811\u8282\u70b9\u5305\u542b\u4e00\u4e2a\u503c\u4ee5\u53ca\u4e24\u4e2a\u6307\u9488\uff0c\u5206\u522b\u6307\u5411\u5176\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\u3002
    • \u5bf9\u4e8e\u4e8c\u53c9\u6811\u4e2d\u7684\u67d0\u4e2a\u8282\u70b9\uff0c\u5176\u5de6\uff08\u53f3\uff09\u5b50\u8282\u70b9\u53ca\u5176\u4ee5\u4e0b\u5f62\u6210\u7684\u6811\u88ab\u79f0\u4e3a\u8be5\u8282\u70b9\u7684\u5de6\uff08\u53f3\uff09\u5b50\u6811\u3002
    • \u4e8c\u53c9\u6811\u7684\u76f8\u5173\u672f\u8bed\u5305\u62ec\u6839\u8282\u70b9\u3001\u53f6\u8282\u70b9\u3001\u5c42\u3001\u5ea6\u3001\u8fb9\u3001\u9ad8\u5ea6\u548c\u6df1\u5ea6\u7b49\u3002
    • \u4e8c\u53c9\u6811\u7684\u521d\u59cb\u5316\u3001\u8282\u70b9\u63d2\u5165\u548c\u8282\u70b9\u5220\u9664\u64cd\u4f5c\u4e0e\u94fe\u8868\u64cd\u4f5c\u65b9\u6cd5\u7c7b\u4f3c\u3002
    • \u5e38\u89c1\u7684\u4e8c\u53c9\u6811\u7c7b\u578b\u6709\u5b8c\u7f8e\u4e8c\u53c9\u6811\u3001\u5b8c\u5168\u4e8c\u53c9\u6811\u3001\u6ee1\u4e8c\u53c9\u6811\u548c\u5e73\u8861\u4e8c\u53c9\u6811\u3002\u5b8c\u7f8e\u4e8c\u53c9\u6811\u662f\u6700\u7406\u60f3\u7684\u72b6\u6001\uff0c\u800c\u94fe\u8868\u662f\u9000\u5316\u540e\u7684\u6700\u5dee\u72b6\u6001\u3002
    • \u4e8c\u53c9\u6811\u53ef\u4ee5\u7528\u6570\u7ec4\u8868\u793a\uff0c\u65b9\u6cd5\u662f\u5c06\u8282\u70b9\u503c\u548c\u7a7a\u4f4d\u6309\u5c42\u5e8f\u904d\u5386\u987a\u5e8f\u6392\u5217\uff0c\u5e76\u6839\u636e\u7236\u8282\u70b9\u4e0e\u5b50\u8282\u70b9\u4e4b\u95f4\u7684\u7d22\u5f15\u6620\u5c04\u5173\u7cfb\u6765\u5b9e\u73b0\u6307\u9488\u3002
    • \u4e8c\u53c9\u6811\u7684\u5c42\u5e8f\u904d\u5386\u662f\u4e00\u79cd\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u65b9\u6cd5\uff0c\u5b83\u4f53\u73b0\u4e86\u201c\u4e00\u5708\u4e00\u5708\u5411\u5916\u201d\u7684\u5206\u5c42\u904d\u5386\u65b9\u5f0f\uff0c\u901a\u5e38\u901a\u8fc7\u961f\u5217\u6765\u5b9e\u73b0\u3002
    • \u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386\u7686\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff0c\u5b83\u4eec\u4f53\u73b0\u4e86\u201c\u8d70\u5230\u5c3d\u5934\uff0c\u518d\u56de\u5934\u7ee7\u7eed\u201d\u7684\u56de\u6eaf\u904d\u5386\u65b9\u5f0f\uff0c\u901a\u5e38\u4f7f\u7528\u9012\u5f52\u6765\u5b9e\u73b0\u3002
    • \u4e8c\u53c9\u641c\u7d22\u6811\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u5143\u7d20\u67e5\u627e\u6570\u636e\u7ed3\u6784\uff0c\u5176\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(\\log n)\\) \u3002\u5f53\u4e8c\u53c9\u641c\u7d22\u6811\u9000\u5316\u4e3a\u94fe\u8868\u65f6\uff0c\u5404\u9879\u65f6\u95f4\u590d\u6742\u5ea6\u4f1a\u52a3\u5316\u81f3 \\(O(n)\\) \u3002
    • AVL \u6811\uff0c\u4e5f\u79f0\u4e3a\u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811\uff0c\u5b83\u901a\u8fc7\u65cb\u8f6c\u64cd\u4f5c\uff0c\u786e\u4fdd\u5728\u4e0d\u65ad\u63d2\u5165\u548c\u5220\u9664\u8282\u70b9\u540e\uff0c\u6811\u4ecd\u7136\u4fdd\u6301\u5e73\u8861\u3002
    • AVL \u6811\u7684\u65cb\u8f6c\u64cd\u4f5c\u5305\u62ec\u53f3\u65cb\u3001\u5de6\u65cb\u3001\u5148\u53f3\u65cb\u518d\u5de6\u65cb\u3001\u5148\u5de6\u65cb\u518d\u53f3\u65cb\u3002\u5728\u63d2\u5165\u6216\u5220\u9664\u8282\u70b9\u540e\uff0cAVL \u6811\u4f1a\u4ece\u5e95\u5411\u9876\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\u3002
    "}]} \ No newline at end of file +{"config":{"lang":["ja"],"separator":"[\\s\\-\uff0c\u3002]+","pipeline":["stemmer"]},"docs":[{"location":"","title":"Home","text":"

    \u300a Hello \u7b97\u6cd5 \u300b

    \u52a8\u753b\u56fe\u89e3\u3001\u80fd\u8fd0\u884c\u3001\u53ef\u63d0\u95ee\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u5feb\u901f\u5165\u95e8\u6559\u7a0b

    @Krahets

    \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

    \u300c\u5168\u4e66\u52a8\u753b\u56fe\u89e3\u300d

    \u5185\u5bb9\u6e05\u6670\u6613\u61c2\u3001\u5b66\u4e60\u66f2\u7ebf\u5e73\u6ed1\u7535\u8111\u3001\u5e73\u677f\u3001\u624b\u673a\u5168\u7ec8\u7aef\u9605\u8bfb

    \"A picture is worth a thousand words.\"

    \u201c\u4e00\u56fe\u80dc\u5343\u8a00\u201d

    \u300c\u4ee3\u7801\u4e00\u952e\u8fd0\u884c\u300d

    \u63d0\u4f9b\u5404\u4e2a\u7b97\u6cd5\u4e0e\u6570\u636e\u7ed3\u6784\u7684\u7b80\u6d01\u5b9e\u73b0\u4e0e\u6d4b\u8bd5\u6837\u4f8b\uff0c\u7686\u53ef\u76f4\u63a5\u8fd0\u884c\u652f\u6301 Java, C++, Python, Go, JS, TS, C#, Swift, Zig \u7b49\u8bed\u8a00

    \"Talk is cheap. Show me the code.\"

    \u201c\u5c11\u5439\u725b\uff0c\u770b\u4ee3\u7801\u201d

    \u300c\u53ef\u8ba8\u8bba\u4e0e\u63d0\u95ee\u300d

    \u9f13\u52b1\u5c0f\u4f19\u4f34\u4eec\u4e92\u5e2e\u4e92\u52a9\u3001\u5171\u540c\u6210\u957f\u63d0\u95ee\u4e0e\u8bc4\u8bba\u4e00\u822c\u80fd\u5728\u4e24\u65e5\u5185\u5f97\u5230\u56de\u590d

    \u201c\u8ffd\u98ce\u8d76\u6708\u83ab\u505c\u7559\uff0c\u5e73\u829c\u5c3d\u5904\u662f\u6625\u5c71\u201d

    \u4e00\u8d77\u52a0\u6cb9\uff01

    \u5e8f

    \u4e24\u5e74\u524d\uff0c\u6211\u5728\u529b\u6263\u4e0a\u5206\u4eab\u4e86\u300a\u5251\u6307 Offer\u300b\u7cfb\u5217\u9898\u89e3\uff0c\u53d7\u5230\u4e86\u8bb8\u591a\u670b\u53cb\u7684\u559c\u7231\u4e0e\u652f\u6301\u3002\u5728\u6b64\u671f\u95f4\uff0c\u6211\u56de\u7b54\u4e86\u4f17\u591a\u8bfb\u8005\u7684\u8bc4\u8bba\u95ee\u9898\uff0c\u5176\u4e2d\u6700\u5e38\u89c1\u7684\u4e00\u4e2a\u95ee\u9898\u662f\u201c\u5982\u4f55\u5165\u95e8\u5b66\u4e60\u7b97\u6cd5\u201d\u3002\u6211\u9010\u6e10\u4e5f\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\uff0c\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\u5730\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\u4e66\u7c4d\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\u5360\u636e\u4e86\u5927\u90e8\u5206\u7cbe\u529b\uff0c\u539a\u91cd\u7684\u4e66\u7c4d\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\u6b64\u95ee\u9898\u7684\u7ed9\u51fa\u7684\u7b54\u6848\uff0c\u867d\u7136\u4e0d\u4e00\u5b9a\u6b63\u786e\uff0c\u4f46\u81f3\u5c11\u662f\u4e00\u6b21\u79ef\u6781\u7684\u5c1d\u8bd5\u3002\u8fd9\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\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\u5e94\u5bf9\u5237\u9898\u548c\u9605\u8bfb\u6587\u732e\uff0c\u9010\u6b65\u6784\u5efa\u8d77\u5b8c\u6574\u7684\u77e5\u8bc6\u4f53\u7cfb\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\u5176 LeetBook\u300a\u56fe\u89e3\u7b97\u6cd5\u6570\u636e\u7ed3\u6784\u300b\u5df2\u88ab\u8ba2\u9605 24 \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\u6210\u957f\u3002\u611f\u8c22\u6bcf\u4e00\u4f4d\u6295\u5165\u65f6\u95f4\u4e0e\u7cbe\u529b\u7684\u64b0\u7a3f\u4eba\uff0c\u662f\u4ed6\u4eec\u65e0\u79c1\u5949\u732e\u4f7f\u8fd9\u672c\u4e66\u8d8a\u53d8\u8d8a\u597d\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, gvenusleo, justin\u2010tse, krahets, nuomi1, Reanon, 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 justin-tseJS / TS krahetsJava / Python nuomi1Swift ReanonGo / C sjinzhRust / Zig"},{"location":"chapter_appendix/contribution/","title":"13.2. \u00a0 \u4e00\u8d77\u53c2\u4e0e\u521b\u4f5c","text":"

    \u5f00\u6e90\u7684\u9b45\u529b

    \u7eb8\u8d28\u4e66\u7c4d\u7684\u4e24\u6b21\u5370\u5237\u7684\u95f4\u9694\u65f6\u95f4\u5f80\u5f80\u9700\u8981\u6570\u5e74\uff0c\u5185\u5bb9\u66f4\u65b0\u975e\u5e38\u4e0d\u65b9\u4fbf\u3002\u4f46\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

    \u7531\u4e8e\u4f5c\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\u5931\u6548\u94fe\u63a5\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\u5e2e\u52a9\u5176\u4ed6\u8bfb\u8005\u83b7\u5f97\u66f4\u4f18\u8d28\u7684\u5b66\u4e60\u8d44\u6e90\u3002\u6240\u6709\u64b0\u7a3f\u4eba\u5c06\u5728\u4ed3\u5e93\u548c\u7f51\u7ad9\u4e3b\u9875\u4e0a\u5c55\u793a\uff0c\u4ee5\u611f\u8c22\u4ed6\u4eec\u5bf9\u5f00\u6e90\u793e\u533a\u7684\u65e0\u79c1\u5949\u732e\uff01

    "},{"location":"chapter_appendix/contribution/#1321","title":"13.2.1. \u00a0 \u5185\u5bb9\u5fae\u8c03","text":"

    \u5728\u6bcf\u4e2a\u9875\u9762\u7684\u53f3\u4e0a\u89d2\u6709\u4e00\u4e2a\u300c\u7f16\u8f91\u300d\u56fe\u6807\uff0c\u60a8\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u4fee\u6539\u6587\u672c\u6216\u4ee3\u7801\uff1a

    1. \u70b9\u51fb\u7f16\u8f91\u6309\u94ae\uff0c\u5982\u679c\u9047\u5230\u201c\u9700\u8981 Fork \u6b64\u4ed3\u5e93\u201d\u7684\u63d0\u793a\uff0c\u8bf7\u540c\u610f\u8be5\u64cd\u4f5c\uff1b
    2. \u4fee\u6539 Markdown \u6e90\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u786e\u4fdd\u5185\u5bb9\u6b63\u786e\uff0c\u540c\u65f6\u5c3d\u91cf\u4fdd\u6301\u6392\u7248\u683c\u5f0f\u7684\u7edf\u4e00\uff1b
    3. \u5728\u9875\u9762\u5e95\u90e8\u586b\u5199\u4fee\u6539\u8bf4\u660e\uff0c\u7136\u540e\u70b9\u51fb\u201cPropose file change\u201d\u6309\u94ae\uff1b\u9875\u9762\u8df3\u8f6c\u540e\uff0c\u70b9\u51fb\u201cCreate pull request\u201d\u6309\u94ae\u5373\u53ef\u53d1\u8d77\u62c9\u53d6\u8bf7\u6c42\u3002

    Fig. \u9875\u9762\u7f16\u8f91\u6309\u952e

    \u7531\u4e8e\u56fe\u7247\u65e0\u6cd5\u76f4\u63a5\u4fee\u6539\uff0c\u56e0\u6b64\u9700\u8981\u901a\u8fc7\u65b0\u5efa Issue \u6216\u8bc4\u8bba\u7559\u8a00\u6765\u63cf\u8ff0\u56fe\u7247\u95ee\u9898\uff0c\u6211\u4eec\u4f1a\u5c3d\u5feb\u91cd\u65b0\u7ed8\u5236\u5e76\u66ff\u6362\u56fe\u7247\u3002

    "},{"location":"chapter_appendix/contribution/#1322","title":"13.2.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 Pull Request \u5de5\u4f5c\u6d41\u7a0b\uff1a

    1. \u767b\u5f55 GitHub \uff0c\u5c06\u672c\u4ed3\u5e93 Fork \u5230\u4e2a\u4eba\u8d26\u53f7\u4e0b\uff1b
    2. \u8fdb\u5165\u60a8\u7684 Fork \u4ed3\u5e93\u7f51\u9875\uff0c\u4f7f\u7528 git clone \u547d\u4ee4\u5c06\u4ed3\u5e93\u514b\u9686\u81f3\u672c\u5730\uff1b
    3. \u5728\u672c\u5730\u8fdb\u884c\u5185\u5bb9\u521b\u4f5c\uff0c\u5e76\u901a\u8fc7\u8fd0\u884c\u6d4b\u8bd5\u4ee5\u9a8c\u8bc1\u4ee3\u7801\u7684\u6b63\u786e\u6027\uff1b
    4. \u5c06\u672c\u5730\u6240\u505a\u66f4\u6539 Commit \uff0c\u7136\u540e Push \u81f3\u8fdc\u7a0b\u4ed3\u5e93\uff1b
    5. \u5237\u65b0\u4ed3\u5e93\u7f51\u9875\uff0c\u70b9\u51fb\u201cCreate pull request\u201d\u6309\u94ae\u5373\u53ef\u53d1\u8d77\u62c9\u53d6\u8bf7\u6c42\uff1b
    "},{"location":"chapter_appendix/contribution/#1323-docker","title":"13.2.3. \u00a0 Docker \u90e8\u7f72","text":"

    \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 Docker \u6765\u90e8\u7f72\u672c\u9879\u76ee\u3002\u6267\u884c\u4ee5\u4e0b\u811a\u672c\uff0c\u7a0d\u7b49\u7247\u523b\u540e\uff0c\u5373\u53ef\u4f7f\u7528\u6d4f\u89c8\u5668\u6253\u5f00 http://localhost:8000 \u6765\u8bbf\u95ee\u672c\u9879\u76ee\u3002

    git clone https://github.com/krahets/hello-algo.git\ncd hello-algo\ndocker-compose up -d\n

    \u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5373\u53ef\u5220\u9664\u90e8\u7f72\u3002

    docker-compose down\n
    "},{"location":"chapter_appendix/installation/","title":"13.1. \u00a0 \u7f16\u7a0b\u73af\u5883\u5b89\u88c5","text":""},{"location":"chapter_appendix/installation/#1311-vscode","title":"13.1.1. \u00a0 VSCode","text":"

    \u672c\u4e66\u63a8\u8350\u4f7f\u7528\u5f00\u6e90\u8f7b\u91cf\u7684 VSCode \u4f5c\u4e3a\u672c\u5730 IDE \uff0c\u4e0b\u8f7d\u5e76\u5b89\u88c5 VSCode \u3002

    "},{"location":"chapter_appendix/installation/#1312-java","title":"13.1.2. \u00a0 Java \u73af\u5883","text":"
    1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 OpenJDK\uff08\u7248\u672c\u9700\u6ee1\u8db3 > JDK 9\uff09\u3002
    2. \u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 java \uff0c\u5b89\u88c5 Extension Pack for Java \u3002
    "},{"location":"chapter_appendix/installation/#1313-cc","title":"13.1.3. \u00a0 C/C++ \u73af\u5883","text":"
    1. Windows \u7cfb\u7edf\u9700\u8981\u5b89\u88c5 MinGW\uff08\u914d\u7f6e\u6559\u7a0b\uff09\uff0cMacOS \u81ea\u5e26 Clang \u65e0\u9700\u5b89\u88c5\u3002
    2. \u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 c++ \uff0c\u5b89\u88c5 C/C++ Extension Pack \u3002
    3. \uff08\u53ef\u9009\uff09\u6253\u5f00 Settings \u9875\u9762\uff0c\u641c\u7d22 Clang_format_fallback Style \u4ee3\u7801\u683c\u5f0f\u5316\u9009\u9879\uff0c\u8bbe\u7f6e\u4e3a { BasedOnStyle: Microsoft, BreakBeforeBraces: Attach } \u3002
    "},{"location":"chapter_appendix/installation/#1314-python","title":"13.1.4. \u00a0 Python \u73af\u5883","text":"
    1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 Miniconda3 \u3002
    2. \u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 python \uff0c\u5b89\u88c5 Python Extension Pack \u3002
    3. \uff08\u53ef\u9009\uff09\u5728\u547d\u4ee4\u884c\u8f93\u5165 pip install black \uff0c\u5b89\u88c5\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\u3002
    "},{"location":"chapter_appendix/installation/#1315-go","title":"13.1.5. \u00a0 Go \u73af\u5883","text":"
    1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 go \u3002
    2. \u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 go \uff0c\u5b89\u88c5 Go \u3002
    3. \u5feb\u6377\u952e Ctrl + 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\u3002
    "},{"location":"chapter_appendix/installation/#1316-javascript","title":"13.1.6. \u00a0 JavaScript \u73af\u5883","text":"
    1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 node.js \u3002
    2. \u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 javascript \uff0c\u5b89\u88c5 JavaScript (ES6) code snippets \u3002
    3. \uff08\u53ef\u9009\uff09\u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 Prettier \uff0c\u5b89\u88c5\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\u3002
    "},{"location":"chapter_appendix/installation/#1317-c","title":"13.1.7. \u00a0 C# \u73af\u5883","text":"
    1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 .Net 6.0 \uff1b
    2. \u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 c# \uff0c\u5b89\u88c5 c# \u3002
    "},{"location":"chapter_appendix/installation/#1318-swift","title":"13.1.8. \u00a0 Swift \u73af\u5883","text":"
    1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 Swift\uff1b
    2. \u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 swift \uff0c\u5b89\u88c5 Swift for Visual Studio Code\u3002
    "},{"location":"chapter_appendix/installation/#1319-rust","title":"13.1.9. \u00a0 Rust \u73af\u5883","text":"
    1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 Rust\uff1b
    2. \u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 rust \uff0c\u5b89\u88c5 rust-analyzer\u3002
    "},{"location":"chapter_array_and_linkedlist/","title":"4. \u00a0 \u6570\u7ec4\u4e0e\u94fe\u8868","text":""},{"location":"chapter_array_and_linkedlist/array/","title":"4.1. \u00a0 \u6570\u7ec4","text":"

    \u300c\u6570\u7ec4 Array\u300d\u662f\u4e00\u79cd\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u5176\u5c06\u76f8\u540c\u7c7b\u578b\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\u5143\u7d20\u7684\u300c\u7d22\u5f15 Index\u300d\u3002

    Fig. \u6570\u7ec4\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f

    \u6570\u7ec4\u521d\u59cb\u5316\u3002\u901a\u5e38\u6709\u65e0\u521d\u59cb\u503c\u548c\u7ed9\u5b9a\u521d\u59cb\u503c\u4e24\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5728\u672a\u7ed9\u5b9a\u521d\u59cb\u503c\u7684\u60c5\u51b5\u4e0b\uff0c\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\u901a\u5e38\u4f1a\u88ab\u521d\u59cb\u5316\u4e3a\u9ed8\u8ba4\u503c \\(0\\) \u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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.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\nint* arr1 = new int[5];\nint* nums1 = new int[5] { 1, 3, 2, 5, 4 };\n
    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.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.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.c
    int arr[5] = { 0 }; // { 0, 0, 0, 0, 0 }\nint nums[5] = { 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.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.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
    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
    "},{"location":"chapter_array_and_linkedlist/array/#411","title":"4.1.1. \u00a0 \u6570\u7ec4\u4f18\u70b9","text":"

    \u5728\u6570\u7ec4\u4e2d\u8bbf\u95ee\u5143\u7d20\u975e\u5e38\u9ad8\u6548\u3002\u7531\u4e8e\u6570\u7ec4\u5143\u7d20\u88ab\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\uff0c\u56e0\u6b64\u8ba1\u7b97\u6570\u7ec4\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\u975e\u5e38\u5bb9\u6613\u3002\u7ed9\u5b9a\u6570\u7ec4\u9996\u4e2a\u5143\u7d20\u7684\u5730\u5740\u548c\u67d0\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u516c\u5f0f\u8ba1\u7b97\u5f97\u5230\u8be5\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\uff0c\u4ece\u800c\u76f4\u63a5\u8bbf\u95ee\u6b64\u5143\u7d20\u3002

    Fig. \u6570\u7ec4\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\u8ba1\u7b97

    # \u5143\u7d20\u5185\u5b58\u5730\u5740 = \u6570\u7ec4\u5185\u5b58\u5730\u5740 + \u5143\u7d20\u957f\u5ea6 * \u5143\u7d20\u7d22\u5f15\nelementAddr = firtstElementAddr + elementLength * elementIndex\n

    \u4e3a\u4ec0\u4e48\u6570\u7ec4\u5143\u7d20\u7684\u7d22\u5f15\u8981\u4ece \\(0\\) \u5f00\u59cb\u7f16\u53f7\u5462\uff1f

    \u89c2\u5bdf\u4e0a\u56fe\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

    \u7136\u800c\uff0c\u4ece\u5730\u5740\u8ba1\u7b97\u516c\u5f0f\u7684\u89d2\u5ea6\u770b\uff0c\u7d22\u5f15\u672c\u8d28\u4e0a\u8868\u793a\u7684\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\u7d22\u5f15\u4e3a \\(0\\) \u4e5f\u662f\u5408\u7406\u7684\u3002

    \u8bbf\u95ee\u5143\u7d20\u7684\u9ad8\u6548\u6027\u5e26\u6765\u4e86\u8bf8\u591a\u4fbf\u5229\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u968f\u673a\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u5143\u7d20\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart array.java
    /* \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20 */\nint randomAccess(int[] nums) {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nint randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nint randomNum = nums[randomIndex];\nreturn randomNum;\n}\n
    array.cpp
    /* \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20 */\nint randomAccess(int *nums, int size) {\n// \u5728\u533a\u95f4 [0, size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nint randomIndex = rand() % size;\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nint randomNum = nums[randomIndex];\nreturn randomNum;\n}\n
    array.py
    def 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\nrandom_index = random.randint(0, len(nums) - 1)\n# \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nrandom_num = nums[random_index]\nreturn random_num\n
    array.go
    /* \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20 */\nfunc randomAccess(nums []int) (randomNum int) {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nrandomIndex := rand.Intn(len(nums))\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nrandomNum = nums[randomIndex]\nreturn\n}\n
    array.js
    /* \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20 */\nfunction randomAccess(nums) {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nconst random_index = Math.floor(Math.random() * nums.length);\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nconst random_num = nums[random_index];\nreturn random_num;\n}\n
    array.ts
    /* \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20 */\nfunction randomAccess(nums: number[]): number {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nconst random_index = Math.floor(Math.random() * nums.length);\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nconst random_num = nums[random_index];\nreturn random_num;\n}\n
    array.c
    /* \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20 */\nint randomAccess(int *nums, int size) {\n// \u5728\u533a\u95f4 [0, size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nint randomIndex = rand() % size;\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nint randomNum = nums[randomIndex];\nreturn randomNum;\n}\n
    array.cs
    /* \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20 */\nint randomAccess(int[] nums) {\nRandom random = new();\n// \u5728\u533a\u95f4 [0, nums.Length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nint randomIndex = random.Next(nums.Length);\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nint randomNum = nums[randomIndex];\nreturn randomNum;\n}\n
    array.swift
    /* \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20 */\nfunc randomAccess(nums: [Int]) -> Int {\n// \u5728\u533a\u95f4 [0, nums.count) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nlet randomIndex = nums.indices.randomElement()!\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nlet randomNum = nums[randomIndex]\nreturn randomNum\n}\n
    array.zig
    // \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20\nfn randomAccess(nums: []i32) i32 {\n// \u5728\u533a\u95f4 [0, nums.len) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6574\u6570\nvar randomIndex = std.crypto.random.intRangeLessThan(usize, 0, nums.len);\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nvar randomNum = nums[randomIndex];\nreturn randomNum;\n}\n
    array.dart
    /* \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20 */\nint randomAccess(List nums) {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nint randomIndex = Random().nextInt(nums.length);\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nint randomNum = nums[randomIndex];\nreturn randomNum;\n}\n
    "},{"location":"chapter_array_and_linkedlist/array/#412","title":"4.1.2. \u00a0 \u6570\u7ec4\u7f3a\u70b9","text":"

    \u6570\u7ec4\u5728\u521d\u59cb\u5316\u540e\u957f\u5ea6\u4e0d\u53ef\u53d8\u3002\u7531\u4e8e\u7cfb\u7edf\u65e0\u6cd5\u4fdd\u8bc1\u6570\u7ec4\u4e4b\u540e\u7684\u5185\u5b58\u7a7a\u95f4\u662f\u53ef\u7528\u7684\uff0c\u56e0\u6b64\u6570\u7ec4\u957f\u5ea6\u65e0\u6cd5\u6269\u5c55\u3002\u800c\u82e5\u5e0c\u671b\u6269\u5bb9\u6570\u7ec4\uff0c\u5219\u9700\u65b0\u5efa\u4e00\u4e2a\u6570\u7ec4\uff0c\u7136\u540e\u628a\u539f\u6570\u7ec4\u5143\u7d20\u4f9d\u6b21\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\uff0c\u5728\u6570\u7ec4\u5f88\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd9\u662f\u975e\u5e38\u8017\u65f6\u7684\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nint[] res = new int[nums.length + enlarge];\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor (int i = 0; i < nums.length; i++) {\nres[i] = nums[i];\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res;\n}\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\nint *res = new int[size + enlarge];\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor (int i = 0; i < size; i++) {\nres[i] = nums[i];\n}\n// \u91ca\u653e\u5185\u5b58\ndelete[] nums;\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res;\n}\n
    array.py
    def 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\nres = [0] * (len(nums) + enlarge)\n# \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor i in range(len(nums)):\nres[i] = nums[i]\n# \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res\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\nres := make([]int, len(nums)+enlarge)\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor i, num := range nums {\nres[i] = num\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn 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\u662f\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\nconst 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\nfor (let i = 0; i < nums.length; i++) {\nres[i] = nums[i];\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn 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\u662f\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\nconst 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\nfor (let i = 0; i < nums.length; i++) {\nres[i] = nums[i];\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn 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\nint *res = (int *)malloc(sizeof(int) * (size + enlarge));\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor (int i = 0; i < size; i++) {\nres[i] = nums[i];\n}\n// \u521d\u59cb\u5316\u6269\u5c55\u540e\u7684\u7a7a\u95f4\nfor (int i = size; i < size + enlarge; i++) {\nres[i] = 0;\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn 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\nint[] res = new int[nums.Length + enlarge];\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor (int i = 0; i < nums.Length; i++) {\nres[i] = nums[i];\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn 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\nvar 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\nfor i in nums.indices {\nres[i] = nums[i]\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn 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\nvar res = try mem_allocator.alloc(i32, nums.len + enlarge);\nstd.mem.set(i32, res, 0);\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nstd.mem.copy(i32, res, nums);\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res;\n}\n
    array.dart
    /* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nList extend(List nums, int enlarge) {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\nList<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\nfor (var i = 0; i < nums.length; i++) {\nres[i] = nums[i];\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res;\n}\n

    \u6570\u7ec4\u4e2d\u63d2\u5165\u6216\u5220\u9664\u5143\u7d20\u6548\u7387\u4f4e\u4e0b\u3002\u5982\u679c\u6211\u4eec\u60f3\u8981\u5728\u6570\u7ec4\u4e2d\u95f4\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\uff0c\u7531\u4e8e\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\u653e\u4efb\u4f55\u6570\u636e\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4e0d\u5f97\u4e0d\u5c06\u6b64\u7d22\u5f15\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u7136\u540e\u518d\u628a\u5143\u7d20\u8d4b\u503c\u7ed9\u8be5\u7d22\u5f15\u3002

    Fig. \u6570\u7ec4\u63d2\u5165\u5143\u7d20

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nfor (int i = nums.length - 1; i > index; i--) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num;\n}\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\nfor (int i = size - 1; i > index; i--) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num;\n}\n
    array.py
    def insert(nums: list[int], num: int, index: int) -> None:\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\nfor i in range(len(nums) - 1, index, -1):\nnums[i] = nums[i - 1]\n# \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num\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\nfor i := len(nums) - 1; i > index; i-- {\nnums[i] = nums[i-1]\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[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\nfor (let i = nums.length - 1; i > index; i--) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[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\nfor (let i = nums.length - 1; i > index; i--) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[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\nfor (int i = size - 1; i > index; i--) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[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\nfor (int i = nums.Length - 1; i > index; i--) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[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\nfor i in sequence(first: nums.count - 1, next: { $0 > index + 1 ? $0 - 1 : nil }) {\nnums[i] = nums[i - 1]\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[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\nvar i = nums.len - 1;\nwhile (i > index) : (i -= 1) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num;\n}\n
    array.dart
    /* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(List 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\nfor (var i = nums.length - 1; i > index; i--) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num;\n}\n

    \u5220\u9664\u5143\u7d20\u4e5f\u7c7b\u4f3c\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\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\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5220\u9664\u5143\u7d20\u540e\uff0c\u539f\u5148\u672b\u5c3e\u7684\u5143\u7d20\u53d8\u5f97\u201c\u65e0\u610f\u4e49\u201d\u4e86\uff0c\u6211\u4eec\u65e0\u9700\u7279\u610f\u53bb\u4fee\u6539\u5b83\u3002

    Fig. \u6570\u7ec4\u5220\u9664\u5143\u7d20

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart array.java
    /* \u5220\u9664\u7d22\u5f15 index \u5904\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\nfor (int i = index; i < nums.length - 1; i++) {\nnums[i] = nums[i + 1];\n}\n}\n
    array.cpp
    /* \u5220\u9664\u7d22\u5f15 index \u5904\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\nfor (int i = index; i < size - 1; i++) {\nnums[i] = nums[i + 1];\n}\n}\n
    array.py
    def remove(nums: list[int], index: int) -> None:\n\"\"\"\u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20\"\"\"\n# \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor i in range(index, len(nums) - 1):\nnums[i] = nums[i + 1]\n
    array.go
    /* \u5220\u9664\u7d22\u5f15 index \u5904\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\nfor i := index; i < len(nums)-1; i++ {\nnums[i] = nums[i+1]\n}\n}\n
    array.js
    /* \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 */\nfunction remove(nums, index) {\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (let i = index; i < nums.length - 1; i++) {\nnums[i] = nums[i + 1];\n}\n}\n
    array.ts
    /* \u5220\u9664\u7d22\u5f15 index \u5904\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\nfor (let i = index; i < nums.length - 1; i++) {\nnums[i] = nums[i + 1];\n}\n}\n
    array.c
    /* \u5220\u9664\u7d22\u5f15 index \u5904\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\nfor (int i = index; i < size - 1; i++) {\nnums[i] = nums[i + 1];\n}\n}\n
    array.cs
    /* \u5220\u9664\u7d22\u5f15 index \u5904\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\nfor (int i = index; i < nums.Length - 1; i++) {\nnums[i] = nums[i + 1];\n}\n}\n
    array.swift
    /* \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 */\nfunc remove(nums: inout [Int], index: Int) {\nlet count = nums.count\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor i in sequence(first: index, next: { $0 < count - 1 - 1 ? $0 + 1 : nil }) {\nnums[i] = nums[i + 1]\n}\n}\n
    array.zig
    // \u5220\u9664\u7d22\u5f15 index \u5904\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\nvar i = index;\nwhile (i < nums.len - 1) : (i += 1) {\nnums[i] = nums[i + 1];\n}\n}\n
    array.dart
    /* \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 */\nvoid remove(List nums, int index) {\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (var i = index; i < nums.length - 1; i++) {\nnums[i] = nums[i + 1];\n}\n}\n

    \u603b\u7ed3\u6765\u770b\uff0c\u6570\u7ec4\u7684\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u6709\u4ee5\u4e0b\u7f3a\u70b9\uff1a

    • \u65f6\u95f4\u590d\u6742\u5ea6\u9ad8\uff1a\u6570\u7ec4\u7684\u63d2\u5165\u548c\u5220\u9664\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u6570\u7ec4\u957f\u5ea6\u3002
    • \u4e22\u5931\u5143\u7d20\uff1a\u7531\u4e8e\u6570\u7ec4\u7684\u957f\u5ea6\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u5728\u63d2\u5165\u5143\u7d20\u540e\uff0c\u8d85\u51fa\u6570\u7ec4\u957f\u5ea6\u8303\u56f4\u7684\u5143\u7d20\u4f1a\u4e22\u5931\u3002
    • \u5185\u5b58\u6d6a\u8d39\uff1a\u6211\u4eec\u53ef\u4ee5\u521d\u59cb\u5316\u4e00\u4e2a\u6bd4\u8f83\u957f\u7684\u6570\u7ec4\uff0c\u53ea\u7528\u524d\u9762\u4e00\u90e8\u5206\uff0c\u8fd9\u6837\u5728\u63d2\u5165\u6570\u636e\u65f6\uff0c\u4e22\u5931\u7684\u672b\u5c3e\u5143\u7d20\u90fd\u662f\u6211\u4eec\u4e0d\u5173\u5fc3\u7684\uff0c\u4f46\u8fd9\u6837\u505a\u540c\u65f6\u4e5f\u4f1a\u9020\u6210\u5185\u5b58\u7a7a\u95f4\u7684\u6d6a\u8d39\u3002
    "},{"location":"chapter_array_and_linkedlist/array/#413","title":"4.1.3. \u00a0 \u6570\u7ec4\u5e38\u7528\u64cd\u4f5c","text":"

    \u6570\u7ec4\u904d\u5386\u3002\u4ee5\u4e0b\u4ecb\u7ecd\u4e24\u79cd\u5e38\u7528\u7684\u904d\u5386\u65b9\u6cd5\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart array.java
    /* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int[] nums) {\nint count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i < nums.length; i++) {\ncount++;\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (int num : nums) {\ncount++;\n}\n}\n
    array.cpp
    /* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int *nums, int size) {\nint count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i < size; i++) {\ncount++;\n}\n}\n
    array.py
    def traverse(nums: list[int]) -> None:\n\"\"\"\u904d\u5386\u6570\u7ec4\"\"\"\ncount = 0\n# \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor i in range(len(nums)):\ncount += 1\n# \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor num in nums:\ncount += 1\n# \u540c\u65f6\u904d\u5386\u6570\u636e\u7d22\u5f15\u548c\u5143\u7d20\nfor i, num in enumerate(nums):\ncount += 1\n
    array.go
    /* \u904d\u5386\u6570\u7ec4 */\nfunc traverse(nums []int) {\ncount := 0\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor i := 0; i < len(nums); i++ {\ncount++\n}\ncount = 0\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor range nums {\ncount++\n}\n}\n
    array.js
    /* \u904d\u5386\u6570\u7ec4 */\nfunction traverse(nums) {\nlet count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (let i = 0; i < nums.length; i++) {\ncount++;\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (let num of nums) {\ncount += 1;\n}\n}\n
    array.ts
    /* \u904d\u5386\u6570\u7ec4 */\nfunction traverse(nums: number[]): void {\nlet count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (let i = 0; i < nums.length; i++) {\ncount++;\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (let num of nums) {\ncount += 1;\n}\n}\n
    array.c
    /* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int *nums, int size) {\nint count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i < size; i++) {\ncount++;\n}\n}\n
    array.cs
    /* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int[] nums) {\nint count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i < nums.Length; i++) {\ncount++;\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nforeach (int num in nums) {\ncount++;\n}\n}\n
    array.swift
    /* \u904d\u5386\u6570\u7ec4 */\nfunc traverse(nums: [Int]) {\nvar count = 0\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor _ in nums.indices {\ncount += 1\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor _ in nums {\ncount += 1\n}\n}\n
    array.zig
    // \u904d\u5386\u6570\u7ec4\nfn traverse(nums: []i32) void {\nvar count: i32 = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nvar i: i32 = 0;\nwhile (i < nums.len) : (i += 1) {\ncount += 1;\n}\ncount = 0;\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (nums) |_| {\ncount += 1;\n}\n}\n
    array.dart
    /* \u904d\u5386\u6570\u7ec4\u5143\u7d20 */\nvoid traverse(List nums) {\nvar count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (var i = 0; i < nums.length; i++) {\ncount++;\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (var num in nums) {\ncount++;\n}\n// \u901a\u8fc7 forEach \u65b9\u6cd5\u904d\u5386\u6570\u7ec4\nnums.forEach((element) {\ncount++;\n});\n}\n

    \u6570\u7ec4\u67e5\u627e\u3002\u901a\u8fc7\u904d\u5386\u6570\u7ec4\uff0c\u67e5\u627e\u6570\u7ec4\u5185\u7684\u6307\u5b9a\u5143\u7d20\uff0c\u5e76\u8f93\u51fa\u5bf9\u5e94\u7d22\u5f15\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart array.java
    /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int[] nums, int target) {\nfor (int i = 0; i < nums.length; i++) {\nif (nums[i] == target)\nreturn i;\n}\nreturn -1;\n}\n
    array.cpp
    /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int *nums, int size, int target) {\nfor (int i = 0; i < size; i++) {\nif (nums[i] == target)\nreturn i;\n}\nreturn -1;\n}\n
    array.py
    def find(nums: list[int], target: int) -> int:\n\"\"\"\u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\"\"\"\nfor i in range(len(nums)):\nif nums[i] == target:\nreturn i\nreturn -1\n
    array.go
    /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunc find(nums []int, target int) (index int) {\nindex = -1\nfor i := 0; i < len(nums); i++ {\nif nums[i] == target {\nindex = i\nbreak\n}\n}\nreturn\n}\n
    array.js
    /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunction find(nums, target) {\nfor (let i = 0; i < nums.length; i++) {\nif (nums[i] == target) return i;\n}\nreturn -1;\n}\n
    array.ts
    /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunction find(nums: number[], target: number): number {\nfor (let i = 0; i < nums.length; i++) {\nif (nums[i] === target) {\nreturn i;\n}\n}\nreturn -1;\n}\n
    array.c
    /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int *nums, int size, int target) {\nfor (int i = 0; i < size; i++) {\nif (nums[i] == target)\nreturn i;\n}\nreturn -1;\n}\n
    array.cs
    /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int[] nums, int target) {\nfor (int i = 0; i < nums.Length; i++) {\nif (nums[i] == target)\nreturn i;\n}\nreturn -1;\n}\n
    array.swift
    /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunc find(nums: [Int], target: Int) -> Int {\nfor i in nums.indices {\nif nums[i] == target {\nreturn i\n}\n}\nreturn -1\n}\n
    array.zig
    // \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\nfn find(nums: []i32, target: i32) i32 {\nfor (nums) |num, i| {\nif (num == target) return @intCast(i32, i);\n}\nreturn -1;\n}\n
    array.dart
    /* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(List nums, int target) {\nfor (var i = 0; i < nums.length; i++) {\nif (nums[i] == target) return i;\n}\nreturn -1;\n}\n
    "},{"location":"chapter_array_and_linkedlist/array/#414","title":"4.1.4. \u00a0 \u6570\u7ec4\u5178\u578b\u5e94\u7528","text":"

    \u968f\u673a\u8bbf\u95ee\u3002\u5982\u679c\u6211\u4eec\u60f3\u8981\u968f\u673a\u62bd\u53d6\u4e00\u4e9b\u6837\u672c\uff0c\u90a3\u4e48\u53ef\u4ee5\u7528\u6570\u7ec4\u5b58\u50a8\uff0c\u5e76\u751f\u6210\u4e00\u4e2a\u968f\u673a\u5e8f\u5217\uff0c\u6839\u636e\u7d22\u5f15\u5b9e\u73b0\u6837\u672c\u7684\u968f\u673a\u62bd\u53d6\u3002

    \u4e8c\u5206\u67e5\u627e\u3002\u4f8b\u5982\u524d\u6587\u67e5\u5b57\u5178\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b57\u5178\u4e2d\u7684\u6240\u6709\u5b57\u6309\u7167\u62fc\u97f3\u987a\u5e8f\u5b58\u50a8\u5728\u6570\u7ec4\u4e2d\uff0c\u7136\u540e\u4f7f\u7528\u4e0e\u65e5\u5e38\u67e5\u7eb8\u8d28\u5b57\u5178\u76f8\u540c\u7684\u201c\u7ffb\u5f00\u4e2d\u95f4\uff0c\u6392\u9664\u4e00\u534a\u201d\u7684\u65b9\u5f0f\uff0c\u6765\u5b9e\u73b0\u4e00\u4e2a\u67e5\u7535\u5b50\u5b57\u5178\u7684\u7b97\u6cd5\u3002

    \u6df1\u5ea6\u5b66\u4e60\u3002\u795e\u7ecf\u7f51\u7edc\u4e2d\u5927\u91cf\u4f7f\u7528\u4e86\u5411\u91cf\u3001\u77e9\u9635\u3001\u5f20\u91cf\u4e4b\u95f4\u7684\u7ebf\u6027\u4ee3\u6570\u8fd0\u7b97\uff0c\u8fd9\u4e9b\u6570\u636e\u90fd\u662f\u4ee5\u6570\u7ec4\u7684\u5f62\u5f0f\u6784\u5efa\u7684\u3002\u6570\u7ec4\u662f\u795e\u7ecf\u7f51\u7edc\u7f16\u7a0b\u4e2d\u6700\u5e38\u4f7f\u7528\u7684\u6570\u636e\u7ed3\u6784\u3002

    "},{"location":"chapter_array_and_linkedlist/linked_list/","title":"4.2. \u00a0 \u94fe\u8868","text":"

    \u5185\u5b58\u7a7a\u95f4\u662f\u6240\u6709\u7a0b\u5e8f\u7684\u516c\u5171\u8d44\u6e90\uff0c\u6392\u9664\u5df2\u88ab\u5360\u7528\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u7a7a\u95f2\u5185\u5b58\u7a7a\u95f4\u901a\u5e38\u6563\u843d\u5728\u5185\u5b58\u5404\u5904\u3002\u5728\u4e0a\u4e00\u8282\u4e2d\uff0c\u6211\u4eec\u63d0\u5230\u5b58\u50a8\u6570\u7ec4\u7684\u5185\u5b58\u7a7a\u95f4\u5fc5\u987b\u662f\u8fde\u7eed\u7684\uff0c\u800c\u5f53\u6211\u4eec\u9700\u8981\u7533\u8bf7\u4e00\u4e2a\u975e\u5e38\u5927\u7684\u6570\u7ec4\u65f6\uff0c\u7a7a\u95f2\u5185\u5b58\u4e2d\u53ef\u80fd\u6ca1\u6709\u8fd9\u4e48\u5927\u7684\u8fde\u7eed\u7a7a\u95f4\u3002\u4e0e\u6570\u7ec4\u76f8\u6bd4\uff0c\u94fe\u8868\u66f4\u5177\u7075\u6d3b\u6027\uff0c\u5b83\u53ef\u4ee5\u88ab\u5b58\u50a8\u5728\u975e\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\u3002

    \u300c\u94fe\u8868 Linked List\u300d\u662f\u4e00\u79cd\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u5176\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a\u8282\u70b9\u5bf9\u8c61\uff0c\u5404\u4e2a\u8282\u70b9\u4e4b\u95f4\u901a\u8fc7\u6307\u9488\u8fde\u63a5\uff0c\u4ece\u5f53\u524d\u8282\u70b9\u901a\u8fc7\u6307\u9488\u53ef\u4ee5\u8bbf\u95ee\u5230\u4e0b\u4e00\u4e2a\u8282\u70b9\u3002\u7531\u4e8e\u6307\u9488\u8bb0\u5f55\u4e86\u4e0b\u4e2a\u8282\u70b9\u7684\u5185\u5b58\u5730\u5740\uff0c\u56e0\u6b64\u65e0\u9700\u4fdd\u8bc1\u5185\u5b58\u5730\u5740\u7684\u8fde\u7eed\u6027\uff0c\u4ece\u800c\u53ef\u4ee5\u5c06\u5404\u4e2a\u8282\u70b9\u5206\u6563\u5b58\u50a8\u5728\u5185\u5b58\u5404\u5904\u3002

    \u94fe\u8868\u300c\u8282\u70b9 Node\u300d\u5305\u542b\u4e24\u9879\u6570\u636e\uff0c\u4e00\u662f\u8282\u70b9\u300c\u503c Value\u300d\uff0c\u4e8c\u662f\u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u300c\u6307\u9488 Pointer\u300d\uff0c\u6216\u79f0\u300c\u5f15\u7528 Reference\u300d\u3002

    Fig. \u94fe\u8868\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    /* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nint val;        // \u8282\u70b9\u503c\nListNode next;  // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode(int x) { val = x; }  // \u6784\u9020\u51fd\u6570\n}\n
    /* \u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct ListNode {\nint val;         // \u8282\u70b9\u503c\nListNode *next;  // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode(int x) : val(x), next(nullptr) {}  // \u6784\u9020\u51fd\u6570\n};\n
    class ListNode:\n\"\"\"\u94fe\u8868\u8282\u70b9\u7c7b\"\"\"\ndef __init__(self, val: int):\nself.val: int = val                  # \u8282\u70b9\u503c\nself.next: Optional[ListNode] = None # \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n
    /* \u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype ListNode struct {\nVal  int       // \u8282\u70b9\u503c\nNext *ListNode // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n}\n// NewListNode \u6784\u9020\u51fd\u6570\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u94fe\u8868\nfunc NewListNode(val int) *ListNode {\nreturn &ListNode{\nVal:  val,\nNext: nil,\n}\n}\n
    /* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nval;\nnext;\nconstructor(val, next) {\nthis.val = (val === undefined ? 0 : val);       // \u8282\u70b9\u503c\nthis.next = (next === undefined ? null : next); // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n}\n}\n
    /* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nval: number;\nnext: ListNode | null;\nconstructor(val?: number, next?: ListNode | null) {\nthis.val = val === undefined ? 0 : val;        // \u8282\u70b9\u503c\nthis.next = next === undefined ? null : next;  // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n}\n}\n
    /* \u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct ListNode {\nint val;               // \u8282\u70b9\u503c\nstruct ListNode *next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n};\ntypedef struct ListNode ListNode;\n/* \u6784\u9020\u51fd\u6570 */\nListNode *newListNode(int val) {\nListNode *node, *next;\nnode = (ListNode *) malloc(sizeof(ListNode));\nnode->val = val;\nnode->next = NULL;\nreturn node;\n}\n
    /* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode\n{\nint val;         // \u8282\u70b9\u503c\nListNode next;   // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\nListNode(int x) => val = x;  //\u6784\u9020\u51fd\u6570\n}\n
    /* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nvar val: Int // \u8282\u70b9\u503c\nvar next: ListNode? // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\ninit(x: Int) { // \u6784\u9020\u51fd\u6570\nval = x\n}\n}\n
    // \u94fe\u8868\u8282\u70b9\u7c7b\npub fn ListNode(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nval: T = 0, // \u8282\u70b9\u503c\nnext: ?*Self = null, // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n// \u6784\u9020\u51fd\u6570\npub fn init(self: *Self, x: i32) void {\nself.val = x;\nself.next = null;\n}\n};\n}\n
    /* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nint val; // \u8282\u70b9\u503c\nListNode? next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode(this.val, [this.next]); // \u6784\u9020\u51fd\u6570\n}\n

    \u5c3e\u8282\u70b9\u6307\u5411\u4ec0\u4e48\uff1f

    \u6211\u4eec\u5c06\u94fe\u8868\u7684\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u79f0\u4e3a\u300c\u5c3e\u8282\u70b9\u300d\uff0c\u5176\u6307\u5411\u7684\u662f\u201c\u7a7a\u201d\uff0c\u5728 Java, C++, Python \u4e2d\u5206\u522b\u8bb0\u4e3a \\(\\text{null}\\) , \\(\\text{nullptr}\\) , \\(\\text{None}\\) \u3002\u5728\u4e0d\u5f15\u8d77\u6b67\u4e49\u7684\u524d\u63d0\u4e0b\uff0c\u672c\u4e66\u90fd\u4f7f\u7528 \\(\\text{null}\\) \u6765\u8868\u793a\u7a7a\u3002

    \u5982\u4f55\u79f0\u547c\u94fe\u8868\uff1f

    \u5728\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u6570\u7ec4\u6574\u4f53\u5c31\u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u4f8b\u5982\u6570\u7ec4 nums \uff0c\u5305\u542b\u5404\u4e2a\u5143\u7d20 nums[0] , nums[1] \u7b49\u7b49\u3002\u800c\u94fe\u8868\u662f\u7531\u591a\u4e2a\u8282\u70b9\u5bf9\u8c61\u7ec4\u6210\uff0c\u6211\u4eec\u901a\u5e38\u5c06\u5934\u8282\u70b9\u5f53\u4f5c\u94fe\u8868\u7684\u4ee3\u79f0\uff0c\u4f8b\u5982\u5934\u8282\u70b9 head \u548c\u94fe\u8868 head \u5b9e\u9645\u4e0a\u662f\u540c\u4e49\u7684\u3002

    \u94fe\u8868\u521d\u59cb\u5316\u65b9\u6cd5\u3002\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\u5f15\u7528\u6307\u5411\u5173\u7cfb\u3002\u5b8c\u6210\u540e\uff0c\u5373\u53ef\u4ee5\u4ece\u94fe\u8868\u7684\u5934\u8282\u70b9\uff08\u5373\u9996\u4e2a\u8282\u70b9\uff09\u51fa\u53d1\uff0c\u901a\u8fc7\u6307\u9488 next \u4f9d\u6b21\u8bbf\u95ee\u6240\u6709\u8282\u70b9\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\u5f15\u7528\u6307\u5411\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\u5f15\u7528\u6307\u5411\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n
    linked_list.py
    # \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\u5f15\u7528\u6307\u5411\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\u5f15\u7528\u6307\u5411\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\u5f15\u7528\u6307\u5411\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\u5f15\u7528\u6307\u5411\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\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\u5f15\u7528\u6307\u5411\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 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\u5f15\u7528\u6307\u5411\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\u5f15\u7528\u6307\u5411\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\u5f15\u7528\u6307\u5411\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\u5f15\u7528\u6307\u5411\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
    "},{"location":"chapter_array_and_linkedlist/linked_list/#421","title":"4.2.1. \u00a0 \u94fe\u8868\u4f18\u70b9","text":"

    \u94fe\u8868\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\u7684\u64cd\u4f5c\u6548\u7387\u9ad8\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u60f3\u5728\u94fe\u8868\u4e2d\u95f4\u7684\u4e24\u4e2a\u8282\u70b9 A , B \u4e4b\u95f4\u63d2\u5165\u4e00\u4e2a\u65b0\u8282\u70b9 P \uff0c\u6211\u4eec\u53ea\u9700\u8981\u6539\u53d8\u4e24\u4e2a\u8282\u70b9\u6307\u9488\u5373\u53ef\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff1b\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u6570\u7ec4\u7684\u63d2\u5165\u64cd\u4f5c\u6548\u7387\u8981\u4f4e\u5f97\u591a\u3002

    Fig. \u94fe\u8868\u63d2\u5165\u8282\u70b9

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart linked_list.java
    /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode n0, ListNode P) {\nListNode n1 = n0.next;\nP.next = n1;\nn0.next = P;\n}\n
    linked_list.cpp
    /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode *n0, ListNode *P) {\nListNode *n1 = n0->next;\nP->next = n1;\nn0->next = P;\n}\n
    linked_list.py
    def insert(n0: ListNode, P: ListNode) -> None:\n\"\"\"\u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P\"\"\"\nn1 = n0.next\nP.next = n1\nn0.next = P\n
    linked_list.go
    /* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunc insertNode(n0 *ListNode, P *ListNode) {\nn1 := n0.Next\nP.Next = n1\nn0.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) {\nconst n1 = n0.next;\nP.next = n1;\nn0.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 {\nconst n1 = n0.next;\nP.next = n1;\nn0.next = 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) {\nListNode *n1 = n0->next;\nP->next = n1;\nn0->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) {\nListNode? n1 = n0.next;\nP.next = n1;\nn0.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) {\nlet n1 = n0.next\nP.next = n1\nn0.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 {\nvar n1 = n0.?.next;\nP.?.next = n1;\nn0.?.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) {\nListNode? n1 = n0.next;\nP.next = n1;\nn0.next = P;\n}\n

    \u5728\u94fe\u8868\u4e2d\u5220\u9664\u8282\u70b9\u4e5f\u975e\u5e38\u65b9\u4fbf\uff0c\u53ea\u9700\u6539\u53d8\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488\u5373\u53ef\u3002\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5c3d\u7ba1\u5728\u5220\u9664\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u8282\u70b9 P \u4ecd\u7136\u6307\u5411 n1 \uff0c\u4f46\u5b9e\u9645\u4e0a P \u5df2\u7ecf\u4e0d\u518d\u5c5e\u4e8e\u6b64\u94fe\u8868\uff0c\u56e0\u4e3a\u904d\u5386\u6b64\u94fe\u8868\u65f6\u65e0\u6cd5\u8bbf\u95ee\u5230 P \u3002

    Fig. \u94fe\u8868\u5220\u9664\u8282\u70b9

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart linked_list.java
    /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode n0) {\nif (n0.next == null)\nreturn;\n// n0 -> P -> n1\nListNode P = n0.next;\nListNode n1 = P.next;\nn0.next = n1;\n}\n
    linked_list.cpp
    /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode *n0) {\nif (n0->next == nullptr)\nreturn;\n// n0 -> P -> n1\nListNode *P = n0->next;\nListNode *n1 = P->next;\nn0->next = n1;\n// \u91ca\u653e\u5185\u5b58\ndelete P;\n}\n
    linked_list.py
    def remove(n0: ListNode) -> None:\n\"\"\"\u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9\"\"\"\nif not n0.next:\nreturn\n# n0 -> P -> n1\nP = n0.next\nn1 = P.next\nn0.next = n1\n
    linked_list.go
    /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunc removeNode(n0 *ListNode) {\nif n0.Next == nil {\nreturn\n}\n// n0 -> P -> n1\nP := n0.Next\nn1 := P.Next\nn0.Next = n1\n}\n
    linked_list.js
    /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunction remove(n0) {\nif (!n0.next) return;\n// n0 -> P -> n1\nconst P = n0.next;\nconst n1 = P.next;\nn0.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 {\nif (!n0.next) {\nreturn;\n}\n// n0 -> P -> n1\nconst P = n0.next;\nconst n1 = P.next;\nn0.next = n1;\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 removeNode(ListNode *n0) {\nif (!n0->next)\nreturn;\n// n0 -> P -> n1\nListNode *P = n0->next;\nListNode *n1 = P->next;\nn0->next = n1;\n// \u91ca\u653e\u5185\u5b58\nfree(P);\n}\n
    linked_list.cs
    /* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode n0) {\nif (n0.next == null)\nreturn;\n// n0 -> P -> n1\nListNode P = n0.next;\nListNode? n1 = P.next;\nn0.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) {\nif n0.next == nil {\nreturn\n}\n// n0 -> P -> n1\nlet P = n0.next\nlet n1 = P?.next\nn0.next = n1\nP?.next = nil\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 {\nif (n0.?.next == null) return;\n// n0 -> P -> n1\nvar P = n0.?.next;\nvar n1 = P.?.next;\nn0.?.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) {\nif (n0.next == null) return;\n// n0 -> P -> n1\nListNode P = n0.next!;\nListNode? n1 = P.next;\nn0.next = n1;\n}\n
    "},{"location":"chapter_array_and_linkedlist/linked_list/#422","title":"4.2.2. \u00a0 \u94fe\u8868\u7f3a\u70b9","text":"

    \u94fe\u8868\u8bbf\u95ee\u8282\u70b9\u6548\u7387\u8f83\u4f4e\u3002\u5982\u4e0a\u8282\u6240\u8ff0\uff0c\u6570\u7ec4\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u4e0b\u8bbf\u95ee\u4efb\u610f\u5143\u7d20\u3002\u7136\u800c\uff0c\u94fe\u8868\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95ee\u4efb\u610f\u8282\u70b9\uff0c\u8fd9\u662f\u56e0\u4e3a\u7cfb\u7edf\u9700\u8981\u4ece\u5934\u8282\u70b9\u51fa\u53d1\uff0c\u9010\u4e2a\u5411\u540e\u904d\u5386\u76f4\u81f3\u627e\u5230\u76ee\u6807\u8282\u70b9\u3002\u4f8b\u5982\uff0c\u82e5\u8981\u8bbf\u95ee\u94fe\u8868\u7d22\u5f15\u4e3a index\uff08\u5373\u7b2c index + 1 \u4e2a\uff09\u7684\u8282\u70b9\uff0c\u5219\u9700\u8981\u5411\u540e\u904d\u5386 index \u8f6e\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart linked_list.java
    /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode access(ListNode head, int index) {\nfor (int i = 0; i < index; i++) {\nif (head == null)\nreturn null;\nhead = head.next;\n}\nreturn head;\n}\n
    linked_list.cpp
    /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode *access(ListNode *head, int index) {\nfor (int i = 0; i < index; i++) {\nif (head == nullptr)\nreturn nullptr;\nhead = head->next;\n}\nreturn head;\n}\n
    linked_list.py
    def access(head: ListNode, index: int) -> ListNode | None:\n\"\"\"\u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9\"\"\"\nfor _ in range(index):\nif not head:\nreturn None\nhead = head.next\nreturn head\n
    linked_list.go
    /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunc access(head *ListNode, index int) *ListNode {\nfor i := 0; i < index; i++ {\nif head == nil {\nreturn nil\n}\nhead = head.Next\n}\nreturn head\n}\n
    linked_list.js
    /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunction access(head, index) {\nfor (let i = 0; i < index; i++) {\nif (!head) {\nreturn null;\n}\nhead = head.next;\n}\nreturn 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 {\nfor (let i = 0; i < index; i++) {\nif (!head) {\nreturn null;\n}\nhead = head.next;\n}\nreturn head;\n}\n
    linked_list.c
    /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode *access(ListNode *head, int index) {\nwhile (head && head->next && index) {\nhead = head->next;\nindex--;\n}\nreturn head;\n}\n
    linked_list.cs
    /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode? access(ListNode head, int index) {\nfor (int i = 0; i < index; i++) {\nif (head == null)\nreturn null;\nhead = head.next;\n}\nreturn 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? {\nvar head: ListNode? = head\nfor _ in 0 ..< index {\nif head == nil {\nreturn nil\n}\nhead = head?.next\n}\nreturn 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) {\nvar head = node;\nvar i: i32 = 0;\nwhile (i < index) : (i += 1) {\nhead = head.?.next;\nif (head == null) return null;\n}\nreturn head;\n}\n
    linked_list.dart
    /* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode? access(ListNode? head, int index) {\nfor (var i = 0; i < index; i++) {\nif (head == null) return null;\nhead = head.next;\n}\nreturn head;\n}\n

    \u94fe\u8868\u7684\u5185\u5b58\u5360\u7528\u8f83\u5927\u3002\u94fe\u8868\u4ee5\u8282\u70b9\u4e3a\u5355\u4f4d\uff0c\u6bcf\u4e2a\u8282\u70b9\u9664\u4e86\u4fdd\u5b58\u503c\u4e4b\u5916\uff0c\u8fd8\u9700\u989d\u5916\u4fdd\u5b58\u6307\u9488\uff08\u5f15\u7528\uff09\u3002\u8fd9\u610f\u5473\u7740\u5728\u76f8\u540c\u6570\u636e\u91cf\u7684\u60c5\u51b5\u4e0b\uff0c\u94fe\u8868\u6bd4\u6570\u7ec4\u9700\u8981\u5360\u7528\u66f4\u591a\u7684\u5185\u5b58\u7a7a\u95f4\u3002

    "},{"location":"chapter_array_and_linkedlist/linked_list/#423","title":"4.2.3. \u00a0 \u94fe\u8868\u5e38\u7528\u64cd\u4f5c","text":"

    \u904d\u5386\u94fe\u8868\u67e5\u627e\u3002\u904d\u5386\u94fe\u8868\uff0c\u67e5\u627e\u94fe\u8868\u5185\u503c\u4e3a target \u7684\u8282\u70b9\uff0c\u8f93\u51fa\u8282\u70b9\u5728\u94fe\u8868\u4e2d\u7684\u7d22\u5f15\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart linked_list.java
    /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode head, int target) {\nint index = 0;\nwhile (head != null) {\nif (head.val == target)\nreturn index;\nhead = head.next;\nindex++;\n}\nreturn -1;\n}\n
    linked_list.cpp
    /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode *head, int target) {\nint index = 0;\nwhile (head != nullptr) {\nif (head->val == target)\nreturn index;\nhead = head->next;\nindex++;\n}\nreturn -1;\n}\n
    linked_list.py
    def find(head: ListNode, target: int) -> int:\n\"\"\"\u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9\"\"\"\nindex = 0\nwhile head:\nif head.val == target:\nreturn index\nhead = head.next\nindex += 1\nreturn -1\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 {\nindex := 0\nfor head != nil {\nif head.Val == target {\nreturn index\n}\nhead = head.Next\nindex++\n}\nreturn -1\n}\n
    linked_list.js
    /* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunction find(head, target) {\nlet index = 0;\nwhile (head !== null) {\nif (head.val === target) {\nreturn index;\n}\nhead = head.next;\nindex += 1;\n}\nreturn -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 {\nlet index = 0;\nwhile (head !== null) {\nif (head.val === target) {\nreturn index;\n}\nhead = head.next;\nindex += 1;\n}\nreturn -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) {\nint index = 0;\nwhile (head) {\nif (head->val == target)\nreturn index;\nhead = head->next;\nindex++;\n}\nreturn -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) {\nint index = 0;\nwhile (head != null) {\nif (head.val == target)\nreturn index;\nhead = head.next;\nindex++;\n}\nreturn -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 {\nvar head: ListNode? = head\nvar index = 0\nwhile head != nil {\nif head?.val == target {\nreturn index\n}\nhead = head?.next\nindex += 1\n}\nreturn -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 {\nvar head = node;\nvar index: i32 = 0;\nwhile (head != null) {\nif (head.?.val == target) return index;\nhead = head.?.next;\nindex += 1;\n}\nreturn -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) {\nint index = 0;\nwhile (head != null) {\nif (head.val == target) {\nreturn index;\n}\nhead = head.next;\nindex++;\n}\nreturn -1;\n}\n
    "},{"location":"chapter_array_and_linkedlist/linked_list/#424","title":"4.2.4. \u00a0 \u5e38\u89c1\u94fe\u8868\u7c7b\u578b","text":"

    \u5355\u5411\u94fe\u8868\u3002\u5373\u4e0a\u8ff0\u4ecb\u7ecd\u7684\u666e\u901a\u94fe\u8868\u3002\u5355\u5411\u94fe\u8868\u7684\u8282\u70b9\u5305\u542b\u503c\u548c\u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\u4e24\u9879\u6570\u636e\u3002\u6211\u4eec\u5c06\u9996\u4e2a\u8282\u70b9\u79f0\u4e3a\u5934\u8282\u70b9\uff0c\u5c06\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u6210\u4e3a\u5c3e\u8282\u70b9\uff0c\u5c3e\u8282\u70b9\u6307\u5411 \\(\\text{null}\\) \u3002

    \u73af\u5f62\u94fe\u8868\u3002\u5982\u679c\u6211\u4eec\u4ee4\u5355\u5411\u94fe\u8868\u7684\u5c3e\u8282\u70b9\u6307\u5411\u5934\u8282\u70b9\uff08\u5373\u9996\u5c3e\u76f8\u63a5\uff09\uff0c\u5219\u5f97\u5230\u4e00\u4e2a\u73af\u5f62\u94fe\u8868\u3002\u5728\u73af\u5f62\u94fe\u8868\u4e2d\uff0c\u4efb\u610f\u8282\u70b9\u90fd\u53ef\u4ee5\u89c6\u4f5c\u5934\u8282\u70b9\u3002

    \u53cc\u5411\u94fe\u8868\u3002\u4e0e\u5355\u5411\u94fe\u8868\u76f8\u6bd4\uff0c\u53cc\u5411\u94fe\u8868\u8bb0\u5f55\u4e86\u4e24\u4e2a\u65b9\u5411\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\u3002\u53cc\u5411\u94fe\u8868\u7684\u8282\u70b9\u5b9a\u4e49\u540c\u65f6\u5305\u542b\u6307\u5411\u540e\u7ee7\u8282\u70b9\uff08\u4e0b\u4e00\u8282\u70b9\uff09\u548c\u524d\u9a71\u8282\u70b9\uff08\u4e0a\u4e00\u8282\u70b9\uff09\u7684\u6307\u9488\u3002\u76f8\u8f83\u4e8e\u5355\u5411\u94fe\u8868\uff0c\u53cc\u5411\u94fe\u8868\u66f4\u5177\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u671d\u4e24\u4e2a\u65b9\u5411\u904d\u5386\u94fe\u8868\uff0c\u4f46\u76f8\u5e94\u5730\u4e5f\u9700\u8981\u5360\u7528\u66f4\u591a\u7684\u5185\u5b58\u7a7a\u95f4\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nint val;        // \u8282\u70b9\u503c\nListNode next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode(int x) { val = x; }  // \u6784\u9020\u51fd\u6570\n}\n
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct ListNode {\nint val;         // \u8282\u70b9\u503c\nListNode *next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode *prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode(int x) : val(x), next(nullptr), prev(nullptr) {}  // \u6784\u9020\u51fd\u6570\n};\n
    class ListNode:\n\"\"\"\u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b\"\"\"\ndef __init__(self, val: int):\nself.val: int = val                   # \u8282\u70b9\u503c\nself.next: Optional[ListNode] = None  # \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nself.prev: Optional[ListNode] = None  # \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype DoublyListNode struct {\nVal  int             // \u8282\u70b9\u503c\nNext *DoublyListNode // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nPrev *DoublyListNode // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n}\n// NewDoublyListNode \u521d\u59cb\u5316\nfunc NewDoublyListNode(val int) *DoublyListNode {\nreturn &DoublyListNode{\nVal:  val,\nNext: nil,\nPrev: nil,\n}\n}\n
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nval;\nnext;\nprev;\nconstructor(val, next, prev) {\nthis.val = val  ===  undefined ? 0 : val;        // \u8282\u70b9\u503c\nthis.next = next  ===  undefined ? null : next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nthis.prev = prev  ===  undefined ? null : prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n}\n}\n
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nval: number;\nnext: ListNode | null;\nprev: ListNode | null;\nconstructor(val?: number, next?: ListNode | null, prev?: ListNode | null) {\nthis.val = val  ===  undefined ? 0 : val;        // \u8282\u70b9\u503c\nthis.next = next  ===  undefined ? null : next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nthis.prev = prev  ===  undefined ? null : prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n}\n}\n
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct ListNode {\nint val;               // \u8282\u70b9\u503c\nstruct ListNode *next; // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nstruct ListNode *prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n};\ntypedef struct ListNode ListNode;\n/* \u6784\u9020\u51fd\u6570 */\nListNode *newListNode(int val) {\nListNode *node, *next;\nnode = (ListNode *) malloc(sizeof(ListNode));\nnode->val = val;\nnode->next = NULL;\nnode->prev = NULL;\nreturn node;\n}\n
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nint val;        // \u8282\u70b9\u503c\nListNode next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode(int x) => val = x;  // \u6784\u9020\u51fd\u6570\n}\n
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nvar val: Int // \u8282\u70b9\u503c\nvar next: ListNode? // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nvar prev: ListNode? // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\ninit(x: Int) { // \u6784\u9020\u51fd\u6570\nval = x\n}\n}\n
    // \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b\npub fn ListNode(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nval: T = 0, // \u8282\u70b9\u503c\nnext: ?*Self = null, // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nprev: ?*Self = null, // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n// \u6784\u9020\u51fd\u6570\npub fn init(self: *Self, x: i32) void {\nself.val = x;\nself.next = null;\nself.prev = null;\n}\n};\n}\n
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nint val;        // \u8282\u70b9\u503c\nListNode next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode(this.val, [this.next, this.prev]);  // \u6784\u9020\u51fd\u6570\n}\n

    Fig. \u5e38\u89c1\u94fe\u8868\u79cd\u7c7b

    "},{"location":"chapter_array_and_linkedlist/list/","title":"4.3. \u00a0 \u5217\u8868","text":"

    \u6570\u7ec4\u957f\u5ea6\u4e0d\u53ef\u53d8\u5bfc\u81f4\u5b9e\u7528\u6027\u964d\u4f4e\u3002\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4e8b\u5148\u65e0\u6cd5\u786e\u5b9a\u9700\u8981\u5b58\u50a8\u591a\u5c11\u6570\u636e\uff0c\u8fd9\u4f7f\u6570\u7ec4\u957f\u5ea6\u7684\u9009\u62e9\u53d8\u5f97\u56f0\u96be\u3002\u82e5\u957f\u5ea6\u8fc7\u5c0f\uff0c\u9700\u8981\u5728\u6301\u7eed\u6dfb\u52a0\u6570\u636e\u65f6\u9891\u7e41\u6269\u5bb9\u6570\u7ec4\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\u51fa\u73b0\u4e86\u4e00\u79cd\u88ab\u79f0\u4e3a\u300c\u52a8\u6001\u6570\u7ec4 Dynamic Array\u300d\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5373\u957f\u5ea6\u53ef\u53d8\u7684\u6570\u7ec4\uff0c\u4e5f\u5e38\u88ab\u79f0\u4e3a\u300c\u5217\u8868 List\u300d\u3002\u5217\u8868\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\uff0c\u7ee7\u627f\u4e86\u6570\u7ec4\u7684\u4f18\u70b9\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u52a8\u6001\u6269\u5bb9\u3002\u5728\u5217\u8868\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u81ea\u7531\u6dfb\u52a0\u5143\u7d20\uff0c\u800c\u65e0\u9700\u62c5\u5fc3\u8d85\u8fc7\u5bb9\u91cf\u9650\u5236\u3002

    "},{"location":"chapter_array_and_linkedlist/list/#431","title":"4.3.1. \u00a0 \u5217\u8868\u5e38\u7528\u64cd\u4f5c","text":"

    \u521d\u59cb\u5316\u5217\u8868\u3002\u901a\u5e38\u6211\u4eec\u4f1a\u4f7f\u7528\u201c\u65e0\u521d\u59cb\u503c\u201d\u548c\u201c\u6709\u521d\u59cb\u503c\u201d\u7684\u4e24\u79cd\u521d\u59cb\u5316\u65b9\u6cd5\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart list.java
    /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<Integer> list1 = 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> list = new ArrayList<>(Arrays.asList(numbers));\n
    list.cpp
    /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u9700\u6ce8\u610f\uff0cC++ \u4e2d vector \u5373\u662f\u672c\u6587\u63cf\u8ff0\u7684 list\n// \u65e0\u521d\u59cb\u503c\nvector<int> list1;\n// \u6709\u521d\u59cb\u503c\nvector<int> list = { 1, 3, 2, 5, 4 };\n
    list.py
    # \u521d\u59cb\u5316\u5217\u8868\n# \u65e0\u521d\u59cb\u503c\nlist1: List[int] = []\n# \u6709\u521d\u59cb\u503c\nlist: List[int] = [1, 3, 2, 5, 4]\n
    list_test.go
    /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nlist1 := []int\n// \u6709\u521d\u59cb\u503c\nlist := []int{1, 3, 2, 5, 4}\n
    list.js
    /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nconst list1 = [];\n// \u6709\u521d\u59cb\u503c\nconst list = [1, 3, 2, 5, 4];\n
    list.ts
    /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nconst list1: number[] = [];\n// \u6709\u521d\u59cb\u503c\nconst list: number[] = [1, 3, 2, 5, 4];\n
    list.c
    // C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
    list.cs
    /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<int> list1 = new ();\n// \u6709\u521d\u59cb\u503c\nint[] numbers = new int[] { 1, 3, 2, 5, 4 };\nList<int> list = numbers.ToList();\n
    list.swift
    /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nlet list1: [Int] = []\n// \u6709\u521d\u59cb\u503c\nvar list = [1, 3, 2, 5, 4]\n
    list.zig
    // \u521d\u59cb\u5316\u5217\u8868\nvar list = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer list.deinit();\ntry list.appendSlice(&[_]i32{ 1, 3, 2, 5, 4 });\n
    list.dart
    /* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<int> list1 = [];\n// \u6709\u521d\u59cb\u503c\nList<int> list = [1, 3, 2, 5, 4];\n

    \u8bbf\u95ee\u4e0e\u66f4\u65b0\u5143\u7d20\u3002\u7531\u4e8e\u5217\u8868\u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart list.java
    /* \u8bbf\u95ee\u5143\u7d20 */\nint num = list.get(1);  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist.set(1, 0);  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
    list.cpp
    /* \u8bbf\u95ee\u5143\u7d20 */\nint num = list[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
    list.py
    # \u8bbf\u95ee\u5143\u7d20\nnum: int = list[1]  # \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n# \u66f4\u65b0\u5143\u7d20\nlist[1] = 0    # \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
    list_test.go
    /* \u8bbf\u95ee\u5143\u7d20 */\nnum := list[1]  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0     // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
    list.js
    /* \u8bbf\u95ee\u5143\u7d20 */\nconst num = list[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
    list.ts
    /* \u8bbf\u95ee\u5143\u7d20 */\nconst num: number = list[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[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.cs
    /* \u8bbf\u95ee\u5143\u7d20 */\nint num = list[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
    list.swift
    /* \u8bbf\u95ee\u5143\u7d20 */\nlet num = list[1] // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0 // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
    list.zig
    // \u8bbf\u95ee\u5143\u7d20\nvar num = list.items[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n// \u66f4\u65b0\u5143\u7d20\nlist.items[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0  \n
    list.dart
    /* \u8bbf\u95ee\u5143\u7d20 */\nint num = list[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n

    \u5728\u5217\u8868\u4e2d\u6dfb\u52a0\u3001\u63d2\u5165\u3001\u5220\u9664\u5143\u7d20\u3002\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart list.java
    /* \u6e05\u7a7a\u5217\u8868 */\nlist.clear();\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.add(1);\nlist.add(3);\nlist.add(2);\nlist.add(5);\nlist.add(4);\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.add(3, 6);  // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n/* \u5220\u9664\u5143\u7d20 */\nlist.remove(3);  // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
    list.cpp
    /* \u6e05\u7a7a\u5217\u8868 */\nlist.clear();\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.push_back(1);\nlist.push_back(3);\nlist.push_back(2);\nlist.push_back(5);\nlist.push_back(4);\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.insert(list.begin() + 3, 6);  // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n/* \u5220\u9664\u5143\u7d20 */\nlist.erase(list.begin() + 3);      // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
    list.py
    # \u6e05\u7a7a\u5217\u8868\nlist.clear()\n# \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\nlist.append(1)\nlist.append(3)\nlist.append(2)\nlist.append(5)\nlist.append(4)\n# \u4e2d\u95f4\u63d2\u5165\u5143\u7d20\nlist.insert(3, 6)  # \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n# \u5220\u9664\u5143\u7d20\nlist.pop(3)        # \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
    list_test.go
    /* \u6e05\u7a7a\u5217\u8868 */\nlist = nil\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist = append(list, 1)\nlist = append(list, 3)\nlist = append(list, 2)\nlist = append(list, 5)\nlist = append(list, 4)\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist = append(list[:3], append([]int{6}, list[3:]...)...) // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n/* \u5220\u9664\u5143\u7d20 */\nlist = append(list[:3], list[4:]...) // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
    list.js
    /* \u6e05\u7a7a\u5217\u8868 */\nlist.length = 0;\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.push(1);\nlist.push(3);\nlist.push(2);\nlist.push(5);\nlist.push(4);\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.splice(3, 0, 6);\n/* \u5220\u9664\u5143\u7d20 */\nlist.splice(3, 1);\n
    list.ts
    /* \u6e05\u7a7a\u5217\u8868 */\nlist.length = 0;\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.push(1);\nlist.push(3);\nlist.push(2);\nlist.push(5);\nlist.push(4);\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.splice(3, 0, 6);\n/* \u5220\u9664\u5143\u7d20 */\nlist.splice(3, 1);\n
    list.c
    // C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
    list.cs
    /* \u6e05\u7a7a\u5217\u8868 */\nlist.Clear();\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.Add(1);\nlist.Add(3);\nlist.Add(2);\nlist.Add(5);\nlist.Add(4);\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.Insert(3, 6);\n/* \u5220\u9664\u5143\u7d20 */\nlist.RemoveAt(3);\n
    list.swift
    /* \u6e05\u7a7a\u5217\u8868 */\nlist.removeAll()\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.append(1)\nlist.append(3)\nlist.append(2)\nlist.append(5)\nlist.append(4)\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.insert(6, at: 3) // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n/* \u5220\u9664\u5143\u7d20 */\nlist.remove(at: 3) // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
    list.zig
    // \u6e05\u7a7a\u5217\u8868\nlist.clearRetainingCapacity();\n// \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\ntry list.append(1);\ntry list.append(3);\ntry list.append(2);\ntry list.append(5);\ntry list.append(4);\n// \u4e2d\u95f4\u63d2\u5165\u5143\u7d20\ntry list.insert(3, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n// \u5220\u9664\u5143\u7d20\n_ = list.orderedRemove(3); // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
    list.dart
    /* \u6e05\u7a7a\u5217\u8868 */\nlist.clear();\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.add(1);\nlist.add(3);\nlist.add(2);\nlist.add(5);\nlist.add(4);\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.insert(3, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n/* \u5220\u9664\u5143\u7d20 */\nlist.removeAt(3); // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n

    \u904d\u5386\u5217\u8868\u3002\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart list.java
    /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < list.size(); i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (int n : list) {\ncount++;\n}\n
    list.cpp
    /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < list.size(); i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (int n : list) {\ncount++;\n}\n
    list.py
    # \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868\ncount = 0\nfor i in range(len(list)):\ncount += 1\n# \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20\ncount = 0\nfor n in list:\ncount += 1\n
    list_test.go
    /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\ncount := 0\nfor i := 0; i < len(list); i++ {\ncount++\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0\nfor range list {\ncount++\n}\n
    list.js
    /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nlet count = 0;\nfor (let i = 0; i < list.length; i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (const n of list) {\ncount++;\n}\n
    list.ts
    /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nlet count = 0;\nfor (let i = 0; i < list.length; i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (const n of list) {\ncount++;\n}\n
    list.c
    // C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
    list.cs
    /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < list.Count(); i++)\n{\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nforeach (int n in list)\n{\ncount++;\n}\n
    list.swift
    /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nvar count = 0\nfor _ in list.indices {\ncount += 1\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0\nfor _ in list {\ncount += 1\n}\n
    list.zig
    // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868\nvar count: i32 = 0;\nvar i: i32 = 0;\nwhile (i < list.items.len) : (i += 1) {\ncount += 1;\n}\n// \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20\ncount = 0;\nfor (list.items) |_| {\ncount += 1;\n}\n
    list.dart
    /* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < list.length; i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (int n in list) {\ncount++;\n}\n

    \u62fc\u63a5\u4e24\u4e2a\u5217\u8868\u3002\u7ed9\u5b9a\u4e00\u4e2a\u65b0\u5217\u8868 list1 \uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8be5\u5217\u8868\u62fc\u63a5\u5230\u539f\u5217\u8868\u7684\u5c3e\u90e8\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart list.java
    /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<Integer> list1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));\nlist.addAll(list1);  // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n
    list.cpp
    /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nvector<int> list1 = { 6, 8, 7, 10, 9 };\n// \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\nlist.insert(list.end(), list1.begin(), list1.end());\n
    list.py
    # \u62fc\u63a5\u4e24\u4e2a\u5217\u8868\nlist1: List[int] = [6, 8, 7, 10, 9]\nlist += list1  # \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n
    list_test.go
    /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nlist1 := []int{6, 8, 7, 10, 9}\nlist = append(list, list1...)  // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n
    list.js
    /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nconst list1 = [6, 8, 7, 10, 9];\nlist.push(...list1);  // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n
    list.ts
    /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nconst list1: number[] = [6, 8, 7, 10, 9];\nlist.push(...list1);  // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n
    list.c
    // C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
    list.cs
    /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<int> list1 = new() { 6, 8, 7, 10, 9 };\nlist.AddRange(list1);  // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n
    list.swift
    /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nlet list1 = [6, 8, 7, 10, 9]\nlist.append(contentsOf: list1) // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n
    list.zig
    // \u62fc\u63a5\u4e24\u4e2a\u5217\u8868\nvar list1 = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer list1.deinit();\ntry list1.appendSlice(&[_]i32{ 6, 8, 7, 10, 9 });\ntry list.insertSlice(list.items.len, list1.items); // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n
    list.dart
    /* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<int> list1 = [6, 8, 7, 10, 9];\nlist.addAll(list1);  // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n

    \u6392\u5e8f\u5217\u8868\u3002\u6392\u5e8f\u4e5f\u662f\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\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\u5bdf\u7684\u300c\u4e8c\u5206\u67e5\u627e\u300d\u548c\u300c\u53cc\u6307\u9488\u300d\u7b97\u6cd5\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart list.java
    /* \u6392\u5e8f\u5217\u8868 */\nCollections.sort(list);  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
    list.cpp
    /* \u6392\u5e8f\u5217\u8868 */\nsort(list.begin(), list.end());  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
    list.py
    # \u6392\u5e8f\u5217\u8868\nlist.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(list)  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
    list.js
    /* \u6392\u5e8f\u5217\u8868 */  list.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 */\nlist.sort((a, b) => a - b);  // \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.cs
    /* \u6392\u5e8f\u5217\u8868 */\nlist.Sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
    list.swift
    /* \u6392\u5e8f\u5217\u8868 */\nlist.sort() // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
    list.zig
    // \u6392\u5e8f\u5217\u8868\nstd.sort.sort(i32, list.items, {}, comptime std.sort.asc(i32));\n
    list.dart
    /* \u6392\u5e8f\u5217\u8868 */\nlist.sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
    "},{"location":"chapter_array_and_linkedlist/list/#432","title":"4.3.2. \u00a0 \u5217\u8868\u5b9e\u73b0 *","text":"

    \u4e3a\u4e86\u5e2e\u52a9\u52a0\u6df1\u5bf9\u5217\u8868\u7684\u7406\u89e3\uff0c\u6211\u4eec\u5728\u6b64\u63d0\u4f9b\u4e00\u4e2a\u7b80\u6613\u7248\u5217\u8868\u5b9e\u73b0\u3002\u9700\u8981\u5173\u6ce8\u4e09\u4e2a\u6838\u5fc3\u70b9\uff1a

    • \u521d\u59cb\u5bb9\u91cf\uff1a\u9009\u53d6\u4e00\u4e2a\u5408\u7406\u7684\u6570\u7ec4\u521d\u59cb\u5bb9\u91cf\u3002\u5728\u672c\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9009\u62e9 10 \u4f5c\u4e3a\u521d\u59cb\u5bb9\u91cf\u3002
    • \u6570\u91cf\u8bb0\u5f55\uff1a\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf 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\u3002
    • \u6269\u5bb9\u673a\u5236\uff1a\u63d2\u5165\u5143\u7d20\u65f6\u53ef\u80fd\u8d85\u51fa\u5217\u8868\u5bb9\u91cf\uff0c\u6b64\u65f6\u9700\u8981\u6269\u5bb9\u5217\u8868\u3002\u6269\u5bb9\u65b9\u6cd5\u662f\u6839\u636e\u6269\u5bb9\u500d\u6570\u521b\u5efa\u4e00\u4e2a\u66f4\u5927\u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u5f53\u524d\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\u4f9d\u6b21\u79fb\u52a8\u81f3\u65b0\u6570\u7ec4\u3002\u5728\u672c\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u89c4\u5b9a\u6bcf\u6b21\u5c06\u6570\u7ec4\u6269\u5bb9\u81f3\u4e4b\u524d\u7684 2 \u500d\u3002

    \u672c\u793a\u4f8b\u65e8\u5728\u5e2e\u52a9\u8bfb\u8005\u76f4\u89c2\u7406\u89e3\u5217\u8868\u7684\u5de5\u4f5c\u673a\u5236\u3002\u5b9e\u9645\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u5217\u8868\u5b9e\u73b0\u66f4\u52a0\u6807\u51c6\u548c\u590d\u6742\uff0c\u5404\u4e2a\u53c2\u6570\u7684\u8bbe\u5b9a\u4e5f\u975e\u5e38\u6709\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart my_list.java
    /* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate int[] nums; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nprivate int capacity = 10; // \u5217\u8868\u5bb9\u91cf\nprivate int size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nprivate int extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n/* \u6784\u9020\u65b9\u6cd5 */\npublic MyList() {\nnums = new int[capacity];\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09 */\npublic int size() {\nreturn size;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\npublic int capacity() {\nreturn capacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\npublic int get(int index) {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index < 0 || index >= size)\nthrow new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\nreturn nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\npublic void set(int index, int num) {\nif (index < 0 || index >= size)\nthrow new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\nnums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\npublic void add(int num) {\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (size == capacity())\nextendCapacity();\nnums[size] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nsize++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\npublic void insert(int index, int num) {\nif (index < 0 || index >= size)\nthrow new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (size == capacity())\nextendCapacity();\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (int j = size - 1; j >= index; j--) {\nnums[j + 1] = nums[j];\n}\nnums[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nsize++;\n}\n/* \u5220\u9664\u5143\u7d20 */\npublic int remove(int index) {\nif (index < 0 || index >= size)\nthrow new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\nint num = nums[index];\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (int j = index; j < size - 1; j++) {\nnums[j] = nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nsize--;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\npublic 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\nnums = Arrays.copyOf(nums, capacity() * extendRatio);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\ncapacity = nums.length;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\npublic int[] toArray() {\nint size = size();\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nint[] nums = new int[size];\nfor (int i = 0; i < size; i++) {\nnums[i] = get(i);\n}\nreturn nums;\n}\n}\n
    my_list.cpp
    /* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate:\nint *nums;             // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nint numsCapacity = 10; // \u5217\u8868\u5bb9\u91cf\nint numsSize = 0;      // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nint extendRatio = 2;   // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nMyList() {\nnums = new int[numsCapacity];\n}\n/* \u6790\u6784\u65b9\u6cd5 */\n~MyList() {\ndelete[] nums;\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\nint size() {\nreturn numsSize;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nint capacity() {\nreturn numsCapacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\nint get(int index) {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index < 0 || index >= size())\nthrow out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\nreturn nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nvoid set(int index, int num) {\nif (index < 0 || index >= size())\nthrow out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\nnums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nvoid add(int num) {\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (size() == capacity())\nextendCapacity();\nnums[size()] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nnumsSize++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nvoid insert(int index, int num) {\nif (index < 0 || index >= size())\nthrow out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (size() == capacity())\nextendCapacity();\n// \u7d22\u5f15 i \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (int j = size() - 1; j >= index; j--) {\nnums[j + 1] = nums[j];\n}\nnums[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nnumsSize++;\n}\n/* \u5220\u9664\u5143\u7d20 */\nint remove(int index) {\nif (index < 0 || index >= size())\nthrow out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\nint num = nums[index];\n// \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (int j = index; j < size() - 1; j++) {\nnums[j] = nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nnumsSize--;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\nvoid extendCapacity() {\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\nint newCapacity = capacity() * extendRatio;\nint *tmp = nums;\nnums = new int[newCapacity];\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor (int i = 0; i < size(); i++) {\nnums[i] = tmp[i];\n}\n// \u91ca\u653e\u5185\u5b58\ndelete[] tmp;\nnumsCapacity = newCapacity;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a Vector \u7528\u4e8e\u6253\u5370 */\nvector<int> toVector() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvector<int> vec(size());\nfor (int i = 0; i < size(); i++) {\nvec[i] = nums[i];\n}\nreturn vec;\n}\n};\n
    my_list.py
    class MyList:\n\"\"\"\u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0\"\"\"\ndef __init__(self):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.__capacity: int = 10  # \u5217\u8868\u5bb9\u91cf\nself.__nums: list[int] = [0] * self.__capacity  # \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nself.__size: int = 0  # \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nself.__extend_ratio: int = 2  # \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\ndef size(self) -> int:\n\"\"\"\u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\"\"\"\nreturn self.__size\ndef capacity(self) -> int:\n\"\"\"\u83b7\u53d6\u5217\u8868\u5bb9\u91cf\"\"\"\nreturn self.__capacity\ndef 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\nif index < 0 or index >= self.__size:\nraise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\nreturn self.__nums[index]\ndef set(self, num: int, index: int) -> None:\n\"\"\"\u66f4\u65b0\u5143\u7d20\"\"\"\nif index < 0 or index >= self.__size:\nraise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\nself.__nums[index] = num\ndef add(self, num: int) -> None:\n\"\"\"\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\"\"\"\n# \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif self.size() == self.capacity():\nself.extend_capacity()\nself.__nums[self.__size] = num\nself.__size += 1\ndef insert(self, num: int, index: int) -> None:\n\"\"\"\u4e2d\u95f4\u63d2\u5165\u5143\u7d20\"\"\"\nif index < 0 or index >= self.__size:\nraise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n# \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif self.__size == self.capacity():\nself.extend_capacity()\n# \u7d22\u5f15 i \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor j in range(self.__size - 1, index - 1, -1):\nself.__nums[j + 1] = self.__nums[j]\nself.__nums[index] = num\n# \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.__size += 1\ndef remove(self, index: int) -> int:\n\"\"\"\u5220\u9664\u5143\u7d20\"\"\"\nif index < 0 or index >= self.__size:\nraise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\nnum = self.__nums[index]\n# \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor j in range(index, self.__size - 1):\nself.__nums[j] = self.__nums[j + 1]\n# \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.__size -= 1\n# \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num\ndef extend_capacity(self) -> None:\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\nself.__nums = self.__nums + [0] * self.capacity() * (self.__extend_ratio - 1)\n# \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nself.__capacity = len(self.__nums)\ndef to_array(self) -> list[int]:\n\"\"\"\u8fd4\u56de\u6709\u6548\u957f\u5ea6\u7684\u5217\u8868\"\"\"\nreturn self.__nums[: self.__size]\n
    my_list.go
    /* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\ntype myList struct {\nnumsCapacity int\nnums         []int\nnumsSize     int\nextendRatio  int\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newMyList() *myList {\nreturn &myList{\nnumsCapacity: 10,              // \u5217\u8868\u5bb9\u91cf\nnums:         make([]int, 10), // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nnumsSize:     0,               // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nextendRatio:  2,               // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n}\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09 */\nfunc (l *myList) size() int {\nreturn l.numsSize\n}\n/*  \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nfunc (l *myList) capacity() int {\nreturn l.numsCapacity\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\nif index < 0 || index >= l.numsSize {\npanic(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nreturn l.nums[index]\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nfunc (l *myList) set(num, index int) {\nif index < 0 || index >= l.numsSize {\npanic(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nl.nums[index] = num\n}\n/* \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\nif l.numsSize == l.numsCapacity {\nl.extendCapacity()\n}\nl.nums[l.numsSize] = num\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nl.numsSize++\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nfunc (l *myList) insert(num, index int) {\nif index < 0 || index >= l.numsSize {\npanic(\"\u7d22\u5f15\u8d8a\u754c\")\n}\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif l.numsSize == l.numsCapacity {\nl.extendCapacity()\n}\n// \u7d22\u5f15 i \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor j := l.numsSize - 1; j >= index; j-- {\nl.nums[j+1] = l.nums[j]\n}\nl.nums[index] = num\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nl.numsSize++\n}\n/* \u5220\u9664\u5143\u7d20 */\nfunc (l *myList) remove(index int) int {\nif index < 0 || index >= l.numsSize {\npanic(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nnum := l.nums[index]\n// \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor j := index; j < l.numsSize-1; j++ {\nl.nums[j] = l.nums[j+1]\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nl.numsSize--\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num\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\nl.nums = append(l.nums, make([]int, l.numsCapacity*(l.extendRatio-1))...)\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nl.numsCapacity = len(l.nums)\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\nreturn l.nums[:l.numsSize]\n}\n
    my_list.js
    /* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\n#nums = 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\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n#extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor() {\nthis.#nums = new Array(this.#capacity);\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\nsize() {\nreturn this.#size;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\ncapacity() {\nreturn this.#capacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\nget(index) {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\nreturn this.#nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nset(index, num) {\nif (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\nthis.#nums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nadd(num) {\n// \u5982\u679c\u957f\u5ea6\u7b49\u4e8e\u5bb9\u91cf\uff0c\u5219\u9700\u8981\u6269\u5bb9\nif (this.#size === this.#capacity) {\nthis.extendCapacity();\n}\n// \u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u5c3e\u90e8\nthis.#nums[this.#size] = num;\nthis.#size++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\ninsert(index, num) {\nif (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\nif (this.#size === this.#capacity) {\nthis.extendCapacity();\n}\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (let j = this.#size - 1; j >= index; j--) {\nthis.#nums[j + 1] = this.#nums[j];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nthis.#nums[index] = num;\nthis.#size++;\n}\n/* \u5220\u9664\u5143\u7d20 */\nremove(index) {\nif (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\nlet num = this.#nums[index];\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (let j = index; j < this.#size - 1; j++) {\nthis.#nums[j] = this.#nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nthis.#size--;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\nextendCapacity() {\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\nthis.#nums = this.#nums.concat(\nnew Array(this.capacity() * (this.#extendRatio - 1))\n);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nthis.#capacity = this.#nums.length;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\ntoArray() {\nlet size = this.size();\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nconst nums = new Array(size);\nfor (let i = 0; i < size; i++) {\nnums[i] = this.get(i);\n}\nreturn nums;\n}\n}\n
    my_list.ts
    /* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate nums: Array<number>; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nprivate _capacity: number = 10; // \u5217\u8868\u5bb9\u91cf\nprivate _size: number = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nprivate extendRatio: number = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor() {\nthis.nums = new Array(this._capacity);\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\npublic size(): number {\nreturn this._size;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\npublic capacity(): number {\nreturn this._capacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\npublic get(index: number): number {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\nreturn this.nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\npublic set(index: number, num: number): void {\nif (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\nthis.nums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\npublic add(num: number): void {\n// \u5982\u679c\u957f\u5ea6\u7b49\u4e8e\u5bb9\u91cf\uff0c\u5219\u9700\u8981\u6269\u5bb9\nif (this._size === this._capacity) this.extendCapacity();\n// \u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u5c3e\u90e8\nthis.nums[this._size] = num;\nthis._size++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\npublic insert(index: number, num: number): void {\nif (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\nif (this._size === this._capacity) {\nthis.extendCapacity();\n}\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (let j = this._size - 1; j >= index; j--) {\nthis.nums[j + 1] = this.nums[j];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nthis.nums[index] = num;\nthis._size++;\n}\n/* \u5220\u9664\u5143\u7d20 */\npublic remove(index: number): number {\nif (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\nlet num = this.nums[index];\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (let j = index; j < this._size - 1; j++) {\nthis.nums[j] = this.nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nthis._size--;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\npublic 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\nthis.nums = this.nums.concat(\nnew Array(this.capacity() * (this.extendRatio - 1))\n);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nthis._capacity = this.nums.length;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\npublic toArray(): number[] {\nlet size = this.size();\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nconst nums = new Array(size);\nfor (let i = 0; i < size; i++) {\nnums[i] = this.get(i);\n}\nreturn nums;\n}\n}\n
    my_list.c
    /* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nstruct myList {\nint *nums;       // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nint capacity;    // \u5217\u8868\u5bb9\u91cf\nint size;        // \u5217\u8868\u5927\u5c0f\nint extendRatio; // \u5217\u8868\u6bcf\u6b21\u6269\u5bb9\u7684\u500d\u6570\n};\ntypedef struct myList myList;\n/* \u6784\u9020\u51fd\u6570 */\nmyList *newMyList() {\nmyList *list = malloc(sizeof(myList));\nlist->capacity = 10;\nlist->nums = malloc(sizeof(int) * list->capacity);\nlist->size = 0;\nlist->extendRatio = 2;\nreturn list;\n}\n/* \u6790\u6784\u51fd\u6570 */\nvoid delMyList(myList *list) {\nfree(list->nums);\nfree(list);\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6 */\nint size(myList *list) {\nreturn list->size;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nint capacity(myList *list) {\nreturn list->capacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\nint get(myList *list, int index) {\nassert(index >= 0 && index < list->size);\nreturn list->nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nvoid set(myList *list, int index, int num) {\nassert(index >= 0 && index < list->size);\nlist->nums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nvoid add(myList *list, int num) {\nif (size(list) == capacity(list)) {\nextendCapacity(list); // \u6269\u5bb9\n}\nlist->nums[size(list)] = num;\nlist->size++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nvoid insert(myList *list, int index, int num) {\nassert(index >= 0 && index < size(list));\nfor (int i = size(list); i > index; --i) {\nlist->nums[i] = list->nums[i - 1];\n}\nlist->nums[index] = num;\nlist->size++;\n}\n/* \u5220\u9664\u5143\u7d20 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nint removeNum(myList *list, int index) {\nassert(index >= 0 && index < size(list));\nint num = list->nums[index];\nfor (int i = index; i < size(list) - 1; i++) {\nlist->nums[i] = list->nums[i + 1];\n}\nlist->size--;\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\nvoid extendCapacity(myList *list) {\n// \u5148\u5206\u914d\u7a7a\u95f4\nint newCapacity = capacity(list) * list->extendRatio;\nint *extend = (int *)malloc(sizeof(int) * newCapacity);\nint *temp = list->nums;\n// \u62f7\u8d1d\u65e7\u6570\u636e\u5230\u65b0\u6570\u636e\nfor (int i = 0; i < size(list); i++)\nextend[i] = list->nums[i];\n// \u91ca\u653e\u65e7\u6570\u636e\nfree(temp);\n// \u66f4\u65b0\u65b0\u6570\u636e\nlist->nums = extend;\nlist->capacity = newCapacity;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a Array \u7528\u4e8e\u6253\u5370 */\nint *toArray(myList *list) {\nreturn list->nums;\n}\n
    my_list.cs
    /* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate int[] nums;           // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nprivate int numsCapacity = 10;    // \u5217\u8868\u5bb9\u91cf\nprivate int numsSize = 0;         // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nprivate int extendRatio = 2;  // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n/* \u6784\u9020\u65b9\u6cd5 */\npublic MyList() {\nnums = new int[numsCapacity];\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\npublic int size() {\nreturn numsSize;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\npublic int capacity() {\nreturn numsCapacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\npublic int get(int index) {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index < 0 || index >= numsSize)\nthrow new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\nreturn nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\npublic void set(int index, int num) {\nif (index < 0 || index >= numsSize)\nthrow new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\nnums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\npublic void add(int num) {\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (numsSize == numsCapacity)\nextendCapacity();\nnums[numsSize] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nnumsSize++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\npublic void insert(int index, int num) {\nif (index < 0 || index >= numsSize)\nthrow new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (numsSize == numsCapacity)\nextendCapacity();\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (int j = numsSize - 1; j >= index; j--) {\nnums[j + 1] = nums[j];\n}\nnums[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nnumsSize++;\n}\n/* \u5220\u9664\u5143\u7d20 */\npublic int remove(int index) {\nif (index < 0 || index >= numsSize)\nthrow new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\nint num = nums[index];\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (int j = index; j < numsSize - 1; j++) {\nnums[j] = nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nnumsSize--;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\npublic void extendCapacity() {\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a numsCapacity * extendRatio \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nSystem.Array.Resize(ref nums, numsCapacity * extendRatio);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nnumsCapacity = nums.Length;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\npublic int[] toArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nint[] nums = new int[numsSize];\nfor (int i = 0; i < numsSize; i++) {\nnums[i] = get(i);\n}\nreturn nums;\n}\n}\n
    my_list.swift
    /* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate var nums: [Int] // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nprivate var _capacity = 10 // \u5217\u8868\u5bb9\u91cf\nprivate var _size = 0 // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nprivate let extendRatio = 2 // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n/* \u6784\u9020\u65b9\u6cd5 */\ninit() {\nnums = Array(repeating: 0, count: _capacity)\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\nfunc size() -> Int {\n_size\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nfunc capacity() -> Int {\n_capacity\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\nfunc get(index: Int) -> Int {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u9519\u8bef\uff0c\u4e0b\u540c\nif index < 0 || index >= _size {\nfatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nreturn nums[index]\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nfunc set(index: Int, num: Int) {\nif index < 0 || index >= _size {\nfatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nnums[index] = num\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nfunc add(num: Int) {\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif _size == _capacity {\nextendCapacity()\n}\nnums[_size] = num\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n_size += 1\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nfunc insert(index: Int, num: Int) {\nif index < 0 || index >= _size {\nfatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n}\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif _size == _capacity {\nextendCapacity()\n}\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor j in sequence(first: _size - 1, next: { $0 >= index + 1 ? $0 - 1 : nil }) {\nnums[j + 1] = nums[j]\n}\nnums[index] = num\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n_size += 1\n}\n/* \u5220\u9664\u5143\u7d20 */\n@discardableResult\nfunc remove(index: Int) -> Int {\nif index < 0 || index >= _size {\nfatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nlet num = nums[index]\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor j in index ..< (_size - 1) {\nnums[j] = nums[j + 1]\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n_size -= 1\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num\n}\n/* \u5217\u8868\u6269\u5bb9 */\nfunc 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\nnums = nums + Array(repeating: 0, count: _capacity * (extendRatio - 1))\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n_capacity = nums.count\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\nfunc toArray() -> [Int] {\nvar nums = Array(repeating: 0, count: _size)\nfor i in 0 ..< _size {\nnums[i] = get(index: i)\n}\nreturn nums\n}\n}\n
    my_list.zig
    // \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0\nfn MyList(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nnums: []T = undefined,                        // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nnums_capacity: usize = 10,                     // \u5217\u8868\u5bb9\u91cf\nnum_size: usize = 0,                           // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nextend_ratio: usize = 2,                       // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\nmem_arena: ?std.heap.ArenaAllocator = null,\nmem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n// \u6784\u9020\u65b9\u6cd5\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u5217\u8868\uff09\npub fn init(self: *Self, allocator: std.mem.Allocator) !void {\nif (self.mem_arena == null) {\nself.mem_arena = std.heap.ArenaAllocator.init(allocator);\nself.mem_allocator = self.mem_arena.?.allocator();\n}\nself.nums = try self.mem_allocator.alloc(T, self.nums_capacity);\nstd.mem.set(T, self.nums, @as(T, 0));\n}\n// \u6790\u6784\u65b9\u6cd5\uff08\u91ca\u653e\u5185\u5b58\uff09\npub fn deinit(self: *Self) void {\nif (self.mem_arena == null) return;\nself.mem_arena.?.deinit();\n}\n// \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\npub fn size(self: *Self) usize {\nreturn self.num_size;\n}\n// \u83b7\u53d6\u5217\u8868\u5bb9\u91cf\npub fn capacity(self: *Self) usize {\nreturn self.nums_capacity;\n}\n// \u8bbf\u95ee\u5143\u7d20\npub fn get(self: *Self, index: usize) T {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\nreturn self.nums[index];\n}  // \u66f4\u65b0\u5143\u7d20\npub fn set(self: *Self, index: usize, num: T) void {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\nself.nums[index] = num;\n}  // \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\npub fn add(self: *Self, num: T) !void {\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (self.size() == self.capacity()) try self.extendCapacity();\nself.nums[self.size()] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.num_size += 1;\n}  // \u4e2d\u95f4\u63d2\u5165\u5143\u7d20\npub fn insert(self: *Self, index: usize, num: T) !void {\nif (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\nif (self.size() == self.capacity()) try self.extendCapacity();\n// \u7d22\u5f15 i \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nvar j = self.size() - 1;\nwhile (j >= index) : (j -= 1) {\nself.nums[j + 1] = self.nums[j];\n}\nself.nums[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.num_size += 1;\n}\n// \u5220\u9664\u5143\u7d20\npub fn remove(self: *Self, index: usize) T {\nif (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\nvar num = self.nums[index];\n// \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nvar j = index;\nwhile (j < self.size() - 1) : (j += 1) {\nself.nums[j] = self.nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.num_size -= 1;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n// \u5217\u8868\u6269\u5bb9\npub fn extendCapacity(self: *Self) !void {\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\nvar newCapacity = self.capacity() * self.extend_ratio;\nvar extend = try self.mem_allocator.alloc(T, newCapacity);\nstd.mem.set(T, extend, @as(T, 0));\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nstd.mem.copy(T, extend, self.nums);\nself.nums = extend;\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nself.nums_capacity = newCapacity;\n}\n// \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\npub fn toArray(self: *Self) ![]T {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvar nums = try self.mem_allocator.alloc(T, self.size());\nstd.mem.set(T, nums, @as(T, 0));\nfor (nums) |*num, i| {\nnum.* = self.get(i);\n}\nreturn nums;\n}\n};\n}\n
    my_list.dart
    /* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nlate List<int> _nums; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nint _capacity = 10; // \u5217\u8868\u5bb9\u91cf\nint _size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nint _extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n/* \u6784\u9020\u65b9\u6cd5 */\nMyList() {\n_nums = List.filled(_capacity, 0);\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\nint size() => _size;\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nint capacity() => _capacity;\n/* \u8bbf\u95ee\u5143\u7d20 */\nint get(int index) {\nif (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\nreturn _nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nvoid set(int index, int num) {\nif (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n_nums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nvoid add(int num) {\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (_size == _capacity) extendCapacity();\n_nums[_size] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n_size++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nvoid insert(int index, int num) {\nif (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\nif (_size == _capacity) extendCapacity();\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (var j = _size - 1; j >= index; j--) {\n_nums[j + 1] = _nums[j];\n}\n_nums[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n_size++;\n}\n/* \u5220\u9664\u5143\u7d20 */\nint remove(int index) {\nif (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\nint num = _nums[index];\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (var j = index; j < _size - 1; j++) {\n_nums[j] = _nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n_size--;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\nvoid extendCapacity() {\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 _extendRatio \u500d\u7684\u65b0\u6570\u7ec4\nfinal _newNums = List.filled(_capacity * _extendRatio, 0);\n// \u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nList.copyRange(_newNums, 0, _nums);\n// \u66f4\u65b0 _nums \u7684\u5f15\u7528\n_nums = _newNums;\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n_capacity = _nums.length;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\nList<int> toArray() {\nList<int> nums = [];\nfor (var i = 0; i < _size; i++) {\nnums.add(get(i));\n}\nreturn nums;\n}\n}\n
    "},{"location":"chapter_array_and_linkedlist/summary/","title":"4.4. \u00a0 \u5c0f\u7ed3","text":"
    • \u6570\u7ec4\u548c\u94fe\u8868\u662f\u4e24\u79cd\u57fa\u672c\u6570\u636e\u7ed3\u6784\uff0c\u5206\u522b\u4ee3\u8868\u6570\u636e\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u548c\u79bb\u6563\u7a7a\u95f4\u5b58\u50a8\u65b9\u5f0f\u3002\u4e24\u8005\u7684\u4f18\u7f3a\u70b9\u5448\u73b0\u51fa\u4e92\u8865\u7684\u7279\u6027\u3002
    • \u6570\u7ec4\u652f\u6301\u968f\u673a\u8bbf\u95ee\u3001\u5360\u7528\u5185\u5b58\u8f83\u5c11\uff1b\u4f46\u63d2\u5165\u548c\u5220\u9664\u5143\u7d20\u6548\u7387\u4f4e\uff0c\u4e14\u521d\u59cb\u5316\u540e\u957f\u5ea6\u4e0d\u53ef\u53d8\u3002
    • \u94fe\u8868\u901a\u8fc7\u66f4\u6539\u6307\u9488\u5b9e\u73b0\u9ad8\u6548\u7684\u8282\u70b9\u63d2\u5165\u4e0e\u5220\u9664\uff0c\u4e14\u53ef\u4ee5\u7075\u6d3b\u8c03\u6574\u957f\u5ea6\uff1b\u4f46\u8282\u70b9\u8bbf\u95ee\u6548\u7387\u4f4e\u3001\u5360\u7528\u5185\u5b58\u8f83\u591a\u3002\u5e38\u89c1\u7684\u94fe\u8868\u7c7b\u578b\u5305\u62ec\u5355\u5411\u94fe\u8868\u3001\u5faa\u73af\u94fe\u8868\u3001\u53cc\u5411\u94fe\u8868\u3002
    • \u52a8\u6001\u6570\u7ec4\uff0c\u53c8\u79f0\u5217\u8868\uff0c\u662f\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u4e00\u79cd\u6570\u636e\u7ed3\u6784\u3002\u5b83\u4fdd\u7559\u4e86\u6570\u7ec4\u7684\u4f18\u52bf\uff0c\u540c\u65f6\u53ef\u4ee5\u7075\u6d3b\u8c03\u6574\u957f\u5ea6\u3002\u5217\u8868\u7684\u51fa\u73b0\u6781\u5927\u5730\u63d0\u9ad8\u4e86\u6570\u7ec4\u7684\u6613\u7528\u6027\uff0c\u4f46\u53ef\u80fd\u5bfc\u81f4\u90e8\u5206\u5185\u5b58\u7a7a\u95f4\u6d6a\u8d39\u3002
    • \u4e0b\u8868\u603b\u7ed3\u5e76\u5bf9\u6bd4\u4e86\u6570\u7ec4\u4e0e\u94fe\u8868\u7684\u5404\u9879\u7279\u6027\u4e0e\u64cd\u4f5c\u6548\u7387\u3002
    \u6570\u7ec4 \u94fe\u8868 \u5b58\u50a8\u65b9\u5f0f \u8fde\u7eed\u5185\u5b58\u7a7a\u95f4 \u79bb\u6563\u5185\u5b58\u7a7a\u95f4 \u6570\u636e\u7ed3\u6784\u957f\u5ea6 \u957f\u5ea6\u4e0d\u53ef\u53d8 \u957f\u5ea6\u53ef\u53d8 \u5185\u5b58\u4f7f\u7528\u7387 \u5360\u7528\u5185\u5b58\u5c11\u3001\u7f13\u5b58\u5c40\u90e8\u6027\u597d \u5360\u7528\u5185\u5b58\u591a \u4f18\u52bf\u64cd\u4f5c \u968f\u673a\u8bbf\u95ee \u63d2\u5165\u3001\u5220\u9664 \u8bbf\u95ee\u5143\u7d20 \\(O(1)\\) \\(O(N)\\) \u6dfb\u52a0\u5143\u7d20 \\(O(N)\\) \\(O(1)\\) \u5220\u9664\u5143\u7d20 \\(O(N)\\) \\(O(1)\\)

    \u7f13\u5b58\u5c40\u90e8\u6027

    \u5728\u8ba1\u7b97\u673a\u4e2d\uff0c\u6570\u636e\u8bfb\u5199\u901f\u5ea6\u6392\u5e8f\u662f\u201c\u786c\u76d8 < \u5185\u5b58 < CPU \u7f13\u5b58\u201d\u3002\u5f53\u6211\u4eec\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u65f6\uff0c\u8ba1\u7b97\u673a\u4e0d\u4ec5\u4f1a\u52a0\u8f7d\u5b83\uff0c\u8fd8\u4f1a\u7f13\u5b58\u5176\u5468\u56f4\u7684\u5176\u4ed6\u6570\u636e\uff0c\u4ece\u800c\u501f\u52a9\u9ad8\u901f\u7f13\u5b58\u6765\u63d0\u5347\u540e\u7eed\u64cd\u4f5c\u7684\u6267\u884c\u901f\u5ea6\u3002\u94fe\u8868\u5219\u4e0d\u7136\uff0c\u8ba1\u7b97\u673a\u53ea\u80fd\u6328\u4e2a\u5730\u7f13\u5b58\u5404\u4e2a\u8282\u70b9\uff0c\u8fd9\u6837\u7684\u591a\u6b21\u201c\u642c\u8fd0\u201d\u964d\u4f4e\u4e86\u6574\u4f53\u6548\u7387\u3002

    "},{"location":"chapter_array_and_linkedlist/summary/#441-q-a","title":"4.4.1. \u00a0 Q & A","text":"

    \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

    \u6808\u5185\u5b58\u5206\u914d\u7531\u7f16\u8bd1\u5668\u81ea\u52a8\u5b8c\u6210\uff0c\u800c\u5806\u5185\u5b58\u7531\u7a0b\u5e8f\u5458\u5728\u4ee3\u7801\u4e2d\u5206\u914d\uff08\u6ce8\u610f\uff0c\u8fd9\u91cc\u7684\u6808\u548c\u5806\u548c\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u6808\u548c\u5806\u4e0d\u662f\u540c\u4e00\u6982\u5ff5\uff09\u3002

    1. \u6808\u4e0d\u7075\u6d3b\uff0c\u5206\u914d\u7684\u5185\u5b58\u5927\u5c0f\u4e0d\u53ef\u66f4\u6539\uff1b\u5806\u76f8\u5bf9\u7075\u6d3b\uff0c\u53ef\u4ee5\u52a8\u6001\u5206\u914d\u5185\u5b58\uff1b
    2. \u6808\u662f\u4e00\u5757\u6bd4\u8f83\u5c0f\u7684\u5185\u5b58\uff0c\u5bb9\u6613\u51fa\u73b0\u5185\u5b58\u4e0d\u8db3\uff1b\u5806\u5185\u5b58\u5f88\u5927\uff0c\u4f46\u662f\u7531\u4e8e\u662f\u52a8\u6001\u5206\u914d\uff0c\u5bb9\u6613\u788e\u7247\u5316\uff0c\u7ba1\u7406\u5806\u5185\u5b58\u7684\u96be\u5ea6\u66f4\u5927\u3001\u6210\u672c\u66f4\u9ad8\uff1b
    3. \u8bbf\u95ee\u6808\u6bd4\u8bbf\u95ee\u5806\u66f4\u5feb\uff0c\u56e0\u4e3a\u6808\u5185\u5b58\u8f83\u5c0f\u3001\u5bf9\u7f13\u5b58\u53cb\u597d\uff0c\u5806\u5e27\u5206\u6563\u5728\u5f88\u5927\u7684\u7a7a\u95f4\u5185\uff0c\u4f1a\u51fa\u73b0\u66f4\u591a\u7684\u7f13\u5b58\u672a\u547d\u4e2d\uff1b

    \u4e3a\u4ec0\u4e48\u6570\u7ec4\u4f1a\u5f3a\u8c03\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\u7ed3\u70b9\u7ec4\u6210\uff0c\u7ed3\u70b9\u4e4b\u95f4\u7531\u6307\u9488\u8fde\u63a5\uff0c\u5404\u4e2a\u7ed3\u70b9\u53ef\u4ee5\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u4f8b\u5982 int, double, string, object \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\u5982\u679c\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\u90a3\u4e48\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 elementLength \u3002

    // \u5143\u7d20\u5185\u5b58\u5730\u5740 = \u6570\u7ec4\u5185\u5b58\u5730\u5740 + \u5143\u7d20\u957f\u5ea6 * \u5143\u7d20\u7d22\u5f15\nelementAddr = firtstElementAddr + elementLength * elementIndex\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\u7ed3\u70b9\u904d\u5386\u5230\u5c3e\u7ed3\u70b9\u5df2\u7ecf\u9047\u4e0d\u5230 P \u4e86\u3002\u8fd9\u610f\u5473\u7740\u7ed3\u70b9 P \u5df2\u7ecf\u4ece\u94fe\u8868\u4e2d\u5220\u9664\u4e86\uff0c\u6b64\u65f6\u7ed3\u70b9 P \u6307\u5411\u54ea\u91cc\u90fd\u4e0d\u4f1a\u5bf9\u8fd9\u6761\u94fe\u8868\u4ea7\u751f\u5f71\u54cd\u4e86\u3002

    \u4ece\u5783\u573e\u56de\u6536\u7684\u89d2\u5ea6\u770b\uff0c\u5bf9\u4e8e Java, Python, Go \u7b49\u62e5\u6709\u81ea\u52a8\u5783\u573e\u56de\u6536\u7684\u8bed\u8a00\u6765\u8bf4\uff0c\u8282\u70b9 P \u662f\u5426\u88ab\u56de\u6536\u53d6\u51b3\u4e8e\u662f\u5426\u6709\u4ecd\u5b58\u5728\u6307\u5411\u5b83\u7684\u5f15\u7528\uff0c\u800c\u4e0d\u662f P.next \u7684\u503c\u3002\u5728 C, 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)\\) \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\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\u7ed3\u70b9\u3001\u5c3e\u7ed3\u70b9\uff0c\u6bcf\u6b21\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u90fd\u662f \\(O(1)\\) \u3002

    \u56fe\u7247\u201c\u94fe\u8868\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f\u201d\u4e2d\uff0c\u6d45\u84dd\u8272\u7684\u5b58\u50a8\u7ed3\u70b9\u6307\u9488\u662f\u5360\u7528\u4e00\u5757\u5185\u5b58\u5730\u5740\u5417\uff1f\u8fd8\u662f\u548c\u7ed3\u70b9\u503c\u5404\u5360\u4e00\u534a\u5462\uff1f

    \u6587\u4e2d\u53ea\u662f\u4e00\u4e2a\u793a\u610f\u56fe\uff0c\u53ea\u662f\u5b9a\u6027\u8868\u793a\u3002\u5b9a\u91cf\u7684\u8bdd\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u5206\u6790\uff1a

    • \u4e0d\u540c\u7c7b\u578b\u7684\u7ed3\u70b9\u503c\u5360\u7528\u7684\u7a7a\u95f4\u662f\u4e0d\u540c\u7684\uff0c\u6bd4\u5982 int, long, double, \u6216\u8005\u662f\u7c7b\u7684\u5b9e\u4f8b\u7b49\u7b49\u3002
    • \u6307\u9488\u53d8\u91cf\u5360\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u5927\u5c0f\u6839\u636e\u6240\u4f7f\u7528\u7684\u64cd\u4f5c\u7cfb\u7edf\u53ca\u7f16\u8bd1\u73af\u5883\u800c\u5b9a\uff0c\u5927\u591a\u4e3a 8 \u5b57\u8282\u6216 4 \u5b57\u8282\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\u5927\u5927\u63d0\u5347\u4e86\u6570\u7ec4\u7684\u5b9e\u7528\u6027\uff0c\u4f46\u526f\u4f5c\u7528\u662f\u4f1a\u9020\u6210\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\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u4e3a\u4e86\u9632\u6b62\u9891\u7e41\u6269\u5bb9\uff0c\u6269\u5bb9\u4e00\u822c\u90fd\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

    "},{"location":"chapter_backtracking/","title":"12. \u00a0 \u56de\u6eaf","text":""},{"location":"chapter_backtracking/backtracking_algorithm/","title":"12.1. \u00a0 \u56de\u6eaf\u7b97\u6cd5","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\u300c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u300d\u6765\u904d\u5386\u89e3\u7a7a\u95f4\u3002\u5728\u4e8c\u53c9\u6811\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\u4e0b\u9762\uff0c\u6211\u4eec\u4ece\u4e8c\u53c9\u6811\u7684\u524d\u5e8f\u904d\u5386\u5165\u624b\uff0c\u9010\u6b65\u4e86\u89e3\u56de\u6eaf\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u3002

    \u4f8b\u9898\u4e00

    \u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811\uff0c\u641c\u7d22\u5e76\u8bb0\u5f55\u6240\u6709\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\uff0c\u8fd4\u56de\u8282\u70b9\u5217\u8868\u3002

    \u89e3\u9898\u601d\u8def\uff1a\u524d\u5e8f\u904d\u5386\u8fd9\u9897\u6811\uff0c\u5e76\u5224\u65ad\u5f53\u524d\u8282\u70b9\u7684\u503c\u662f\u5426\u4e3a \\(7\\) \uff0c\u82e5\u662f\u5219\u5c06\u8be5\u8282\u70b9\u7684\u503c\u52a0\u5165\u5230\u7ed3\u679c\u5217\u8868 res \u4e4b\u4e2d\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart preorder_traversal_i_compact.java
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode root) {\nif (root == null) {\nreturn;\n}\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.add(root);\n}\npreOrder(root.left);\npreOrder(root.right);\n}\n
    preorder_traversal_i_compact.cpp
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode *root) {\nif (root == nullptr) {\nreturn;\n}\nif (root->val == 7) {\n// \u8bb0\u5f55\u89e3\nres.push_back(root);\n}\npreOrder(root->left);\npreOrder(root->right);\n}\n
    preorder_traversal_i_compact.py
    def pre_order(root: TreeNode) -> None:\n\"\"\"\u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00\"\"\"\nif root is None:\nreturn\nif root.val == 7:\n# \u8bb0\u5f55\u89e3\nres.append(root)\npre_order(root.left)\npre_order(root.right)\n
    preorder_traversal_i_compact.go
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunc preOrderI(root *TreeNode, res *[]*TreeNode) {\nif root == nil {\nreturn\n}\nif int(root.Val) == 7 {\n// \u8bb0\u5f55\u89e3\n*res = append(*res, root)\n}\npreOrderI(root.Left, res)\npreOrderI(root.Right, res)\n}\n
    preorder_traversal_i_compact.js
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunction preOrder(root, res) {\nif (root === null) {\nreturn;\n}\nif (root.val === 7) {\n// \u8bb0\u5f55\u89e3\nres.push(root);\n}\npreOrder(root.left, res);\npreOrder(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 {\nif (root === null) {\nreturn;\n}\nif (root.val === 7) {\n// \u8bb0\u5f55\u89e3\nres.push(root);\n}\npreOrder(root.left, res);\npreOrder(root.right, res);\n}\n
    preorder_traversal_i_compact.c
    [class]{}-[func]{preOrder}\n
    preorder_traversal_i_compact.cs
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode root) {\nif (root == null) {\nreturn;\n}\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.Add(root);\n}\npreOrder(root.left);\npreOrder(root.right);\n}\n
    preorder_traversal_i_compact.swift
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunc preOrder(root: TreeNode?) {\nguard let root = root else {\nreturn\n}\nif root.val == 7 {\n// \u8bb0\u5f55\u89e3\nres.append(root)\n}\npreOrder(root: root.left)\npreOrder(root: root.right)\n}\n
    preorder_traversal_i_compact.zig
    [class]{}-[func]{preOrder}\n
    preorder_traversal_i_compact.dart
    [class]{}-[func]{preOrder}\n

    Fig. \u5728\u524d\u5e8f\u904d\u5386\u4e2d\u641c\u7d22\u8282\u70b9

    "},{"location":"chapter_backtracking/backtracking_algorithm/#1211","title":"12.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\u7ed3\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\u7b49\u4ef7\u4e8e\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\u8fd4\u56de\u6839\u8282\u70b9\u5230\u8fd9\u4e9b\u8282\u70b9\u7684\u8def\u5f84\u3002

    \u89e3\u9898\u601d\u8def\uff1a\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart preorder_traversal_ii_compact.java
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(TreeNode root) {\nif (root == null) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.add(root);\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.add(new ArrayList<>(path));\n}\npreOrder(root.left);\npreOrder(root.right);\n// \u56de\u9000\npath.remove(path.size() - 1);\n}\n
    preorder_traversal_ii_compact.cpp
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(TreeNode *root) {\nif (root == nullptr) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.push_back(root);\nif (root->val == 7) {\n// \u8bb0\u5f55\u89e3\nres.push_back(path);\n}\npreOrder(root->left);\npreOrder(root->right);\n// \u56de\u9000\npath.pop_back();\n}\n
    preorder_traversal_ii_compact.py
    def pre_order(root: TreeNode) -> None:\n\"\"\"\u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c\"\"\"\nif root is None:\nreturn\n# \u5c1d\u8bd5\npath.append(root)\nif root.val == 7:\n# \u8bb0\u5f55\u89e3\nres.append(list(path))\npre_order(root.left)\npre_order(root.right)\n# \u56de\u9000\npath.pop()\n
    preorder_traversal_ii_compact.go
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunc preOrderII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {\nif root == nil {\nreturn\n}\n// \u5c1d\u8bd5\n*path = append(*path, root)\nif int(root.Val) == 7 {\n// \u8bb0\u5f55\u89e3\n*res = append(*res, *path)\n}\npreOrderII(root.Left, res, path)\npreOrderII(root.Right, res, path)\n// \u56de\u9000\n*path = (*path)[:len(*path)-1]\n}\n
    preorder_traversal_ii_compact.js
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunction preOrder(root, path, res) {\nif (root === null) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.push(root);\nif (root.val === 7) {\n// \u8bb0\u5f55\u89e3\nres.push([...path]);\n}\npreOrder(root.left, path, res);\npreOrder(root.right, path, res);\n// \u56de\u9000\npath.pop();\n}\n
    preorder_traversal_ii_compact.ts
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunction preOrder(\nroot: TreeNode | null,\npath: TreeNode[],\nres: TreeNode[][]\n): void {\nif (root === null) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.push(root);\nif (root.val === 7) {\n// \u8bb0\u5f55\u89e3\nres.push([...path]);\n}\npreOrder(root.left, path, res);\npreOrder(root.right, path, res);\n// \u56de\u9000\npath.pop();\n}\n
    preorder_traversal_ii_compact.c
    [class]{}-[func]{preOrder}\n
    preorder_traversal_ii_compact.cs
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(TreeNode root) {\nif (root == null) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.Add(root);\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.Add(new List<TreeNode>(path));\n}\npreOrder(root.left);\npreOrder(root.right);\n// \u56de\u9000\npath.RemoveAt(path.Count - 1);\n}\n
    preorder_traversal_ii_compact.swift
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunc preOrder(root: TreeNode?) {\nguard let root = root else {\nreturn\n}\n// \u5c1d\u8bd5\npath.append(root)\nif root.val == 7 {\n// \u8bb0\u5f55\u89e3\nres.append(path)\n}\npreOrder(root: root.left)\npreOrder(root: root.right)\n// \u56de\u9000\npath.removeLast()\n}\n
    preorder_traversal_ii_compact.zig
    [class]{}-[func]{preOrder}\n
    preorder_traversal_ii_compact.dart
    [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\u6362\u53e5\u8bdd\u8bf4\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\u662f\u4e92\u4e3a\u76f8\u53cd\u7684\u3002

    <1><2><3><4><5><6><7><8><9><10><11>

    "},{"location":"chapter_backtracking/backtracking_algorithm/#1212","title":"12.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\u8fd4\u56de\u6839\u8282\u70b9\u5230\u8fd9\u4e9b\u8282\u70b9\u7684\u8def\u5f84\uff0c\u8def\u5f84\u4e2d\u4e0d\u80fd\u5305\u542b\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\u3002

    \u89e3\u9898\u601d\u8def\uff1a\u5728\u4f8b\u9898\u4e8c\u7684\u57fa\u7840\u4e0a\u6dfb\u52a0\u526a\u679d\u64cd\u4f5c\uff0c\u5f53\u9047\u5230\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\u65f6\uff0c\u5219\u7ec8\u6b62\u7ee7\u7eed\u641c\u7d22\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart preorder_traversal_iii_compact.java
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(TreeNode root) {\n// \u526a\u679d\nif (root == null || root.val == 3) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.add(root);\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.add(new ArrayList<>(path));\n}\npreOrder(root.left);\npreOrder(root.right);\n// \u56de\u9000\npath.remove(path.size() - 1);\n}\n
    preorder_traversal_iii_compact.cpp
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(TreeNode *root) {\n// \u526a\u679d\nif (root == nullptr || root->val == 3) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.push_back(root);\nif (root->val == 7) {\n// \u8bb0\u5f55\u89e3\nres.push_back(path);\n}\npreOrder(root->left);\npreOrder(root->right);\n// \u56de\u9000\npath.pop_back();\n}\n
    preorder_traversal_iii_compact.py
    def pre_order(root: TreeNode) -> None:\n\"\"\"\u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09\"\"\"\n# \u526a\u679d\nif root is None or root.val == 3:\nreturn\n# \u5c1d\u8bd5\npath.append(root)\nif root.val == 7:\n# \u8bb0\u5f55\u89e3\nres.append(list(path))\npre_order(root.left)\npre_order(root.right)\n# \u56de\u9000\npath.pop()\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\nif root == nil || root.Val == 3 {\nreturn\n}\n// \u5c1d\u8bd5\n*path = append(*path, root)\nif int(root.Val) == 7 {\n// \u8bb0\u5f55\u89e3\n*res = append(*res, *path)\n}\npreOrderIII(root.Left, res, path)\npreOrderIII(root.Right, res, path)\n// \u56de\u9000\n*path = (*path)[:len(*path)-1]\n}\n
    preorder_traversal_iii_compact.js
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunction preOrder(root, path, res) {\n// \u526a\u679d\nif (root === null || root.val === 3) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.push(root);\nif (root.val === 7) {\n// \u8bb0\u5f55\u89e3\nres.push([...path]);\n}\npreOrder(root.left, path, res);\npreOrder(root.right, path, res);\n// \u56de\u9000\npath.pop();\n}\n
    preorder_traversal_iii_compact.ts
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunction preOrder(\nroot: TreeNode | null,\npath: TreeNode[],\nres: TreeNode[][]\n): void {\n// \u526a\u679d\nif (root === null || root.val === 3) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.push(root);\nif (root.val === 7) {\n// \u8bb0\u5f55\u89e3\nres.push([...path]);\n}\npreOrder(root.left, path, res);\npreOrder(root.right, path, res);\n// \u56de\u9000\npath.pop();\n}\n
    preorder_traversal_iii_compact.c
    [class]{}-[func]{preOrder}\n
    preorder_traversal_iii_compact.cs
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(TreeNode root) {\n// \u526a\u679d\nif (root == null || root.val == 3) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.Add(root);\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.Add(new List<TreeNode>(path));\n}\npreOrder(root.left);\npreOrder(root.right);\n// \u56de\u9000\npath.RemoveAt(path.Count - 1);\n}\n
    preorder_traversal_iii_compact.swift
    /* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunc preOrder(root: TreeNode?) {\n// \u526a\u679d\nguard let root = root, root.val != 3 else {\nreturn\n}\n// \u5c1d\u8bd5\npath.append(root)\nif root.val == 7 {\n// \u8bb0\u5f55\u89e3\nres.append(path)\n}\npreOrder(root: root.left)\npreOrder(root: root.right)\n// \u56de\u9000\npath.removeLast()\n}\n
    preorder_traversal_iii_compact.zig
    [class]{}-[func]{preOrder}\n
    preorder_traversal_iii_compact.dart
    [class]{}-[func]{preOrder}\n

    \u526a\u679d\u662f\u4e00\u4e2a\u975e\u5e38\u5f62\u8c61\u7684\u540d\u8bcd\u3002\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u5229\u7528\u7ea6\u675f\u6761\u4ef6\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\u5347\u641c\u7d22\u6548\u7387\u3002

    Fig. \u6839\u636e\u7ea6\u675f\u6761\u4ef6\u526a\u679d

    "},{"location":"chapter_backtracking/backtracking_algorithm/#1213","title":"12.1.3. \u00a0 \u5e38\u7528\u672f\u8bed","text":"

    \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\u3002

    \u540d\u8bcd \u5b9a\u4e49 \u4f8b\u9898\u4e09 \u89e3 Solution \u89e3\u662f\u6ee1\u8db3\u95ee\u9898\u7279\u5b9a\u6761\u4ef6\u7684\u7b54\u6848\u3002\u56de\u6eaf\u7b97\u6cd5\u7684\u76ee\u6807\u662f\u627e\u5230\u4e00\u4e2a\u6216\u591a\u4e2a\u6ee1\u8db3\u6761\u4ef6\u7684\u89e3 \u6839\u8282\u70b9\u5230\u8282\u70b9 \\(7\\) \u7684\u6240\u6709\u8def\u5f84\uff0c\u4e14\u8def\u5f84\u4e2d\u4e0d\u5305\u542b\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9 \u72b6\u6001 State \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\u5373 path \u8282\u70b9\u5217\u8868 \u7ea6\u675f\u6761\u4ef6 Constraint \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 \u8981\u6c42\u8def\u5f84\u4e2d\u4e0d\u80fd\u5305\u542b\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9 \u5c1d\u8bd5 Attempt \u5c1d\u8bd5\u662f\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u6839\u636e\u5f53\u524d\u72b6\u6001\u548c\u53ef\u7528\u9009\u62e9\u6765\u63a2\u7d22\u89e3\u7a7a\u95f4\u7684\u8fc7\u7a0b\u3002\u5c1d\u8bd5\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 Backtracking \u56de\u9000\u6307\u5728\u641c\u7d22\u4e2d\u9047\u5230\u5230\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u6216\u65e0\u6cd5\u7ee7\u7eed\u641c\u7d22\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\u7ed3\u70b9\u3001\u7ed3\u675f\u7ed3\u70b9\u8bbf\u95ee\u3001\u9047\u5230\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\u65f6\u7ec8\u6b62\u641c\u7d22\uff0c\u51fd\u6570\u8fd4\u56de \u526a\u679d Pruning \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\u7ec8\u6b62\u7ee7\u7eed\u641c\u7d22

    Tip

    \u89e3\u3001\u72b6\u6001\u3001\u7ea6\u675f\u6761\u4ef6\u7b49\u672f\u8bed\u662f\u901a\u7528\u7684\uff0c\u9002\u7528\u4e8e\u56de\u6eaf\u7b97\u6cd5\u3001\u52a8\u6001\u89c4\u5212\u3001\u8d2a\u5fc3\u7b97\u6cd5\u7b49\u3002

    "},{"location":"chapter_backtracking/backtracking_algorithm/#1214","title":"12.1.4. \u00a0 \u6846\u67b6\u4ee3\u7801","text":"

    \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\u4e3a\u63d0\u5347\u4ee3\u7801\u901a\u7528\u6027\uff0c\u6211\u4eec\u5e0c\u671b\u5c06\u56de\u6eaf\u7b97\u6cd5\u7684\u201c\u5c1d\u8bd5\u3001\u56de\u9000\u3001\u526a\u679d\u201d\u7684\u4e3b\u4f53\u6846\u67b6\u63d0\u70bc\u51fa\u6765\u3002

    \u8bbe state \u4e3a\u95ee\u9898\u7684\u5f53\u524d\u72b6\u6001\uff0cchoices \u8868\u793a\u5f53\u524d\u72b6\u6001\u4e0b\u53ef\u4ee5\u505a\u51fa\u7684\u9009\u62e9\uff0c\u5219\u53ef\u5f97\u5230\u4ee5\u4e0b\u56de\u6eaf\u7b97\u6cd5\u7684\u6846\u67b6\u4ee3\u7801\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State state, List<Choice> choices, List<State> res) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (Choice choice : choices) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\nbacktrack(state, choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice);\n}\n}\n}\n
    /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State *state, vector<Choice *> &choices, vector<State *> &res) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (Choice choice : choices) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\nbacktrack(state, choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice);\n}\n}\n}\n
    def backtrack(state: State, choices: list[choice], res: list[state]) -> None:\n\"\"\"\u56de\u6eaf\u7b97\u6cd5\u6846\u67b6\"\"\"\n# \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif is_solution(state):\n# \u8bb0\u5f55\u89e3\nrecord_solution(state, res)\nreturn\n# \u904d\u5386\u6240\u6709\u9009\u62e9\nfor choice in choices:\n# \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif is_valid(state, choice):\n# \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmake_choice(state, choice)\nbacktrack(state, choices, res)\n# \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundo_choice(state, choice)\n
    /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfunc backtrack(state *State, choices []Choice, res *[]State) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif isSolution(state) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor _, choice := range choices {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif isValid(state, choice) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice)\nbacktrack(state, choices, res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice)\n}\n}\n}\n
    /* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfunction backtrack(state, choices, res) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (let choice of choices) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\nbacktrack(state, choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(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\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (let choice of choices) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\nbacktrack(state, choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(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\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res, numRes);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i < numChoices; i++) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, &choices[i])) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, &choices[i]);\nbacktrack(state, choices, numChoices, res, numRes);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, &choices[i]);\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\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nforeach (Choice choice in choices) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\nbacktrack(state, choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(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\nif isSolution(state: state) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state: state, res: &res)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor choice in choices {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif isValid(state: state, choice: choice) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state: &state, choice: choice)\nbacktrack(state: &state, choices: choices, res: &res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state: &state, choice: choice)\n}\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\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (Choice choice in choices) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\nbacktrack(state, choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice);\n}\n}\n}\n

    \u4e0b\u9762\uff0c\u6211\u4eec\u5c1d\u8bd5\u57fa\u4e8e\u6b64\u6846\u67b6\u6765\u89e3\u51b3\u4f8b\u9898\u4e09\u3002\u5728\u4f8b\u9898\u4e09\u4e2d\uff0c\u72b6\u6001 state \u662f\u8282\u70b9\u904d\u5386\u8def\u5f84\uff0c\u9009\u62e9 choices \u662f\u5f53\u524d\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\uff0c\u7ed3\u679c res \u662f\u8def\u5f84\u5217\u8868\uff0c\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart preorder_traversal_iii_template.java
    /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nboolean isSolution(List<TreeNode> state) {\nreturn !state.isEmpty() && state.get(state.size() - 1).val == 7;\n}\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\nres.add(new ArrayList<>(state));\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) {\nreturn choice != null && choice.val != 3;\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(List<TreeNode> state, TreeNode choice) {\nstate.add(choice);\n}\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(List<TreeNode> state, TreeNode choice) {\nstate.remove(state.size() - 1);\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\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (TreeNode choice : choices) {\n// \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(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\nundoChoice(state, choice);\n}\n}\n}\n
    preorder_traversal_iii_template.cpp
    /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool isSolution(vector<TreeNode *> &state) {\nreturn !state.empty() && state.back()->val == 7;\n}\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(vector<TreeNode *> &state, vector<vector<TreeNode *>> &res) {\nres.push_back(state);\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) {\nreturn choice != nullptr && choice->val != 3;\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(vector<TreeNode *> &state, TreeNode *choice) {\nstate.push_back(choice);\n}\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(vector<TreeNode *> &state, TreeNode *choice) {\nstate.pop_back();\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\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (TreeNode *choice : choices) {\n// \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nvector<TreeNode *> nextChoices{choice->left, choice->right};\nbacktrack(state, nextChoices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice);\n}\n}\n}\n
    preorder_traversal_iii_template.py
    def is_solution(state: list[TreeNode]) -> bool:\n\"\"\"\u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3\"\"\"\nreturn state and state[-1].val == 7\ndef record_solution(state: list[TreeNode], res: list[list[TreeNode]]):\n\"\"\"\u8bb0\u5f55\u89e3\"\"\"\nres.append(list(state))\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\"\"\"\nreturn choice is not None and choice.val != 3\ndef make_choice(state: list[TreeNode], choice: TreeNode):\n\"\"\"\u66f4\u65b0\u72b6\u6001\"\"\"\nstate.append(choice)\ndef undo_choice(state: list[TreeNode], choice: TreeNode):\n\"\"\"\u6062\u590d\u72b6\u6001\"\"\"\nstate.pop()\ndef backtrack(\nstate: 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\nif is_solution(state):\n# \u8bb0\u5f55\u89e3\nrecord_solution(state, res)\nreturn\n# \u904d\u5386\u6240\u6709\u9009\u62e9\nfor choice in choices:\n# \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif is_valid(state, choice):\n# \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmake_choice(state, choice)\n# \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, [choice.left, choice.right], res)\n# \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundo_choice(state, choice)\n
    preorder_traversal_iii_template.go
    /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfunc isSolution(state *[]*TreeNode) bool {\nreturn len(*state) != 0 && (*state)[len(*state)-1].Val == 7\n}\n/* \u8bb0\u5f55\u89e3 */\nfunc recordSolution(state *[]*TreeNode, res *[][]*TreeNode) {\n*res = append(*res, *state)\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 {\nreturn choice != nil && choice.Val != 3\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nfunc makeChoice(state *[]*TreeNode, choice *TreeNode) {\n*state = append(*state, choice)\n}\n/* \u6062\u590d\u72b6\u6001 */\nfunc undoChoice(state *[]*TreeNode, choice *TreeNode) {\n*state = (*state)[:len(*state)-1]\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\nif isSolution(state) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor _, choice := range *choices {\n// \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif isValid(state, choice) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice)\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\ntemp := make([]*TreeNode, 0)\ntemp = append(temp, choice.Left, choice.Right)\nbacktrackIII(state, &temp, res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice)\n}\n}\n}\n
    preorder_traversal_iii_template.js
    /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfunction isSolution(state) {\nreturn state && state[state.length - 1]?.val === 7;\n}\n/* \u8bb0\u5f55\u89e3 */\nfunction recordSolution(state, res) {\nres.push([...state]);\n}\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfunction isValid(state, choice) {\nreturn choice !== null && choice.val !== 3;\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nfunction makeChoice(state, choice) {\nstate.push(choice);\n}\n/* \u6062\u590d\u72b6\u6001 */\nfunction undoChoice(state) {\nstate.pop();\n}\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfunction backtrack(state, choices, res) {\n// \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (const choice of choices) {\n// \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, [choice.left, choice.right], res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(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 {\nreturn state && state[state.length - 1]?.val === 7;\n}\n/* \u8bb0\u5f55\u89e3 */\nfunction recordSolution(state: TreeNode[], res: TreeNode[][]): void {\nres.push([...state]);\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 {\nreturn choice !== null && choice.val !== 3;\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nfunction makeChoice(state: TreeNode[], choice: TreeNode): void {\nstate.push(choice);\n}\n/* \u6062\u590d\u72b6\u6001 */\nfunction undoChoice(state: TreeNode[]): void {\nstate.pop();\n}\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfunction backtrack(\nstate: TreeNode[],\nchoices: TreeNode[],\nres: TreeNode[][]\n): void {\n// \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (const choice of choices) {\n// \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, [choice.left, choice.right], res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state);\n}\n}\n}\n
    preorder_traversal_iii_template.c
    [class]{}-[func]{isSolution}\n[class]{}-[func]{recordSolution}\n[class]{}-[func]{isValid}\n[class]{}-[func]{makeChoice}\n[class]{}-[func]{undoChoice}\n[class]{}-[func]{backtrack}\n
    preorder_traversal_iii_template.cs
    /* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool isSolution(List<TreeNode> state) {\nreturn state.Count != 0 && state[^1].val == 7;\n}\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\nres.Add(new List<TreeNode>(state));\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) {\nreturn choice != null && choice.val != 3;\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(List<TreeNode> state, TreeNode choice) {\nstate.Add(choice);\n}\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(List<TreeNode> state, TreeNode choice) {\nstate.RemoveAt(state.Count - 1);\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\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nforeach (TreeNode choice in choices) {\n// \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, new List<TreeNode> { choice.left, choice.right }, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(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/* \u8bb0\u5f55\u89e3 */\nfunc recordSolution(state: [TreeNode], res: inout [[TreeNode]]) {\nres.append(state)\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 {\nchoice != nil && choice!.val != 3\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nfunc makeChoice(state: inout [TreeNode], choice: TreeNode) {\nstate.append(choice)\n}\n/* \u6062\u590d\u72b6\u6001 */\nfunc undoChoice(state: inout [TreeNode], choice: TreeNode) {\nstate.removeLast()\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\nif isSolution(state: state) {\nrecordSolution(state: state, res: &res)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor choice in choices {\n// \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif isValid(state: state, choice: choice) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state: &state, choice: choice)\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(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\nundoChoice(state: &state, choice: choice)\n}\n}\n}\n
    preorder_traversal_iii_template.zig
    [class]{}-[func]{isSolution}\n[class]{}-[func]{recordSolution}\n[class]{}-[func]{isValid}\n[class]{}-[func]{makeChoice}\n[class]{}-[func]{undoChoice}\n[class]{}-[func]{backtrack}\n
    preorder_traversal_iii_template.dart
    [class]{}-[func]{isSolution}\n[class]{}-[func]{recordSolution}\n[class]{}-[func]{isValid}\n[class]{}-[func]{makeChoice}\n[class]{}-[func]{undoChoice}\n[class]{}-[func]{backtrack}\n

    \u76f8\u8f83\u4e8e\u57fa\u4e8e\u524d\u5e8f\u904d\u5386\u7684\u5b9e\u73b0\u4ee3\u7801\uff0c\u57fa\u4e8e\u56de\u6eaf\u7b97\u6cd5\u6846\u67b6\u7684\u5b9e\u73b0\u4ee3\u7801\u867d\u7136\u663e\u5f97\u5570\u55e6\uff0c\u4f46\u901a\u7528\u6027\u66f4\u597d\u3002\u5b9e\u9645\u4e0a\uff0c\u6240\u6709\u56de\u6eaf\u95ee\u9898\u90fd\u53ef\u4ee5\u5728\u8be5\u6846\u67b6\u4e0b\u89e3\u51b3\u3002\u6211\u4eec\u9700\u8981\u6839\u636e\u5177\u4f53\u95ee\u9898\u6765\u5b9a\u4e49 state \u548c choices \uff0c\u5e76\u5b9e\u73b0\u6846\u67b6\u4e2d\u7684\u5404\u4e2a\u65b9\u6cd5\u3002

    "},{"location":"chapter_backtracking/backtracking_algorithm/#1215","title":"12.1.5. \u00a0 \u5178\u578b\u4f8b\u9898","text":"

    \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

    • \u5168\u6392\u5217\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u96c6\u5408\uff0c\u6c42\u51fa\u5176\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u7ec4\u5408\u3002
    • \u5b50\u96c6\u548c\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u96c6\u5408\u548c\u4e00\u4e2a\u76ee\u6807\u548c\uff0c\u627e\u5230\u96c6\u5408\u4e2d\u6240\u6709\u548c\u4e3a\u76ee\u6807\u548c\u7684\u5b50\u96c6\u3002
    • \u6c49\u8bfa\u5854\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e09\u4e2a\u67f1\u5b50\u548c\u4e00\u7cfb\u5217\u5927\u5c0f\u4e0d\u540c\u7684\u5706\u76d8\uff0c\u8981\u6c42\u5c06\u6240\u6709\u5706\u76d8\u4ece\u4e00\u4e2a\u67f1\u5b50\u79fb\u52a8\u5230\u53e6\u4e00\u4e2a\u67f1\u5b50\uff0c\u6bcf\u6b21\u53ea\u80fd\u79fb\u52a8\u4e00\u4e2a\u5706\u76d8\uff0c\u4e14\u4e0d\u80fd\u5c06\u5927\u5706\u76d8\u653e\u5728\u5c0f\u5706\u76d8\u4e0a\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

    • \\(n\\) \u7687\u540e\uff1a\u5728 \\(n \\times n\\) \u7684\u68cb\u76d8\u4e0a\u653e\u7f6e \\(n\\) \u4e2a\u7687\u540e\uff0c\u4f7f\u5f97\u5b83\u4eec\u4e92\u4e0d\u653b\u51fb\u3002
    • \u6570\u72ec\uff1a\u5728 \\(9 \\times 9\\) \u7684\u7f51\u683c\u4e2d\u586b\u5165\u6570\u5b57 \\(1\\) ~ \\(9\\) \uff0c\u4f7f\u5f97\u6bcf\u884c\u3001\u6bcf\u5217\u548c\u6bcf\u4e2a \\(3 \\times 3\\) \u5b50\u7f51\u683c\u4e2d\u7684\u6570\u5b57\u4e0d\u91cd\u590d\u3002
    • \u56fe\u7740\u8272\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u65e0\u5411\u56fe\uff0c\u7528\u6700\u5c11\u7684\u989c\u8272\u7ed9\u56fe\u7684\u6bcf\u4e2a\u9876\u70b9\u7740\u8272\uff0c\u4f7f\u5f97\u76f8\u90bb\u9876\u70b9\u989c\u8272\u4e0d\u540c\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

    • 0-1 \u80cc\u5305\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u7ec4\u7269\u54c1\u548c\u4e00\u4e2a\u80cc\u5305\uff0c\u6bcf\u4e2a\u7269\u54c1\u6709\u4e00\u5b9a\u7684\u4ef7\u503c\u548c\u91cd\u91cf\uff0c\u8981\u6c42\u5728\u80cc\u5305\u5bb9\u91cf\u9650\u5236\u5185\uff0c\u9009\u62e9\u7269\u54c1\u4f7f\u5f97\u603b\u4ef7\u503c\u6700\u5927\u3002
    • \u65c5\u884c\u5546\u95ee\u9898\uff1a\u5728\u4e00\u4e2a\u56fe\u4e2d\uff0c\u4ece\u4e00\u4e2a\u70b9\u51fa\u53d1\uff0c\u8bbf\u95ee\u6240\u6709\u5176\u4ed6\u70b9\u6070\u597d\u4e00\u6b21\u540e\u8fd4\u56de\u8d77\u70b9\uff0c\u6c42\u6700\u77ed\u8def\u5f84\u3002
    • \u6700\u5927\u56e2\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u65e0\u5411\u56fe\uff0c\u627e\u5230\u6700\u5927\u7684\u5b8c\u5168\u5b50\u56fe\uff0c\u5373\u5b50\u56fe\u4e2d\u7684\u4efb\u610f\u4e24\u4e2a\u9876\u70b9\u4e4b\u95f4\u90fd\u6709\u8fb9\u76f8\u8fde\u3002

    \u5728\u63a5\u4e0b\u6765\u7684\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u4e00\u8d77\u653b\u514b\u51e0\u4e2a\u7ecf\u5178\u7684\u56de\u6eaf\u7b97\u6cd5\u95ee\u9898\u3002

    "},{"location":"chapter_backtracking/n_queens_problem/","title":"12.3. \u00a0 N \u7687\u540e\u95ee\u9898","text":"

    Question

    \u6839\u636e\u56fd\u9645\u8c61\u68cb\u7684\u89c4\u5219\uff0c\u7687\u540e\u53ef\u4ee5\u653b\u51fb\u4e0e\u4e4b\u5904\u5728\u540c\u4e00\u884c\u6216\u540c\u4e00\u5217\u6216\u540c\u4e00\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\u4e0b\u56fe\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

    Fig. 4 \u7687\u540e\u95ee\u9898\u7684\u89e3

    \u672c\u9898\u5171\u6709\u4e09\u4e2a\u7ea6\u675f\u6761\u4ef6\uff1a\u591a\u4e2a\u7687\u540e\u4e0d\u80fd\u5728\u540c\u4e00\u884c\u3001\u540c\u4e00\u5217\u548c\u540c\u4e00\u5bf9\u89d2\u7ebf\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5bf9\u89d2\u7ebf\u5206\u4e3a\u4e3b\u5bf9\u89d2\u7ebf \\ \u548c\u526f\u5bf9\u89d2\u7ebf / \u4e24\u79cd\u3002

    Fig. n \u7687\u540e\u95ee\u9898\u7684\u7ea6\u675f\u6761\u4ef6

    \u7687\u540e\u7684\u6570\u91cf\u548c\u68cb\u76d8\u7684\u884c\u6570\u90fd\u4e3a \\(n\\) \uff0c\u56e0\u6b64\u6211\u4eec\u5bb9\u6613\u5f97\u5230\u7b2c\u4e00\u4e2a\u63a8\u8bba\uff1a\u68cb\u76d8\u6bcf\u884c\u90fd\u5141\u8bb8\u4e14\u53ea\u5141\u8bb8\u653e\u7f6e\u4e00\u4e2a\u7687\u540e\u3002\u8fd9\u610f\u5473\u7740\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\u6b64\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

    \u4e0b\u56fe\u5c55\u793a\u4e86 \\(4\\) \u7687\u540e\u95ee\u9898\u7684\u9010\u884c\u653e\u7f6e\u8fc7\u7a0b\u3002\u53d7\u7bc7\u5e45\u9650\u5236\uff0c\u4e0b\u56fe\u4ec5\u5c55\u5f00\u4e86\u7b2c\u4e00\u884c\u7684\u4e00\u4e2a\u641c\u7d22\u5206\u652f\u3002\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u4e0d\u6ee1\u8db3\u5217\u7ea6\u675f\u548c\u5bf9\u89d2\u7ebf\u7ea6\u675f\u7684\u65b9\u6848\u90fd\u526a\u679d\u4e86\u3002

    Fig. \u9010\u884c\u653e\u7f6e\u7b56\u7565

    \u4e3a\u4e86\u5b9e\u73b0\u6839\u636e\u5217\u7ea6\u675f\u526a\u679d\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\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\u89c2\u5bdf\u77e9\u9635\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\u76f8\u7b49\uff0c\u5373 row - col \u4e3a\u6052\u5b9a\u503c\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u82e5\u4e24\u4e2a\u683c\u5b50\u6ee1\u8db3 row1 - col1 == row2 - col2 \uff0c\u5219\u8fd9\u4e24\u4e2a\u683c\u5b50\u4e00\u5b9a\u5904\u5728\u4e00\u6761\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u3002

    \u5229\u7528\u8be5\u6027\u8d28\uff0c\u6211\u4eec\u53ef\u4ee5\u501f\u52a9\u4e00\u4e2a\u6570\u7ec4 diag1 \u6765\u8bb0\u5f55\u6bcf\u6761\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\u3002\u6ce8\u610f\uff0c\\(n\\) \u7ef4\u65b9\u9635 row - col \u7684\u8303\u56f4\u662f \\([-n + 1, n - 1]\\) \uff0c\u56e0\u6b64\u5171\u6709 \\(2n - 1\\) \u6761\u4e3b\u5bf9\u89d2\u7ebf\u3002

    Fig. \u5904\u7406\u5217\u7ea6\u675f\u548c\u5bf9\u89d2\u7ebf\u7ea6\u675f

    \u540c\u7406\uff0c\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u7684\u6240\u6709\u683c\u5b50\u7684 row + col \u662f\u6052\u5b9a\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u540c\u6837\u7684\u65b9\u6cd5\uff0c\u501f\u52a9\u6570\u7ec4 diag2 \u6765\u5904\u7406\u6b21\u5bf9\u89d2\u7ebf\u7ea6\u675f\u3002

    \u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u6211\u4eec\u4fbf\u53ef\u4ee5\u5199\u51fa \\(n\\) \u7687\u540e\u7684\u89e3\u9898\u4ee3\u7801\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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,\nboolean[] cols, boolean[] diags1, boolean[] diags2) {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (row == n) {\nList<List<String>> copyState = new ArrayList<>();\nfor (List<String> sRow : state) {\ncopyState.add(new ArrayList<>(sRow));\n}\nres.add(copyState);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor (int col = 0; col < n; col++) {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\nint diag1 = row - col + n - 1;\nint diag2 = row + col;\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728 (\u5217 \u6216 \u4e3b\u5bf9\u89d2\u7ebf \u6216 \u526f\u5bf9\u89d2\u7ebf) \u5305\u542b\u7687\u540e\nif (!(cols[col] || diags1[diag1] || diags2[diag2])) {\n// \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\nstate.get(row).set(col, \"Q\");\ncols[col] = diags1[diag1] = diags2[diag2] = true;\n// \u653e\u7f6e\u4e0b\u4e00\u884c\nbacktrack(row + 1, n, state, res, cols, diags1, diags2);\n// \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\nstate.get(row).set(col, \"#\");\ncols[col] = diags1[diag1] = diags2[diag2] = false;\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\nList<List<String>> state = new ArrayList<>();\nfor (int i = 0; i < n; i++) {\nList<String> row = new ArrayList<>();\nfor (int j = 0; j < n; j++) {\nrow.add(\"#\");\n}\nstate.add(row);\n}\nboolean[] cols = new boolean[n]; // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nboolean[] diags1 = new boolean[2 * n - 1]; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nboolean[] diags2 = new boolean[2 * n - 1]; // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nList<List<List<String>>> res = new ArrayList<>();\nbacktrack(0, n, state, res, cols, diags1, diags2);\nreturn res;\n}\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,\nvector<bool> &diags1, vector<bool> &diags2) {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (row == n) {\nres.push_back(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor (int col = 0; col < n; col++) {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\nint diag1 = row - col + n - 1;\nint diag2 = row + col;\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728 (\u5217 \u6216 \u4e3b\u5bf9\u89d2\u7ebf \u6216 \u526f\u5bf9\u89d2\u7ebf) \u5305\u542b\u7687\u540e\nif (!(cols[col] || diags1[diag1] || diags2[diag2])) {\n// \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\nstate[row][col] = \"Q\";\ncols[col] = diags1[diag1] = diags2[diag2] = true;\n// \u653e\u7f6e\u4e0b\u4e00\u884c\nbacktrack(row + 1, n, state, res, cols, diags1, diags2);\n// \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\nstate[row][col] = \"#\";\ncols[col] = diags1[diag1] = diags2[diag2] = false;\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\nvector<vector<string>> state(n, vector<string>(n, \"#\"));\nvector<bool> cols(n, false);           // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nvector<bool> diags1(2 * n - 1, false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nvector<bool> diags2(2 * n - 1, false); // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nvector<vector<vector<string>>> res;\nbacktrack(0, n, state, res, cols, diags1, diags2);\nreturn res;\n}\n
    n_queens.py
    def backtrack(\nrow: int,\nn: int,\nstate: list[list[str]],\nres: list[list[list[str]]],\ncols: list[bool],\ndiags1: list[bool],\ndiags2: list[bool],\n):\n\"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e\"\"\"\n# \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif row == n:\nres.append([list(row) for row in state])\nreturn\n# \u904d\u5386\u6240\u6709\u5217\nfor col in range(n):\n# \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\ndiag1 = row - col + n - 1\ndiag2 = row + col\n# \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728 (\u5217 \u6216 \u4e3b\u5bf9\u89d2\u7ebf \u6216 \u526f\u5bf9\u89d2\u7ebf) \u5305\u542b\u7687\u540e\nif not (cols[col] or diags1[diag1] or diags2[diag2]):\n# \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\nstate[row][col] = \"Q\"\ncols[col] = diags1[diag1] = diags2[diag2] = True\n# \u653e\u7f6e\u4e0b\u4e00\u884c\nbacktrack(row + 1, n, state, res, cols, diags1, diags2)\n# \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\nstate[row][col] = \"#\"\ncols[col] = diags1[diag1] = diags2[diag2] = False\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\nstate = [[\"#\" for _ in range(n)] for _ in range(n)]\ncols = [False] * n  # \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\ndiags1 = [False] * (2 * n - 1)  # \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\ndiags2 = [False] * (2 * n - 1)  # \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nres = []\nbacktrack(0, n, state, res, cols, diags1, diags2)\nreturn res\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\nif row == n {\nnewState := make([][]string, len(*state))\nfor i, _ := range newState {\nnewState[i] = make([]string, len((*state)[0]))\ncopy(newState[i], (*state)[i])\n}\n*res = append(*res, newState)\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor col := 0; col < n; col++ {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\ndiag1 := row - col + n - 1\ndiag2 := row + col\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728 (\u5217 \u6216 \u4e3b\u5bf9\u89d2\u7ebf \u6216 \u526f\u5bf9\u89d2\u7ebf) \u5305\u542b\u7687\u540e\nif !((*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\nbacktrack(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/* \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\nif row == n {\nnewState := make([][]string, len(*state))\nfor i, _ := range newState {\nnewState[i] = make([]string, len((*state)[0]))\ncopy(newState[i], (*state)[i])\n}\n*res = append(*res, newState)\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor col := 0; col < n; col++ {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\ndiag1 := row - col + n - 1\ndiag2 := row + col\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728 (\u5217 \u6216 \u4e3b\u5bf9\u89d2\u7ebf \u6216 \u526f\u5bf9\u89d2\u7ebf) \u5305\u542b\u7687\u540e\nif !((*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\nbacktrack(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}\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\nstate := make([][]string, n)\nfor i := 0; i < n; i++ {\nrow := make([]string, n)\nfor i := 0; i < n; i++ {\nrow[i] = \"#\"\n}\nstate[i] = row\n}\n// \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\ncols := make([]bool, n)\ndiags1 := make([]bool, 2*n-1)\ndiags2 := make([]bool, 2*n-1)\nres := make([][][]string, 0)\nbacktrack(0, n, &state, &res, &cols, &diags1, &diags2)\nreturn 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\nif (row === n) {\nres.push(state.map((row) => row.slice()));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor (let col = 0; col < n; col++) {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\nconst diag1 = row - col + n - 1;\nconst diag2 = row + col;\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728 (\u5217 \u6216 \u4e3b\u5bf9\u89d2\u7ebf \u6216 \u526f\u5bf9\u89d2\u7ebf) \u5305\u542b\u7687\u540e\nif (!(cols[col] || diags1[diag1] || diags2[diag2])) {\n// \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\nstate[row][col] = 'Q';\ncols[col] = diags1[diag1] = diags2[diag2] = true;\n// \u653e\u7f6e\u4e0b\u4e00\u884c\nbacktrack(row + 1, n, state, res, cols, diags1, diags2);\n// \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\nstate[row][col] = '#';\ncols[col] = diags1[diag1] = diags2[diag2] = false;\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\nconst state = Array.from({ length: n }, () => Array(n).fill('#'));\nconst cols = Array(n).fill(false); // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nconst diags1 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nconst diags2 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nconst res = [];\nbacktrack(0, n, state, res, cols, diags1, diags2);\nreturn res;\n}\n
    n_queens.ts
    /* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nfunction backtrack(\nrow: number,\nn: number,\nstate: string[][],\nres: string[][][],\ncols: boolean[],\ndiags1: boolean[],\ndiags2: boolean[]\n): void {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (row === n) {\nres.push(state.map((row) => row.slice()));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor (let col = 0; col < n; col++) {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\nconst diag1 = row - col + n - 1;\nconst diag2 = row + col;\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728 (\u5217 \u6216 \u4e3b\u5bf9\u89d2\u7ebf \u6216 \u526f\u5bf9\u89d2\u7ebf) \u5305\u542b\u7687\u540e\nif (!(cols[col] || diags1[diag1] || diags2[diag2])) {\n// \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\nstate[row][col] = 'Q';\ncols[col] = diags1[diag1] = diags2[diag2] = true;\n// \u653e\u7f6e\u4e0b\u4e00\u884c\nbacktrack(row + 1, n, state, res, cols, diags1, diags2);\n// \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\nstate[row][col] = '#';\ncols[col] = diags1[diag1] = diags2[diag2] = false;\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\nconst state = Array.from({ length: n }, () => Array(n).fill('#'));\nconst cols = Array(n).fill(false); // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nconst diags1 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nconst diags2 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nconst res: string[][][] = [];\nbacktrack(0, n, state, res, cols, diags1, diags2);\nreturn res;\n}\n
    n_queens.c
    [class]{}-[func]{backtrack}\n[class]{}-[func]{nQueens}\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,\nbool[] cols, bool[] diags1, bool[] diags2) {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (row == n) {\nList<List<string>> copyState = new List<List<string>>();\nforeach (List<string> sRow in state) {\ncopyState.Add(new List<string>(sRow));\n}\nres.Add(copyState);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor (int col = 0; col < n; col++) {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\nint diag1 = row - col + n - 1;\nint diag2 = row + col;\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728 (\u5217 \u6216 \u4e3b\u5bf9\u89d2\u7ebf \u6216 \u526f\u5bf9\u89d2\u7ebf) \u5305\u542b\u7687\u540e\nif (!(cols[col] || diags1[diag1] || diags2[diag2])) {\n// \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\nstate[row][col] = \"Q\";\ncols[col] = diags1[diag1] = diags2[diag2] = true;\n// \u653e\u7f6e\u4e0b\u4e00\u884c\nbacktrack(row + 1, n, state, res, cols, diags1, diags2);\n// \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\nstate[row][col] = \"#\";\ncols[col] = diags1[diag1] = diags2[diag2] = false;\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\nList<List<string>> state = new List<List<string>>();\nfor (int i = 0; i < n; i++) {\nList<string> row = new List<string>();\nfor (int j = 0; j < n; j++) {\nrow.Add(\"#\");\n}\nstate.Add(row);\n}\nbool[] cols = new bool[n]; // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nbool[] diags1 = new bool[2 * n - 1]; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nbool[] diags2 = new bool[2 * n - 1]; // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nList<List<List<string>>> res = new List<List<List<string>>>();\nbacktrack(0, n, state, res, cols, diags1, diags2);\nreturn 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\nif row == n {\nres.append(state)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor col in 0 ..< n {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\nlet diag1 = row - col + n - 1\nlet diag2 = row + col\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728 (\u5217 \u6216 \u4e3b\u5bf9\u89d2\u7ebf \u6216 \u526f\u5bf9\u89d2\u7ebf) \u5305\u542b\u7687\u540e\nif !(cols[col] || diags1[diag1] || diags2[diag2]) {\n// \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\nstate[row][col] = \"Q\"\ncols[col] = true\ndiags1[diag1] = true\ndiags2[diag2] = true\n// \u653e\u7f6e\u4e0b\u4e00\u884c\nbacktrack(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\nstate[row][col] = \"#\"\ncols[col] = false\ndiags1[diag1] = false\ndiags2[diag2] = false\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\nvar state = Array(repeating: Array(repeating: \"#\", count: n), count: n)\nvar cols = Array(repeating: false, count: n) // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nvar diags1 = Array(repeating: false, count: 2 * n - 1) // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nvar diags2 = Array(repeating: false, count: 2 * n - 1) // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nvar res: [[[String]]] = []\nbacktrack(row: 0, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)\nreturn res\n}\n
    n_queens.zig
    [class]{}-[func]{backtrack}\n[class]{}-[func]{nQueens}\n
    n_queens.dart
    [class]{}-[func]{backtrack}\n[class]{}-[func]{nQueens}\n
    "},{"location":"chapter_backtracking/n_queens_problem/#1231","title":"12.3.1. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"

    \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, n-1, \\cdots, 2, 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\u5730\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

    state \u4f7f\u7528 \\(O(n^2)\\) \u7a7a\u95f4\uff0ccols , diags1 , 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

    "},{"location":"chapter_backtracking/permutations_problem/","title":"12.2. \u00a0 \u5168\u6392\u5217\u95ee\u9898","text":"

    \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\u8fd9\u4e2a\u96c6\u5408\u4e2d\u5143\u7d20\u7684\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u3002

    \u5982\u4e0b\u8868\u6240\u793a\uff0c\u5217\u4e3e\u4e86\u51e0\u4e2a\u793a\u4f8b\u6570\u7ec4\u548c\u5176\u5bf9\u5e94\u7684\u6240\u6709\u6392\u5217\u3002

    \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/#1221","title":"12.2.1. \u00a0 \u65e0\u91cd\u590d\u7684\u60c5\u51b5","text":"

    Question

    \u8f93\u5165\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\u6570\u7ec4\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\\) \u3001\u518d\u9009\u62e9 \\(3\\) \u3001\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\u7b97\u6cd5\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\u6ce8\u610f\uff0c\u6bcf\u4e2a\u5143\u7d20\u53ea\u5141\u8bb8\u88ab\u9009\u62e9\u4e00\u6b21\uff0c\u56e0\u6b64\u5728\u904d\u5386\u9009\u62e9\u65f6\uff0c\u5e94\u5f53\u6392\u9664\u5df2\u7ecf\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u3002

    \u5982\u4e0b\u56fe\u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u641c\u7d22\u8fc7\u7a0b\u5c55\u5f00\u6210\u4e00\u4e2a\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

    Fig. \u5168\u6392\u5217\u7684\u9012\u5f52\u6811

    \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\u4ee3\u7801\u884c\u6570\uff0c\u6211\u4eec\u4e0d\u5355\u72ec\u5b9e\u73b0\u6846\u67b6\u4ee3\u7801\u4e2d\u7684\u5404\u4e2a\u51fd\u6570\uff0c\u800c\u662f\u5c06\u4ed6\u4eec\u5c55\u5f00\u5728 backtrack() \u51fd\u6570\u4e2d\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nif (state.size() == choices.length) {\nres.add(new ArrayList<Integer>(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i < choices.length; i++) {\nint 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\nif (!selected[i]) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nselected[i] = true;\nstate.add(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.remove(state.size() - 1);\n}\n}\n}\n/* \u5168\u6392\u5217 I */\nList<List<Integer>> permutationsI(int[] nums) {\nList<List<Integer>> res = new ArrayList<List<Integer>>();\nbacktrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);\nreturn res;\n}\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\nif (state.size() == choices.size()) {\nres.push_back(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i < choices.size(); i++) {\nint 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\nif (!selected[i]) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nselected[i] = true;\nstate.push_back(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.pop_back();\n}\n}\n}\n/* \u5168\u6392\u5217 I */\nvector<vector<int>> permutationsI(vector<int> nums) {\nvector<int> state;\nvector<bool> selected(nums.size(), false);\nvector<vector<int>> res;\nbacktrack(state, nums, selected, res);\nreturn res;\n}\n
    permutations_i.py
    def backtrack(\nstate: 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\nif len(state) == len(choices):\nres.append(list(state))\nreturn\n# \u904d\u5386\u6240\u6709\u9009\u62e9\nfor i, choice in enumerate(choices):\n# \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\nif not selected[i]:\n# \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nselected[i] = True\nstate.append(choice)\n# \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res)\n# \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = False\nstate.pop()\ndef permutations_i(nums: list[int]) -> list[list[int]]:\n\"\"\"\u5168\u6392\u5217 I\"\"\"\nres = []\nbacktrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\nreturn res\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\nif len(*state) == len(*choices) {\nnewState := append([]int{}, *state...)\n*res = append(*res, newState)\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor i := 0; i < len(*choices); i++ {\nchoice := (*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\nif !(*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\nbacktrackI(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/* \u5168\u6392\u5217 I */\nfunc permutationsI(nums []int) [][]int {\nres := make([][]int, 0)\nstate := make([]int, 0)\nselected := make([]bool, len(nums))\nbacktrackI(&state, &nums, &selected, &res)\nreturn 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\nif (state.length === choices.length) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nchoices.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\nif (!selected[i]) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nselected[i] = true;\nstate.push(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.pop();\n}\n});\n}\n/* \u5168\u6392\u5217 I */\nfunction permutationsI(nums) {\nconst res = [];\nbacktrack([], nums, Array(nums.length).fill(false), res);\nreturn res;\n}\n
    permutations_i.ts
    /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunction backtrack(\nstate: number[],\nchoices: number[],\nselected: boolean[],\nres: number[][]\n): void {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (state.length === choices.length) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nchoices.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\nif (!selected[i]) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nselected[i] = true;\nstate.push(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.pop();\n}\n});\n}\n/* \u5168\u6392\u5217 I */\nfunction permutationsI(nums: number[]): number[][] {\nconst res: number[][] = [];\nbacktrack([], nums, Array(nums.length).fill(false), res);\nreturn res;\n}\n
    permutations_i.c
    [class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsI}\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\nif (state.Count == choices.Length) {\nres.Add(new List<int>(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i < choices.Length; i++) {\nint 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\nif (!selected[i]) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nselected[i] = true;\nstate.Add(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.RemoveAt(state.Count - 1);\n}\n}\n}\n/* \u5168\u6392\u5217 I */\nList<List<int>> permutationsI(int[] nums) {\nList<List<int>> res = new List<List<int>>();\nbacktrack(new List<int>(), nums, new bool[nums.Length], res);\nreturn 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\nif state.count == choices.count {\nres.append(state)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (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\nif !selected[i] {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nselected[i] = true\nstate.append(choice)\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(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\nselected[i] = false\nstate.removeLast()\n}\n}\n}\n/* \u5168\u6392\u5217 I */\nfunc permutationsI(nums: [Int]) -> [[Int]] {\nvar state: [Int] = []\nvar selected = Array(repeating: false, count: nums.count)\nvar res: [[Int]] = []\nbacktrack(state: &state, choices: nums, selected: &selected, res: &res)\nreturn res\n}\n
    permutations_i.zig
    [class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsI}\n
    permutations_i.dart
    [class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsI}\n

    \u9700\u8981\u91cd\u70b9\u5173\u6ce8\u7684\u662f\uff0c\u6211\u4eec\u5f15\u5165\u4e86\u4e00\u4e2a\u5e03\u5c14\u578b\u6570\u7ec4 selected \uff0c\u5b83\u7684\u957f\u5ea6\u4e0e\u8f93\u5165\u6570\u7ec4\u957f\u5ea6\u76f8\u7b49\uff0c\u5176\u4e2d selected[i] \u8868\u793a choices[i] \u662f\u5426\u5df2\u88ab\u9009\u62e9\u3002\u6211\u4eec\u5229\u7528 selected \u907f\u514d\u67d0\u4e2a\u5143\u7d20\u88ab\u91cd\u590d\u9009\u62e9\uff0c\u4ece\u800c\u5b9e\u73b0\u526a\u679d\u3002

    \u5982\u4e0b\u56fe\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, 3 \u7684\u5206\u652f\u3002\u4ece\u672c\u8d28\u4e0a\u7406\u89e3\uff0c\u6b64\u526a\u679d\u64cd\u4f5c\u53ef\u5c06\u641c\u7d22\u7a7a\u95f4\u5927\u5c0f\u4ece \\(O(n^n)\\) \u964d\u4f4e\u81f3 \\(O(n!)\\) \u3002

    Fig. \u5168\u6392\u5217\u526a\u679d\u793a\u4f8b

    "},{"location":"chapter_backtracking/permutations_problem/#1222","title":"12.2.2. \u00a0 \u8003\u8651\u91cd\u590d\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\u7684\u5143\u7d20 \\(1\\) \uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u7b2c\u4e8c\u4e2a\u5143\u7d20\u8bb0\u4e3a \\(\\hat{1}\\) \u3002\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u4e0a\u8ff0\u65b9\u6cd5\u751f\u6210\u7684\u6392\u5217\u6709\u4e00\u534a\u90fd\u662f\u91cd\u590d\u7684\u3002

    Fig. \u91cd\u590d\u6392\u5217

    \u90a3\u4e48\uff0c\u5982\u4f55\u53bb\u9664\u91cd\u590d\u7684\u6392\u5217\u5462\uff1f\u6700\u76f4\u63a5\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868\uff0c\u76f4\u63a5\u5bf9\u6392\u5217\u7ed3\u679c\u8fdb\u884c\u53bb\u91cd\u3002\u7136\u800c\uff0c\u8fd9\u6837\u505a\u4e0d\u591f\u4f18\u96c5\uff0c\u56e0\u4e3a\u751f\u6210\u91cd\u590d\u6392\u5217\u7684\u641c\u7d22\u5206\u652f\u662f\u6ca1\u6709\u5fc5\u8981\u7684\uff0c\u5e94\u5f53\u88ab\u63d0\u524d\u8bc6\u522b\u5e76\u526a\u679d\uff0c\u8fd9\u6837\u53ef\u4ee5\u63d0\u5347\u7b97\u6cd5\u6548\u7387\u3002

    \u89c2\u5bdf\u53d1\u73b0\uff0c\u5728\u7b2c\u4e00\u8f6e\u4e2d\uff0c\u9009\u62e9 \\(1\\) \u6216\u9009\u62e9 \\(\\hat{1}\\) \u662f\u7b49\u4ef7\u7684\uff0c\u56e0\u4e3a\u5728\u8fd9\u4e24\u4e2a\u9009\u62e9\u4e4b\u4e0b\u751f\u6210\u7684\u6240\u6709\u6392\u5217\u90fd\u662f\u91cd\u590d\u7684\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5e94\u8be5\u628a \\(\\hat{1}\\) \u526a\u679d\u6389\u3002\u540c\u7406\uff0c\u5728\u7b2c\u4e00\u8f6e\u9009\u62e9 \\(2\\) \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\u9700\u8981\u5c06\u7b2c\u4e8c\u8f6e\u7684 \\(\\hat{1}\\) \u526a\u679d\u3002

    Fig. \u91cd\u590d\u6392\u5217\u526a\u679d

    \u672c\u8d28\u4e0a\u770b\uff0c\u6211\u4eec\u7684\u76ee\u6807\u662f\u5b9e\u73b0\u5728\u67d0\u4e00\u8f6e\u9009\u62e9\u4e2d\uff0c\u591a\u4e2a\u76f8\u7b49\u7684\u5143\u7d20\u4ec5\u88ab\u9009\u62e9\u4e00\u6b21\u3002\u56e0\u6b64\uff0c\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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nif (state.size() == choices.length) {\nres.add(new ArrayList<Integer>(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nSet<Integer> duplicated = new HashSet<Integer>();\nfor (int i = 0; i < choices.length; i++) {\nint 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\nif (!selected[i] && !duplicated.contains(choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nduplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\nselected[i] = true;\nstate.add(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.remove(state.size() - 1);\n}\n}\n}\n/* \u5168\u6392\u5217 II */\nList<List<Integer>> permutationsII(int[] nums) {\nList<List<Integer>> res = new ArrayList<List<Integer>>();\nbacktrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);\nreturn res;\n}\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\nif (state.size() == choices.size()) {\nres.push_back(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nunordered_set<int> duplicated;\nfor (int i = 0; i < choices.size(); i++) {\nint 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\nif (!selected[i] && duplicated.find(choice) == duplicated.end()) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nduplicated.emplace(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\nselected[i] = true;\nstate.push_back(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.pop_back();\n}\n}\n}\n/* \u5168\u6392\u5217 II */\nvector<vector<int>> permutationsII(vector<int> nums) {\nvector<int> state;\nvector<bool> selected(nums.size(), false);\nvector<vector<int>> res;\nbacktrack(state, nums, selected, res);\nreturn res;\n}\n
    permutations_ii.py
    def backtrack(\nstate: 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\nif len(state) == len(choices):\nres.append(list(state))\nreturn\n# \u904d\u5386\u6240\u6709\u9009\u62e9\nduplicated = set[int]()\nfor 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\nif not selected[i] and choice not in duplicated:\n# \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nduplicated.add(choice)  # \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\nselected[i] = True\nstate.append(choice)\n# \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res)\n# \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = False\nstate.pop()\ndef permutations_ii(nums: list[int]) -> list[list[int]]:\n\"\"\"\u5168\u6392\u5217 II\"\"\"\nres = []\nbacktrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\nreturn res\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\nif len(*state) == len(*choices) {\nnewState := append([]int{}, *state...)\n*res = append(*res, newState)\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nduplicated := make(map[int]struct{}, 0)\nfor i := 0; i < len(*choices); i++ {\nchoice := (*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\nif _, 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\nduplicated[choice] = struct{}{}\n(*selected)[i] = true\n*state = append(*state, choice)\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrackI(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/* \u5168\u6392\u5217 II */\nfunc permutationsII(nums []int) [][]int {\nres := make([][]int, 0)\nstate := make([]int, 0)\nselected := make([]bool, len(nums))\nbacktrackII(&state, &nums, &selected, &res)\nreturn 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\nif (state.length === choices.length) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nconst duplicated = new Set();\nchoices.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\nif (!selected[i] && !duplicated.has(choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nduplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\nselected[i] = true;\nstate.push(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.pop();\n}\n});\n}\n/* \u5168\u6392\u5217 II */\nfunction permutationsII(nums) {\nconst res = [];\nbacktrack([], nums, Array(nums.length).fill(false), res);\nreturn res;\n}\n
    permutations_ii.ts
    /* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunction backtrack(\nstate: number[],\nchoices: number[],\nselected: boolean[],\nres: number[][]\n): void {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (state.length === choices.length) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nconst duplicated = new Set();\nchoices.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\nif (!selected[i] && !duplicated.has(choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nduplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\nselected[i] = true;\nstate.push(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.pop();\n}\n});\n}\n/* \u5168\u6392\u5217 II */\nfunction permutationsII(nums: number[]): number[][] {\nconst res: number[][] = [];\nbacktrack([], nums, Array(nums.length).fill(false), res);\nreturn res;\n}\n
    permutations_ii.c
    [class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsII}\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\nif (state.Count == choices.Length) {\nres.Add(new List<int>(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nISet<int> duplicated = new HashSet<int>();\nfor (int i = 0; i < choices.Length; i++) {\nint 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\nif (!selected[i] && !duplicated.Contains(choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nduplicated.Add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\nselected[i] = true;\nstate.Add(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.RemoveAt(state.Count - 1);\n}\n}\n}\n/* \u5168\u6392\u5217 II */\nList<List<int>> permutationsII(int[] nums) {\nList<List<int>> res = new List<List<int>>();\nbacktrack(new List<int>(), nums, new bool[nums.Length], res);\nreturn 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\nif state.count == choices.count {\nres.append(state)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nvar duplicated: Set<Int> = []\nfor (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\nif !selected[i], !duplicated.contains(choice) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nduplicated.insert(choice) // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\nselected[i] = true\nstate.append(choice)\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(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\nselected[i] = false\nstate.removeLast()\n}\n}\n}\n/* \u5168\u6392\u5217 II */\nfunc permutationsII(nums: [Int]) -> [[Int]] {\nvar state: [Int] = []\nvar selected = Array(repeating: false, count: nums.count)\nvar res: [[Int]] = []\nbacktrack(state: &state, choices: nums, selected: &selected, res: &res)\nreturn res\n}\n
    permutations_ii.zig
    [class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsII}\n
    permutations_ii.dart
    [class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsII}\n

    \u6ce8\u610f\uff0c\u867d\u7136 selected \u548c duplicated \u90fd\u8d77\u5230\u526a\u679d\u7684\u4f5c\u7528\uff0c\u4f46\u4ed6\u4eec\u526a\u6389\u7684\u662f\u4e0d\u540c\u7684\u5206\u652f\uff1a

    • \u526a\u679d\u6761\u4ef6\u4e00\uff1a\u6574\u4e2a\u641c\u7d22\u8fc7\u7a0b\u4e2d\u53ea\u6709\u4e00\u4e2a selected \u3002\u5b83\u8bb0\u5f55\u7684\u662f\u5f53\u524d\u72b6\u6001\u4e2d\u5305\u542b\u54ea\u4e9b\u5143\u7d20\uff0c\u4f5c\u7528\u662f\u907f\u514d\u67d0\u4e2a\u5143\u7d20\u5728 state \u4e2d\u91cd\u590d\u51fa\u73b0\u3002
    • \u526a\u679d\u6761\u4ef6\u4e8c\uff1a\u6bcf\u8f6e\u9009\u62e9\uff08\u5373\u6bcf\u4e2a\u5f00\u542f\u7684 backtrack \u51fd\u6570\uff09\u90fd\u5305\u542b\u4e00\u4e2a duplicated \u3002\u5b83\u8bb0\u5f55\u7684\u662f\u5728\u904d\u5386\u4e2d\u54ea\u4e9b\u5143\u7d20\u5df2\u88ab\u9009\u62e9\u8fc7\uff0c\u4f5c\u7528\u662f\u4fdd\u8bc1\u76f8\u7b49\u5143\u7d20\u53ea\u88ab\u9009\u62e9\u4e00\u6b21\uff0c\u4ee5\u907f\u514d\u4ea7\u751f\u91cd\u590d\u7684\u641c\u7d22\u5206\u652f\u3002

    \u4e0b\u56fe\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

    Fig. \u4e24\u79cd\u526a\u679d\u6761\u4ef6\u7684\u4f5c\u7528\u8303\u56f4

    "},{"location":"chapter_backtracking/permutations_problem/#1223","title":"12.2.3. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"

    \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\uff0c\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\uff0c\u5168\u6392\u5217 I \u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u5168\u6392\u5217 II \u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002

    "},{"location":"chapter_computational_complexity/","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":""},{"location":"chapter_computational_complexity/performance_evaluation/","title":"2.1. \u00a0 \u7b97\u6cd5\u6548\u7387\u8bc4\u4f30","text":""},{"location":"chapter_computational_complexity/performance_evaluation/#211","title":"2.1.1. \u00a0 \u7b97\u6cd5\u8bc4\u4ef7\u7ef4\u5ea6","text":"

    \u4ece\u603b\u4f53\u4e0a\u770b\uff0c\u7b97\u6cd5\u8bbe\u8ba1\u8ffd\u6c42\u4ee5\u4e0b\u4e24\u4e2a\u5c42\u9762\u7684\u76ee\u6807\uff1a

    1. \u627e\u5230\u95ee\u9898\u89e3\u6cd5\u3002\u7b97\u6cd5\u9700\u8981\u5728\u89c4\u5b9a\u7684\u8f93\u5165\u8303\u56f4\u5185\uff0c\u53ef\u9760\u5730\u6c42\u5f97\u95ee\u9898\u7684\u6b63\u786e\u89e3\u3002
    2. \u5bfb\u6c42\u6700\u4f18\u89e3\u6cd5\u3002\u540c\u4e00\u4e2a\u95ee\u9898\u53ef\u80fd\u5b58\u5728\u591a\u79cd\u89e3\u6cd5\uff0c\u6211\u4eec\u5e0c\u671b\u627e\u5230\u5c3d\u53ef\u80fd\u9ad8\u6548\u7684\u7b97\u6cd5\u3002

    \u56e0\u6b64\uff0c\u5728\u80fd\u591f\u89e3\u51b3\u95ee\u9898\u7684\u524d\u63d0\u4e0b\uff0c\u7b97\u6cd5\u6548\u7387\u6210\u4e3a\u4e3b\u8981\u7684\u8bc4\u4ef7\u7ef4\u5ea6\uff0c\u4e3b\u8981\u5305\u62ec\uff1a

    • \u65f6\u95f4\u6548\u7387\uff0c\u5373\u7b97\u6cd5\u8fd0\u884c\u901f\u5ea6\u7684\u5feb\u6162\u3002
    • \u7a7a\u95f4\u6548\u7387\uff0c\u5373\u7b97\u6cd5\u5360\u7528\u5185\u5b58\u7a7a\u95f4\u7684\u5927\u5c0f\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\u638c\u63e1\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\u7684\u65b9\u6cd5\u5219\u81f3\u5173\u91cd\u8981\uff0c\u56e0\u4e3a\u53ea\u6709\u4e86\u89e3\u8bc4\u4ef7\u6807\u51c6\uff0c\u6211\u4eec\u624d\u80fd\u8fdb\u884c\u7b97\u6cd5\u4e4b\u95f4\u7684\u5bf9\u6bd4\u5206\u6790\uff0c\u4ece\u800c\u6307\u5bfc\u7b97\u6cd5\u8bbe\u8ba1\u4e0e\u4f18\u5316\u8fc7\u7a0b\u3002

    "},{"location":"chapter_computational_complexity/performance_evaluation/#212","title":"2.1.2. \u00a0 \u6548\u7387\u8bc4\u4f30\u65b9\u6cd5","text":""},{"location":"chapter_computational_complexity/performance_evaluation/#_1","title":"\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\u6211\u4eec\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u5c31\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\u5c40\u9650\u6027\u3002

    \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\u8868\u73b0\u3002\u4f8b\u5982\uff0c\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\u6211\u4eec\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\u800c\u8fd9\u662f\u4e0d\u73b0\u5b9e\u7684\u3002

    \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\u8f93\u5165\u6570\u636e\u91cf\u8f83\u5c0f\u65f6\uff0c\u7b97\u6cd5 A \u7684\u8fd0\u884c\u65f6\u95f4\u53ef\u80fd\u77ed\u4e8e\u7b97\u6cd5 B\uff1b\u800c\u8f93\u5165\u6570\u636e\u91cf\u8f83\u5927\u65f6\uff0c\u6d4b\u8bd5\u7ed3\u679c\u53ef\u80fd\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\u8fd9\u6837\u9700\u8981\u5360\u7528\u5927\u91cf\u7684\u8ba1\u7b97\u8d44\u6e90\u3002

    "},{"location":"chapter_computational_complexity/performance_evaluation/#_2","title":"\u7406\u8bba\u4f30\u7b97","text":"

    \u7531\u4e8e\u5b9e\u9645\u6d4b\u8bd5\u5177\u6709\u8f83\u5927\u7684\u5c40\u9650\u6027\uff0c\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\u590d\u6742\u5ea6\u5206\u6790 Complexity Analysis\u300d\u6216\u300c\u6e10\u8fd1\u590d\u6742\u5ea6\u5206\u6790 Asymptotic Complexity Analysis\u300d\u3002

    \u590d\u6742\u5ea6\u5206\u6790\u8bc4\u4f30\u7684\u662f\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u968f\u7740\u8f93\u5165\u6570\u636e\u91cf\u589e\u591a\u65f6\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\uff1a

    • \u201c\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u201d\u53ef\u5206\u4e3a\u201c\u8fd0\u884c\u65f6\u95f4\u201d\u548c\u201c\u5360\u7528\u7a7a\u95f4\u201d\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u5c06\u590d\u6742\u5ea6\u5206\u4e3a\u300c\u65f6\u95f4\u590d\u6742\u5ea6 Time Complexity\u300d\u548c\u300c\u7a7a\u95f4\u590d\u6742\u5ea6 Space Complexity\u300d\uff1b
    • \u201c\u968f\u7740\u8f93\u5165\u6570\u636e\u91cf\u589e\u591a\u65f6\u201d\u8868\u793a\u590d\u6742\u5ea6\u4e0e\u8f93\u5165\u6570\u636e\u91cf\u6709\u5173\uff0c\u53cd\u6620\u4e86\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u4e0e\u8f93\u5165\u6570\u636e\u91cf\u4e4b\u95f4\u7684\u5173\u7cfb\uff1b
    • \u201c\u589e\u957f\u8d8b\u52bf\u201d\u8868\u793a\u590d\u6742\u5ea6\u5206\u6790\u5173\u6ce8\u7684\u662f\u7b97\u6cd5\u65f6\u95f4\u4e0e\u7a7a\u95f4\u7684\u589e\u957f\u8d8b\u52bf\uff0c\u800c\u975e\u5177\u4f53\u7684\u8fd0\u884c\u65f6\u95f4\u6216\u5360\u7528\u7a7a\u95f4\uff1b

    \u590d\u6742\u5ea6\u5206\u6790\u514b\u670d\u4e86\u5b9e\u9645\u6d4b\u8bd5\u65b9\u6cd5\u7684\u5f0a\u7aef\u3002\u9996\u5148\uff0c\u5b83\u72ec\u7acb\u4e8e\u6d4b\u8bd5\u73af\u5883\uff0c\u56e0\u6b64\u5206\u6790\u7ed3\u679c\u9002\u7528\u4e8e\u6240\u6709\u8fd0\u884c\u5e73\u53f0\u3002\u5176\u6b21\uff0c\u5b83\u53ef\u4ee5\u4f53\u73b0\u4e0d\u540c\u6570\u636e\u91cf\u4e0b\u7684\u7b97\u6cd5\u6548\u7387\uff0c\u5c24\u5176\u662f\u5728\u5927\u6570\u636e\u91cf\u4e0b\u7684\u7b97\u6cd5\u6027\u80fd\u3002

    \u5982\u679c\u4f60\u5bf9\u590d\u6742\u5ea6\u5206\u6790\u7684\u6982\u5ff5\u4ecd\u611f\u5230\u56f0\u60d1\uff0c\u65e0\u9700\u62c5\u5fc3\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7ae0\u8282\u8be6\u7ec6\u4ecb\u7ecd\u3002

    "},{"location":"chapter_computational_complexity/performance_evaluation/#213","title":"2.1.3. \u00a0 \u590d\u6742\u5ea6\u5206\u6790\u91cd\u8981\u6027","text":"

    \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\u544a\u8bc9\u6211\u4eec\u6267\u884c\u67d0\u4e2a\u7b97\u6cd5\u6240\u9700\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u8d44\u6e90\uff0c\u5e76\u4f7f\u6211\u4eec\u80fd\u591f\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\u7b2c\u4e00\u7ae0\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\u6211\u4eec\u96be\u4ee5\u907f\u514d\u8981\u5206\u6790\u5176\u8fd0\u884c\u901f\u5ea6\u548c\u7a7a\u95f4\u4f7f\u7528\u60c5\u51b5\u3002\u56e0\u6b64\uff0c\u5728\u6df1\u5165\u5b66\u4e60\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u4e4b\u524d\uff0c\u5efa\u8bae\u8bfb\u8005\u5148\u5bf9\u590d\u6742\u5ea6\u5efa\u7acb\u521d\u6b65\u7684\u4e86\u89e3\uff0c\u5e76\u80fd\u591f\u5b8c\u6210\u7b80\u5355\u6848\u4f8b\u7684\u590d\u6742\u5ea6\u5206\u6790\u3002

    "},{"location":"chapter_computational_complexity/space_complexity/","title":"2.3. \u00a0 \u7a7a\u95f4\u590d\u6742\u5ea6","text":"

    \u300c\u7a7a\u95f4\u590d\u6742\u5ea6 Space Complexity\u300d\u7528\u4e8e\u8861\u91cf\u7b97\u6cd5\u4f7f\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\u3002

    "},{"location":"chapter_computational_complexity/space_complexity/#231","title":"2.3.1. \u00a0 \u7b97\u6cd5\u76f8\u5173\u7a7a\u95f4","text":"

    \u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\uff1a

    • \u300c\u8f93\u5165\u7a7a\u95f4\u300d\u7528\u4e8e\u5b58\u50a8\u7b97\u6cd5\u7684\u8f93\u5165\u6570\u636e\uff1b
    • \u300c\u6682\u5b58\u7a7a\u95f4\u300d\u7528\u4e8e\u5b58\u50a8\u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u53d8\u91cf\u3001\u5bf9\u8c61\u3001\u51fd\u6570\u4e0a\u4e0b\u6587\u7b49\u6570\u636e\uff1b
    • \u300c\u8f93\u51fa\u7a7a\u95f4\u300d\u7528\u4e8e\u5b58\u50a8\u7b97\u6cd5\u7684\u8f93\u51fa\u6570\u636e\uff1b

    \u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u7edf\u8ba1\u8303\u56f4\u662f\u300c\u6682\u5b58\u7a7a\u95f4\u300d+\u300c\u8f93\u51fa\u7a7a\u95f4\u300d\u3002

    \u6682\u5b58\u7a7a\u95f4\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5212\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\uff1a

    • \u300c\u6682\u5b58\u6570\u636e\u300d\u7528\u4e8e\u4fdd\u5b58\u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u5404\u79cd\u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u7b49\u3002
    • \u300c\u6808\u5e27\u7a7a\u95f4\u300d\u7528\u4e8e\u4fdd\u5b58\u8c03\u7528\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u6570\u636e\u3002\u7cfb\u7edf\u5728\u6bcf\u6b21\u8c03\u7528\u51fd\u6570\u65f6\u90fd\u4f1a\u5728\u6808\u9876\u90e8\u521b\u5efa\u4e00\u4e2a\u6808\u5e27\uff0c\u51fd\u6570\u8fd4\u56de\u540e\uff0c\u6808\u5e27\u7a7a\u95f4\u4f1a\u88ab\u91ca\u653e\u3002
    • \u300c\u6307\u4ee4\u7a7a\u95f4\u300d\u7528\u4e8e\u4fdd\u5b58\u7f16\u8bd1\u540e\u7684\u7a0b\u5e8f\u6307\u4ee4\uff0c\u5728\u5b9e\u9645\u7edf\u8ba1\u4e2d\u901a\u5e38\u5ffd\u7565\u4e0d\u8ba1\u3002

    \u56e0\u6b64\uff0c\u5728\u5206\u6790\u4e00\u6bb5\u7a0b\u5e8f\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u65f6\uff0c\u6211\u4eec\u4e00\u822c\u7edf\u8ba1 \u6682\u5b58\u6570\u636e\u3001\u8f93\u51fa\u6570\u636e\u3001\u6808\u5e27\u7a7a\u95f4 \u4e09\u90e8\u5206\u3002

    Fig. \u7b97\u6cd5\u4f7f\u7528\u7684\u76f8\u5173\u7a7a\u95f4

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    /* \u7c7b */\nclass Node {\nint val;\nNode next;\nNode(int x) { val = x; }\n}\n/* \u51fd\u6570 */\nint function() {\n// do something...\nreturn 0;\n}\nint algorithm(int n) {        // \u8f93\u5165\u6570\u636e\nfinal int a = 0;          // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nint b = 0;                // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nNode node = new Node(0);  // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nint c = function();       // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c;         // \u8f93\u51fa\u6570\u636e\n}\n
    /* \u7ed3\u6784\u4f53 */\nstruct Node {\nint val;\nNode *next;\nNode(int x) : val(x), next(nullptr) {}\n};\n/* \u51fd\u6570 */\nint func() {\n// do something...\nreturn 0;\n}\nint algorithm(int n) {        // \u8f93\u5165\u6570\u636e\nconst int a = 0;          // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nint b = 0;                // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nNode* node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nint c = func();           // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c;         // \u8f93\u51fa\u6570\u636e\n}\n
    class Node:\n\"\"\"\u7c7b\"\"\"\ndef __init__(self, x: int):\nself.val: int = x                 # \u8282\u70b9\u503c\nself.next: Optional[Node] = None  # \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\ndef function() -> int:\n\"\"\"\u51fd\u6570\"\"\"\n# do something...\nreturn 0\ndef algorithm(n) -> int:  # \u8f93\u5165\u6570\u636e\nA = 0                 # \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff0c\u4e00\u822c\u7528\u5927\u5199\u5b57\u6bcd\u8868\u793a\uff09\nb = 0                 # \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nnode = Node(0)        # \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nc = function()        # \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn A + b + c      # \u8f93\u51fa\u6570\u636e\n
    /* \u7ed3\u6784\u4f53 */\ntype node struct {\nval  int\nnext *node\n}\n/* \u521b\u5efa node \u7ed3\u6784\u4f53  */\nfunc newNode(val int) *node {\nreturn &node{val: val}\n}\n/* \u51fd\u6570 */\nfunc function() int {\n// do something...\nreturn 0\n}\nfunc algorithm(n int) int { // \u8f93\u5165\u6570\u636e\nconst a = 0             // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nb := 0                  // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nnewNode(0)              // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nc := function()         // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c        // \u8f93\u51fa\u6570\u636e\n}\n
    /* \u7c7b */\nclass Node {\nval;\nnext;\nconstructor(val) {\nthis.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\nthis.next = null;                       // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n}\n}\n/* \u51fd\u6570 */\nfunction constFunc() {\n// do something\nreturn 0;\n}\nfunction algorithm(n) {       // \u8f93\u5165\u6570\u636e\nconst a = 0;              // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nlet b = 0;                // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nconst node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nconst c = constFunc();    // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c;         // \u8f93\u51fa\u6570\u636e\n}\n
    /* \u7c7b */\nclass Node {\nval: number;\nnext: Node | null;\nconstructor(val?: number) {\nthis.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\nthis.next = null;                       // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n}\n}\n/* \u51fd\u6570 */\nfunction constFunc(): number {\n// do something\nreturn 0;\n}\nfunction algorithm(n: number): number { // \u8f93\u5165\u6570\u636e\nconst a = 0;                        // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nlet b = 0;                          // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nconst node = new Node(0);           // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nconst c = constFunc();              // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c;                   // \u8f93\u51fa\u6570\u636e\n}\n
    /* \u51fd\u6570 */\nint func() {\n// do something...\nreturn 0;\n}\nint algorithm(int n) { // \u8f93\u5165\u6570\u636e\nconst int a = 0;   // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nint b = 0;         // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nint c = func();    // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c;  // \u8f93\u51fa\u6570\u636e\n}\n
    /* \u7c7b */\nclass Node\n{\nint val;\nNode next;\nNode(int x) { val = x; }\n}\n/* \u51fd\u6570 */\nint function()\n{\n// do something...\nreturn 0;\n}\nint algorithm(int n)          // \u8f93\u5165\u6570\u636e\n{\nconst int a = 0;          // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nint b = 0;                // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nNode node = new Node(0);  // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nint c = function();       // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c;         // \u8f93\u51fa\u6570\u636e\n}\n
    /* \u7c7b */\nclass Node {\nvar val: Int\nvar next: Node?\ninit(x: Int) {\nval = x\n}\n}\n/* \u51fd\u6570 */\nfunc function() -> Int {\n// do something...\nreturn 0\n}\nfunc algorithm(n: Int) -> Int { // \u8f93\u5165\u6570\u636e\nlet a = 0             // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nvar b = 0             // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nlet node = Node(x: 0) // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nlet c = function()    // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c      // \u8f93\u51fa\u6570\u636e\n}\n
    \n
    /* \u7c7b */\nclass Node {\nint val;\nNode next;\nNode(this.val, [this.next]);\n}\n/* \u51fd\u6570 */\nint function() {\n// do something...\nreturn 0;\n}\nint algorithm(int n) {  // \u8f93\u5165\u6570\u636e\nconst int a = 0;      // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nint b = 0;            // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nNode node = Node(0);  // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nint c = function();   // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c;     // \u8f93\u51fa\u6570\u636e\n}\n
    "},{"location":"chapter_computational_complexity/space_complexity/#232","title":"2.3.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\u662f\u5c06\u7edf\u8ba1\u5bf9\u8c61\u4ece\u201c\u8ba1\u7b97\u64cd\u4f5c\u6570\u91cf\u201d\u8f6c\u4e3a\u201c\u4f7f\u7528\u7a7a\u95f4\u5927\u5c0f\u201d\u3002\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u540c\u7684\u662f\uff0c\u6211\u4eec\u901a\u5e38\u53ea\u5173\u6ce8\u300c\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u300d\uff0c\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

    \u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e2d\u7684\u201c\u6700\u5dee\u201d\u6709\u4e24\u5c42\u542b\u4e49\uff0c\u5206\u522b\u662f\u8f93\u5165\u6570\u636e\u7684\u6700\u5dee\u5206\u5e03\u548c\u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u6700\u5dee\u65f6\u95f4\u70b9\u3002

    • \u4ee5\u6700\u5dee\u8f93\u5165\u6570\u636e\u4e3a\u51c6\u3002\u5f53 \\(n < 10\\) \u65f6\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff1b\u4f46\u5f53 \\(n > 10\\) \u65f6\uff0c\u521d\u59cb\u5316\u7684\u6570\u7ec4 nums \u5360\u7528 \\(O(n)\\) \u7a7a\u95f4\uff1b\u56e0\u6b64\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff1b
    • \u4ee5\u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u5cf0\u503c\u5185\u5b58\u4e3a\u51c6\u3002\u4f8b\u5982\uff0c\u7a0b\u5e8f\u5728\u6267\u884c\u6700\u540e\u4e00\u884c\u4e4b\u524d\uff0c\u5360\u7528 \\(O(1)\\) \u7a7a\u95f4\uff1b\u5f53\u521d\u59cb\u5316\u6570\u7ec4 nums \u65f6\uff0c\u7a0b\u5e8f\u5360\u7528 \\(O(n)\\) \u7a7a\u95f4\uff1b\u56e0\u6b64\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff1b
    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    void algorithm(int n) {\nint a = 0;                   // O(1)\nint[] b = new int[10000];    // O(1)\nif (n > 10)\nint[] nums = new int[n]; // O(n)\n}\n
    void algorithm(int n) {\nint a = 0;               // O(1)\nvector<int> b(10000);    // O(1)\nif (n > 10)\nvector<int> nums(n); // O(n)\n}\n
    def algorithm(n: int) -> None:\na = 0                     # O(1)\nb = [0] * 10000     # O(1)\nif n > 10:\nnums = [0] * n  # O(n)\n
    func algorithm(n int) {\na := 0                      // O(1)\nb := make([]int, 10000)     // O(1)\nvar nums []int\nif n > 10 {\nnums := make([]int, n)  // O(n)\n}\nfmt.Println(a, b, nums)\n}\n
    function algorithm(n) {\nconst a = 0;                   // O(1)\nconst b = new Array(10000);    // O(1)\nif (n > 10) {\nconst nums = new Array(n); // O(n)\n}\n}\n
    function algorithm(n: number): void {\nconst a = 0;                   // O(1)\nconst b = new Array(10000);    // O(1)\nif (n > 10) {\nconst nums = new Array(n); // O(n)\n}\n}\n
    void algorithm(int n) {\nint a = 0;               // O(1)\nint b[10000];            // O(1)\nif (n > 10)\nvector<int> nums(n); // O(n)\n}\n
    void algorithm(int n)\n{\nint a = 0;                   // O(1)\nint[] b = new int[10000];    // O(1)\nif (n > 10)\n{\nint[] nums = new int[n]; // O(n)\n}\n}\n
    func algorithm(n: Int) {\nlet a = 0 // O(1)\nlet b = Array(repeating: 0, count: 10000) // O(1)\nif n > 10 {\nlet nums = Array(repeating: 0, count: n) // O(n)\n}\n}\n
    \n
    void algorithm(int n) {\nint a = 0;                            // O(1)\nList<int> b = List.filled(10000, 0);  // O(1)\nif (n > 10) {\nList<int> nums = List.filled(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\u4f8b\u5982\uff0c\u51fd\u6570 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)\\) \u3002\u800c\u9012\u5f52\u51fd\u6570 recur() \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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    int function() {\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\nfor (int i = 0; i < n; i++) {\nfunction();\n}\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\nif (n == 1) return;\nreturn recur(n - 1);\n}\n
    int func() {\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\nfor (int i = 0; i < n; i++) {\nfunc();\n}\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\nif (n == 1) return;\nreturn recur(n - 1);\n}\n
    def function() -> int:\n# do something\nreturn 0\ndef loop(n: int) -> None:\n\"\"\"\u5faa\u73af O(1)\"\"\"\nfor _ in range(n):\nfunction()\ndef recur(n: int) -> int:\n\"\"\"\u9012\u5f52 O(n)\"\"\"\nif n == 1: return\nreturn recur(n - 1)\n
    func function() int {\n// do something\nreturn 0\n}\n/* \u5faa\u73af O(1) */\nfunc loop(n int) {\nfor i := 0; i < n; i++ {\nfunction()\n}\n}\n/* \u9012\u5f52 O(n) */\nfunc recur(n int) {\nif n == 1 {\nreturn\n}\nrecur(n - 1)\n}\n
    function constFunc() {\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nfunction loop(n) {\nfor (let i = 0; i < n; i++) {\nconstFunc();\n}\n}\n/* \u9012\u5f52 O(n) */\nfunction recur(n) {\nif (n === 1) return;\nreturn recur(n - 1);\n}\n
    function constFunc(): number {\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nfunction loop(n: number): void {\nfor (let i = 0; i < n; i++) {\nconstFunc();\n}\n}\n/* \u9012\u5f52 O(n) */\nfunction recur(n: number): void {\nif (n === 1) return;\nreturn recur(n - 1);\n}\n
    int func() {\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\nfor (int i = 0; i < n; i++) {\nfunc();\n}\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\nif (n == 1) return;\nreturn recur(n - 1);\n}\n
    int function()\n{\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n)\n{\nfor (int i = 0; i < n; i++)\n{\nfunction();\n}\n}\n/* \u9012\u5f52 O(n) */\nint recur(int n)\n{\nif (n == 1) return 1;\nreturn recur(n - 1);\n}\n
    @discardableResult\nfunc function() -> Int {\n// do something\nreturn 0\n}\n/* \u5faa\u73af O(1) */\nfunc loop(n: Int) {\nfor _ in 0 ..< n {\nfunction()\n}\n}\n/* \u9012\u5f52 O(n) */\nfunc recur(n: Int) {\nif n == 1 {\nreturn\n}\nrecur(n: n - 1)\n}\n
    \n
    int function() {\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\nfor (int i = 0; i < n; i++) {\nfunction();\n}\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\nif (n == 1) return;\nreturn recur(n - 1);\n}\n
    "},{"location":"chapter_computational_complexity/space_complexity/#233","title":"2.3.3. \u00a0 \u5e38\u89c1\u7c7b\u578b","text":"

    \u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u5e38\u89c1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u7c7b\u578b\u6709\uff08\u4ece\u4f4e\u5230\u9ad8\u6392\u5217\uff09

    \\[ \\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} \\]

    Fig. \u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u5e38\u89c1\u7c7b\u578b

    Tip

    \u90e8\u5206\u793a\u4f8b\u4ee3\u7801\u9700\u8981\u4e00\u4e9b\u524d\u7f6e\u77e5\u8bc6\uff0c\u5305\u62ec\u6570\u7ec4\u3001\u94fe\u8868\u3001\u4e8c\u53c9\u6811\u3001\u9012\u5f52\u7b97\u6cd5\u7b49\u3002\u5982\u679c\u9047\u5230\u770b\u4e0d\u61c2\u7684\u5730\u65b9\u65e0\u9700\u62c5\u5fc3\uff0c\u53ef\u4ee5\u5728\u5b66\u4e60\u5b8c\u540e\u9762\u7ae0\u8282\u540e\u518d\u6765\u590d\u4e60\uff0c\u73b0\u9636\u6bb5\u6211\u4eec\u5148\u4e13\u6ce8\u4e8e\u7406\u89e3\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u542b\u4e49\u548c\u63a8\u7b97\u65b9\u6cd5\u3002

    "},{"location":"chapter_computational_complexity/space_complexity/#o1","title":"\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\u5373\u4e0d\u4f1a\u7d2f\u79ef\u5360\u7528\u7a7a\u95f4\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart space_complexity.java
    /* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nfinal int a = 0;\nint b = 0;\nint[] nums = new int[10000];\nListNode node = new ListNode(0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i < n; i++) {\nint c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i < n; i++) {\nfunction();\n}\n}\n
    space_complexity.cpp
    /* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst int a = 0;\nint b = 0;\nvector<int> nums(10000);\nListNode node(0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i < n; i++) {\nint c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i < n; i++) {\nfunc();\n}\n}\n
    space_complexity.py
    def constant(n: int) -> None:\n\"\"\"\u5e38\u6570\u9636\"\"\"\n# \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\na = 0\nnums = [0] * 10000\nnode = ListNode(0)\n# \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor _ in range(n):\nc = 0\n# \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor _ in range(n):\nfunction()\n
    space_complexity.go
    /* \u5e38\u6570\u9636 */\nfunc spaceConstant(n int) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst a = 0\nb := 0\nnums := make([]int, 10000)\nListNode := newNode(0)\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nvar c int\nfor i := 0; i < n; i++ {\nc = 0\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor i := 0; i < n; i++ {\nfunction()\n}\nfmt.Println(a, b, nums, c, ListNode)\n}\n
    space_complexity.js
    /* \u5e38\u6570\u9636 */\nfunction constant(n) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst a = 0;\nconst b = 0;\nconst nums = new Array(10000);\nconst node = new ListNode(0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (let i = 0; i < n; i++) {\nconst c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (let i = 0; i < n; i++) {\nconstFunc();\n}\n}\n
    space_complexity.ts
    /* \u5e38\u6570\u9636 */\nfunction constant(n: number): void {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst a = 0;\nconst b = 0;\nconst nums = new Array(10000);\nconst node = new ListNode(0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (let i = 0; i < n; i++) {\nconst c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (let i = 0; i < n; i++) {\nconstFunc();\n}\n}\n
    space_complexity.c
    /* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst int a = 0;\nint b = 0;\nint nums[1000];\nListNode *node = newListNode(0);\nfree(node);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i < n; i++) {\nint c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i < n; i++) {\nfunc();\n}\n}\n
    space_complexity.cs
    /* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nint a = 0;\nint b = 0;\nint[] nums = new int[10000];\nListNode node = new ListNode(0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i < n; i++) {\nint c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i < n; i++) {\nfunction();\n}\n}\n
    space_complexity.swift
    /* \u5e38\u6570\u9636 */\nfunc constant(n: Int) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nlet a = 0\nvar b = 0\nlet nums = Array(repeating: 0, count: 10000)\nlet node = ListNode(x: 0)\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor _ in 0 ..< n {\nlet c = 0\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor _ in 0 ..< n {\nfunction()\n}\n}\n
    space_complexity.zig
    // \u5e38\u6570\u9636\nfn constant(n: i32) void {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst a: i32 = 0;\nvar b: i32 = 0;\nvar nums = [_]i32{0}**10000;\nvar node = inc.ListNode(i32){.val = 0};\nvar i: i32 = 0;\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nwhile (i < n) : (i += 1) {\nvar c: i32 = 0;\n_ = c;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\ni = 0;\nwhile (i < n) : (i += 1) {\n_ = function();\n}\n_ = a;\n_ = b;\n_ = nums;\n_ = node;\n}\n
    space_complexity.dart
    /* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nfinal int a = 0;\nint b = 0;\nList<int> nums = List.filled(10000, 0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (var i = 0; i < n; i++) {\nint c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (var i = 0; i < n; i++) {\nfunction();\n}\n}\n
    "},{"location":"chapter_computational_complexity/space_complexity/#on","title":"\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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart space_complexity.java
    /* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\nint[] nums = new int[n];\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nList<ListNode> nodes = new ArrayList<>();\nfor (int i = 0; i < n; i++) {\nnodes.add(new ListNode(i));\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nMap<Integer, String> map = new HashMap<>();\nfor (int i = 0; i < n; i++) {\nmap.put(i, String.valueOf(i));\n}\n}\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\nvector<int> nums(n);\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nvector<ListNode> nodes;\nfor (int i = 0; i < n; i++) {\nnodes.push_back(ListNode(i));\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nunordered_map<int, string> map;\nfor (int i = 0; i < n; i++) {\nmap[i] = to_string(i);\n}\n}\n
    space_complexity.py
    def linear(n: int) -> None:\n\"\"\"\u7ebf\u6027\u9636\"\"\"\n# \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nnums = [0] * n\n# \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nmapp = dict[int, str]()\nfor i in range(n):\nmapp[i] = str(i)\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\nvar nodes []*node\nfor i := 0; i < n; i++ {\nnodes = append(nodes, newNode(i))\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nm := make(map[int]string, n)\nfor i := 0; i < n; i++ {\nm[i] = strconv.Itoa(i)\n}\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\nconst nums = new Array(n);\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nconst nodes = [];\nfor (let i = 0; i < n; i++) {\nnodes.push(new ListNode(i));\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nconst map = new Map();\nfor (let i = 0; i < n; i++) {\nmap.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\nconst nums = new Array(n);\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nconst nodes: ListNode[] = [];\nfor (let i = 0; i < n; i++) {\nnodes.push(new ListNode(i));\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nconst map = new Map();\nfor (let i = 0; i < n; i++) {\nmap.set(i, i.toString());\n}\n}\n
    space_complexity.c
    /* \u54c8\u5e0c\u8868 */\nstruct hashTable {\nint key;\nint val;\nUT_hash_handle hh; // \u57fa\u4e8e uthash.h \u5b9e\u73b0\n};\ntypedef struct hashTable hashTable;\n/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\nint *nums = malloc(sizeof(int) * n);\nfree(nums);\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nListNode **nodes = malloc(sizeof(ListNode *) * n);\nfor (int i = 0; i < n; i++) {\nnodes[i] = newListNode(i);\n}\n// \u5185\u5b58\u91ca\u653e\nfor (int i = 0; i < n; i++) {\nfree(nodes[i]);\n}\nfree(nodes);\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nhashTable *h = NULL;\nfor (int i = 0; i < n; i++) {\nhashTable *tmp = malloc(sizeof(hashTable));\ntmp->key = i;\ntmp->val = i;\nHASH_ADD_INT(h, key, tmp);\n}\n// \u5185\u5b58\u91ca\u653e\nhashTable *curr, *tmp;\nHASH_ITER(hh, h, curr, tmp) {\nHASH_DEL(h, curr);\nfree(curr);\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\nint[] nums = new int[n];\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nList<ListNode> nodes = new();\nfor (int i = 0; i < n; i++) {\nnodes.Add(new ListNode(i));\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nDictionary<int, string> map = new();\nfor (int i = 0; i < n; i++) {\nmap.Add(i, i.ToString());\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\nlet nums = Array(repeating: 0, count: n)\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nlet nodes = (0 ..< n).map { ListNode(x: $0) }\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nlet map = Dictionary(uniqueKeysWithValues: (0 ..< n).map { ($0, \"\\($0)\") })\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\nvar nums = [_]i32{0}**n;\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nvar nodes = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer nodes.deinit();\nvar i: i32 = 0;\nwhile (i < n) : (i += 1) {\ntry nodes.append(i);\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nvar map = std.AutoArrayHashMap(i32, []const u8).init(std.heap.page_allocator);\ndefer map.deinit();\nvar j: i32 = 0;\nwhile (j < n) : (j += 1) {\nconst string = try std.fmt.allocPrint(std.heap.page_allocator, \"{d}\", .{j});\ndefer std.heap.page_allocator.free(string);\ntry map.put(i, string);\n}\n_ = nums;\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\nList<int> nums = List.filled(n, 0);\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nList<ListNode> nodes = [];\nfor (var i = 0; i < n; i++) {\nnodes.add(ListNode(i));\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nMap<int, String> map = HashMap();\nfor (var i = 0; i < n; i++) {\nmap.putIfAbsent(i, () => i.toString());\n}\n}\n

    \u4ee5\u4e0b\u9012\u5f52\u51fd\u6570\u4f1a\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684 algorithm() \u51fd\u6570\uff0c\u4f7f\u7528 \\(O(n)\\) \u5927\u5c0f\u7684\u6808\u5e27\u7a7a\u95f4\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart space_complexity.java
    /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\nSystem.out.println(\"\u9012\u5f52 n = \" + n);\nif (n == 1)\nreturn;\nlinearRecur(n - 1);\n}\n
    space_complexity.cpp
    /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\ncout << \"\u9012\u5f52 n = \" << n << endl;\nif (n == 1)\nreturn;\nlinearRecur(n - 1);\n}\n
    space_complexity.py
    def linear_recur(n: int) -> None:\n\"\"\"\u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\nprint(\"\u9012\u5f52 n =\", n)\nif n == 1:\nreturn\nlinear_recur(n - 1)\n
    space_complexity.go
    /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc spaceLinearRecur(n int) {\nfmt.Println(\"\u9012\u5f52 n =\", n)\nif n == 1 {\nreturn\n}\nspaceLinearRecur(n - 1)\n}\n
    space_complexity.js
    /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction linearRecur(n) {\nconsole.log(`\u9012\u5f52 n = ${n}`);\nif (n === 1) return;\nlinearRecur(n - 1);\n}\n
    space_complexity.ts
    /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction linearRecur(n: number): void {\nconsole.log(`\u9012\u5f52 n = ${n}`);\nif (n === 1) return;\nlinearRecur(n - 1);\n}\n
    space_complexity.c
    /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\nprintf(\"\u9012\u5f52 n = %d\\r\\n\", n);\nif (n == 1)\nreturn;\nlinearRecur(n - 1);\n}\n
    space_complexity.cs
    /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\nConsole.WriteLine(\"\u9012\u5f52 n = \" + n);\nif (n == 1) return;\nlinearRecur(n - 1);\n}\n
    space_complexity.swift
    /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc linearRecur(n: Int) {\nprint(\"\u9012\u5f52 n = \\(n)\")\nif n == 1 {\nreturn\n}\nlinearRecur(n: n - 1)\n}\n
    space_complexity.zig
    // \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn linearRecur(comptime n: i32) void {\nstd.debug.print(\"\u9012\u5f52 n = {}\\n\", .{n});\nif (n == 1) return;\nlinearRecur(n - 1);\n}\n
    space_complexity.dart
    /* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\nprint('\u9012\u5f52 n = $n');\nif (n == 1) return;\nlinearRecur(n - 1);\n}\n

    Fig. \u9012\u5f52\u51fd\u6570\u4ea7\u751f\u7684\u7ebf\u6027\u9636\u7a7a\u95f4\u590d\u6742\u5ea6

    "},{"location":"chapter_computational_complexity/space_complexity/#on2","title":"\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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart space_complexity.java
    /* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n// \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\nint[][] numMatrix = new int[n][n];\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nList<List<Integer>> numList = new ArrayList<>();\nfor (int i = 0; i < n; i++) {\nList<Integer> tmp = new ArrayList<>();\nfor (int j = 0; j < n; j++) {\ntmp.add(0);\n}\nnumList.add(tmp);\n}\n}\n
    space_complexity.cpp
    /* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nvector<vector<int>> numMatrix;\nfor (int i = 0; i < n; i++) {\nvector<int> tmp;\nfor (int j = 0; j < n; j++) {\ntmp.push_back(0);\n}\nnumMatrix.push_back(tmp);\n}\n}\n
    space_complexity.py
    def quadratic(n: int) -> None:\n\"\"\"\u5e73\u65b9\u9636\"\"\"\n# \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nnum_matrix = [[0] * n for _ in range(n)]\n
    space_complexity.go
    /* \u5e73\u65b9\u9636 */\nfunc spaceQuadratic(n int) {\n// \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\nnumMatrix := make([][]int, n)\nfor i := 0; i < n; i++ {\nnumMatrix[i] = make([]int, n)\n}\n}\n
    space_complexity.js
    /* \u5e73\u65b9\u9636 */\nfunction quadratic(n) {\n// \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\nconst numMatrix = Array(n)\n.fill(null)\n.map(() => Array(n).fill(null));\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nconst numList = [];\nfor (let i = 0; i < n; i++) {\nconst tmp = [];\nfor (let j = 0; j < n; j++) {\ntmp.push(0);\n}\nnumList.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\nconst numMatrix = Array(n)\n.fill(null)\n.map(() => Array(n).fill(null));\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nconst numList = [];\nfor (let i = 0; i < n; i++) {\nconst tmp = [];\nfor (let j = 0; j < n; j++) {\ntmp.push(0);\n}\nnumList.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\nint **numMatrix = malloc(sizeof(int *) * n);\nfor (int i = 0; i < n; i++) {\nint *tmp = malloc(sizeof(int) * n);\nfor (int j = 0; j < n; j++) {\ntmp[j] = 0;\n}\nnumMatrix[i] = tmp;\n}\n// \u5185\u5b58\u91ca\u653e\nfor (int i = 0; i < n; i++) {\nfree(numMatrix[i]);\n}\nfree(numMatrix);\n}\n
    space_complexity.cs
    /* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n// \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\nint[,] numMatrix = new int[n, n];\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nList<List<int>> numList = new();\nfor (int i = 0; i < n; i++) {\nList<int> tmp = new();\nfor (int j = 0; j < n; j++) {\ntmp.Add(0);\n}\nnumList.Add(tmp);\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\nlet numList = Array(repeating: Array(repeating: 0, count: n), count: n)\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\nvar nodes = std.ArrayList(std.ArrayList(i32)).init(std.heap.page_allocator);\ndefer nodes.deinit();\nvar i: i32 = 0;\nwhile (i < n) : (i += 1) {\nvar tmp = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer tmp.deinit();\nvar j: i32 = 0;\nwhile (j < n) : (j += 1) {\ntry tmp.append(0);\n}\ntry nodes.append(tmp);\n}\n}\n
    space_complexity.dart
    /* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n// \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\nList<List<int>> numMatrix = List.generate(n, (_) => List.filled(n, 0));\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nList<List<int>> numList = [];\nfor (var i = 0; i < n; i++) {\nList<int> tmp = [];\nfor (int j = 0; j < n; j++) {\ntmp.add(0);\n}\nnumList.add(tmp);\n}\n}\n

    \u5728\u4ee5\u4e0b\u9012\u5f52\u51fd\u6570\u4e2d\uff0c\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684 algorithm() \uff0c\u5e76\u4e14\u6bcf\u4e2a\u51fd\u6570\u4e2d\u90fd\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u6570\u7ec4\uff0c\u957f\u5ea6\u5206\u522b\u4e3a \\(n, n-1, n-2, ..., 2, 1\\) \uff0c\u5e73\u5747\u957f\u5ea6\u4e3a \\(\\frac{n}{2}\\) \uff0c\u56e0\u6b64\u603b\u4f53\u5360\u7528 \\(O(n^2)\\) \u7a7a\u95f4\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart space_complexity.java
    /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\nif (n <= 0)\nreturn 0;\n// \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\nint[] nums = new int[n];\nSystem.out.println(\"\u9012\u5f52 n = \" + n + \" \u4e2d\u7684 nums \u957f\u5ea6 = \" + nums.length);\nreturn quadraticRecur(n - 1);\n}\n
    space_complexity.cpp
    /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\nif (n <= 0)\nreturn 0;\nvector<int> nums(n);\ncout << \"\u9012\u5f52 n = \" << n << \" \u4e2d\u7684 nums \u957f\u5ea6 = \" << nums.size() << endl;\nreturn quadraticRecur(n - 1);\n}\n
    space_complexity.py
    def quadratic_recur(n: int) -> int:\n\"\"\"\u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\nif n <= 0:\nreturn 0\n# \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\nnums = [0] * n\nreturn quadratic_recur(n - 1)\n
    space_complexity.go
    /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc spaceQuadraticRecur(n int) int {\nif n <= 0 {\nreturn 0\n}\nnums := make([]int, n)\nfmt.Printf(\"\u9012\u5f52 n = %d \u4e2d\u7684 nums \u957f\u5ea6 = %d \\n\", n, len(nums))\nreturn spaceQuadraticRecur(n - 1)\n}\n
    space_complexity.js
    /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction quadraticRecur(n) {\nif (n <= 0) return 0;\nconst nums = new Array(n);\nconsole.log(`\u9012\u5f52 n = ${n} \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}`);\nreturn quadraticRecur(n - 1);\n}\n
    space_complexity.ts
    /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction quadraticRecur(n: number): number {\nif (n <= 0) return 0;\nconst nums = new Array(n);\nconsole.log(`\u9012\u5f52 n = ${n} \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}`);\nreturn quadraticRecur(n - 1);\n}\n
    space_complexity.c
    /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\nif (n <= 0)\nreturn 0;\nint *nums = malloc(sizeof(int) * n);\nprintf(\"\u9012\u5f52 n = %d \u4e2d\u7684 nums \u957f\u5ea6 = %d\\r\\n\", n, n);\nfree(nums);\nreturn quadraticRecur(n - 1);\n}\n
    space_complexity.cs
    /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\nif (n <= 0) return 0;\nint[] nums = new int[n];\nConsole.WriteLine(\"\u9012\u5f52 n = \" + n + \" \u4e2d\u7684 nums \u957f\u5ea6 = \" + nums.Length);\nreturn quadraticRecur(n - 1);\n}\n
    space_complexity.swift
    /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\n@discardableResult\nfunc quadraticRecur(n: Int) -> Int {\nif n <= 0 {\nreturn 0\n}\n// \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\nlet nums = Array(repeating: 0, count: n)\nprint(\"\u9012\u5f52 n = \\(n) \u4e2d\u7684 nums \u957f\u5ea6 = \\(nums.count)\")\nreturn quadraticRecur(n: n - 1)\n}\n
    space_complexity.zig
    // \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn quadraticRecur(comptime n: i32) i32 {\nif (n <= 0) return 0;\nvar nums = [_]i32{0}**n;\nstd.debug.print(\"\u9012\u5f52 n = {} \u4e2d\u7684 nums \u957f\u5ea6 = {}\\n\", .{n, nums.len});\nreturn quadraticRecur(n - 1);\n}\n
    space_complexity.dart
    /* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\nif (n <= 0) return 0;\nList<int> nums = List.filled(n, 0);\nprint('\u9012\u5f52 n = $n \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}');\nreturn quadraticRecur(n - 1);\n}\n

    Fig. \u9012\u5f52\u51fd\u6570\u4ea7\u751f\u7684\u5e73\u65b9\u9636\u7a7a\u95f4\u590d\u6742\u5ea6

    "},{"location":"chapter_computational_complexity/space_complexity/#o2n","title":"\u6307\u6570\u9636 \\(O(2^n)\\)","text":"

    \u6307\u6570\u9636\u5e38\u89c1\u4e8e\u4e8c\u53c9\u6811\u3002\u9ad8\u5ea6\u4e3a \\(n\\) \u7684\u300c\u6ee1\u4e8c\u53c9\u6811\u300d\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(2^n - 1\\) \uff0c\u5360\u7528 \\(O(2^n)\\) \u7a7a\u95f4\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart space_complexity.java
    /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode buildTree(int n) {\nif (n == 0)\nreturn null;\nTreeNode root = new TreeNode(0);\nroot.left = buildTree(n - 1);\nroot.right = buildTree(n - 1);\nreturn root;\n}\n
    space_complexity.cpp
    /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode *buildTree(int n) {\nif (n == 0)\nreturn nullptr;\nTreeNode *root = new TreeNode(0);\nroot->left = buildTree(n - 1);\nroot->right = buildTree(n - 1);\nreturn root;\n}\n
    space_complexity.py
    def build_tree(n: int) -> TreeNode | None:\n\"\"\"\u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09\"\"\"\nif n == 0:\nreturn None\nroot = TreeNode(0)\nroot.left = build_tree(n - 1)\nroot.right = build_tree(n - 1)\nreturn root\n
    space_complexity.go
    /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunc buildTree(n int) *treeNode {\nif n == 0 {\nreturn nil\n}\nroot := newTreeNode(0)\nroot.left = buildTree(n - 1)\nroot.right = buildTree(n - 1)\nreturn root\n}\n
    space_complexity.js
    /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunction buildTree(n) {\nif (n === 0) return null;\nconst root = new TreeNode(0);\nroot.left = buildTree(n - 1);\nroot.right = buildTree(n - 1);\nreturn root;\n}\n
    space_complexity.ts
    /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunction buildTree(n: number): TreeNode | null {\nif (n === 0) return null;\nconst root = new TreeNode(0);\nroot.left = buildTree(n - 1);\nroot.right = buildTree(n - 1);\nreturn root;\n}\n
    space_complexity.c
    /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode *buildTree(int n) {\nif (n == 0)\nreturn NULL;\nTreeNode *root = newTreeNode(0);\nroot->left = buildTree(n - 1);\nroot->right = buildTree(n - 1);\nreturn root;\n}\n
    space_complexity.cs
    /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode? buildTree(int n) {\nif (n == 0) return null;\nTreeNode root = new TreeNode(0);\nroot.left = buildTree(n - 1);\nroot.right = buildTree(n - 1);\nreturn root;\n}\n
    space_complexity.swift
    /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunc buildTree(n: Int) -> TreeNode? {\nif n == 0 {\nreturn nil\n}\nlet root = TreeNode(x: 0)\nroot.left = buildTree(n: n - 1)\nroot.right = buildTree(n: n - 1)\nreturn 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) {\nif (n == 0) return null;\nconst root = try mem_allocator.create(inc.TreeNode(i32));\nroot.init(0);\nroot.left = try buildTree(mem_allocator, n - 1);\nroot.right = try buildTree(mem_allocator, n - 1);\nreturn root;\n}\n
    space_complexity.dart
    /* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode? buildTree(int n) {\nif (n == 0) return null;\nTreeNode root = TreeNode(0);\nroot.left = buildTree(n - 1);\nroot.right = buildTree(n - 1);\nreturn root;\n}\n

    Fig. \u6ee1\u4e8c\u53c9\u6811\u4ea7\u751f\u7684\u6307\u6570\u9636\u7a7a\u95f4\u590d\u6742\u5ea6

    "},{"location":"chapter_computational_complexity/space_complexity/#olog-n","title":"\u5bf9\u6570\u9636 \\(O(\\log n)\\)","text":"

    \u5bf9\u6570\u9636\u5e38\u89c1\u4e8e\u5206\u6cbb\u7b97\u6cd5\u548c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u7b49\u3002

    \u4f8b\u5982\u201c\u5f52\u5e76\u6392\u5e8f\u201d\u7b97\u6cd5\uff0c\u8f93\u5165\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\uff0c\u6bcf\u8f6e\u9012\u5f52\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\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\u201c\u6570\u5b57\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\u201d\uff0c\u8f93\u5165\u4efb\u610f\u6b63\u6574\u6570 \\(n\\) \uff0c\u5b83\u7684\u4f4d\u6570\u4e3a \\(\\log_{10} n\\) \uff0c\u5373\u5bf9\u5e94\u5b57\u7b26\u4e32\u957f\u5ea6\u4e3a \\(\\log_{10} n\\) \uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log_{10} n) = O(\\log n)\\) \u3002

    "},{"location":"chapter_computational_complexity/space_complexity/#234","title":"2.3.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\u662f\u975e\u5e38\u56f0\u96be\u7684\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\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\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\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002

    "},{"location":"chapter_computational_complexity/summary/","title":"2.4. \u00a0 \u5c0f\u7ed3","text":"

    \u7b97\u6cd5\u6548\u7387\u8bc4\u4f30

    • \u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u662f\u8bc4\u4ef7\u7b97\u6cd5\u6027\u80fd\u7684\u4e24\u4e2a\u5173\u952e\u7ef4\u5ea6\u3002
    • \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b9e\u9645\u6d4b\u8bd5\u6765\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\uff0c\u4f46\u96be\u4ee5\u6d88\u9664\u6d4b\u8bd5\u73af\u5883\u7684\u5f71\u54cd\uff0c\u4e14\u4f1a\u8017\u8d39\u5927\u91cf\u8ba1\u7b97\u8d44\u6e90\u3002
    • \u590d\u6742\u5ea6\u5206\u6790\u53ef\u4ee5\u514b\u670d\u5b9e\u9645\u6d4b\u8bd5\u7684\u5f0a\u7aef\uff0c\u5206\u6790\u7ed3\u679c\u9002\u7528\u4e8e\u6240\u6709\u8fd0\u884c\u5e73\u53f0\uff0c\u5e76\u4e14\u80fd\u591f\u63ed\u793a\u7b97\u6cd5\u5728\u4e0d\u540c\u6570\u636e\u89c4\u6a21\u4e0b\u7684\u6548\u7387\u3002

    \u65f6\u95f4\u590d\u6742\u5ea6

    • \u65f6\u95f4\u590d\u6742\u5ea6\u7528\u4e8e\u8861\u91cf\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u968f\u6570\u636e\u91cf\u589e\u957f\u7684\u8d8b\u52bf\uff0c\u53ef\u4ee5\u6709\u6548\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u5931\u6548\uff0c\u5982\u5728\u8f93\u5165\u6570\u636e\u91cf\u8f83\u5c0f\u6216\u65f6\u95f4\u590d\u6742\u5ea6\u76f8\u540c\u65f6\uff0c\u65e0\u6cd5\u7cbe\u786e\u5bf9\u6bd4\u7b97\u6cd5\u6548\u7387\u7684\u4f18\u52a3\u3002
    • \u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4f7f\u7528\u5927 \\(O\\) \u7b26\u53f7\u8868\u793a\uff0c\u5373\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c\uff0c\u53cd\u6620\u5f53 \\(n\\) \u8d8b\u5411\u6b63\u65e0\u7a77\u65f6\uff0c\\(T(n)\\) \u7684\u589e\u957f\u7ea7\u522b\u3002
    • \u63a8\u7b97\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u4e3a\u4e24\u6b65\uff0c\u9996\u5148\u7edf\u8ba1\u8ba1\u7b97\u64cd\u4f5c\u6570\u91cf\uff0c\u7136\u540e\u5224\u65ad\u6e10\u8fd1\u4e0a\u754c\u3002
    • \u5e38\u89c1\u65f6\u95f4\u590d\u6742\u5ea6\u4ece\u5c0f\u5230\u5927\u6392\u5217\u6709 \\(O(1)\\) , \\(O(\\log n)\\) , \\(O(n)\\) , \\(O(n \\log n)\\) , \\(O(n^2)\\) , \\(O(2^n)\\) , \\(O(n!)\\) \u7b49\u3002
    • \u67d0\u4e9b\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u975e\u56fa\u5b9a\uff0c\u800c\u662f\u4e0e\u8f93\u5165\u6570\u636e\u7684\u5206\u5e03\u6709\u5173\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u4e3a\u6700\u5dee\u3001\u6700\u4f73\u3001\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u51e0\u4e4e\u4e0d\u7528\uff0c\u56e0\u4e3a\u8f93\u5165\u6570\u636e\u4e00\u822c\u9700\u8981\u6ee1\u8db3\u4e25\u683c\u6761\u4ef6\u624d\u80fd\u8fbe\u5230\u6700\u4f73\u60c5\u51b5\u3002
    • \u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u53cd\u6620\u7b97\u6cd5\u5728\u968f\u673a\u6570\u636e\u8f93\u5165\u4e0b\u7684\u8fd0\u884c\u6548\u7387\uff0c\u6700\u63a5\u8fd1\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u7b97\u6cd5\u6027\u80fd\u3002\u8ba1\u7b97\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u9700\u8981\u7edf\u8ba1\u8f93\u5165\u6570\u636e\u5206\u5e03\u4ee5\u53ca\u7efc\u5408\u540e\u7684\u6570\u5b66\u671f\u671b\u3002

    \u7a7a\u95f4\u590d\u6742\u5ea6

    • \u7c7b\u4f3c\u4e8e\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u7528\u4e8e\u8861\u91cf\u7b97\u6cd5\u5360\u7528\u7a7a\u95f4\u968f\u6570\u636e\u91cf\u589e\u957f\u7684\u8d8b\u52bf\u3002
    • \u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u76f8\u5173\u5185\u5b58\u7a7a\u95f4\u53ef\u5206\u4e3a\u8f93\u5165\u7a7a\u95f4\u3001\u6682\u5b58\u7a7a\u95f4\u3001\u8f93\u51fa\u7a7a\u95f4\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u8f93\u5165\u7a7a\u95f4\u4e0d\u8ba1\u5165\u7a7a\u95f4\u590d\u6742\u5ea6\u8ba1\u7b97\u3002\u6682\u5b58\u7a7a\u95f4\u53ef\u5206\u4e3a\u6307\u4ee4\u7a7a\u95f4\u3001\u6570\u636e\u7a7a\u95f4\u3001\u6808\u5e27\u7a7a\u95f4\uff0c\u5176\u4e2d\u6808\u5e27\u7a7a\u95f4\u901a\u5e38\u4ec5\u5728\u9012\u5f52\u51fd\u6570\u4e2d\u5f71\u54cd\u7a7a\u95f4\u590d\u6742\u5ea6\u3002
    • \u6211\u4eec\u901a\u5e38\u53ea\u5173\u6ce8\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u5373\u7edf\u8ba1\u7b97\u6cd5\u5728\u6700\u5dee\u8f93\u5165\u6570\u636e\u548c\u6700\u5dee\u8fd0\u884c\u65f6\u95f4\u70b9\u4e0b\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u3002
    • \u5e38\u89c1\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece\u5c0f\u5230\u5927\u6392\u5217\u6709 \\(O(1)\\) , \\(O(\\log n)\\) , \\(O(n)\\) , \\(O(n^2)\\) , \\(O(2^n)\\) \u7b49\u3002
    "},{"location":"chapter_computational_complexity/summary/#241-q-a","title":"2.4.1. \u00a0 Q & A","text":"

    \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\u88ab\u4f18\u5316\u81f3 \\(O(1)\\) \u3002\u4e0d\u8fc7\u7edd\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\uff08\u4f8b\u5982 Java, Python, C++, Go, C# \u7b49\uff09 \u90fd\u4e0d\u652f\u6301\u81ea\u52a8\u4f18\u5316\u5c3e\u9012\u5f52\uff0c\u56e0\u6b64\u4e00\u822c\u6765\u8bf4\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

    \u51fd\u6570\uff08function\uff09\u53ef\u4ee5\u72ec\u7acb\u88ab\u6267\u884c\uff0c\u6240\u6709\u53c2\u6570\u90fd\u4ee5\u663e\u5f0f\u4f20\u9012\u3002 \u65b9\u6cd5\uff08method\uff09\u4e0e\u4e00\u4e2a\u5bf9\u8c61\u5173\u8054\uff0c\u65b9\u6cd5\u88ab\u9690\u5f0f\u4f20\u9012\u7ed9\u8c03\u7528\u5b83\u7684\u5bf9\u8c61\uff0c\u65b9\u6cd5\u80fd\u591f\u5bf9\u7c7b\u7684\u5b9e\u4f8b\u4e2d\u5305\u542b\u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002

    \u56fe\u7247\u201c\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u5e38\u89c1\u7c7b\u578b\u201d\u53cd\u6620\u7684\u662f\u5426\u662f\u5360\u7528\u7a7a\u95f4\u7684\u7edd\u5bf9\u5927\u5c0f\uff1f

    \u4e0d\u662f\uff0c\u8be5\u56fe\u7247\u5c55\u793a\u7684\u662f\u7a7a\u95f4\u590d\u6742\u5ea6\uff08\u5373\u589e\u957f\u8d8b\u52bf\uff09\uff0c\u800c\u4e0d\u662f\u5360\u7528\u7a7a\u95f4\u7684\u7edd\u5bf9\u5927\u5c0f\u3002\u6bcf\u4e2a\u66f2\u7ebf\u90fd\u5305\u542b\u4e00\u4e2a\u5e38\u6570\u9879\uff0c\u7528\u6765\u628a\u6240\u6709\u66f2\u7ebf\u7684\u53d6\u503c\u8303\u56f4\u538b\u7f29\u5230\u4e00\u4e2a\u89c6\u89c9\u8212\u9002\u7684\u8303\u56f4\u5185\u3002 \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\uff1b\u4f46\u76f8\u5bf9\u5730 \\(n = 8^5\\) \u5c31\u5f88\u597d\u9009\u4e86\uff0c\u8fd9\u662f\u590d\u6742\u5ea6\u5360\u4e3b\u5bfc\u7684\u60c5\u51b5\u3002

    "},{"location":"chapter_computational_complexity/time_complexity/","title":"2.2. \u00a0 \u65f6\u95f4\u590d\u6742\u5ea6","text":""},{"location":"chapter_computational_complexity/time_complexity/#221","title":"2.2.1. \u00a0 \u7edf\u8ba1\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4","text":"

    \u8fd0\u884c\u65f6\u95f4\u53ef\u4ee5\u76f4\u89c2\u4e14\u51c6\u786e\u5730\u53cd\u6620\u7b97\u6cd5\u7684\u6548\u7387\u3002\u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\u51c6\u786e\u9884\u4f30\u4e00\u6bb5\u4ee3\u7801\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u5e94\u8be5\u5982\u4f55\u64cd\u4f5c\u5462\uff1f

    1. \u786e\u5b9a\u8fd0\u884c\u5e73\u53f0\uff0c\u5305\u62ec\u786c\u4ef6\u914d\u7f6e\u3001\u7f16\u7a0b\u8bed\u8a00\u3001\u7cfb\u7edf\u73af\u5883\u7b49\uff0c\u8fd9\u4e9b\u56e0\u7d20\u90fd\u4f1a\u5f71\u54cd\u4ee3\u7801\u7684\u8fd0\u884c\u6548\u7387\u3002
    2. \u8bc4\u4f30\u5404\u79cd\u8ba1\u7b97\u64cd\u4f5c\u6240\u9700\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u4f8b\u5982\u52a0\u6cd5\u64cd\u4f5c + \u9700\u8981 1 ns\uff0c\u4e58\u6cd5\u64cd\u4f5c * \u9700\u8981 10 ns\uff0c\u6253\u5370\u64cd\u4f5c\u9700\u8981 5 ns \u7b49\u3002
    3. \u7edf\u8ba1\u4ee3\u7801\u4e2d\u6240\u6709\u7684\u8ba1\u7b97\u64cd\u4f5c\uff0c\u5e76\u5c06\u6240\u6709\u64cd\u4f5c\u7684\u6267\u884c\u65f6\u95f4\u6c42\u548c\uff0c\u4ece\u800c\u5f97\u5230\u8fd0\u884c\u65f6\u95f4\u3002

    \u4f8b\u5982\u4ee5\u4e0b\u4ee3\u7801\uff0c\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u6839\u636e\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5f97\u5230\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u4e3a \\(6n + 12\\) ns \u3002

    \\[ 1 + 1 + 10 + (1 + 5) \\times n = 6n + 12 \\] JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\nint a = 2;  // 1 ns\na = a + 1;  // 1 ns\na = a * 2;  // 10 ns\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i < n; i++) {  // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\nSystem.out.println(0);     // 5 ns\n}\n}\n
    // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\nint a = 2;  // 1 ns\na = a + 1;  // 1 ns\na = a * 2;  // 10 ns\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i < n; i++) {  // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\ncout << 0 << endl;         // 5 ns\n}\n}\n
    # \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\ndef algorithm(n: int) -> None:\na = 2      # 1 ns\na = a + 1  # 1 ns\na = a * 2  # 10 ns\n# \u5faa\u73af n \u6b21\nfor _ in range(n):  # 1 ns\nprint(0)        # 5 ns\n
    // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunc algorithm(n int) {\na := 2      // 1 ns\na = a + 1   // 1 ns\na = a * 2   // 10 ns\n// \u5faa\u73af n \u6b21\nfor i := 0; i < n; i++ {    // 1 ns\nfmt.Println(a)          // 5 ns\n}\n}\n
    // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunction algorithm(n) {\nvar a = 2; // 1 ns\na = a + 1; // 1 ns\na = a * 2; // 10 ns\n// \u5faa\u73af n \u6b21\nfor(let i = 0; i < n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\nconsole.log(0); // 5 ns\n}\n}\n
    // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunction algorithm(n: number): void {\nvar a: number = 2; // 1 ns\na = a + 1; // 1 ns\na = a * 2; // 10 ns\n// \u5faa\u73af n \u6b21\nfor(let i = 0; i < n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\nconsole.log(0); // 5 ns\n}\n}\n
    // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\nint a = 2;  // 1 ns\na = a + 1;  // 1 ns\na = a * 2;  // 10 ns\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i < n; i++) {   // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\nprintf(\"%d\", 0);            // 5 ns\n}\n}\n
    // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n)\n{\nint a = 2;  // 1 ns\na = a + 1;  // 1 ns\na = a * 2;  // 10 ns\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i < n; i++)\n{  // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\nConsole.WriteLine(0);     // 5 ns\n}\n}\n
    // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunc algorithm(n: Int) {\nvar a = 2 // 1 ns\na = a + 1 // 1 ns\na = a * 2 // 10 ns\n// \u5faa\u73af n \u6b21\nfor _ in 0 ..< n { // 1 ns\nprint(0) // 5 ns\n}\n}\n
    \n
    // \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\nint a = 2; // 1 ns\na = a + 1; // 1 ns\na = a * 2; // 10 ns\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i < n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\nprint(0); // 5 ns\n}\n}\n

    \u7136\u800c\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\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/#222","title":"2.2.2. \u00a0 \u7edf\u8ba1\u65f6\u95f4\u589e\u957f\u8d8b\u52bf","text":"

    \u300c\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u300d\u91c7\u53d6\u4e86\u4e00\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\uff0c\u5176\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\u8f83\u4e3a\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 , B , C \u3002

    • \u7b97\u6cd5 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\u300c\u5e38\u6570\u9636\u300d\u3002
    • \u7b97\u6cd5 B \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\u300c\u7ebf\u6027\u9636\u300d\u3002
    • \u7b97\u6cd5 C \u4e2d\u7684\u6253\u5370\u64cd\u4f5c\u9700\u8981\u5faa\u73af \\(1000000\\) \u6b21\uff0c\u4f46\u8fd0\u884c\u65f6\u95f4\u4ecd\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\u300c\u5e38\u6570\u9636\u300d\u3002
    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    // \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\nSystem.out.println(0);\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\nfor (int i = 0; i < n; i++) {\nSystem.out.println(0);\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\nfor (int i = 0; i < 1000000; i++) {\nSystem.out.println(0);\n}\n}\n
    // \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\ncout << 0 << endl;\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\nfor (int i = 0; i < n; i++) {\ncout << 0 << endl;\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\nfor (int i = 0; i < 1000000; i++) {\ncout << 0 << endl;\n}\n}\n
    # \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_A(n: int) -> None:\nprint(0)\n# \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\ndef algorithm_B(n: int) -> None:\nfor _ in range(n):\nprint(0)\n# \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_C(n: int) -> None:\nfor _ in range(1000000):\nprint(0)\n
    // \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithm_A(n int) {\nfmt.Println(0)\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunc algorithm_B(n int) {\nfor i := 0; i < n; i++ {\nfmt.Println(0)\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithm_C(n int) {\nfor i := 0; i < 1000000; i++ {\nfmt.Println(0)\n}\n}\n
    // \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_A(n) {\nconsole.log(0);\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunction algorithm_B(n) {\nfor (let i = 0; i < n; i++) {\nconsole.log(0);\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_C(n) {\nfor (let i = 0; i < 1000000; i++) {\nconsole.log(0);\n}\n}\n
    // \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_A(n: number): void {\nconsole.log(0);\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunction algorithm_B(n: number): void {\nfor (let i = 0; i < n; i++) {\nconsole.log(0);\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_C(n: number): void {\nfor (let i = 0; i < 1000000; i++) {\nconsole.log(0);\n}\n}\n
    // \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\nprintf(\"%d\", 0);\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\nfor (int i = 0; i < n; i++) {\nprintf(\"%d\", 0);\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\nfor (int i = 0; i < 1000000; i++) {\nprintf(\"%d\", 0);\n}\n}\n
    // \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n)\n{\nConsole.WriteLine(0);\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n)\n{\nfor (int i = 0; i < n; i++)\n{\nConsole.WriteLine(0);\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n)\n{\nfor (int i = 0; i < 1000000; i++)\n{\nConsole.WriteLine(0);\n}\n}\n
    // \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithmA(n: Int) {\nprint(0)\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunc algorithmB(n: Int) {\nfor _ in 0 ..< n {\nprint(0)\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithmC(n: Int) {\nfor _ in 0 ..< 1000000 {\nprint(0)\n}\n}\n
    \n
    // \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithmA(int n) {\nprint(0);\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithmB(int n) {\nfor (int i = 0; i < n; i++) {\nprint(0);\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithmC(int n) {\nfor (int i = 0; i < 1000000; i++) {\nprint(0);\n}\n}\n

    Fig. \u7b97\u6cd5 A, B, C \u7684\u65f6\u95f4\u589e\u957f\u8d8b\u52bf

    \u76f8\u8f83\u4e8e\u76f4\u63a5\u7edf\u8ba1\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u6709\u54ea\u4e9b\u4f18\u52bf\u548c\u5c40\u9650\u6027\u5462\uff1f

    \u65f6\u95f4\u590d\u6742\u5ea6\u80fd\u591f\u6709\u6548\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\u3002\u4f8b\u5982\uff0c\u7b97\u6cd5 B \u7684\u8fd0\u884c\u65f6\u95f4\u5448\u7ebf\u6027\u589e\u957f\uff0c\u5728 \\(n > 1\\) \u65f6\u6bd4\u7b97\u6cd5 A \u6162\uff0c\u5728 \\(n > 1000000\\) \u65f6\u6bd4\u7b97\u6cd5 C \u6162\u3002\u4e8b\u5b9e\u4e0a\uff0c\u53ea\u8981\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u8db3\u591f\u5927\uff0c\u590d\u6742\u5ea6\u4e3a\u300c\u5e38\u6570\u9636\u300d\u7684\u7b97\u6cd5\u4e00\u5b9a\u4f18\u4e8e\u300c\u7ebf\u6027\u9636\u300d\u7684\u7b97\u6cd5\uff0c\u8fd9\u6b63\u662f\u65f6\u95f4\u589e\u957f\u8d8b\u52bf\u6240\u8868\u8fbe\u7684\u542b\u4e49\u3002

    \u65f6\u95f4\u590d\u6742\u5ea6\u7684\u63a8\u7b97\u65b9\u6cd5\u66f4\u7b80\u4fbf\u3002\u663e\u7136\uff0c\u8fd0\u884c\u5e73\u53f0\u548c\u8ba1\u7b97\u64cd\u4f5c\u7c7b\u578b\u90fd\u4e0e\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u65e0\u5173\u3002\u56e0\u6b64\u5728\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u5730\u5c06\u6240\u6709\u8ba1\u7b97\u64cd\u4f5c\u7684\u6267\u884c\u65f6\u95f4\u89c6\u4e3a\u76f8\u540c\u7684\u201c\u5355\u4f4d\u65f6\u95f4\u201d\uff0c\u4ece\u800c\u5c06\u201c\u8ba1\u7b97\u64cd\u4f5c\u7684\u8fd0\u884c\u65f6\u95f4\u7684\u7edf\u8ba1\u201d\u7b80\u5316\u4e3a\u201c\u8ba1\u7b97\u64cd\u4f5c\u7684\u6570\u91cf\u7684\u7edf\u8ba1\u201d\uff0c\u8fd9\u6837\u7684\u7b80\u5316\u65b9\u6cd5\u5927\u5927\u964d\u4f4e\u4e86\u4f30\u7b97\u96be\u5ea6\u3002

    \u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u5b58\u5728\u4e00\u5b9a\u7684\u5c40\u9650\u6027\u3002\u4f8b\u5982\uff0c\u5c3d\u7ba1\u7b97\u6cd5 A \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\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

    "},{"location":"chapter_computational_complexity/time_complexity/#223","title":"2.2.3. \u00a0 \u51fd\u6570\u6e10\u8fd1\u4e0a\u754c","text":"

    \u8bbe\u7b97\u6cd5\u7684\u8ba1\u7b97\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\u4e0b\u7b97\u6cd5\u7684\u64cd\u4f5c\u6570\u91cf\u4e3a

    \\[ T(n) = 3 + 2n \\] JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    void algorithm(int n) {\nint a = 1;  // +1\na = a + 1;  // +1\na = a * 2;  // +1\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i < n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\nSystem.out.println(0);    // +1\n}\n}\n
    void algorithm(int n) {\nint a = 1;  // +1\na = a + 1;  // +1\na = a * 2;  // +1\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i < n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\ncout << 0 << endl;    // +1\n}\n}\n
    def algorithm(n: int) -> None:\na = 1      # +1\na = a + 1  # +1\na = a * 2  # +1\n# \u5faa\u73af n \u6b21\nfor i in range(n):  # +1\nprint(0)        # +1\n
    func algorithm(n int) {\na := 1      // +1\na = a + 1   // +1\na = a * 2   // +1\n// \u5faa\u73af n \u6b21\nfor i := 0; i < n; i++ {   // +1\nfmt.Println(a)         // +1\n}\n}\n
    function algorithm(n) {\nvar a = 1; // +1\na += 1; // +1\na *= 2; // +1\n// \u5faa\u73af n \u6b21\nfor(let i = 0; i < n; i++){ // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\nconsole.log(0); // +1\n}\n}\n
    function algorithm(n: number): void{\nvar a: number = 1; // +1\na += 1; // +1\na *= 2; // +1\n// \u5faa\u73af n \u6b21\nfor(let i = 0; i < n; i++){ // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\nconsole.log(0); // +1\n}\n}\n
    void algorithm(int n) {\nint a = 1;  // +1\na = a + 1;  // +1\na = a * 2;  // +1\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i < n; i++) {   // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\nprintf(\"%d\", 0);            // +1\n}\n}  
    void algorithm(int n)\n{\nint a = 1;  // +1\na = a + 1;  // +1\na = a * 2;  // +1\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i < n; i++) // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n{\nConsole.WriteLine(0);   // +1\n}\n}\n
    func algorithm(n: Int) {\nvar a = 1 // +1\na = a + 1 // +1\na = a * 2 // +1\n// \u5faa\u73af n \u6b21\nfor _ in 0 ..< n { // +1\nprint(0) // +1\n}\n}\n
    \n
    void algorithm(int n) {\nint a = 1; // +1\na = a + 1; // +1\na = a * 2; // +1\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i < n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\nprint(0); // +1\n}\n}\n

    \\(T(n)\\) \u662f\u4e00\u6b21\u51fd\u6570\uff0c\u8bf4\u660e\u65f6\u95f4\u589e\u957f\u8d8b\u52bf\u662f\u7ebf\u6027\u7684\uff0c\u56e0\u6b64\u53ef\u4ee5\u5f97\u51fa\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

    \u63a8\u7b97\u65f6\u95f4\u590d\u6742\u5ea6\u672c\u8d28\u4e0a\u662f\u8ba1\u7b97\u201c\u64cd\u4f5c\u6570\u91cf\u51fd\u6570 \\(T(n)\\)\u201d\u7684\u6e10\u8fd1\u4e0a\u754c\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u6765\u770b\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c\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) $$ \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)) $$

    Fig. \u51fd\u6570\u7684\u6e10\u8fd1\u4e0a\u754c

    \u4ece\u672c\u8d28\u4e0a\u8bb2\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

    "},{"location":"chapter_computational_complexity/time_complexity/#224","title":"2.2.4. \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\u9700\u62c5\u5fc3\u3002\u56e0\u4e3a\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u6211\u4eec\u53ea\u9700\u8981\u638c\u63e1\u63a8\u7b97\u65b9\u6cd5\uff0c\u6570\u5b66\u610f\u4e49\u53ef\u4ee5\u9010\u6e10\u9886\u609f\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":"\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\u88ab\u5ffd\u7565\u3002\u6839\u636e\u6b64\u539f\u5219\uff0c\u53ef\u4ee5\u603b\u7ed3\u51fa\u4ee5\u4e0b\u8ba1\u6570\u7b80\u5316\u6280\u5de7\uff1a

    1. \u5ffd\u7565\u4e0e \\(n\\) \u65e0\u5173\u7684\u64cd\u4f5c\u3002\u56e0\u4e3a\u5b83\u4eec\u90fd\u662f \\(T(n)\\) \u4e2d\u7684\u5e38\u6570\u9879\uff0c\u5bf9\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u4ea7\u751f\u5f71\u54cd\u3002
    2. \u7701\u7565\u6240\u6709\u7cfb\u6570\u3002\u4f8b\u5982\uff0c\u5faa\u73af \\(2n\\) \u6b21\u3001\\(5n + 1\\) \u6b21\u7b49\uff0c\u90fd\u53ef\u4ee5\u7b80\u5316\u8bb0\u4e3a \\(n\\) \u6b21\uff0c\u56e0\u4e3a \\(n\\) \u524d\u9762\u7684\u7cfb\u6570\u5bf9\u65f6\u95f4\u590d\u6742\u5ea6\u6ca1\u6709\u5f71\u54cd\u3002
    3. \u5faa\u73af\u5d4c\u5957\u65f6\u4f7f\u7528\u4e58\u6cd5\u3002\u603b\u64cd\u4f5c\u6570\u91cf\u7b49\u4e8e\u5916\u5c42\u5faa\u73af\u548c\u5185\u5c42\u5faa\u73af\u64cd\u4f5c\u6570\u91cf\u4e4b\u79ef\uff0c\u6bcf\u4e00\u5c42\u5faa\u73af\u4f9d\u7136\u53ef\u4ee5\u5206\u522b\u5957\u7528\u4e0a\u8ff0 1. \u548c 2. \u6280\u5de7\u3002

    \u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u4f7f\u7528\u4e0a\u8ff0\u6280\u5de7\u524d\u3001\u540e\u7684\u7edf\u8ba1\u7ed3\u679c\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} \\]

    \u6700\u7ec8\uff0c\u4e24\u8005\u90fd\u80fd\u63a8\u51fa\u76f8\u540c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7ed3\u679c\uff0c\u5373 \\(O(n^2)\\) \u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    void algorithm(int n) {\nint a = 1;  // +0\uff08\u6280\u5de7 1\uff09\na = a + n;  // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor (int i = 0; i < 5 * n + 1; i++) {\nSystem.out.println(0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (int i = 0; i < 2 * n; i++) {\nfor (int j = 0; j < n + 1; j++) {\nSystem.out.println(0);\n}\n}\n}\n
    void algorithm(int n) {\nint a = 1;  // +0\uff08\u6280\u5de7 1\uff09\na = a + n;  // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor (int i = 0; i < 5 * n + 1; i++) {\ncout << 0 << endl;\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (int i = 0; i < 2 * n; i++) {\nfor (int j = 0; j < n + 1; j++) {\ncout << 0 << endl;\n}\n}\n}\n
    def algorithm(n: int) -> None:\na = 1      # +0\uff08\u6280\u5de7 1\uff09\na = a + n  # +0\uff08\u6280\u5de7 1\uff09\n# +n\uff08\u6280\u5de7 2\uff09\nfor i in range(5 * n + 1):\nprint(0)\n# +n*n\uff08\u6280\u5de7 3\uff09\nfor i in range(2 * n):\nfor j in range(n + 1):\nprint(0)\n
    func algorithm(n int) {\na := 1     // +0\uff08\u6280\u5de7 1\uff09\na = a + n  // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor i := 0; i < 5 * n + 1; i++ {\nfmt.Println(0)\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor i := 0; i < 2 * n; i++ {\nfor j := 0; j < n + 1; j++ {\nfmt.Println(0)\n}\n}\n}\n
    function algorithm(n) {\nlet a = 1;  // +0\uff08\u6280\u5de7 1\uff09\na = a + n;  // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor (let i = 0; i < 5 * n + 1; i++) {\nconsole.log(0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (let i = 0; i < 2 * n; i++) {\nfor (let j = 0; j < n + 1; j++) {\nconsole.log(0);\n}\n}\n}\n
    function algorithm(n: number): void {\nlet a = 1;  // +0\uff08\u6280\u5de7 1\uff09\na = a + n;  // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor (let i = 0; i < 5 * n + 1; i++) {\nconsole.log(0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (let i = 0; i < 2 * n; i++) {\nfor (let j = 0; j < n + 1; j++) {\nconsole.log(0);\n}\n}\n}\n
    void algorithm(int n) {\nint a = 1;  // +0\uff08\u6280\u5de7 1\uff09\na = a + n;  // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor (int i = 0; i < 5 * n + 1; i++) {\nprintf(\"%d\", 0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (int i = 0; i < 2 * n; i++) {\nfor (int j = 0; j < n + 1; j++) {\nprintf(\"%d\", 0);\n}\n}\n}\n
    void algorithm(int n)\n{\nint a = 1;  // +0\uff08\u6280\u5de7 1\uff09\na = a + n;  // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor (int i = 0; i < 5 * n + 1; i++)\n{\nConsole.WriteLine(0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (int i = 0; i < 2 * n; i++)\n{\nfor (int j = 0; j < n + 1; j++)\n{\nConsole.WriteLine(0);\n}\n}\n}\n
    func algorithm(n: Int) {\nvar a = 1 // +0\uff08\u6280\u5de7 1\uff09\na = a + n // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor _ in 0 ..< (5 * n + 1) {\nprint(0)\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor _ in 0 ..< (2 * n) {\nfor _ in 0 ..< (n + 1) {\nprint(0)\n}\n}\n}\n
    \n
    void algorithm(int n) {\nint a = 1; // +0\uff08\u6280\u5de7 1\uff09\na = a + n; // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor (int i = 0; i < 5 * n + 1; i++) {\nprint(0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (int i = 0; i < 2 * n; i++) {\nfor (int j = 0; j < n + 1; j++) {\nprint(0);\n}\n}\n}\n
    "},{"location":"chapter_computational_complexity/time_complexity/#_2","title":"\u7b2c\u4e8c\u6b65\uff1a\u5224\u65ad\u6e10\u8fd1\u4e0a\u754c","text":"

    \u65f6\u95f4\u590d\u6742\u5ea6\u7531\u591a\u9879\u5f0f \\(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\u88ab\u5ffd\u7565\u3002

    \u4ee5\u4e0b\u8868\u683c\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

    \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/#225","title":"2.2.5. \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\u5305\u62ec\uff08\u6309\u7167\u4ece\u4f4e\u5230\u9ad8\u7684\u987a\u5e8f\u6392\u5217\uff09\uff1a

    \\[ \\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} \\]

    Fig. \u65f6\u95f4\u590d\u6742\u5ea6\u7684\u5e38\u89c1\u7c7b\u578b

    Tip

    \u90e8\u5206\u793a\u4f8b\u4ee3\u7801\u9700\u8981\u4e00\u4e9b\u9884\u5907\u77e5\u8bc6\uff0c\u5305\u62ec\u6570\u7ec4\u3001\u9012\u5f52\u7b97\u6cd5\u7b49\u3002\u5982\u679c\u9047\u5230\u4e0d\u7406\u89e3\u7684\u90e8\u5206\uff0c\u8bf7\u4e0d\u8981\u62c5\u5fc3\uff0c\u53ef\u4ee5\u5728\u5b66\u4e60\u5b8c\u540e\u9762\u7ae0\u8282\u540e\u518d\u56de\u987e\u3002\u73b0\u9636\u6bb5\uff0c\u8bf7\u5148\u4e13\u6ce8\u4e8e\u7406\u89e3\u65f6\u95f4\u590d\u6742\u5ea6\u7684\u542b\u4e49\u548c\u63a8\u7b97\u65b9\u6cd5\u3002

    "},{"location":"chapter_computational_complexity/time_complexity/#o1","title":"\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

    \u5bf9\u4e8e\u4ee5\u4e0b\u7b97\u6cd5\uff0c\u5c3d\u7ba1\u64cd\u4f5c\u6570\u91cf size \u53ef\u80fd\u5f88\u5927\uff0c\u4f46\u7531\u4e8e\u5176\u4e0e\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart time_complexity.java
    /* \u5e38\u6570\u9636 */\nint constant(int n) {\nint count = 0;\nint size = 100000;\nfor (int i = 0; i < size; i++)\ncount++;\nreturn count;\n}\n
    time_complexity.cpp
    /* \u5e38\u6570\u9636 */\nint constant(int n) {\nint count = 0;\nint size = 100000;\nfor (int i = 0; i < size; i++)\ncount++;\nreturn count;\n}\n
    time_complexity.py
    def constant(n: int) -> int:\n\"\"\"\u5e38\u6570\u9636\"\"\"\ncount = 0\nsize = 100000\nfor _ in range(size):\ncount += 1\nreturn count\n
    time_complexity.go
    /* \u5e38\u6570\u9636 */\nfunc constant(n int) int {\ncount := 0\nsize := 100000\nfor i := 0; i < size; i++ {\ncount++\n}\nreturn count\n}\n
    time_complexity.js
    /* \u5e38\u6570\u9636 */\nfunction constant(n) {\nlet count = 0;\nconst size = 100000;\nfor (let i = 0; i < size; i++) count++;\nreturn count;\n}\n
    time_complexity.ts
    /* \u5e38\u6570\u9636 */\nfunction constant(n: number): number {\nlet count = 0;\nconst size = 100000;\nfor (let i = 0; i < size; i++) count++;\nreturn count;\n}\n
    time_complexity.c
    /* \u5e38\u6570\u9636 */\nint constant(int n) {\nint count = 0;\nint size = 100000;\nint i = 0;\nfor (int i = 0; i < size; i++) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.cs
    /* \u5e38\u6570\u9636 */\nint constant(int n) {\nint count = 0;\nint size = 100000;\nfor (int i = 0; i < size; i++)\ncount++;\nreturn count;\n}\n
    time_complexity.swift
    /* \u5e38\u6570\u9636 */\nfunc constant(n: Int) -> Int {\nvar count = 0\nlet size = 100_000\nfor _ in 0 ..< size {\ncount += 1\n}\nreturn count\n}\n
    time_complexity.zig
    // \u5e38\u6570\u9636\nfn constant(n: i32) i32 {\n_ = n;\nvar count: i32 = 0;\nconst size: i32 = 100_000;\nvar i: i32 = 0;\nwhile(i<size) : (i += 1) {\ncount += 1;\n}\nreturn count;\n}\n
    time_complexity.dart
    /* \u5e38\u6570\u9636 */\nint constant(int n) {\nint count = 0;\nint size = 100000;\nfor (var i = 0; i < size; i++) {\ncount++;\n}\nreturn count;\n}\n
    "},{"location":"chapter_computational_complexity/time_complexity/#on","title":"\u7ebf\u6027\u9636 \\(O(n)\\)","text":"

    \u7ebf\u6027\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u76f8\u5bf9\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4ee5\u7ebf\u6027\u7ea7\u522b\u589e\u957f\u3002\u7ebf\u6027\u9636\u901a\u5e38\u51fa\u73b0\u5728\u5355\u5c42\u5faa\u73af\u4e2d\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart time_complexity.java
    /* \u7ebf\u6027\u9636 */\nint linear(int n) {\nint count = 0;\nfor (int i = 0; i < n; i++)\ncount++;\nreturn count;\n}\n
    time_complexity.cpp
    /* \u7ebf\u6027\u9636 */\nint linear(int n) {\nint count = 0;\nfor (int i = 0; i < n; i++)\ncount++;\nreturn count;\n}\n
    time_complexity.py
    def linear(n: int) -> int:\n\"\"\"\u7ebf\u6027\u9636\"\"\"\ncount = 0\nfor _ in range(n):\ncount += 1\nreturn count\n
    time_complexity.go
    /* \u7ebf\u6027\u9636 */\nfunc linear(n int) int {\ncount := 0\nfor i := 0; i < n; i++ {\ncount++\n}\nreturn count\n}\n
    time_complexity.js
    /* \u7ebf\u6027\u9636 */\nfunction linear(n) {\nlet count = 0;\nfor (let i = 0; i < n; i++) count++;\nreturn count;\n}\n
    time_complexity.ts
    /* \u7ebf\u6027\u9636 */\nfunction linear(n: number): number {\nlet count = 0;\nfor (let i = 0; i < n; i++) count++;\nreturn count;\n}\n
    time_complexity.c
    /* \u7ebf\u6027\u9636 */\nint linear(int n) {\nint count = 0;\nfor (int i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.cs
    /* \u7ebf\u6027\u9636 */\nint linear(int n) {\nint count = 0;\nfor (int i = 0; i < n; i++)\ncount++;\nreturn count;\n}\n
    time_complexity.swift
    /* \u7ebf\u6027\u9636 */\nfunc linear(n: Int) -> Int {\nvar count = 0\nfor _ in 0 ..< n {\ncount += 1\n}\nreturn count\n}\n
    time_complexity.zig
    // \u7ebf\u6027\u9636\nfn linear(n: i32) i32 {\nvar count: i32 = 0;\nvar i: i32 = 0;\nwhile (i < n) : (i += 1) {\ncount += 1;\n}\nreturn count;\n}\n
    time_complexity.dart
    /* \u7ebf\u6027\u9636 */\nint linear(int n) {\nint count = 0;\nfor (var i = 0; i < n; i++) {\ncount++;\n}\nreturn 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\u3002

    \u5982\u4f55\u786e\u5b9a\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \uff1f

    \u6570\u636e\u5927\u5c0f \\(n\\) \u9700\u6839\u636e\u8f93\u5165\u6570\u636e\u7684\u7c7b\u578b\u6765\u5177\u4f53\u786e\u5b9a\u3002\u4f8b\u5982\uff0c\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u76f4\u63a5\u5c06 \\(n\\) \u89c6\u4e3a\u8f93\u5165\u6570\u636e\u5927\u5c0f\uff1b\u5728\u4e0b\u9762\u904d\u5386\u6570\u7ec4\u7684\u793a\u4f8b\u4e2d\uff0c\u6570\u636e\u5927\u5c0f \\(n\\) \u4e3a\u6570\u7ec4\u7684\u957f\u5ea6\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart time_complexity.java
    /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(int[] nums) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (int num : nums) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.cpp
    /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(vector<int> &nums) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (int num : nums) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.py
    def array_traversal(nums: list[int]) -> int:\n\"\"\"\u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09\"\"\"\ncount = 0\n# \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor num in nums:\ncount += 1\nreturn count\n
    time_complexity.go
    /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunc arrayTraversal(nums []int) int {\ncount := 0\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor range nums {\ncount++\n}\nreturn count\n}\n
    time_complexity.js
    /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunction arrayTraversal(nums) {\nlet count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (let i = 0; i < nums.length; i++) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.ts
    /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunction arrayTraversal(nums: number[]): number {\nlet count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (let i = 0; i < nums.length; i++) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.c
    /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(int *nums, int n) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (int i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.cs
    /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(int[] nums) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nforeach (int num in nums) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.swift
    /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunc arrayTraversal(nums: [Int]) -> Int {\nvar count = 0\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor _ in nums {\ncount += 1\n}\nreturn count\n}\n
    time_complexity.zig
    // \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09\nfn arrayTraversal(nums: []i32) i32 {\nvar count: i32 = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (nums) |_| {\ncount += 1;\n}\nreturn count;\n}\n
    time_complexity.dart
    /* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(List<int> nums) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (var num in nums) {\ncount++;\n}\nreturn count;\n}\n
    "},{"location":"chapter_computational_complexity/time_complexity/#on2","title":"\u5e73\u65b9\u9636 \\(O(n^2)\\)","text":"

    \u5e73\u65b9\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u76f8\u5bf9\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f\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\u90fd\u4e3a \\(O(n)\\) \uff0c\u56e0\u6b64\u603b\u4f53\u4e3a \\(O(n^2)\\) \u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart time_complexity.java
    /* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (int i = 0; i < n; i++) {\nfor (int j = 0; j < n; j++) {\ncount++;\n}\n}\nreturn count;\n}\n
    time_complexity.cpp
    /* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (int i = 0; i < n; i++) {\nfor (int j = 0; j < n; j++) {\ncount++;\n}\n}\nreturn count;\n}\n
    time_complexity.py
    def quadratic(n: int) -> int:\n\"\"\"\u5e73\u65b9\u9636\"\"\"\ncount = 0\n# \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor i in range(n):\nfor j in range(n):\ncount += 1\nreturn count\n
    time_complexity.go
    /* \u5e73\u65b9\u9636 */\nfunc quadratic(n int) int {\ncount := 0\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor i := 0; i < n; i++ {\nfor j := 0; j < n; j++ {\ncount++\n}\n}\nreturn count\n}\n
    time_complexity.js
    /* \u5e73\u65b9\u9636 */\nfunction quadratic(n) {\nlet count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (let i = 0; i < n; i++) {\nfor (let j = 0; j < n; j++) {\ncount++;\n}\n}\nreturn count;\n}\n
    time_complexity.ts
    /* \u5e73\u65b9\u9636 */\nfunction quadratic(n: number): number {\nlet count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (let i = 0; i < n; i++) {\nfor (let j = 0; j < n; j++) {\ncount++;\n}\n}\nreturn count;\n}\n
    time_complexity.c
    /* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (int i = 0; i < n; i++) {\nfor (int j = 0; j < n; j++) {\ncount++;\n}\n}\nreturn count;\n}\n
    time_complexity.cs
    /* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (int i = 0; i < n; i++) {\nfor (int j = 0; j < n; j++) {\ncount++;\n}\n}\nreturn count;\n}\n
    time_complexity.swift
    /* \u5e73\u65b9\u9636 */\nfunc quadratic(n: Int) -> Int {\nvar count = 0\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor _ in 0 ..< n {\nfor _ in 0 ..< n {\ncount += 1\n}\n}\nreturn count\n}\n
    time_complexity.zig
    // \u5e73\u65b9\u9636\nfn quadratic(n: i32) i32 {\nvar count: i32 = 0;\nvar i: i32 = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nwhile (i < n) : (i += 1) {\nvar j: i32 = 0;\nwhile (j < n) : (j += 1) {\ncount += 1;\n}\n}\nreturn count;\n}\n
    time_complexity.dart
    /* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (int i = 0; i < n; i++) {\nfor (int j = 0; j < n; j++) {\ncount++;\n}\n}\nreturn count;\n}\n

    Fig. \u5e38\u6570\u9636\u3001\u7ebf\u6027\u9636\u3001\u5e73\u65b9\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

    \u4ee5\u300c\u5192\u6ce1\u6392\u5e8f\u300d\u4e3a\u4f8b\uff0c\u5916\u5c42\u5faa\u73af\u6267\u884c \\(n - 1\\) \u6b21\uff0c\u5185\u5c42\u5faa\u73af\u6267\u884c \\(n-1, n-2, \\cdots, 2, 1\\) \u6b21\uff0c\u5e73\u5747\u4e3a \\(\\frac{n}{2}\\) \u6b21\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002

    \\[ O((n - 1) \\frac{n}{2}) = O(n^2) \\] JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart time_complexity.java
    /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(int[] nums) {\nint count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (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 \nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n
    time_complexity.cpp
    /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(vector<int> &nums) {\nint count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (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 \nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n
    time_complexity.py
    def bubble_sort(nums: list[int]) -> int:\n\"\"\"\u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09\"\"\"\ncount = 0  # \u8ba1\u6570\u5668\n# \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor 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 \nfor j in range(i):\nif nums[j] > nums[j + 1]:\n# \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\ntmp: int = nums[j]\nnums[j] = nums[j + 1]\nnums[j + 1] = tmp\ncount += 3  # \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\nreturn count\n
    time_complexity.go
    /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunc bubbleSort(nums []int) int {\ncount := 0 // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor 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\nfor j := 0; j < i; j++ {\nif nums[j] > nums[j+1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\ntmp := nums[j]\nnums[j] = nums[j+1]\nnums[j+1] = tmp\ncount += 3 // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count\n}\n
    time_complexity.js
    /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunction bubbleSort(nums) {\nlet count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (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 \nfor (let j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n
    time_complexity.ts
    /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunction bubbleSort(nums: number[]): number {\nlet count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (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 \nfor (let j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n
    time_complexity.c
    /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(int *nums, int n) {\nint count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (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 \nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n
    time_complexity.cs
    /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(int[] nums) {\nint count = 0;  // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (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 \nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3;  // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n
    time_complexity.swift
    /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunc bubbleSort(nums: inout [Int]) -> Int {\nvar count = 0 // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor 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 \nfor j in 0 ..< i {\nif nums[j] > nums[j + 1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j]\nnums[j] = nums[j + 1]\nnums[j + 1] = tmp\ncount += 3 // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count\n}\n
    time_complexity.zig
    // \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09\nfn bubbleSort(nums: []i32) i32 {\nvar count: i32 = 0;  // \u8ba1\u6570\u5668 \n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nvar i: i32 = @intCast(i32, nums.len ) - 1;\nwhile (i > 0) : (i -= 1) {\nvar 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 \nwhile (j < i) : (j += 1) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nvar tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3;  // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n
    time_complexity.dart
    /* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(List<int> nums) {\nint count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (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\nfor (var j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n
    "},{"location":"chapter_computational_complexity/time_complexity/#o2n","title":"\u6307\u6570\u9636 \\(O(2^n)\\)","text":"

    Note

    \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

    \u6307\u6570\u9636\u589e\u957f\u975e\u5e38\u8fc5\u901f\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u901a\u5e38\u662f\u4e0d\u53ef\u63a5\u53d7\u7684\u3002\u82e5\u4e00\u4e2a\u95ee\u9898\u4f7f\u7528\u300c\u66b4\u529b\u679a\u4e3e\u300d\u6c42\u89e3\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \uff0c\u90a3\u4e48\u901a\u5e38\u9700\u8981\u4f7f\u7528\u300c\u52a8\u6001\u89c4\u5212\u300d\u6216\u300c\u8d2a\u5fc3\u7b97\u6cd5\u300d\u7b49\u65b9\u6cd5\u6765\u89e3\u51b3\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart time_complexity.java
    /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\nint count = 0, base = 1;\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (int i = 0; i < n; i++) {\nfor (int j = 0; j < base; j++) {\ncount++;\n}\nbase *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n
    time_complexity.cpp
    /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\nint count = 0, base = 1;\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (int i = 0; i < n; i++) {\nfor (int j = 0; j < base; j++) {\ncount++;\n}\nbase *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n
    time_complexity.py
    def exponential(n: int) -> int:\n\"\"\"\u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\"\"\"\ncount = 0\nbase = 1\n# cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor _ in range(n):\nfor _ in range(base):\ncount += 1\nbase *= 2\n# count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count\n
    time_complexity.go
    /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09*/\nfunc exponential(n int) int {\ncount, base := 0, 1\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor i := 0; i < n; i++ {\nfor j := 0; j < base; j++ {\ncount++\n}\nbase *= 2\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count\n}\n
    time_complexity.js
    /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction exponential(n) {\nlet count = 0,\nbase = 1;\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (let i = 0; i < n; i++) {\nfor (let j = 0; j < base; j++) {\ncount++;\n}\nbase *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n
    time_complexity.ts
    /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction exponential(n: number): number {\nlet count = 0,\nbase = 1;\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (let i = 0; i < n; i++) {\nfor (let j = 0; j < base; j++) {\ncount++;\n}\nbase *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n
    time_complexity.c
    /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\nint count = 0;\nint bas = 1;\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (int i = 0; i < n; i++) {\nfor (int j = 0; j < bas; j++) {\ncount++;\n}\nbas *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n
    time_complexity.cs
    /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\nint count = 0, bas = 1;\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (int i = 0; i < n; i++) {\nfor (int j = 0; j < bas; j++) {\ncount++;\n}\nbas *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n
    time_complexity.swift
    /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunc exponential(n: Int) -> Int {\nvar count = 0\nvar base = 1\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor _ in 0 ..< n {\nfor _ in 0 ..< base {\ncount += 1\n}\nbase *= 2\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count\n}\n
    time_complexity.zig
    // \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\nfn exponential(n: i32) i32 {\nvar count: i32 = 0;\nvar bas: i32 = 1;\nvar i: i32 = 0;\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nwhile (i < n) : (i += 1) {\nvar j: i32 = 0;\nwhile (j < bas) : (j += 1) {\ncount += 1;\n}\nbas *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n
    time_complexity.dart
    /* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\nint count = 0, base = 1;\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (var i = 0; i < n; i++) {\nfor (var j = 0; j < base; j++) {\ncount++;\n}\nbase *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n

    Fig. \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\u3002\u4f8b\u5982\u4ee5\u4e0b\u4ee3\u7801\uff0c\u4e0d\u65ad\u5730\u4e00\u5206\u4e3a\u4e8c\uff0c\u7ecf\u8fc7 \\(n\\) \u6b21\u5206\u88c2\u540e\u505c\u6b62\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart time_complexity.java
    /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\nif (n == 1)\nreturn 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
    time_complexity.cpp
    /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\nif (n == 1)\nreturn 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
    time_complexity.py
    def exp_recur(n: int) -> int:\n\"\"\"\u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\nif n == 1:\nreturn 1\nreturn exp_recur(n - 1) + exp_recur(n - 1) + 1\n
    time_complexity.go
    /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09*/\nfunc expRecur(n int) int {\nif n == 1 {\nreturn 1\n}\nreturn expRecur(n-1) + expRecur(n-1) + 1\n}\n
    time_complexity.js
    /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction expRecur(n) {\nif (n == 1) return 1;\nreturn 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 {\nif (n == 1) return 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
    time_complexity.c
    /* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\nif (n == 1)\nreturn 1;\nreturn 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) {\nif (n == 1) return 1;\nreturn 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 {\nif n == 1 {\nreturn 1\n}\nreturn expRecur(n: n - 1) + expRecur(n: n - 1) + 1\n}\n
    time_complexity.zig
    // \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn expRecur(n: i32) i32 {\nif (n == 1) return 1;\nreturn 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) {\nif (n == 1) return 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
    "},{"location":"chapter_computational_complexity/time_complexity/#olog-n","title":"\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\u7684\u60c5\u51b5\u201d\u3002\u5bf9\u6570\u9636\u4ec5\u6b21\u4e8e\u5e38\u6570\u9636\uff0c\u65f6\u95f4\u589e\u957f\u7f13\u6162\uff0c\u662f\u7406\u60f3\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002

    \u5bf9\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u300c\u4e8c\u5206\u67e5\u627e\u300d\u548c\u300c\u5206\u6cbb\u7b97\u6cd5\u300d\u4e2d\uff0c\u4f53\u73b0\u4e86\u201c\u4e00\u5206\u4e3a\u591a\u201d\u548c\u201c\u5316\u7e41\u4e3a\u7b80\u201d\u7684\u7b97\u6cd5\u601d\u60f3\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart time_complexity.java
    /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(float n) {\nint count = 0;\nwhile (n > 1) {\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n
    time_complexity.cpp
    /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(float n) {\nint count = 0;\nwhile (n > 1) {\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n
    time_complexity.py
    def logarithmic(n: float) -> int:\n\"\"\"\u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\"\"\"\ncount = 0\nwhile n > 1:\nn = n / 2\ncount += 1\nreturn count\n
    time_complexity.go
    /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09*/\nfunc logarithmic(n float64) int {\ncount := 0\nfor n > 1 {\nn = n / 2\ncount++\n}\nreturn count\n}\n
    time_complexity.js
    /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction logarithmic(n) {\nlet count = 0;\nwhile (n > 1) {\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n
    time_complexity.ts
    /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction logarithmic(n: number): number {\nlet count = 0;\nwhile (n > 1) {\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n
    time_complexity.c
    /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(float n) {\nint count = 0;\nwhile (n > 1) {\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n
    time_complexity.cs
    /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(float n) {\nint count = 0;\nwhile (n > 1) {\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n
    time_complexity.swift
    /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunc logarithmic(n: Double) -> Int {\nvar count = 0\nvar n = n\nwhile n > 1 {\nn = n / 2\ncount += 1\n}\nreturn count\n}\n
    time_complexity.zig
    // \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\nfn logarithmic(n: f32) i32 {\nvar count: i32 = 0;\nvar n_var = n;\nwhile (n_var > 1)\n{\nn_var = n_var / 2;\ncount +=1;\n}\nreturn count;\n}\n
    time_complexity.dart
    /* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(num n) {\nint count = 0;\nwhile (n > 1) {\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n

    Fig. \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\u3002\u4ee5\u4e0b\u4ee3\u7801\u5f62\u6210\u4e86\u4e00\u4e2a\u9ad8\u5ea6\u4e3a \\(\\log_2 n\\) \u7684\u9012\u5f52\u6811\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart time_complexity.java
    /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(float n) {\nif (n <= 1)\nreturn 0;\nreturn logRecur(n / 2) + 1;\n}\n
    time_complexity.cpp
    /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(float n) {\nif (n <= 1)\nreturn 0;\nreturn logRecur(n / 2) + 1;\n}\n
    time_complexity.py
    def log_recur(n: float) -> int:\n\"\"\"\u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\nif n <= 1:\nreturn 0\nreturn log_recur(n / 2) + 1\n
    time_complexity.go
    /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09*/\nfunc logRecur(n float64) int {\nif n <= 1 {\nreturn 0\n}\nreturn logRecur(n/2) + 1\n}\n
    time_complexity.js
    /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction logRecur(n) {\nif (n <= 1) return 0;\nreturn logRecur(n / 2) + 1;\n}\n
    time_complexity.ts
    /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction logRecur(n: number): number {\nif (n <= 1) return 0;\nreturn logRecur(n / 2) + 1;\n}\n
    time_complexity.c
    /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(float n) {\nif (n <= 1)\nreturn 0;\nreturn logRecur(n / 2) + 1;\n}\n
    time_complexity.cs
    /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(float n) {\nif (n <= 1) return 0;\nreturn logRecur(n / 2) + 1;\n}\n
    time_complexity.swift
    /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc logRecur(n: Double) -> Int {\nif n <= 1 {\nreturn 0\n}\nreturn logRecur(n: n / 2) + 1\n}\n
    time_complexity.zig
    // \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn logRecur(n: f32) i32 {\nif (n <= 1) return 0;\nreturn logRecur(n / 2) + 1;\n}\n
    time_complexity.dart
    /* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(num n) {\nif (n <= 1) return 0;\nreturn logRecur(n / 2) + 1;\n}\n
    "},{"location":"chapter_computational_complexity/time_complexity/#on-log-n","title":"\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

    \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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart time_complexity.java
    /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(float n) {\nif (n <= 1)\nreturn 1;\nint count = linearLogRecur(n / 2) +\nlinearLogRecur(n / 2);\nfor (int i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.cpp
    /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(float n) {\nif (n <= 1)\nreturn 1;\nint count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\nfor (int i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.py
    def linear_log_recur(n: float) -> int:\n\"\"\"\u7ebf\u6027\u5bf9\u6570\u9636\"\"\"\nif n <= 1:\nreturn 1\ncount: int = linear_log_recur(n // 2) + linear_log_recur(n // 2)\nfor _ in range(n):\ncount += 1\nreturn count\n
    time_complexity.go
    /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunc linearLogRecur(n float64) int {\nif n <= 1 {\nreturn 1\n}\ncount := linearLogRecur(n/2) +\nlinearLogRecur(n/2)\nfor i := 0.0; i < n; i++ {\ncount++\n}\nreturn count\n}\n
    time_complexity.js
    /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunction linearLogRecur(n) {\nif (n <= 1) return 1;\nlet count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\nfor (let i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.ts
    /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunction linearLogRecur(n: number): number {\nif (n <= 1) return 1;\nlet count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\nfor (let i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.c
    /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(float n) {\nif (n <= 1)\nreturn 1;\nint count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\nfor (int i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.cs
    /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(float n) {\nif (n <= 1) return 1;\nint count = linearLogRecur(n / 2) +\nlinearLogRecur(n / 2);\nfor (int i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n
    time_complexity.swift
    /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunc linearLogRecur(n: Double) -> Int {\nif n <= 1 {\nreturn 1\n}\nvar count = linearLogRecur(n: n / 2) + linearLogRecur(n: n / 2)\nfor _ in stride(from: 0, to: n, by: 1) {\ncount += 1\n}\nreturn count\n}\n
    time_complexity.zig
    // \u7ebf\u6027\u5bf9\u6570\u9636\nfn linearLogRecur(n: f32) i32 {\nif (n <= 1) return 1;\nvar count: i32 = linearLogRecur(n / 2) +\nlinearLogRecur(n / 2);\nvar i: f32 = 0;\nwhile (i < n) : (i += 1) {\ncount += 1;\n}\nreturn count;\n}\n
    time_complexity.dart
    /* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(num n) {\nif (n <= 1) return 1;\nint count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\nfor (var i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n

    Fig. \u7ebf\u6027\u5bf9\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

    "},{"location":"chapter_computational_complexity/time_complexity/#on_1","title":"\u9636\u4e58\u9636 \\(O(n!)\\)","text":"

    \u9636\u4e58\u9636\u5bf9\u5e94\u6570\u5b66\u4e0a\u7684\u300c\u5168\u6392\u5217\u300d\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 \\cdots \\times 2 \\times 1 \\]

    \u9636\u4e58\u901a\u5e38\u4f7f\u7528\u9012\u5f52\u5b9e\u73b0\u3002\u4f8b\u5982\u4ee5\u4e0b\u4ee3\u7801\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\u7ec8\u6b62\u5206\u88c2\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart time_complexity.java
    /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\nif (n == 0)\nreturn 1;\nint count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor (int i = 0; i < n; i++) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n
    time_complexity.cpp
    /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\nif (n == 0)\nreturn 1;\nint count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor (int i = 0; i < n; i++) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n
    time_complexity.py
    def factorial_recur(n: int) -> int:\n\"\"\"\u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\nif n == 0:\nreturn 1\ncount = 0\n# \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor _ in range(n):\ncount += factorial_recur(n - 1)\nreturn count\n
    time_complexity.go
    /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc factorialRecur(n int) int {\nif n == 0 {\nreturn 1\n}\ncount := 0\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor i := 0; i < n; i++ {\ncount += factorialRecur(n - 1)\n}\nreturn count\n}\n
    time_complexity.js
    /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction factorialRecur(n) {\nif (n == 0) return 1;\nlet count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor (let i = 0; i < n; i++) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n
    time_complexity.ts
    /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction factorialRecur(n: number): number {\nif (n == 0) return 1;\nlet count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor (let i = 0; i < n; i++) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n
    time_complexity.c
    /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\nif (n == 0)\nreturn 1;\nint count = 0;\nfor (int i = 0; i < n; i++) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n
    time_complexity.cs
    /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\nif (n == 0) return 1;\nint count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor (int i = 0; i < n; i++) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n
    time_complexity.swift
    /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc factorialRecur(n: Int) -> Int {\nif n == 0 {\nreturn 1\n}\nvar count = 0\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor _ in 0 ..< n {\ncount += factorialRecur(n: n - 1)\n}\nreturn count\n}\n
    time_complexity.zig
    // \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn factorialRecur(n: i32) i32 {\nif (n == 0) return 1;\nvar count: i32 = 0;\nvar i: i32 = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nwhile (i < n) : (i += 1) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n
    time_complexity.dart
    /* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\nif (n == 0) return 1;\nint count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor (var i = 0; i < n; i++) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n

    Fig. \u9636\u4e58\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

    "},{"location":"chapter_computational_complexity/time_complexity/#226","title":"2.2.6. \u00a0 \u6700\u5dee\u3001\u6700\u4f73\u3001\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6","text":"

    \u67d0\u4e9b\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u662f\u56fa\u5b9a\u7684\uff0c\u800c\u662f\u4e0e\u8f93\u5165\u6570\u636e\u7684\u5206\u5e03\u6709\u5173\u3002\u4f8b\u5982\uff0c\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\u4f46\u5143\u7d20\u987a\u5e8f\u662f\u968f\u673a\u6253\u4e71\u7684\uff1b\u7b97\u6cd5\u7684\u4efb\u52a1\u662f\u8fd4\u56de\u5143\u7d20 \\(1\\) \u7684\u7d22\u5f15\u3002\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\uff1a

    • \u5f53 nums = [?, ?, ..., 1] \uff0c\u5373\u5f53\u672b\u5c3e\u5143\u7d20\u662f \\(1\\) \u65f6\uff0c\u9700\u8981\u5b8c\u6574\u904d\u5386\u6570\u7ec4\uff0c\u6b64\u65f6\u8fbe\u5230 \u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\)\uff1b
    • \u5f53 nums = [1, ?, ?, ...] \uff0c\u5373\u5f53\u9996\u4e2a\u6570\u5b57\u4e3a \\(1\\) \u65f6\uff0c\u65e0\u8bba\u6570\u7ec4\u591a\u957f\u90fd\u4e0d\u9700\u8981\u7ee7\u7eed\u904d\u5386\uff0c\u6b64\u65f6\u8fbe\u5230 \u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 \\(\\Omega(1)\\)\uff1b

    \u201c\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c\u201d\u4f7f\u7528\u5927 \\(O\\) \u8bb0\u53f7\u8868\u793a\uff0c\u4ee3\u8868\u300c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u300d\u3002\u76f8\u5e94\u5730\uff0c\u201c\u51fd\u6570\u6e10\u8fd1\u4e0b\u754c\u201d\u7528 \\(\\Omega\\) \u8bb0\u53f7\u6765\u8868\u793a\uff0c\u4ee3\u8868\u300c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u300d\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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) {\nInteger[] nums = new Integer[n];\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (int i = 0; i < n; i++) {\nnums[i] = i + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nCollections.shuffle(Arrays.asList(nums));\n// Integer[] -> int[]\nint[] res = new int[n];\nfor (int i = 0; i < n; i++) {\nres[i] = nums[i];\n}\nreturn res;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int[] nums) {\nfor (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)\nif (nums[i] == 1)\nreturn i;\n}\nreturn -1;\n}\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) {\nvector<int> nums(n);\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (int i = 0; i < n; i++) {\nnums[i] = i + 1;\n}\n// \u4f7f\u7528\u7cfb\u7edf\u65f6\u95f4\u751f\u6210\u968f\u673a\u79cd\u5b50\nunsigned seed = chrono::system_clock::now().time_since_epoch().count();\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nshuffle(nums.begin(), nums.end(), default_random_engine(seed));\nreturn nums;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(vector<int> &nums) {\nfor (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)\nif (nums[i] == 1)\nreturn i;\n}\nreturn -1;\n}\n
    worst_best_time_complexity.py
    def 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\nnums = [i for i in range(1, n + 1)]\n# \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nrandom.shuffle(nums)\nreturn nums\ndef find_one(nums: list[int]) -> int:\n\"\"\"\u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15\"\"\"\nfor 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)\nif nums[i] == 1:\nreturn i\nreturn -1\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 {\nnums := make([]int, n)\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor i := 0; i < n; i++ {\nnums[i] = i + 1\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nrand.Shuffle(len(nums), func(i, j int) {\nnums[i], nums[j] = nums[j], nums[i]\n})\nreturn nums\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunc findOne(nums []int) int {\nfor 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)\nif nums[i] == 1 {\nreturn i\n}\n}\nreturn -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) {\nconst nums = Array(n);\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (let i = 0; i < n; i++) {\nnums[i] = i + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nfor (let i = 0; i < n; i++) {\nconst r = Math.floor(Math.random() * (i + 1));\nconst temp = nums[i];\nnums[i] = nums[r];\nnums[r] = temp;\n}\nreturn nums;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunction findOne(nums) {\nfor (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)\nif (nums[i] === 1) {\nreturn i;\n}\n}\nreturn -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[] {\nconst nums = Array(n);\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (let i = 0; i < n; i++) {\nnums[i] = i + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nfor (let i = 0; i < n; i++) {\nconst r = Math.floor(Math.random() * (i + 1));\nconst temp = nums[i];\nnums[i] = nums[r];\nnums[r] = temp;\n}\nreturn nums;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunction findOne(nums: number[]): number {\nfor (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)\nif (nums[i] === 1) {\nreturn i;\n}\n}\nreturn -1;\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\u4e3an\uff0c\u5143\u7d20\u7c7b\u578b\u4e3aint\uff09\nint *nums = (int *)malloc(n * sizeof(int));\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (int i = 0; i < n; i++) {\nnums[i] = i + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nfor (int i = n - 1; i > 0; i--) {\nint j = rand() % (i + 1);\nint temp = nums[i];\nnums[i] = nums[j];\nnums[j] = temp;\n}\nreturn nums;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int *nums, int n) {\nfor (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)\nif (nums[i] == 1)\nreturn i;\n}\nreturn -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) {\nint[] nums = new int[n];\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (int i = 0; i < n; i++) {\nnums[i] = i + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nfor (int i = 0; i < nums.Length; i++) {\nvar index = new Random().Next(i, nums.Length);\nvar tmp = nums[i];\nvar ran = nums[index];\nnums[i] = ran;\nnums[index] = tmp;\n}\nreturn nums;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int[] nums) {\nfor (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)\nif (nums[i] == 1)\nreturn i;\n}\nreturn -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 }\nvar nums = Array(1 ... n)\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nnums.shuffle()\nreturn nums\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunc findOne(nums: [Int]) -> Int {\nfor 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)\nif nums[i] == 1 {\nreturn i\n}\n}\nreturn -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\npub fn randomNumbers(comptime n: usize) [n]i32 {\nvar nums: [n]i32 = undefined;\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (nums) |*num, i| {\nnum.* = @intCast(i32, i) + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nconst rand = std.crypto.random;\nrand.shuffle(i32, &nums);\nreturn nums;\n}\n// \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15\npub fn findOne(nums: []i32) i32 {\nfor (nums) |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)\nif (num == 1) return @intCast(i32, i);\n}\nreturn -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) {\nfinal nums = List.filled(n, 0);\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (var i = 0; i < n; i++) {\nnums[i] = i + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nnums.shuffle();\nreturn nums;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(List<int> nums) {\nfor (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)\nif (nums[i] == 1) return i;\n}\nreturn -1;\n}\n

    Tip

    \u5b9e\u9645\u5e94\u7528\u4e2d\u6211\u4eec\u5f88\u5c11\u4f7f\u7528\u300c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u300d\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\u76f8\u53cd\uff0c\u300c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u300d\u66f4\u4e3a\u5b9e\u7528\uff0c\u56e0\u4e3a\u5b83\u7ed9\u51fa\u4e86\u4e00\u4e2a\u201c\u6548\u7387\u5b89\u5168\u503c\u201d\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\u6216\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u53ea\u51fa\u73b0\u5728\u201c\u7279\u6b8a\u5206\u5e03\u7684\u6570\u636e\u201d\u4e2d\uff0c\u8fd9\u4e9b\u60c5\u51b5\u7684\u51fa\u73b0\u6982\u7387\u53ef\u80fd\u5f88\u5c0f\uff0c\u56e0\u6b64\u5e76\u4e0d\u80fd\u6700\u771f\u5b9e\u5730\u53cd\u6620\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u3002\u76f8\u8f83\u4e4b\u4e0b\uff0c\u300c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u300d\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\u5219\u662f\u6570\u7ec4\u957f\u5ea6\u7684\u4e00\u534a \\(\\frac{n}{2}\\) \uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(\\Theta(\\frac{n}{2}) = \\Theta(n)\\) \u3002

    \u4f46\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5c24\u5176\u662f\u8f83\u4e3a\u590d\u6742\u7684\u7b97\u6cd5\uff0c\u8ba1\u7b97\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u6bd4\u8f83\u56f0\u96be\uff0c\u56e0\u4e3a\u5f88\u96be\u7b80\u4fbf\u5730\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\u6211\u4eec\u5e38\u5e38\u4f7f\u7528\u5b83\u6765\u8868\u793a\u300c\u5e73\u5747\u590d\u6742\u5ea6\u300d\uff0c\u4f46\u4ece\u4e25\u683c\u610f\u4e49\u4e0a\u770b\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":"3. \u00a0 \u6570\u636e\u7ed3\u6784\u7b80\u4ecb","text":""},{"location":"chapter_data_structure/basic_data_types/","title":"3.2. \u00a0 \u57fa\u672c\u6570\u636e\u7c7b\u578b","text":"

    \u8c08\u53ca\u8ba1\u7b97\u673a\u4e2d\u7684\u6570\u636e\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\u3002\u5b83\u5305\u62ec\uff1a

    • \u6574\u6570\u7c7b\u578b byte , short , int , long \uff1b
    • \u6d6e\u70b9\u6570\u7c7b\u578b float , double \uff0c\u7528\u4e8e\u8868\u793a\u5c0f\u6570\uff1b
    • \u5b57\u7b26\u7c7b\u578b char \uff0c\u7528\u4e8e\u8868\u793a\u5404\u79cd\u8bed\u8a00\u7684\u5b57\u6bcd\u3001\u6807\u70b9\u7b26\u53f7\u3001\u751a\u81f3\u8868\u60c5\u7b26\u53f7\u7b49\uff1b
    • \u5e03\u5c14\u7c7b\u578b bool \uff0c\u7528\u4e8e\u8868\u793a\u201c\u662f\u201d\u4e0e\u201c\u5426\u201d\u5224\u65ad\uff1b

    \u6240\u6709\u57fa\u672c\u6570\u636e\u7c7b\u578b\u90fd\u4ee5\u4e8c\u8fdb\u5236\u7684\u5f62\u5f0f\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u4e2d\u3002\u5728\u8ba1\u7b97\u673a\u4e2d\uff0c\u6211\u4eec\u5c06 \\(1\\) \u4e2a\u4e8c\u8fdb\u5236\u4f4d\u79f0\u4e3a \\(1\\) \u6bd4\u7279\uff0c\u5e76\u89c4\u5b9a \\(1\\) \u5b57\u8282\uff08byte\uff09\u7531 \\(8\\) \u6bd4\u7279\uff08bits\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\uff0c\u4f8b\u5982\uff1a

    • \u6574\u6570\u7c7b\u578b byte \u5360\u7528 \\(1\\) byte = \\(8\\) bits \uff0c\u53ef\u4ee5\u8868\u793a \\(2^{8}\\) \u4e2a\u4e0d\u540c\u7684\u6570\u5b57\uff1b
    • \u6574\u6570\u7c7b\u578b int \u5360\u7528 \\(4\\) bytes = \\(32\\) bits \uff0c\u53ef\u4ee5\u8868\u793a \\(2^{32}\\) \u4e2a\u6570\u5b57\uff1b

    \u4e0b\u8868\u5217\u4e3e\u4e86\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\u9700\u786c\u80cc\uff0c\u5927\u81f4\u7406\u89e3\u5373\u53ef\uff0c\u9700\u8981\u65f6\u53ef\u4ee5\u901a\u8fc7\u67e5\u8868\u6765\u56de\u5fc6\u3002

    \u7c7b\u578b \u7b26\u53f7 \u5360\u7528\u7a7a\u95f4 \u6700\u5c0f\u503c \u6700\u5927\u503c \u9ed8\u8ba4\u503c \u6574\u6570 byte 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 f\\) double 8 bytes \\(2.225 \\times 10^{-308}\\) \\(1.798 \\times 10^{308}\\) \\(0.0\\) \u5b57\u7b26 char 2 bytes / 1 byte \\(0\\) \\(2^{16} - 1\\) \\(0\\) \u5e03\u5c14 bool 1 byte \\(\\text{false}\\) \\(\\text{true}\\) \\(\\text{false}\\)

    Tip

    \u5b57\u7b26\u7684\u5360\u7528\u7a7a\u95f4\u5927\u5c0f\u53d6\u51b3\u4e8e\u7f16\u7a0b\u8bed\u8a00\u91c7\u7528\u7684\u5b57\u7b26\u7f16\u7801\u65b9\u6cd5\uff0c\u8be6\u89c1\u300c\u5b57\u7b26\u7f16\u7801\u300d\u7ae0\u8282\u3002

    \u73b0\u4ee3\u8ba1\u7b97\u673a CPU \u901a\u5e38\u5c06 1 \u5b57\u8282\u4f5c\u4e3a\u6700\u5c0f\u5bfb\u5740\u5185\u5b58\u5355\u5143\u3002\u56e0\u6b64\uff0c\u5373\u4f7f\u8868\u793a\u5e03\u5c14\u91cf\u4ec5\u9700 1 \u4f4d\uff08\\(0\\) \u6216 \\(1\\)\uff09\uff0c\u5b83\u5728\u5185\u5b58\u4e2d\u901a\u5e38\u88ab\u5b58\u50a8\u4e3a 1 \u5b57\u8282\u3002

    \u90a3\u4e48\uff0c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e0e\u6570\u636e\u7ed3\u6784\u4e4b\u95f4\u6709\u4ec0\u4e48\u8054\u7cfb\u4e0e\u533a\u522b\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\u5b83\u7684\u4e3b\u8bed\u662f\u201c\u7ed3\u6784\u201d\uff0c\u800c\u975e\u201c\u6570\u636e\u201d\u3002\u5982\u679c\u60f3\u8981\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 \u3001\u8fd8\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\u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    /* \u4f7f\u7528\u591a\u79cd\u300c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u300d\u6765\u521d\u59cb\u5316\u300c\u6570\u7ec4\u300d */\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nboolean[] booleans = new boolean[5];\n
    /* \u4f7f\u7528\u591a\u79cd\u300c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u300d\u6765\u521d\u59cb\u5316\u300c\u6570\u7ec4\u300d */\nint numbers[5];\nfloat decimals[5];\nchar characters[5];\nbool booleans[5];\n
    # Python \u7684 list \u53ef\u4ee5\u81ea\u7531\u5b58\u50a8\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5bf9\u8c61\nlist = [0, 0.0, 'a', False]\n
    // \u4f7f\u7528\u591a\u79cd\u300c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u300d\u6765\u521d\u59cb\u5316\u300c\u6570\u7ec4\u300d\nvar numbers = [5]int{}\nvar decimals = [5]float64{}\nvar characters = [5]byte{}\nvar booleans = [5]bool{}\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\u300c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u300d\u6765\u521d\u59cb\u5316\u300c\u6570\u7ec4\u300d */\nconst numbers: number[] = [];\nconst characters: string[] = [];\nconst booleans: boolean[] = [];\n
    /* \u4f7f\u7528\u591a\u79cd\u300c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u300d\u6765\u521d\u59cb\u5316\u300c\u6570\u7ec4\u300d */\nint numbers[10];\nfloat decimals[10];\nchar characters[10];\nbool booleans[10];\n
    /* \u4f7f\u7528\u591a\u79cd\u300c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u300d\u6765\u521d\u59cb\u5316\u300c\u6570\u7ec4\u300d */\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nbool[] booleans = new bool[5];\n
    /* \u4f7f\u7528\u591a\u79cd\u300c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u300d\u6765\u521d\u59cb\u5316\u300c\u6570\u7ec4\u300d */\nlet numbers = Array(repeating: Int(), count: 5)\nlet decimals = Array(repeating: Double(), count: 5)\nlet characters = Array(repeating: Character(\"a\"), count: 5)\nlet booleans = Array(repeating: Bool(), count: 5)\n
    \n
    /* \u4f7f\u7528\u591a\u79cd\u300c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u300d\u6765\u521d\u59cb\u5316\u300c\u6570\u7ec4\u300d */\nList<int> numbers = List.filled(5, 0);\nList<double> decimals = List.filled(5, 0.0);\nList<String> characters = List.filled(5, 'a');\nList<bool> booleans = List.filled(5, false);\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\u300c\u5b57\u7b26\u96c6\u300d\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

    "},{"location":"chapter_data_structure/character_encoding/#341-ascii","title":"3.4.1. \u00a0 ASCII \u5b57\u7b26\u96c6","text":"

    \u300cASCII \u7801\u300d\u662f\u6700\u65e9\u51fa\u73b0\u7684\u5b57\u7b26\u96c6\uff0c\u5168\u79f0\u4e3a\u201c\u7f8e\u56fd\u6807\u51c6\u4fe1\u606f\u4ea4\u6362\u4ee3\u7801\u201d\u3002\u5b83\u4f7f\u7528 7 \u4f4d\u4e8c\u8fdb\u5236\u6570\uff08\u5373\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\u8fd9\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

    Fig. 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\u4f8b\u5982\uff0c\u6c49\u5b57\u5927\u7ea6\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\u7684\u7f55\u89c1\u5b57\u548c\u7e41\u4f53\u5b57\u3002\u4e4b\u540e\u5728 GB2312 \u7684\u57fa\u7840\u4e0a\uff0c\u6269\u5c55\u5f97\u5230\u4e86\u300cGBK\u300d\u5b57\u7b26\u96c6\uff0c\u5b83\u5171\u6536\u5f55\u4e86 21886 \u4e2a\u6c49\u5b57\u3002\u5728 GBK \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\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\uff1b\u53e6\u4e00\u65b9\u9762\uff0c\u540c\u4e00\u79cd\u8bed\u8a00\u4e5f\u5b58\u5728\u591a\u79cd\u5b57\u7b26\u96c6\u6807\u51c6\uff0c\u5982\u679c\u4e24\u53f0\u7535\u8111\u5b89\u88c5\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\u4eba\u4eec\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\u5168\u79f0\u4e3a\u201c\u7edf\u4e00\u5b57\u7b26\u7f16\u7801\u201d\uff0c\u7406\u8bba\u4e0a\u80fd\u5bb9\u7eb3\u4e00\u767e\u591a\u4e07\u4e2a\u5b57\u7b26\u3002\u5b83\u81f4\u529b\u4e8e\u5c06\u5168\u7403\u8303\u56f4\u5185\u7684\u5b57\u7b26\u7eb3\u5165\u5230\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\u6b62 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\u3001\u751a\u81f3\u662f\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 3 \u5b57\u8282\u751a\u81f3 4 \u5b57\u8282\u3002

    Unicode \u662f\u4e00\u79cd\u5b57\u7b26\u96c6\u6807\u51c6\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\u540c\u4e00\u4e2a\u6587\u672c\u4e2d\u65f6\uff0c\u7cfb\u7edf\u5982\u4f55\u89e3\u6790\u5b57\u7b26\uff1f\u4f8b\u5982\uff0c\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\u4e0b\u56fe\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 \uff0c\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\u51fa\u8fd9\u4e2a\u77ed\u8bed\u7684\u5185\u5bb9\u4e86\u3002

    Fig. Unicode \u7f16\u7801\u793a\u4f8b

    \u7136\u800c\uff0cASCII \u7801\u5df2\u7ecf\u5411\u6211\u4eec\u8bc1\u660e\uff0c\u7f16\u7801\u82f1\u6587\u53ea\u9700\u8981 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\u5927\u5c0f\u7684 2 \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\u7684\u7f16\u7801\uff0c\u4f7f\u7528 1 \u5230 4 \u4e2a\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\u8981 1 \u4e2a\u5b57\u8282\uff0c\u62c9\u4e01\u5b57\u6bcd\u548c\u5e0c\u814a\u5b57\u6bcd\u9700\u8981 2 \u4e2a\u5b57\u8282\uff0c\u5e38\u7528\u7684\u4e2d\u6587\u5b57\u7b26\u9700\u8981 3 \u4e2a\u5b57\u8282\uff0c\u5176\u4ed6\u7684\u4e00\u4e9b\u751f\u50fb\u5b57\u7b26\u9700\u8981 4 \u4e2a\u5b57\u8282\u3002

    UTF-8 \u7684\u7f16\u7801\u89c4\u5219\u5e76\u4e0d\u590d\u6742\uff0c\u5206\u4e3a\u4e24\u79cd\u60c5\u51b5\uff1a

    • \u5bf9\u4e8e\u957f\u5ea6\u4e3a 1 \u5b57\u8282\u7684\u5b57\u7b26\uff0c\u5c06\u6700\u9ad8\u4f4d\u8bbe\u7f6e\u4e3a \\(0\\) \u3001\u5176\u4f59 7 \u4f4d\u8bbe\u7f6e\u4e3a Unicode \u7801\u70b9\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0cASCII \u5b57\u7b26\u5728 Unicode \u5b57\u7b26\u96c6\u4e2d\u5360\u636e\u4e86\u524d 128 \u4e2a\u7801\u70b9\u3002\u4e5f\u5c31\u662f\u8bf4\uff0cUTF-8 \u7f16\u7801\u53ef\u4ee5\u5411\u4e0b\u517c\u5bb9 ASCII \u7801\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 UTF-8 \u6765\u89e3\u6790\u5e74\u4ee3\u4e45\u8fdc\u7684 ASCII \u7801\u6587\u672c\u3002
    • \u5bf9\u4e8e\u957f\u5ea6\u4e3a \\(n\\) \u5b57\u8282\u7684\u5b57\u7b26\uff08\u5176\u4e2d \\(n > 1\\)\uff09\uff0c\u5c06\u9996\u4e2a\u5b57\u8282\u7684\u9ad8 \\(n\\) \u4f4d\u90fd\u8bbe\u7f6e\u4e3a \\(1\\) \u3001\u7b2c \\(n + 1\\) \u4f4d\u8bbe\u7f6e\u4e3a \\(0\\) \uff1b\u4ece\u7b2c\u4e8c\u4e2a\u5b57\u8282\u5f00\u59cb\uff0c\u5c06\u6bcf\u4e2a\u5b57\u8282\u7684\u9ad8 2 \u4f4d\u90fd\u8bbe\u7f6e\u4e3a \\(10\\) \uff1b\u5176\u4f59\u6240\u6709\u4f4d\u7528\u4e8e\u586b\u5145\u5b57\u7b26\u7684 Unicode \u7801\u70b9\u3002

    \u4e0b\u56fe\u5c55\u793a\u4e86\u201cHello\u7b97\u6cd5\u201d\u5bf9\u5e94\u7684 UTF-8 \u7f16\u7801\u3002\u5c06\u6700\u9ad8 \\(n\\) \u4f4d\u8bbe\u7f6e\u4e3a \\(1\\) \u6bd4\u8f83\u5bb9\u6613\u7406\u89e3\uff0c\u53ef\u4ee5\u5411\u7cfb\u7edf\u6307\u51fa\u5b57\u7b26\u7684\u957f\u5ea6\u4e3a \\(n\\) \u3002\u90a3\u4e48\uff0c\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\uff0c\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\u662f\u56e0\u4e3a\u957f\u5ea6\u4e3a 1 \u5b57\u8282\u7684\u5b57\u7b26\u7684\u6700\u9ad8\u4e00\u4f4d\u662f \\(0\\) \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\u7684\u5224\u65ad\u51fa\u5f02\u5e38\u3002

    Fig. UTF-8 \u7f16\u7801\u793a\u4f8b

    \u9664\u4e86 UTF-8 \u4e4b\u5916\uff0c\u5e38\u89c1\u7684\u7f16\u7801\u65b9\u5f0f\u8fd8\u5305\u62ec UTF-16 \u548c UTF-32 \u3002\u5b83\u4eec\u4e3a Unicode \u5b57\u7b26\u96c6\u63d0\u4f9b\u4e86\u4e0d\u540c\u7684\u7f16\u7801\u65b9\u6cd5\u3002

    • UTF-16 \u7f16\u7801\uff1a\u4f7f\u7528 2 \u6216 4 \u4e2a\u5b57\u8282\u6765\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\u3002\u6240\u6709\u7684 ASCII \u5b57\u7b26\u548c\u5f88\u591a\u5e38\u7528\u7684\u975e\u82f1\u6587\u5b57\u7b26\uff0c\u90fd\u7528 2 \u4e2a\u5b57\u8282\u8868\u793a\uff1b\u5c11\u6570\u5b57\u7b26\u9700\u8981\u7528\u5230 4 \u4e2a\u5b57\u8282\u8868\u793a\u3002\u5bf9\u4e8e 2 \u5b57\u8282\u7684\u5b57\u7b26\uff0cUTF-16 \u7f16\u7801\u4e0e Unicode \u7801\u70b9\u76f8\u7b49\u3002
    • UTF-32 \u7f16\u7801\uff1a\u6bcf\u4e2a\u5b57\u7b26\u90fd\u4f7f\u7528 4 \u4e2a\u5b57\u8282\u3002\u8fd9\u610f\u5473\u7740 UTF-32 \u4f1a\u6bd4 UTF-8 \u548c UTF-16 \u66f4\u5360\u7528\u7a7a\u95f4\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u4e3b\u8981\u4f7f\u7528 ASCII \u5b57\u7b26\u7684\u6587\u672c\u3002

    \u4ece\u5b58\u50a8\u7a7a\u95f4\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 \u4e2a\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\u53ea\u9700\u8981 2 \u4e2a\u5b57\u8282\uff0c\u800c UTF-8 \u53ef\u80fd\u9700\u8981 3 \u4e2a\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\u90fd\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\u8fd9\u662f\u56e0\u4e3a\u5728\u7b49\u957f\u7f16\u7801\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u770b\u4f5c\u6570\u7ec4\u6765\u5904\u7406\uff0c\u5177\u4f53\u6765\u8bf4\uff1a

    • \u968f\u673a\u8bbf\u95ee: UTF-16 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u8fdb\u884c\u968f\u673a\u8bbf\u95ee\u3002UTF-8 \u662f\u4e00\u79cd\u53d8\u957f\u7f16\u7801\uff0c\u8981\u627e\u5230\u7b2c \\(i\\) \u4e2a\u5b57\u7b26\uff0c\u6211\u4eec\u9700\u8981\u4ece\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\u5904\u904d\u5386\u5230\u7b2c \\(i\\) \u4e2a\u5b57\u7b26\uff0c\u8fd9\u9700\u8981 \\(O(n)\\) \u7684\u65f6\u95f4\u3002
    • \u5b57\u7b26\u8ba1\u6570: \u4e0e\u968f\u673a\u8bbf\u95ee\u7c7b\u4f3c\uff0c\u8ba1\u7b97 UTF-16 \u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u4e5f\u662f \\(O(1)\\) \u7684\u64cd\u4f5c\u3002\u4f46\u662f\uff0c\u8ba1\u7b97 UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u9700\u8981\u904d\u5386\u6574\u4e2a\u5b57\u7b26\u4e32\u3002
    • \u5b57\u7b26\u4e32\u64cd\u4f5c: \u5728 UTF-16 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u4e2d\uff0c\u5f88\u591a\u5b57\u7b26\u4e32\u64cd\u4f5c\uff08\u5982\u5206\u5272\u3001\u8fde\u63a5\u3001\u63d2\u5165\u3001\u5220\u9664\u7b49\uff09\u90fd\u66f4\u5bb9\u6613\u8fdb\u884c\u3002\u5728 UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u4e0a\u8fdb\u884c\u8fd9\u4e9b\u64cd\u4f5c\u901a\u5e38\u9700\u8981\u989d\u5916\u7684\u8ba1\u7b97\uff0c\u4ee5\u786e\u4fdd\u4e0d\u4f1a\u4ea7\u751f\u65e0\u6548\u7684 UTF-8 \u7f16\u7801\u3002

    \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\u5230\u8bb8\u591a\u56e0\u7d20\uff1a

    • Java \u7684 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
    • JavaScript \u548c TypeScript \u7684\u5b57\u7b26\u4e32\u4f7f\u7528 UTF-16 \u7f16\u7801\u7684\u539f\u56e0\u4e0e Java \u7c7b\u4f3c\u3002\u5f53 JavaScript \u8bed\u8a00\u5728 1995 \u5e74\u88ab Netscape \u516c\u53f8\u9996\u6b21\u5f15\u5165\u65f6\uff0cUnicode \u8fd8\u5904\u4e8e\u76f8\u5bf9\u65e9\u671f\u7684\u9636\u6bb5\uff0c\u90a3\u65f6\u5019\u4f7f\u7528 16 \u4f4d\u7684\u7f16\u7801\u5c31\u8db3\u591f\u8868\u793a\u6240\u6709\u7684 Unicode \u5b57\u7b26\u4e86\u3002
    • C# \u4f7f\u7528 UTF-16 \u7f16\u7801\uff0c\u4e3b\u8981\u56e0\u4e3a .NET \u5e73\u53f0\u662f\u7531 Microsoft \u8bbe\u8ba1\u7684\uff0c\u800c Microsoft \u7684\u5f88\u591a\u6280\u672f\uff0c\u5305\u62ec Windows \u64cd\u4f5c\u7cfb\u7edf\uff0c\u90fd\u5e7f\u6cdb\u5730\u4f7f\u7528 UTF-16 \u7f16\u7801\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\u56e0\u6b64\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\u589e\u52a0\u4e86\u7f16\u7a0b\u7684\u590d\u6742\u6027\u548c Debug \u96be\u5ea6\u3002

    \u51fa\u4e8e\u4ee5\u4e0a\u539f\u56e0\uff0c\u90e8\u5206\u7f16\u7a0b\u8bed\u8a00\u63d0\u51fa\u4e86\u4e0d\u540c\u7684\u7f16\u7801\u65b9\u6848\uff1a

    • Python 3 \u4f7f\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\u5bf9\u4e8e\u5168\u90e8\u662f ASCII \u5b57\u7b26\u7684\u5b57\u7b26\u4e32\uff0c\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 1 \u4e2a\u5b57\u8282\uff1b\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u7684\u5b57\u7b26\u8d85\u51fa\u4e86 ASCII \u8303\u56f4\uff0c\u4f46\u5168\u90e8\u5728\u57fa\u672c\u591a\u8bed\u8a00\u5e73\u9762\uff08BMP\uff09\u5185\uff0c\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 2 \u4e2a\u5b57\u8282\uff1b\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u6709\u8d85\u51fa BMP \u7684\u5b57\u7b26\uff0c\u90a3\u4e48\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 4 \u4e2a\u5b57\u8282\u3002
    • Go \u8bed\u8a00\u7684 string \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\u3002
    • Rust \u8bed\u8a00\u7684 str \u548c String \u7c7b\u578b\u5728\u5185\u90e8\u4f7f\u7528 UTF-8 \u7f16\u7801\u3002Rust \u4e5f\u63d0\u4f9b\u4e86 char \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\u4e24\u4e2a\u4e0d\u540c\u7684\u95ee\u9898\u3002\u5728\u6587\u4ef6\u5b58\u50a8\u6216\u7f51\u7edc\u4f20\u8f93\u4e2d\uff0c\u6211\u4eec\u4e00\u822c\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":"

    \u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u4ece\u903b\u8f91\u7ed3\u6784\u548c\u7269\u7406\u7ed3\u6784\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":"

    \u300c\u903b\u8f91\u7ed3\u6784\u300d\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\u987a\u5e8f\u4f9d\u6b21\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\u7956\u5148\u4e0e\u540e\u4ee3\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

    \u903b\u8f91\u7ed3\u6784\u901a\u5e38\u5206\u4e3a\u300c\u7ebf\u6027\u300d\u548c\u300c\u975e\u7ebf\u6027\u300d\u4e24\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\uff0c\u4f8b\u5982\u7f51\u72b6\u6216\u6811\u72b6\u7ed3\u6784\u3002

    • \u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff1a\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\uff1b
    • \u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff1a\u6811\u3001\u56fe\u3001\u5806\u3001\u54c8\u5e0c\u8868\uff1b

    Fig. \u7ebf\u6027\u4e0e\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784

    "},{"location":"chapter_data_structure/classification_of_data_structure/#312","title":"3.1.2. \u00a0 \u7269\u7406\u7ed3\u6784\uff1a\u8fde\u7eed\u4e0e\u79bb\u6563","text":"

    \u5728\u8ba1\u7b97\u673a\u4e2d\uff0c\u5185\u5b58\u548c\u786c\u76d8\u662f\u4e24\u79cd\u4e3b\u8981\u7684\u5b58\u50a8\u786c\u4ef6\u8bbe\u5907\u3002\u300c\u786c\u76d8\u300d\u4e3b\u8981\u7528\u4e8e\u957f\u671f\u5b58\u50a8\u6570\u636e\uff0c\u5bb9\u91cf\u8f83\u5927\uff08\u901a\u5e38\u53ef\u8fbe\u5230 TB \u7ea7\u522b\uff09\u3001\u901f\u5ea6\u8f83\u6162\u3002\u300c\u5185\u5b58\u300d\u7528\u4e8e\u8fd0\u884c\u7a0b\u5e8f\u65f6\u6682\u5b58\u6570\u636e\uff0c\u901f\u5ea6\u8f83\u5feb\uff0c\u4f46\u5bb9\u91cf\u8f83\u5c0f\uff08\u901a\u5e38\u4e3a GB \u7ea7\u522b\uff09\u3002

    \u5728\u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\uff0c\u76f8\u5173\u6570\u636e\u90fd\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\u3002\u4e0b\u56fe\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 1 byte \u7684\u6570\u636e\uff0c\u5728\u7b97\u6cd5\u8fd0\u884c\u65f6\uff0c\u6240\u6709\u6570\u636e\u90fd\u88ab\u5b58\u50a8\u5728\u8fd9\u4e9b\u5355\u5143\u683c\u4e2d\u3002

    \u7cfb\u7edf\u901a\u8fc7\u300c\u5185\u5b58\u5730\u5740 Memory Location\u300d\u6765\u8bbf\u95ee\u76ee\u6807\u5185\u5b58\u4f4d\u7f6e\u7684\u6570\u636e\u3002\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

    Fig. \u5185\u5b58\u6761\u3001\u5185\u5b58\u7a7a\u95f4\u3001\u5185\u5b58\u5730\u5740

    \u5185\u5b58\u662f\u6240\u6709\u7a0b\u5e8f\u7684\u5171\u4eab\u8d44\u6e90\uff0c\u5f53\u5185\u5b58\u88ab\u67d0\u4e2a\u7a0b\u5e8f\u5360\u7528\u65f6\uff0c\u5176\u4ed6\u7a0b\u5e8f\u65e0\u6cd5\u540c\u65f6\u4f7f\u7528\u3002\u56e0\u6b64\uff0c\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\u4f8b\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\u8fd0\u884c\u7684\u7a0b\u5e8f\u5f88\u591a\u5e76\u4e14\u7f3a\u5c11\u5927\u91cf\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u90a3\u4e48\u6240\u9009\u7528\u7684\u6570\u636e\u7ed3\u6784\u5fc5\u987b\u80fd\u591f\u5b58\u50a8\u5728\u79bb\u6563\u7684\u5185\u5b58\u7a7a\u95f4\u5185\u3002

    \u300c\u7269\u7406\u7ed3\u6784\u300d\u53cd\u6620\u4e86\u6570\u636e\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u53ef\u5206\u4e3a\u6570\u7ec4\u7684\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u548c\u94fe\u8868\u7684\u79bb\u6563\u7a7a\u95f4\u5b58\u50a8\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\u540c\u65f6\u5728\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u65b9\u9762\u5448\u73b0\u51fa\u4e92\u8865\u7684\u7279\u70b9\u3002

    Fig. \u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u4e0e\u79bb\u6563\u7a7a\u95f4\u5b58\u50a8

    \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\u53ef\u5b9e\u73b0\uff1a\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\u3001\u77e9\u9635\u3001\u5f20\u91cf\uff08\u7ef4\u5ea6 \\(\\geq 3\\) \u7684\u6570\u7ec4\uff09\u7b49\uff1b
    • \u57fa\u4e8e\u94fe\u8868\u53ef\u5b9e\u73b0\uff1a\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\u7b49\uff1b

    \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u4e5f\u88ab\u79f0\u4e3a\u300c\u9759\u6001\u6570\u636e\u7ed3\u6784\u300d\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\u88ab\u79f0\u4e3a\u300c\u52a8\u6001\u6570\u636e\u7ed3\u6784\u300d\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\u82e5\u611f\u89c9\u7406\u89e3\u7269\u7406\u7ed3\u6784\u6709\u56f0\u96be\uff0c\u5efa\u8bae\u5148\u9605\u8bfb\u4e0b\u4e00\u7ae0\u201c\u6570\u7ec4\u4e0e\u94fe\u8868\u201d\uff0c\u7136\u540e\u518d\u56de\u5934\u7406\u89e3\u7269\u7406\u7ed3\u6784\u7684\u542b\u4e49\u3002\u6570\u7ec4\u4e0e\u94fe\u8868\u662f\u5176\u4ed6\u6240\u6709\u6570\u636e\u7ed3\u6784\u7684\u57fa\u77f3\uff0c\u5efa\u8bae\u4f60\u6295\u5165\u66f4\u591a\u65f6\u95f4\u6df1\u5165\u4e86\u89e3\u8fd9\u4e24\u79cd\u57fa\u672c\u6570\u636e\u7ed3\u6784\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\u7684 * \u7b26\u53f7\u7684\u662f\u9009\u8bfb\u7ae0\u8282\u3002\u5982\u679c\u4f60\u65f6\u95f4\u6709\u9650\u6216\u611f\u5230\u7406\u89e3\u56f0\u96be\uff0c\u5efa\u8bae\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 \u539f\u7801\u3001\u53cd\u7801\u548c\u8865\u7801","text":"

    \u4ece\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\u3002\u4f8b\u5982\uff0cbyte \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\u5230\u539f\u7801\u3001\u53cd\u7801\u3001\u8865\u7801\u7684\u76f8\u5173\u77e5\u8bc6\u3002\u5728\u5c55\u5f00\u5206\u6790\u4e4b\u524d\uff0c\u6211\u4eec\u9996\u5148\u7ed9\u51fa\u4e09\u8005\u7684\u5b9a\u4e49\uff1a

    • \u539f\u7801\uff1a\u6211\u4eec\u5c06\u6570\u5b57\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u7684\u6700\u9ad8\u4f4d\u89c6\u4e3a\u7b26\u53f7\u4f4d\uff0c\u5176\u4e2d \\(0\\) \u8868\u793a\u6b63\u6570\uff0c\\(1\\) \u8868\u793a\u8d1f\u6570\uff0c\u5176\u4f59\u4f4d\u8868\u793a\u6570\u5b57\u7684\u503c\u3002
    • \u53cd\u7801\uff1a\u6b63\u6570\u7684\u53cd\u7801\u4e0e\u5176\u539f\u7801\u76f8\u540c\uff0c\u8d1f\u6570\u7684\u53cd\u7801\u662f\u5bf9\u5176\u539f\u7801\u9664\u7b26\u53f7\u4f4d\u5916\u7684\u6240\u6709\u4f4d\u53d6\u53cd\u3002
    • \u8865\u7801\uff1a\u6b63\u6570\u7684\u8865\u7801\u4e0e\u5176\u539f\u7801\u76f8\u540c\uff0c\u8d1f\u6570\u7684\u8865\u7801\u662f\u5728\u5176\u53cd\u7801\u7684\u57fa\u7840\u4e0a\u52a0 \\(1\\) \u3002

    Fig. \u539f\u7801\u3001\u53cd\u7801\u4e0e\u8865\u7801\u4e4b\u95f4\u7684\u76f8\u4e92\u8f6c\u6362

    \u663e\u7136\uff0c\u300c\u539f\u7801\u300d\u6700\u4e3a\u76f4\u89c2\uff0c\u7136\u800c\u6570\u5b57\u5374\u662f\u4ee5\u300c\u8865\u7801\u300d\u7684\u5f62\u5f0f\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u4e2d\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u539f\u7801\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\uff0c\u6211\u4eec\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 & = 0000 \\space 0001 + 1000 \\space 0010 \\newline & = 1000 \\space 0011 \\newline & = -3 \\end{aligned} \\]

    \u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u8ba1\u7b97\u673a\u5f15\u5165\u4e86\u300c\u53cd\u7801\u300d\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u5148\u5c06\u539f\u7801\u8f6c\u6362\u4e3a\u53cd\u7801\uff0c\u5e76\u5728\u53cd\u7801\u4e0b\u8ba1\u7b97 \\(1 + (-2)\\) \uff0c\u5e76\u5c06\u7ed3\u679c\u4ece\u53cd\u7801\u8f6c\u5316\u56de\u539f\u7801\uff0c\u5219\u53ef\u5f97\u5230\u6b63\u786e\u7ed3\u679c \\(-1\\) \u3002

    \\[ \\begin{aligned} & 1 + (-2) \\newline & = 0000 \\space 0001 \\space \\text{(\u539f\u7801)} + 1000 \\space 0010 \\space \\text{(\u539f\u7801)} \\newline & = 0000 \\space 0001 \\space \\text{(\u53cd\u7801)} + 1111 \\space 1101 \\space \\text{(\u53cd\u7801)} \\newline & = 1111 \\space 1110 \\space \\text{(\u53cd\u7801)} \\newline & = 1000 \\space 0001 \\space \\text{(\u539f\u7801)} \\newline & = -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\u7740\u4e24\u4e2a\u4e0d\u540c\u7684\u4e8c\u8fdb\u5236\u7f16\u7801\uff0c\u800c\u8fd9\u53ef\u80fd\u4f1a\u5e26\u6765\u6b67\u4e49\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u5728\u6761\u4ef6\u5224\u65ad\u4e2d\uff0c\u5982\u679c\u6ca1\u6709\u533a\u5206\u6b63\u96f6\u548c\u8d1f\u96f6\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u9519\u8bef\u7684\u5224\u65ad\u7ed3\u679c\u3002\u5982\u679c\u6211\u4eec\u60f3\u8981\u5904\u7406\u6b63\u96f6\u548c\u8d1f\u96f6\u6b67\u4e49\uff0c\u5219\u9700\u8981\u5f15\u5165\u989d\u5916\u7684\u5224\u65ad\u64cd\u4f5c\uff0c\u5176\u53ef\u80fd\u4f1a\u964d\u4f4e\u8ba1\u7b97\u673a\u7684\u8fd0\u7b97\u6548\u7387\u3002

    \\[ \\begin{aligned} +0 & = 0000 \\space 0000 \\newline -0 & = 1000 \\space 0000 \\end{aligned} \\]

    \u4e0e\u539f\u7801\u4e00\u6837\uff0c\u53cd\u7801\u4e5f\u5b58\u5728\u6b63\u8d1f\u96f6\u6b67\u4e49\u95ee\u9898\u3002\u4e3a\u6b64\uff0c\u8ba1\u7b97\u673a\u8fdb\u4e00\u6b65\u5f15\u5165\u4e86\u300c\u8865\u7801\u300d\u3002\u90a3\u4e48\uff0c\u8865\u7801\u6709\u4ec0\u4e48\u4f5c\u7528\u5462\uff1f\u6211\u4eec\u5148\u6765\u5206\u6790\u4e00\u4e0b\u8d1f\u96f6\u7684\u8865\u7801\u7684\u8ba1\u7b97\u8fc7\u7a0b\uff1a

    \\[ \\begin{aligned} -0 = \\space & 1000 \\space 0000 \\space \\text{(\u539f\u7801)} \\newline = \\space & 1111 \\space 1111 \\space \\text{(\u53cd\u7801)} \\newline = 1 \\space & 0000 \\space 0000 \\space \\text{(\u8865\u7801)} \\newline \\end{aligned} \\]

    \u5728\u8d1f\u96f6\u7684\u53cd\u7801\u57fa\u7840\u4e0a\u52a0 \\(1\\) \u4f1a\u4ea7\u751f\u8fdb\u4f4d\uff0c\u800c\u7531\u4e8e byte \u7684\u957f\u5ea6\u53ea\u6709 8 \u4f4d\uff0c\u56e0\u6b64\u6ea2\u51fa\u5230\u7b2c 9 \u4f4d\u7684 \\(1\\) \u4f1a\u88ab\u820d\u5f03\u3002\u4ece\u800c\u5f97\u5230\u8d1f\u96f6\u7684\u8865\u7801\u4e3a \\(0000 \\space 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\u4ece\u800c\u89e3\u51b3\u4e86\u6b63\u8d1f\u96f6\u6b67\u4e49\u95ee\u9898\u3002

    \u8fd8\u5269\u4f59\u6700\u540e\u4e00\u4e2a\u7591\u60d1\uff1abyte \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\u662f\u53ef\u4ee5\u4e92\u76f8\u8f6c\u6362\u7684\u3002

    \u7136\u800c\uff0c\u8865\u7801 \\(1000 \\space 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 \\space 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 \\space 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 & = 1111 \\space 1111 \\space \\text{(\u539f\u7801)} + 1000 \\space 0001 \\space \\text{(\u539f\u7801)} \\newline & = 1000 \\space 0000 \\space \\text{(\u53cd\u7801)} + 1111 \\space 1110 \\space \\text{(\u53cd\u7801)} \\newline & = 1000 \\space 0001 \\space \\text{(\u8865\u7801)} + 1111 \\space 1111 \\space \\text{(\u8865\u7801)} \\newline & = 1000 \\space 0000 \\space \\text{(\u8865\u7801)} \\newline & = -128 \\end{aligned} \\]

    \u4f60\u53ef\u80fd\u5df2\u7ecf\u53d1\u73b0\uff0c\u4e0a\u8ff0\u7684\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\u4ece\u800c\u63d0\u9ad8\u8fd0\u7b97\u901f\u5ea6\u3002

    \u7136\u800c\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\uff0c\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\u9700\u7279\u522b\u5904\u7406\u6b63\u8d1f\u96f6\u7684\u6b67\u4e49\u95ee\u9898\u3002\u8fd9\u5927\u5927\u7b80\u5316\u4e86\u786c\u4ef6\u8bbe\u8ba1\uff0c\u5e76\u63d0\u9ad8\u4e86\u8fd0\u7b97\u6548\u7387\u3002

    \u8865\u7801\u7684\u8bbe\u8ba1\u975e\u5e38\u7cbe\u5999\uff0c\u7531\u4e8e\u7bc7\u5e45\u5173\u7cfb\u6211\u4eec\u5148\u4ecb\u7ecd\u5230\u8fd9\u91cc\u3002\u5efa\u8bae\u6709\u5174\u8da3\u7684\u8bfb\u8005\u8fdb\u4e00\u6b65\u6df1\u5ea6\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\u6839\u636e IEEE 754 \u6807\u51c6\uff0c32-bit \u957f\u5ea6\u7684 float \u7531\u4ee5\u4e0b\u90e8\u5206\u6784\u6210\uff1a

    • \u7b26\u53f7\u4f4d \\(\\mathrm{S}\\) \uff1a\u5360 1 bit \uff1b
    • \u6307\u6570\u4f4d \\(\\mathrm{E}\\) \uff1a\u5360 8 bits \uff1b
    • \u5206\u6570\u4f4d \\(\\mathrm{N}\\) \uff1a\u5360 24 bits \uff0c\u5176\u4e2d 23 \u4f4d\u663e\u5f0f\u5b58\u50a8\uff1b

    \u8bbe 32-bit \u4e8c\u8fdb\u5236\u6570\u7684\u7b2c \\(i\\) \u4f4d\u4e3a \\(b_i\\) \uff0c\u5219 float \u503c\u7684\u8ba1\u7b97\u65b9\u6cd5\u5b9a\u4e49\u4e3a\uff1a

    \\[ \\text { val } = (-1)^{b_{31}} \\times 2^{\\left(b_{30} b_{29} \\ldots b_{23}\\right)_2-127} \\times\\left(1 . b_{22} b_{21} \\ldots b_0\\right)_2 \\]

    \u8f6c\u5316\u5230\u5341\u8fdb\u5236\u4e0b\u7684\u8ba1\u7b97\u516c\u5f0f\u4e3a

    \\[ \\text { val }=(-1)^{\\mathrm{S}} \\times 2^{\\mathrm{E} -127} \\times (1 + \\mathrm{N}) \\]

    \u5176\u4e2d\u5404\u9879\u7684\u53d6\u503c\u8303\u56f4\u4e3a

    \\[ \\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} \\]

    Fig. IEEE 754 \u6807\u51c6\u4e0b\u7684 float \u8868\u793a\u65b9\u5f0f

    \u4ee5\u4e0a\u56fe\u4e3a\u4f8b\uff0c\\(\\mathrm{S} = 0\\) \uff0c \\(\\mathrm{E} = 124\\) \uff0c\\(\\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\\) \uff0c\u6613\u5f97

    \\[ \\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

    \u8fdb\u4e00\u6b65\u5730\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

    \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}\\)

    \u7279\u522b\u5730\uff0c\u6b21\u6b63\u89c4\u6570\u663e\u8457\u63d0\u5347\u4e86\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\uff0c\u8fd9\u662f\u56e0\u4e3a\uff1a

    • \u6700\u5c0f\u6b63\u6b63\u89c4\u6570\u4e3a \\(2^{-126} \\approx 1.18 \\times 10^{-38}\\) \uff1b
    • \u6700\u5c0f\u6b63\u6b21\u6b63\u89c4\u6570\u4e3a \\(2^{-126} \\times 2^{-23} \\approx 1.4 \\times 10^{-45}\\) \uff1b

    \u53cc\u7cbe\u5ea6 double \u4e5f\u91c7\u7528\u7c7b\u4f3c float \u7684\u8868\u793a\u65b9\u6cd5\uff0c\u6b64\u5904\u4e0d\u518d\u8be6\u8ff0\u3002

    "},{"location":"chapter_data_structure/summary/","title":"3.5. \u00a0 \u5c0f\u7ed3","text":"

    \u6570\u636e\u7ed3\u6784\u5206\u7c7b

    • \u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u4ece\u903b\u8f91\u7ed3\u6784\u548c\u7269\u7406\u7ed3\u6784\u4e24\u4e2a\u89d2\u5ea6\u8fdb\u884c\u5206\u7c7b\u3002\u903b\u8f91\u7ed3\u6784\u63cf\u8ff0\u4e86\u6570\u636e\u5143\u7d20\u4e4b\u95f4\u7684\u903b\u8f91\u5173\u7cfb\uff0c\u800c\u7269\u7406\u7ed3\u6784\u63cf\u8ff0\u4e86\u6570\u636e\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u5b58\u50a8\u65b9\u5f0f\u3002
    • \u5e38\u89c1\u7684\u903b\u8f91\u7ed3\u6784\u5305\u62ec\u7ebf\u6027\u3001\u6811\u72b6\u548c\u7f51\u72b6\u7b49\u3002\u901a\u5e38\u6211\u4eec\u6839\u636e\u903b\u8f91\u7ed3\u6784\u5c06\u6570\u636e\u7ed3\u6784\u5206\u4e3a\u7ebf\u6027\uff08\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\uff09\u548c\u975e\u7ebf\u6027\uff08\u6811\u3001\u56fe\u3001\u5806\uff09\u4e24\u79cd\u3002\u54c8\u5e0c\u8868\u7684\u5b9e\u73b0\u53ef\u80fd\u540c\u65f6\u5305\u542b\u7ebf\u6027\u548c\u975e\u7ebf\u6027\u7ed3\u6784\u3002
    • \u5f53\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u6570\u636e\u88ab\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u3002\u6bcf\u4e2a\u5185\u5b58\u7a7a\u95f4\u90fd\u62e5\u6709\u5bf9\u5e94\u7684\u5185\u5b58\u5730\u5740\uff0c\u7a0b\u5e8f\u901a\u8fc7\u8fd9\u4e9b\u5185\u5b58\u5730\u5740\u8bbf\u95ee\u6570\u636e\u3002
    • \u7269\u7406\u7ed3\u6784\u4e3b\u8981\u5206\u4e3a\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\uff08\u6570\u7ec4\uff09\u548c\u79bb\u6563\u7a7a\u95f4\u5b58\u50a8\uff08\u94fe\u8868\uff09\u3002\u6240\u6709\u6570\u636e\u7ed3\u6784\u90fd\u662f\u7531\u6570\u7ec4\u3001\u94fe\u8868\u6216\u4e24\u8005\u7684\u7ec4\u5408\u5b9e\u73b0\u7684\u3002

    \u6570\u636e\u7c7b\u578b\u4e0e\u7f16\u7801

    • \u8ba1\u7b97\u673a\u4e2d\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u5305\u62ec\u6574\u6570 byte, short, int, long \u3001\u6d6e\u70b9\u6570 float, double \u3001\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
    • \u539f\u7801\u3001\u53cd\u7801\u548c\u8865\u7801\u662f\u5728\u8ba1\u7b97\u673a\u4e2d\u7f16\u7801\u6570\u5b57\u7684\u4e09\u79cd\u65b9\u6cd5\uff0c\u5b83\u4eec\u4e4b\u95f4\u662f\u53ef\u4ee5\u76f8\u4e92\u8f6c\u6362\u7684\u3002\u6574\u6570\u7684\u539f\u7801\u7684\u6700\u9ad8\u4f4d\u662f\u7b26\u53f7\u4f4d\uff0c\u5176\u4f59\u4f4d\u662f\u6570\u5b57\u7684\u503c\u3002
    • \u6574\u6570\u5728\u8ba1\u7b97\u673a\u4e2d\u662f\u4ee5\u8865\u7801\u7684\u5f62\u5f0f\u5b58\u50a8\u7684\u3002\u5728\u8865\u7801\u8868\u793a\u4e0b\uff0c\u8ba1\u7b97\u673a\u53ef\u4ee5\u5bf9\u6b63\u6570\u548c\u8d1f\u6570\u7684\u52a0\u6cd5\u4e00\u89c6\u540c\u4ec1\uff0c\u4e0d\u9700\u8981\u4e3a\u51cf\u6cd5\u64cd\u4f5c\u5355\u72ec\u8bbe\u8ba1\u7279\u6b8a\u7684\u786c\u4ef6\u7535\u8def\uff0c\u5e76\u4e14\u4e0d\u5b58\u5728\u6b63\u8d1f\u96f6\u6b67\u4e49\u7684\u95ee\u9898\u3002
    • \u6d6e\u70b9\u6570\u7684\u7f16\u7801\u7531 1 \u4f4d\u7b26\u53f7\u4f4d\u30018 \u4f4d\u6307\u6570\u4f4d\u548c 23 \u4f4d\u5206\u6570\u4f4d\u6784\u6210\u3002\u7531\u4e8e\u5b58\u5728\u6307\u6570\u4f4d\uff0c\u6d6e\u70b9\u6570\u7684\u53d6\u503c\u8303\u56f4\u8fdc\u5927\u4e8e\u6574\u6570\uff0c\u4ee3\u4ef7\u662f\u727a\u7272\u4e86\u7cbe\u5ea6\u3002
    • ASCII \u7801\u662f\u6700\u65e9\u51fa\u73b0\u7684\u82f1\u6587\u5b57\u7b26\u96c6\uff0c\u957f\u5ea6\u4e3a 1 \u5b57\u8282\uff0c\u5171\u6536\u5f55 127 \u4e2a\u5b57\u7b26\u3002GBK \u5b57\u7b26\u96c6\u662f\u5e38\u7528\u7684\u4e2d\u6587\u5b57\u7b26\u96c6\uff0c\u5171\u6536\u5f55\u4e24\u4e07\u591a\u4e2a\u6c49\u5b57\u3002Unicode \u81f4\u529b\u4e8e\u63d0\u4f9b\u4e00\u4e2a\u5b8c\u6574\u7684\u5b57\u7b26\u96c6\u6807\u51c6\uff0c\u6536\u5f55\u4e16\u754c\u5185\u5404\u79cd\u8bed\u8a00\u7684\u5b57\u7b26\uff0c\u4ece\u800c\u89e3\u51b3\u7531\u4e8e\u5b57\u7b26\u7f16\u7801\u65b9\u6cd5\u4e0d\u4e00\u81f4\u800c\u5bfc\u81f4\u7684\u4e71\u7801\u95ee\u9898\u3002
    • UTF-8 \u662f\u6700\u53d7\u6b22\u8fce\u7684 Unicode \u7f16\u7801\u65b9\u6cd5\uff0c\u901a\u7528\u6027\u975e\u5e38\u597d\u3002\u5b83\u662f\u4e00\u79cd\u53d8\u957f\u7684\u7f16\u7801\u65b9\u6cd5\uff0c\u5177\u6709\u5f88\u597d\u7684\u6269\u5c55\u6027\uff0c\u6709\u6548\u63d0\u5347\u4e86\u5b58\u50a8\u7a7a\u95f4\u7684\u4f7f\u7528\u6548\u7387\u3002UTF-16 \u548c UTF-32 \u662f\u7b49\u957f\u7684\u7f16\u7801\u65b9\u6cd5\u3002\u5728\u7f16\u7801\u4e2d\u6587\u65f6\uff0cUTF-16 \u6bd4 UTF-8 \u7684\u5360\u7528\u7a7a\u95f4\u66f4\u5c0f\u3002Java, C# \u7b49\u7f16\u7a0b\u8bed\u8a00\u9ed8\u8ba4\u4f7f\u7528 UTF-16 \u7f16\u7801\u3002
    "},{"location":"chapter_data_structure/summary/#351-q-a","title":"3.5.1. \u00a0 Q & A","text":"

    \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\u62c9\u94fe\u6cd5\u201d\uff08\u540e\u7eed\u6563\u5217\u8868\u7ae0\u8282\u4f1a\u8bb2\uff09\u3002\u5728\u62c9\u94fe\u6cd5\u4e2d\uff0c\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5730\u5740\uff08\u6876\uff09\u6307\u5411\u4e00\u4e2a\u94fe\u8868\uff1b\u5f53\u8fd9\u4e2a\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\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 bytes \u5417\uff1f

    \u8fd9\u4e2a\u4e0e\u7f16\u7a0b\u8bed\u8a00\u91c7\u7528\u7684\u7f16\u7801\u65b9\u6cd5\u6709\u5173\u3002\u4f8b\u5982\uff0cJava, JS, TS, C# \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

    "},{"location":"chapter_graph/","title":"9. \u00a0 \u56fe","text":""},{"location":"chapter_graph/graph/","title":"9.1. \u00a0 \u56fe","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} \\]

    Fig. \u94fe\u8868\u3001\u6811\u3001\u56fe\u4e4b\u95f4\u7684\u5173\u7cfb

    \u90a3\u4e48\uff0c\u56fe\u4e0e\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u7684\u5173\u7cfb\u662f\u4ec0\u4e48\uff1f\u5982\u679c\u6211\u4eec\u628a\u300c\u9876\u70b9\u300d\u770b\u4f5c\u8282\u70b9\uff0c\u628a\u300c\u8fb9\u300d\u770b\u4f5c\u8fde\u63a5\u5404\u4e2a\u8282\u70b9\u7684\u6307\u9488\uff0c\u5219\u53ef\u5c06\u300c\u56fe\u300d\u770b\u4f5c\u662f\u4e00\u79cd\u4ece\u300c\u94fe\u8868\u300d\u62d3\u5c55\u800c\u6765\u7684\u6570\u636e\u7ed3\u6784\u3002\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\u4ece\u800c\u66f4\u4e3a\u590d\u6742\u3002

    "},{"location":"chapter_graph/graph/#911","title":"9.1.1. \u00a0 \u56fe\u5e38\u89c1\u7c7b\u578b","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\u3002

    • \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u8fb9\u8868\u793a\u4e24\u9876\u70b9\u4e4b\u95f4\u7684\u201c\u53cc\u5411\u201d\u8fde\u63a5\u5173\u7cfb\uff0c\u4f8b\u5982\u5fae\u4fe1\u6216 QQ \u4e2d\u7684\u201c\u597d\u53cb\u5173\u7cfb\u201d\uff1b
    • \u5728\u6709\u5411\u56fe\u4e2d\uff0c\u8fb9\u5177\u6709\u65b9\u5411\u6027\uff0c\u5373 \\(A \\rightarrow B\\) \u548c \\(A \\leftarrow B\\) \u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u662f\u76f8\u4e92\u72ec\u7acb\u7684\uff0c\u4f8b\u5982\u5fae\u535a\u6216\u6296\u97f3\u4e0a\u7684\u201c\u5173\u6ce8\u201d\u4e0e\u201c\u88ab\u5173\u6ce8\u201d\u5173\u7cfb\uff1b

    Fig. \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\u3002

    • \u5bf9\u4e8e\u8fde\u901a\u56fe\uff0c\u4ece\u67d0\u4e2a\u9876\u70b9\u51fa\u53d1\uff0c\u53ef\u4ee5\u5230\u8fbe\u5176\u4f59\u4efb\u610f\u9876\u70b9\uff1b
    • \u5bf9\u4e8e\u975e\u8fde\u901a\u56fe\uff0c\u4ece\u67d0\u4e2a\u9876\u70b9\u51fa\u53d1\uff0c\u81f3\u5c11\u6709\u4e00\u4e2a\u9876\u70b9\u65e0\u6cd5\u5230\u8fbe\uff1b

    Fig. \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\u300c\u6709\u6743\u56fe Weighted Graph\u300d\u3002\u4f8b\u5982\uff0c\u5728\u738b\u8005\u8363\u8000\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

    Fig. \u6709\u6743\u56fe\u4e0e\u65e0\u6743\u56fe

    "},{"location":"chapter_graph/graph/#912","title":"9.1.2. \u00a0 \u56fe\u5e38\u7528\u672f\u8bed","text":"
    • \u300c\u90bb\u63a5 Adjacency\u300d\uff1a\u5f53\u4e24\u9876\u70b9\u4e4b\u95f4\u5b58\u5728\u8fb9\u76f8\u8fde\u65f6\uff0c\u79f0\u8fd9\u4e24\u9876\u70b9\u201c\u90bb\u63a5\u201d\u3002\u5728\u4e0a\u56fe\u4e2d\uff0c\u9876\u70b9 1 \u7684\u90bb\u63a5\u9876\u70b9\u4e3a\u9876\u70b9 2\u30013\u30015\u3002
    • \u300c\u8def\u5f84 Path\u300d\uff1a\u4ece\u9876\u70b9 A \u5230\u9876\u70b9 B \u7ecf\u8fc7\u7684\u8fb9\u6784\u6210\u7684\u5e8f\u5217\u88ab\u79f0\u4e3a\u4ece A \u5230 B \u7684\u201c\u8def\u5f84\u201d\u3002\u5728\u4e0a\u56fe\u4e2d\uff0c\u8fb9\u5e8f\u5217 1-5-2-4 \u662f\u9876\u70b9 1 \u5230\u9876\u70b9 4 \u7684\u4e00\u6761\u8def\u5f84\u3002
    • \u300c\u5ea6 Degree\u300d\u8868\u793a\u4e00\u4e2a\u9876\u70b9\u62e5\u6709\u7684\u8fb9\u6570\u3002\u5bf9\u4e8e\u6709\u5411\u56fe\uff0c\u300c\u5165\u5ea6 In-Degree\u300d\u8868\u793a\u6709\u591a\u5c11\u6761\u8fb9\u6307\u5411\u8be5\u9876\u70b9\uff0c\u300c\u51fa\u5ea6 Out-Degree\u300d\u8868\u793a\u6709\u591a\u5c11\u6761\u8fb9\u4ece\u8be5\u9876\u70b9\u6307\u51fa\u3002
    "},{"location":"chapter_graph/graph/#913","title":"9.1.3. \u00a0 \u56fe\u7684\u8868\u793a","text":"

    \u56fe\u7684\u5e38\u7528\u8868\u793a\u65b9\u6cd5\u5305\u62ec\u300c\u90bb\u63a5\u77e9\u9635\u300d\u548c\u300c\u90bb\u63a5\u8868\u300d\u3002\u4ee5\u4e0b\u4f7f\u7528\u65e0\u5411\u56fe\u8fdb\u884c\u4e3e\u4f8b\u3002

    "},{"location":"chapter_graph/graph/#_1","title":"\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\u4e0b\u56fe\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

    Fig. \u56fe\u7684\u90bb\u63a5\u77e9\u9635\u8868\u793a

    \u90bb\u63a5\u77e9\u9635\u5177\u6709\u4ee5\u4e0b\u7279\u6027\uff1a

    • \u9876\u70b9\u4e0d\u80fd\u4e0e\u81ea\u8eab\u76f8\u8fde\uff0c\u56e0\u6b64\u90bb\u63a5\u77e9\u9635\u4e3b\u5bf9\u89d2\u7ebf\u5143\u7d20\u6ca1\u6709\u610f\u4e49\u3002
    • \u5bf9\u4e8e\u65e0\u5411\u56fe\uff0c\u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u7b49\u4ef7\uff0c\u6b64\u65f6\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\u3002
    • \u5c06\u90bb\u63a5\u77e9\u9635\u7684\u5143\u7d20\u4ece \\(1\\) , \\(0\\) \u66ff\u6362\u4e3a\u6743\u91cd\uff0c\u5219\u53ef\u8868\u793a\u6709\u6743\u56fe\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\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":"\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\\) \u6761\u94fe\u8868\u5bf9\u5e94\u9876\u70b9 \\(i\\) \uff0c\u5176\u4e2d\u5b58\u50a8\u4e86\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff08\u5373\u4e0e\u8be5\u9876\u70b9\u76f8\u8fde\u7684\u9876\u70b9\uff09\u3002

    Fig. \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\u4e0a\u56fe\u53ef\u53d1\u73b0\uff0c\u90bb\u63a5\u8868\u7ed3\u6784\u4e0e\u54c8\u5e0c\u8868\u4e2d\u7684\u300c\u94fe\u5730\u5740\u6cd5\u300d\u975e\u5e38\u76f8\u4f3c\uff0c\u56e0\u6b64\u6211\u4eec\u4e5f\u53ef\u4ee5\u91c7\u7528\u7c7b\u4f3c\u65b9\u6cd5\u6765\u4f18\u5316\u6548\u7387\u3002\u4f8b\u5982\uff0c\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)\\) \uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4e2d\u5e8f\u904d\u5386\u83b7\u53d6\u6709\u5e8f\u5e8f\u5217\uff1b\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u54c8\u5e0c\u8868\uff0c\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u964d\u4f4e\u81f3 \\(O(1)\\) \u3002

    "},{"location":"chapter_graph/graph/#914","title":"9.1.4. \u00a0 \u56fe\u5e38\u89c1\u5e94\u7528","text":"

    \u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8bb8\u591a\u7cfb\u7edf\u90fd\u53ef\u4ee5\u7528\u56fe\u6765\u5efa\u6a21\uff0c\u76f8\u5e94\u7684\u5f85\u6c42\u89e3\u95ee\u9898\u4e5f\u53ef\u4ee5\u7ea6\u5316\u4e3a\u56fe\u8ba1\u7b97\u95ee\u9898\u3002

    \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\u300c\u8fb9\u300d\u7684\u64cd\u4f5c\u548c\u5bf9\u300c\u9876\u70b9\u300d\u7684\u64cd\u4f5c\u3002\u5728\u300c\u90bb\u63a5\u77e9\u9635\u300d\u548c\u300c\u90bb\u63a5\u8868\u300d\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\u6709\uff1a

    • \u6dfb\u52a0\u6216\u5220\u9664\u8fb9\uff1a\u76f4\u63a5\u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u4fee\u6539\u6307\u5b9a\u7684\u8fb9\u5373\u53ef\uff0c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002\u800c\u7531\u4e8e\u662f\u65e0\u5411\u56fe\uff0c\u56e0\u6b64\u9700\u8981\u540c\u65f6\u66f4\u65b0\u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u3002
    • \u6dfb\u52a0\u9876\u70b9\uff1a\u5728\u90bb\u63a5\u77e9\u9635\u7684\u5c3e\u90e8\u6dfb\u52a0\u4e00\u884c\u4e00\u5217\uff0c\u5e76\u5168\u90e8\u586b \\(0\\) \u5373\u53ef\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002
    • \u5220\u9664\u9876\u70b9\uff1a\u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u4e00\u884c\u4e00\u5217\u3002\u5f53\u5220\u9664\u9996\u884c\u9996\u5217\u65f6\u8fbe\u5230\u6700\u5dee\u60c5\u51b5\uff0c\u9700\u8981\u5c06 \\((n-1)^2\\) \u4e2a\u5143\u7d20\u201c\u5411\u5de6\u4e0a\u79fb\u52a8\u201d\uff0c\u4ece\u800c\u4f7f\u7528 \\(O(n^2)\\) \u65f6\u95f4\u3002
    • \u521d\u59cb\u5316\uff1a\u4f20\u5165 \\(n\\) \u4e2a\u9876\u70b9\uff0c\u521d\u59cb\u5316\u957f\u5ea6\u4e3a \\(n\\) \u7684\u9876\u70b9\u5217\u8868 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
    \u521d\u59cb\u5316\u90bb\u63a5\u77e9\u9635\u6dfb\u52a0\u8fb9\u5220\u9664\u8fb9\u6dfb\u52a0\u9876\u70b9\u5220\u9664\u9876\u70b9

    \u4ee5\u4e0b\u662f\u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u8868\u793a\u56fe\u7684\u5b9e\u73b0\u4ee3\u7801\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart graph_adjacency_matrix.java
    /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nList<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\nList<List<Integer>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n/* \u6784\u9020\u65b9\u6cd5 */\npublic GraphAdjMat(int[] vertices, int[][] edges) {\nthis.vertices = new ArrayList<>();\nthis.adjMat = new ArrayList<>();\n// \u6dfb\u52a0\u9876\u70b9\nfor (int val : vertices) {\naddVertex(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\nfor (int[] e : edges) {\naddEdge(e[0], e[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\npublic int size() {\nreturn vertices.size();\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npublic void addVertex(int val) {\nint n = size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nvertices.add(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nList<Integer> newRow = new ArrayList<>(n);\nfor (int j = 0; j < n; j++) {\nnewRow.add(0);\n}\nadjMat.add(newRow);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor (List<Integer> row : adjMat) {\nrow.add(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\npublic void removeVertex(int index) {\nif (index >= size())\nthrow new IndexOutOfBoundsException();\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nvertices.remove(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nadjMat.remove(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor (List<Integer> row : adjMat) {\nrow.remove(index);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\npublic void addEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\nthrow new IndexOutOfBoundsException();\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nadjMat.get(i).set(j, 1);\nadjMat.get(j).set(i, 1);\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\npublic void removeEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\nthrow new IndexOutOfBoundsException();\nadjMat.get(i).set(j, 0);\nadjMat.get(j).set(i, 0);\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\npublic void print() {\nSystem.out.print(\"\u9876\u70b9\u5217\u8868 = \");\nSystem.out.println(vertices);\nSystem.out.println(\"\u90bb\u63a5\u77e9\u9635 =\");\nPrintUtil.printMatrix(adjMat);\n}\n}\n
    graph_adjacency_matrix.cpp
    /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nvector<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\nvector<vector<int>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nGraphAdjMat(const vector<int> &vertices, const vector<vector<int>> &edges) {\n// \u6dfb\u52a0\u9876\u70b9\nfor (int val : vertices) {\naddVertex(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\nfor (const vector<int> &edge : edges) {\naddEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nint size() const {\nreturn vertices.size();\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(int val) {\nint n = size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nvertices.push_back(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nadjMat.emplace_back(n, 0);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor (vector<int> &row : adjMat) {\nrow.push_back(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(int index) {\nif (index >= size()) {\nthrow out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nvertices.erase(vertices.begin() + index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nadjMat.erase(adjMat.begin() + index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor (vector<int> &row : adjMat) {\nrow.erase(row.begin() + index);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid addEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\nthrow out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n}\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nadjMat[i][j] = 1;\nadjMat[j][i] = 1;\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid removeEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\nthrow out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n}\nadjMat[i][j] = 0;\nadjMat[j][i] = 0;\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nvoid print() {\ncout << \"\u9876\u70b9\u5217\u8868 = \";\nprintVector(vertices);\ncout << \"\u90bb\u63a5\u77e9\u9635 =\" << endl;\nprintVectorMatrix(adjMat);\n}\n};\n
    graph_adjacency_matrix.py
    class GraphAdjMat:\n\"\"\"\u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\"\"\"\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\nvertices: list[int] = []\n# \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\nadj_mat: list[list[int]] = []\ndef __init__(self, vertices: list[int], edges: list[list[int]]) -> None:\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.vertices: list[int] = []\nself.adj_mat: list[list[int]] = []\n# \u6dfb\u52a0\u9876\u70b9\nfor val in vertices:\nself.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\nfor e in edges:\nself.add_edge(e[0], e[1])\ndef size(self) -> int:\n\"\"\"\u83b7\u53d6\u9876\u70b9\u6570\u91cf\"\"\"\nreturn len(self.vertices)\ndef add_vertex(self, val: int) -> None:\n\"\"\"\u6dfb\u52a0\u9876\u70b9\"\"\"\nn = self.size()\n# \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nself.vertices.append(val)\n# \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nnew_row = [0] * n\nself.adj_mat.append(new_row)\n# \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor row in self.adj_mat:\nrow.append(0)\ndef remove_vertex(self, index: int) -> None:\n\"\"\"\u5220\u9664\u9876\u70b9\"\"\"\nif index >= self.size():\nraise IndexError()\n# \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nself.vertices.pop(index)\n# \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nself.adj_mat.pop(index)\n# \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor row in self.adj_mat:\nrow.pop(index)\ndef add_edge(self, i: int, j: int) -> None:\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\nif i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\nraise IndexError()\n# \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nself.adj_mat[i][j] = 1\nself.adj_mat[j][i] = 1\ndef remove_edge(self, i: int, j: int) -> None:\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\nif i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\nraise IndexError()\nself.adj_mat[i][j] = 0\nself.adj_mat[j][i] = 0\ndef print(self) -> None:\n\"\"\"\u6253\u5370\u90bb\u63a5\u77e9\u9635\"\"\"\nprint(\"\u9876\u70b9\u5217\u8868 =\", self.vertices)\nprint(\"\u90bb\u63a5\u77e9\u9635 =\")\nprint_matrix(self.adj_mat)\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\nvertices []int\n// \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\nadjMat [][]int\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newGraphAdjMat(vertices []int, edges [][]int) *graphAdjMat {\n// \u6dfb\u52a0\u9876\u70b9\nn := len(vertices)\nadjMat := make([][]int, n)\nfor i := range adjMat {\nadjMat[i] = make([]int, n)\n}\n// \u521d\u59cb\u5316\u56fe\ng := &graphAdjMat{\nvertices: vertices,\nadjMat:   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\nfor i := range edges {\ng.addEdge(edges[i][0], edges[i][1])\n}\nreturn g\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nfunc (g *graphAdjMat) size() int {\nreturn len(g.vertices)\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nfunc (g *graphAdjMat) addVertex(val int) {\nn := g.size()\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\ng.vertices = append(g.vertices, val)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nnewRow := make([]int, n)\ng.adjMat = append(g.adjMat, newRow)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor i := range g.adjMat {\ng.adjMat[i] = append(g.adjMat[i], 0)\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\nfunc (g *graphAdjMat) removeVertex(index int) {\nif index >= g.size() {\nreturn\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\ng.vertices = append(g.vertices[:index], g.vertices[index+1:]...)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\ng.adjMat = append(g.adjMat[:index], g.adjMat[index+1:]...)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor i := range g.adjMat {\ng.adjMat[i] = append(g.adjMat[i][:index], g.adjMat[i][index+1:]...)\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\nif i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {\nfmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n}\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\ng.adjMat[i][j] = 1\ng.adjMat[j][i] = 1\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\nif i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {\nfmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n}\ng.adjMat[i][j] = 0\ng.adjMat[j][i] = 0\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nfunc (g *graphAdjMat) print() {\nfmt.Printf(\"\\t\u9876\u70b9\u5217\u8868 = %v\\n\", g.vertices)\nfmt.Printf(\"\\t\u90bb\u63a5\u77e9\u9635 = \\n\")\nfor i := range g.adjMat {\nfmt.Printf(\"\\t\\t\\t%v\\n\", g.adjMat[i])\n}\n}\n
    graph_adjacency_matrix.js
    /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nvertices; // \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\nadjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n/* \u6784\u9020\u51fd\u6570 */\nconstructor(vertices, edges) {\nthis.vertices = [];\nthis.adjMat = [];\n// \u6dfb\u52a0\u9876\u70b9\nfor (const val of vertices) {\nthis.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\nfor (const e of edges) {\nthis.addEdge(e[0], e[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nsize() {\nreturn this.vertices.length;\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\naddVertex(val) {\nconst n = this.size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nthis.vertices.push(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nconst newRow = [];\nfor (let j = 0; j < n; j++) {\nnewRow.push(0);\n}\nthis.adjMat.push(newRow);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor (const row of this.adjMat) {\nrow.push(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\nremoveVertex(index) {\nif (index >= this.size()) {\nthrow new RangeError('Index Out Of Bounds Exception');\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nthis.vertices.splice(index, 1);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nthis.adjMat.splice(index, 1);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor (const row of this.adjMat) {\nrow.splice(index, 1);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\naddEdge(i, j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\nthrow new RangeError('Index Out Of Bounds Exception');\n}\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nthis.adjMat[i][j] = 1;\nthis.adjMat[j][i] = 1;\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nremoveEdge(i, j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\nthrow new RangeError('Index Out Of Bounds Exception');\n}\nthis.adjMat[i][j] = 0;\nthis.adjMat[j][i] = 0;\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nprint() {\nconsole.log('\u9876\u70b9\u5217\u8868 = ', this.vertices);\nconsole.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 {\nvertices: 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\nadjMat: number[][]; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n/* \u6784\u9020\u51fd\u6570 */\nconstructor(vertices: number[], edges: number[][]) {\nthis.vertices = [];\nthis.adjMat = [];\n// \u6dfb\u52a0\u9876\u70b9\nfor (const val of vertices) {\nthis.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\nfor (const e of edges) {\nthis.addEdge(e[0], e[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nsize(): number {\nreturn this.vertices.length;\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\naddVertex(val: number): void {\nconst n: number = this.size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nthis.vertices.push(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nconst newRow: number[] = [];\nfor (let j: number = 0; j < n; j++) {\nnewRow.push(0);\n}\nthis.adjMat.push(newRow);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor (const row of this.adjMat) {\nrow.push(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\nremoveVertex(index: number): void {\nif (index >= this.size()) {\nthrow new RangeError('Index Out Of Bounds Exception');\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nthis.vertices.splice(index, 1);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nthis.adjMat.splice(index, 1);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor (const row of this.adjMat) {\nrow.splice(index, 1);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\naddEdge(i: number, j: number): void {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\nthrow new RangeError('Index Out Of Bounds Exception');\n}\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nthis.adjMat[i][j] = 1;\nthis.adjMat[j][i] = 1;\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nremoveEdge(i: number, j: number): void {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\nthrow new RangeError('Index Out Of Bounds Exception');\n}\nthis.adjMat[i][j] = 0;\nthis.adjMat[j][i] = 0;\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nprint(): void {\nconsole.log('\u9876\u70b9\u5217\u8868 = ', this.vertices);\nconsole.log('\u90bb\u63a5\u77e9\u9635 =', this.adjMat);\n}\n}\n
    graph_adjacency_matrix.c
    [class]{graphAdjMat}-[func]{}\n
    graph_adjacency_matrix.cs
    /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nList<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\nList<List<int>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n/* \u6784\u9020\u51fd\u6570 */\npublic GraphAdjMat(int[] vertices, int[][] edges) {\nthis.vertices = new List<int>();\nthis.adjMat = new List<List<int>>();\n// \u6dfb\u52a0\u9876\u70b9\nforeach (int val in vertices) {\naddVertex(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\nforeach (int[] e in edges) {\naddEdge(e[0], e[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\npublic int size() {\nreturn vertices.Count;\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npublic void addVertex(int val) {\nint n = size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nvertices.Add(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nList<int> newRow = new List<int>(n);\nfor (int j = 0; j < n; j++) {\nnewRow.Add(0);\n}\nadjMat.Add(newRow);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nforeach (List<int> row in adjMat) {\nrow.Add(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\npublic void removeVertex(int index) {\nif (index >= size())\nthrow new IndexOutOfRangeException();\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nvertices.RemoveAt(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nadjMat.RemoveAt(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nforeach (List<int> row in adjMat) {\nrow.RemoveAt(index);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\npublic void addEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\nthrow new IndexOutOfRangeException();\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nadjMat[i][j] = 1;\nadjMat[j][i] = 1;\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\npublic void removeEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\nthrow new IndexOutOfRangeException();\nadjMat[i][j] = 0;\nadjMat[j][i] = 0;\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\npublic void print() {\nConsole.Write(\"\u9876\u70b9\u5217\u8868 = \");\nPrintUtil.PrintList(vertices);\nConsole.WriteLine(\"\u90bb\u63a5\u77e9\u9635 =\");\nPrintUtil.PrintMatrix(adjMat);\n}\n}\n
    graph_adjacency_matrix.swift
    /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nprivate 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\nprivate var adjMat: [[Int]] // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n/* \u6784\u9020\u65b9\u6cd5 */\ninit(vertices: [Int], edges: [[Int]]) {\nself.vertices = []\nadjMat = []\n// \u6dfb\u52a0\u9876\u70b9\nfor val in vertices {\naddVertex(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\nfor e in edges {\naddEdge(i: e[0], j: e[1])\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nfunc size() -> Int {\nvertices.count\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nfunc addVertex(val: Int) {\nlet n = size()\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nvertices.append(val)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nlet newRow = Array(repeating: 0, count: n)\nadjMat.append(newRow)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor i in adjMat.indices {\nadjMat[i].append(0)\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\nfunc removeVertex(index: Int) {\nif index >= size() {\nfatalError(\"\u8d8a\u754c\")\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nvertices.remove(at: index)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nadjMat.remove(at: index)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor i in adjMat.indices {\nadjMat[i].remove(at: index)\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfunc addEdge(i: Int, j: Int) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif i < 0 || j < 0 || i >= size() || j >= size() || i == j {\nfatalError(\"\u8d8a\u754c\")\n}\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nadjMat[i][j] = 1\nadjMat[j][i] = 1\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfunc removeEdge(i: Int, j: Int) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif i < 0 || j < 0 || i >= size() || j >= size() || i == j {\nfatalError(\"\u8d8a\u754c\")\n}\nadjMat[i][j] = 0\nadjMat[j][i] = 0\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nfunc print() {\nSwift.print(\"\u9876\u70b9\u5217\u8868 = \", terminator: \"\")\nSwift.print(vertices)\nSwift.print(\"\u90bb\u63a5\u77e9\u9635 =\")\nPrintUtil.printMatrix(matrix: adjMat)\n}\n}\n
    graph_adjacency_matrix.zig
    \n
    graph_adjacency_matrix.dart
    /* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nList<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\nList<List<int>> adjMat = []; //\u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n/* \u6784\u9020\u65b9\u6cd5 */\nGraphAdjMat(List<int> vertices, List<List<int>> edges) {\nthis.vertices = [];\nthis.adjMat = [];\n// \u6dfb\u52a0\u9876\u70b9\nfor (int val in vertices) {\naddVertex(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\nfor (List<int> e in edges) {\naddEdge(e[0], e[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nint size() {\nreturn vertices.length;\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(int val) {\nint n = size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nvertices.add(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nList<int> newRow = List.filled(n, 0, growable: true);\nadjMat.add(newRow);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor (List<int> row in adjMat) {\nrow.add(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(int index) {\nif (index >= size()) {\nthrow IndexError;\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nvertices.removeAt(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nadjMat.removeAt(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor (List<int> row in adjMat) {\nrow.removeAt(index);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid addEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\nthrow IndexError;\n}\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nadjMat[i][j] = 1;\nadjMat[j][i] = 1;\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid removeEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\nthrow IndexError;\n}\nadjMat[i][j] = 0;\nadjMat[j][i] = 0;\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nvoid printAdjMat() {\nprint(\"\u9876\u70b9\u5217\u8868 = $vertices\");\nprint(\"\u90bb\u63a5\u77e9\u9635 = \");\nprintMatrix(adjMat);\n}\n}\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\u6709\uff1a

    • \u6dfb\u52a0\u8fb9\uff1a\u5728\u9876\u70b9\u5bf9\u5e94\u94fe\u8868\u7684\u672b\u5c3e\u6dfb\u52a0\u8fb9\u5373\u53ef\uff0c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002\u56e0\u4e3a\u662f\u65e0\u5411\u56fe\uff0c\u6240\u4ee5\u9700\u8981\u540c\u65f6\u6dfb\u52a0\u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u3002
    • \u5220\u9664\u8fb9\uff1a\u5728\u9876\u70b9\u5bf9\u5e94\u94fe\u8868\u4e2d\u67e5\u627e\u5e76\u5220\u9664\u6307\u5b9a\u8fb9\uff0c\u4f7f\u7528 \\(O(m)\\) \u65f6\u95f4\u3002\u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u9700\u8981\u540c\u65f6\u5220\u9664\u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u3002
    • \u6dfb\u52a0\u9876\u70b9\uff1a\u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u94fe\u8868\uff0c\u5e76\u5c06\u65b0\u589e\u9876\u70b9\u4f5c\u4e3a\u94fe\u8868\u5934\u8282\u70b9\uff0c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002
    • \u5220\u9664\u9876\u70b9\uff1a\u9700\u904d\u5386\u6574\u4e2a\u90bb\u63a5\u8868\uff0c\u5220\u9664\u5305\u542b\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u8fb9\uff0c\u4f7f\u7528 \\(O(n + m)\\) \u65f6\u95f4\u3002
    • \u521d\u59cb\u5316\uff1a\u5728\u90bb\u63a5\u8868\u4e2d\u521b\u5efa \\(n\\) \u4e2a\u9876\u70b9\u548c \\(2m\\) \u6761\u8fb9\uff0c\u4f7f\u7528 \\(O(n + m)\\) \u65f6\u95f4\u3002
    \u521d\u59cb\u5316\u90bb\u63a5\u8868\u6dfb\u52a0\u8fb9\u5220\u9664\u8fb9\u6dfb\u52a0\u9876\u70b9\u5220\u9664\u9876\u70b9

    \u4ee5\u4e0b\u662f\u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u56fe\u7684\u4ee3\u7801\u793a\u4f8b\u3002\u7ec6\u5fc3\u7684\u540c\u5b66\u53ef\u80fd\u6ce8\u610f\u5230\uff0c\u6211\u4eec\u5728\u90bb\u63a5\u8868\u4e2d\u4f7f\u7528 Vertex \u8282\u70b9\u7c7b\u6765\u8868\u793a\u9876\u70b9\uff0c\u8fd9\u6837\u505a\u7684\u539f\u56e0\u6709\uff1a

    • \u5982\u679c\u6211\u4eec\u9009\u62e9\u901a\u8fc7\u9876\u70b9\u503c\u6765\u533a\u5206\u4e0d\u540c\u9876\u70b9\uff0c\u90a3\u4e48\u503c\u91cd\u590d\u7684\u9876\u70b9\u5c06\u65e0\u6cd5\u88ab\u533a\u5206\u3002
    • \u5982\u679c\u7c7b\u4f3c\u90bb\u63a5\u77e9\u9635\u90a3\u6837\uff0c\u4f7f\u7528\u9876\u70b9\u5217\u8868\u7d22\u5f15\u6765\u533a\u5206\u4e0d\u540c\u9876\u70b9\u3002\u90a3\u4e48\uff0c\u5047\u8bbe\u6211\u4eec\u60f3\u8981\u5220\u9664\u7d22\u5f15\u4e3a \\(i\\) \u7684\u9876\u70b9\uff0c\u5219\u9700\u8981\u904d\u5386\u6574\u4e2a\u90bb\u63a5\u8868\uff0c\u5c06\u5176\u4e2d \\(> i\\) \u7684\u7d22\u5f15\u5168\u90e8\u51cf \\(1\\) \uff0c\u8fd9\u6837\u64cd\u4f5c\u6548\u7387\u8f83\u4f4e\u3002
    • \u56e0\u6b64\u6211\u4eec\u8003\u8651\u5f15\u5165\u9876\u70b9\u7c7b Vertex \uff0c\u4f7f\u5f97\u6bcf\u4e2a\u9876\u70b9\u90fd\u662f\u552f\u4e00\u7684\u5bf9\u8c61\uff0c\u6b64\u65f6\u5220\u9664\u9876\u70b9\u65f6\u5c31\u65e0\u9700\u6539\u52a8\u5176\u4f59\u9876\u70b9\u4e86\u3002
    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart graph_adjacency_list.java
    /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n// \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nMap<Vertex, List<Vertex>> adjList;\n/* \u6784\u9020\u65b9\u6cd5 */\npublic GraphAdjList(Vertex[][] edges) {\nthis.adjList = new HashMap<>();\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor (Vertex[] edge : edges) {\naddVertex(edge[0]);\naddVertex(edge[1]);\naddEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\npublic int size() {\nreturn adjList.size();\n}\n/* \u6dfb\u52a0\u8fb9 */\npublic void addEdge(Vertex vet1, Vertex vet2) {\nif (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\nthrow new IllegalArgumentException();\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nadjList.get(vet1).add(vet2);\nadjList.get(vet2).add(vet1);\n}\n/* \u5220\u9664\u8fb9 */\npublic void removeEdge(Vertex vet1, Vertex vet2) {\nif (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\nthrow new IllegalArgumentException();\n// \u5220\u9664\u8fb9 vet1 - vet2\nadjList.get(vet1).remove(vet2);\nadjList.get(vet2).remove(vet1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npublic void addVertex(Vertex vet) {\nif (adjList.containsKey(vet))\nreturn;\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nadjList.put(vet, new ArrayList<>());\n}\n/* \u5220\u9664\u9876\u70b9 */\npublic void removeVertex(Vertex vet) {\nif (!adjList.containsKey(vet))\nthrow new IllegalArgumentException();\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nadjList.remove(vet);\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor (List<Vertex> list : adjList.values()) {\nlist.remove(vet);\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\npublic void print() {\nSystem.out.println(\"\u90bb\u63a5\u8868 =\");\nfor (Map.Entry<Vertex, List<Vertex>> pair : adjList.entrySet()) {\nList<Integer> tmp = new ArrayList<>();\nfor (Vertex vertex : pair.getValue())\ntmp.add(vertex.val);\nSystem.out.println(pair.getKey().val + \": \" + tmp + \",\");\n}\n}\n}\n
    graph_adjacency_list.cpp
    /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\npublic:\n// \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nunordered_map<Vertex *, vector<Vertex *>> adjList;\n/* \u5728 vector \u4e2d\u5220\u9664\u6307\u5b9a\u8282\u70b9 */\nvoid remove(vector<Vertex *> &vec, Vertex *vet) {\nfor (int i = 0; i < vec.size(); i++) {\nif (vec[i] == vet) {\nvec.erase(vec.begin() + i);\nbreak;\n}\n}\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nGraphAdjList(const vector<vector<Vertex *>> &edges) {\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor (const vector<Vertex *> &edge : edges) {\naddVertex(edge[0]);\naddVertex(edge[1]);\naddEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nint size() {\nreturn adjList.size();\n}\n/* \u6dfb\u52a0\u8fb9 */\nvoid addEdge(Vertex *vet1, Vertex *vet2) {\nif (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\nthrow invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nadjList[vet1].push_back(vet2);\nadjList[vet2].push_back(vet1);\n}\n/* \u5220\u9664\u8fb9 */\nvoid removeEdge(Vertex *vet1, Vertex *vet2) {\nif (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\nthrow invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n// \u5220\u9664\u8fb9 vet1 - vet2\nremove(adjList[vet1], vet2);\nremove(adjList[vet2], vet1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(Vertex *vet) {\nif (adjList.count(vet))\nreturn;\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nadjList[vet] = vector<Vertex *>();\n}\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(Vertex *vet) {\nif (!adjList.count(vet))\nthrow invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nadjList.erase(vet);\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor (auto &adj : adjList) {\nremove(adj.second, vet);\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\nvoid print() {\ncout << \"\u90bb\u63a5\u8868 =\" << endl;\nfor (auto &adj : adjList) {\nconst auto &key = adj.first;\nconst auto &vec = adj.second;\ncout << key->val << \": \";\nprintVector(vetsToVals(vec));\n}\n}\n};\n
    graph_adjacency_list.py
    class GraphAdjList:\n\"\"\"\u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\"\"\"\ndef __init__(self, edges: list[list[Vertex]]) -> None:\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n# \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nself.adj_list = dict[Vertex, Vertex]()\n# \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor edge in edges:\nself.add_vertex(edge[0])\nself.add_vertex(edge[1])\nself.add_edge(edge[0], edge[1])\ndef size(self) -> int:\n\"\"\"\u83b7\u53d6\u9876\u70b9\u6570\u91cf\"\"\"\nreturn len(self.adj_list)\ndef add_edge(self, vet1: Vertex, vet2: Vertex) -> None:\n\"\"\"\u6dfb\u52a0\u8fb9\"\"\"\nif vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\nraise ValueError()\n# \u6dfb\u52a0\u8fb9 vet1 - vet2\nself.adj_list[vet1].append(vet2)\nself.adj_list[vet2].append(vet1)\ndef remove_edge(self, vet1: Vertex, vet2: Vertex) -> None:\n\"\"\"\u5220\u9664\u8fb9\"\"\"\nif vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\nraise ValueError()\n# \u5220\u9664\u8fb9 vet1 - vet2\nself.adj_list[vet1].remove(vet2)\nself.adj_list[vet2].remove(vet1)\ndef add_vertex(self, vet: Vertex) -> None:\n\"\"\"\u6dfb\u52a0\u9876\u70b9\"\"\"\nif vet in self.adj_list:\nreturn\n# \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nself.adj_list[vet] = []\ndef remove_vertex(self, vet: Vertex) -> None:\n\"\"\"\u5220\u9664\u9876\u70b9\"\"\"\nif vet not in self.adj_list:\nraise ValueError()\n# \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nself.adj_list.pop(vet)\n# \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor vertex in self.adj_list:\nif vet in self.adj_list[vertex]:\nself.adj_list[vertex].remove(vet)\ndef print(self) -> None:\n\"\"\"\u6253\u5370\u90bb\u63a5\u8868\"\"\"\nprint(\"\u90bb\u63a5\u8868 =\")\nfor vertex in self.adj_list:\ntmp = [v.val for v in self.adj_list[vertex]]\nprint(f\"{vertex.val}: {tmp},\")\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: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nadjList map[Vertex][]Vertex\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newGraphAdjList(edges [][]Vertex) *graphAdjList {\ng := &graphAdjList{\nadjList: make(map[Vertex][]Vertex),\n}\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor _, edge := range edges {\ng.addVertex(edge[0])\ng.addVertex(edge[1])\ng.addEdge(edge[0], edge[1])\n}\nreturn g\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nfunc (g *graphAdjList) size() int {\nreturn len(g.adjList)\n}\n/* \u6dfb\u52a0\u8fb9 */\nfunc (g *graphAdjList) addEdge(vet1 Vertex, vet2 Vertex) {\n_, ok1 := g.adjList[vet1]\n_, ok2 := g.adjList[vet2]\nif !ok1 || !ok2 || vet1 == vet2 {\npanic(\"error\")\n}\n// \u6dfb\u52a0\u8fb9 vet1 - vet2, \u6dfb\u52a0\u533f\u540d struct{},\ng.adjList[vet1] = append(g.adjList[vet1], vet2)\ng.adjList[vet2] = append(g.adjList[vet2], vet1)\n}\n/* \u5220\u9664\u8fb9 */\nfunc (g *graphAdjList) removeEdge(vet1 Vertex, vet2 Vertex) {\n_, ok1 := g.adjList[vet1]\n_, ok2 := g.adjList[vet2]\nif !ok1 || !ok2 || vet1 == vet2 {\npanic(\"error\")\n}\n// \u5220\u9664\u8fb9 vet1 - vet2\nDeleteSliceElms(g.adjList[vet1], vet2)\nDeleteSliceElms(g.adjList[vet2], vet1)\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nfunc (g *graphAdjList) addVertex(vet Vertex) {\n_, ok := g.adjList[vet]\nif ok {\nreturn\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\ng.adjList[vet] = make([]Vertex, 0)\n}\n/* \u5220\u9664\u9876\u70b9 */\nfunc (g *graphAdjList) removeVertex(vet Vertex) {\n_, ok := g.adjList[vet]\nif !ok {\npanic(\"error\")\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\ndelete(g.adjList, vet)\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor _, list := range g.adjList {\nDeleteSliceElms(list, vet)\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\nfunc (g *graphAdjList) print() {\nvar builder strings.Builder\nfmt.Printf(\"\u90bb\u63a5\u8868 = \\n\")\nfor k, v := range g.adjList {\nbuilder.WriteString(\"\\t\\t\" + strconv.Itoa(k.Val) + \": \")\nfor _, vet := range v {\nbuilder.WriteString(strconv.Itoa(vet.Val) + \" \")\n}\nfmt.Println(builder.String())\nbuilder.Reset()\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: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nadjList;\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor(edges) {\nthis.adjList = new Map();\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor (const edge of edges) {\nthis.addVertex(edge[0]);\nthis.addVertex(edge[1]);\nthis.addEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nsize() {\nreturn this.adjList.size;\n}\n/* \u6dfb\u52a0\u8fb9 */\naddEdge(vet1, vet2) {\nif (\n!this.adjList.has(vet1) ||\n!this.adjList.has(vet2) ||\nvet1 === vet2\n) {\nthrow new Error('Illegal Argument Exception');\n}\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nthis.adjList.get(vet1).push(vet2);\nthis.adjList.get(vet2).push(vet1);\n}\n/* \u5220\u9664\u8fb9 */\nremoveEdge(vet1, vet2) {\nif (\n!this.adjList.has(vet1) ||\n!this.adjList.has(vet2) ||\nvet1 === vet2\n) {\nthrow new Error('Illegal Argument Exception');\n}\n// \u5220\u9664\u8fb9 vet1 - vet2\nthis.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\nthis.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\naddVertex(vet) {\nif (this.adjList.has(vet)) return;\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nthis.adjList.set(vet, []);\n}\n/* \u5220\u9664\u9876\u70b9 */\nremoveVertex(vet) {\nif (!this.adjList.has(vet)) {\nthrow new Error('Illegal Argument Exception');\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nthis.adjList.delete(vet);\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor (let set of this.adjList.values()) {\nconst index = set.indexOf(vet);\nif (index > -1) {\nset.splice(index, 1);\n}\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\nprint() {\nconsole.log('\u90bb\u63a5\u8868 =');\nfor (const [key, value] of this.adjList) {\nconst tmp = [];\nfor (const vertex of value) {\ntmp.push(vertex.val);\n}\nconsole.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: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nadjList: Map<Vertex, Vertex[]>;\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor(edges: Vertex[][]) {\nthis.adjList = new Map();\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor (const edge of edges) {\nthis.addVertex(edge[0]);\nthis.addVertex(edge[1]);\nthis.addEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nsize(): number {\nreturn this.adjList.size;\n}\n/* \u6dfb\u52a0\u8fb9 */\naddEdge(vet1: Vertex, vet2: Vertex): void {\nif (\n!this.adjList.has(vet1) ||\n!this.adjList.has(vet2) ||\nvet1 === vet2\n) {\nthrow new Error('Illegal Argument Exception');\n}\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nthis.adjList.get(vet1).push(vet2);\nthis.adjList.get(vet2).push(vet1);\n}\n/* \u5220\u9664\u8fb9 */\nremoveEdge(vet1: Vertex, vet2: Vertex): void {\nif (\n!this.adjList.has(vet1) ||\n!this.adjList.has(vet2) ||\nvet1 === vet2\n) {\nthrow new Error('Illegal Argument Exception');\n}\n// \u5220\u9664\u8fb9 vet1 - vet2\nthis.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\nthis.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\naddVertex(vet: Vertex): void {\nif (this.adjList.has(vet)) return;\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nthis.adjList.set(vet, []);\n}\n/* \u5220\u9664\u9876\u70b9 */\nremoveVertex(vet: Vertex): void {\nif (!this.adjList.has(vet)) {\nthrow new Error('Illegal Argument Exception');\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nthis.adjList.delete(vet);\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor (let set of this.adjList.values()) {\nconst index: number = set.indexOf(vet);\nif (index > -1) {\nset.splice(index, 1);\n}\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\nprint(): void {\nconsole.log('\u90bb\u63a5\u8868 =');\nfor (const [key, value] of this.adjList.entries()) {\nconst tmp = [];\nfor (const vertex of value) {\ntmp.push(vertex.val);\n}\nconsole.log(key.val + ': ' + tmp.join());\n}\n}\n}\n
    graph_adjacency_list.c
    [class]{graphAdjList}-[func]{}\n
    graph_adjacency_list.cs
    /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n// \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\npublic Dictionary<Vertex, List<Vertex>> adjList;\n/* \u6784\u9020\u51fd\u6570 */\npublic GraphAdjList(Vertex[][] edges) {\nthis.adjList = new Dictionary<Vertex, List<Vertex>>();\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nforeach (Vertex[] edge in edges) {\naddVertex(edge[0]);\naddVertex(edge[1]);\naddEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\npublic int size() {\nreturn adjList.Count;\n}\n/* \u6dfb\u52a0\u8fb9 */\npublic void addEdge(Vertex vet1, Vertex vet2) {\nif (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\nthrow new InvalidOperationException();\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nadjList[vet1].Add(vet2);\nadjList[vet2].Add(vet1);\n}\n/* \u5220\u9664\u8fb9 */\npublic void removeEdge(Vertex vet1, Vertex vet2) {\nif (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\nthrow new InvalidOperationException();\n// \u5220\u9664\u8fb9 vet1 - vet2\nadjList[vet1].Remove(vet2);\nadjList[vet2].Remove(vet1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npublic void addVertex(Vertex vet) {\nif (adjList.ContainsKey(vet))\nreturn;\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nadjList.Add(vet, new List<Vertex>());\n}\n/* \u5220\u9664\u9876\u70b9 */\npublic void removeVertex(Vertex vet) {\nif (!adjList.ContainsKey(vet))\nthrow new InvalidOperationException();\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nadjList.Remove(vet);\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nforeach (List<Vertex> list in adjList.Values) {\nlist.Remove(vet);\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\npublic void print() {\nConsole.WriteLine(\"\u90bb\u63a5\u8868 =\");\nforeach (KeyValuePair<Vertex, List<Vertex>> pair in adjList) {\nList<int> tmp = new List<int>();\nforeach (Vertex vertex in pair.Value)\ntmp.Add(vertex.val);\nConsole.WriteLine(pair.Key.val + \": [\" + string.Join(\", \", tmp) + \"],\");\n}\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: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\npublic private(set) var adjList: [Vertex: [Vertex]]\n/* \u6784\u9020\u65b9\u6cd5 */\npublic init(edges: [[Vertex]]) {\nadjList = [:]\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor edge in edges {\naddVertex(vet: edge[0])\naddVertex(vet: edge[1])\naddEdge(vet1: edge[0], vet2: edge[1])\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\npublic func size() -> Int {\nadjList.count\n}\n/* \u6dfb\u52a0\u8fb9 */\npublic func addEdge(vet1: Vertex, vet2: Vertex) {\nif adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\nfatalError(\"\u53c2\u6570\u9519\u8bef\")\n}\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nadjList[vet1]?.append(vet2)\nadjList[vet2]?.append(vet1)\n}\n/* \u5220\u9664\u8fb9 */\npublic func removeEdge(vet1: Vertex, vet2: Vertex) {\nif adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\nfatalError(\"\u53c2\u6570\u9519\u8bef\")\n}\n// \u5220\u9664\u8fb9 vet1 - vet2\nadjList[vet1]?.removeAll(where: { $0 == vet2 })\nadjList[vet2]?.removeAll(where: { $0 == vet1 })\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npublic func addVertex(vet: Vertex) {\nif adjList[vet] != nil {\nreturn\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nadjList[vet] = []\n}\n/* \u5220\u9664\u9876\u70b9 */\npublic func removeVertex(vet: Vertex) {\nif adjList[vet] == nil {\nfatalError(\"\u53c2\u6570\u9519\u8bef\")\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nadjList.removeValue(forKey: vet)\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor key in adjList.keys {\nadjList[key]?.removeAll(where: { $0 == vet })\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\npublic func print() {\nSwift.print(\"\u90bb\u63a5\u8868 =\")\nfor pair in adjList {\nvar tmp: [Int] = []\nfor vertex in pair.value {\ntmp.append(vertex.val)\n}\nSwift.print(\"\\(pair.key.val): \\(tmp),\")\n}\n}\n}\n
    graph_adjacency_list.zig
    [class]{GraphAdjList}-[func]{}\n
    graph_adjacency_list.dart
    /* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n// \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nMap<Vertex, List<Vertex>> adjList = {};\n/* \u6784\u9020\u65b9\u6cd5 */\nGraphAdjList(List<List<Vertex>> edges) {\nfor (List<Vertex> edge in edges) {\naddVertex(edge[0]);\naddVertex(edge[1]);\naddEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nint size() {\nreturn adjList.length;\n}\n/* \u6dfb\u52a0\u8fb9 */\nvoid addEdge(Vertex vet1, Vertex vet2) {\nif (!adjList.containsKey(vet1) ||\n!adjList.containsKey(vet2) ||\nvet1 == vet2) {\nthrow ArgumentError;\n}\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nadjList[vet1]!.add(vet2);\nadjList[vet2]!.add(vet1);\n}\n/* \u5220\u9664\u8fb9 */\nvoid removeEdge(Vertex vet1, Vertex vet2) {\nif (!adjList.containsKey(vet1) ||\n!adjList.containsKey(vet2) ||\nvet1 == vet2) {\nthrow ArgumentError;\n}\n// \u5220\u9664\u8fb9 vet1 - vet2\nadjList[vet1]!.remove(vet2);\nadjList[vet2]!.remove(vet1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(Vertex vet) {\nif (adjList.containsKey(vet)) return;\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nadjList[vet] = [];\n}\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(Vertex vet) {\nif (!adjList.containsKey(vet)) {\nthrow ArgumentError;\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nadjList.remove(vet);\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nadjList.forEach((key, value) {\nvalue.remove(vet);\n});\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\nvoid printAdjList() {\nprint(\"\u90bb\u63a5\u8868 =\");\nadjList.forEach((key, value) {\nList<int> tmp = [];\nfor (Vertex vertex in value) {\ntmp.add(vertex.val);\n}\nprint(\"${key.val}: $tmp,\");\n});\n}\n}\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\u4e0b\u8868\u4e3a\u90bb\u63a5\u77e9\u9635\u548c\u90bb\u63a5\u8868\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u6548\u7387\u5bf9\u6bd4\u3002

    \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\u4e0a\u8868\uff0c\u4f3c\u4e4e\u90bb\u63a5\u8868\uff08\u54c8\u5e0c\u8868\uff09\u7684\u65f6\u95f4\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\u8981\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":"

    \u56fe\u4e0e\u6811\u7684\u5173\u7cfb

    \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\u662f\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\uff0c\u5efa\u8bae\u4f60\u5728\u5b66\u4e60\u672c\u7ae0\u8282\u65f6\u878d\u4f1a\u8d2f\u901a\u4e24\u8005\u7684\u6982\u5ff5\u4e0e\u5b9e\u73b0\u65b9\u6cd5\u3002

    \u300c\u56fe\u300d\u548c\u300c\u6811\u300d\u90fd\u662f\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u90fd\u9700\u8981\u4f7f\u7528\u300c\u641c\u7d22\u7b97\u6cd5\u300d\u6765\u5b9e\u73b0\u904d\u5386\u64cd\u4f5c\u3002

    \u4e0e\u6811\u7c7b\u4f3c\uff0c\u56fe\u7684\u904d\u5386\u65b9\u5f0f\u4e5f\u53ef\u5206\u4e3a\u4e24\u79cd\uff0c\u5373\u300c\u5e7f\u5ea6\u4f18\u5148\u904d\u5386 Breadth-First Traversal\u300d\u548c\u300c\u6df1\u5ea6\u4f18\u5148\u904d\u5386 Depth-First Traversal\u300d\uff0c\u4e5f\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\u8ddd\u79bb\u6700\u8fd1\u7684\u9876\u70b9\u5f00\u59cb\u8bbf\u95ee\uff0c\u5e76\u4e00\u5c42\u5c42\u5411\u5916\u6269\u5f20\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u4ece\u67d0\u4e2a\u9876\u70b9\u51fa\u53d1\uff0c\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

    Fig. \u56fe\u7684\u5e7f\u5ea6\u4f18\u5148\u904d\u5386

    "},{"location":"chapter_graph/graph_traversal/#_1","title":"\u7b97\u6cd5\u5b9e\u73b0","text":"

    BFS \u901a\u5e38\u501f\u52a9\u300c\u961f\u5217\u300d\u6765\u5b9e\u73b0\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

    1. \u5c06\u904d\u5386\u8d77\u59cb\u9876\u70b9 startVet \u52a0\u5165\u961f\u5217\uff0c\u5e76\u5f00\u542f\u5faa\u73af\uff1b
    2. \u5728\u5faa\u73af\u7684\u6bcf\u8f6e\u8fed\u4ee3\u4e2d\uff0c\u5f39\u51fa\u961f\u9996\u9876\u70b9\u5e76\u8bb0\u5f55\u8bbf\u95ee\uff0c\u7136\u540e\u5c06\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\u52a0\u5165\u5230\u961f\u5217\u5c3e\u90e8\uff1b
    3. \u5faa\u73af\u6b65\u9aa4 2. \uff0c\u76f4\u5230\u6240\u6709\u9876\u70b9\u88ab\u8bbf\u95ee\u5b8c\u6210\u540e\u7ed3\u675f\uff1b

    \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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nList<Vertex> res = new ArrayList<>();\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nSet<Vertex> visited = new HashSet<>() {{ add(startVet); }};\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nQueue<Vertex> que = new LinkedList<>() {{ offer(startVet); }};\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile (!que.isEmpty()) {\nVertex vet = que.poll(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.add(vet);            // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (Vertex adjVet : graph.adjList.get(vet)) {\nif (visited.contains(adjVet))\ncontinue;        // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nque.offer(adjVet);   // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res;\n}\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\nvector<Vertex *> res;\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nunordered_set<Vertex *> visited = {startVet};\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nqueue<Vertex *> que;\nque.push(startVet);\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile (!que.empty()) {\nVertex *vet = que.front();\nque.pop();          // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.push_back(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (auto adjVet : graph.adjList[vet]) {\nif (visited.count(adjVet))\ncontinue;            // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nque.push(adjVet);        // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.emplace(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res;\n}\n
    graph_bfs.py
    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\nres = []\n# \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nvisited = set[Vertex]([start_vet])\n# \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nque = deque[Vertex]([start_vet])\n# \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile len(que) > 0:\nvet = que.popleft()  # \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.append(vet)  # \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n# \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor adj_vet in graph.adj_list[vet]:\nif adj_vet in visited:\ncontinue  # \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nque.append(adj_vet)  # \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.add(adj_vet)  # \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n# \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res\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\nres := make([]Vertex, 0)\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nvisited := make(map[Vertex]struct{})\nvisited[startVet] = struct{}{}\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS, \u4f7f\u7528\u5207\u7247\u6a21\u62df\u961f\u5217\nqueue := make([]Vertex, 0)\nqueue = append(queue, startVet)\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nfor len(queue) > 0 {\n// \u961f\u9996\u9876\u70b9\u51fa\u961f\nvet := queue[0]\nqueue = queue[1:]\n// \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nres = append(res, vet)\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor _, adjVet := range g.adjList[vet] {\n_, isExist := visited[adjVet]\n// \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nif !isExist {\nqueue = append(queue, adjVet)\nvisited[adjVet] = struct{}{}\n}\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn 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\nconst res = [];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nconst visited = new Set();\nvisited.add(startVet);\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nconst que = [startVet];\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile (que.length) {\nconst vet = que.shift(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (const adjVet of graph.adjList.get(vet) ?? []) {\nif (visited.has(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nque.push(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn 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\nconst res: Vertex[] = [];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nconst visited: Set<Vertex> = new Set();\nvisited.add(startVet);\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nconst que = [startVet];\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile (que.length) {\nconst vet = que.shift(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (const adjVet of graph.adjList.get(vet) ?? []) {\nif (visited.has(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nque.push(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\nvisited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res;\n}\n
    graph_bfs.c
    [class]{}-[func]{graphBFS}\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\nList<Vertex> res = new List<Vertex>();\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nHashSet<Vertex> visited = new HashSet<Vertex>() { startVet };\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nQueue<Vertex> que = new Queue<Vertex>();\nque.Enqueue(startVet);\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile (que.Count > 0) {\nVertex vet = que.Dequeue(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.Add(vet);               // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nforeach (Vertex adjVet in graph.adjList[vet]) {\nif (visited.Contains(adjVet)) {\ncontinue;          // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nque.Enqueue(adjVet);   // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.Add(adjVet);   // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn 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\nvar res: [Vertex] = []\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nvar visited: Set<Vertex> = [startVet]\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nvar que: [Vertex] = [startVet]\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile !que.isEmpty {\nlet vet = que.removeFirst() // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.append(vet) // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor adjVet in graph.adjList[vet] ?? [] {\nif visited.contains(adjVet) {\ncontinue // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nque.append(adjVet) // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.insert(adjVet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res\n}\n
    graph_bfs.zig
    [class]{}-[func]{graphBFS}\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\nList<Vertex> res = [];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nSet<Vertex> visited = {};\nvisited.add(startVet);\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nQueue<Vertex> que = Queue();\nque.add(startVet);\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile (que.isNotEmpty) {\nVertex vet = que.removeFirst(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (Vertex adjVet in graph.adjList[vet]!) {\nif (visited.contains(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nque.add(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res;\n}\n

    \u4ee3\u7801\u76f8\u5bf9\u62bd\u8c61\uff0c\u5efa\u8bae\u5bf9\u7167\u4ee5\u4e0b\u52a8\u753b\u56fe\u793a\u6765\u52a0\u6df1\u7406\u89e3\u3002

    <1><2><3><4><5><6><7><8><9><10><11>

    \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\u662f\u5141\u8bb8\u88ab\u4efb\u610f\u6253\u4e71\u7684\u3002\u4ee5\u4e0a\u56fe\u4e3a\u4f8b\uff0c\u9876\u70b9 \\(1\\) , \\(3\\) \u7684\u8bbf\u95ee\u987a\u5e8f\u53ef\u4ee5\u4ea4\u6362\u3001\u9876\u70b9 \\(2\\) , \\(4\\) , \\(6\\) \u7684\u8bbf\u95ee\u987a\u5e8f\u4e5f\u53ef\u4ee5\u4efb\u610f\u4ea4\u6362\u3002

    "},{"location":"chapter_graph/graph_traversal/#_2","title":"\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

    "},{"location":"chapter_graph/graph_traversal/#932","title":"9.3.2. \u00a0 \u6df1\u5ea6\u4f18\u5148\u904d\u5386","text":"

    \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\u5177\u4f53\u5730\uff0c\u4ece\u67d0\u4e2a\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

    Fig. \u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u904d\u5386

    "},{"location":"chapter_graph/graph_traversal/#_3","title":"\u7b97\u6cd5\u5b9e\u73b0","text":"

    \u8fd9\u79cd\u201c\u8d70\u5230\u5c3d\u5934 + \u56de\u6eaf\u201d\u7684\u7b97\u6cd5\u5f62\u5f0f\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u6765\u5b9e\u73b0\u3002\u4e0e BFS \u7c7b\u4f3c\uff0c\u5728 DFS \u4e2d\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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) {\nres.add(vet);     // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (Vertex adjVet : graph.adjList.get(vet)) {\nif (visited.contains(adjVet))\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adjVet);\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\nList<Vertex> res = new ArrayList<>();\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nSet<Vertex> visited = new HashSet<>();\ndfs(graph, visited, res, startVet);\nreturn res;\n}\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) {\nres.push_back(vet);   // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.emplace(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (Vertex *adjVet : graph.adjList[vet]) {\nif (visited.count(adjVet))\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adjVet);\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\nvector<Vertex *> res;\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nunordered_set<Vertex *> visited;\ndfs(graph, visited, res, startVet);\nreturn res;\n}\n
    graph_dfs.py
    def dfs(graph: GraphAdjList, visited: set[Vertex], res: list[Vertex], vet: Vertex):\n\"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570\"\"\"\nres.append(vet)  # \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.add(vet)  # \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n# \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor adjVet in graph.adj_list[vet]:\nif adjVet in visited:\ncontinue  # \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n# \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adjVet)\ndef graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:\n\"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS\"\"\"\n# \u9876\u70b9\u904d\u5386\u5e8f\u5217\nres = []\n# \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nvisited = set[Vertex]()\ndfs(graph, visited, res, start_vet)\nreturn res\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)\nvisited[vet] = struct{}{}\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor _, adjVet := range g.adjList[vet] {\n_, isExist := visited[adjVet]\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\nif !isExist {\ndfs(g, 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\nfunc graphDFS(g *graphAdjList, startVet Vertex) []Vertex {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nres := make([]Vertex, 0)\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nvisited := make(map[Vertex]struct{})\ndfs(g, visited, &res, startVet)\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn 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) {\nres.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (const adjVet of graph.adjList.get(vet)) {\nif (visited.has(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adjVet);\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\nconst res = [];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nconst visited = new Set();\ndfs(graph, visited, res, startVet);\nreturn res;\n}\n
    graph_dfs.ts
    /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nfunction dfs(\ngraph: GraphAdjList,\nvisited: Set<Vertex>,\nres: Vertex[],\nvet: Vertex\n): void {\nres.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (const adjVet of graph.adjList.get(vet)) {\nif (visited.has(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adjVet);\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\nconst res: Vertex[] = [];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nconst visited: Set<Vertex> = new Set();\ndfs(graph, visited, res, startVet);\nreturn res;\n}\n
    graph_dfs.c
    [class]{}-[func]{dfs}\n[class]{}-[func]{graphDFS}\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) {\nres.Add(vet);     // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.Add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nforeach (Vertex adjVet in graph.adjList[vet]) {\nif (visited.Contains(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9                             \n}\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adjVet);\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\nList<Vertex> res = new List<Vertex>();\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nHashSet<Vertex> visited = new HashSet<Vertex>();\ndfs(graph, visited, res, startVet);\nreturn 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) {\nres.append(vet) // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.insert(vet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor adjVet in graph.adjList[vet] ?? [] {\nif visited.contains(adjVet) {\ncontinue // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph: graph, visited: &visited, res: &res, vet: adjVet)\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\nvar res: [Vertex] = []\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nvar visited: Set<Vertex> = []\ndfs(graph: graph, visited: &visited, res: &res, vet: startVet)\nreturn res\n}\n
    graph_dfs.zig
    [class]{}-[func]{dfs}\n[class]{}-[func]{graphDFS}\n
    graph_dfs.dart
    /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nvoid dfs(\nGraphAdjList graph,\nSet<Vertex> visited,\nList<Vertex> res,\nVertex vet,\n) {\nres.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (Vertex adjVet in graph.adjList[vet]!) {\nif (visited.contains(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adjVet);\n}\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\nList<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nList<Vertex> res = [];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nSet<Vertex> visited = {};\ndfs(graph, visited, res, startVet);\nreturn res;\n}\n

    \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u7b97\u6cd5\u6d41\u7a0b\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5176\u4e2d\uff1a

    • \u76f4\u865a\u7ebf\u4ee3\u8868\u5411\u4e0b\u9012\u63a8\uff0c\u8868\u793a\u5f00\u542f\u4e86\u4e00\u4e2a\u65b0\u7684\u9012\u5f52\u65b9\u6cd5\u6765\u8bbf\u95ee\u65b0\u9876\u70b9\uff1b
    • \u66f2\u865a\u7ebf\u4ee3\u8868\u5411\u4e0a\u56de\u6eaf\uff0c\u8868\u793a\u6b64\u9012\u5f52\u65b9\u6cd5\u5df2\u7ecf\u8fd4\u56de\uff0c\u56de\u6eaf\u5230\u4e86\u5f00\u542f\u6b64\u9012\u5f52\u65b9\u6cd5\u7684\u4f4d\u7f6e\uff1b

    \u4e3a\u4e86\u52a0\u6df1\u7406\u89e3\uff0c\u5efa\u8bae\u5c06\u56fe\u793a\u4e0e\u4ee3\u7801\u7ed3\u5408\u8d77\u6765\uff0c\u5728\u8111\u4e2d\uff08\u6216\u8005\u7528\u7b14\u753b\u4e0b\u6765\uff09\u6a21\u62df\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>

    \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\u3001\u201c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u201d\u3001\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\u4e0d\u540c\u7684\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/#_4","title":"\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

    "},{"location":"chapter_graph/summary/","title":"9.4. \u00a0 \u5c0f\u7ed3","text":"
    • \u56fe\u7531\u9876\u70b9\u548c\u8fb9\u7ec4\u6210\uff0c\u53ef\u4ee5\u88ab\u8868\u793a\u4e3a\u4e00\u7ec4\u9876\u70b9\u548c\u4e00\u7ec4\u8fb9\u6784\u6210\u7684\u96c6\u5408\u3002
    • \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\u5177\u6709\u66f4\u9ad8\u7684\u81ea\u7531\u5ea6\uff0c\u56e0\u800c\u66f4\u4e3a\u590d\u6742\u3002
    • \u6709\u5411\u56fe\u7684\u8fb9\u5177\u6709\u65b9\u5411\u6027\uff0c\u8fde\u901a\u56fe\u4e2d\u7684\u4efb\u610f\u9876\u70b9\u5747\u53ef\u8fbe\uff0c\u6709\u6743\u56fe\u7684\u6bcf\u6761\u8fb9\u90fd\u5305\u542b\u6743\u91cd\u53d8\u91cf\u3002
    • \u90bb\u63a5\u77e9\u9635\u5229\u7528\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\u6709\u8fb9\u6216\u65e0\u8fb9\u3002\u90bb\u63a5\u77e9\u9635\u5728\u589e\u5220\u67e5\u64cd\u4f5c\u4e0a\u6548\u7387\u5f88\u9ad8\uff0c\u4f46\u7a7a\u95f4\u5360\u7528\u8f83\u591a\u3002
    • \u90bb\u63a5\u8868\u4f7f\u7528\u591a\u4e2a\u94fe\u8868\u6765\u8868\u793a\u56fe\uff0c\u7b2c \\(i\\) \u6761\u94fe\u8868\u5bf9\u5e94\u9876\u70b9 \\(i\\) \uff0c\u5176\u4e2d\u5b58\u50a8\u4e86\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\u3002\u90bb\u63a5\u8868\u76f8\u5bf9\u4e8e\u90bb\u63a5\u77e9\u9635\u66f4\u52a0\u8282\u7701\u7a7a\u95f4\uff0c\u4f46\u7531\u4e8e\u9700\u8981\u904d\u5386\u94fe\u8868\u6765\u67e5\u627e\u8fb9\uff0c\u65f6\u95f4\u6548\u7387\u8f83\u4f4e\u3002
    • \u5f53\u90bb\u63a5\u8868\u4e2d\u7684\u94fe\u8868\u8fc7\u957f\u65f6\uff0c\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811\u6216\u54c8\u5e0c\u8868\uff0c\u4ece\u800c\u63d0\u5347\u67e5\u8be2\u6548\u7387\u3002
    • \u4ece\u7b97\u6cd5\u601d\u60f3\u89d2\u5ea6\u5206\u6790\uff0c\u90bb\u63a5\u77e9\u9635\u4f53\u73b0\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\uff0c\u90bb\u63a5\u8868\u4f53\u73b0\u201c\u4ee5\u65f6\u95f4\u6362\u7a7a\u95f4\u201d\u3002
    • \u56fe\u53ef\u7528\u4e8e\u5efa\u6a21\u5404\u7c7b\u73b0\u5b9e\u7cfb\u7edf\uff0c\u5982\u793e\u4ea4\u7f51\u7edc\u3001\u5730\u94c1\u7ebf\u8def\u7b49\u3002
    • \u6811\u662f\u56fe\u7684\u4e00\u79cd\u7279\u4f8b\uff0c\u6811\u7684\u904d\u5386\u4e5f\u662f\u56fe\u7684\u904d\u5386\u7684\u4e00\u79cd\u7279\u4f8b\u3002
    • \u56fe\u7684\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u662f\u4e00\u79cd\u7531\u8fd1\u53ca\u8fdc\u3001\u5c42\u5c42\u6269\u5f20\u7684\u641c\u7d22\u65b9\u5f0f\uff0c\u901a\u5e38\u501f\u52a9\u961f\u5217\u5b9e\u73b0\u3002
    • \u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u662f\u4e00\u79cd\u4f18\u5148\u8d70\u5230\u5e95\u3001\u65e0\u8def\u53ef\u8d70\u65f6\u518d\u56de\u6eaf\u7684\u641c\u7d22\u65b9\u5f0f\uff0c\u5e38\u57fa\u4e8e\u9012\u5f52\u6765\u5b9e\u73b0\u3002
    "},{"location":"chapter_graph/summary/#941-q-a","title":"9.4.1. \u00a0 Q & A","text":"

    \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\u8ba4\u4e3a\u662f\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\uff0c\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\u4f1a\u9700\u8981\u6309\u7167\u6307\u5b9a\u89c4\u5219\u6765\u6392\u5e8f\uff0c\u6bd4\u5982\u6309\u7167\u9876\u70b9\u6dfb\u52a0\u7684\u6b21\u5e8f\u3001\u6216\u8005\u6309\u7167\u9876\u70b9\u503c\u5927\u5c0f\u7684\u987a\u5e8f\u7b49\u7b49\uff0c\u8fd9\u6837\u53ef\u4ee5\u6709\u52a9\u4e8e\u5feb\u901f\u67e5\u627e\u201c\u5e26\u6709\u67d0\u79cd\u6781\u503c\u201d\u7684\u9876\u70b9\u3002

    "},{"location":"chapter_hashing/","title":"6. \u00a0 \u6563\u5217\u8868","text":""},{"location":"chapter_hashing/hash_collision/","title":"6.2. \u00a0 \u54c8\u5e0c\u51b2\u7a81","text":"

    \u5728\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u54c8\u5e0c\u51fd\u6570\u4e3a\u6bcf\u4e2a\u8f93\u5165\u751f\u6210\u552f\u4e00\u7684\u8f93\u51fa\uff0c\u5b9e\u73b0 key \u548c\u6570\u7ec4\u7d22\u5f15\u7684\u4e00\u4e00\u5bf9\u5e94\u3002\u4f46\u5b9e\u9645\u4e0a\uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u8f93\u5165\u7a7a\u95f4\u901a\u5e38\u8fdc\u5927\u4e8e\u8f93\u51fa\u7a7a\u95f4\uff0c\u56e0\u6b64\u591a\u4e2a\u8f93\u5165\u4ea7\u751f\u76f8\u540c\u8f93\u51fa\u7684\u60c5\u51b5\u662f\u4e0d\u53ef\u907f\u514d\u7684\u3002\u4f8b\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\u6570\u7ec4\u7d22\u5f15\u3002

    \u8fd9\u79cd\u591a\u4e2a\u8f93\u5165\u5bf9\u5e94\u540c\u4e00\u8f93\u51fa\u7d22\u5f15\u7684\u73b0\u8c61\u88ab\u79f0\u4e3a\u300c\u54c8\u5e0c\u51b2\u7a81 Hash Collision\u300d\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\u54c8\u5e0c\u51b2\u7a81\u7684\u5904\u7406\u65b9\u6cd5\u4e3b\u8981\u6709\u4e24\u79cd\uff1a

    • \u6269\u5927\u54c8\u5e0c\u8868\u5bb9\u91cf\uff1a\u54c8\u5e0c\u8868\u5bb9\u91cf\u8d8a\u5927\uff0c\u952e\u503c\u5bf9\u805a\u96c6\u7684\u6982\u7387\u5c31\u8d8a\u4f4e\u3002\u6781\u7aef\u60c5\u51b5\u4e0b\uff0c\u5f53\u8f93\u5165\u7a7a\u95f4\u548c\u8f93\u51fa\u7a7a\u95f4\u5927\u5c0f\u76f8\u7b49\u65f6\uff0c\u54c8\u5e0c\u8868\u7b49\u540c\u4e8e\u6570\u7ec4\uff0c\u6bcf\u4e2a key \u90fd\u5bf9\u5e94\u552f\u4e00\u7684\u6570\u7ec4\u7d22\u5f15\u3002
    • \u4f18\u5316\u54c8\u5e0c\u8868\u7ed3\u6784\uff1a\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\u94fe\u5f0f\u5730\u5740\u548c\u5f00\u653e\u5bfb\u5740\u3002\u8fd9\u7c7b\u65b9\u6cd5\u7684\u601d\u8def\u662f\u901a\u8fc7\u6539\u826f\u6570\u636e\u7ed3\u6784\uff0c\u4f7f\u5f97\u54c8\u5e0c\u8868\u53ef\u4ee5\u5728\u53d1\u751f\u54c8\u5e0c\u51b2\u7a81\u65f6\u4ecd\u7136\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u3002\u5f53\u7136\uff0c\u8fd9\u4e9b\u4f18\u5316\u5f80\u5f80\u662f\u4ee5\u727a\u7272\u65f6\u95f4\u6548\u7387\u4e3a\u4ee3\u4ef7\u7684\u3002
    "},{"location":"chapter_hashing/hash_collision/#621","title":"6.2.1. \u00a0 \u54c8\u5e0c\u8868\u6269\u5bb9","text":"

    \u54c8\u5e0c\u51fd\u6570\u7684\u6700\u540e\u4e00\u6b65\u901a\u5e38\u662f\u5bf9\u6876\u6570\u91cf \\(n\\) \u53d6\u4f59\uff0c\u4f5c\u7528\u662f\u5c06\u54c8\u5e0c\u503c\u6620\u5c04\u5230\u6876\u7d22\u5f15\u8303\u56f4\uff0c\u4ece\u800c\u5c06 key \u653e\u5165\u5bf9\u5e94\u7684\u6876\u4e2d\u3002\u5f53\u54c8\u5e0c\u8868\u5bb9\u91cf\u8d8a\u5927\uff08\u5373 \\(n\\) \u8d8a\u5927\uff09\u65f6\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\u5f53\u54c8\u5e0c\u8868\u5185\u7684\u51b2\u7a81\u603b\u4f53\u8f83\u4e3a\u4e25\u91cd\u65f6\uff0c\u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u901a\u8fc7\u6269\u5bb9\u54c8\u5e0c\u8868\u6765\u7f13\u89e3\u51b2\u7a81\u3002\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\u5f00\u9500\u8f83\u5927\u3002

    \u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u4f7f\u7528\u300c\u8d1f\u8f7d\u56e0\u5b50 Load Factor\u300d\u6765\u8861\u91cf\u54c8\u5e0c\u51b2\u7a81\u7684\u4e25\u91cd\u7a0b\u5ea6\uff0c\u5b9a\u4e49\u4e3a\u54c8\u5e0c\u8868\u4e2d\u5143\u7d20\u6570\u91cf\u9664\u4ee5\u6876\u6570\u91cf\uff0c\u5e38\u4f5c\u4e3a\u54c8\u5e0c\u8868\u6269\u5bb9\u7684\u89e6\u53d1\u6761\u4ef6\u3002\u5728 Java \u4e2d\uff0c\u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7 \\(0.75\\) \u65f6\uff0c\u7cfb\u7edf\u4f1a\u5c06 HashMap \u5bb9\u91cf\u6269\u5c55\u4e3a\u539f\u5148\u7684 \\(2\\) \u500d\u3002

    "},{"location":"chapter_hashing/hash_collision/#622","title":"6.2.2. \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

    Fig. \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868

    \u94fe\u5f0f\u5730\u5740\u4e0b\uff0c\u54c8\u5e0c\u8868\u7684\u64cd\u4f5c\u65b9\u6cd5\u5305\u62ec\uff1a

    • \u67e5\u8be2\u5143\u7d20\uff1a\u8f93\u5165 key \uff0c\u7ecf\u8fc7\u54c8\u5e0c\u51fd\u6570\u5f97\u5230\u6570\u7ec4\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
    • \u6dfb\u52a0\u5143\u7d20\uff1a\u5148\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u8bbf\u95ee\u94fe\u8868\u5934\u8282\u70b9\uff0c\u7136\u540e\u5c06\u8282\u70b9\uff08\u5373\u952e\u503c\u5bf9\uff09\u6dfb\u52a0\u5230\u94fe\u8868\u4e2d\u3002
    • \u5220\u9664\u5143\u7d20\uff1a\u6839\u636e\u54c8\u5e0c\u51fd\u6570\u7684\u7ed3\u679c\u8bbf\u95ee\u94fe\u8868\u5934\u90e8\uff0c\u63a5\u7740\u904d\u5386\u94fe\u8868\u4ee5\u67e5\u627e\u76ee\u6807\u8282\u70b9\uff0c\u5e76\u5c06\u5176\u5220\u9664\u3002

    \u8be5\u65b9\u6cd5\u5b58\u5728\u4e00\u4e9b\u5c40\u9650\u6027\uff0c\u5305\u62ec\uff1a

    • \u5360\u7528\u7a7a\u95f4\u589e\u5927\uff0c\u7531\u4e8e\u94fe\u8868\u6216\u4e8c\u53c9\u6811\u5305\u542b\u8282\u70b9\u6307\u9488\uff0c\u76f8\u6bd4\u6570\u7ec4\u66f4\u52a0\u8017\u8d39\u5185\u5b58\u7a7a\u95f4\uff1b
    • \u67e5\u8be2\u6548\u7387\u964d\u4f4e\uff0c\u56e0\u4e3a\u9700\u8981\u7ebf\u6027\u904d\u5386\u94fe\u8868\u6765\u67e5\u627e\u5bf9\u5e94\u5143\u7d20\uff1b

    \u4ee5\u4e0b\u7ed9\u51fa\u4e86\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\u7684\u7b80\u5355\u5b9e\u73b0\uff0c\u9700\u8981\u6ce8\u610f\uff1a

    • \u4e3a\u4e86\u4f7f\u5f97\u4ee3\u7801\u5c3d\u91cf\u7b80\u77ed\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\u4ee3\u66ff\u94fe\u8868\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u54c8\u5e0c\u8868\uff08\u6570\u7ec4\uff09\u5305\u542b\u591a\u4e2a\u6876\uff0c\u6bcf\u4e2a\u6876\u90fd\u662f\u4e00\u4e2a\u5217\u8868\u3002
    • \u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u54c8\u5e0c\u8868\u6269\u5bb9\u65b9\u6cd5\u3002\u5177\u4f53\u6765\u770b\uff0c\u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7 \\(0.75\\) \u65f6\uff0c\u6211\u4eec\u5c06\u54c8\u5e0c\u8868\u6269\u5bb9\u81f3 \\(2\\) \u500d\u3002
    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart hash_map_chaining.java
    /* \u952e\u503c\u5bf9 */\nclass Pair {\npublic int key;\npublic String val;\npublic Pair(int key, String val) {\nthis.key = key;\nthis.val = val;\n}\n}\n/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\nint size; // \u952e\u503c\u5bf9\u6570\u91cf\nint capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\ndouble loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nint extendRatio; // \u6269\u5bb9\u500d\u6570\nList<List<Pair>> buckets; // \u6876\u6570\u7ec4\n/* \u6784\u9020\u65b9\u6cd5 */\npublic HashMapChaining() {\nsize = 0;\ncapacity = 4;\nloadThres = 2 / 3.0;\nextendRatio = 2;\nbuckets = new ArrayList<>(capacity);\nfor (int i = 0; i < capacity; i++) {\nbuckets.add(new ArrayList<>());\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(int key) {\nreturn key % capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor() {\nreturn (double) size / capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nString get(int key) {\nint index = hashFunc(key);\nList<Pair> bucket = buckets.get(index);\n// \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nfor (Pair pair : bucket) {\nif (pair.key == key) {\nreturn pair.val;\n}\n}\n// \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de null\nreturn null;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(int key, String val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (loadFactor() > loadThres) {\nextend();\n}\nint index = hashFunc(key);\nList<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\nfor (Pair pair : bucket) {\nif (pair.key == key) {\npair.val = val;\nreturn;\n}\n}\n// \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\nPair pair = new Pair(key, val);\nbucket.add(pair);\nsize++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid remove(int key) {\nint index = hashFunc(key);\nList<Pair> bucket = buckets.get(index);\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor (Pair pair : bucket) {\nif (pair.key == key)\nbucket.remove(pair);\n}\nsize--;\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nList<List<Pair>> bucketsTmp = buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\ncapacity *= extendRatio;\nbuckets = new ArrayList<>(capacity);\nfor (int i = 0; i < capacity; i++) {\nbuckets.add(new ArrayList<>());\n}\nsize = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (List<Pair> bucket : bucketsTmp) {\nfor (Pair pair : bucket) {\nput(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print() {\nfor (List<Pair> bucket : buckets) {\nList<String> res = new ArrayList<>();\nfor (Pair pair : bucket) {\nres.add(pair.key + \" -> \" + pair.val);\n}\nSystem.out.println(res);\n}\n}\n}\n
    hash_map_chaining.cpp
    /* \u952e\u503c\u5bf9 */\nstruct Pair {\npublic:\nint key;\nstring val;\nPair(int key, string val) {\nthis->key = key;\nthis->val = val;\n}\n};\n/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\nprivate:\nint size;                       // \u952e\u503c\u5bf9\u6570\u91cf\nint capacity;                   // \u54c8\u5e0c\u8868\u5bb9\u91cf\ndouble loadThres;               // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nint extendRatio;                // \u6269\u5bb9\u500d\u6570\nvector<vector<Pair *>> buckets; // \u6876\u6570\u7ec4\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nHashMapChaining() : size(0), capacity(4), loadThres(2.0 / 3), extendRatio(2) {\nbuckets.resize(capacity);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(int key) {\nreturn key % capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor() {\nreturn (double)size / (double)capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nstring get(int key) {\nint index = hashFunc(key);\n// \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nfor (Pair *pair : buckets[index]) {\nif (pair->key == key) {\nreturn pair->val;\n}\n}\n// \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de nullptr\nreturn nullptr;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(int key, string val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (loadFactor() > loadThres) {\nextend();\n}\nint index = hashFunc(key);\n// \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nfor (Pair *pair : buckets[index]) {\nif (pair->key == key) {\npair->val = val;\nreturn;\n}\n}\n// \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\nbuckets[index].push_back(new Pair(key, val));\nsize++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid remove(int key) {\nint index = hashFunc(key);\nauto &bucket = buckets[index];\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor (int i = 0; i < bucket.size(); i++) {\nif (bucket[i]->key == key) {\nPair *tmp = bucket[i];\nbucket.erase(bucket.begin() + i); // \u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\ndelete tmp;                       // \u91ca\u653e\u5185\u5b58\nsize--;\nreturn;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nvector<vector<Pair *>> bucketsTmp = buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\ncapacity *= extendRatio;\nbuckets.clear();\nbuckets.resize(capacity);\nsize = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (auto &bucket : bucketsTmp) {\nfor (Pair *pair : bucket) {\nput(pair->key, pair->val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print() {\nfor (auto &bucket : buckets) {\ncout << \"[\";\nfor (Pair *pair : bucket) {\ncout << pair->key << \" -> \" << pair->val << \", \";\n}\ncout << \"]\\n\";\n}\n}\n};\n
    hash_map_chaining.py
    class Pair:\n\"\"\"\u952e\u503c\u5bf9\"\"\"\ndef __init__(self, key: int, val: str):\nself.key = key\nself.val = val\nclass HashMapChaining:\n\"\"\"\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\"\"\"\ndef __init__(self):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.size = 0  # \u952e\u503c\u5bf9\u6570\u91cf\nself.capacity = 4  # \u54c8\u5e0c\u8868\u5bb9\u91cf\nself.load_thres = 2 / 3  # \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nself.extend_ratio = 2  # \u6269\u5bb9\u500d\u6570\nself.buckets = [[] for _ in range(self.capacity)]  # \u6876\u6570\u7ec4\ndef hash_func(self, key: int) -> int:\n\"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\nreturn key % self.capacity\ndef load_factor(self) -> float:\n\"\"\"\u8d1f\u8f7d\u56e0\u5b50\"\"\"\nreturn self.size / self.capacity\ndef get(self, key: int) -> str:\n\"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\nindex = self.hash_func(key)\nbucket = self.buckets[index]\n# \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nfor pair in bucket:\nif pair.key == key:\nreturn pair.val\n# \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de None\nreturn None\ndef 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\nif self.load_factor() > self.load_thres:\nself.extend()\nindex = self.hash_func(key)\nbucket = self.buckets[index]\n# \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nfor pair in bucket:\nif pair.key == key:\npair.val = val\nreturn\n# \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\npair = Pair(key, val)\nbucket.append(pair)\nself.size += 1\ndef remove(self, key: int):\n\"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\nindex = self.hash_func(key)\nbucket = self.buckets[index]\n# \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor pair in bucket:\nif pair.key == key:\nbucket.remove(pair)\nself.size -= 1\nreturn\ndef extend(self):\n\"\"\"\u6269\u5bb9\u54c8\u5e0c\u8868\"\"\"\n# \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nbuckets = self.buckets\n# \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nself.capacity *= self.extend_ratio\nself.buckets = [[] for _ in range(self.capacity)]\nself.size = 0\n# \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor bucket in buckets:\nfor pair in bucket:\nself.put(pair.key, pair.val)\ndef print(self):\n\"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\nfor bucket in self.buckets:\nres = []\nfor pair in bucket:\nres.append(str(pair.key) + \" -> \" + pair.val)\nprint(res)\n
    hash_map_chaining.go
    [class]{pair}-[func]{}\n[class]{hashMapChaining}-[func]{}\n
    hash_map_chaining.js
    [class]{Pair}-[func]{}\n[class]{HashMapChaining}-[func]{}\n
    hash_map_chaining.ts
    [class]{Pair}-[func]{}\n[class]{HashMapChaining}-[func]{}\n
    hash_map_chaining.c
    [class]{pair}-[func]{}\n[class]{hashMapChaining}-[func]{}\n
    hash_map_chaining.cs
    [class]{Pair}-[func]{}\n[class]{HashMapChaining}-[func]{}\n
    hash_map_chaining.swift
    [class]{Pair}-[func]{}\n[class]{HashMapChaining}-[func]{}\n
    hash_map_chaining.zig
    [class]{Pair}-[func]{}\n[class]{HashMapChaining}-[func]{}\n
    hash_map_chaining.dart
    [class]{Pair}-[func]{}\n[class]{HashMapChaining}-[func]{}\n

    Tip

    \u4e3a\u4e86\u63d0\u9ad8\u6548\u7387\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u300cAVL \u6811\u300d\u6216\u300c\u7ea2\u9ed1\u6811\u300d\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/#623","title":"6.2.3. \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\u3002

    "},{"location":"chapter_hashing/hash_collision/#_1","title":"\u7ebf\u6027\u63a2\u6d4b","text":"

    \u7ebf\u6027\u63a2\u6d4b\u91c7\u7528\u56fa\u5b9a\u6b65\u957f\u7684\u7ebf\u6027\u67e5\u627e\u6765\u8fdb\u884c\u63a2\u6d4b\uff0c\u5bf9\u5e94\u7684\u54c8\u5e0c\u8868\u64cd\u4f5c\u65b9\u6cd5\u4e3a\uff1a

    • \u63d2\u5165\u5143\u7d20\uff1a\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u8ba1\u7b97\u6570\u7ec4\u7d22\u5f15\uff0c\u82e5\u53d1\u73b0\u6876\u5185\u5df2\u6709\u5143\u7d20\uff0c\u5219\u4ece\u51b2\u7a81\u4f4d\u7f6e\u5411\u540e\u7ebf\u6027\u904d\u5386\uff08\u6b65\u957f\u901a\u5e38\u4e3a \\(1\\) \uff09\uff0c\u76f4\u81f3\u627e\u5230\u7a7a\u4f4d\uff0c\u5c06\u5143\u7d20\u63d2\u5165\u5176\u4e2d\u3002
    • \u67e5\u627e\u5143\u7d20\uff1a\u82e5\u53d1\u73b0\u54c8\u5e0c\u51b2\u7a81\uff0c\u5219\u4f7f\u7528\u76f8\u540c\u6b65\u957f\u5411\u540e\u7ebf\u6027\u904d\u5386\uff0c\u76f4\u5230\u627e\u5230\u5bf9\u5e94\u5143\u7d20\uff0c\u8fd4\u56de value \u5373\u53ef\uff1b\u6216\u8005\u82e5\u9047\u5230\u7a7a\u4f4d\uff0c\u8bf4\u660e\u76ee\u6807\u952e\u503c\u5bf9\u4e0d\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u8fd4\u56de \\(\\text{None}\\) \u3002

    Fig. \u7ebf\u6027\u63a2\u6d4b

    \u7136\u800c\uff0c\u7ebf\u6027\u63a2\u6d4b\u5b58\u5728\u4ee5\u4e0b\u7f3a\u9677\uff1a

    • \u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5143\u7d20\u3002\u5220\u9664\u5143\u7d20\u4f1a\u5728\u6570\u7ec4\u5185\u4ea7\u751f\u4e00\u4e2a\u7a7a\u4f4d\uff0c\u67e5\u627e\u5176\u4ed6\u5143\u7d20\u65f6\uff0c\u8be5\u7a7a\u4f4d\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u8bef\u5224\u5143\u7d20\u4e0d\u5b58\u5728\u3002\u56e0\u6b64\uff0c\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u6807\u5fd7\u4f4d\u6765\u6807\u8bb0\u5df2\u5220\u9664\u5143\u7d20\u3002
    • \u5bb9\u6613\u4ea7\u751f\u805a\u96c6\u3002\u6570\u7ec4\u5185\u8fde\u7eed\u88ab\u5360\u7528\u4f4d\u7f6e\u8d8a\u957f\uff0c\u8fd9\u4e9b\u8fde\u7eed\u4f4d\u7f6e\u53d1\u751f\u54c8\u5e0c\u51b2\u7a81\u7684\u53ef\u80fd\u6027\u8d8a\u5927\uff0c\u8fdb\u4e00\u6b65\u4fc3\u4f7f\u8fd9\u4e00\u4f4d\u7f6e\u7684\u201c\u805a\u5806\u751f\u957f\u201d\uff0c\u6700\u7ec8\u5bfc\u81f4\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u6548\u7387\u964d\u4f4e\u3002

    \u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u4e3a\u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\uff09\u54c8\u5e0c\u8868\u7684\u7b80\u5355\u5b9e\u73b0\uff0c\u91cd\u70b9\u5305\u62ec\uff1a

    • \u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u56fa\u5b9a\u7684\u952e\u503c\u5bf9\u5b9e\u4f8b removed \u6765\u6807\u8bb0\u5df2\u5220\u9664\u5143\u7d20\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u4e00\u4e2a\u6876\u4e3a \\(\\text{None}\\) \u6216 removed \u65f6\uff0c\u8fd9\u4e2a\u6876\u90fd\u662f\u7a7a\u7684\uff0c\u53ef\u7528\u4e8e\u653e\u7f6e\u952e\u503c\u5bf9\u3002
    • \u88ab\u6807\u8bb0\u4e3a\u5df2\u5220\u9664\u7684\u7a7a\u95f4\u662f\u53ef\u4ee5\u518d\u6b21\u88ab\u4f7f\u7528\u7684\u3002\u5f53\u63d2\u5165\u5143\u7d20\u65f6\uff0c\u82e5\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u627e\u5230\u4e86\u88ab\u6807\u8bb0\u4e3a\u5df2\u5220\u9664\u7684\u7d22\u5f15\uff0c\u5219\u53ef\u5c06\u8be5\u5143\u7d20\u653e\u7f6e\u5230\u8be5\u7d22\u5f15\u3002
    • \u5728\u7ebf\u6027\u63a2\u6d4b\u65f6\uff0c\u6211\u4eec\u4ece\u5f53\u524d\u7d22\u5f15 index \u5411\u540e\u904d\u5386\uff1b\u800c\u5f53\u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u9700\u8981\u56de\u5230\u5934\u90e8\u7ee7\u7eed\u904d\u5386\u3002
    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart hash_map_open_addressing.java
    /* \u952e\u503c\u5bf9 */\nclass Pair {\npublic int key;\npublic String val;\npublic Pair(int key, String val) {\nthis.key = key;\nthis.val = val;\n}\n}\n/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\nprivate int size; // \u952e\u503c\u5bf9\u6570\u91cf\nprivate int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\nprivate double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nprivate int extendRatio; // \u6269\u5bb9\u500d\u6570\nprivate Pair[] buckets; // \u6876\u6570\u7ec4\nprivate Pair removed; // \u5220\u9664\u6807\u8bb0\n/* \u6784\u9020\u65b9\u6cd5 */\npublic HashMapOpenAddressing() {\nsize = 0;\ncapacity = 4;\nloadThres = 2.0 / 3.0;\nextendRatio = 2;\nbuckets = new Pair[capacity];\nremoved = new Pair(-1, \"-1\");\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\npublic int hashFunc(int key) {\nreturn key % capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\npublic double loadFactor() {\nreturn (double) size / capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npublic String get(int key) {\nint index = hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (int i = 0; i < capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nint j = (index + i) % capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u8fd4\u56de null\nif (buckets[j] == null)\nreturn null;\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif (buckets[j].key == key && buckets[j] != removed)\nreturn buckets[j].val;\n}\nreturn null;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\npublic void put(int key, String val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (loadFactor() > loadThres) {\nextend();\n}\nint index = hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (int i = 0; i < capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nint j = (index + i) % 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\nif (buckets[j] == null || buckets[j] == removed) {\nbuckets[j] = new Pair(key, val);\nsize += 1;\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val\nif (buckets[j].key == key) {\nbuckets[j].val = val;\nreturn;\n}\n}\n}\n/* \u5220\u9664\u64cd\u4f5c */\npublic void remove(int key) {\nint index = hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (int i = 0; i < capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nint j = (index + i) % capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (buckets[j] == null) {\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u6807\u8bb0\u5220\u9664\u5e76\u8fd4\u56de\nif (buckets[j].key == key) {\nbuckets[j] = removed;\nsize -= 1;\nreturn;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\npublic void extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nPair[] bucketsTmp = buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\ncapacity *= extendRatio;\nbuckets = new Pair[capacity];\nsize = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (Pair pair : bucketsTmp) {\nif (pair != null && pair != removed) {\nput(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\npublic void print() {\nfor (Pair pair : buckets) {\nif (pair != null) {\nSystem.out.println(pair.key + \" -> \" + pair.val);\n} else {\nSystem.out.println(\"null\");\n}\n}\n}\n}\n
    hash_map_open_addressing.cpp
    /* \u952e\u503c\u5bf9 */\nstruct Pair {\nint key;\nstring val;\nPair(int k, string v) : key(k), val(v) {\n}\n};\n/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\nprivate:\nint size;               // \u952e\u503c\u5bf9\u6570\u91cf\nint capacity;           // \u54c8\u5e0c\u8868\u5bb9\u91cf\ndouble loadThres;       // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nint extendRatio;        // \u6269\u5bb9\u500d\u6570\nvector<Pair *> buckets; // \u6876\u6570\u7ec4\nPair *removed;          // \u5220\u9664\u6807\u8bb0\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nHashMapOpenAddressing() {\n// \u6784\u9020\u65b9\u6cd5\nsize = 0;\ncapacity = 4;\nloadThres = 2.0 / 3.0;\nextendRatio = 2;\nbuckets = vector<Pair *>(capacity, nullptr);\nremoved = new Pair(-1, \"-1\");\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(int key) {\nreturn key % capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor() {\nreturn static_cast<double>(size) / capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nstring get(int key) {\nint index = hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (int i = 0; i < capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nint j = (index + i) % capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u8fd4\u56de nullptr\nif (buckets[j] == nullptr)\nreturn nullptr;\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif (buckets[j]->key == key && buckets[j] != removed)\nreturn buckets[j]->val;\n}\nreturn nullptr;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(int key, string val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (loadFactor() > loadThres)\nextend();\nint index = hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (int i = 0; i < capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nint j = (index + i) % 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\nif (buckets[j] == nullptr || buckets[j] == removed) {\nbuckets[j] = new Pair(key, val);\nsize += 1;\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val\nif (buckets[j]->key == key) {\nbuckets[j]->val = val;\nreturn;\n}\n}\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid remove(int key) {\nint index = hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (int i = 0; i < capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nint j = (index + i) % capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (buckets[j] == nullptr)\nreturn;\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u6807\u8bb0\u5220\u9664\u5e76\u8fd4\u56de\nif (buckets[j]->key == key) {\ndelete buckets[j]; // \u91ca\u653e\u5185\u5b58\nbuckets[j] = removed;\nsize -= 1;\nreturn;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nvector<Pair *> bucketsTmp = buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\ncapacity *= extendRatio;\nbuckets = vector<Pair *>(capacity, nullptr);\nsize = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (Pair *pair : bucketsTmp) {\nif (pair != nullptr && pair != removed) {\nput(pair->key, pair->val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print() {\nfor (auto &pair : buckets) {\nif (pair != nullptr) {\ncout << pair->key << \" -> \" << pair->val << endl;\n} else {\ncout << \"nullptr\" << endl;\n}\n}\n}\n};\n
    hash_map_open_addressing.py
    class Pair:\n\"\"\"\u952e\u503c\u5bf9\"\"\"\ndef __init__(self, key: int, val: str):\nself.key = key\nself.val = val\nclass HashMapOpenAddressing:\n\"\"\"\u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868\"\"\"\ndef __init__(self):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.size = 0  # \u952e\u503c\u5bf9\u6570\u91cf\nself.capacity = 4  # \u54c8\u5e0c\u8868\u5bb9\u91cf\nself.load_thres = 2 / 3  # \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nself.extend_ratio = 2  # \u6269\u5bb9\u500d\u6570\nself.buckets: list[Pair | None] = [None] * self.capacity  # \u6876\u6570\u7ec4\nself.removed = Pair(-1, \"-1\")  # \u5220\u9664\u6807\u8bb0\ndef hash_func(self, key: int) -> int:\n\"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\nreturn key % self.capacity\ndef load_factor(self) -> float:\n\"\"\"\u8d1f\u8f7d\u56e0\u5b50\"\"\"\nreturn self.size / self.capacity\ndef get(self, key: int) -> str:\n\"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\nindex = self.hash_func(key)\n# \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor i in range(self.capacity):\n# \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nj = (index + i) % self.capacity\n# \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u8fd4\u56de None\nif self.buckets[j] is None:\nreturn None\n# \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif self.buckets[j].key == key and self.buckets[j] != self.removed:\nreturn self.buckets[j].val\ndef 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\nif self.load_factor() > self.load_thres:\nself.extend()\nindex = self.hash_func(key)\n# \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor i in range(self.capacity):\n# \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nj = (index + i) % self.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\nif self.buckets[j] in [None, self.removed]:\nself.buckets[j] = Pair(key, val)\nself.size += 1\nreturn\n# \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val\nif self.buckets[j].key == key:\nself.buckets[j].val = val\nreturn\ndef remove(self, key: int):\n\"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\nindex = self.hash_func(key)\n# \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor i in range(self.capacity):\n# \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nj = (index + i) % self.capacity\n# \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif self.buckets[j] is None:\nreturn\n# \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u6807\u8bb0\u5220\u9664\u5e76\u8fd4\u56de\nif self.buckets[j].key == key:\nself.buckets[j] = self.removed\nself.size -= 1\nreturn\ndef extend(self):\n\"\"\"\u6269\u5bb9\u54c8\u5e0c\u8868\"\"\"\n# \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nbuckets_tmp = self.buckets\n# \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nself.capacity *= self.extend_ratio\nself.buckets = [None] * self.capacity\nself.size = 0\n# \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor pair in buckets_tmp:\nif pair not in [None, self.removed]:\nself.put(pair.key, pair.val)\ndef print(self) -> None:\n\"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\nfor pair in self.buckets:\nif pair is not None:\nprint(pair.key, \"->\", pair.val)\nelse:\nprint(\"None\")\n
    hash_map_open_addressing.go
    [class]{pair}-[func]{}\n[class]{hashMapOpenAddressing}-[func]{}\n
    hash_map_open_addressing.js
    [class]{Pair}-[func]{}\n[class]{HashMapOpenAddressing}-[func]{}\n
    hash_map_open_addressing.ts
    [class]{Pair}-[func]{}\n[class]{HashMapOpenAddressing}-[func]{}\n
    hash_map_open_addressing.c
    [class]{pair}-[func]{}\n[class]{hashMapOpenAddressing}-[func]{}\n
    hash_map_open_addressing.cs
    [class]{Pair}-[func]{}\n[class]{HashMapOpenAddressing}-[func]{}\n
    hash_map_open_addressing.swift
    [class]{Pair}-[func]{}\n[class]{HashMapOpenAddressing}-[func]{}\n
    hash_map_open_addressing.zig
    [class]{Pair}-[func]{}\n[class]{HashMapOpenAddressing}-[func]{}\n
    hash_map_open_addressing.dart
    [class]{Pair}-[func]{}\n[class]{HashMapOpenAddressing}-[func]{}\n
    "},{"location":"chapter_hashing/hash_collision/#_2","title":"\u591a\u6b21\u54c8\u5e0c","text":"

    \u987e\u540d\u601d\u4e49\uff0c\u591a\u6b21\u54c8\u5e0c\u65b9\u6cd5\u662f\u4f7f\u7528\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570 \\(f_1(x)\\) , \\(f_2(x)\\) , \\(f_3(x)\\) , \\(\\cdots\\) \u8fdb\u884c\u63a2\u6d4b\u3002

    • \u63d2\u5165\u5143\u7d20\uff1a\u82e5\u54c8\u5e0c\u51fd\u6570 \\(f_1(x)\\) \u51fa\u73b0\u51b2\u7a81\uff0c\u5219\u5c1d\u8bd5 \\(f_2(x)\\) \uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u5230\u627e\u5230\u7a7a\u4f4d\u540e\u63d2\u5165\u5143\u7d20\u3002
    • \u67e5\u627e\u5143\u7d20\uff1a\u5728\u76f8\u540c\u7684\u54c8\u5e0c\u51fd\u6570\u987a\u5e8f\u4e0b\u8fdb\u884c\u67e5\u627e\uff0c\u76f4\u5230\u627e\u5230\u76ee\u6807\u5143\u7d20\u65f6\u8fd4\u56de\uff1b\u6216\u9047\u5230\u7a7a\u4f4d\u6216\u5df2\u5c1d\u8bd5\u6240\u6709\u54c8\u5e0c\u51fd\u6570\uff0c\u8bf4\u660e\u54c8\u5e0c\u8868\u4e2d\u4e0d\u5b58\u5728\u8be5\u5143\u7d20\uff0c\u5219\u8fd4\u56de \\(\\text{None}\\) \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\u589e\u52a0\u989d\u5916\u7684\u8ba1\u7b97\u91cf\u3002

    \u7f16\u7a0b\u8bed\u8a00\u7684\u9009\u62e9

    Java \u91c7\u7528\u94fe\u5f0f\u5730\u5740\u3002\u81ea JDK 1.8 \u4ee5\u6765\uff0c\u5f53 HashMap \u5185\u6570\u7ec4\u957f\u5ea6\u8fbe\u5230 64 \u4e14\u94fe\u8868\u957f\u5ea6\u8fbe\u5230 8 \u65f6\uff0c\u94fe\u8868\u4f1a\u88ab\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811\u4ee5\u63d0\u5347\u67e5\u627e\u6027\u80fd\u3002

    Python \u91c7\u7528\u5f00\u653e\u5bfb\u5740\u3002\u5b57\u5178 dict \u4f7f\u7528\u4f2a\u968f\u673a\u6570\u8fdb\u884c\u63a2\u6d4b\u3002

    Golang \u91c7\u7528\u94fe\u5f0f\u5730\u5740\u3002Go \u89c4\u5b9a\u6bcf\u4e2a\u6876\u6700\u591a\u5b58\u50a8 8 \u4e2a\u952e\u503c\u5bf9\uff0c\u8d85\u51fa\u5bb9\u91cf\u5219\u8fde\u63a5\u4e00\u4e2a\u6ea2\u51fa\u6876\uff1b\u5f53\u6ea2\u51fa\u6876\u8fc7\u591a\u65f6\uff0c\u4f1a\u6267\u884c\u4e00\u6b21\u7279\u6b8a\u7684\u7b49\u91cf\u6269\u5bb9\u64cd\u4f5c\uff0c\u4ee5\u786e\u4fdd\u6027\u80fd\u3002

    "},{"location":"chapter_hashing/hash_map/","title":"6.1. \u00a0 \u54c8\u5e0c\u8868","text":"

    \u300c\u54c8\u5e0c\u8868 Hash Table\u300d\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 key \uff0c\u5219\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u83b7\u53d6\u5bf9\u5e94\u7684 value \u3002

    \u4ee5\u4e00\u4e2a\u5305\u542b \\(n\\) \u4e2a\u5b66\u751f\u7684\u6570\u636e\u5e93\u4e3a\u4f8b\uff0c\u6bcf\u4e2a\u5b66\u751f\u90fd\u6709\u201c\u59d3\u540d name\u201d\u548c\u201c\u5b66\u53f7 id\u201d\u4e24\u9879\u6570\u636e\u3002\u5047\u5982\u6211\u4eec\u5e0c\u671b\u5b9e\u73b0\u67e5\u8be2\u529f\u80fd\uff0c\u4f8b\u5982\u201c\u8f93\u5165\u4e00\u4e2a\u5b66\u53f7\uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u59d3\u540d\u201d\uff0c\u5219\u53ef\u4ee5\u91c7\u7528\u54c8\u5e0c\u8868\u6765\u5b9e\u73b0\u3002

    Fig. \u54c8\u5e0c\u8868\u7684\u62bd\u8c61\u8868\u793a

    \u9664\u54c8\u5e0c\u8868\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u6216\u94fe\u8868\u5b9e\u73b0\u5143\u7d20\u67e5\u8be2\uff0c\u5176\u4e2d\uff1a

    • \u67e5\u8be2\u5143\u7d20\u9700\u8981\u904d\u5386\u6240\u6709\u5143\u7d20\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff1b
    • \u6dfb\u52a0\u5143\u7d20\u4ec5\u9700\u6dfb\u52a0\u81f3\u5c3e\u90e8\u5373\u53ef\uff0c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\uff1b
    • \u5220\u9664\u5143\u7d20\u9700\u8981\u5148\u67e5\u8be2\u518d\u5220\u9664\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff1b
    \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\u56e0\u6b64\uff0c\u54c8\u5e0c\u8868\u5e38\u7528\u4e8e\u5bf9\u67e5\u627e\u6548\u7387\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\u3002

    "},{"location":"chapter_hashing/hash_map/#611","title":"6.1.1. \u00a0 \u54c8\u5e0c\u8868\u5e38\u7528\u64cd\u4f5c","text":"

    \u54c8\u5e0c\u8868\u7684\u57fa\u672c\u64cd\u4f5c\u5305\u62ec \u521d\u59cb\u5316\u3001\u67e5\u8be2\u64cd\u4f5c\u3001\u6dfb\u52a0\u4e0e\u5220\u9664\u952e\u503c\u5bf9\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart hash_map.java
    /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nMap<Integer, String> map = new HashMap<>();\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.put(12836, \"\u5c0f\u54c8\");   map.put(15937, \"\u5c0f\u5570\");   map.put(16750, \"\u5c0f\u7b97\");   map.put(13276, \"\u5c0f\u6cd5\");\nmap.put(10583, \"\u5c0f\u9e2d\");\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/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.remove(10583);\n
    hash_map.cpp
    /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nunordered_map<int, string> map;\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/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nstring name = map[15937];\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.erase(10583);\n
    hash_map.py
    # \u521d\u59cb\u5316\u54c8\u5e0c\u8868\nmapp: Dict = {}\n# \u6dfb\u52a0\u64cd\u4f5c\n# \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmapp[12836] = \"\u5c0f\u54c8\"\nmapp[15937] = \"\u5c0f\u5570\"\nmapp[16750] = \"\u5c0f\u7b97\"\nmapp[13276] = \"\u5c0f\u6cd5\"\nmapp[10583] = \"\u5c0f\u9e2d\"\n# \u67e5\u8be2\u64cd\u4f5c\n# \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nname: str = mapp[15937]\n# \u5220\u9664\u64cd\u4f5c\n# \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmapp.pop(10583)\n
    hash_map.go
    /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nmapp := make(map[int]string)\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmapp[12836] = \"\u5c0f\u54c8\"\nmapp[15937] = \"\u5c0f\u5570\"\nmapp[16750] = \"\u5c0f\u7b97\"\nmapp[13276] = \"\u5c0f\u6cd5\"\nmapp[10583] = \"\u5c0f\u9e2d\"\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nname := mapp[15937]\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\ndelete(mapp, 10583)\n
    hash_map.js
    /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nconst map = new ArrayHashMap();\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/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet name = map.get(15937);\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/* \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/* \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.c
    // C \u672a\u63d0\u4f9b\u5185\u7f6e\u54c8\u5e0c\u8868\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)\nmap.Add(12836, \"\u5c0f\u54c8\");\nmap.Add(15937, \"\u5c0f\u5570\");\nmap.Add(16750, \"\u5c0f\u7b97\");\nmap.Add(13276, \"\u5c0f\u6cd5\");\nmap.Add(10583, \"\u5c0f\u9e2d\");\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nString name = map[15937];\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.Remove(10583);\n
    hash_map.swift
    /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nvar map: [Int: String] = [:]\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/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet name = map[15937]!\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.zig
    \n
    hash_map.dart
    /* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nMap<int, String> map = {};\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/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nString name = map[15937];\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.remove(10583);\n

    \u904d\u5386\u54c8\u5e0c\u8868\u6709\u4e09\u79cd\u65b9\u5f0f\uff0c\u5373 \u904d\u5386\u952e\u503c\u5bf9\u3001\u904d\u5386\u952e\u3001\u904d\u5386\u503c\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart hash_map.java
    /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor (Map.Entry <Integer, String> kv: map.entrySet()) {\nSystem.out.println(kv.getKey() + \" -> \" + kv.getValue());\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nfor (int key: map.keySet()) {\nSystem.out.println(key);\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor (String val: map.values()) {\nSystem.out.println(val);\n}\n
    hash_map.cpp
    /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor (auto kv: map) {\ncout << kv.first << \" -> \" << kv.second << endl;\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nfor (auto key: map) {\ncout << key.first << endl;\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor (auto val: map) {\ncout << val.second << endl;\n}\n
    hash_map.py
    # \u904d\u5386\u54c8\u5e0c\u8868\n# \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor key, value in mapp.items():\nprint(key, \"->\", value)\n# \u5355\u72ec\u904d\u5386\u952e key\nfor key in mapp.keys():\nprint(key)\n# \u5355\u72ec\u904d\u5386\u503c value\nfor value in mapp.values():\nprint(value)\n
    hash_map_test.go
    /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor key, value := range mapp {\nfmt.Println(key, \"->\", value)\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nfor key := range mapp {\nfmt.Println(key)\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor _, value := range mapp {\nfmt.Println(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()) {\nconsole.info(k + ' -> ' + v);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u952e Key');\nfor (const k of map.keys()) {\nconsole.info(k);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u503c Value');\nfor (const v of map.values()) {\nconsole.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()) {\nconsole.info(k + ' -> ' + v);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u952e Key');\nfor (const k of map.keys()) {\nconsole.info(k);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u503c Value');\nfor (const v of map.values()) {\nconsole.info(v);\n}\n
    hash_map.c
    // C \u672a\u63d0\u4f9b\u5185\u7f6e\u54c8\u5e0c\u8868\n
    hash_map.cs
    /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nforeach (var kv in map)\n{\nConsole.WriteLine(kv.Key + \" -> \" + kv.Value);\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nforeach (int key in map.Keys)\n{\nConsole.WriteLine(key);\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nforeach (String val in map.Values)\n{\nConsole.WriteLine(val);\n}\n
    hash_map.swift
    /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nfor (key, value) in map {\nprint(\"\\(key) -> \\(value)\")\n}\n// \u5355\u72ec\u904d\u5386\u952e Key\nfor key in map.keys {\nprint(key)\n}\n// \u5355\u72ec\u904d\u5386\u503c Value\nfor value in map.values {\nprint(value)\n}\n
    hash_map.zig
    \n
    hash_map.dart
    /* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nmap.forEach((key, value) {\nprint('$key -> $value');\n});\n// \u5355\u72ec\u904d\u5386\u952e Key\nmap.keys.forEach((key) {\nprint(key);\n});\n// \u5355\u72ec\u904d\u5386\u503c Value\nmap.values.forEach((value) {\nprint(value);\n});\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\u901a\u5e38\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\u5b9a\u4f4d\u8f93\u5165\u7684 key \u5bf9\u5e94\u7684\u6876\uff0c\u4ece\u800c\u5f97\u5230 value \u3002

    \u90a3\u4e48\uff0c\u5982\u4f55\u57fa\u4e8e key \u6765\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\u5b58\u50a8\u5728\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e\u3002

    \u54c8\u5e0c\u51fd\u6570\u7684\u8ba1\u7b97\u8fc7\u7a0b\u5206\u4e3a\u4e24\u6b65\uff1a\u8f93\u5165\u4e00\u4e2a key \uff0c\u9996\u5148\u901a\u8fc7\u51fd\u6570 hash(key) \u8ba1\u7b97\u5f97\u5230\u54c8\u5e0c\u503c\uff0c\u63a5\u4e0b\u6765\u5c06\u54c8\u5e0c\u503c\u5bf9\u6876\u6570\u91cf\uff08\u6570\u7ec4\u957f\u5ea6\uff09\u53d6\u6a21\uff0c\u4ece\u800c\u83b7\u53d6\u8be5 key \u5bf9\u5e94\u7684\u6570\u7ec4\u7d22\u5f15 index \u3002\u8ba1\u7b97\u516c\u5f0f\u5982\u4e0b

    \\[ index = \\text{hash}(key) \\bmod {c} \\]

    \u5176\u4e2d\uff0c \\(\\bmod\\) \u8868\u793a\u53d6\u4f59\u8fd0\u7b97\uff0c \\(c\\) \u4e3a\u6876\u6570\u91cf\uff08\u6570\u7ec4\u957f\u5ea6\uff09\u3002\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 \\(c = 100\\) , \\(\\text{hash}(key) = key\\) \uff0c\u6613\u5f97\u54c8\u5e0c\u51fd\u6570\u4e3a \\(key \\bmod 100\\) \u3002\u4e0b\u56fe\u4ee5 key \u5b66\u53f7\u548c value \u59d3\u540d\u4e3a\u4f8b\uff0c\u5c55\u793a\u4e86\u54c8\u5e0c\u51fd\u6570\u7684\u5de5\u4f5c\u539f\u7406\u3002

    Fig. \u54c8\u5e0c\u51fd\u6570\u5de5\u4f5c\u539f\u7406

    \u4ee5\u4e0b\u4ee3\u7801\u7ed9\u51fa\u4e86\u4e00\u4e2a\u7b80\u5355\u54c8\u5e0c\u8868\u5b9e\u73b0\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart array_hash_map.java
    /* \u952e\u503c\u5bf9 */\nclass Pair {\npublic int key;\npublic String val;\npublic Pair(int key, String val) {\nthis.key = key;\nthis.val = val;\n}\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\nprivate List<Pair> buckets;\npublic ArrayHashMap() {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nbuckets = new ArrayList<>();\nfor (int i = 0; i < 100; i++) {\nbuckets.add(null);\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nprivate int hashFunc(int key) {\nint index = key % 100;\nreturn index;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npublic String get(int key) {\nint index = hashFunc(key);\nPair pair = buckets.get(index);\nif (pair == null)\nreturn null;\nreturn pair.val;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\npublic void put(int key, String val) {\nPair pair = new Pair(key, val);\nint index = hashFunc(key);\nbuckets.set(index, pair);\n}\n/* \u5220\u9664\u64cd\u4f5c */\npublic void remove(int key) {\nint index = hashFunc(key);\n// \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\nbuckets.set(index, null);\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\npublic List<Pair> pairSet() {\nList<Pair> pairSet = new ArrayList<>();\nfor (Pair pair : buckets) {\nif (pair != null)\npairSet.add(pair);\n}\nreturn pairSet;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\npublic List<Integer> keySet() {\nList<Integer> keySet = new ArrayList<>();\nfor (Pair pair : buckets) {\nif (pair != null)\nkeySet.add(pair.key);\n}\nreturn keySet;\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\npublic List<String> valueSet() {\nList<String> valueSet = new ArrayList<>();\nfor (Pair pair : buckets) {\nif (pair != null)\nvalueSet.add(pair.val);\n}\nreturn valueSet;\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\npublic void print() {\nfor (Pair kv : pairSet()) {\nSystem.out.println(kv.key + \" -> \" + kv.val);\n}\n}\n}\n
    array_hash_map.cpp
    /* \u952e\u503c\u5bf9 */\nstruct Pair {\npublic:\nint key;\nstring val;\nPair(int key, string val) {\nthis->key = key;\nthis->val = val;\n}\n};\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\nprivate:\nvector<Pair *> buckets;\npublic:\nArrayHashMap() {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nbuckets = vector<Pair *>(100);\n}\n~ArrayHashMap() {\n// \u91ca\u653e\u5185\u5b58\nfor (const auto &bucket : buckets) {\ndelete bucket;\n}\nbuckets.clear();\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(int key) {\nint index = key % 100;\nreturn index;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nstring get(int key) {\nint index = hashFunc(key);\nPair *pair = buckets[index];\nif (pair == nullptr)\nreturn nullptr;\nreturn pair->val;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(int key, string val) {\nPair *pair = new Pair(key, val);\nint index = hashFunc(key);\nbuckets[index] = pair;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid remove(int key) {\nint index = hashFunc(key);\n// \u91ca\u653e\u5185\u5b58\u5e76\u7f6e\u4e3a nullptr\ndelete buckets[index];\nbuckets[index] = nullptr;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\nvector<Pair *> pairSet() {\nvector<Pair *> pairSet;\nfor (Pair *pair : buckets) {\nif (pair != nullptr) {\npairSet.push_back(pair);\n}\n}\nreturn pairSet;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\nvector<int> keySet() {\nvector<int> keySet;\nfor (Pair *pair : buckets) {\nif (pair != nullptr) {\nkeySet.push_back(pair->key);\n}\n}\nreturn keySet;\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\nvector<string> valueSet() {\nvector<string> valueSet;\nfor (Pair *pair : buckets) {\nif (pair != nullptr) {\nvalueSet.push_back(pair->val);\n}\n}\nreturn valueSet;\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print() {\nfor (Pair *kv : pairSet()) {\ncout << kv->key << \" -> \" << kv->val << endl;\n}\n}\n};\n
    array_hash_map.py
    class Pair:\n\"\"\"\u952e\u503c\u5bf9\"\"\"\ndef __init__(self, key: int, val: str):\nself.key = key\nself.val = val\nclass ArrayHashMap:\n\"\"\"\u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868\"\"\"\ndef __init__(self):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n# \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nself.buckets: list[Pair | None] = [None] * 100\ndef hash_func(self, key: int) -> int:\n\"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\nindex = key % 100\nreturn index\ndef get(self, key: int) -> str:\n\"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\nindex: int = self.hash_func(key)\npair: Pair = self.buckets[index]\nif pair is None:\nreturn None\nreturn pair.val\ndef put(self, key: int, val: str):\n\"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"\npair = Pair(key, val)\nindex: int = self.hash_func(key)\nself.buckets[index] = pair\ndef remove(self, key: int):\n\"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\nindex: int = self.hash_func(key)\n# \u7f6e\u4e3a None \uff0c\u4ee3\u8868\u5220\u9664\nself.buckets[index] = None\ndef entry_set(self) -> list[Pair]:\n\"\"\"\u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9\"\"\"\nresult: list[Pair] = []\nfor pair in self.buckets:\nif pair is not None:\nresult.append(pair)\nreturn result\ndef key_set(self) -> list[int]:\n\"\"\"\u83b7\u53d6\u6240\u6709\u952e\"\"\"\nresult = []\nfor pair in self.buckets:\nif pair is not None:\nresult.append(pair.key)\nreturn result\ndef value_set(self) -> list[str]:\n\"\"\"\u83b7\u53d6\u6240\u6709\u503c\"\"\"\nresult = []\nfor pair in self.buckets:\nif pair is not None:\nresult.append(pair.val)\nreturn result\ndef print(self):\n\"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\nfor pair in self.buckets:\nif pair is not None:\nprint(pair.key, \"->\", pair.val)\n
    array_hash_map.go
    /* \u952e\u503c\u5bf9 */\ntype pair struct {\nkey int\nval string\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\ntype arrayHashMap struct {\nbuckets []*pair\n}\n/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nfunc newArrayHashMap() *arrayHashMap {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nbuckets := make([]*pair, 100)\nreturn &arrayHashMap{buckets: buckets}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (a *arrayHashMap) hashFunc(key int) int {\nindex := key % 100\nreturn index\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (a *arrayHashMap) get(key int) string {\nindex := a.hashFunc(key)\npair := a.buckets[index]\nif pair == nil {\nreturn \"Not Found\"\n}\nreturn pair.val\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (a *arrayHashMap) put(key int, val string) {\npair := &pair{key: key, val: val}\nindex := a.hashFunc(key)\na.buckets[index] = pair\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (a *arrayHashMap) remove(key int) {\nindex := a.hashFunc(key)\n// \u7f6e\u4e3a nil \uff0c\u4ee3\u8868\u5220\u9664\na.buckets[index] = nil\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u5bf9 */\nfunc (a *arrayHashMap) pairSet() []*pair {\nvar pairs []*pair\nfor _, pair := range a.buckets {\nif pair != nil {\npairs = append(pairs, pair)\n}\n}\nreturn pairs\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\nfunc (a *arrayHashMap) keySet() []int {\nvar keys []int\nfor _, pair := range a.buckets {\nif pair != nil {\nkeys = append(keys, pair.key)\n}\n}\nreturn keys\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\nfunc (a *arrayHashMap) valueSet() []string {\nvar values []string\nfor _, pair := range a.buckets {\nif pair != nil {\nvalues = append(values, pair.val)\n}\n}\nreturn values\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (a *arrayHashMap) print() {\nfor _, pair := range a.buckets {\nif pair != nil {\nfmt.Println(pair.key, \"->\", pair.val)\n}\n}\n}\n
    array_hash_map.js
    /* \u952e\u503c\u5bf9 Number -> String */\nclass Pair {\nconstructor(key, val) {\nthis.key = key;\nthis.val = val;\n}\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n#buckets;\nconstructor() {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nthis.#buckets = new Array(100).fill(null);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\n#hashFunc(key) {\nreturn key % 100;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nget(key) {\nlet index = this.#hashFunc(key);\nlet pair = this.#buckets[index];\nif (pair === null) return null;\nreturn pair.val;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nset(key, val) {\nlet index = this.#hashFunc(key);\nthis.#buckets[index] = new Pair(key, val);\n}\n/* \u5220\u9664\u64cd\u4f5c */\ndelete(key) {\nlet index = this.#hashFunc(key);\n// \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\nthis.#buckets[index] = null;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\nentries() {\nlet arr = [];\nfor (let i = 0; i < this.#buckets.length; i++) {\nif (this.#buckets[i]) {\narr.push(this.#buckets[i]);\n}\n}\nreturn arr;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\nkeys() {\nlet arr = [];\nfor (let i = 0; i < this.#buckets.length; i++) {\nif (this.#buckets[i]) {\narr.push(this.#buckets[i]?.key);\n}\n}\nreturn arr;\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\nvalues() {\nlet arr = [];\nfor (let i = 0; i < this.#buckets.length; i++) {\nif (this.#buckets[i]) {\narr.push(this.#buckets[i]?.val);\n}\n}\nreturn arr;\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nprint() {\nlet pairSet = this.entries();\nfor (const pair of pairSet) {\nif (!pair) continue;\nconsole.info(`${pair.key} -> ${pair.val}`);\n}\n}\n}\n
    array_hash_map.ts
    /* \u952e\u503c\u5bf9 Number -> String */\nclass Pair {\npublic key: number;\npublic val: string;\nconstructor(key: number, val: string) {\nthis.key = key;\nthis.val = val;\n}\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\nprivate readonly buckets: (Pair | null)[];\nconstructor() {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nthis.buckets = new Array(100).fill(null);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nprivate hashFunc(key: number): number {\nreturn key % 100;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npublic get(key: number): string | null {\nlet index = this.hashFunc(key);\nlet pair = this.buckets[index];\nif (pair === null) return null;\nreturn pair.val;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\npublic set(key: number, val: string) {\nlet index = this.hashFunc(key);\nthis.buckets[index] = new Pair(key, val);\n}\n/* \u5220\u9664\u64cd\u4f5c */\npublic delete(key: number) {\nlet index = this.hashFunc(key);\n// \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\nthis.buckets[index] = null;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\npublic entries(): (Pair | null)[] {\nlet arr: (Pair | null)[] = [];\nfor (let i = 0; i < this.buckets.length; i++) {\nif (this.buckets[i]) {\narr.push(this.buckets[i]);\n}\n}\nreturn arr;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\npublic keys(): (number | undefined)[] {\nlet arr: (number | undefined)[] = [];\nfor (let i = 0; i < this.buckets.length; i++) {\nif (this.buckets[i]) {\narr.push(this.buckets[i]?.key);\n}\n}\nreturn arr;\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\npublic values(): (string | undefined)[] {\nlet arr: (string | undefined)[] = [];\nfor (let i = 0; i < this.buckets.length; i++) {\nif (this.buckets[i]) {\narr.push(this.buckets[i]?.val);\n}\n}\nreturn arr;\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\npublic print() {\nlet pairSet = this.entries();\nfor (const pair of pairSet) {\nif (!pair) continue;\nconsole.info(`${pair.key} -> ${pair.val}`);\n}\n}\n}\n
    array_hash_map.c
    /* \u952e\u503c\u5bf9 int->string */\nstruct pair {\nint key;\nchar *val;\n};\ntypedef struct pair pair;\n[class]{arrayHashMap}-[func]{}\n
    array_hash_map.cs
    /* \u952e\u503c\u5bf9 int->string */\nclass Pair {\npublic int key;\npublic string val;\npublic Pair(int key, string val) {\nthis.key = key;\nthis.val = val;\n}\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\nprivate List<Pair?> buckets;\npublic ArrayHashMap() {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nbuckets = new();\nfor (int i = 0; i < 100; i++) {\nbuckets.Add(null);\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nprivate int hashFunc(int key) {\nint index = key % 100;\nreturn index;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npublic string? get(int key) {\nint index = hashFunc(key);\nPair? pair = buckets[index];\nif (pair == null) return null;\nreturn pair.val;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\npublic void put(int key, string val) {\nPair pair = new Pair(key, val);\nint index = hashFunc(key);\nbuckets[index] = pair;\n}\n/* \u5220\u9664\u64cd\u4f5c */\npublic void remove(int key) {\nint index = hashFunc(key);\n// \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\nbuckets[index] = null;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\npublic List<Pair> pairSet() {\nList<Pair> pairSet = new();\nforeach (Pair? pair in buckets) {\nif (pair != null)\npairSet.Add(pair);\n}\nreturn pairSet;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\npublic List<int> keySet() {\nList<int> keySet = new();\nforeach (Pair? pair in buckets) {\nif (pair != null)\nkeySet.Add(pair.key);\n}\nreturn keySet;\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\npublic List<string> valueSet() {\nList<string> valueSet = new();\nforeach (Pair? pair in buckets) {\nif (pair != null)\nvalueSet.Add(pair.val);\n}\nreturn valueSet;\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\npublic void print() {\nforeach (Pair kv in pairSet()) {\nConsole.WriteLine(kv.key + \" -> \" + kv.val);\n}\n}\n}\n
    array_hash_map.swift
    /* \u952e\u503c\u5bf9 */\nclass Pair {\nvar key: Int\nvar val: String\ninit(key: Int, val: String) {\nself.key = key\nself.val = val\n}\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\nprivate var buckets: [Pair?] = []\ninit() {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nfor _ in 0 ..< 100 {\nbuckets.append(nil)\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nprivate func hashFunc(key: Int) -> Int {\nlet index = key % 100\nreturn index\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc get(key: Int) -> String? {\nlet index = hashFunc(key: key)\nlet pair = buckets[index]\nreturn pair?.val\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc put(key: Int, val: String) {\nlet pair = Pair(key: key, val: val)\nlet index = hashFunc(key: key)\nbuckets[index] = pair\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfunc remove(key: Int) {\nlet index = hashFunc(key: key)\n// \u7f6e\u4e3a nil \uff0c\u4ee3\u8868\u5220\u9664\nbuckets[index] = nil\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\nfunc pairSet() -> [Pair] {\nvar pairSet: [Pair] = []\nfor pair in buckets {\nif let pair = pair {\npairSet.append(pair)\n}\n}\nreturn pairSet\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\nfunc keySet() -> [Int] {\nvar keySet: [Int] = []\nfor pair in buckets {\nif let pair = pair {\nkeySet.append(pair.key)\n}\n}\nreturn keySet\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\nfunc valueSet() -> [String] {\nvar valueSet: [String] = []\nfor pair in buckets {\nif let pair = pair {\nvalueSet.append(pair.val)\n}\n}\nreturn valueSet\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc print() {\nfor pair in pairSet() {\nSwift.print(\"\\(pair.key) -> \\(pair.val)\")\n}\n}\n}\n
    array_hash_map.zig
    // \u952e\u503c\u5bf9\nconst Pair = struct {\nkey: usize = undefined,\nval: []const u8 = undefined,\npub fn init(key: usize, val: []const u8) Pair {\nreturn Pair {\n.key = key,\n.val = val,\n};\n}\n};\n// \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868\nfn ArrayHashMap(comptime T: type) type {\nreturn struct {\nbuckets: ?std.ArrayList(?T) = null,\nmem_allocator: std.mem.Allocator = undefined,\nconst Self = @This();\n// \u6784\u9020\u65b9\u6cd5\npub fn init(self: *Self, allocator: std.mem.Allocator) !void {\nself.mem_allocator = allocator;\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nself.buckets = std.ArrayList(?T).init(self.mem_allocator);\nvar i: i32 = 0;\nwhile (i < 100) : (i += 1) {\ntry self.buckets.?.append(null);\n}\n}\n// \u6790\u6784\u65b9\u6cd5\npub fn deinit(self: *Self) void {\nif (self.buckets != null) self.buckets.?.deinit();\n}\n// \u54c8\u5e0c\u51fd\u6570\nfn hashFunc(key: usize) usize {\nvar index = key % 100;\nreturn index;\n}\n// \u67e5\u8be2\u64cd\u4f5c\npub fn get(self: *Self, key: usize) []const u8 {\nvar index = hashFunc(key);\nvar pair = self.buckets.?.items[index];\nreturn pair.?.val;\n}\n// \u6dfb\u52a0\u64cd\u4f5c\npub fn put(self: *Self, key: usize, val: []const u8) !void {\nvar pair = Pair.init(key, val);\nvar index = hashFunc(key);\nself.buckets.?.items[index] = pair;\n}\n// \u5220\u9664\u64cd\u4f5c\npub fn remove(self: *Self, key: usize) !void {\nvar index = hashFunc(key);\n// \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\nself.buckets.?.items[index] = null;\n}       // \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9\npub fn pairSet(self: *Self) !*std.ArrayList(T) {\nvar entry_set = std.ArrayList(T).init(self.mem_allocator);\nfor (self.buckets.?.items) |item| {\nif (item == null) continue;\ntry entry_set.append(item.?);\n}\nreturn &entry_set;\n}  // \u83b7\u53d6\u6240\u6709\u952e\npub fn keySet(self: *Self) !*std.ArrayList(usize) {\nvar key_set = std.ArrayList(usize).init(self.mem_allocator);\nfor (self.buckets.?.items) |item| {\nif (item == null) continue;\ntry key_set.append(item.?.key);\n}\nreturn &key_set;\n}  // \u83b7\u53d6\u6240\u6709\u503c\npub fn valueSet(self: *Self) !*std.ArrayList([]const u8) {\nvar value_set = std.ArrayList([]const u8).init(self.mem_allocator);\nfor (self.buckets.?.items) |item| {\nif (item == null) continue;\ntry value_set.append(item.?.val);\n}\nreturn &value_set;\n}\n// \u6253\u5370\u54c8\u5e0c\u8868\npub fn print(self: *Self) !void {\nvar entry_set = try self.pairSet();\ndefer entry_set.deinit();\nfor (entry_set.items) |item| {\nstd.debug.print(\"{} -> {s}\\n\", .{item.key, item.val});\n}\n}\n};\n}\n
    array_hash_map.dart
    /* \u952e\u503c\u5bf9 */\nclass Pair {\nint key;\nString val;\nPair(this.key, this.val);\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\nlate List<Pair?> _buckets;\nArrayHashMap() {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n_buckets = List.filled(100, null);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint _hashFunc(int key) {\nfinal int index = key % 100;\nreturn index;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nString? get(int key) {\nfinal int index = _hashFunc(key);\nfinal Pair? pair = _buckets[index];\nif (pair == null) {\nreturn null;\n}\nreturn pair.val;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(int key, String val) {\nfinal Pair pair = Pair(key, val);\nfinal int index = _hashFunc(key);\n_buckets[index] = pair;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid remove(int key) {\nfinal int index = _hashFunc(key);\n_buckets[index] = null;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\nList<Pair> pairSet() {\nList<Pair> pairSet = [];\nfor (final Pair? pair in _buckets) {\nif (pair != null) {\npairSet.add(pair);\n}\n}\nreturn pairSet;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\nList<int> keySet() {\nList<int> keySet = [];\nfor (final Pair? pair in _buckets) {\nif (pair != null) {\nkeySet.add(pair.key);\n}\n}\nreturn keySet;\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\nList<String> values() {\nList<String> valueSet = [];\nfor (final Pair? pair in _buckets) {\nif (pair != null) {\nvalueSet.add(pair.val);\n}\n}\nreturn valueSet;\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid printHashMap() {\nfor (final Pair kv in pairSet()) {\nprint(\"${kv.key} -> ${kv.val}\");\n}\n}\n}\n
    "},{"location":"chapter_hashing/hash_map/#613","title":"6.1.3. \u00a0 \u54c8\u5e0c\u51b2\u7a81","text":"

    \u672c\u8d28\u4e0a\u770b\uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u662f\u5c06\u4e00\u4e2a\u5e9e\u5927\u7684\u8f93\u5165\u7a7a\u95f4\uff08key \u8303\u56f4\uff09\u6620\u5c04\u5230\u4e00\u4e2a\u8f83\u5c0f\u7684\u8f93\u51fa\u7a7a\u95f4\uff08\u6570\u7ec4\u7d22\u5f15\u8303\u56f4\uff09\u3002\u56e0\u6b64\uff0c\u7406\u8bba\u4e0a\u4e00\u5b9a\u5b58\u5728\u201d\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 \\bmod 100 = 20336 \\bmod 100 = 36 \\]

    \u4e24\u4e2a\u5b66\u53f7\u6307\u5411\u4e86\u540c\u4e00\u4e2a\u59d3\u540d\uff0c\u8fd9\u663e\u7136\u662f\u4e0d\u5bf9\u7684\u3002\u6211\u4eec\u628a\u8fd9\u79cd\u60c5\u51b5\u79f0\u4e3a\u201c\u54c8\u5e0c\u51b2\u7a81\u201d\u3002\u5728\u4e0b\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u91cd\u70b9\u8ba8\u8bba\u5982\u4f55\u89e3\u51b3\u51b2\u7a81\u95ee\u9898\u3002

    Fig. \u54c8\u5e0c\u51b2\u7a81\u793a\u4f8b

    "},{"location":"chapter_hashing/summary/","title":"6.3. \u00a0 \u5c0f\u7ed3","text":"
    • \u54c8\u5e0c\u8868\u80fd\u591f\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u5c06\u952e key \u6620\u5c04\u5230\u503c value\uff0c\u6548\u7387\u975e\u5e38\u9ad8\u3002
    • \u5e38\u89c1\u7684\u54c8\u5e0c\u8868\u64cd\u4f5c\u5305\u62ec\u67e5\u8be2\u3001\u6dfb\u52a0\u4e0e\u5220\u9664\u952e\u503c\u5bf9\u3001\u904d\u5386\u952e\u503c\u5bf9\u7b49\u3002
    • \u54c8\u5e0c\u51fd\u6570\u5c06 key \u6620\u5c04\u4e3a\u6570\u7ec4\u7d22\u5f15\uff08\u6876\u7d22\u5f15\uff09\uff0c\u4ece\u800c\u8bbf\u95ee\u5bf9\u5e94\u7684\u503c value \u3002
    • \u4e24\u4e2a\u4e0d\u540c\u7684 key \u53ef\u80fd\u5728\u7ecf\u8fc7\u54c8\u5e0c\u51fd\u6570\u540e\u5f97\u5230\u76f8\u540c\u7684\u7d22\u5f15\uff0c\u5bfc\u81f4\u67e5\u8be2\u7ed3\u679c\u51fa\u9519\uff0c\u8fd9\u79cd\u73b0\u8c61\u88ab\u79f0\u4e3a\u54c8\u5e0c\u51b2\u7a81\u3002
    • \u7f13\u89e3\u54c8\u5e0c\u51b2\u7a81\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u6269\u5bb9\u54c8\u5e0c\u8868\u548c\u4f18\u5316\u54c8\u5e0c\u8868\u7684\u8868\u793a\u65b9\u6cd5\u3002
    • \u8d1f\u8f7d\u56e0\u5b50\u5b9a\u4e49\u4e3a\u54c8\u5e0c\u8868\u4e2d\u5143\u7d20\u6570\u91cf\u9664\u4ee5\u6876\u6570\u91cf\uff0c\u53cd\u6620\u4e86\u54c8\u5e0c\u51b2\u7a81\u7684\u4e25\u91cd\u7a0b\u5ea6\uff0c\u5e38\u7528\u4f5c\u89e6\u53d1\u54c8\u5e0c\u8868\u6269\u5bb9\u7684\u6761\u4ef6\u3002\u4e0e\u6570\u7ec4\u6269\u5bb9\u7c7b\u4f3c\uff0c\u54c8\u5e0c\u8868\u6269\u5bb9\u64cd\u4f5c\u4e5f\u4f1a\u4ea7\u751f\u8f83\u5927\u7684\u5f00\u9500\u3002
    • \u94fe\u5f0f\u5730\u5740\u901a\u8fc7\u5c06\u5355\u4e2a\u5143\u7d20\u8f6c\u5316\u4e3a\u94fe\u8868\uff0c\u5c06\u6240\u6709\u51b2\u7a81\u5143\u7d20\u5b58\u50a8\u5728\u540c\u4e00\u4e2a\u94fe\u8868\u4e2d\uff0c\u4ece\u800c\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u3002\u7136\u800c\uff0c\u8fc7\u957f\u7684\u94fe\u8868\u4f1a\u964d\u4f4e\u67e5\u8be2\u6548\u7387\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u94fe\u8868\u8f6c\u6362\u4e3a AVL \u6811\u6216\u7ea2\u9ed1\u6811\u6765\u6539\u5584\u3002
    • \u5f00\u653e\u5bfb\u5740\u901a\u8fc7\u591a\u6b21\u63a2\u6d4b\u6765\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u3002\u7ebf\u6027\u63a2\u6d4b\u4f7f\u7528\u56fa\u5b9a\u6b65\u957f\uff0c\u7f3a\u70b9\u662f\u4e0d\u80fd\u5220\u9664\u5143\u7d20\u4e14\u5bb9\u6613\u4ea7\u751f\u805a\u96c6\u3002\u591a\u6b21\u54c8\u5e0c\u4f7f\u7528\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570\u8fdb\u884c\u63a2\u6d4b\uff0c\u76f8\u5bf9\u7ebf\u6027\u63a2\u6d4b\u4e0d\u6613\u4ea7\u751f\u805a\u96c6\uff0c\u4f46\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570\u589e\u52a0\u4e86\u8ba1\u7b97\u91cf\u3002
    • \u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u91c7\u53d6\u4e86\u4e0d\u540c\u7684\u54c8\u5e0c\u8868\u5b9e\u73b0\u7b56\u7565\u3002\u4f8b\u5982\uff0cJava \u7684 HashMap \u4f7f\u7528\u94fe\u5f0f\u5730\u5740\uff0c\u800c Python \u7684 Dict \u91c7\u7528\u5f00\u653e\u5bfb\u5740\u3002
    "},{"location":"chapter_heap/","title":"8. \u00a0 \u5806","text":""},{"location":"chapter_heap/build_heap/","title":"8.2. \u00a0 \u5efa\u5806\u64cd\u4f5c","text":"

    \u5982\u679c\u6211\u4eec\u60f3\u8981\u6839\u636e\u8f93\u5165\u5217\u8868\u751f\u6210\u4e00\u4e2a\u5806\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a\u300c\u5efa\u5806\u300d\u3002

    "},{"location":"chapter_heap/build_heap/#821","title":"8.2.1. \u00a0 \u501f\u52a9\u5165\u5806\u65b9\u6cd5\u5b9e\u73b0","text":"

    \u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u501f\u52a9\u201c\u5143\u7d20\u5165\u5806\u64cd\u4f5c\u201d\u5b9e\u73b0\uff0c\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u7a7a\u5806\uff0c\u7136\u540e\u5c06\u5217\u8868\u5143\u7d20\u4f9d\u6b21\u6dfb\u52a0\u5230\u5806\u4e2d\u3002

    \u8bbe\u5143\u7d20\u6570\u91cf\u4e3a \\(n\\) \uff0c\u5219\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u5165\u5806\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u3002\u5728\u4f9d\u6b21\u6dfb\u52a0\u5143\u7d20\u65f6\uff0c\u5806\u7684\u5e73\u5747\u957f\u5ea6\u4e3a \\(\\frac{n}{2}\\) \uff0c\u56e0\u6b64\u8be5\u65b9\u6cd5\u7684\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002

    "},{"location":"chapter_heap/build_heap/#822","title":"8.2.2. \u00a0 \u57fa\u4e8e\u5806\u5316\u64cd\u4f5c\u5b9e\u73b0","text":"

    \u6709\u8da3\u7684\u662f\uff0c\u5b58\u5728\u4e00\u79cd\u66f4\u9ad8\u6548\u7684\u5efa\u5806\u65b9\u6cd5\uff0c\u5176\u65f6\u95f4\u590d\u6742\u5ea6\u4ec5\u4e3a \\(O(n)\\) \u3002\u6211\u4eec\u5148\u5c06\u5217\u8868\u6240\u6709\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u5230\u5806\u4e2d\uff0c\u7136\u540e\u8fed\u4ee3\u5730\u5bf9\u5404\u4e2a\u8282\u70b9\u6267\u884c\u201c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u201d\u3002\u5f53\u7136\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u5bf9\u53f6\u8282\u70b9\u6267\u884c\u5806\u5316\u64cd\u4f5c\uff0c\u56e0\u4e3a\u5b83\u4eec\u6ca1\u6709\u5b50\u8282\u70b9\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nmaxHeap = new ArrayList<>(nums);\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor (int i = parent(size() - 1); i >= 0; i--) {\nsiftDown(i);\n}\n}\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\nmaxHeap = nums;\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor (int i = parent(size() - 1); i >= 0; i--) {\nsiftDown(i);\n}\n}\n
    my_heap.py
    def __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\nself.max_heap = nums\n# \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor i in range(self.parent(self.size() - 1), -1, -1):\nself.sift_down(i)\n
    my_heap.go
    /* \u6784\u9020\u65b9\u6cd5\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\nh := &maxHeap{data: nums}\nfor i := len(h.data) - 1; i >= 0; i-- {\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nh.siftDown(i)\n}\nreturn h\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\nthis.#maxHeap = nums === undefined ? [] : [...nums];\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor (let i = this.#parent(this.size() - 1); i >= 0; i--) {\nthis.#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\nthis.maxHeap = nums === undefined ? [] : [...nums];\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor (let i = this.parent(this.size() - 1); i >= 0; i--) {\nthis.siftDown(i);\n}\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\nmaxHeap *h = (maxHeap *)malloc(sizeof(maxHeap));\nh->size = size;\nmemcpy(h->data, nums, size * sizeof(int));\nfor (int i = size - 1; i >= 0; i--) {\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nsiftDown(h, i);\n}\nreturn h;\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\nmaxHeap = new List<int>(nums);\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nvar size = parent(this.size() - 1);\nfor (int i = size; i >= 0; i--) {\nsiftDown(i);\n}\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\nmaxHeap = nums\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor i in stride(from: parent(i: size() - 1), through: 0, by: -1) {\nsiftDown(i: i)\n}\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 {\nif (self.max_heap != null) return;\nself.max_heap = std.ArrayList(T).init(allocator);\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\ntry self.max_heap.?.appendSlice(nums);\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nvar i: usize = parent(self.size() - 1) + 1;\nwhile (i > 0) : (i -= 1) {\ntry self.siftDown(i - 1);\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\nfor (int i = _parent(size() - 1); i >= 0; i--) {\n_siftDown(i);\n}\n}\n
    "},{"location":"chapter_heap/build_heap/#823","title":"8.2.3. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"

    \u4e3a\u4ec0\u4e48\u7b2c\u4e8c\u79cd\u5efa\u5806\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(n)\\) \uff1f\u6211\u4eec\u6765\u5c55\u5f00\u63a8\u7b97\u4e00\u4e0b\u3002

    • \u5b8c\u5168\u4e8c\u53c9\u6811\u4e2d\uff0c\u8bbe\u8282\u70b9\u603b\u6570\u4e3a \\(n\\) \uff0c\u5219\u53f6\u8282\u70b9\u6570\u91cf\u4e3a \\((n + 1) / 2\\) \uff0c\u5176\u4e2d \\(/\\) \u4e3a\u5411\u4e0b\u6574\u9664\u3002\u56e0\u6b64\uff0c\u5728\u6392\u9664\u53f6\u8282\u70b9\u540e\uff0c\u9700\u8981\u5806\u5316\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\((n - 1)/2\\) \uff0c\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff1b
    • \u5728\u4ece\u9876\u81f3\u5e95\u5806\u5316\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6bcf\u4e2a\u8282\u70b9\u6700\u591a\u5806\u5316\u5230\u53f6\u8282\u70b9\uff0c\u56e0\u6b64\u6700\u5927\u8fed\u4ee3\u6b21\u6570\u4e3a\u4e8c\u53c9\u6811\u9ad8\u5ea6 \\(O(\\log n)\\) \uff1b

    \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\u7136\u800c\uff0c\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\u7279\u6027\u3002

    \u63a5\u4e0b\u6765\u6211\u4eec\u6765\u8fdb\u884c\u66f4\u4e3a\u8be6\u7ec6\u7684\u8ba1\u7b97\u3002\u4e3a\u4e86\u51cf\u5c0f\u8ba1\u7b97\u96be\u5ea6\uff0c\u6211\u4eec\u5047\u8bbe\u6811\u662f\u4e00\u4e2a\u201c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u201d\uff0c\u8be5\u5047\u8bbe\u4e0d\u4f1a\u5f71\u54cd\u8ba1\u7b97\u7ed3\u679c\u7684\u6b63\u786e\u6027\u3002\u8bbe\u4e8c\u53c9\u6811\uff08\u5373\u5806\uff09\u8282\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u6811\u9ad8\u5ea6\u4e3a \\(h\\) \u3002\u4e0a\u6587\u63d0\u5230\uff0c\u8282\u70b9\u5806\u5316\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

    Fig. \u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u5404\u5c42\u8282\u70b9\u6570\u91cf

    \u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5404\u5c42\u7684\u201c\u8282\u70b9\u6570\u91cf \\(\\times\\) \u8282\u70b9\u9ad8\u5ea6\u201d\u6c42\u548c\uff0c\u4ece\u800c\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) + \\cdots + 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

    \\[ \\begin{aligned} T(h) & = 2^0h + 2^1(h-1) + 2^2(h-2) + \\cdots + 2^{h-1}\\times1 \\newline 2 T(h) & = 2^1h + 2^2(h-1) + 2^3(h-2) + \\cdots + 2^{h}\\times1 \\newline \\end{aligned} \\]

    \u4f7f\u7528\u9519\u4f4d\u76f8\u51cf\u6cd5\uff0c\u4ee4\u4e0b\u5f0f \\(2 T(h)\\) \u51cf\u53bb\u4e0a\u5f0f \\(T(h)\\) \uff0c\u53ef\u5f97

    \\[ 2T(h) - T(h) = T(h) = -2^0h + 2^1 + 2^2 + \\cdots + 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

    \\[ \\begin{aligned} T(h) & = 2 \\frac{1 - 2^h}{1 - 2} - h \\newline & = 2^{h+1} - h \\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\u53ef\u5206\u4e3a\u4e24\u79cd\u7c7b\u578b\uff1a

    • \u300c\u5927\u9876\u5806 Max Heap\u300d\uff0c\u4efb\u610f\u8282\u70b9\u7684\u503c \\(\\geq\\) \u5176\u5b50\u8282\u70b9\u7684\u503c\uff1b
    • \u300c\u5c0f\u9876\u5806 Min Heap\u300d\uff0c\u4efb\u610f\u8282\u70b9\u7684\u503c \\(\\leq\\) \u5176\u5b50\u8282\u70b9\u7684\u503c\uff1b

    Fig. \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\uff1a

    • \u6700\u5e95\u5c42\u8282\u70b9\u9760\u5de6\u586b\u5145\uff0c\u5176\u4ed6\u5c42\u7684\u8282\u70b9\u90fd\u88ab\u586b\u6ee1\u3002
    • \u6211\u4eec\u5c06\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\u79f0\u4e3a\u300c\u5806\u9876\u300d\uff0c\u5c06\u5e95\u5c42\u6700\u9760\u53f3\u7684\u8282\u70b9\u79f0\u4e3a\u300c\u5806\u5e95\u300d\u3002
    • \u5bf9\u4e8e\u5927\u9876\u5806\uff08\u5c0f\u9876\u5806\uff09\uff0c\u5806\u9876\u5143\u7d20\uff08\u5373\u6839\u8282\u70b9\uff09\u7684\u503c\u5206\u522b\u662f\u6700\u5927\uff08\u6700\u5c0f\uff09\u7684\u3002
    "},{"location":"chapter_heap/heap/#811","title":"8.1.1. \u00a0 \u5806\u5e38\u7528\u64cd\u4f5c","text":"

    \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\u4f5c\u5b9e\u73b0\u4f18\u5148\u961f\u5217\uff0c\u5927\u9876\u5806\u76f8\u5f53\u4e8e\u5143\u7d20\u6309\u4ece\u5927\u5230\u5c0f\u987a\u5e8f\u51fa\u961f\u7684\u4f18\u5148\u961f\u5217\u3002\u4ece\u4f7f\u7528\u89d2\u5ea6\u6765\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u300c\u4f18\u5148\u961f\u5217\u300d\u548c\u300c\u5806\u300d\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\u4f7f\u7528\u300c\u5806\u300d\u6765\u547d\u540d\u3002

    \u5806\u7684\u5e38\u7528\u64cd\u4f5c\u89c1\u4e0b\u8868\uff0c\u65b9\u6cd5\u540d\u9700\u8981\u6839\u636e\u7f16\u7a0b\u8bed\u8a00\u6765\u786e\u5b9a\u3002

    \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

    Tip

    \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\u4fee\u6539 Comparator \u6765\u5b9e\u73b0\u201c\u5c0f\u9876\u5806\u201d\u4e0e\u201c\u5927\u9876\u5806\u201d\u4e4b\u95f4\u7684\u8f6c\u6362\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.offer(1);\nmaxHeap.offer(3);\nmaxHeap.offer(2);\nmaxHeap.offer(5);\nmaxHeap.offer(4);\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.peek(); // 5\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 = heap.poll();  // 5\npeek = heap.poll();  // 4\npeek = heap.poll();  // 3\npeek = heap.poll();  // 2\npeek = heap.poll();  // 1\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.size();\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = maxHeap.isEmpty();\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nminHeap = new PriorityQueue<>(Arrays.asList(1, 3, 2, 5, 4));\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/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.push(1);\nmaxHeap.push(3);\nmaxHeap.push(2);\nmaxHeap.push(5);\nmaxHeap.push(4);\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.top(); // 5\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/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.size();\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = maxHeap.empty();\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.py
    # \u521d\u59cb\u5316\u5c0f\u9876\u5806\nmin_heap, flag = [], 1\n# \u521d\u59cb\u5316\u5927\u9876\u5806\nmax_heap, flag = [], -1\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# \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# \u83b7\u53d6\u5806\u9876\u5143\u7d20\npeek: int = flag * max_heap[0] # 5\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# \u83b7\u53d6\u5806\u5927\u5c0f\nsize: int = len(max_heap)\n# \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = not max_heap\n# \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806\nmin_heap: List[int] = [1, 3, 2, 5, 4]\nheapq.heapify(min_heap)\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// 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// 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\nlast := (*h)[len(*h)-1]\n*h = (*h)[:len(*h)-1]\nreturn last\n}\n// Len sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Len() int {\nreturn len(*h)\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\nreturn (*h)[i].(int) > (*h)[j].(int)\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// Top \u83b7\u53d6\u5806\u9876\u5143\u7d20\nfunc (h *intHeap) Top() any {\nreturn (*h)[0]\n}\n/* Driver Code */\nfunc TestHeap(t *testing.T) {\n/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5927\u9876\u5806\nmaxHeap := &intHeap{}\nheap.Init(maxHeap)\n/* \u5143\u7d20\u5165\u5806 */\n// \u8c03\u7528 heap.Interface \u7684\u65b9\u6cd5\uff0c\u6765\u6dfb\u52a0\u5143\u7d20\nheap.Push(maxHeap, 1)\nheap.Push(maxHeap, 3)\nheap.Push(maxHeap, 2)\nheap.Push(maxHeap, 4)\nheap.Push(maxHeap, 5)\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\ntop := maxHeap.Top()\nfmt.Printf(\"\u5806\u9876\u5143\u7d20\u4e3a %d\\n\", top)\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u8c03\u7528 heap.Interface \u7684\u65b9\u6cd5\uff0c\u6765\u79fb\u9664\u5143\u7d20\nheap.Pop(maxHeap) // 5\nheap.Pop(maxHeap) // 4\nheap.Pop(maxHeap) // 3\nheap.Pop(maxHeap) // 2\nheap.Pop(maxHeap) // 1\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nsize := len(*maxHeap)\nfmt.Printf(\"\u5806\u5143\u7d20\u6570\u91cf\u4e3a %d\\n\", size)\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nisEmpty := len(*maxHeap) == 0\nfmt.Printf(\"\u5806\u662f\u5426\u4e3a\u7a7a %t\\n\", isEmpty)\n}\n
    heap.js
    // JavaScript \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
    heap.ts
    // TypeScript \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
    heap.c
    // C \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
    heap.cs
    /* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nPriorityQueue<int, int> minHeap = new PriorityQueue<int, int>();\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 PriorityQueue<int, int>(Comparer<int>.Create((x, y) => y - x));\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/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.Peek();//5\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/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.Count;\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = maxHeap.Count == 0;\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nminHeap = new PriorityQueue<int, int>(new List<(int, int)> { (1, 1), (3, 3), (2, 2), (5, 5), (4, 4), });\n
    heap.swift
    // Swift \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
    heap.zig
    \n
    heap.dart
    // Dart \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\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":"\u5806\u7684\u5b58\u50a8\u4e0e\u8868\u793a","text":"

    \u6211\u4eec\u5728\u4e8c\u53c9\u6811\u7ae0\u8282\u4e2d\u5b66\u4e60\u5230\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\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

    \u5177\u4f53\u800c\u8a00\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\u53d6\u6574\uff09\u3002\u5f53\u7d22\u5f15\u8d8a\u754c\u65f6\uff0c\u8868\u793a\u7a7a\u8282\u70b9\u6216\u8282\u70b9\u4e0d\u5b58\u5728\u3002

    Fig. \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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart my_heap.java
    /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nint right(int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nint parent(int i) {\nreturn (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n
    my_heap.cpp
    /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nint right(int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nint parent(int i) {\nreturn (i - 1) / 2; // \u5411\u4e0b\u53d6\u6574\n}\n
    my_heap.py
    def left(self, i: int) -> int:\n\"\"\"\u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15\"\"\"\nreturn 2 * i + 1\ndef right(self, i: int) -> int:\n\"\"\"\u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15\"\"\"\nreturn 2 * i + 2\ndef parent(self, i: int) -> int:\n\"\"\"\u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15\"\"\"\nreturn (i - 1) // 2  # \u5411\u4e0b\u6574\u9664\n
    my_heap.go
    /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nfunc (h *maxHeap) left(i int) int {\nreturn 2*i + 1\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nfunc (h *maxHeap) right(i int) int {\nreturn 2*i + 2\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nfunc (h *maxHeap) parent(i int) int {\n// \u5411\u4e0b\u6574\u9664\nreturn (i - 1) / 2\n}\n
    my_heap.js
    /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\n#left(i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\n#right(i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\n#parent(i) {\nreturn 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 {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nright(i: number): number {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nparent(i: number): number {\nreturn Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n}\n
    my_heap.c
    /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint left(maxHeap *h, int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nint right(maxHeap *h, int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nint parent(maxHeap *h, int i) {\nreturn (i - 1) / 2;\n}\n
    my_heap.cs
    /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nint right(int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nint parent(int i) {\nreturn (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n
    my_heap.swift
    /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nfunc left(i: Int) -> Int {\n2 * i + 1\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nfunc right(i: Int) -> Int {\n2 * i + 2\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.zig
    // \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15\nfn left(i: usize) usize {\nreturn 2 * i + 1;\n}\n// \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15\nfn right(i: usize) usize {\nreturn 2 * i + 2;\n}\n// \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15\nfn parent(i: usize) usize {\n// return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\nreturn @divFloor(i - 1, 2);\n}\n
    my_heap.dart
    /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint _left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nint _right(int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nint _parent(int i) {\nreturn (i - 1) ~/ 2; // \u5411\u4e0b\u6574\u9664\n}\n
    "},{"location":"chapter_heap/heap/#_2","title":"\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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart my_heap.java
    /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\nreturn maxHeap.get(0);\n}\n
    my_heap.cpp
    /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\nreturn maxHeap[0];\n}\n
    my_heap.py
    def peek(self) -> int:\n\"\"\"\u8bbf\u95ee\u5806\u9876\u5143\u7d20\"\"\"\nreturn self.max_heap[0]\n
    my_heap.go
    /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunc (h *maxHeap) peek() any {\nreturn h.data[0]\n}\n
    my_heap.js
    /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\npeek() {\nreturn this.#maxHeap[0];\n}\n
    my_heap.ts
    /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\npeek(): number {\nreturn this.maxHeap[0];\n}\n
    my_heap.c
    /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek(maxHeap *h) {\nreturn h->data[0];\n}\n
    my_heap.cs
    /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\nreturn maxHeap[0];\n}\n
    my_heap.swift
    /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunc peek() -> Int {\nmaxHeap[0]\n}\n
    my_heap.zig
    // \u8bbf\u95ee\u5806\u9876\u5143\u7d20\nfn peek(self: *Self) T {\nreturn self.max_heap.?.items[0];\n}  
    my_heap.dart
    /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\nreturn _maxHeap[0];\n}\n
    "},{"location":"chapter_heap/heap/#_3","title":"\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\u3002\u56e0\u6b64\uff0c\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\u5177\u4f53\u6765\u8bf4\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\u9700\u4ea4\u6362\u7684\u8282\u70b9\u65f6\u7ed3\u675f\u3002

    <1><2><3><4><5><6><7><8><9>

    \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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart my_heap.java
    /* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n// \u6dfb\u52a0\u8282\u70b9\nmaxHeap.add(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nsiftUp(size() - 1);\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\nwhile (true) {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nint p = parent(i);\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p < 0 || maxHeap.get(i) <= maxHeap.get(p))\nbreak;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n
    my_heap.cpp
    /* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n// \u6dfb\u52a0\u8282\u70b9\nmaxHeap.push_back(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nsiftUp(size() - 1);\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\nwhile (true) {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nint p = parent(i);\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p < 0 || maxHeap[i] <= maxHeap[p])\nbreak;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(maxHeap[i], maxHeap[p]);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n
    my_heap.py
    def push(self, val: int):\n\"\"\"\u5143\u7d20\u5165\u5806\"\"\"\n# \u6dfb\u52a0\u8282\u70b9\nself.max_heap.append(val)\n# \u4ece\u5e95\u81f3\u9876\u5806\u5316\nself.sift_up(self.size() - 1)\ndef sift_up(self, i: int):\n\"\"\"\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316\"\"\"\nwhile True:\n# \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\np = self.parent(i)\n# \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif p < 0 or self.max_heap[i] <= self.max_heap[p]:\nbreak\n# \u4ea4\u6362\u4e24\u8282\u70b9\nself.swap(i, p)\n# \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p\n
    my_heap.go
    /* \u5143\u7d20\u5165\u5806 */\nfunc (h *maxHeap) push(val any) {\n// \u6dfb\u52a0\u8282\u70b9\nh.data = append(h.data, val)\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nh.siftUp(len(h.data) - 1)\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfunc (h *maxHeap) siftUp(i int) {\nfor true {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\np := h.parent(i)\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif p < 0 || h.data[i].(int) <= h.data[p].(int) {\nbreak\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nh.swap(i, p)\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p\n}\n}\n
    my_heap.js
    /* \u5143\u7d20\u5165\u5806 */\npush(val) {\n// \u6dfb\u52a0\u8282\u70b9\nthis.#maxHeap.push(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nthis.#siftUp(this.size() - 1);\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\n#siftUp(i) {\nwhile (true) {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nconst p = this.#parent(i);\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p < 0 || this.#maxHeap[i] <= this.#maxHeap[p]) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nthis.#swap(i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n
    my_heap.ts
    /* \u5143\u7d20\u5165\u5806 */\npush(val: number): void {\n// \u6dfb\u52a0\u8282\u70b9\nthis.maxHeap.push(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nthis.siftUp(this.size() - 1);\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nsiftUp(i: number): void {\nwhile (true) {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nconst p = this.parent(i);\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p < 0 || this.maxHeap[i] <= this.maxHeap[p]) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nthis.swap(i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n
    my_heap.c
    /* \u5143\u7d20\u5165\u5806 */\nvoid push(maxHeap *h, int val) {\n// \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4e0d\u5e94\u8be5\u6dfb\u52a0\u8fd9\u4e48\u591a\u8282\u70b9\nif (h->size == MAX_SIZE) {\nprintf(\"heap is full!\");\nreturn;\n}\n// \u6dfb\u52a0\u8282\u70b9\nh->data[h->size] = val;\nh->size++;\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nsiftUp(h, h->size - 1);\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(maxHeap *h, int i) {\nwhile (true) {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nint p = parent(h, i);\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p < 0 || h->data[i] <= h->data[p]) {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(h, i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n
    my_heap.cs
    /* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n// \u6dfb\u52a0\u8282\u70b9\nmaxHeap.Add(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nsiftUp(size() - 1);\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\nwhile (true) {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nint p = parent(i);\n// \u82e5\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\uff0c\u5219\u7ed3\u675f\u5806\u5316\nif (p < 0 || maxHeap[i] <= maxHeap[p])\nbreak;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n
    my_heap.swift
    /* \u5143\u7d20\u5165\u5806 */\nfunc push(val: Int) {\n// \u6dfb\u52a0\u8282\u70b9\nmaxHeap.append(val)\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nsiftUp(i: size() - 1)\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfunc siftUp(i: Int) {\nvar i = i\nwhile true {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nlet p = parent(i: i)\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif p < 0 || maxHeap[i] <= maxHeap[p] {\nbreak\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(i: i, j: p)\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p\n}\n}\n
    my_heap.zig
    // \u5143\u7d20\u5165\u5806\nfn push(self: *Self, val: T) !void {\n// \u6dfb\u52a0\u8282\u70b9\ntry self.max_heap.?.append(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\ntry self.siftUp(self.size() - 1);\n}  // \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316\nfn siftUp(self: *Self, i_: usize) !void {\nvar i = i_;\nwhile (true) {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nvar p = parent(i);\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p < 0 or self.max_heap.?.items[i] <= self.max_heap.?.items[p]) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\ntry self.swap(i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = 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[class]{MaxHeap}-[func]{siftUp}\n
    "},{"location":"chapter_heap/heap/#_4","title":"\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\u4fee\u590d\u53d8\u5f97\u56f0\u96be\u3002\u4e3a\u4e86\u5c3d\u91cf\u51cf\u5c11\u5143\u7d20\u7d22\u5f15\u7684\u53d8\u52a8\uff0c\u6211\u4eec\u91c7\u53d6\u4ee5\u4e0b\u64cd\u4f5c\u6b65\u9aa4\uff1a

    1. \u4ea4\u6362\u5806\u9876\u5143\u7d20\u4e0e\u5806\u5e95\u5143\u7d20\uff08\u5373\u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff09\uff1b
    2. \u4ea4\u6362\u5b8c\u6210\u540e\uff0c\u5c06\u5806\u5e95\u4ece\u5217\u8868\u4e2d\u5220\u9664\uff08\u6ce8\u610f\uff0c\u7531\u4e8e\u5df2\u7ecf\u4ea4\u6362\uff0c\u5b9e\u9645\u4e0a\u5220\u9664\u7684\u662f\u539f\u6765\u7684\u5806\u9876\u5143\u7d20\uff09\uff1b
    3. \u4ece\u6839\u8282\u70b9\u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u6267\u884c\u5806\u5316\uff1b

    \u987e\u540d\u601d\u4e49\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u7684\u64cd\u4f5c\u65b9\u5411\u4e0e\u4ece\u5e95\u81f3\u9876\u5806\u5316\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\uff1b\u7136\u540e\u5faa\u73af\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u76f4\u5230\u8d8a\u8fc7\u53f6\u8282\u70b9\u6216\u9047\u5230\u65e0\u9700\u4ea4\u6362\u7684\u8282\u70b9\u65f6\u7ed3\u675f\u3002

    <1><2><3><4><5><6><7><8><9><10>

    \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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart my_heap.java
    /* \u5143\u7d20\u51fa\u5806 */\nint pop() {\n// \u5224\u7a7a\u5904\u7406\nif (isEmpty())\nthrow new IndexOutOfBoundsException();\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nswap(0, size() - 1);\n// \u5220\u9664\u8282\u70b9\nint val = maxHeap.remove(size() - 1);\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nsiftDown(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nint l = left(i), r = right(i), ma = i;\nif (l < size() && maxHeap.get(l) > maxHeap.get(ma))\nma = l;\nif (r < size() && maxHeap.get(r) > maxHeap.get(ma))\nma = r;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i)\nbreak;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(i, ma);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n
    my_heap.cpp
    /* \u5143\u7d20\u51fa\u5806 */\nvoid pop() {\n// \u5224\u7a7a\u5904\u7406\nif (empty()) {\nthrow out_of_range(\"\u5806\u4e3a\u7a7a\");\n}\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nswap(maxHeap[0], maxHeap[size() - 1]);\n// \u5220\u9664\u8282\u70b9\nmaxHeap.pop_back();\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nsiftDown(0);\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nint l = left(i), r = right(i), ma = i;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (l < size() && maxHeap[l] > maxHeap[ma])\nma = l;\nif (r < size() && maxHeap[r] > maxHeap[ma])\nma = r;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i)\nbreak;\nswap(maxHeap[i], maxHeap[ma]);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n
    my_heap.py
    def pop(self) -> int:\n\"\"\"\u5143\u7d20\u51fa\u5806\"\"\"\n# \u5224\u7a7a\u5904\u7406\nif self.is_empty():\nraise IndexError(\"\u5806\u4e3a\u7a7a\")\n# \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nself.swap(0, self.size() - 1)\n# \u5220\u9664\u8282\u70b9\nval = self.max_heap.pop()\n# \u4ece\u9876\u81f3\u5e95\u5806\u5316\nself.sift_down(0)\n# \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val\ndef sift_down(self, i: int):\n\"\"\"\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\"\"\"\nwhile True:\n# \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nl, r, ma = self.left(i), self.right(i), i\nif l < self.size() and self.max_heap[l] > self.max_heap[ma]:\nma = l\nif r < self.size() and self.max_heap[r] > self.max_heap[ma]:\nma = r\n# \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif ma == i:\nbreak\n# \u4ea4\u6362\u4e24\u8282\u70b9\nself.swap(i, ma)\n# \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma\n
    my_heap.go
    /* \u5143\u7d20\u51fa\u5806 */\nfunc (h *maxHeap) pop() any {\n// \u5224\u7a7a\u5904\u7406\nif h.isEmpty() {\nfmt.Println(\"error\")\nreturn nil\n}\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nh.swap(0, h.size()-1)\n// \u5220\u9664\u8282\u70b9\nval := h.data[len(h.data)-1]\nh.data = h.data[:len(h.data)-1]\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nh.siftDown(0)\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc (h *maxHeap) siftDown(i int) {\nfor true {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a max\nl, r, max := h.left(i), h.right(i), i\nif l < h.size() && h.data[l].(int) > h.data[max].(int) {\nmax = l\n}\nif r < h.size() && h.data[r].(int) > h.data[max].(int) {\nmax = r\n}\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif max == i {\nbreak\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nh.swap(i, max)\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = max\n}\n}\n
    my_heap.js
    /* \u5143\u7d20\u51fa\u5806 */\npop() {\n// \u5224\u7a7a\u5904\u7406\nif (this.isEmpty()) throw new Error('\u5806\u4e3a\u7a7a');\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nthis.#swap(0, this.size() - 1);\n// \u5220\u9664\u8282\u70b9\nconst val = this.#maxHeap.pop();\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nthis.#siftDown(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\n#siftDown(i) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nconst l = this.#left(i),\nr = this.#right(i);\nlet ma = i;\nif (l < this.size() && this.#maxHeap[l] > this.#maxHeap[ma]) ma = l;\nif (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\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nthis.#swap(i, ma);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n
    my_heap.ts
    /* \u5143\u7d20\u51fa\u5806 */\npop(): number {\n// \u5224\u7a7a\u5904\u7406\nif (this.isEmpty()) throw new RangeError('Heap is empty.');\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nthis.swap(0, this.size() - 1);\n// \u5220\u9664\u8282\u70b9\nconst val = this.maxHeap.pop();\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nthis.siftDown(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nsiftDown(i: number): void {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nconst l = this.left(i),\nr = this.right(i);\nlet ma = i;\nif (l < this.size() && this.maxHeap[l] > this.maxHeap[ma]) ma = l;\nif (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\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nthis.swap(i, ma);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n
    my_heap.c
    /* \u5143\u7d20\u51fa\u5806 */\nint pop(maxHeap *h) {\n// \u5224\u7a7a\u5904\u7406\nif (isEmpty(h)) {\nprintf(\"heap is empty!\");\nreturn INT_MAX;\n}\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nswap(h, 0, size(h) - 1);\n// \u5220\u9664\u8282\u70b9\nint val = h->data[h->size - 1];\nh->size--;\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nsiftDown(h, 0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(maxHeap *h, int i) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a max\nint l = left(h, i);\nint r = right(h, i);\nint max = i;\nif (l < size(h) && h->data[l] > h->data[max]) {\nmax = l;\n}\nif (r < size(h) && h->data[r] > h->data[max]) {\nmax = r;\n}\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (max == i) {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(h, i, max);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = max;\n}\n}\n
    my_heap.cs
    /* \u5143\u7d20\u51fa\u5806 */\nint pop() {\n// \u5224\u7a7a\u5904\u7406\nif (isEmpty())\nthrow new IndexOutOfRangeException();\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nswap(0, size() - 1);\n// \u5220\u9664\u8282\u70b9\nint val = maxHeap.Last();\nmaxHeap.RemoveAt(size() - 1);\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nsiftDown(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nint l = left(i), r = right(i), ma = i;\nif (l < size() && maxHeap[l] > maxHeap[ma])\nma = l;\nif (r < size() && maxHeap[r] > maxHeap[ma])\nma = r;\n// \u82e5\u201c\u8282\u70b9 i \u6700\u5927\u201d\u6216\u201c\u8d8a\u8fc7\u53f6\u8282\u70b9\u201d\uff0c\u5219\u7ed3\u675f\u5806\u5316\nif (ma == i) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(i, ma);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n
    my_heap.swift
    /* \u5143\u7d20\u51fa\u5806 */\nfunc pop() -> Int {\n// \u5224\u7a7a\u5904\u7406\nif isEmpty() {\nfatalError(\"\u5806\u4e3a\u7a7a\")\n}\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nswap(i: 0, j: size() - 1)\n// \u5220\u9664\u8282\u70b9\nlet val = maxHeap.remove(at: size() - 1)\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nsiftDown(i: 0)\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc siftDown(i: Int) {\nvar i = i\nwhile true {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nlet l = left(i: i)\nlet r = right(i: i)\nvar ma = i\nif l < size(), maxHeap[l] > maxHeap[ma] {\nma = l\n}\nif r < size(), maxHeap[r] > maxHeap[ma] {\nma = r\n}\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif ma == i {\nbreak\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(i: i, j: ma)\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma\n}\n}\n
    my_heap.zig
    // \u5143\u7d20\u51fa\u5806\nfn pop(self: *Self) !T {\n// \u5224\u65ad\u5904\u7406\nif (self.isEmpty()) unreachable;\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\ntry self.swap(0, self.size() - 1);\n// \u5220\u9664\u8282\u70b9\nvar val = self.max_heap.?.pop();\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\ntry self.siftDown(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n} // \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\nfn siftDown(self: *Self, i_: usize) !void {\nvar i = i_;\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nvar l = left(i);\nvar r = right(i);\nvar ma = i;\nif (l < self.size() and self.max_heap.?.items[l] > self.max_heap.?.items[ma]) ma = l;\nif (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\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\ntry self.swap(i, ma);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n
    my_heap.dart
    /* \u5143\u7d20\u51fa\u5806 */\nint pop() {\n// \u5224\u7a7a\u5904\u7406\nif (isEmpty()) throw Exception('\u5806\u4e3a\u7a7a');\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n_swap(0, size() - 1);\n// \u5220\u9664\u8282\u70b9\nint val = _maxHeap.removeLast();\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\n_siftDown(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n}\n[class]{MaxHeap}-[func]{siftDown}\n
    "},{"location":"chapter_heap/heap/#813","title":"8.1.3. \u00a0 \u5806\u5e38\u89c1\u5e94\u7528","text":"
    • \u4f18\u5148\u961f\u5217\uff1a\u5806\u901a\u5e38\u4f5c\u4e3a\u5b9e\u73b0\u4f18\u5148\u961f\u5217\u7684\u9996\u9009\u6570\u636e\u7ed3\u6784\uff0c\u5176\u5165\u961f\u548c\u51fa\u961f\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(\\log n)\\) \uff0c\u800c\u5efa\u961f\u64cd\u4f5c\u4e3a \\(O(n)\\) \uff0c\u8fd9\u4e9b\u64cd\u4f5c\u90fd\u975e\u5e38\u9ad8\u6548\u3002
    • \u5806\u6392\u5e8f\uff1a\u7ed9\u5b9a\u4e00\u7ec4\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u5b83\u4eec\u5efa\u7acb\u4e00\u4e2a\u5806\uff0c\u7136\u540e\u4e0d\u65ad\u5730\u6267\u884c\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\uff0c\u4ece\u800c\u5f97\u5230\u6709\u5e8f\u6570\u636e\u3002\u7136\u800c\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u4f7f\u7528\u4e00\u79cd\u66f4\u4f18\u96c5\u7684\u65b9\u5f0f\u5b9e\u73b0\u5806\u6392\u5e8f\uff0c\u8be6\u89c1\u540e\u7eed\u7684\u5806\u6392\u5e8f\u7ae0\u8282\u3002
    • \u83b7\u53d6\u6700\u5927\u7684 \\(k\\) \u4e2a\u5143\u7d20\uff1a\u8fd9\u662f\u4e00\u4e2a\u7ecf\u5178\u7684\u7b97\u6cd5\u95ee\u9898\uff0c\u540c\u65f6\u4e5f\u662f\u4e00\u79cd\u5178\u578b\u5e94\u7528\uff0c\u4f8b\u5982\u9009\u62e9\u70ed\u5ea6\u524d 10 \u7684\u65b0\u95fb\u4f5c\u4e3a\u5fae\u535a\u70ed\u641c\uff0c\u9009\u53d6\u9500\u91cf\u524d 10 \u7684\u5546\u54c1\u7b49\u3002
    "},{"location":"chapter_heap/summary/","title":"8.4. \u00a0 \u5c0f\u7ed3","text":"
    • \u5806\u662f\u4e00\u68f5\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u6839\u636e\u6210\u7acb\u6761\u4ef6\u53ef\u5206\u4e3a\u5927\u9876\u5806\u548c\u5c0f\u9876\u5806\u3002\u5927\uff08\u5c0f\uff09\u9876\u5806\u7684\u5806\u9876\u5143\u7d20\u662f\u6700\u5927\uff08\u5c0f\uff09\u7684\u3002
    • \u4f18\u5148\u961f\u5217\u7684\u5b9a\u4e49\u662f\u5177\u6709\u51fa\u961f\u4f18\u5148\u7ea7\u7684\u961f\u5217\uff0c\u901a\u5e38\u4f7f\u7528\u5806\u6765\u5b9e\u73b0\u3002
    • \u5806\u7684\u5e38\u7528\u64cd\u4f5c\u53ca\u5176\u5bf9\u5e94\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5305\u62ec\uff1a\u5143\u7d20\u5165\u5806 \\(O(\\log n)\\) \u3001\u5806\u9876\u5143\u7d20\u51fa\u5806 \\(O(\\log n)\\) \u548c\u8bbf\u95ee\u5806\u9876\u5143\u7d20 \\(O(1)\\) \u7b49\u3002
    • \u5b8c\u5168\u4e8c\u53c9\u6811\u975e\u5e38\u9002\u5408\u7528\u6570\u7ec4\u8868\u793a\uff0c\u56e0\u6b64\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u6570\u7ec4\u6765\u5b58\u50a8\u5806\u3002
    • \u5806\u5316\u64cd\u4f5c\u7528\u4e8e\u7ef4\u62a4\u5806\u7684\u6027\u8d28\uff0c\u5728\u5165\u5806\u548c\u51fa\u5806\u64cd\u4f5c\u4e2d\u90fd\u4f1a\u7528\u5230\u3002
    • \u8f93\u5165 \\(n\\) \u4e2a\u5143\u7d20\u5e76\u5efa\u5806\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u4f18\u5316\u81f3 \\(O(n)\\) \uff0c\u975e\u5e38\u9ad8\u6548\u3002
    "},{"location":"chapter_heap/top_k/","title":"8.3. \u00a0 Top-K \u95ee\u9898","text":"

    Question

    \u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \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 \\(k\\) \u8f6e\u904d\u5386\uff0c\u5206\u522b\u5728\u6bcf\u8f6e\u4e2d\u63d0\u53d6\u7b2c \\(1\\) , \\(2\\) , \\(\\cdots\\) , \\(k\\) \u5927\u7684\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nk)\\) \u3002

    \u8be5\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

    Fig. \u904d\u5386\u5bfb\u627e\u6700\u5927\u7684 k \u4e2a\u5143\u7d20

    Tip

    \u5f53 \\(k = n\\) \u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\uff0c\u7b49\u4ef7\u4e8e\u300c\u9009\u62e9\u6392\u5e8f\u300d\u7b97\u6cd5\u3002

    "},{"location":"chapter_heap/top_k/#832","title":"8.3.2. \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u6392\u5e8f","text":"

    \u6211\u4eec\u53ef\u4ee5\u5bf9\u6570\u7ec4 nums \u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\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\u8981\u627e\u51fa\u6700\u5927\u7684 \\(k\\) \u4e2a\u5143\u7d20\u5373\u53ef\uff0c\u800c\u4e0d\u9700\u8981\u6392\u5e8f\u5176\u4ed6\u5143\u7d20\u3002

    Fig. \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\u4e0b\uff1a

    1. \u521d\u59cb\u5316\u4e00\u4e2a\u5c0f\u9876\u5806\uff0c\u5176\u5806\u9876\u5143\u7d20\u6700\u5c0f\uff1b
    2. \u5148\u5c06\u6570\u7ec4\u7684\u524d \\(k\\) \u4e2a\u5143\u7d20\u4f9d\u6b21\u5165\u5806\uff1b
    3. \u4ece\u7b2c \\(k + 1\\) \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\uff0c\u5e76\u5c06\u5f53\u524d\u5143\u7d20\u5165\u5806\uff1b
    4. \u904d\u5386\u5b8c\u6210\u540e\uff0c\u5806\u4e2d\u4fdd\u5b58\u7684\u5c31\u662f\u6700\u5927\u7684 \\(k\\) \u4e2a\u5143\u7d20\uff1b
    <1><2><3><4><5><6><7><8><9>

    \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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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) {\nQueue<Integer> heap = new PriorityQueue<Integer>();\n// \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nfor (int i = 0; i < k; i++) {\nheap.add(nums[i]);\n}\n// \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\nfor (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\nif (nums[i] > heap.peek()) {\nheap.poll();\nheap.add(nums[i]);\n}\n}\nreturn heap;\n}\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) {\npriority_queue<int, vector<int>, greater<int>> heap;\n// \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nfor (int i = 0; i < k; i++) {\nheap.push(nums[i]);\n}\n// \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\nfor (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\nif (nums[i] > heap.top()) {\nheap.pop();\nheap.push(nums[i]);\n}\n}\nreturn heap;\n}\n
    top_k.py
    def 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\"\"\"\nheap = []\n# \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nfor i in range(k):\nheapq.heappush(heap, nums[i])\n# \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\nfor 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\nif nums[i] > heap[0]:\nheapq.heappop(heap)\nheapq.heappush(heap, nums[i])\nreturn heap\n
    top_k.go
    [class]{maxHeap}-[func]{topKHeap}\n
    top_k.js
    [class]{}-[func]{topKHeap}\n
    top_k.ts
    [class]{}-[func]{topKHeap}\n
    top_k.c
    [class]{maxHeap}-[func]{topKHeap}\n
    top_k.cs
    [class]{top_k}-[func]{topKHeap}\n
    top_k.swift
    [class]{}-[func]{topKHeap}\n
    top_k.zig
    [class]{}-[func]{topKHeap}\n
    top_k.dart
    [class]{}-[func]{top_k_heap}\n
    "},{"location":"chapter_introduction/","title":"1. \u00a0 \u5f15\u8a00","text":""},{"location":"chapter_introduction/algorithms_are_everywhere/","title":"1.1. \u00a0 \u7b97\u6cd5\u65e0\u5904\u4e0d\u5728","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\u4e8e\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\u5b9e\u9645\u4e0a\uff0c\u4f60\u5df2\u7ecf\u5b66\u4f1a\u4e86\u8bb8\u591a\u7b97\u6cd5\uff0c\u5e76\u4e60\u60ef\u5c06\u4ed6\u4eec\u5e94\u7528\u5230\u65e5\u5e38\u751f\u6d3b\u4e2d\u4e86\u3002\u4e0b\u9762\uff0c\u6211\u5c06\u4e3e\u4e24\u4e2a\u5177\u4f53\u4f8b\u5b50\u6765\u8bc1\u5b9e\u8fd9\u4e00\u70b9\u3002

    \u4f8b\u4e00\uff1a\u7ec4\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

    \u4ece\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u79ef\u6728\u7684\u5404\u79cd\u5f62\u72b6\u548c\u8fde\u63a5\u65b9\u5f0f\u4ee3\u8868\u6570\u636e\u7ed3\u6784\uff0c\u800c\u7ec4\u88c5\u8bf4\u660e\u4e66\u4e0a\u7684\u4e00\u7cfb\u5217\u6b65\u9aa4\u5219\u662f\u7b97\u6cd5\u3002

    Fig. \u62fc\u88c5\u79ef\u6728

    \u4f8b\u4e8c\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\u7684\u82f1\u6587\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\u8fd9\u6837\u64cd\u4f5c\uff1a

    1. \u7ffb\u5f00\u5b57\u5178\u7ea6\u4e00\u534a\u7684\u9875\u6570\uff0c\u67e5\u770b\u8be5\u9875\u9996\u5b57\u6bcd\u662f\u4ec0\u4e48\uff08\u5047\u8bbe\u4e3a \\(m\\) \uff09\uff1b
    2. \u7531\u4e8e\u5728\u82f1\u6587\u5b57\u6bcd\u8868\u4e2d \\(r\\) \u4f4d\u4e8e \\(m\\) \u4e4b\u540e\uff0c\u6240\u4ee5\u6392\u9664\u5b57\u5178\u524d\u534a\u90e8\u5206\uff0c\u67e5\u627e\u8303\u56f4\u7f29\u5c0f\u5230\u540e\u534a\u90e8\u5206\uff1b
    3. \u4e0d\u65ad\u91cd\u590d\u6b65\u9aa4 1-2 \uff0c\u76f4\u81f3\u627e\u5230\u62fc\u97f3\u9996\u5b57\u6bcd\u4e3a \\(r\\) \u7684\u9875\u7801\u4e3a\u6b62\u3002
    <1><2><3><4><5>

    \u67e5\u9605\u5b57\u5178\u8fd9\u4e2a\u5c0f\u5b66\u751f\u5fc5\u5907\u6280\u80fd\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u8457\u540d\u7684\u300c\u4e8c\u5206\u67e5\u627e\u300d\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\u300c\u6570\u7ec4\u300d\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\u662f\u300c\u4e8c\u5206\u67e5\u627e\u300d\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\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

    \u9605\u8bfb\u81f3\u6b64\uff0c\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\u90a3\u4e48\u592a\u597d\u4e86\uff01\u56e0\u4e3a\u8fd9\u6b63\u662f\u672c\u4e66\u5b58\u5728\u7684\u610f\u4e49\u3002\u63a5\u4e0b\u6765\uff0c\u8fd9\u672c\u4e66\u5c06\u4e00\u6b65\u6b65\u5f15\u5bfc\u4f60\u6df1\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":"
    • \u7b97\u6cd5\u5728\u65e5\u5e38\u751f\u6d3b\u4e2d\u65e0\u5904\u4e0d\u5728\uff0c\u5e76\u4e0d\u662f\u9065\u4e0d\u53ef\u53ca\u7684\u9ad8\u6df1\u77e5\u8bc6\u3002\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u5df2\u7ecf\u5728\u4e0d\u77e5\u4e0d\u89c9\u4e2d\u5b66\u4e60\u4e86\u8bb8\u591a\u201c\u7b97\u6cd5\u201d\uff0c\u7528\u4ee5\u89e3\u51b3\u751f\u6d3b\u4e2d\u7684\u5927\u5c0f\u95ee\u9898\u3002
    • \u67e5\u9605\u5b57\u5178\u7684\u539f\u7406\u4e0e\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u76f8\u4e00\u81f4\u3002\u4e8c\u5206\u67e5\u627e\u4f53\u73b0\u4e86\u5206\u800c\u6cbb\u4e4b\u7684\u91cd\u8981\u7b97\u6cd5\u601d\u60f3\u3002
    • \u7b97\u6cd5\u662f\u5728\u6709\u9650\u65f6\u95f4\u5185\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\u7684\u4e00\u7ec4\u6307\u4ee4\u6216\u64cd\u4f5c\u6b65\u9aa4\uff0c\u800c\u6570\u636e\u7ed3\u6784\u662f\u8ba1\u7b97\u673a\u4e2d\u7ec4\u7ec7\u548c\u5b58\u50a8\u6570\u636e\u7684\u65b9\u5f0f\u3002
    • \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7d27\u5bc6\u76f8\u8fde\u3002\u6570\u636e\u7ed3\u6784\u662f\u7b97\u6cd5\u7684\u57fa\u77f3\uff0c\u800c\u7b97\u6cd5\u5219\u662f\u53d1\u6325\u6570\u636e\u7ed3\u6784\u4f5c\u7528\u7684\u821e\u53f0\u3002
    • \u4e50\u9ad8\u79ef\u6728\u5bf9\u5e94\u4e8e\u6570\u636e\uff0c\u79ef\u6728\u5f62\u72b6\u548c\u8fde\u63a5\u65b9\u5f0f\u4ee3\u8868\u6570\u636e\u7ed3\u6784\uff0c\u62fc\u88c5\u79ef\u6728\u7684\u6b65\u9aa4\u5219\u5bf9\u5e94\u7b97\u6cd5\u3002
    "},{"location":"chapter_introduction/what_is_dsa/","title":"1.2. \u00a0 \u7b97\u6cd5\u662f\u4ec0\u4e48","text":""},{"location":"chapter_introduction/what_is_dsa/#121","title":"1.2.1. \u00a0 \u7b97\u6cd5\u5b9a\u4e49","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\u3002\u7b97\u6cd5\u5177\u6709\u4ee5\u4e0b\u7279\u6027\uff1a

    • \u95ee\u9898\u662f\u660e\u786e\u7684\uff0c\u5177\u6709\u6e05\u6670\u7684\u8f93\u5165\u548c\u8f93\u51fa\u5b9a\u4e49\u3002
    • \u89e3\u5177\u6709\u786e\u5b9a\u6027\uff0c\u5373\u7ed9\u5b9a\u76f8\u540c\u7684\u8f93\u5165\u65f6\uff0c\u8f93\u51fa\u59cb\u7ec8\u76f8\u540c\u3002
    • \u5177\u6709\u53ef\u884c\u6027\uff0c\u5728\u6709\u9650\u6b65\u9aa4\u3001\u65f6\u95f4\u548c\u5185\u5b58\u7a7a\u95f4\u4e0b\u53ef\u5b8c\u6210\u3002
    "},{"location":"chapter_introduction/what_is_dsa/#122","title":"1.2.2. \u00a0 \u6570\u636e\u7ed3\u6784\u5b9a\u4e49","text":"

    \u300c\u6570\u636e\u7ed3\u6784 Data Structure\u300d\u662f\u8ba1\u7b97\u673a\u4e2d\u7ec4\u7ec7\u548c\u5b58\u50a8\u6570\u636e\u7684\u65b9\u5f0f\u3002\u4e3a\u4e86\u63d0\u9ad8\u6570\u636e\u5b58\u50a8\u548c\u64cd\u4f5c\u6027\u80fd\uff0c\u6570\u636e\u7ed3\u6784\u7684\u8bbe\u8ba1\u76ee\u6807\u5305\u62ec\uff1a

    • \u7a7a\u95f4\u5360\u7528\u5c3d\u91cf\u51cf\u5c11\uff0c\u8282\u7701\u8ba1\u7b97\u673a\u5185\u5b58\u3002
    • \u6570\u636e\u64cd\u4f5c\u5c3d\u53ef\u80fd\u5feb\u901f\uff0c\u6db5\u76d6\u6570\u636e\u8bbf\u95ee\u3001\u6dfb\u52a0\u3001\u5220\u9664\u3001\u66f4\u65b0\u7b49\u3002
    • \u63d0\u4f9b\u7b80\u6d01\u7684\u6570\u636e\u8868\u793a\u548c\u903b\u8f91\u4fe1\u606f\uff0c\u4ee5\u5229\u4e8e\u7b97\u6cd5\u9ad8\u6548\u8fd0\u884c\u3002

    \u6570\u636e\u7ed3\u6784\u8bbe\u8ba1\u662f\u4e00\u4e2a\u5145\u6ee1\u6743\u8861\u7684\u8fc7\u7a0b\uff0c\u8fd9\u610f\u5473\u7740\u8981\u5728\u67d0\u65b9\u9762\u53d6\u5f97\u4f18\u52bf\uff0c\u5f80\u5f80\u9700\u8981\u5728\u53e6\u4e00\u65b9\u9762\u4f5c\u51fa\u59a5\u534f\u3002\u4f8b\u5982\uff0c\u94fe\u8868\u76f8\u8f83\u4e8e\u6570\u7ec4\uff0c\u5728\u6570\u636e\u6dfb\u52a0\u548c\u5220\u9664\u64cd\u4f5c\u4e0a\u66f4\u52a0\u4fbf\u6377\uff0c\u4f46\u727a\u7272\u4e86\u6570\u636e\u8bbf\u95ee\u901f\u5ea6\uff1b\u56fe\u76f8\u8f83\u4e8e\u94fe\u8868\uff0c\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684\u903b\u8f91\u4fe1\u606f\uff0c\u4f46\u9700\u8981\u5360\u7528\u66f4\u5927\u7684\u5185\u5b58\u7a7a\u95f4\u3002

    "},{"location":"chapter_introduction/what_is_dsa/#123","title":"1.2.3. \u00a0 \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u5173\u7cfb","text":"

    \u300c\u6570\u636e\u7ed3\u6784\u300d\u4e0e\u300c\u7b97\u6cd5\u300d\u9ad8\u5ea6\u76f8\u5173\u4e14\u7d27\u5bc6\u7ed3\u5408\uff0c\u5177\u4f53\u8868\u73b0\u5728\uff1a

    • \u6570\u636e\u7ed3\u6784\u662f\u7b97\u6cd5\u7684\u57fa\u77f3\u3002\u6570\u636e\u7ed3\u6784\u4e3a\u7b97\u6cd5\u63d0\u4f9b\u4e86\u7ed3\u6784\u5316\u5b58\u50a8\u7684\u6570\u636e\uff0c\u4ee5\u53ca\u7528\u4e8e\u64cd\u4f5c\u6570\u636e\u7684\u65b9\u6cd5\u3002
    • \u7b97\u6cd5\u662f\u6570\u636e\u7ed3\u6784\u53d1\u6325\u7684\u821e\u53f0\u3002\u6570\u636e\u7ed3\u6784\u672c\u8eab\u4ec5\u5b58\u50a8\u6570\u636e\u4fe1\u606f\uff0c\u901a\u8fc7\u7ed3\u5408\u7b97\u6cd5\u624d\u80fd\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\u3002
    • \u7279\u5b9a\u7b97\u6cd5\u901a\u5e38\u6709\u5bf9\u5e94\u6700\u4f18\u7684\u6570\u636e\u7ed3\u6784\u3002\u7b97\u6cd5\u901a\u5e38\u53ef\u4ee5\u57fa\u4e8e\u4e0d\u540c\u7684\u6570\u636e\u7ed3\u6784\u8fdb\u884c\u5b9e\u73b0\uff0c\u4f46\u6700\u7ec8\u6267\u884c\u6548\u7387\u53ef\u80fd\u76f8\u5dee\u5f88\u5927\u3002

    Fig. \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u5173\u7cfb

    \u7c7b\u6bd4\u300cLEGO \u4e50\u9ad8\u300d\u548c\u300c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u300d\uff0c\u5219\u5bf9\u5e94\u5173\u7cfb\u5982\u4e0b\u8868\u6240\u793a\u3002

    \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5 LEGO \u4e50\u9ad8 \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\u6ce8\u610f\u7684\u662f\uff0c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u72ec\u7acb\u4e8e\u7f16\u7a0b\u8bed\u8a00\u3002\u6b63\u56e0\u5982\u6b64\uff0c\u672c\u4e66\u5f97\u4ee5\u63d0\u4f9b\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\u300c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u300d\u7b80\u79f0\u4e3a\u300c\u7b97\u6cd5\u300d\u3002\u4f8b\u5982\uff0c\u4f17\u6240\u5468\u77e5\u7684 LeetCode \u7b97\u6cd5\u9898\u76ee\uff0c\u5b9e\u9645\u4e0a\u540c\u65f6\u8003\u5bdf\u4e86\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u4e24\u65b9\u9762\u7684\u77e5\u8bc6\u3002

    "},{"location":"chapter_preface/","title":"0. \u00a0 \u5199\u5728\u524d\u9762","text":""},{"location":"chapter_preface/about_the_book/","title":"0.1. \u00a0 \u5173\u4e8e\u672c\u4e66","text":"

    \u672c\u9879\u76ee\u65e8\u5728\u521b\u5efa\u4e00\u672c\u5f00\u6e90\u514d\u8d39\u3001\u65b0\u624b\u53cb\u597d\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u5165\u95e8\u6559\u7a0b\u3002

    • \u5168\u4e66\u91c7\u7528\u52a8\u753b\u56fe\u89e3\uff0c\u7ed3\u6784\u5316\u5730\u8bb2\u89e3\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u77e5\u8bc6\uff0c\u5185\u5bb9\u6e05\u6670\u6613\u61c2\u3001\u5b66\u4e60\u66f2\u7ebf\u5e73\u6ed1\uff1b
    • \u7b97\u6cd5\u6e90\u4ee3\u7801\u7686\u53ef\u4e00\u952e\u8fd0\u884c\uff0c\u652f\u6301 Java, C++, Python, Go, JS, TS, C#, Swift, Zig \u7b49\u8bed\u8a00\uff1b
    • \u9f13\u52b1\u8bfb\u8005\u5728\u7ae0\u8282\u8ba8\u8bba\u533a\u4e92\u5e2e\u4e92\u52a9\u3001\u5171\u540c\u8fdb\u6b65\uff0c\u63d0\u95ee\u4e0e\u8bc4\u8bba\u901a\u5e38\u53ef\u5728\u4e24\u65e5\u5185\u5f97\u5230\u56de\u590d\uff1b
    "},{"location":"chapter_preface/about_the_book/#011","title":"0.1.1. \u00a0 \u8bfb\u8005\u5bf9\u8c61","text":"

    \u82e5\u60a8\u662f\u300c\u7b97\u6cd5\u521d\u5b66\u8005\u300d\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\u8fd9\u672c\u4e66\u6b63\u662f\u4e3a\u60a8\u91cf\u8eab\u5b9a\u5236\uff01

    \u5982\u679c\u60a8\u662f\u300c\u7b97\u6cd5\u8001\u624b\u300d\uff0c\u5df2\u7ecf\u79ef\u7d2f\u4e00\u5b9a\u5237\u9898\u91cf\uff0c\u719f\u6089\u5927\u90e8\u5206\u9898\u578b\uff0c\u90a3\u4e48\u672c\u4e66\u53ef\u52a9\u60a8\u56de\u987e\u4e0e\u68b3\u7406\u7b97\u6cd5\u77e5\u8bc6\u4f53\u7cfb\uff0c\u4ed3\u5e93\u6e90\u4ee3\u7801\u53ef\u4ee5\u88ab\u5f53\u4f5c\u201c\u5237\u9898\u5de5\u5177\u5e93\u201d\u6216\u201c\u7b97\u6cd5\u5b57\u5178\u201d\u6765\u4f7f\u7528\u3002

    \u82e5\u60a8\u662f\u300c\u7b97\u6cd5\u4e13\u5bb6\u300d\uff0c\u6211\u4eec\u671f\u5f85\u6536\u5230\u60a8\u7684\u5b9d\u8d35\u5efa\u8bae\uff0c\u6216\u8005\u4e00\u8d77\u53c2\u4e0e\u521b\u4f5c\u3002

    \u524d\u7f6e\u6761\u4ef6

    \u60a8\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\u4e3b\u8981\u5185\u5bb9\u5305\u62ec\uff1a

    • \u590d\u6742\u5ea6\u5206\u6790\uff1a\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u8bc4\u4ef7\u7ef4\u5ea6\u3001\u7b97\u6cd5\u6548\u7387\u7684\u8bc4\u4f30\u65b9\u6cd5\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u3001\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u5305\u62ec\u63a8\u7b97\u65b9\u6cd5\u3001\u5e38\u89c1\u7c7b\u578b\u3001\u793a\u4f8b\u7b49\u3002
    • \u6570\u636e\u7ed3\u6784\uff1a\u5e38\u89c1\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u6570\u636e\u5728\u5185\u5b58\u4e2d\u7684\u5b58\u50a8\u5f62\u5f0f\u3001\u6570\u636e\u7ed3\u6784\u7684\u5206\u7c7b\u65b9\u6cd5\u3002\u6d89\u53ca\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u3001\u6563\u5217\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\u7b49\u6570\u636e\u7ed3\u6784\uff0c\u5185\u5bb9\u5305\u62ec\u5b9a\u4e49\u3001\u4f18\u7f3a\u70b9\u3001\u5e38\u7528\u64cd\u4f5c\u3001\u5e38\u89c1\u7c7b\u578b\u3001\u5178\u578b\u5e94\u7528\u3001\u5b9e\u73b0\u65b9\u6cd5\u7b49\u3002
    • \u7b97\u6cd5\uff1a\u67e5\u627e\u7b97\u6cd5\u3001\u6392\u5e8f\u7b97\u6cd5\u3001\u641c\u7d22\u4e0e\u56de\u6eaf\u3001\u52a8\u6001\u89c4\u5212\u3001\u5206\u6cbb\u7b97\u6cd5\u7b49\uff0c\u5185\u5bb9\u6db5\u76d6\u5b9a\u4e49\u3001\u5e94\u7528\u573a\u666f\u3001\u4f18\u7f3a\u70b9\u3001\u65f6\u7a7a\u6548\u7387\u3001\u5b9e\u73b0\u65b9\u6cd5\u3001\u793a\u4f8b\u9898\u76ee\u7b49\u3002

    Fig. Hello \u7b97\u6cd5\u5185\u5bb9\u7ed3\u6784

    "},{"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

    • \u611f\u8c22\u6211\u5728\u516c\u53f8\u7684\u5bfc\u5e08\u674e\u6c50\u535a\u58eb\uff0c\u5728\u6df1\u5165\u4ea4\u8c08\u4e2d\u60a8\u9f13\u52b1\u6211\u201c\u884c\u52a8\u8d77\u6765\u201d\uff0c\u575a\u5b9a\u4e86\u6211\u5199\u8fd9\u672c\u4e66\u7684\u51b3\u5fc3\u3002
    • \u611f\u8c22\u6211\u7684\u5973\u670b\u53cb\u6ce1\u6ce1\u4f5c\u4e3a\u672c\u4e66\u7684\u9996\u4f4d\u8bfb\u8005\uff0c\u4ece\u7b97\u6cd5\u5c0f\u767d\u7684\u89d2\u5ea6\u63d0\u51fa\u8bb8\u591a\u5b9d\u8d35\u5efa\u8bae\uff0c\u4f7f\u5f97\u672c\u4e66\u66f4\u9002\u5408\u65b0\u624b\u9605\u8bfb\u3002
    • \u611f\u8c22\u817e\u5b9d\u3001\u7426\u5b9d\u3001\u98de\u5b9d\u4e3a\u672c\u4e66\u8d77\u4e86\u4e00\u4e2a\u5bcc\u6709\u521b\u610f\u7684\u540d\u5b57\uff0c\u5524\u8d77\u5927\u5bb6\u5199\u4e0b\u7b2c\u4e00\u884c\u4ee3\u7801 \"Hello World!\" \u7684\u7f8e\u597d\u56de\u5fc6\u3002
    • \u611f\u8c22\u82cf\u6f7c\u4e3a\u672c\u4e66\u8bbe\u8ba1\u4e86\u7cbe\u7f8e\u7684\u5c01\u9762\u548c LOGO\uff0c\u5e76\u5728\u6211\u7684\u5f3a\u8feb\u75c7\u4e0b\u591a\u6b21\u8010\u5fc3\u4fee\u6539\u3002
    • \u611f\u8c22 @squidfunk \u63d0\u4f9b\u7684\u5199\u4f5c\u6392\u7248\u5efa\u8bae\uff0c\u4ee5\u53ca\u6770\u51fa\u7684\u5f00\u6e90\u9879\u76ee Material-for-MkDocs \u3002

    \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\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\u4e9b\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\u60a8\u901a\u8bfb\u672c\u8282\u5185\u5bb9\u3002

    "},{"location":"chapter_preface/suggestions/#021","title":"0.2.1. \u00a0 \u884c\u6587\u98ce\u683c\u7ea6\u5b9a","text":"

    \u6807\u9898\u540e\u6807\u6ce8 * \u7684\u662f\u9009\u8bfb\u7ae0\u8282\uff0c\u5185\u5bb9\u76f8\u5bf9\u56f0\u96be\u3002\u5982\u679c\u4f60\u7684\u65f6\u95f4\u6709\u9650\uff0c\u5efa\u8bae\u53ef\u4ee5\u5148\u8df3\u8fc7\u3002

    \u6587\u7ae0\u4e2d\u7684\u91cd\u8981\u540d\u8bcd\u4f1a\u7528 \u300c \u300d \u62ec\u53f7\u6807\u6ce8\uff0c\u4f8b\u5982 \u300c\u6570\u7ec4 Array\u300d \u3002\u8bf7\u52a1\u5fc5\u8bb0\u4f4f\u8fd9\u4e9b\u540d\u8bcd\uff0c\u5305\u62ec\u82f1\u6587\u7ffb\u8bd1\uff0c\u4ee5\u4fbf\u540e\u7eed\u9605\u8bfb\u6587\u732e\u65f6\u4f7f\u7528\u3002

    \u52a0\u7c97\u7684\u6587\u5b57 \u8868\u793a\u91cd\u70b9\u5185\u5bb9\u6216\u603b\u7ed3\u6027\u8bed\u53e5\uff0c\u8fd9\u7c7b\u6587\u5b57\u503c\u5f97\u7279\u522b\u5173\u6ce8\u3002

    \u4e13\u6709\u540d\u8bcd\u548c\u6709\u7279\u6307\u542b\u4e49\u7684\u8bcd\u53e5\u4f1a\u4f7f\u7528 \u201c\u53cc\u5f15\u53f7\u201d \u6807\u6ce8\uff0c\u4ee5\u907f\u514d\u6b67\u4e49\u3002

    \u672c\u4e66\u90e8\u5206\u653e\u5f03\u4e86\u7f16\u7a0b\u8bed\u8a00\u7684\u6ce8\u91ca\u89c4\u8303\uff0c\u4ee5\u6362\u53d6\u66f4\u52a0\u7d27\u51d1\u7684\u5185\u5bb9\u6392\u7248\u3002\u6ce8\u91ca\u4e3b\u8981\u5206\u4e3a\u4e09\u79cd\u7c7b\u578b\uff1a\u6807\u9898\u6ce8\u91ca\u3001\u5185\u5bb9\u6ce8\u91ca\u3001\u591a\u884c\u6ce8\u91ca\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\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// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\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# \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\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// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\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// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\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// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\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// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\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// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\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// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\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// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n// \u591a\u884c\n// \u6ce8\u91ca\n
    /* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\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\u56e0\u6b64\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

    \u5728\u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u5982\u679c\u53d1\u73b0\u67d0\u6bb5\u5185\u5bb9\u63d0\u4f9b\u4e86\u52a8\u753b\u6216\u56fe\u89e3\uff0c\u5efa\u8bae\u4ee5\u56fe\u4e3a\u4e3b\u7ebf\uff0c\u4ee5\u6587\u5b57\uff08\u901a\u5e38\u4f4d\u4e8e\u56fe\u50cf\u4e0a\u65b9\uff09\u4e3a\u8f85\uff0c\u7efc\u5408\u4e24\u8005\u6765\u7406\u89e3\u5185\u5bb9\u3002

    Fig. \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\u5728GitHub \u4ed3\u5e93\uff0c\u6e90\u4ee3\u7801\u5305\u542b\u8be6\u7ec6\u6ce8\u91ca\uff0c\u5e76\u9644\u6709\u6d4b\u8bd5\u6837\u4f8b\uff0c\u53ef\u76f4\u63a5\u8fd0\u884c\u3002

    \u5982\u679c\u5b66\u4e60\u65f6\u95f4\u6709\u9650\uff0c\u5efa\u8bae\u4f60\u81f3\u5c11\u901a\u8bfb\u5e76\u8fd0\u884c\u6240\u6709\u4ee3\u7801\u3002\u5982\u679c\u65f6\u95f4\u5145\u88d5\uff0c\u5efa\u8bae\u53c2\u7167\u4ee3\u7801\u81ea\u884c\u6572\u4e00\u904d\u3002\u4e0e\u4ec5\u9605\u8bfb\u4ee3\u7801\u76f8\u6bd4\uff0c\u7f16\u5199\u4ee3\u7801\u7684\u8fc7\u7a0b\u5f80\u5f80\u80fd\u5e26\u6765\u66f4\u591a\u6536\u83b7\u3002

    Fig. \u8fd0\u884c\u4ee3\u7801\u793a\u4f8b

    \u7b2c\u4e00\u6b65\uff1a\u5b89\u88c5\u672c\u5730\u7f16\u7a0b\u73af\u5883\u3002\u8bf7\u53c2\u7167\u9644\u5f55\u6559\u7a0b\u8fdb\u884c\u5b89\u88c5\uff0c\u5982\u679c\u5df2\u5b89\u88c5\u5219\u53ef\u8df3\u8fc7\u6b64\u6b65\u9aa4\u3002

    \u7b2c\u4e8c\u6b65\uff1a\u4e0b\u8f7d\u4ee3\u7801\u4ed3\u3002\u5982\u679c\u5df2\u7ecf\u5b89\u88c5 Git \uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u514b\u9686\u672c\u4ed3\u5e93\u3002

    git clone https://github.com/krahets/hello-algo.git\n

    \u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u70b9\u51fb\u201cDownload ZIP\u201d\u76f4\u63a5\u4e0b\u8f7d\u4ee3\u7801\u538b\u7f29\u5305\uff0c\u7136\u540e\u5728\u672c\u5730\u89e3\u538b\u5373\u53ef\u3002

    Fig. \u514b\u9686\u4ed3\u5e93\u4e0e\u4e0b\u8f7d\u4ee3\u7801

    \u7b2c\u4e09\u6b65\uff1a\u8fd0\u884c\u6e90\u4ee3\u7801\u3002\u5982\u679c\u4ee3\u7801\u5757\u9876\u90e8\u6807\u6709\u6587\u4ef6\u540d\u79f0\uff0c\u5219\u53ef\u4ee5\u5728\u4ed3\u5e93\u7684 codes \u6587\u4ef6\u5939\u4e2d\u627e\u5230\u76f8\u5e94\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u3002\u6e90\u4ee3\u7801\u6587\u4ef6\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

    Fig. \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":"

    \u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u8bf7\u4e0d\u8981\u201c\u60ef\u7740\u201d\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\u5176\u4ed6\u5c0f\u4f19\u4f34\u4eec\u5c06\u7aed\u8bda\u4e3a\u4f60\u89e3\u7b54\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u53ef\u5728\u4e24\u5929\u5185\u5f97\u5230\u56de\u590d\u3002

    \u540c\u65f6\uff0c\u4e5f\u5e0c\u671b\u60a8\u80fd\u5728\u8bc4\u8bba\u533a\u591a\u82b1\u4e9b\u65f6\u95f4\u3002\u4e00\u65b9\u9762\uff0c\u60a8\u53ef\u4ee5\u4e86\u89e3\u5927\u5bb6\u9047\u5230\u7684\u95ee\u9898\uff0c\u4ece\u800c\u67e5\u6f0f\u8865\u7f3a\uff0c\u8fd9\u5c06\u6709\u52a9\u4e8e\u6fc0\u53d1\u66f4\u6df1\u5165\u7684\u601d\u8003\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u5e0c\u671b\u60a8\u80fd\u6177\u6168\u5730\u56de\u7b54\u5176\u4ed6\u5c0f\u4f19\u4f34\u7684\u95ee\u9898\u3001\u5206\u4eab\u60a8\u7684\u89c1\u89e3\uff0c\u8ba9\u5927\u5bb6\u5171\u540c\u5b66\u4e60\u548c\u8fdb\u6b65\u3002

    Fig. \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\uff1a

    1. \u7b97\u6cd5\u5165\u95e8\u3002\u6211\u4eec\u9700\u8981\u719f\u6089\u5404\u79cd\u6570\u636e\u7ed3\u6784\u7684\u7279\u70b9\u548c\u7528\u6cd5\uff0c\u5b66\u4e60\u4e0d\u540c\u7b97\u6cd5\u7684\u539f\u7406\u3001\u6d41\u7a0b\u3001\u7528\u9014\u548c\u6548\u7387\u7b49\u65b9\u9762\u5185\u5bb9\u3002
    2. \u5237\u7b97\u6cd5\u9898\u3002\u5efa\u8bae\u4ece\u70ed\u95e8\u9898\u76ee\u5f00\u5237\uff0c\u5982\u5251\u6307 Offer\u548cLeetCode Hot 100\uff0c\u5148\u79ef\u7d2f\u81f3\u5c11 100 \u9053\u9898\u76ee\uff0c\u719f\u6089\u4e3b\u6d41\u7684\u7b97\u6cd5\u95ee\u9898\u3002\u521d\u6b21\u5237\u9898\u65f6\uff0c\u201c\u77e5\u8bc6\u9057\u5fd8\u201d\u53ef\u80fd\u662f\u4e00\u4e2a\u6311\u6218\uff0c\u4f46\u8bf7\u653e\u5fc3\uff0c\u8fd9\u662f\u5f88\u6b63\u5e38\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u6309\u7167\u201c\u827e\u5bbe\u6d69\u65af\u9057\u5fd8\u66f2\u7ebf\u201d\u6765\u590d\u4e60\u9898\u76ee\uff0c\u901a\u5e38\u5728\u8fdb\u884c 3-5 \u8f6e\u7684\u91cd\u590d\u540e\uff0c\u5c31\u80fd\u5c06\u5176\u7262\u8bb0\u5728\u5fc3\u3002
    3. \u642d\u5efa\u77e5\u8bc6\u4f53\u7cfb\u3002\u5728\u5b66\u4e60\u65b9\u9762\uff0c\u6211\u4eec\u53ef\u4ee5\u9605\u8bfb\u7b97\u6cd5\u4e13\u680f\u6587\u7ae0\u3001\u89e3\u9898\u6846\u67b6\u548c\u7b97\u6cd5\u6559\u6750\uff0c\u4ee5\u4e0d\u65ad\u4e30\u5bcc\u77e5\u8bc6\u4f53\u7cfb\u3002\u5728\u5237\u9898\u65b9\u9762\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u91c7\u7528\u8fdb\u9636\u5237\u9898\u7b56\u7565\uff0c\u5982\u6309\u4e13\u9898\u5206\u7c7b\u3001\u4e00\u9898\u591a\u89e3\u3001\u4e00\u89e3\u591a\u9898\u7b49\uff0c\u76f8\u5173\u7684\u5237\u9898\u5fc3\u5f97\u53ef\u4ee5\u5728\u5404\u4e2a\u793e\u533a\u627e\u5230\u3002

    \u4f5c\u4e3a\u4e00\u672c\u5165\u95e8\u6559\u7a0b\uff0c\u672c\u4e66\u5185\u5bb9\u4e3b\u8981\u6db5\u76d6\u201c\u7b2c\u4e00\u9636\u6bb5\u201d\uff0c\u65e8\u5728\u5e2e\u52a9\u4f60\u66f4\u9ad8\u6548\u5730\u5c55\u5f00\u7b2c\u4e8c\u548c\u7b2c\u4e09\u9636\u6bb5\u7684\u5b66\u4e60\u3002

    Fig. \u7b97\u6cd5\u5b66\u4e60\u8def\u7ebf

    "},{"location":"chapter_preface/summary/","title":"0.3. \u00a0 \u5c0f\u7ed3","text":"
    • \u672c\u4e66\u7684\u4e3b\u8981\u53d7\u4f17\u662f\u7b97\u6cd5\u521d\u5b66\u8005\u3002\u5982\u679c\u5df2\u6709\u4e00\u5b9a\u57fa\u7840\uff0c\u672c\u4e66\u80fd\u5e2e\u52a9\u60a8\u7cfb\u7edf\u56de\u987e\u7b97\u6cd5\u77e5\u8bc6\uff0c\u4e66\u5185\u6e90\u4ee3\u7801\u4e5f\u53ef\u4f5c\u4e3a\u201c\u5237\u9898\u5de5\u5177\u5e93\u201d\u4f7f\u7528\u3002
    • \u4e66\u4e2d\u5185\u5bb9\u4e3b\u8981\u5305\u62ec\u590d\u6742\u5ea6\u5206\u6790\u3001\u6570\u636e\u7ed3\u6784\u3001\u7b97\u6cd5\u4e09\u90e8\u5206\uff0c\u6db5\u76d6\u4e86\u8be5\u9886\u57df\u7684\u5927\u90e8\u5206\u4e3b\u9898\u3002
    • \u5bf9\u4e8e\u7b97\u6cd5\u65b0\u624b\uff0c\u5728\u521d\u5b66\u9636\u6bb5\u9605\u8bfb\u4e00\u672c\u5165\u95e8\u4e66\u7c4d\u81f3\u5173\u91cd\u8981\uff0c\u53ef\u4ee5\u5c11\u8d70\u8bb8\u591a\u5f2f\u8def\u3002
    • \u4e66\u5185\u7684\u52a8\u753b\u548c\u56fe\u89e3\u901a\u5e38\u7528\u4e8e\u4ecb\u7ecd\u91cd\u70b9\u548c\u96be\u70b9\u77e5\u8bc6\u3002\u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u5e94\u7ed9\u4e88\u8fd9\u4e9b\u5185\u5bb9\u66f4\u591a\u5173\u6ce8\u3002
    • \u5b9e\u8df5\u4e43\u5b66\u4e60\u7f16\u7a0b\u4e4b\u6700\u4f73\u9014\u5f84\u3002\u5f3a\u70c8\u5efa\u8bae\u8fd0\u884c\u6e90\u4ee3\u7801\u5e76\u4eb2\u81ea\u6572\u6253\u4ee3\u7801\u3002
    • \u672c\u4e66\u7f51\u9875\u7248\u7684\u6bcf\u4e2a\u7ae0\u8282\u90fd\u8bbe\u6709\u8ba8\u8bba\u533a\uff0c\u6b22\u8fce\u968f\u65f6\u5206\u4eab\u4f60\u7684\u7591\u60d1\u4e0e\u89c1\u89e3\u3002
    "},{"location":"chapter_reference/","title":"\u53c2\u8003\u6587\u732e","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":"10. \u00a0 \u641c\u7d22","text":""},{"location":"chapter_searching/binary_search/","title":"10.1. \u00a0 \u4e8c\u5206\u67e5\u627e","text":"

    \u300c\u4e8c\u5206\u67e5\u627e Binary Search\u300d\u662f\u4e00\u79cd\u57fa\u4e8e\u5206\u6cbb\u601d\u60f3\u7684\u9ad8\u6548\u641c\u7d22\u7b97\u6cd5\u3002\u5b83\u5229\u7528\u6570\u636e\u7684\u6709\u5e8f\u6027\uff0c\u6bcf\u8f6e\u51cf\u5c11\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\uff0c\u6570\u7ec4\u4e0d\u5305\u542b\u91cd\u590d\u5143\u7d20\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

    \u5bf9\u4e8e\u4e0a\u8ff0\u95ee\u9898\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\u5176\u4e2d\uff0c\u4e2d\u62ec\u53f7\u8868\u793a\u201c\u95ed\u533a\u95f4\u201d\uff0c\u5373\u5305\u542b\u8fb9\u754c\u503c\u672c\u8eab\u3002

    \u63a5\u4e0b\u6765\uff0c\u5faa\u73af\u6267\u884c\u4ee5\u4e0b\u4e24\u4e2a\u6b65\u9aa4\uff1a

    1. \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 \\(m = \\lfloor {(i + j) / 2} \\rfloor\\) \uff0c\u5176\u4e2d \\(\\lfloor \\space \\rfloor\\) \u8868\u793a\u5411\u4e0b\u53d6\u6574\u64cd\u4f5c\u3002
    2. \u5224\u65ad nums[m] \u548c target \u7684\u5927\u5c0f\u5173\u7cfb\uff0c\u5206\u4e3a\u4e09\u79cd\u60c5\u51b5\uff1a
      1. \u5f53 nums[m] < target \u65f6\uff0c\u8bf4\u660e target \u5728\u533a\u95f4 \\([m + 1, j]\\) \u4e2d\uff0c\u56e0\u6b64\u6267\u884c \\(i = m + 1\\) \uff1b
      2. \u5f53 nums[m] > target \u65f6\uff0c\u8bf4\u660e target \u5728\u533a\u95f4 \\([i, m - 1]\\) \u4e2d\uff0c\u56e0\u6b64\u6267\u884c \\(j = m - 1\\) \uff1b
      3. \u5f53 nums[m] = target \u65f6\uff0c\u8bf4\u660e\u627e\u5230 target \uff0c\u56e0\u6b64\u8fd4\u56de\u7d22\u5f15 \\(m\\) \uff1b

    \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

    <0><1><2><3><4><5><6><7>

    \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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nint 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\nwhile (i <= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\nelse if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\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\nint 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\nwhile (i <= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)    // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\nelse if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n
    binary_search.py
    def 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\ni, 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\nwhile 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\u9700\u8003\u8651\u5927\u6570\u8d8a\u754c\u95ee\u9898\nm = (i + j) // 2  # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target:\ni = m + 1  # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelif nums[m] > target:\nj = m - 1  # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse:\nreturn m  # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn -1  # \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\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\ni, 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\nfor i <= j {\nm := i + (j-i)/2      // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1\n} else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1\n} else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -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\nlet i = 0,\nj = 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\nwhile (i <= j) {\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m \uff0c\u4f7f\u7528 parseInt() \u5411\u4e0b\u53d6\u6574\nconst m = parseInt(i + (j - i) / 2);\nif (nums[m] < target)\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\nelse if (nums[m] > target)\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\nelse 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\nreturn -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\nlet i = 0,\nj = 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\nwhile (i <= j) {\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nconst m = Math.floor(i + (j - i) / 2);\nif (nums[m] < target) {\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\n} else if (nums[m] > target) {\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -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\nint 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\nwhile (i <= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)    // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\nelse if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -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\nint 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\nwhile (i <= j) {\nint m = i + (j - i) / 2;   // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)      // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\nelse if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\nelse                       // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -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\nvar i = 0\nvar 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\nwhile i <= j {\nlet m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1\n} else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1\n} else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -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\nvar i: usize = 0;\nvar 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\nwhile (i <= j) {\nvar m = i + (j - i) / 2;                // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums.items[m] < target) {           // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\n} else if (nums.items[m] > target) {    // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\n} else {                                // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn @intCast(T, m);\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -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\nint 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\nwhile (i <= j) {\nint m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target) {\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\n} else if (nums[m] > target) {\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n

    \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u3002\u6bcf\u8f6e\u7f29\u5c0f\u4e00\u534a\u533a\u95f4\uff0c\u56e0\u6b64\u4e8c\u5206\u5faa\u73af\u6b21\u6570\u4e3a \\(\\log_2 n\\) \u3002

    \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002\u6307\u9488 i , 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\u7684\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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart binary_search.java
    /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nint binarySearchLCRO(int[] nums, int target) {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nint 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\nwhile (i < j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\nelse if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n
    binary_search.cpp
    /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nint binarySearchLCRO(vector<int> &nums, int target) {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nint 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\nwhile (i < j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)    // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\nelse if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n
    binary_search.py
    def binary_search_lcro(nums: list[int], target: int) -> int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09\"\"\"\n# \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\ni, 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\nwhile i < j:\nm = (i + j) // 2  # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target:\ni = m + 1  # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\nelif nums[m] > target:\nj = m  # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nelse:\nreturn m  # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn -1  # \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n
    binary_search.go
    /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nfunc binarySearchLCRO(nums []int, target int) int {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\ni, 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\nfor i < j {\nm := i + (j-i)/2      // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1\n} else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m\n} else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1\n}\n
    binary_search.js
    /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nfunction binarySearchLCRO(nums, target) {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nlet i = 0,\nj = nums.length;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nwhile (i < j) {\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m \uff0c\u4f7f\u7528 parseInt() \u5411\u4e0b\u53d6\u6574\nconst m = parseInt(i + (j - i) / 2);\nif (nums[m] < target)\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\nelse if (nums[m] > target)\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nelse return m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n
    binary_search.ts
    /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nfunction binarySearchLCRO(nums: number[], target: number): number {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nlet i = 0,\nj = nums.length;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nwhile (i < j) {\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nconst m = Math.floor(i + (j - i) / 2);\nif (nums[m] < target) {\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\n} else if (nums[m] > target) {\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\n
    binary_search.c
    /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nint binarySearchLCRO(int *nums, int len, int target) {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nint 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\nwhile (i < j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)    // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\nelse if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n
    binary_search.cs
    /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nint binarySearchLCRO(int[] nums, int target) {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nint 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\nwhile (i < j) {\nint m = i + (j - i) / 2;   // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)      // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\nelse if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\nelse                       // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n
    binary_search.swift
    /* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nfunc binarySearchLCRO(nums: [Int], target: Int) -> Int {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nvar i = 0\nvar 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\nwhile i < j {\nlet m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1\n} else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m\n} else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1\n}\n
    binary_search.zig
    // \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09\nfn binarySearchLCRO(comptime T: type, nums: std.ArrayList(T), target: T) T {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nvar i: usize = 0;\nvar 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\nwhile (i <= j) {\nvar m = i + (j - i) / 2;                // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums.items[m] < target) {           // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\n} else if (nums.items[m] > target) {    // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\n} else {                                // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn @intCast(T, m);\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -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 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nint 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\nwhile (i < j) {\nint m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target) {\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\n} else if (nums[m] > target) {\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n

    \u5982\u4e0b\u56fe\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

    \u5728\u201c\u53cc\u95ed\u533a\u95f4\u201d\u8868\u793a\u6cd5\u4e2d\uff0c\u7531\u4e8e\u5de6\u53f3\u8fb9\u754c\u90fd\u88ab\u5b9a\u4e49\u4e3a\u95ed\u533a\u95f4\uff0c\u56e0\u6b64\u6307\u9488 \\(i\\) \u548c \\(j\\) \u7f29\u5c0f\u533a\u95f4\u64cd\u4f5c\u4e5f\u662f\u5bf9\u79f0\u7684\u3002\u8fd9\u6837\u66f4\u4e0d\u5bb9\u6613\u51fa\u9519\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u901a\u5e38\u91c7\u7528\u201c\u53cc\u95ed\u533a\u95f4\u201d\u7684\u5199\u6cd5\u3002

    Fig. \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\uff1a

    • \u4e8c\u5206\u67e5\u627e\u7684\u65f6\u95f4\u6548\u7387\u9ad8\u3002\u5728\u5927\u6570\u636e\u91cf\u4e0b\uff0c\u5bf9\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5177\u6709\u663e\u8457\u4f18\u52bf\u3002\u4f8b\u5982\uff0c\u5f53\u6570\u636e\u5927\u5c0f \\(n = 2^{20}\\) \u65f6\uff0c\u7ebf\u6027\u67e5\u627e\u9700\u8981 \\(2^{20} = 1048576\\) \u8f6e\u5faa\u73af\uff0c\u800c\u4e8c\u5206\u67e5\u627e\u4ec5\u9700 \\(\\log_2 2^{20} = 20\\) \u8f6e\u5faa\u73af\u3002
    • \u4e8c\u5206\u67e5\u627e\u65e0\u9700\u989d\u5916\u7a7a\u95f4\u3002\u76f8\u8f83\u4e8e\u9700\u8981\u501f\u52a9\u989d\u5916\u7a7a\u95f4\u7684\u641c\u7d22\u7b97\u6cd5\uff08\u4f8b\u5982\u54c8\u5e0c\u67e5\u627e\uff09\uff0c\u4e8c\u5206\u67e5\u627e\u66f4\u52a0\u8282\u7701\u7a7a\u95f4\u3002

    \u7136\u800c\uff0c\u4e8c\u5206\u67e5\u627e\u5e76\u975e\u9002\u7528\u4e8e\u6240\u6709\u60c5\u51b5\uff0c\u539f\u56e0\u5982\u4e0b\uff1a

    • \u4e8c\u5206\u67e5\u627e\u4ec5\u9002\u7528\u4e8e\u6709\u5e8f\u6570\u636e\u3002\u82e5\u8f93\u5165\u6570\u636e\u65e0\u5e8f\uff0c\u4e3a\u4e86\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u800c\u4e13\u95e8\u8fdb\u884c\u6392\u5e8f\uff0c\u5f97\u4e0d\u507f\u5931\u3002\u56e0\u4e3a\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4e3a \\(O(n \\log n)\\) \uff0c\u6bd4\u7ebf\u6027\u67e5\u627e\u548c\u4e8c\u5206\u67e5\u627e\u90fd\u66f4\u9ad8\u3002\u5bf9\u4e8e\u9891\u7e41\u63d2\u5165\u5143\u7d20\u7684\u573a\u666f\uff0c\u4e3a\u4fdd\u6301\u6570\u7ec4\u6709\u5e8f\u6027\uff0c\u9700\u8981\u5c06\u5143\u7d20\u63d2\u5165\u5230\u7279\u5b9a\u4f4d\u7f6e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u4e5f\u662f\u975e\u5e38\u6602\u8d35\u7684\u3002
    • \u4e8c\u5206\u67e5\u627e\u4ec5\u9002\u7528\u4e8e\u6570\u7ec4\u3002\u4e8c\u5206\u67e5\u627e\u9700\u8981\u8df3\u8dc3\u5f0f\uff08\u975e\u8fde\u7eed\u5730\uff09\u8bbf\u95ee\u5143\u7d20\uff0c\u800c\u5728\u94fe\u8868\u4e2d\u6267\u884c\u8df3\u8dc3\u5f0f\u8bbf\u95ee\u7684\u6548\u7387\u8f83\u4f4e\uff0c\u56e0\u6b64\u4e0d\u9002\u5408\u5e94\u7528\u5728\u94fe\u8868\u6216\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u3002
    • \u5c0f\u6570\u636e\u91cf\u4e0b\uff0c\u7ebf\u6027\u67e5\u627e\u6027\u80fd\u66f4\u4f73\u3002\u5728\u7ebf\u6027\u67e5\u627e\u4e2d\uff0c\u6bcf\u8f6e\u53ea\u9700\u8981 1 \u6b21\u5224\u65ad\u64cd\u4f5c\uff1b\u800c\u5728\u4e8c\u5206\u67e5\u627e\u4e2d\uff0c\u9700\u8981 1 \u6b21\u52a0\u6cd5\u30011 \u6b21\u9664\u6cd5\u30011 ~ 3 \u6b21\u5224\u65ad\u64cd\u4f5c\u30011 \u6b21\u52a0\u6cd5\uff08\u51cf\u6cd5\uff09\uff0c\u5171 4 ~ 6 \u4e2a\u5355\u5143\u64cd\u4f5c\uff1b\u56e0\u6b64\uff0c\u5f53\u6570\u636e\u91cf \\(n\\) \u8f83\u5c0f\u65f6\uff0c\u7ebf\u6027\u67e5\u627e\u53cd\u800c\u6bd4\u4e8c\u5206\u67e5\u627e\u66f4\u5feb\u3002
    "},{"location":"chapter_searching/binary_search_edge/","title":"10.2. \u00a0 \u4e8c\u5206\u67e5\u627e\u8fb9\u754c","text":"

    \u5728\u4e0a\u4e00\u8282\u4e2d\uff0c\u9898\u76ee\u89c4\u5b9a\u6570\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u90fd\u662f\u552f\u4e00\u7684\u3002\u5982\u679c\u76ee\u6807\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u591a\u6b21\u51fa\u73b0\uff0c\u4e0a\u8282\u4ecb\u7ecd\u7684\u65b9\u6cd5\u53ea\u80fd\u4fdd\u8bc1\u8fd4\u56de\u5176\u4e2d\u4e00\u4e2a\u76ee\u6807\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u800c\u65e0\u6cd5\u786e\u5b9a\u8be5\u7d22\u5f15\u7684\u5de6\u8fb9\u548c\u53f3\u8fb9\u8fd8\u6709\u591a\u5c11\u76ee\u6807\u5143\u7d20\u3002

    Question

    \u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6709\u5e8f\u6570\u7ec4 nums \uff0c\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\u3002\u8bf7\u67e5\u627e\u5e76\u8fd4\u56de\u5143\u7d20 target \u5728\u6570\u7ec4\u4e2d\u9996\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u3002\u82e5\u6570\u7ec4\u4e2d\u4e0d\u5305\u542b\u8be5\u5143\u7d20\uff0c\u5219\u8fd4\u56de \\(-1\\) \u3002

    "},{"location":"chapter_searching/binary_search_edge/#1021","title":"10.2.1. \u00a0 \u7b80\u5355\u65b9\u6cd5","text":"

    \u4e3a\u4e86\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5de6\u8fb9\u7684 target \uff0c\u6211\u4eec\u53ef\u4ee5\u5206\u4e3a\u4e24\u6b65\uff1a

    1. \u8fdb\u884c\u4e8c\u5206\u67e5\u627e\uff0c\u5b9a\u4f4d\u5230\u4efb\u610f\u4e00\u4e2a target \u7684\u7d22\u5f15\uff0c\u8bb0\u4e3a \\(k\\) \uff1b
    2. \u4ee5\u7d22\u5f15 \\(k\\) \u4e3a\u8d77\u59cb\u70b9\uff0c\u5411\u5de6\u8fdb\u884c\u7ebf\u6027\u904d\u5386\uff0c\u627e\u5230\u6700\u5de6\u8fb9\u7684 target \u8fd4\u56de\u5373\u53ef\u3002

    Fig. \u7ebf\u6027\u67e5\u627e\u6700\u5de6\u8fb9\u7684\u5143\u7d20

    \u8fd9\u4e2a\u65b9\u6cd5\u867d\u7136\u6709\u6548\uff0c\u4f46\u7531\u4e8e\u5305\u542b\u7ebf\u6027\u67e5\u627e\uff0c\u5176\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u80fd\u4f1a\u52a3\u5316\u81f3 \\(O(n)\\) \u3002

    "},{"location":"chapter_searching/binary_search_edge/#1022","title":"10.2.2. \u00a0 \u4e8c\u5206\u65b9\u6cd5","text":"

    \u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u4ec5\u901a\u8fc7\u4e8c\u5206\u67e5\u627e\u89e3\u51b3\u4ee5\u4e0a\u95ee\u9898\u3002\u6574\u4f53\u7b97\u6cd5\u6d41\u7a0b\u4e0d\u53d8\uff0c\u5148\u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 \\(m\\) \uff0c\u518d\u5224\u65ad target \u548c nums[m] \u5927\u5c0f\u5173\u7cfb\uff1a

    • \u5f53 nums[m] < target \u6216 nums[m] > target \u65f6\uff0c\u8bf4\u660e\u8fd8\u6ca1\u6709\u627e\u5230 target \uff0c\u56e0\u6b64\u91c7\u53d6\u4e0e\u4e0a\u8282\u4ee3\u7801\u76f8\u540c\u7684\u7f29\u5c0f\u533a\u95f4\u64cd\u4f5c\uff0c\u4ece\u800c\u4f7f\u6307\u9488 \\(i\\) \u548c \\(j\\) \u5411 target \u9760\u8fd1\u3002
    • \u5f53 nums[m] == target \u65f6\uff0c\u8bf4\u660e\u201c\u5c0f\u4e8e target \u7684\u5143\u7d20\u201d\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

    \u4e8c\u5206\u67e5\u627e\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\u8fd4\u56de\u7d22\u5f15 \\(i\\) \u5373\u53ef\u3002

    <1><2><3><4><5><6><7><8>

    \u6ce8\u610f\uff0c\u6570\u7ec4\u53ef\u80fd\u4e0d\u5305\u542b\u76ee\u6807\u5143\u7d20 target \u3002\u56e0\u6b64\u5728\u51fd\u6570\u8fd4\u56de\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u5224\u65ad nums[i] \u4e0e target \u662f\u5426\u76f8\u7b49\uff0c\u4ee5\u53ca\u7d22\u5f15 \\(i\\) \u662f\u5426\u8d8a\u754c\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart binary_search_edge.java
    /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a\u5143\u7d20 */\nint binarySearchLeftEdge(int[] nums, int target) {\nint i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelse if (nums[m] > target)\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse\nj = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n}\nif (i == nums.length || nums[i] != target)\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn i;\n}\n
    binary_search_edge.cpp
    /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a\u5143\u7d20 */\nint binarySearchLeftEdge(vector<int> &nums, int target) {\nint i = 0, j = nums.size() - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelse if (nums[m] > target)\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse\nj = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n}\nif (i == nums.size() || nums[i] != target)\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn i;\n}\n
    binary_search_edge.py
    def binary_search_left_edge(nums: list[int], target: int) -> int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a\u5143\u7d20\"\"\"\ni, j = 0, len(nums) - 1  # \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile i <= j:\nm = (i + j) // 2  # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target:\ni = m + 1  # target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelif nums[m] > target:\nj = m - 1  # target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse:\nj = m - 1  # \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\nif i == len(nums) or nums[i] != target:\nreturn -1  # \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn i\n
    binary_search_edge.go
    /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a\u5143\u7d20 */\nfunc binarySearchLeftEdge(nums []int, target int) int {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\ni, j := 0, len(nums)-1\nfor i <= j {\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nm := i + (j-i)/2\nif nums[m] < target {\n// target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1\n} else if nums[m] > target {\n// target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1\n} else {\n// \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1\n}\n}\nif i == len(nums) || nums[i] != target {\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1\n}\nreturn i\n}\n
    binary_search_edge.js
    /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a\u5143\u7d20 */\nfunction binarySearchLeftEdge(nums, target) {\nlet i = 0,\nj = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nlet m = Math.floor((i + j) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target) {\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if (nums[m] > target) {\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nj = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n}\n}\nif (i == nums.length || nums[i] != target) {\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\nreturn i;\n}\n
    binary_search_edge.ts
    /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a\u5143\u7d20 */\nfunction binarySearchLeftEdge(nums: number[], target: number): number {\nlet i = 0, j = nums.length - 1;  // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nlet m = Math.floor((i + j) / 2);  // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target) {\ni = m + 1;  // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if (nums[m] > target) {\nj = m - 1;  // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nj = m - 1;  // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n}\n}\nif (i == nums.length || nums[i] != target) {\nreturn -1;  // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\nreturn i;\n}\n
    binary_search_edge.c
    /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a\u5143\u7d20 */\nint binarySearchLeftEdge(int *nums, int size, int target) {\nint i = 0, j = size - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelse if (nums[m] > target)\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse\nj = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n}\nif (i == size || nums[i] != target)\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn i;\n}\n
    binary_search_edge.cs
    /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a\u5143\u7d20 */\nint binarySearchLeftEdge(int[] nums, int target) {\nint i = 0, j = nums.Length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelse if (nums[m] > target)\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse\nj = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n}\nif (i == nums.Length || nums[i] != target)\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn i;\n}\n
    binary_search_edge.swift
    /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a\u5143\u7d20 */\nfunc binarySearchLeftEdge(nums: [Int], target: Int) -> Int {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nvar i = 0\nvar j = nums.count - 1\nwhile i <= j {\nlet m = i + (j - 1) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target {\ni = m + 1 // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if nums[m] > target {\nj = m - 1 // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nj = m - 1 // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n}\n}\nif i == nums.count || nums[i] != target {\nreturn -1 // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\nreturn i\n}\n
    binary_search_edge.zig
    [class]{}-[func]{binarySearchLeftEdge}\n
    binary_search_edge.dart
    /* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a\u5143\u7d20 */\nint binarySearchLeftEdge(List<int> nums, int target) {\nint i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nint m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u95f4\u7d22\u5f15 m\nif (nums[m] < target)\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelse if (nums[m] > target)\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse\nj = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n}\nif (i == nums.length || nums[i] != target) return -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn i;\n}\n
    "},{"location":"chapter_searching/binary_search_edge/#1023","title":"10.2.3. \u00a0 \u67e5\u627e\u53f3\u8fb9\u754c","text":"

    \u7c7b\u4f3c\u5730\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4e8c\u5206\u67e5\u627e\u6700\u53f3\u8fb9\u7684 target \u3002\u5f53 nums[m] == target \u65f6\uff0c\u8bf4\u660e\u5927\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 \\([m + 1, j]\\) \u4e2d\uff0c\u56e0\u6b64\u6267\u884c i = m + 1 \uff0c\u4f7f\u5f97\u6307\u9488 \\(i\\) \u5411\u5927\u4e8e target \u7684\u5143\u7d20\u9760\u8fd1\u3002

    \u5b8c\u6210\u4e8c\u5206\u540e\uff0c\\(i\\) \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\uff0c\\(j\\) \u6307\u5411\u6700\u53f3\u8fb9\u7684 target \uff0c\u56e0\u6b64\u8fd4\u56de\u7d22\u5f15 \\(j\\) \u5373\u53ef\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart binary_search_edge.java
    /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a\u5143\u7d20 */\nint binarySearchRightEdge(int[] nums, int target) {\nint i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelse if (nums[m] > target)\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse\ni = m + 1; // \u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [m+1, j] \u4e2d\n}\nif (j < 0 || nums[j] != target)\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn j;\n}\n
    binary_search_edge.cpp
    /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a\u5143\u7d20 */\nint binarySearchRightEdge(vector<int> &nums, int target) {\nint i = 0, j = nums.size() - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelse if (nums[m] > target)\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse\ni = m + 1; // \u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [m+1, j] \u4e2d\n}\nif (j < 0 || nums[j] != target)\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn j;\n}\n
    binary_search_edge.py
    def binary_search_right_edge(nums: list[int], target: int) -> int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a\u5143\u7d20\"\"\"\ni, j = 0, len(nums) - 1  # \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile i <= j:\nm = (i + j) // 2  # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target:\ni = m + 1  # target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelif nums[m] > target:\nj = m - 1  # target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse:\ni = m + 1  # \u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [m+1, j] \u4e2d\nif j == len(nums) or nums[j] != target:\nreturn -1  # \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn j\n
    binary_search_edge.go
    /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a\u5143\u7d20 */\nfunc binarySearchRightEdge(nums []int, target int) int {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\ni, j := 0, len(nums)-1\nfor i <= j {\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nm := i + (j-i)/2\nif nums[m] < target {\n// target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1\n} else if nums[m] > target {\n// target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1\n} else {\n// \u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1\n}\n}\nif j < 0 || nums[j] != target {\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1\n}\nreturn j\n}\n
    binary_search_edge.js
    /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a\u5143\u7d20 */\nfunction binarySearchRightEdge(nums, target) {\nlet i = 0,\nj = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nlet m = Math.floor((i + j) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target) {\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if (nums[m] > target) {\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\ni = m + 1; // \u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [m+1, j] \u4e2d\n}\n}\nif (j == nums.length || nums[j] != target) {\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\nreturn j;\n}\n
    binary_search_edge.ts
    /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a\u5143\u7d20 */\nfunction binarySearchRightEdge(nums: number[], target: number): number {\nlet i = 0, j = nums.length - 1;  // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nlet m = Math.floor((i + j) / 2);  // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target) {\ni = m + 1;  // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if (nums[m] > target) {\nj = m - 1;  // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\ni = m + 1;  // \u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [m+1, j] \u4e2d\n}\n}\nif (j == nums.length || nums[j] != target) {\nreturn -1;  // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\nreturn j;\n}\n
    binary_search_edge.c
    /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a\u5143\u7d20 */\nint binarySearchRightEdge(int *nums, int size, int target) {\nint i = 0, j = size - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelse if (nums[m] > target)\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse\ni = m + 1; // \u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [m+1, j] \u4e2d\n}\nif (j < 0 || nums[j] != target)\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn j;\n}\n
    binary_search_edge.cs
    /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a\u5143\u7d20 */\nint binarySearchRightEdge(int[] nums, int target) {\nint i = 0, j = nums.Length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target)\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelse if (nums[m] > target)\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse\ni = m + 1; // \u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [m+1, j] \u4e2d\n}\nif (j < 0 || nums[j] != target)\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn j;\n}\n
    binary_search_edge.swift
    /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a\u5143\u7d20 */\nfunc binarySearchRightEdge(nums: [Int], target: Int) -> Int {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nvar i = 0\nvar j = nums.count - 1\nwhile i <= j {\nlet m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target {\ni = m + 1 // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if nums[m] > target {\nj = m - 1 // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\ni = m + 1 // \u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [m+1, j] \u4e2d\n}\n}\nif j < 0 || nums[j] != target {\nreturn -1 // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\nreturn j\n}\n
    binary_search_edge.zig
    [class]{}-[func]{binarySearchRightEdge}\n
    binary_search_edge.dart
    /* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a\u5143\u7d20 */\nint binarySearchRightEdge(List<int> nums, int target) {\nint i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nint m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u95f4\u7d22\u5f15 m\nif (nums[m] < target)\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelse if (nums[m] > target)\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse\ni = m + 1; // \u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [m+1, j] \u4e2d\n}\nif (j < 0 || nums[j] != target) return -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn j;\n}\n

    \u89c2\u5bdf\u4e0b\u56fe\uff0c\u641c\u7d22\u6700\u53f3\u8fb9\u5143\u7d20\u65f6\u6307\u9488 \\(j\\) \u7684\u4f5c\u7528\u4e0e\u641c\u7d22\u6700\u5de6\u8fb9\u5143\u7d20\u65f6\u6307\u9488 \\(i\\) \u7684\u4f5c\u7528\u4e00\u81f4\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u641c\u7d22\u6700\u5de6\u8fb9\u5143\u7d20\u548c\u6700\u53f3\u8fb9\u5143\u7d20\u7684\u5b9e\u73b0\u662f\u955c\u50cf\u5bf9\u79f0\u7684\u3002

    Fig. \u67e5\u627e\u6700\u5de6\u8fb9\u548c\u6700\u53f3\u8fb9\u5143\u7d20\u7684\u5bf9\u79f0\u6027

    Tip

    \u4ee5\u4e0a\u4ee3\u7801\u91c7\u53d6\u7684\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

    "},{"location":"chapter_searching/replace_linear_by_hashing/","title":"10.3. \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

    "},{"location":"chapter_searching/replace_linear_by_hashing/#1031","title":"10.3.1. \u00a0 \u7ebf\u6027\u67e5\u627e\uff1a\u4ee5\u65f6\u95f4\u6362\u7a7a\u95f4","text":"

    \u8003\u8651\u76f4\u63a5\u904d\u5386\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\u3002\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

    Fig. \u7ebf\u6027\u67e5\u627e\u6c42\u89e3\u4e24\u6570\u4e4b\u548c

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart two_sum.java
    /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nint[] twoSumBruteForce(int[] nums, int target) {\nint size = nums.length;\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor (int i = 0; i < size - 1; i++) {\nfor (int j = i + 1; j < size; j++) {\nif (nums[i] + nums[j] == target)\nreturn new int[] { i, j };\n}\n}\nreturn new int[0];\n}\n
    two_sum.cpp
    /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nvector<int> twoSumBruteForce(vector<int> &nums, int target) {\nint size = nums.size();\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor (int i = 0; i < size - 1; i++) {\nfor (int j = i + 1; j < size; j++) {\nif (nums[i] + nums[j] == target)\nreturn {i, j};\n}\n}\nreturn {};\n}\n
    two_sum.py
    def 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 O(n^2)\nfor i in range(len(nums) - 1):\nfor j in range(i + 1, len(nums)):\nif nums[i] + nums[j] == target:\nreturn [i, j]\nreturn []\n
    two_sum.go
    /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunc twoSumBruteForce(nums []int, target int) []int {\nsize := len(nums)\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor i := 0; i < size-1; i++ {\nfor j := i + 1; i < size; j++ {\nif nums[i]+nums[j] == target {\nreturn []int{i, j}\n}\n}\n}\nreturn nil\n}\n
    two_sum.js
    /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunction twoSumBruteForce(nums, target) {\nconst n = nums.length;\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor (let i = 0; i < n; i++) {\nfor (let j = i + 1; j < n; j++) {\nif (nums[i] + nums[j] === target) {\nreturn [i, j];\n}\n}\n}\nreturn [];\n}\n
    two_sum.ts
    /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunction twoSumBruteForce(nums: number[], target: number): number[] {\nconst n = nums.length;\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor (let i = 0; i < n; i++) {\nfor (let j = i + 1; j < n; j++) {\nif (nums[i] + nums[j] === target) {\nreturn [i, j];\n}\n}\n}\nreturn [];\n}\n
    two_sum.c
    /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nint *twoSumBruteForce(int *nums, int numsSize, int target, int *returnSize) {\nfor (int i = 0; i < numsSize; ++i) {\nfor (int j = i + 1; j < numsSize; ++j) {\nif (nums[i] + nums[j] == target) {\nint *res = malloc(sizeof(int) * 2);\nres[0] = i, res[1] = j;\n*returnSize = 2;\nreturn res;\n}\n}\n}\n*returnSize = 0;\nreturn NULL;\n}\n
    two_sum.cs
    /* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nint[] twoSumBruteForce(int[] nums, int target) {\nint size = nums.Length;\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor (int i = 0; i < size - 1; i++) {\nfor (int j = i + 1; j < size; j++) {\nif (nums[i] + nums[j] == target)\nreturn new int[] { i, j };\n}\n}\nreturn new int[0];\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 O(n^2)\nfor i in nums.indices.dropLast() {\nfor j in nums.indices.dropFirst(i + 1) {\nif nums[i] + nums[j] == target {\nreturn [i, j]\n}\n}\n}\nreturn [0]\n}\n
    two_sum.zig
    // \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e\nfn twoSumBruteForce(nums: []i32, target: i32) ?[2]i32 {\nvar size: usize = nums.len;\nvar i: usize = 0;\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nwhile (i < size - 1) : (i += 1) {\nvar j = i + 1;\nwhile (j < size) : (j += 1) {\nif (nums[i] + nums[j] == target) {\nreturn [_]i32{@intCast(i32, i), @intCast(i32, j)};\n}\n}\n}\nreturn null;\n}\n
    two_sum.dart
    /* \u65b9\u6cd5\u4e00\uff1a \u66b4\u529b\u679a\u4e3e */\nList<int> twoSumBruteForce(List<int> nums, int target) {\nint size = nums.length;\nfor (var i = 0; i < size - 1; i++) {\nfor (var j = i + 1; j < size; j++) {\nif (nums[i] + nums[j] == target) return [i, j];\n}\n}\nreturn [0];\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/#1032","title":"10.3.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\uff1a

    1. \u5224\u65ad\u6570\u5b57 target - nums[i] \u662f\u5426\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u82e5\u662f\u5219\u76f4\u63a5\u8fd4\u56de\u8fd9\u4e24\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\uff1b
    2. \u5c06\u952e\u503c\u5bf9 nums[i] \u548c\u7d22\u5f15 i \u6dfb\u52a0\u8fdb\u54c8\u5e0c\u8868\uff1b
    <1><2><3>

    \u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff0c\u4ec5\u9700\u5355\u5c42\u5faa\u73af\u5373\u53ef\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart two_sum.java
    /* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nint[] twoSumHashTable(int[] nums, int target) {\nint size = nums.length;\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nMap<Integer, Integer> dic = new HashMap<>();\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor (int i = 0; i < size; i++) {\nif (dic.containsKey(target - nums[i])) {\nreturn new int[] { dic.get(target - nums[i]), i };\n}\ndic.put(nums[i], i);\n}\nreturn new int[0];\n}\n
    two_sum.cpp
    /* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nvector<int> twoSumHashTable(vector<int> &nums, int target) {\nint size = nums.size();\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nunordered_map<int, int> dic;\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor (int i = 0; i < size; i++) {\nif (dic.find(target - nums[i]) != dic.end()) {\nreturn {dic[target - nums[i]], i};\n}\ndic.emplace(nums[i], i);\n}\nreturn {};\n}\n
    two_sum.py
    def 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 O(n)\ndic = {}\n# \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor i in range(len(nums)):\nif target - nums[i] in dic:\nreturn [dic[target - nums[i]], i]\ndic[nums[i]] = i\nreturn []\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 O(n)\nhashTable := map[int]int{}\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor idx, val := range nums {\nif preIdx, ok := hashTable[target-val]; ok {\nreturn []int{preIdx, idx}\n}\nhashTable[val] = idx\n}\nreturn nil\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 O(n)\nlet m = {};\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor (let i = 0; i < nums.length; i++) {\nif (m[nums[i]] !== undefined) {\nreturn [m[nums[i]], i];\n} else {\nm[target - nums[i]] = i;\n}\n}\nreturn [];\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 O(n)\nlet m: Map<number, number> = new Map();\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor (let i = 0; i < nums.length; i++) {\nlet index = m.get(nums[i]);\nif (index !== undefined) {\nreturn [index, i];\n} else {\nm.set(target - nums[i], i);\n}\n}\nreturn [];\n}\n
    two_sum.c
    /* \u54c8\u5e0c\u8868 */\nstruct hashTable {\nint key;\nint val;\nUT_hash_handle hh; // \u57fa\u4e8e uthash.h \u5b9e\u73b0\n};\ntypedef struct hashTable hashTable;\n/* \u54c8\u5e0c\u8868\u67e5\u8be2 */\nhashTable *find(hashTable *h, int key) {\nhashTable *tmp;\nHASH_FIND_INT(h, &key, tmp);\nreturn tmp;\n}\n/* \u54c8\u5e0c\u8868\u5143\u7d20\u63d2\u5165 */\nvoid insert(hashTable *h, int key, int val) {\nhashTable *t = find(h, key);\nif (t == NULL) {\nhashTable *tmp = malloc(sizeof(hashTable));\ntmp->key = key, tmp->val = val;\nHASH_ADD_INT(h, key, tmp);\n} else {\nt->val = val;\n}\n}\n/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nint *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) {\nhashTable *hashtable = NULL;\nfor (int i = 0; i < numsSize; i++) {\nhashTable *t = find(hashtable, target - nums[i]);\nif (t != NULL) {\nint *res = malloc(sizeof(int) * 2);\nres[0] = t->val, res[1] = i;\n*returnSize = 2;\nreturn res;\n}\ninsert(hashtable, nums[i], i);\n}\n*returnSize = 0;\nreturn NULL;\n}\n
    two_sum.cs
    /* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nint[] twoSumHashTable(int[] nums, int target) {\nint size = nums.Length;\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nDictionary<int, int> dic = new();\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor (int i = 0; i < size; i++) {\nif (dic.ContainsKey(target - nums[i])) {\nreturn new int[] { dic[target - nums[i]], i };\n}\ndic.Add(nums[i], i);\n}\nreturn new int[0];\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 O(n)\nvar dic: [Int: Int] = [:]\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor i in nums.indices {\nif let j = dic[target - nums[i]] {\nreturn [j, i]\n}\ndic[nums[i]] = i\n}\nreturn [0]\n}\n
    two_sum.zig
    // \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868\nfn twoSumHashTable(nums: []i32, target: i32) !?[2]i32 {\nvar size: usize = nums.len;\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nvar dic = std.AutoHashMap(i32, i32).init(std.heap.page_allocator);\ndefer dic.deinit();\nvar i: usize = 0;\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nwhile (i < size) : (i += 1) {\nif (dic.contains(target - nums[i])) {\nreturn [_]i32{dic.get(target - nums[i]).?, @intCast(i32, i)};\n}\ntry dic.put(nums[i], @intCast(i32, i));\n}\nreturn null;\n}\n
    two_sum.dart
    /* \u65b9\u6cd5\u4e8c\uff1a \u8f85\u52a9\u54c8\u5e0c\u8868 */\nList<int> twoSumHashTable(List<int> nums, int target) {\nint size = nums.length;\nMap<int, int> dic = HashMap();\nfor (var i = 0; i < size; i++) {\nif (dic.containsKey(target - nums[i])) {\nreturn [dic[target - nums[i]]!, i];\n}\ndic.putIfAbsent(nums[i], () => i);\n}\nreturn [0];\n}\n

    \u6b64\u65b9\u6cd5\u901a\u8fc7\u54c8\u5e0c\u67e5\u627e\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n^2)\\) \u964d\u4f4e\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.4. \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

    \u6839\u636e\u5b9e\u73b0\u601d\u8def\uff0c\u641c\u7d22\u7b97\u6cd5\u603b\u4f53\u53ef\u5206\u4e3a\u4e24\u79cd\uff1a

    • \u901a\u8fc7\u904d\u5386\u6570\u636e\u7ed3\u6784\u6765\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\uff0c\u4f8b\u5982\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6811\u548c\u56fe\u7684\u904d\u5386\u7b49\u3002
    • \u5229\u7528\u6570\u636e\u7ec4\u7ec7\u7ed3\u6784\u6216\u6570\u636e\u5305\u542b\u7684\u5148\u9a8c\u4fe1\u606f\uff0c\u5b9e\u73b0\u9ad8\u6548\u5143\u7d20\u67e5\u627e\uff0c\u4f8b\u5982\u4e8c\u5206\u67e5\u627e\u3001\u54c8\u5e0c\u67e5\u627e\u548c\u4e8c\u53c9\u641c\u7d22\u6811\u67e5\u627e\u7b49\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/#1041","title":"10.4.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

    • \u300c\u7ebf\u6027\u641c\u7d22\u300d\u9002\u7528\u4e8e\u6570\u7ec4\u548c\u94fe\u8868\u7b49\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u3002\u5b83\u4ece\u6570\u636e\u7ed3\u6784\u7684\u4e00\u7aef\u5f00\u59cb\uff0c\u9010\u4e2a\u8bbf\u95ee\u5143\u7d20\uff0c\u76f4\u5230\u627e\u5230\u76ee\u6807\u5143\u7d20\u6216\u5230\u8fbe\u53e6\u4e00\u7aef\u4ecd\u6ca1\u6709\u627e\u5230\u76ee\u6807\u5143\u7d20\u4e3a\u6b62\u3002
    • \u300c\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u300d\u548c\u300c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u300d\u662f\u56fe\u548c\u6811\u7684\u4e24\u79cd\u904d\u5386\u7b56\u7565\u3002\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u4ece\u521d\u59cb\u8282\u70b9\u5f00\u59cb\u9010\u5c42\u641c\u7d22\uff0c\u7531\u8fd1\u53ca\u8fdc\u5730\u8bbf\u95ee\u5404\u4e2a\u8282\u70b9\u3002\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u662f\u4ece\u521d\u59cb\u8282\u70b9\u5f00\u59cb\uff0c\u6cbf\u7740\u4e00\u6761\u8def\u5f84\u8d70\u5230\u5934\u4e3a\u6b62\uff0c\u518d\u56de\u6eaf\u5e76\u5c1d\u8bd5\u5176\u4ed6\u8def\u5f84\uff0c\u76f4\u5230\u904d\u5386\u5b8c\u6574\u4e2a\u6570\u636e\u7ed3\u6784\u3002

    \u66b4\u529b\u641c\u7d22\u7684\u4f18\u70b9\u662f\u7b80\u5355\u4e14\u901a\u7528\u6027\u597d\uff0c\u65e0\u9700\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/#1042","title":"10.4.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

    • \u300c\u4e8c\u5206\u67e5\u627e\u300d\u5229\u7528\u6570\u636e\u7684\u6709\u5e8f\u6027\u5b9e\u73b0\u9ad8\u6548\u67e5\u627e\uff0c\u4ec5\u9002\u7528\u4e8e\u6570\u7ec4\u3002
    • \u300c\u54c8\u5e0c\u67e5\u627e\u300d\u5229\u7528\u54c8\u5e0c\u8868\u5c06\u641c\u7d22\u6570\u636e\u548c\u76ee\u6807\u6570\u636e\u5efa\u7acb\u4e3a\u952e\u503c\u5bf9\u6620\u5c04\uff0c\u4ece\u800c\u5b9e\u73b0\u67e5\u8be2\u64cd\u4f5c\u3002
    • \u300c\u6811\u67e5\u627e\u300d\u5728\u7279\u5b9a\u7684\u6811\u7ed3\u6784\uff08\u4f8b\u5982\u4e8c\u53c9\u641c\u7d22\u6811\uff09\u4e2d\uff0c\u57fa\u4e8e\u6bd4\u8f83\u8282\u70b9\u503c\u6765\u5feb\u901f\u6392\u9664\u8282\u70b9\uff0c\u4ece\u800c\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\u652f\u3002

    Note

    \u81ea\u9002\u5e94\u641c\u7d22\u7b97\u6cd5\u5e38\u88ab\u79f0\u4e3a\u67e5\u627e\u7b97\u6cd5\uff0c\u4e3b\u8981\u5173\u6ce8\u5728\u7279\u5b9a\u6570\u636e\u7ed3\u6784\u4e2d\u5feb\u901f\u68c0\u7d22\u76ee\u6807\u5143\u7d20\u3002

    "},{"location":"chapter_searching/searching_algorithm_revisited/#1043","title":"10.4.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\u5728\u8be5\u6570\u636e\u4e2d\u641c\u7d22\u76ee\u6807\u5143\u7d20\u3002\u5404\u4e2a\u65b9\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u5982\u4e0b\u56fe\u6240\u793a\u3002

    Fig. \u591a\u79cd\u641c\u7d22\u7b56\u7565

    \u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\u7684\u64cd\u4f5c\u6548\u7387\u4e0e\u7279\u6027\u5982\u4e0b\u8868\u6240\u793a\u3002

    \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

    \u9664\u4e86\u4ee5\u4e0a\u8868\u683c\u5185\u5bb9\uff0c\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

    • \u901a\u7528\u6027\u8f83\u597d\uff0c\u65e0\u9700\u4efb\u4f55\u6570\u636e\u9884\u5904\u7406\u64cd\u4f5c\u3002\u5047\u5982\u6211\u4eec\u4ec5\u9700\u67e5\u8be2\u4e00\u6b21\u6570\u636e\uff0c\u90a3\u4e48\u5176\u4ed6\u4e09\u79cd\u65b9\u6cd5\u7684\u6570\u636e\u9884\u5904\u7406\u7684\u65f6\u95f4\u6bd4\u7ebf\u6027\u641c\u7d22\u7684\u65f6\u95f4\u8fd8\u8981\u66f4\u957f\u3002
    • \u9002\u7528\u4e8e\u4f53\u91cf\u8f83\u5c0f\u7684\u6570\u636e\uff0c\u6b64\u60c5\u51b5\u4e0b\u65f6\u95f4\u590d\u6742\u5ea6\u5bf9\u6548\u7387\u5f71\u54cd\u8f83\u5c0f\u3002
    • \u9002\u7528\u4e8e\u6570\u636e\u66f4\u65b0\u9891\u7387\u8f83\u9ad8\u7684\u573a\u666f\uff0c\u56e0\u4e3a\u8be5\u65b9\u6cd5\u4e0d\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u4efb\u4f55\u989d\u5916\u7ef4\u62a4\u3002

    \u4e8c\u5206\u67e5\u627e

    • \u9002\u7528\u4e8e\u5927\u6570\u636e\u91cf\u7684\u60c5\u51b5\uff0c\u6548\u7387\u8868\u73b0\u7a33\u5b9a\uff0c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u3002
    • \u6570\u636e\u91cf\u4e0d\u80fd\u8fc7\u5927\uff0c\u56e0\u4e3a\u5b58\u50a8\u6570\u7ec4\u9700\u8981\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u3002
    • \u4e0d\u9002\u7528\u4e8e\u9ad8\u9891\u589e\u5220\u6570\u636e\u7684\u573a\u666f\uff0c\u56e0\u4e3a\u7ef4\u62a4\u6709\u5e8f\u6570\u7ec4\u7684\u5f00\u9500\u8f83\u5927\u3002

    \u54c8\u5e0c\u67e5\u627e

    • \u9002\u5408\u5bf9\u67e5\u8be2\u6027\u80fd\u8981\u6c42\u5f88\u9ad8\u7684\u573a\u666f\uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002
    • \u4e0d\u9002\u5408\u9700\u8981\u6709\u5e8f\u6570\u636e\u6216\u8303\u56f4\u67e5\u627e\u7684\u573a\u666f\uff0c\u56e0\u4e3a\u54c8\u5e0c\u8868\u65e0\u6cd5\u7ef4\u62a4\u6570\u636e\u7684\u6709\u5e8f\u6027\u3002
    • \u5bf9\u54c8\u5e0c\u51fd\u6570\u548c\u54c8\u5e0c\u51b2\u7a81\u5904\u7406\u7b56\u7565\u7684\u4f9d\u8d56\u6027\u8f83\u9ad8\uff0c\u5177\u6709\u8f83\u5927\u7684\u6027\u80fd\u52a3\u5316\u98ce\u9669\u3002
    • \u4e0d\u9002\u5408\u6570\u636e\u91cf\u8fc7\u5927\u7684\u60c5\u51b5\uff0c\u56e0\u4e3a\u54c8\u5e0c\u8868\u9700\u8981\u989d\u5916\u7a7a\u95f4\u6765\u6700\u5927\u7a0b\u5ea6\u5730\u51cf\u5c11\u51b2\u7a81\uff0c\u4ece\u800c\u63d0\u4f9b\u826f\u597d\u7684\u67e5\u8be2\u6027\u80fd\u3002

    \u6811\u67e5\u627e

    • \u9002\u7528\u4e8e\u6d77\u91cf\u6570\u636e\uff0c\u56e0\u4e3a\u6811\u8282\u70b9\u5728\u5185\u5b58\u4e2d\u662f\u79bb\u6563\u5b58\u50a8\u7684\u3002
    • \u9002\u5408\u9700\u8981\u7ef4\u62a4\u6709\u5e8f\u6570\u636e\u6216\u8303\u56f4\u67e5\u627e\u7684\u573a\u666f\u3002
    • \u5728\u6301\u7eed\u589e\u5220\u8282\u70b9\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u53ef\u80fd\u4ea7\u751f\u503e\u659c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u52a3\u5316\u81f3 \\(O(n)\\) \u3002
    • \u82e5\u4f7f\u7528 AVL \u6811\u6216\u7ea2\u9ed1\u6811\uff0c\u5219\u5404\u9879\u64cd\u4f5c\u53ef\u5728 \\(O(\\log n)\\) \u6548\u7387\u4e0b\u7a33\u5b9a\u8fd0\u884c\uff0c\u4f46\u7ef4\u62a4\u6811\u5e73\u8861\u7684\u64cd\u4f5c\u4f1a\u589e\u52a0\u989d\u5916\u5f00\u9500\u3002
    "},{"location":"chapter_searching/summary/","title":"10.5. \u00a0 \u5c0f\u7ed3","text":"
    • \u4e8c\u5206\u67e5\u627e\u4f9d\u8d56\u4e8e\u6570\u636e\u7684\u6709\u5e8f\u6027\uff0c\u901a\u8fc7\u5faa\u73af\u9010\u6b65\u7f29\u51cf\u4e00\u534a\u641c\u7d22\u533a\u95f4\u6765\u5b9e\u73b0\u67e5\u627e\u3002\u5b83\u8981\u6c42\u8f93\u5165\u6570\u636e\u6709\u5e8f\uff0c\u4e14\u4ec5\u9002\u7528\u4e8e\u6570\u7ec4\u6216\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u3002
    • \u66b4\u529b\u641c\u7d22\u901a\u8fc7\u904d\u5386\u6570\u636e\u7ed3\u6784\u6765\u5b9a\u4f4d\u6570\u636e\u3002\u7ebf\u6027\u641c\u7d22\u9002\u7528\u4e8e\u6570\u7ec4\u548c\u94fe\u8868\uff0c\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u548c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u9002\u7528\u4e8e\u56fe\u548c\u6811\u3002\u6b64\u7c7b\u7b97\u6cd5\u901a\u7528\u6027\u597d\uff0c\u65e0\u9700\u5bf9\u6570\u636e\u9884\u5904\u7406\uff0c\u4f46\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u8f83\u9ad8\u3002
    • \u54c8\u5e0c\u67e5\u627e\u3001\u6811\u67e5\u627e\u548c\u4e8c\u5206\u67e5\u627e\u5c5e\u4e8e\u9ad8\u6548\u641c\u7d22\u65b9\u6cd5\uff0c\u53ef\u5728\u7279\u5b9a\u6570\u636e\u7ed3\u6784\u4e2d\u5feb\u901f\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\u3002\u6b64\u7c7b\u7b97\u6cd5\u6548\u7387\u9ad8\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe \\(O(\\log n)\\) \u751a\u81f3 \\(O(1)\\) \uff0c\u4f46\u901a\u5e38\u9700\u8981\u501f\u52a9\u989d\u5916\u6570\u636e\u7ed3\u6784\u3002
    • \u5b9e\u9645\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u6570\u636e\u4f53\u91cf\u3001\u641c\u7d22\u6027\u80fd\u8981\u6c42\u3001\u6570\u636e\u67e5\u8be2\u548c\u66f4\u65b0\u9891\u7387\u7b49\u56e0\u7d20\u8fdb\u884c\u5177\u4f53\u5206\u6790\uff0c\u4ece\u800c\u9009\u62e9\u5408\u9002\u7684\u641c\u7d22\u65b9\u6cd5\u3002
    • \u7ebf\u6027\u641c\u7d22\u9002\u7528\u4e8e\u5c0f\u578b\u6216\u9891\u7e41\u66f4\u65b0\u7684\u6570\u636e\uff1b\u4e8c\u5206\u67e5\u627e\u9002\u7528\u4e8e\u5927\u578b\u3001\u6392\u5e8f\u7684\u6570\u636e\uff1b\u54c8\u5e0c\u67e5\u627e\u9002\u5408\u5bf9\u67e5\u8be2\u6548\u7387\u8981\u6c42\u8f83\u9ad8\u4e14\u65e0\u9700\u8303\u56f4\u67e5\u8be2\u7684\u6570\u636e\uff1b\u6811\u67e5\u627e\u9002\u7528\u4e8e\u9700\u8981\u7ef4\u62a4\u987a\u5e8f\u548c\u652f\u6301\u8303\u56f4\u67e5\u8be2\u7684\u5927\u578b\u52a8\u6001\u6570\u636e\u3002
    • \u7528\u54c8\u5e0c\u67e5\u627e\u66ff\u6362\u7ebf\u6027\u67e5\u627e\u662f\u4e00\u79cd\u5e38\u7528\u7684\u4f18\u5316\u8fd0\u884c\u65f6\u95f4\u7684\u7b56\u7565\uff0c\u53ef\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n)\\) \u964d\u4f4e\u81f3 \\(O(1)\\) \u3002
    "},{"location":"chapter_sorting/","title":"11. \u00a0 \u6392\u5e8f","text":""},{"location":"chapter_sorting/bubble_sort/","title":"11.3. \u00a0 \u5192\u6ce1\u6392\u5e8f","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

    \u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5143\u7d20\u4ea4\u6362\u64cd\u4f5c\u6a21\u62df\u4e0a\u8ff0\u8fc7\u7a0b\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\u5b83\u4fe9\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>

    "},{"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\u4e3a\uff1a

    1. \u9996\u5148\uff0c\u5bf9 \\(n\\) \u4e2a\u5143\u7d20\u6267\u884c\u201c\u5192\u6ce1\u201d\uff0c\u5c06\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u6b63\u786e\u4f4d\u7f6e\uff0c
    2. \u63a5\u4e0b\u6765\uff0c\u5bf9\u5269\u4f59 \\(n - 1\\) \u4e2a\u5143\u7d20\u6267\u884c\u201c\u5192\u6ce1\u201d\uff0c\u5c06\u7b2c\u4e8c\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u6b63\u786e\u4f4d\u7f6e\u3002
    3. \u4ee5\u6b64\u7c7b\u63a8\uff0c\u7ecf\u8fc7 \\(n - 1\\) \u8f6e\u201c\u5192\u6ce1\u201d\u540e\uff0c\u524d \\(n - 1\\) \u5927\u7684\u5143\u7d20\u90fd\u88ab\u4ea4\u6362\u81f3\u6b63\u786e\u4f4d\u7f6e\u3002
    4. \u4ec5\u5269\u7684\u4e00\u4e2a\u5143\u7d20\u5fc5\u5b9a\u662f\u6700\u5c0f\u5143\u7d20\uff0c\u65e0\u9700\u6392\u5e8f\uff0c\u56e0\u6b64\u6570\u7ec4\u6392\u5e8f\u5b8c\u6210\u3002

    Fig. \u5192\u6ce1\u6392\u5e8f\u6d41\u7a0b

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart bubble_sort.java
    /* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(int[] nums) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (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 \nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\n}\n}\n}\n}\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]\nfor (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 \nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n// \u8fd9\u91cc\u4f7f\u7528\u4e86 std::swap() \u51fd\u6570\nswap(nums[j], nums[j + 1]);\n}\n}\n}\n}\n
    bubble_sort.py
    def bubble_sort(nums: list[int]) -> None:\n\"\"\"\u5192\u6ce1\u6392\u5e8f\"\"\"\nn = len(nums)\n# \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor 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 \nfor j in range(i):\nif nums[j] > nums[j + 1]:\n# \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nnums[j], nums[j + 1] = nums[j + 1], nums[j]\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]\nfor 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\nfor j := 0; j < i; j++ {\nif nums[j] > nums[j+1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nnums[j], nums[j+1] = nums[j+1], nums[j]\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]\nfor (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 \nfor (let j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[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]\nfor (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 \nfor (let j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[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]\nfor (int i = 0; i < size - 1; 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 \nfor (int j = 0; j < size - 1 - i; j++) {\nif (nums[j] > nums[j + 1]) {\nint temp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = temp;\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]\nfor (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 \nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\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]\nfor 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 \nfor j in stride(from: 0, to: i, by: 1) {\nif nums[j] > nums[j + 1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j]\nnums[j] = nums[j + 1]\nnums[j + 1] = tmp\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]\nvar i: usize = nums.len - 1;\nwhile (i > 0) : (i -= 1) {\nvar 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 \nwhile (j < i) : (j += 1) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nvar tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[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]\nfor (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 \nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[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\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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]\nfor (int i = nums.length - 1; i > 0; i--) {\nboolean 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 \nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\nflag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif (!flag)\nbreak; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n}\n}\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]\nfor (int i = nums.size() - 1; i > 0; i--) {\nbool 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 \nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n// \u8fd9\u91cc\u4f7f\u7528\u4e86 std::swap() \u51fd\u6570\nswap(nums[j], nums[j + 1]);\nflag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif (!flag)\nbreak; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n}\n}\n
    bubble_sort.py
    def bubble_sort_with_flag(nums: list[int]) -> None:\n\"\"\"\u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09\"\"\"\nn = len(nums)\n# \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor i in range(n - 1, 0, -1):\nflag = 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 \nfor j in range(i):\nif nums[j] > nums[j + 1]:\n# \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nnums[j], nums[j + 1] = nums[j + 1], nums[j]\nflag = True  # \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\nif not flag:\nbreak  # \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\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]\nfor i := len(nums) - 1; i > 0; i-- {\nflag := 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\nfor j := 0; j < i; j++ {\nif nums[j] > nums[j+1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nnums[j], nums[j+1] = nums[j+1], nums[j]\nflag = true // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif flag == false { // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\nbreak\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]\nfor (let i = nums.length - 1; i > 0; i--) {\nlet 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 \nfor (let j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\nflag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif (!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]\nfor (let i = nums.length - 1; i > 0; i--) {\nlet 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 \nfor (let j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\nflag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif (!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]\nfor (int i = 0; i < size - 1; i++) {\nbool 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 \nfor (int j = 0; j < size - 1 - i; j++) {\nif (nums[j] > nums[j + 1]) {\nint temp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = temp;\nflag = true;\n}\n}\nif (!flag)\nbreak;\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]\nfor (int i = nums.Length - 1; i > 0; i--) {\nbool 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 \nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\nflag = true;  // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif (!flag) break;     // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\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]\nfor i in stride(from: nums.count - 1, to: 0, by: -1) {\nvar flag = false // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\nfor j in stride(from: 0, to: i, by: 1) {\nif nums[j] > nums[j + 1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j]\nnums[j] = nums[j + 1]\nnums[j + 1] = tmp\nflag = true // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif !flag { // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\nbreak\n}\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]\nvar i: usize = nums.len - 1;\nwhile (i > 0) : (i -= 1) {\nvar flag = false;   // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\nvar 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 \nwhile (j < i) : (j += 1) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nvar tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\nflag = true;\n}\n}\nif (!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]\nfor (int i = nums.length - 1; i > 0; i--) {\nbool 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 \nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\nflag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif (!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":"
    • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3001\u81ea\u9002\u5e94\u6392\u5e8f \uff1a\u5404\u8f6e\u201c\u5192\u6ce1\u201d\u904d\u5386\u7684\u6570\u7ec4\u957f\u5ea6\u4f9d\u6b21\u4e3a \\(n - 1\\) , \\(n - 2\\) , \\(\\cdots\\) , \\(2\\) , \\(1\\) \uff0c\u603b\u548c\u4e3a \\(\\frac{(n - 1) n}{2}\\) \u3002\u5728\u5f15\u5165 flag \u4f18\u5316\u540e\uff0c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe\u5230 \\(O(n)\\) \u3002
    • \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3001\u539f\u5730\u6392\u5e8f\uff1a\u6307\u9488 \\(i\\) , \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002
    • \u7a33\u5b9a\u6392\u5e8f\uff1a\u7531\u4e8e\u5728\u201c\u5192\u6ce1\u201d\u4e2d\u9047\u5230\u76f8\u7b49\u5143\u7d20\u4e0d\u4ea4\u6362\u3002
    "},{"location":"chapter_sorting/bucket_sort/","title":"11.8. \u00a0 \u6876\u6392\u5e8f","text":"

    \u524d\u8ff0\u7684\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\u6c34\u5e73\u3002

    \u300c\u6876\u6392\u5e8f Bucket Sort\u300d\u662f\u5206\u6cbb\u601d\u60f3\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\u5143\u7d20\u662f\u8303\u56f4 \\([0, 1)\\) \u7684\u6d6e\u70b9\u6570\u3002\u6876\u6392\u5e8f\u7684\u6d41\u7a0b\u5982\u4e0b\uff1a

    1. \u521d\u59cb\u5316 \\(k\\) \u4e2a\u6876\uff0c\u5c06 \\(n\\) \u4e2a\u5143\u7d20\u5206\u914d\u5230 \\(k\\) \u4e2a\u6876\u4e2d\uff1b
    2. \u5bf9\u6bcf\u4e2a\u6876\u5206\u522b\u6267\u884c\u6392\u5e8f\uff08\u672c\u6587\u91c7\u7528\u7f16\u7a0b\u8bed\u8a00\u7684\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff09\uff1b
    3. \u6309\u7167\u6876\u7684\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\uff0c\u5408\u5e76\u7ed3\u679c\uff1b

    Fig. \u6876\u6392\u5e8f\u7b97\u6cd5\u6d41\u7a0b

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nint k = nums.length / 2;\nList<List<Float>> buckets = new ArrayList<>();\nfor (int i = 0; i < k; i++) {\nbuckets.add(new ArrayList<>());\n}\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor (float num : nums) {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\nint i = (int) (num * k);\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\nbuckets.get(i).add(num);\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor (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\nCollections.sort(bucket);\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nint i = 0;\nfor (List<Float> bucket : buckets) {\nfor (float num : bucket) {\nnums[i++] = num;\n}\n}\n}\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\nint k = nums.size() / 2;\nvector<vector<float>> buckets(k);\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor (float num : nums) {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\nint i = num * k;\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 bucket_idx\nbuckets[i].push_back(num);\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor (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\nsort(bucket.begin(), bucket.end());\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nint i = 0;\nfor (vector<float> &bucket : buckets) {\nfor (float num : bucket) {\nnums[i++] = num;\n}\n}\n}\n
    bucket_sort.py
    def bucket_sort(nums: list[float]) -> None:\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\nk = len(nums) // 2\nbuckets = [[] for _ in range(k)]\n# 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor num in nums:\n# \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\ni = int(num * k)\n# \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\nbuckets[i].append(num)\n# 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f5\nfor 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\nbucket.sort()\n# 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\ni = 0\nfor bucket in buckets:\nfor num in bucket:\nnums[i] = num\ni += 1\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\nk := len(nums) / 2\nbuckets := make([][]float64, k)\nfor i := 0; i < k; i++ {\nbuckets[i] = make([]float64, 0)\n}\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor _, num := range nums {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\ni := int(num * float64(k))\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\nbuckets[i] = append(buckets[i], num)\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor 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\nsort.Float64s(buckets[i])\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\ni := 0\nfor _, bucket := range buckets {\nfor _, num := range bucket {\nnums[i] = num\ni++\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\nconst k = nums.length / 2;\nconst buckets = [];\nfor (let i = 0; i < k; i++) {\nbuckets.push([]);\n}\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor (const num of nums) {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\nconst i = Math.floor(num * k);\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\nbuckets[i].push(num);\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor (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\nbucket.sort((a, b) => a - b);\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nlet i = 0;\nfor (const bucket of buckets) {\nfor (const num of bucket) {\nnums[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\nconst k = nums.length / 2;\nconst buckets: number[][] = [];\nfor (let i = 0; i < k; i++) {\nbuckets.push([]);\n}\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor (const num of nums) {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\nconst i = Math.floor(num * k);\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\nbuckets[i].push(num);\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor (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\nbucket.sort((a, b) => a - b);\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nlet i = 0;\nfor (const bucket of buckets) {\nfor (const num of bucket) {\nnums[i++] = num;\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\nint k = size / 2;\nfloat **buckets = calloc(k, sizeof(float *));\nfor (int i = 0; i < k; i++) {\n// \u6bcf\u4e2a\u6876\u6700\u591a\u53ef\u4ee5\u5206\u914d k \u4e2a\u5143\u7d20\nbuckets[i] = calloc(ARRAY_SIZE, sizeof(float));\n}\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor (int i = 0; i < size; i++) {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\nint bucket_idx = nums[i] * k;\nint 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\nwhile (buckets[bucket_idx][j] > 0 && buckets[bucket_idx][j] < nums[i]) {\nj++;\n}\nfloat temp = nums[i];\nwhile (j < ARRAY_SIZE && buckets[bucket_idx][j] > 0) {\nswap(&temp, &buckets[bucket_idx][j]);\nj++;\n}\nbuckets[bucket_idx][j] = temp;\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor (int i = 0; i < k; i++) {\nqsort(buckets[i], ARRAY_SIZE, sizeof(float), compare_float);\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nfor (int i = 0, j = 0; j < k; j++) {\nfor (int l = 0; l < ARRAY_SIZE; l++) {\nif (buckets[j][l] > 0) {\nnums[i++] = buckets[j][l];\n}\n}\n}\n// \u91ca\u653e\u4e0a\u8ff0\u5206\u914d\u7684\u5185\u5b58\nfor (int i = 0; i < k; i++) {\nfree(buckets[i]);\n}\nfree(buckets);\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\nint k = nums.Length / 2;\nList<List<float>> buckets = new List<List<float>>();\nfor (int i = 0; i < k; i++) {\nbuckets.Add(new List<float>());\n}\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nforeach (float num in nums) {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\nint i = (int) (num * k);\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\nbuckets[i].Add(num);\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nforeach (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\nbucket.Sort();\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nint j = 0;\nforeach (List<float> bucket in buckets) {\nforeach (float num in bucket) {\nnums[j++] = num;\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\nlet k = nums.count / 2\nvar buckets = (0 ..< k).map { _ in [Double]() }\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor num in nums {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\nlet i = Int(num * k)\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\nbuckets[i].append(num)\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor 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\nbuckets[i].sort()\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nvar i = nums.startIndex\nfor bucket in buckets {\nfor num in bucket {\nnums[i] = num\nnums.formIndex(after: &i)\n}\n}\n}\n
    bucket_sort.zig
    [class]{}-[func]{bucketSort}\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\nint k = nums.length ~/ 2;\nList<List<double>> buckets = List.generate(k, (index) => []);\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor (double num in nums) {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\nint i = (num * k).toInt();\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 bucket_idx\nbuckets[i].add(num);\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor (List<double> bucket in buckets) {\nbucket.sort();\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nint i = 0;\nfor (List<double> bucket in buckets) {\nfor (double num in bucket) {\nnums[i++] = num;\n}\n}\n}\n

    \u6876\u6392\u5e8f\u7684\u9002\u7528\u573a\u666f\u662f\u4ec0\u4e48\uff1f

    \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

    "},{"location":"chapter_sorting/bucket_sort/#1182","title":"11.8.2. \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
    • \u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n + k)\\) \uff1a\u5047\u8bbe\u5143\u7d20\u5728\u5404\u4e2a\u6876\u5185\u5e73\u5747\u5206\u5e03\uff0c\u90a3\u4e48\u6bcf\u4e2a\u6876\u5185\u7684\u5143\u7d20\u6570\u91cf\u4e3a \\(\\frac{n}{k}\\) \u3002\u5047\u8bbe\u6392\u5e8f\u5355\u4e2a\u6876\u4f7f\u7528 \\(O(\\frac{n}{k} \\log\\frac{n}{k})\\) \u65f6\u95f4\uff0c\u5219\u6392\u5e8f\u6240\u6709\u6876\u4f7f\u7528 \\(O(n \\log\\frac{n}{k})\\) \u65f6\u95f4\u3002\u5f53\u6876\u6570\u91cf \\(k\\) \u6bd4\u8f83\u5927\u65f6\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5219\u8d8b\u5411\u4e8e \\(O(n)\\) \u3002\u5408\u5e76\u7ed3\u679c\u65f6\u9700\u8981\u904d\u5386 \\(n\\) \u4e2a\u6876\uff0c\u82b1\u8d39 \\(O(k)\\) \u65f6\u95f4\u3002
    • \u81ea\u9002\u5e94\u6392\u5e8f\uff1a\u5728\u6700\u574f\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u6570\u636e\u88ab\u5206\u914d\u5230\u4e00\u4e2a\u6876\u4e2d\uff0c\u4e14\u6392\u5e8f\u8be5\u6876\u4f7f\u7528 \\(O(n^2)\\) \u65f6\u95f4\u3002
    • \u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(n + k)\\) \u3001\u975e\u539f\u5730\u6392\u5e8f \uff1a\u9700\u8981\u501f\u52a9 \\(k\\) \u4e2a\u6876\u548c\u603b\u5171 \\(n\\) \u4e2a\u5143\u7d20\u7684\u989d\u5916\u7a7a\u95f4\u3002
    • \u6876\u6392\u5e8f\u662f\u5426\u7a33\u5b9a\u53d6\u51b3\u4e8e\u6392\u5e8f\u6876\u5185\u5143\u7d20\u7684\u7b97\u6cd5\u662f\u5426\u7a33\u5b9a\u3002
    "},{"location":"chapter_sorting/bucket_sort/#1183","title":"11.8.3. \u00a0 \u5982\u4f55\u5b9e\u73b0\u5e73\u5747\u5206\u914d","text":"

    \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 \u4efd\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\u4e2a\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\u8fd9\u79cd\u65b9\u6cd5\u672c\u8d28\u4e0a\u662f\u521b\u5efa\u4e00\u4e2a\u9012\u5f52\u6811\uff0c\u4f7f\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

    Fig. \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\u4e0b\u56fe\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

    Fig. \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\u3002\u8ba1\u6570\u6392\u5e8f\u7684\u6574\u4f53\u6d41\u7a0b\u5982\u4e0b\uff1a

    1. \u904d\u5386\u6570\u7ec4\uff0c\u627e\u51fa\u6570\u7ec4\u4e2d\u7684\u6700\u5927\u6570\u5b57\uff0c\u8bb0\u4e3a \\(m\\) \uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(m + 1\\) \u7684\u8f85\u52a9\u6570\u7ec4 counter \uff1b
    2. \u501f\u52a9 counter \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\u3002
    3. \u7531\u4e8e counter \u7684\u5404\u4e2a\u7d22\u5f15\u5929\u7136\u6709\u5e8f\uff0c\u56e0\u6b64\u76f8\u5f53\u4e8e\u6240\u6709\u6570\u5b57\u5df2\u7ecf\u88ab\u6392\u5e8f\u597d\u4e86\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u904d\u5386 counter \uff0c\u6839\u636e\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u5c06\u5b83\u4eec\u6309\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u586b\u5165 nums \u5373\u53ef\u3002

    Fig. \u8ba1\u6570\u6392\u5e8f\u6d41\u7a0b

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nint m = 0;\nfor (int num : nums) {\nm = 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\nint[] counter = new int[m + 1];\nfor (int num : nums) {\ncounter[num]++;\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nint i = 0;\nfor (int num = 0; num < m + 1; num++) {\nfor (int j = 0; j < counter[num]; j++, i++) {\nnums[i] = num;\n}\n}\n}\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\nint m = 0;\nfor (int num : nums) {\nm = 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\nvector<int> counter(m + 1, 0);\nfor (int num : nums) {\ncounter[num]++;\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nint i = 0;\nfor (int num = 0; num < m + 1; num++) {\nfor (int j = 0; j < counter[num]; j++, i++) {\nnums[i] = num;\n}\n}\n}\n
    counting_sort.py
    def counting_sort_naive(nums: list[int]) -> None:\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\nm = 0\nfor num in nums:\nm = 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\ncounter = [0] * (m + 1)\nfor num in nums:\ncounter[num] += 1\n# 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\ni = 0\nfor num in range(m + 1):\nfor _ in range(counter[num]):\nnums[i] = num\ni += 1\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\nm := 0\nfor _, num := range nums {\nif num > m {\nm = 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\ncounter := make([]int, m+1)\nfor _, num := range nums {\ncounter[num]++\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nfor i, num := 0, 0; num < m+1; num++ {\nfor j := 0; j < counter[num]; j++ {\nnums[i] = num\ni++\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\nlet m = 0;\nfor (const num of nums) {\nm = 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\nconst counter = new Array(m + 1).fill(0);\nfor (const num of nums) {\ncounter[num]++;\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nlet i = 0;\nfor (let num = 0; num < m + 1; num++) {\nfor (let j = 0; j < counter[num]; j++, i++) {\nnums[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\nlet m = 0;\nfor (const num of nums) {\nm = 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\nconst counter: number[] = new Array<number>(m + 1).fill(0);\nfor (const num of nums) {\ncounter[num]++;\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nlet i = 0;\nfor (let num = 0; num < m + 1; num++) {\nfor (let j = 0; j < counter[num]; j++, i++) {\nnums[i] = num;\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\nint m = 0;\nfor (int i = 0; i < size; i++) {\nif (nums[i] > m) {\nm = 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\nint *counter = malloc(sizeof(int) * m);\nfor (int i = 0; i < size; i++) {\ncounter[nums[i]]++;\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nint i = 0;\nfor (int num = 0; num < m + 1; num++) {\nfor (int j = 0; j < counter[num]; j++, i++) {\nnums[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\nint m = 0;\nforeach (int num in nums) {\nm = 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\nint[] counter = new int[m + 1];\nforeach (int num in nums) {\ncounter[num]++;\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nint i = 0;\nfor (int num = 0; num < m + 1; num++) {\nfor (int j = 0; j < counter[num]; j++, i++) {\nnums[i] = num;\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\nlet 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\nvar counter = Array(repeating: 0, count: m + 1)\nfor num in nums {\ncounter[num] += 1\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nvar i = 0\nfor num in stride(from: 0, to: m + 1, by: 1) {\nfor _ in stride(from: 0, to: counter[num], by: 1) {\nnums[i] = num\ni += 1\n}\n}\n}\n
    counting_sort.zig
    [class]{}-[func]{countingSortNaive}\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\nint m = 0;\nfor (int num in nums) {\nm = 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\nList<int> counter = List.filled(m + 1, 0);\nfor (int num in nums) {\ncounter[num]++;\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nint i = 0;\nfor (int num = 0; num < m + 1; num++) {\nfor (int j = 0; j < counter[num]; j++, i++) {\nnums[i] = num;\n}\n}\n}\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\u662f\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

    "},{"location":"chapter_sorting/counting_sort/#1192","title":"11.9.2. \u00a0 \u5b8c\u6574\u5b9e\u73b0","text":"

    \u7ec6\u5fc3\u7684\u540c\u5b66\u53ef\u80fd\u53d1\u73b0\uff0c\u5982\u679c\u8f93\u5165\u6570\u636e\u662f\u5bf9\u8c61\uff0c\u4e0a\u8ff0\u6b65\u9aa4 3. \u5c31\u5931\u6548\u4e86\u3002\u4f8b\u5982\uff0c\u8f93\u5165\u6570\u636e\u662f\u5546\u54c1\u5bf9\u8c61\uff0c\u6211\u4eec\u60f3\u8981\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\u300c\u524d\u7f00\u548c\u300d\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\uff0c\u5373

    \\[ \\text{prefix}[i] = \\sum_{j=0}^i \\text{counter[j]} \\]

    \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\uff1a

    1. \u5c06 num \u586b\u5165\u6570\u7ec4 res \u7684\u7d22\u5f15 prefix[num] - 1 \u5904\uff1b
    2. \u4ee4\u524d\u7f00\u548c prefix[num] \u51cf\u5c0f \\(1\\) \uff0c\u4ece\u800c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\uff1b

    \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

    <1><2><3><4><5><6><7><8>

    \u8ba1\u6570\u6392\u5e8f\u7684\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nint m = 0;\nfor (int num : nums) {\nm = 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\nint[] counter = new int[m + 1];\nfor (int num : nums) {\ncounter[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\nfor (int i = 0; i < m; i++) {\ncounter[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\nint n = nums.length;\nint[] res = new int[n];\nfor (int i = n - 1; i >= 0; i--) {\nint num = nums[i];\nres[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[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\nfor (int i = 0; i < n; i++) {\nnums[i] = res[i];\n}\n}\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\nint m = 0;\nfor (int num : nums) {\nm = 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\nvector<int> counter(m + 1, 0);\nfor (int num : nums) {\ncounter[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\nfor (int i = 0; i < m; i++) {\ncounter[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\nint n = nums.size();\nvector<int> res(n);\nfor (int i = n - 1; i >= 0; i--) {\nint num = nums[i];\nres[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[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\nnums = res;\n}\n
    counting_sort.py
    def counting_sort(nums: list[int]) -> None:\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\nm = 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\ncounter = [0] * (m + 1)\nfor num in nums:\ncounter[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\nfor i in range(m):\ncounter[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\nn = len(nums)\nres = [0] * n\nfor i in range(n - 1, -1, -1):\nnum = nums[i]\nres[counter[num] - 1] = num  # \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[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\nfor i in range(n):\nnums[i] = res[i]\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\nm := 0\nfor _, num := range nums {\nif num > m {\nm = 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\ncounter := make([]int, m+1)\nfor _, num := range nums {\ncounter[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\nfor i := 0; i < m; i++ {\ncounter[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\nn := len(nums)\nres := make([]int, n)\nfor i := n - 1; i >= 0; i-- {\nnum := nums[i]\n// \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\nres[counter[num]-1] = num\n// \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\ncounter[num]--\n}\n// \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\ncopy(nums, res)\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\nlet m = 0;\nfor (const num of nums) {\nm = 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\nconst counter = new Array(m + 1).fill(0);\nfor (const num of nums) {\ncounter[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\nfor (let i = 0; i < m; i++) {\ncounter[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\nconst n = nums.length;\nconst res = new Array(n);\nfor (let i = n - 1; i >= 0; i--) {\nconst num = nums[i];\nres[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[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\nfor (let i = 0; i < n; i++) {\nnums[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\nlet m = 0;\nfor (const num of nums) {\nm = 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\nconst counter: number[] = new Array<number>(m + 1).fill(0);\nfor (const num of nums) {\ncounter[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\nfor (let i = 0; i < m; i++) {\ncounter[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\nconst n = nums.length;\nconst res: number[] = new Array<number>(n);\nfor (let i = n - 1; i >= 0; i--) {\nconst num = nums[i];\nres[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[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\nfor (let i = 0; i < n; i++) {\nnums[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\nint m = 0;\nfor (int i = 0; i < size; i++) {\nif (nums[i] > m) {\nm = 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\nint *counter = malloc(sizeof(int) * m);\nfor (int i = 0; i < size; i++) {\ncounter[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\nfor (int i = 0; i < m; i++) {\ncounter[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\nint *res = malloc(sizeof(int) * size);\nfor (int i = size - 1; i >= 0; i--) {\nint num = nums[i];\nres[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[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\nmemcpy(nums, res, size * sizeof(int));\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\nint m = 0;\nforeach (int num in nums) {\nm = 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\nint[] counter = new int[m + 1];\nforeach (int num in nums) {\ncounter[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\nfor (int i = 0; i < m; i++) {\ncounter[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\nint n = nums.Length;\nint[] res = new int[n];\nfor (int i = n - 1; i >= 0; i--) {\nint num = nums[i];\nres[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[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\nfor (int i = 0; i < n; i++) {\nnums[i] = res[i];\n}\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\nlet 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\nvar counter = Array(repeating: 0, count: m + 1)\nfor num in nums {\ncounter[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\nfor i in stride(from: 0, to: m, by: 1) {\ncounter[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\nvar res = Array(repeating: 0, count: nums.count)\nfor i in stride(from: nums.count - 1, through: 0, by: -1) {\nlet num = nums[i]\nres[counter[num] - 1] = num // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[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\nfor i in stride(from: 0, to: nums.count, by: 1) {\nnums[i] = res[i]\n}\n}\n
    counting_sort.zig
    [class]{}-[func]{countingSort}\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\nint m = 0;\nfor (int num in nums) {\nm = 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\nList<int> counter = List.filled(m + 1, 0);\nfor (int num in nums) {\ncounter[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\nfor (int i = 0; i < m; i++) {\ncounter[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\nint n = nums.length;\nList<int> res = List.filled(n, 0);\nfor (int i = n - 1; i >= 0; i--) {\nint num = nums[i];\nres[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[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\nnums.setAll(0, res);\n}\n
    "},{"location":"chapter_sorting/counting_sort/#1193","title":"11.9.3. \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
    • \u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n + m)\\) \uff1a\u6d89\u53ca\u904d\u5386 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)\\) \u3002
    • \u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(n + m)\\) \u3001\u975e\u539f\u5730\u6392\u5e8f \uff1a\u501f\u52a9\u4e86\u957f\u5ea6\u5206\u522b\u4e3a \\(n\\) \u548c \\(m\\) \u7684\u6570\u7ec4 res \u548c counter \u3002
    • \u7a33\u5b9a\u6392\u5e8f\uff1a\u7531\u4e8e\u5411 res \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
    "},{"location":"chapter_sorting/counting_sort/#1194","title":"11.9.4. \u00a0 \u5c40\u9650\u6027","text":"

    \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\u5de5\u4f5c\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\u8981\u5c06\u5176\u7528\u4e8e\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u636e\uff0c\u9700\u8981\u786e\u4fdd\u8fd9\u4e9b\u6570\u636e\u53ef\u4ee5\u88ab\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\u5373\u53ef\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\u300c\u5806\u300d\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\uff1a

    1. \u8f93\u5165\u6570\u7ec4\u5e76\u5efa\u7acb\u5c0f\u9876\u5806\uff0c\u6b64\u65f6\u6700\u5c0f\u5143\u7d20\u4f4d\u4e8e\u5806\u9876\u3002
    2. \u4e0d\u65ad\u6267\u884c\u51fa\u5806\u64cd\u4f5c\uff0c\u4f9d\u6b21\u8bb0\u5f55\u51fa\u5806\u5143\u7d20\uff0c\u5373\u53ef\u5f97\u5230\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\u7684\u5e8f\u5217\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\u4e0b\uff1a

    1. \u8f93\u5165\u6570\u7ec4\u5e76\u5efa\u7acb\u5927\u9876\u5806\u3002\u5b8c\u6210\u540e\uff0c\u6700\u5927\u5143\u7d20\u4f4d\u4e8e\u5806\u9876\u3002
    2. \u5c06\u5806\u9876\u5143\u7d20\uff08\u7b2c\u4e00\u4e2a\u5143\u7d20\uff09\u4e0e\u5806\u5e95\u5143\u7d20\uff08\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff09\u4ea4\u6362\u3002\u5b8c\u6210\u4ea4\u6362\u540e\uff0c\u5806\u7684\u957f\u5ea6\u51cf \\(1\\) \uff0c\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u52a0 \\(1\\) \u3002
    3. \u4ece\u5806\u9876\u5143\u7d20\u5f00\u59cb\uff0c\u4ece\u9876\u5230\u5e95\u6267\u884c\u5806\u5316\u64cd\u4f5c\uff08Sift Down\uff09\u3002\u5b8c\u6210\u5806\u5316\u540e\uff0c\u5806\u7684\u6027\u8d28\u5f97\u5230\u4fee\u590d\u3002
    4. \u5faa\u73af\u6267\u884c\u7b2c 2. \u548c 3. \u6b65\u3002\u5faa\u73af \\(n - 1\\) \u8f6e\u540e\uff0c\u5373\u53ef\u5b8c\u6210\u6570\u7ec4\u6392\u5e8f\u3002

    \u5b9e\u9645\u4e0a\uff0c\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\u4e2d\u4e5f\u5305\u542b\u7b2c 2. \u548c 3. \u6b65\uff0c\u53ea\u662f\u591a\u4e86\u4e00\u4e2a\u5f39\u51fa\u5143\u7d20\u7684\u6b65\u9aa4\u3002

    <1><2><3><4><5><6><7><8><9><10><11><12>

    \u5728\u4ee3\u7801\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e0e\u5806\u7ae0\u8282\u76f8\u540c\u7684\u4ece\u9876\u81f3\u5e95\u5806\u5316\uff08Sift Down\uff09\u7684\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nint l = 2 * i + 1;\nint r = 2 * i + 2;\nint ma = i;\nif (l < n && nums[l] > nums[ma])\nma = l;\nif (r < n && nums[r] > nums[ma])\nma = r;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i)\nbreak;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nint temp = nums[i];\nnums[i] = nums[ma];\nnums[ma] = temp;\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\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\nfor (int i = nums.length / 2 - 1; i >= 0; i--) {\nsiftDown(nums, nums.length, i);\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor (int i = nums.length - 1; i > 0; i--) {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nint tmp = nums[0];\nnums[0] = nums[i];\nnums[i] = tmp;\n// \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\nsiftDown(nums, i, 0);\n}\n}\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) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nint l = 2 * i + 1;\nint r = 2 * i + 2;\nint ma = i;\nif (l < n && nums[l] > nums[ma])\nma = l;\nif (r < n && nums[r] > nums[ma])\nma = r;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i) {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(nums[i], nums[ma]);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\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\nfor (int i = nums.size() / 2 - 1; i >= 0; --i) {\nsiftDown(nums, nums.size(), i);\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor (int i = nums.size() - 1; i > 0; --i) {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nswap(nums[0], nums[i]);\n// \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\nsiftDown(nums, i, 0);\n}\n}\n
    heap_sort.py
    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\"\"\"\nwhile True:\n# \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nl = 2 * i + 1\nr = 2 * i + 2\nma = i\nif l < n and nums[l] > nums[ma]:\nma = l\nif r < n and nums[r] > nums[ma]:\nma = r\n# \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif ma == i:\nbreak\n# \u4ea4\u6362\u4e24\u8282\u70b9\nnums[i], nums[ma] = nums[ma], nums[i]\n# \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma\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\nfor i in range(len(nums) // 2 - 1, -1, -1):\nsift_down(nums, len(nums), i)\n# \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor i in range(len(nums) - 1, 0, -1):\n# \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nnums[0], nums[i] = nums[i], nums[0]\n# \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\nsift_down(nums, i, 0)\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) {\nfor true {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nl := 2*i + 1\nr := 2*i + 2\nma := i\nif l < n && (*nums)[l] > (*nums)[ma] {\nma = l\n}\nif r < n && (*nums)[r] > (*nums)[ma] {\nma = r\n}\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif ma == i {\nbreak\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\n(*nums)[i], (*nums)[ma] = (*nums)[ma], (*nums)[i]\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma\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\nfor i := len(*nums)/2 - 1; i >= 0; i-- {\nsiftDown(nums, len(*nums), i)\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor i := len(*nums) - 1; i > 0; i-- {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\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\nsiftDown(nums, 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) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nlet l = 2 * i + 1;\nlet r = 2 * i + 2;\nlet ma = i;\nif (l < n && nums[l] > nums[ma]) {\nma = l;\n}\nif (r < n && nums[r] > nums[ma]) {\nma = r;\n}\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma === i) {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\n[nums[i], nums[ma]] = [nums[ma], nums[i]];\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\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\nfor (let i = Math.floor(nums.length / 2) - 1; i >= 0; i--) {\nsiftDown(nums, nums.length, i);\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor (let i = nums.length - 1; i > 0; i--) {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\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\nsiftDown(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 {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nlet l = 2 * i + 1;\nlet r = 2 * i + 2;\nlet ma = i;\nif (l < n && nums[l] > nums[ma]) {\nma = l;\n}\nif (r < n && nums[r] > nums[ma]) {\nma = r;\n}\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma === i) {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\n[nums[i], nums[ma]] = [nums[ma], nums[i]];\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\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\nfor (let i = Math.floor(nums.length / 2) - 1; i >= 0; i--) {\nsiftDown(nums, nums.length, i);\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor (let i = nums.length - 1; i > 0; i--) {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\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\nsiftDown(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) {\nwhile (1) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nint l = 2 * i + 1;\nint r = 2 * i + 2;\nint ma = i;\nif (l < n && nums[l] > nums[ma])\nma = l;\nif (r < n && nums[r] > nums[ma])\nma = r;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i) {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nint temp = nums[i];\nnums[i] = nums[ma];\nnums[ma] = temp;\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\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\nfor (int i = n / 2 - 1; i >= 0; --i) {\nsiftDown(nums, n, i);\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor (int i = n - 1; i > 0; --i) {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nint tmp = nums[0];\nnums[0] = nums[i];\nnums[i] = tmp;\n// \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\nsiftDown(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) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nint l = 2 * i + 1;\nint r = 2 * i + 2;\nint ma = i;\nif (l < n && nums[l] > nums[ma])\nma = l;\nif (r < n && nums[r] > nums[ma])\nma = r;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i)\nbreak;\n// \u4ea4\u6362\u4e24\u8282\u70b9\n(nums[ma], nums[i]) = (nums[i], nums[ma]);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\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\nfor (int i = nums.Length / 2 - 1; i >= 0; i--) {\nsiftDown(nums, nums.Length, i);\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor (int i = nums.Length - 1; i > 0; i--) {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\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\nsiftDown(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) {\nvar i = i\nwhile true {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nlet l = 2 * i + 1\nlet r = 2 * i + 2\nvar ma = i\nif l < n, nums[l] > nums[ma] {\nma = l\n}\nif r < n, nums[r] > nums[ma] {\nma = r\n}\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif ma == i {\nbreak\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nnums.swapAt(i, ma)\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma\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\nfor i in stride(from: nums.count / 2 - 1, through: 0, by: -1) {\nsiftDown(nums: &nums, n: nums.count, i: i)\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor i in stride(from: nums.count - 1, to: 0, by: -1) {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nnums.swapAt(0, i)\n// \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\nsiftDown(nums: &nums, n: i, i: 0)\n}\n}\n
    heap_sort.zig
    [class]{}-[func]{siftDown}\n[class]{}-[func]{heapSort}\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) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nint l = 2 * i + 1;\nint r = 2 * i + 2;\nint ma = i;\nif (l < n && nums[l] > nums[ma]) ma = l;\nif (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\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nint temp = nums[i];\nnums[i] = nums[ma];\nnums[ma] = temp;\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\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\nfor (int i = nums.length ~/ 2 - 1; i >= 0; i--) {\nsiftDown(nums, nums.length, i);\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor (int i = nums.length - 1; i > 0; i--) {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nint tmp = nums[0];\nnums[0] = nums[i];\nnums[i] = tmp;\n// \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\nsiftDown(nums, i, 0);\n}\n}\n
    "},{"location":"chapter_sorting/heap_sort/#1172","title":"11.7.2. \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
    • \u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n \\log n)\\) \u3001\u975e\u81ea\u9002\u5e94\u6392\u5e8f \uff1a\u5efa\u5806\u64cd\u4f5c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002\u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \uff0c\u5171\u5faa\u73af \\(n - 1\\) \u8f6e\u3002
    • \u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(1)\\) \u3001\u539f\u5730\u6392\u5e8f \uff1a\u51e0\u4e2a\u6307\u9488\u53d8\u91cf\u4f7f\u7528 \\(O(1)\\) \u7a7a\u95f4\u3002\u5143\u7d20\u4ea4\u6362\u548c\u5806\u5316\u64cd\u4f5c\u90fd\u662f\u5728\u539f\u6570\u7ec4\u4e0a\u8fdb\u884c\u7684\u3002
    • \u975e\u7a33\u5b9a\u6392\u5e8f\uff1a\u5728\u4ea4\u6362\u5806\u9876\u5143\u7d20\u548c\u5806\u5e95\u5143\u7d20\u65f6\uff0c\u76f8\u7b49\u5143\u7d20\u7684\u76f8\u5bf9\u4f4d\u7f6e\u53ef\u80fd\u53d1\u751f\u53d8\u5316\u3002
    "},{"location":"chapter_sorting/insertion_sort/","title":"11.4. \u00a0 \u63d2\u5165\u6392\u5e8f","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

    \u56de\u5fc6\u6570\u7ec4\u7684\u5143\u7d20\u63d2\u5165\u64cd\u4f5c\uff0c\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\u518d\u5c06 base \u8d4b\u503c\u7ed9\u76ee\u6807\u7d22\u5f15\u3002

    Fig. \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\u4e0b\uff1a

    1. \u521d\u59cb\u72b6\u6001\u4e0b\uff0c\u6570\u7ec4\u7684\u7b2c 1 \u4e2a\u5143\u7d20\u5df2\u5b8c\u6210\u6392\u5e8f\u3002
    2. \u9009\u53d6\u6570\u7ec4\u7684\u7b2c 2 \u4e2a\u5143\u7d20\u4f5c\u4e3a base \uff0c\u5c06\u5176\u63d2\u5165\u5230\u6b63\u786e\u4f4d\u7f6e\u540e\uff0c\u6570\u7ec4\u7684\u524d 2 \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002
    3. \u9009\u53d6\u7b2c 3 \u4e2a\u5143\u7d20\u4f5c\u4e3a base \uff0c\u5c06\u5176\u63d2\u5165\u5230\u6b63\u786e\u4f4d\u7f6e\u540e\uff0c\u6570\u7ec4\u7684\u524d 3 \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002
    4. \u4ee5\u6b64\u7c7b\u63a8\uff0c\u5728\u6700\u540e\u4e00\u8f6e\u4e2d\uff0c\u9009\u53d6\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a base \uff0c\u5c06\u5176\u63d2\u5165\u5230\u6b63\u786e\u4f4d\u7f6e\u540e\uff0c\u6240\u6709\u5143\u7d20\u5747\u5df2\u6392\u5e8f\u3002

    Fig. \u63d2\u5165\u6392\u5e8f\u6d41\u7a0b

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nfor (int i = 1; i < nums.length; i++) {\nint 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\nwhile (j >= 0 && nums[j] > base) {\nnums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--;\n}\nnums[j + 1] = base;        // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n}\n}\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\nfor (int i = 1; i < nums.size(); i++) {\nint 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\nwhile (j >= 0 && nums[j] > base) {\nnums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--;\n}\nnums[j + 1] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n}\n}\n
    insertion_sort.py
    def insertion_sort(nums: list[int]) -> None:\n\"\"\"\u63d2\u5165\u6392\u5e8f\"\"\"\n# \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\nfor i in range(1, len(nums)):\nbase = nums[i]\nj = 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\nwhile j >= 0 and nums[j] > base:\nnums[j + 1] = nums[j]  # \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj -= 1\nnums[j + 1] = base  # \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\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]\nfor i := 1; i < len(nums); i++ {\nbase := nums[i]\nj := i - 1\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\nfor j >= 0 && nums[j] > base {\nnums[j+1] = nums[j] // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--\n}\nnums[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\nfor (let i = 1; i < nums.length; i++) {\nlet base = nums[i],\nj = i - 1;\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile (j >= 0 && nums[j] > base) {\nnums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--;\n}\nnums[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\nfor (let i = 1; i < nums.length; i++) {\nconst base = nums[i];\nlet j = i - 1;\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile (j >= 0 && nums[j] > base) {\nnums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--;\n}\nnums[j + 1] = 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\nfor (int i = 1; i < size; i++) {\nint 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\nwhile (j >= 0 && nums[j] > base) {\n// \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nnums[j + 1] = nums[j];\nj--;\n}\n// \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\nnums[j + 1] = base;\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\nfor (int i = 1; i < nums.Length; i++) {\nint 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\nwhile (j >= 0 && nums[j] > bas) {\nnums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--;\n}\nnums[j + 1] = bas;         // \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\nfor i in stride(from: 1, to: nums.count, by: 1) {\nlet base = nums[i]\nvar j = i - 1\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile j >= 0, nums[j] > base {\nnums[j + 1] = nums[j] // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj -= 1\n}\nnums[j + 1] = base // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\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\nvar i: usize = 1;\nwhile (i < nums.len) : (i += 1) {\nvar base = nums[i];\nvar j: usize = i;\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile (j >= 1 and nums[j - 1] > base) : (j -= 1) {\nnums[j] = nums[j - 1];  // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n}\nnums[j] = 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\nfor (int i = 1; i < nums.length; i++) {\nint 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\nwhile (j >= 0 && nums[j] > base) {\nnums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--;\n}\nnums[j + 1] = 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":"
    • \u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n^2)\\) \u3001\u81ea\u9002\u5e94\u6392\u5e8f \uff1a\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u6bcf\u6b21\u63d2\u5165\u64cd\u4f5c\u5206\u522b\u9700\u8981\u5faa\u73af \\(n - 1\\) , \\(n-2\\) , \\(\\cdots\\) , \\(2\\) , \\(1\\) \u6b21\uff0c\u6c42\u548c\u5f97\u5230 \\(\\frac{(n - 1) n}{2}\\) \uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002\u5728\u9047\u5230\u6709\u5e8f\u6570\u636e\u65f6\uff0c\u63d2\u5165\u64cd\u4f5c\u4f1a\u63d0\u524d\u7ec8\u6b62\u3002\u5f53\u8f93\u5165\u6570\u7ec4\u5b8c\u5168\u6709\u5e8f\u65f6\uff0c\u63d2\u5165\u6392\u5e8f\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u3002
    • \u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(1)\\) \u3001\u539f\u5730\u6392\u5e8f \uff1a\u6307\u9488 \\(i\\) , \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002
    • \u7a33\u5b9a\u6392\u5e8f\uff1a\u5728\u63d2\u5165\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f1a\u5c06\u5143\u7d20\u63d2\u5165\u5230\u76f8\u7b49\u5143\u7d20\u7684\u53f3\u4fa7\uff0c\u4e0d\u4f1a\u6539\u53d8\u5b83\u4eec\u7684\u987a\u5e8f\u3002
    "},{"location":"chapter_sorting/insertion_sort/#1143","title":"11.4.3. \u00a0 \u63d2\u5165\u6392\u5e8f\u4f18\u52bf","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\u76f8\u6bd4\u5feb\u901f\u6392\u5e8f\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\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\u4f5c\u7528\uff1b\u6bcf\u8f6e\u4e2d\u7684\u5355\u5143\u8ba1\u7b97\u64cd\u4f5c\u6570\u91cf\u8d77\u5230\u51b3\u5b9a\u6027\u56e0\u7d20\u3002

    \u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\uff08\u4f8b\u5982 Java\uff09\u7684\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\u90fd\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\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\u3002\u8fd9\u662f\u56e0\u4e3a\uff1a

    • \u5192\u6ce1\u6392\u5e8f\u57fa\u4e8e\u5143\u7d20\u4ea4\u6362\u5b9e\u73b0\uff0c\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf\uff0c\u5171\u6d89\u53ca 3 \u4e2a\u5355\u5143\u64cd\u4f5c\uff1b\u63d2\u5165\u6392\u5e8f\u57fa\u4e8e\u5143\u7d20\u8d4b\u503c\u5b9e\u73b0\uff0c\u4ec5\u9700 1 \u4e2a\u5355\u5143\u64cd\u4f5c\u3002\u56e0\u6b64\uff0c\u5192\u6ce1\u6392\u5e8f\u7684\u8ba1\u7b97\u5f00\u9500\u901a\u5e38\u6bd4\u63d2\u5165\u6392\u5e8f\u66f4\u9ad8\u3002
    • \u9009\u62e9\u6392\u5e8f\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n^2)\\) \u3002\u5982\u679c\u7ed9\u5b9a\u4e00\u7ec4\u90e8\u5206\u6709\u5e8f\u7684\u6570\u636e\uff0c\u63d2\u5165\u6392\u5e8f\u901a\u5e38\u6bd4\u9009\u62e9\u6392\u5e8f\u6548\u7387\u66f4\u9ad8\u3002
    • \u9009\u62e9\u6392\u5e8f\u4e0d\u7a33\u5b9a\uff0c\u65e0\u6cd5\u5e94\u7528\u4e8e\u591a\u7ea7\u6392\u5e8f\u3002
    "},{"location":"chapter_sorting/merge_sort/","title":"11.6. \u00a0 \u5f52\u5e76\u6392\u5e8f","text":"

    \u300c\u5f52\u5e76\u6392\u5e8f Merge Sort\u300d\u57fa\u4e8e\u5206\u6cbb\u601d\u60f3\u5b9e\u73b0\u6392\u5e8f\uff0c\u5305\u542b\u201c\u5212\u5206\u201d\u548c\u201c\u5408\u5e76\u201d\u4e24\u4e2a\u9636\u6bb5\uff1a

    1. \u5212\u5206\u9636\u6bb5\uff1a\u901a\u8fc7\u9012\u5f52\u4e0d\u65ad\u5730\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5904\u5206\u5f00\uff0c\u5c06\u957f\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\u8f6c\u6362\u4e3a\u77ed\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\uff1b
    2. \u5408\u5e76\u9636\u6bb5\uff1a\u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u5212\u5206\uff0c\u5f00\u59cb\u5408\u5e76\uff0c\u6301\u7eed\u5730\u5c06\u5de6\u53f3\u4e24\u4e2a\u8f83\u77ed\u7684\u6709\u5e8f\u6570\u7ec4\u5408\u5e76\u4e3a\u4e00\u4e2a\u8f83\u957f\u7684\u6709\u5e8f\u6570\u7ec4\uff0c\u76f4\u81f3\u7ed3\u675f\uff1b

    Fig. \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":"

    \u201c\u5212\u5206\u9636\u6bb5\u201d\u4ece\u9876\u81f3\u5e95\u9012\u5f52\u5730\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5207\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u76f4\u81f3\u957f\u5ea6\u4e3a 1 \uff1b

    1. \u8ba1\u7b97\u6570\u7ec4\u4e2d\u70b9 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\uff1b
    2. \u9012\u5f52\u6267\u884c\u6b65\u9aa4 1. \uff0c\u76f4\u81f3\u5b50\u6570\u7ec4\u533a\u95f4\u957f\u5ea6\u4e3a 1 \u65f6\uff0c\u7ec8\u6b62\u9012\u5f52\u5212\u5206\uff1b

    \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>

    \u89c2\u5bdf\u53d1\u73b0\uff0c\u5f52\u5e76\u6392\u5e8f\u7684\u9012\u5f52\u987a\u5e8f\u4e0e\u4e8c\u53c9\u6811\u7684\u540e\u5e8f\u904d\u5386\u76f8\u540c\uff0c\u5177\u4f53\u6765\u770b\uff1a

    • \u540e\u5e8f\u904d\u5386\uff1a\u5148\u9012\u5f52\u5de6\u5b50\u6811\uff0c\u518d\u9012\u5f52\u53f3\u5b50\u6811\uff0c\u6700\u540e\u5904\u7406\u6839\u8282\u70b9\u3002
    • \u5f52\u5e76\u6392\u5e8f\uff1a\u5148\u9012\u5f52\u5de6\u5b50\u6570\u7ec4\uff0c\u518d\u9012\u5f52\u53f3\u5b50\u6570\u7ec4\uff0c\u6700\u540e\u5904\u7406\u5408\u5e76\u3002
    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart merge_sort.java
    /* \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]\nvoid merge(int[] nums, int left, int mid, int right) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nint[] tmp = Arrays.copyOfRange(nums, left, right + 1);\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nint leftStart = left - left, leftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nint rightStart = mid + 1 - left, rightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nint i = leftStart, j = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (int k = left; k <= right; k++) {\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++\nif (i > leftEnd)\nnums[k] = tmp[j++];\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++\nelse if (j > rightEnd || tmp[i] <= tmp[j])\nnums[k] = tmp[i++];\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++\nelse\nnums[k] = tmp[j++];\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(int[] nums, int left, int right) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left >= right)\nreturn;                      // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nint mid = (left + right) / 2;    // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid);      // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n
    merge_sort.cpp
    /* \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]\nvoid merge(vector<int> &nums, int left, int mid, int right) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nvector<int> tmp(nums.begin() + left, nums.begin() + right + 1);\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nint leftStart = left - left, leftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nint rightStart = mid + 1 - left, rightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nint i = leftStart, j = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (int k = left; k <= right; k++) {\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++\nif (i > leftEnd)\nnums[k] = tmp[j++];\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++\nelse if (j > rightEnd || tmp[i] <= tmp[j])\nnums[k] = tmp[i++];\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++\nelse\nnums[k] = tmp[j++];\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(vector<int> &nums, int left, int right) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left >= right)\nreturn; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nint mid = (left + right) / 2;    // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid);      // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n
    merge_sort.py
    def merge(nums: list[int], left: int, mid: int, right: int) -> None:\n\"\"\"\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]\n# \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\ntmp = list(nums[left : right + 1])\n# \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nleft_start = 0\nleft_end = mid - left\n# \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nright_start = mid + 1 - left\nright_end = right - left\n# i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\ni = left_start\nj = right_start\n# \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor k in range(left, right + 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++\nif i > left_end:\nnums[k] = tmp[j]\nj += 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++\nelif j > right_end or tmp[i] <= tmp[j]:\nnums[k] = tmp[i]\ni += 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++\nelse:\nnums[k] = tmp[j]\nj += 1\ndef merge_sort(nums: list[int], left: int, right: int) -> None:\n\"\"\"\u5f52\u5e76\u6392\u5e8f\"\"\"\n# \u7ec8\u6b62\u6761\u4ef6\nif left >= right:\nreturn  # \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n# \u5212\u5206\u9636\u6bb5\nmid = (left + right) // 2  # \u8ba1\u7b97\u4e2d\u70b9\nmerge_sort(nums, left, mid)  # \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmerge_sort(nums, mid + 1, right)  # \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n# \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right)\n
    merge_sort.go
    /* \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]\nfunc merge(nums []int, left, mid, right int) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4 \u501f\u52a9 copy \u6a21\u5757\ntmp := make([]int, right-left+1)\nfor i := left; i <= right; i++ {\ntmp[i-left] = nums[i]\n}\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nleftStart, leftEnd := left-left, mid-left\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nrightStart, rightEnd := mid+1-left, right-left\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\ni, j := leftStart, rightStart\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor k := left; k <= right; k++ {\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++\nif i > leftEnd {\nnums[k] = tmp[j]\nj++\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 || tmp[i] <= tmp[j] {\nnums[k] = tmp[i]\ni++\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 {\nnums[k] = tmp[j]\nj++\n}\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nfunc mergeSort(nums []int, left, right int) {\n// \u7ec8\u6b62\u6761\u4ef6\nif left >= right {\nreturn\n}\n// \u5212\u5206\u9636\u6bb5\nmid := (left + right) / 2\nmergeSort(nums, left, mid)\nmergeSort(nums, mid+1, right)\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right)\n}\n
    merge_sort.js
    /* \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]\nfunction merge(nums, left, mid, right) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nlet tmp = nums.slice(left, right + 1);\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet leftStart = left - left,\nleftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet rightStart = mid + 1 - left,\nrightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nlet i = leftStart,\nj = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (let k = left; k <= right; k++) {\nif (i > leftEnd) {\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++\nnums[k] = tmp[j++];\n} else if (j > rightEnd || tmp[i] <= tmp[j]) {\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++\nnums[k] = tmp[i++];\n} else {\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++\nnums[k] = tmp[j++];\n}\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nfunction mergeSort(nums, left, right) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nlet mid = Math.floor((left + right) / 2); // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n
    merge_sort.ts
    /* \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]\nfunction merge(nums: number[], left: number, mid: number, right: number): void {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nlet tmp = nums.slice(left, right + 1);\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet leftStart = left - left,\nleftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet rightStart = mid + 1 - left,\nrightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nlet i = leftStart,\nj = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (let k = left; k <= right; k++) {\nif (i > leftEnd) {\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++\nnums[k] = tmp[j++];\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 || tmp[i] <= tmp[j]) {\nnums[k] = tmp[i++];\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 {\nnums[k] = tmp[j++];\n}\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nfunction mergeSort(nums: number[], left: number, right: number): void {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nlet mid = Math.floor((left + right) / 2); // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n
    merge_sort.c
    /* \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]\nvoid merge(int *nums, int left, int mid, int right) {\nint index;\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nint tmp[right + 1 - left];\nfor (index = left; index < right + 1; index++) {\ntmp[index - left] = nums[index];\n}\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nint leftStart = left - left, leftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nint rightStart = mid + 1 - left, rightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nint i = leftStart, j = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (int k = left; k <= right; k++) {\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++\nif (i > leftEnd)\nnums[k] = tmp[j++];\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++\nelse if (j > rightEnd || tmp[i] <= tmp[j])\nnums[k] = tmp[i++];\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++\nelse\nnums[k] = tmp[j++];\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(int *nums, int left, int right) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left >= right)\nreturn; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nint mid = (left + right) / 2;    // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid);      // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n
    merge_sort.cs
    /* \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]\nvoid merge(int[] nums, int left, int mid, int right) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nint[] tmp = nums[left..(right + 1)];\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15  \nint leftStart = left - left, leftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15       \nint rightStart = mid + 1 - left, rightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nint i = leftStart, j = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (int k = left; k <= right; k++) {\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++\nif (i > leftEnd)\nnums[k] = tmp[j++];\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++\nelse if (j > rightEnd || tmp[i] <= tmp[j])\nnums[k] = tmp[i++];\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++\nelse\nnums[k] = tmp[j++];\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(int[] nums, int left, int right) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left >= right) return;       // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nint mid = (left + right) / 2;    // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid);      // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n
    merge_sort.swift
    /* \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]\nfunc merge(nums: inout [Int], left: Int, mid: Int, right: Int) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nlet tmp = Array(nums[left ..< (right + 1)])\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet leftStart = left - left\nlet leftEnd = mid - left\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet rightStart = mid + 1 - left\nlet rightEnd = right - left\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nvar i = leftStart\nvar j = rightStart\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor k in left ... right {\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++\nif i > leftEnd {\nnums[k] = tmp[j]\nj += 1\n}\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++\nelse if j > rightEnd || tmp[i] <= tmp[j] {\nnums[k] = tmp[i]\ni += 1\n}\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++\nelse {\nnums[k] = tmp[j]\nj += 1\n}\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nfunc mergeSort(nums: inout [Int], left: Int, right: Int) {\n// \u7ec8\u6b62\u6761\u4ef6\nif left >= right { // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nreturn\n}\n// \u5212\u5206\u9636\u6bb5\nlet mid = (left + right) / 2 // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums: &nums, left: left, right: mid) // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums: &nums, left: mid + 1, right: right) // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums: &nums, left: left, mid: mid, right: 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\nvar mem_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);\ndefer mem_arena.deinit();\nconst mem_allocator = mem_arena.allocator();\nvar tmp = try mem_allocator.alloc(i32, right + 1 - left);\nstd.mem.copy(i32, tmp, nums[left..right+1]);\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15  \nvar leftStart = left - left;\nvar leftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15       \nvar rightStart = mid + 1 - left;\nvar rightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nvar i = leftStart;\nvar j = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nvar k = left;\nwhile (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++\nif (i > leftEnd) {\nnums[k] = tmp[j];\nj += 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]) {\nnums[k] = tmp[i];\ni += 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 {\nnums[k] = tmp[j];\nj += 1;\n}\n}\n}\n// \u5f52\u5e76\u6392\u5e8f\nfn mergeSort(nums: []i32, left: usize, right: usize) !void {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left >= right) return;              // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nvar mid = (left + right) / 2;           // \u8ba1\u7b97\u4e2d\u70b9\ntry mergeSort(nums, left, mid);         // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\ntry mergeSort(nums, mid + 1, right);    // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\ntry merge(nums, left, mid, right);\n}\n
    merge_sort.dart
    /* \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]\nvoid merge(List<int> nums, int left, int mid, int right) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nList<int> tmp = nums.sublist(left, right + 1);\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nint leftStart = left - left, leftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nint rightStart = mid + 1 - left, rightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nint i = leftStart, j = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (int k = left; k <= right; k++) {\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++\nif (i > leftEnd)\nnums[k] = tmp[j++];\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++\nelse if (j > rightEnd || tmp[i] <= tmp[j])\nnums[k] = tmp[i++];\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++\nelse\nnums[k] = tmp[j++];\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(List<int> nums, int left, int right) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nint mid = (left + right) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n

    \u5408\u5e76\u65b9\u6cd5 merge() \u4ee3\u7801\u4e2d\u7684\u96be\u70b9\u5305\u62ec\uff1a

    • \u5728\u9605\u8bfb\u4ee3\u7801\u65f6\uff0c\u9700\u8981\u7279\u522b\u6ce8\u610f\u5404\u4e2a\u53d8\u91cf\u7684\u542b\u4e49\u3002nums \u7684\u5f85\u5408\u5e76\u533a\u95f4\u4e3a [left, right] \uff0c\u4f46\u7531\u4e8e tmp \u4ec5\u590d\u5236\u4e86 nums \u8be5\u533a\u95f4\u7684\u5143\u7d20\uff0c\u56e0\u6b64 tmp \u5bf9\u5e94\u533a\u95f4\u4e3a [0, right - left] \u3002
    • \u5728\u6bd4\u8f83 tmp[i] \u548c tmp[j] \u7684\u5927\u5c0f\u65f6\uff0c\u8fd8\u9700\u8003\u8651\u5b50\u6570\u7ec4\u904d\u5386\u5b8c\u6210\u540e\u7684\u7d22\u5f15\u8d8a\u754c\u95ee\u9898\uff0c\u5373 i > leftEnd \u548c j > rightEnd \u7684\u60c5\u51b5\u3002\u7d22\u5f15\u8d8a\u754c\u7684\u4f18\u5148\u7ea7\u662f\u6700\u9ad8\u7684\uff0c\u5982\u679c\u5de6\u5b50\u6570\u7ec4\u5df2\u7ecf\u88ab\u5408\u5e76\u5b8c\u4e86\uff0c\u90a3\u4e48\u4e0d\u9700\u8981\u7ee7\u7eed\u6bd4\u8f83\uff0c\u76f4\u63a5\u5408\u5e76\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u5373\u53ef\u3002
    "},{"location":"chapter_sorting/merge_sort/#1162","title":"11.6.2. \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
    • \u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n \\log n)\\) \u3001\u975e\u81ea\u9002\u5e94\u6392\u5e8f \uff1a\u5212\u5206\u4ea7\u751f\u9ad8\u5ea6\u4e3a \\(\\log n\\) \u7684\u9012\u5f52\u6811\uff0c\u6bcf\u5c42\u5408\u5e76\u7684\u603b\u64cd\u4f5c\u6570\u91cf\u4e3a \\(n\\) \uff0c\u56e0\u6b64\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002
    • \u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u3001\u975e\u539f\u5730\u6392\u5e8f \uff1a\u9012\u5f52\u6df1\u5ea6\u4e3a \\(\\log n\\) \uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u5927\u5c0f\u7684\u6808\u5e27\u7a7a\u95f4\u3002\u5408\u5e76\u64cd\u4f5c\u9700\u8981\u501f\u52a9\u8f85\u52a9\u6570\u7ec4\u5b9e\u73b0\uff0c\u4f7f\u7528 \\(O(n)\\) \u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002
    • \u7a33\u5b9a\u6392\u5e8f\uff1a\u5728\u5408\u5e76\u8fc7\u7a0b\u4e2d\uff0c\u76f8\u7b49\u5143\u7d20\u7684\u6b21\u5e8f\u4fdd\u6301\u4e0d\u53d8\u3002
    "},{"location":"chapter_sorting/merge_sort/#1163","title":"11.6.3. \u00a0 \u94fe\u8868\u6392\u5e8f *","text":"

    \u5f52\u5e76\u6392\u5e8f\u5728\u6392\u5e8f\u94fe\u8868\u65f6\u5177\u6709\u663e\u8457\u4f18\u52bf\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u4f18\u5316\u81f3 \\(O(1)\\) \uff0c\u539f\u56e0\u5982\u4e0b\uff1a

    • \u7531\u4e8e\u94fe\u8868\u4ec5\u9700\u6539\u53d8\u6307\u9488\u5c31\u53ef\u5b9e\u73b0\u8282\u70b9\u7684\u589e\u5220\u64cd\u4f5c\uff0c\u56e0\u6b64\u5408\u5e76\u9636\u6bb5\uff08\u5c06\u4e24\u4e2a\u77ed\u6709\u5e8f\u94fe\u8868\u5408\u5e76\u4e3a\u4e00\u4e2a\u957f\u6709\u5e8f\u94fe\u8868\uff09\u65e0\u9700\u521b\u5efa\u8f85\u52a9\u94fe\u8868\u3002
    • \u901a\u8fc7\u4f7f\u7528\u201c\u8fed\u4ee3\u5212\u5206\u201d\u66ff\u4ee3\u201c\u9012\u5f52\u5212\u5206\u201d\uff0c\u53ef\u7701\u53bb\u9012\u5f52\u4f7f\u7528\u7684\u6808\u5e27\u7a7a\u95f4\uff1b

    \u5177\u4f53\u5b9e\u73b0\u7ec6\u8282\u6bd4\u8f83\u590d\u6742\uff0c\u6709\u5174\u8da3\u7684\u540c\u5b66\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\u601d\u60f3\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\u300c\u54e8\u5175\u5212\u5206\u300d\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\u4e3a\uff1a

    1. \u9009\u53d6\u6570\u7ec4\u6700\u5de6\u7aef\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6\u6570\uff0c\u521d\u59cb\u5316\u4e24\u4e2a\u6307\u9488 i \u548c j \u5206\u522b\u6307\u5411\u6570\u7ec4\u7684\u4e24\u7aef\uff1b
    2. \u8bbe\u7f6e\u4e00\u4e2a\u5faa\u73af\uff0c\u5728\u6bcf\u8f6e\u4e2d\u4f7f\u7528 i\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\uff1b
    3. \u5faa\u73af\u6267\u884c\u6b65\u9aa4 2. \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\uff1b

    \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

    <1><2><3><4><5><6><7><8><9>

    \u5feb\u901f\u6392\u5e8f\u7684\u5206\u6cbb\u601d\u60f3

    \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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart quick_sort.java
    /* \u5143\u7d20\u4ea4\u6362 */\nvoid swap(int[] nums, int i, int j) {\nint tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n/* \u54e8\u5175\u5212\u5206 */\nint partition(int[] nums, int left, int right) {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left])\nj--;          // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j && nums[i] <= nums[left])\ni++;          // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left);  // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i;             // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
    quick_sort.cpp
    /* \u5143\u7d20\u4ea4\u6362 */\nvoid swap(vector<int> &nums, int i, int j) {\nint tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n/* \u54e8\u5175\u5212\u5206 */\nint partition(vector<int> &nums, int left, int right) {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left])\nj--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j && nums[i] <= nums[left])\ni++;          // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i;            // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
    quick_sort.py
    def partition(self, nums: list[int], left: int, right: int) -> int:\n\"\"\"\u54e8\u5175\u5212\u5206\"\"\"\n# \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\ni, j = left, right\nwhile i < j:\nwhile i < j and nums[j] >= nums[left]:\nj -= 1  # \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile i < j and nums[i] <= nums[left]:\ni += 1  # \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n# \u5143\u7d20\u4ea4\u6362\nnums[i], nums[j] = nums[j], nums[i]\n# \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nnums[i], nums[left] = nums[left], nums[i]\nreturn i  # \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n
    quick_sort.go
    /* \u54e8\u5175\u5212\u5206 */\nfunc (q *quickSort) partition(nums []int, left, right int) int {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\ni, j := left, right\nfor i < j {\nfor i < j && nums[j] >= nums[left] {\nj-- // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nfor i < j && nums[i] <= nums[left] {\ni++ // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\n// \u5143\u7d20\u4ea4\u6362\nnums[i], nums[j] = nums[j], nums[i]\n}\n// \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nnums[i], nums[left] = nums[left], nums[i]\nreturn i // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
    quick_sort.js
    /* \u5143\u7d20\u4ea4\u6362 */\nswap(nums, i, j) {\nlet tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n/* \u54e8\u5175\u5212\u5206 */\npartition(nums, left, right) {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nlet i = left,\nj = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left]) {\nj -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nwhile (i < j && nums[i] <= nums[left]) {\ni += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\n// \u5143\u7d20\u4ea4\u6362\nthis.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nthis.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn 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 {\nlet tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n/* \u54e8\u5175\u5212\u5206 */\npartition(nums: number[], left: number, right: number): number {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nlet i = left,\nj = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left]) {\nj -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nwhile (i < j && nums[i] <= nums[left]) {\ni += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\n// \u5143\u7d20\u4ea4\u6362\nthis.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nthis.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn 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) {\nint tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\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] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left]) {\n// \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nj--;\n}\nwhile (i < j && nums[i] <= nums[left]) {\n// \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\ni++;\n}\n// \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\nswap(nums, i, j);\n}\n// \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nswap(nums, i, left);\n// \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\nreturn i;\n}\n
    quick_sort.cs
    /* \u5143\u7d20\u4ea4\u6362 */\nvoid swap(int[] nums, int i, int j) {\nint tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n/* \u54e8\u5175\u5212\u5206 */\nint partition(int[] nums, int left, int right) {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left])\nj--;          // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j && nums[i] <= nums[left])\ni++;          // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left);  // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn 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) {\nlet tmp = nums[i]\nnums[i] = nums[j]\nnums[j] = tmp\n}\n/* \u54e8\u5175\u5212\u5206 */\nfunc partition(nums: inout [Int], left: Int, right: Int) -> Int {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nvar i = left\nvar j = right\nwhile i < j {\nwhile i < j, nums[j] >= nums[left] {\nj -= 1 // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nwhile i < j, nums[i] <= nums[left] {\ni += 1 // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nswap(nums: &nums, i: i, j: j) // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums: &nums, i: i, j: left) // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
    quick_sort.zig
    // \u5143\u7d20\u4ea4\u6362\nfn swap(nums: []i32, i: usize, j: usize) void {\nvar tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n// \u54e8\u5175\u5212\u5206\nfn partition(nums: []i32, left: usize, right: usize) usize {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nvar i = left;\nvar j = right;\nwhile (i < j) {\nwhile (i < j and nums[j] >= nums[left]) j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j and nums[i] <= nums[left]) i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j);   // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left);    // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn 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) {\nint tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n/* \u54e8\u5175\u5212\u5206 */\nint _partition(List<int> nums, int left, int right) {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left]) j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (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\nreturn 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":"
    1. \u9996\u5148\uff0c\u5bf9\u539f\u6570\u7ec4\u6267\u884c\u4e00\u6b21\u300c\u54e8\u5175\u5212\u5206\u300d\uff0c\u5f97\u5230\u672a\u6392\u5e8f\u7684\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\uff1b
    2. \u7136\u540e\uff0c\u5bf9\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u5206\u522b\u9012\u5f52\u6267\u884c\u300c\u54e8\u5175\u5212\u5206\u300d\uff1b
    3. \u6301\u7eed\u9012\u5f52\uff0c\u76f4\u81f3\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\uff0c\u4ece\u800c\u5b8c\u6210\u6574\u4e2a\u6570\u7ec4\u7684\u6392\u5e8f\uff1b

    Fig. \u5feb\u901f\u6392\u5e8f\u6d41\u7a0b

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nif (left >= right)\nreturn;\n// \u54e8\u5175\u5212\u5206\nint pivot = partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums, left, pivot - 1);\nquickSort(nums, pivot + 1, right);\n}\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\nif (left >= right)\nreturn;\n// \u54e8\u5175\u5212\u5206\nint pivot = partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums, left, pivot - 1);\nquickSort(nums, pivot + 1, right);\n}\n
    quick_sort.py
    def quick_sort(self, nums: list[int], left: int, right: int) -> None:\n\"\"\"\u5feb\u901f\u6392\u5e8f\"\"\"\n# \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nif left >= right:\nreturn\n# \u54e8\u5175\u5212\u5206\npivot = self.partition(nums, left, right)\n# \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nself.quick_sort(nums, left, pivot - 1)\nself.quick_sort(nums, pivot + 1, right)\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\nif left >= right {\nreturn\n}\n// \u54e8\u5175\u5212\u5206\npivot := q.partition(nums, left, right)\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nq.quickSort(nums, left, pivot-1)\nq.quickSort(nums, pivot+1, 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\nif (left >= right) return;\n// \u54e8\u5175\u5212\u5206\nconst pivot = this.partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nthis.quickSort(nums, left, pivot - 1);\nthis.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\nif (left >= right) {\nreturn;\n}\n// \u54e8\u5175\u5212\u5206\nconst pivot = this.partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nthis.quickSort(nums, left, pivot - 1);\nthis.quickSort(nums, pivot + 1, right);\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] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left]) {\n// \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nj--;\n}\nwhile (i < j && nums[i] <= nums[left]) {\n// \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\ni++;\n}\n// \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\nswap(nums, i, j);\n}\n// \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nswap(nums, i, left);\n// \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\nreturn i;\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\nif (left >= right) {\nreturn;\n}\n// \u54e8\u5175\u5212\u5206\nint pivot = partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums, left, pivot - 1);\nquickSort(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\nif (left >= right)\nreturn;\n// \u54e8\u5175\u5212\u5206\nint pivot = partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums, left, pivot - 1);\nquickSort(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\nif left >= right {\nreturn\n}\n// \u54e8\u5175\u5212\u5206\nlet pivot = partition(nums: &nums, left: left, right: right)\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums: &nums, left: left, right: pivot - 1)\nquickSort(nums: &nums, left: pivot + 1, right: 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\nif (left >= right) return;\n// \u54e8\u5175\u5212\u5206\nvar pivot = partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums, left, pivot - 1);\nquickSort(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\nif (left >= right) return;\n// \u54e8\u5175\u5212\u5206\nint pivot = _partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums, left, pivot - 1);\nquickSort(nums, pivot + 1, right);\n}\n
    "},{"location":"chapter_sorting/quick_sort/#1152","title":"11.5.2. \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
    • \u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n \\log n)\\) \u3001\u81ea\u9002\u5e94\u6392\u5e8f \uff1a\u5728\u5e73\u5747\u60c5\u51b5\u4e0b\uff0c\u54e8\u5175\u5212\u5206\u7684\u9012\u5f52\u5c42\u6570\u4e3a \\(\\log n\\) \uff0c\u6bcf\u5c42\u4e2d\u7684\u603b\u5faa\u73af\u6570\u4e3a \\(n\\) \uff0c\u603b\u4f53\u4f7f\u7528 \\(O(n \\log n)\\) \u65f6\u95f4\u3002\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u6bcf\u8f6e\u54e8\u5175\u5212\u5206\u64cd\u4f5c\u90fd\u5c06\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\u5212\u5206\u4e3a\u957f\u5ea6\u4e3a \\(0\\) \u548c \\(n - 1\\) \u7684\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u6b64\u65f6\u9012\u5f52\u5c42\u6570\u8fbe\u5230 \\(n\\) \u5c42\uff0c\u6bcf\u5c42\u4e2d\u7684\u5faa\u73af\u6570\u4e3a \\(n\\) \uff0c\u603b\u4f53\u4f7f\u7528 \\(O(n^2)\\) \u65f6\u95f4\u3002
    • \u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u3001\u539f\u5730\u6392\u5e8f \uff1a\u5728\u8f93\u5165\u6570\u7ec4\u5b8c\u5168\u5012\u5e8f\u7684\u60c5\u51b5\u4e0b\uff0c\u8fbe\u5230\u6700\u5dee\u9012\u5f52\u6df1\u5ea6 \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002\u6392\u5e8f\u64cd\u4f5c\u662f\u5728\u539f\u6570\u7ec4\u4e0a\u8fdb\u884c\u7684\uff0c\u672a\u501f\u52a9\u989d\u5916\u6570\u7ec4\u3002
    • \u975e\u7a33\u5b9a\u6392\u5e8f\uff1a\u5728\u54e8\u5175\u5212\u5206\u7684\u6700\u540e\u4e00\u6b65\uff0c\u57fa\u51c6\u6570\u53ef\u80fd\u4f1a\u88ab\u4ea4\u6362\u81f3\u76f8\u7b49\u5143\u7d20\u7684\u53f3\u4fa7\u3002
    "},{"location":"chapter_sorting/quick_sort/#1153","title":"11.5.3. \u00a0 \u5feb\u6392\u4e3a\u4ec0\u4e48\u5feb\uff1f","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\u300c\u5f52\u5e76\u6392\u5e8f\u300d\u548c\u300c\u5806\u6392\u5e8f\u300d\u76f8\u540c\uff0c\u4f46\u901a\u5e38\u5feb\u901f\u6392\u5e8f\u7684\u6548\u7387\u66f4\u9ad8\uff0c\u539f\u56e0\u5982\u4e0b\uff1a

    • \u51fa\u73b0\u6700\u5dee\u60c5\u51b5\u7684\u6982\u7387\u5f88\u4f4e\uff1a\u867d\u7136\u5feb\u901f\u6392\u5e8f\u7684\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u6ca1\u6709\u5f52\u5e76\u6392\u5e8f\u7a33\u5b9a\uff0c\u4f46\u5728\u7edd\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5feb\u901f\u6392\u5e8f\u80fd\u5728 \\(O(n \\log n)\\) \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e0b\u8fd0\u884c\u3002
    • \u7f13\u5b58\u4f7f\u7528\u6548\u7387\u9ad8\uff1a\u5728\u6267\u884c\u54e8\u5175\u5212\u5206\u64cd\u4f5c\u65f6\uff0c\u7cfb\u7edf\u53ef\u5c06\u6574\u4e2a\u5b50\u6570\u7ec4\u52a0\u8f7d\u5230\u7f13\u5b58\uff0c\u56e0\u6b64\u8bbf\u95ee\u5143\u7d20\u7684\u6548\u7387\u8f83\u9ad8\u3002\u800c\u50cf\u300c\u5806\u6392\u5e8f\u300d\u8fd9\u7c7b\u7b97\u6cd5\u9700\u8981\u8df3\u8dc3\u5f0f\u8bbf\u95ee\u5143\u7d20\uff0c\u4ece\u800c\u7f3a\u4e4f\u8fd9\u4e00\u7279\u6027\u3002
    • \u590d\u6742\u5ea6\u7684\u5e38\u6570\u7cfb\u6570\u4f4e\uff1a\u5728\u4e0a\u8ff0\u4e09\u79cd\u7b97\u6cd5\u4e2d\uff0c\u5feb\u901f\u6392\u5e8f\u7684\u6bd4\u8f83\u3001\u8d4b\u503c\u3001\u4ea4\u6362\u7b49\u64cd\u4f5c\u7684\u603b\u6570\u91cf\u6700\u5c11\u3002\u8fd9\u4e0e\u300c\u63d2\u5165\u6392\u5e8f\u300d\u6bd4\u300c\u5192\u6ce1\u6392\u5e8f\u300d\u66f4\u5feb\u7684\u539f\u56e0\u7c7b\u4f3c\u3002
    "},{"location":"chapter_sorting/quick_sort/#1154","title":"11.5.4. \u00a0 \u57fa\u51c6\u6570\u4f18\u5316","text":"

    \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\u300c\u5192\u6ce1\u6392\u5e8f\u300d\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nif ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))\nreturn left;\nelse if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\nreturn mid;\nelse\nreturn right;\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\nint med = medianThree(nums, left, (left + right) / 2, right);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nswap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left])\nj--;          // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j && nums[i] <= nums[left])\ni++;          // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left);  // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i;             // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\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\nif ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))\nreturn left;\nelse if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\nreturn mid;\nelse\nreturn right;\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\nint med = medianThree(nums, left, (left + right) / 2, right);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nswap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left])\nj--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j && nums[i] <= nums[left])\ni++;          // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i;            // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
    quick_sort.py
    def 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\nif (nums[left] < nums[mid]) ^ (nums[left] < nums[right]):\nreturn left\nelif (nums[mid] < nums[left]) ^ (nums[mid] < nums[right]):\nreturn mid\nreturn right\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] \u4f5c\u4e3a\u57fa\u51c6\u6570\nmed = self.median_three(nums, left, (left + right) // 2, right)\n# \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nnums[left], nums[med] = nums[med], nums[left]\n# \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\ni, j = left, right\nwhile i < j:\nwhile i < j and nums[j] >= nums[left]:\nj -= 1  # \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile i < j and nums[i] <= nums[left]:\ni += 1  # \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n# \u5143\u7d20\u4ea4\u6362\nnums[i], nums[j] = nums[j], nums[i]\n# \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nnums[i], nums[left] = nums[left], nums[i]\nreturn i  # \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\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\nif (nums[left] < nums[mid]) != (nums[left] < nums[right]) {\nreturn left\n} else if (nums[mid] < nums[left]) != (nums[mid] < nums[right]) {\nreturn mid\n}\nreturn right\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] \u4f5c\u4e3a\u57fa\u51c6\u6570\nmed := q.medianThree(nums, left, (left+right)/2, right)\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nnums[left], nums[med] = nums[med], nums[left]\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\ni, j := left, right\nfor i < j {\nfor i < j && nums[j] >= nums[left] {\nj-- //\u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nfor i < j && nums[i] <= nums[left] {\ni++ //\u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\n//\u5143\u7d20\u4ea4\u6362\nnums[i], nums[j] = nums[j], nums[i]\n}\n//\u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nnums[i], nums[left] = nums[left], nums[i]\nreturn i //\u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\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\nif ((nums[left] < nums[mid]) ^ (nums[left] < nums[right])) return left;\nelse if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\nreturn mid;\nelse return right;\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\nlet med = this.medianThree(\nnums,\nleft,\nMath.floor((left + right) / 2),\nright\n);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nthis.swap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nlet i = left,\nj = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left]) j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j && nums[i] <= nums[left]) i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nthis.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nthis.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn 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(\nnums: number[],\nleft: number,\nmid: number,\nright: 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\nif (Number(nums[left] < nums[mid]) ^ Number(nums[left] < nums[right])) {\nreturn left;\n} else if (\nNumber(nums[mid] < nums[left]) ^ Number(nums[mid] < nums[right])\n) {\nreturn mid;\n} else {\nreturn right;\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\nlet med = this.medianThree(\nnums,\nleft,\nMath.floor((left + right) / 2),\nright\n);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nthis.swap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nlet i = left,\nj = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left]) {\nj--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nwhile (i < j && nums[i] <= nums[left]) {\ni++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nthis.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nthis.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn 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\nif ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))\nreturn left;\nelse if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\nreturn mid;\nelse\nreturn right;\n}\n/* \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\nif ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))\nreturn left;\nelse if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\nreturn mid;\nelse\nreturn right;\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\nint med = medianThree(nums, left, (left + right) / 2, right);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nswap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left])\nj--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j && nums[i] <= nums[left])\ni++;          // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn 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\nif ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))\nreturn left;\nelse if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\nreturn mid;\nelse\nreturn right;\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\nint med = medianThree(nums, left, (left + right) / 2, right);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nswap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left])\nj--;          // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j && nums[i] <= nums[left])\ni++;          // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left);  // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn 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 {\nif (nums[left] < nums[mid]) != (nums[left] < nums[right]) {\nreturn left\n} else if (nums[mid] < nums[left]) != (nums[mid] < nums[right]) {\nreturn mid\n} else {\nreturn right\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\nlet 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\nswap(nums: &nums, i: left, j: med)\nreturn partition(nums: &nums, left: left, right: right)\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\nif ((nums[left] < nums[mid]) != (nums[left] < nums[right])) {\nreturn left;\n} else if ((nums[mid] < nums[left]) != (nums[mid] < nums[right])) {\nreturn mid;\n} else {\nreturn right;\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\nvar med = medianThree(nums, left, (left + right) / 2, right);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nswap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nvar i = left;\nvar j = right;\nwhile (i < j) {\nwhile (i < j and nums[j] >= nums[left]) j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j and nums[i] <= nums[left]) i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j);   // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left);    // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn 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\nif ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))\nreturn left;\nelse if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\nreturn mid;\nelse\nreturn right;\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\nint 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] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left]) j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (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\nreturn 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\u5012\u5e8f\u7684\u8f93\u5165\u6570\u7ec4\u4e3a\u4f8b\uff0c\u7531\u4e8e\u6bcf\u8f6e\u54e8\u5175\u5212\u5206\u540e\u53f3\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a \\(0\\) \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 \\(\\frac{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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nwhile (left < right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nint pivot = partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left < right - pivot) {\nquickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nquickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n}\n}\n}\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\nwhile (left < right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nint pivot = partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left < right - pivot) {\nquickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1;                 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nquickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1;                 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n}\n}\n}\n
    quick_sort.py
    def quick_sort(self, nums: list[int], left: int, right: int) -> None:\n\"\"\"\u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09\"\"\"\n# \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\nwhile left < right:\n# \u54e8\u5175\u5212\u5206\u64cd\u4f5c\npivot = self.partition(nums, left, right)\n# \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif pivot - left < right - pivot:\nself.quick_sort(nums, left, pivot - 1)  # \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1  # \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\nelse:\nself.quick_sort(nums, pivot + 1, right)  # \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1  # \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\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\nfor left < right {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\npivot := q.partition(nums, left, right)\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif pivot-left < right-pivot {\nq.quickSort(nums, left, pivot-1) // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1                 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nq.quickSort(nums, pivot+1, right) // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = 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\nwhile (left < right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nlet pivot = this.partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left < right - pivot) {\nthis.quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nthis.quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = 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\nwhile (left < right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nlet pivot = this.partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left < right - pivot) {\nthis.quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nthis.quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = 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\nwhile (left < right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nint pivot = partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left < right - pivot) {\nquickSortTailCall(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1;                         // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nquickSortTailCall(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = 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\nwhile (left < right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nint pivot = partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left < right - pivot) {\nquickSort(nums, left, pivot - 1);  // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1;  // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nquickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = 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) {\nvar left = left\nvar right = right\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\nwhile left < right {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nlet pivot = partition(nums: &nums, left: left, right: right)\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left) < (right - pivot) {\nquickSortTailCall(nums: &nums, left: left, right: pivot - 1) // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nquickSortTailCall(nums: &nums, left: pivot + 1, right: right) // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = 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 {\nvar left = left_;\nvar right = right_;\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nwhile (left < right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nvar pivot = partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left < right - pivot) {\nquickSort(nums, left, pivot - 1);   // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1;                   // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nquickSort(nums, pivot + 1, right);  // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = 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\nwhile (left < right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nint pivot = _partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left < right - pivot) {\nquickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nquickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = 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\u6211\u4eec\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\u6b65\u9aa4\u5982\u4e0b\uff1a

    1. \u521d\u59cb\u5316\u4f4d\u6570 \\(k = 1\\) \uff1b
    2. \u5bf9\u5b66\u53f7\u7684\u7b2c \\(k\\) \u4f4d\u6267\u884c\u300c\u8ba1\u6570\u6392\u5e8f\u300d\u3002\u5b8c\u6210\u540e\uff0c\u6570\u636e\u4f1a\u6839\u636e\u7b2c \\(k\\) \u4f4d\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\uff1b
    3. \u5c06 \\(k\\) \u589e\u52a0 \\(1\\) \uff0c\u7136\u540e\u8fd4\u56de\u6b65\u9aa4 2. \u7ee7\u7eed\u8fed\u4ee3\uff0c\u76f4\u5230\u6240\u6709\u4f4d\u90fd\u6392\u5e8f\u5b8c\u6210\u540e\u7ed3\u675f\uff1b

    Fig. \u57fa\u6570\u6392\u5e8f\u7b97\u6cd5\u6d41\u7a0b

    \u4e0b\u9762\u6765\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 \\space 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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nreturn (num / exp) % 10;\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\nint[] counter = new int[10];\nint n = nums.length;\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (int i = 0; i < n; i++) {\nint d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[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\nfor (int i = 1; i < 10; i++) {\ncounter[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\nint[] res = new int[n];\nfor (int i = n - 1; i >= 0; i--) {\nint d = digit(nums[i], exp);\nint j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i];       // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d]--;           // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor (int i = 0; i < n; i++)\nnums[i] = res[i];\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\nint m = Integer.MIN_VALUE;\nfor (int num : nums)\nif (num > m)\nm = num;\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor (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)\ncountingSortDigit(nums, exp);\n}\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\nreturn (num / exp) % 10;\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\nvector<int> counter(10, 0);\nint n = nums.size();\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (int i = 0; i < n; i++) {\nint d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[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\nfor (int i = 1; i < 10; i++) {\ncounter[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\nvector<int> res(n, 0);\nfor (int i = n - 1; i >= 0; i--) {\nint d = digit(nums[i], exp);\nint j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i];       // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d]--;           // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor (int i = 0; i < n; i++)\nnums[i] = res[i];\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\nint m = *max_element(nums.begin(), nums.end());\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor (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)\ncountingSortDigit(nums, exp);\n}\n
    radix_sort.py
    def 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\nreturn (num // exp) % 10\ndef counting_sort_digit(nums: list[int], exp: int) -> None:\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\ncounter = [0] * 10\nn = len(nums)\n# \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor i in range(n):\nd = digit(nums[i], exp)  # \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[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\nfor i in range(1, 10):\ncounter[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\nres = [0] * n\nfor i in range(n - 1, -1, -1):\nd = digit(nums[i], exp)\nj = counter[d] - 1  # \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i]  # \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d] -= 1  # \u5c06 d \u7684\u6570\u91cf\u51cf 1\n# \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor i in range(n):\nnums[i] = res[i]\ndef radix_sort(nums: list[int]) -> None:\n\"\"\"\u57fa\u6570\u6392\u5e8f\"\"\"\n# \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\nm = max(nums)\n# \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nexp = 1\nwhile 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)\ncounting_sort_digit(nums, exp)\nexp *= 10\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\nreturn (num / exp) % 10\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\ncounter := make([]int, 10)\nn := len(nums)\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor i := 0; i < n; i++ {\nd := digit(nums[i], exp) // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[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\nfor i := 1; i < 10; i++ {\ncounter[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\nres := make([]int, n)\nfor i := n - 1; i >= 0; i-- {\nd := digit(nums[i], exp)\nj := counter[d] - 1 // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i]    // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d]--        // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor i := 0; i < n; i++ {\nnums[i] = res[i]\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\nmax := math.MinInt\nfor _, num := range nums {\nif num > max {\nmax = num\n}\n}\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor 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)\ncountingSortDigit(nums, 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\nreturn Math.floor(num / exp) % 10;\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\nconst counter = new Array(10).fill(0);\nconst n = nums.length;\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (let i = 0; i < n; i++) {\nconst d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[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\nfor (let i = 1; i < 10; i++) {\ncounter[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\nconst res = new Array(n).fill(0);\nfor (let i = n - 1; i >= 0; i--) {\nconst d = digit(nums[i], exp);\nconst j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor (let i = 0; i < n; i++) {\nnums[i] = res[i];\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\nlet m = Number.MIN_VALUE;\nfor (const num of nums) {\nif (num > m) {\nm = num;\n}\n}\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor (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)\ncountingSortDigit(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\nreturn Math.floor(num / exp) % 10;\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\nconst counter = new Array(10).fill(0);\nconst n = nums.length;\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (let i = 0; i < n; i++) {\nconst d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[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\nfor (let i = 1; i < 10; i++) {\ncounter[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\nconst res = new Array(n).fill(0);\nfor (let i = n - 1; i >= 0; i--) {\nconst d = digit(nums[i], exp);\nconst j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor (let i = 0; i < n; i++) {\nnums[i] = res[i];\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\nlet m = Number.MIN_VALUE;\nfor (const num of nums) {\nif (num > m) {\nm = num;\n}\n}\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor (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)\ncountingSortDigit(nums, exp);\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\nreturn (num / exp) % 10;\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\nint *counter = (int *)malloc((sizeof(int) * 10));\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (int i = 0; i < size; i++) {\n// \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\nint d = digit(nums[i], exp);\n// \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\ncounter[d]++;\n}\n// \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\nfor (int i = 1; i < 10; i++) {\ncounter[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\nint *res = (int *)malloc(sizeof(int) * size);\nfor (int i = size - 1; i >= 0; i--) {\nint d = digit(nums[i], exp);\nint j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i];       // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d]--;           // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor (int i = 0; i < size; i++) {\nnums[i] = res[i];\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\nint max = INT32_MIN;\nfor (size_t i = 0; i < size - 1; i++) {\nif (nums[i] > max) {\nmax = nums[i];\n}\n}\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor (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)\ncountingSortDigit(nums, size, 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\nreturn (num / exp) % 10;\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\nint[] counter = new int[10];\nint n = nums.Length;\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (int i = 0; i < n; i++) {\nint d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[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\nfor (int i = 1; i < 10; i++) {\ncounter[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\nint[] res = new int[n];\nfor (int i = n - 1; i >= 0; i--) {\nint d = digit(nums[i], exp);\nint j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i];       // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d]--;           // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor (int i = 0; i < n; i++) {\nnums[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\nint m = int.MinValue;\nforeach (int num in nums) {\nif (num > m) m = num;\n}\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor (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)\ncountingSortDigit(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/* \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\nvar counter = Array(repeating: 0, count: 10)\nlet n = nums.count\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor i in nums.indices {\nlet d = digit(num: nums[i], exp: exp) // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[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\nfor i in 1 ..< 10 {\ncounter[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\nvar res = Array(repeating: 0, count: n)\nfor i in stride(from: n - 1, through: 0, by: -1) {\nlet d = digit(num: nums[i], exp: exp)\nlet j = counter[d] - 1 // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i] // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d] -= 1 // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor i in nums.indices {\nnums[i] = res[i]\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\nvar m = Int.min\nfor num in nums {\nif num > m {\nm = num\n}\n}\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor 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)\ncountingSortDigit(nums: &nums, exp: exp)\n}\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\nreturn @mod(@divFloor(num, exp), 10);\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\nvar mem_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);\n// defer mem_arena.deinit();\nconst mem_allocator = mem_arena.allocator();\nvar counter = try mem_allocator.alloc(usize, 10);\nstd.mem.set(usize, counter, 0);\nvar n = nums.len;\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (nums) |num| {\nvar d = @bitCast(u32, digit(num, exp)); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[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\nvar i: usize = 1;\nwhile (i < 10) : (i += 1) {\ncounter[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\nvar res = try mem_allocator.alloc(i32, n);\ni = n - 1;\nwhile (i >= 0) : (i -= 1) {\nvar d = @bitCast(u32, digit(nums[i], exp));\nvar j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i];       // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d] -= 1;        // \u5c06 d \u7684\u6570\u91cf\u51cf 1\nif (i == 0) break;\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\ni = 0;\nwhile (i < n) : (i += 1) {\nnums[i] = res[i];\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\nvar m: i32 = std.math.minInt(i32);\nfor (nums) |num| {\nif (num > m) m = num;\n}\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nvar exp: i32 = 1;\nwhile (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)\ntry countingSortDigit(nums, exp);    }\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\nreturn (num ~/ exp) % 10;\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\nList<int> counter = List<int>.filled(10, 0);\nint n = nums.length;\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (int i = 0; i < n; i++) {\nint d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[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\nfor (int i = 1; i < 10; i++) {\ncounter[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\nList<int> res = List<int>.filled(n, 0);\nfor (int i = n - 1; i >= 0; i--) {\nint d = digit(nums[i], exp);\nint j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor (int i = 0; i < n; i++) nums[i] = res[i];\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\nint m = -1 << 63; for (int num in nums) if (num > m) m = num;\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor (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)\ncountingSortDigit(nums, exp);\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\u6211\u4eec\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

    • \u65f6\u95f4\u590d\u6742\u5ea6 \\(O(nk)\\) \uff1a\u8bbe\u6570\u636e\u91cf\u4e3a \\(n\\) \u3001\u6570\u636e\u4e3a \\(d\\) \u8fdb\u5236\u3001\u6700\u5927\u4f4d\u6570\u4e3a \\(k\\) \uff0c\u5219\u5bf9\u67d0\u4e00\u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\u4f7f\u7528 \\(O(n + d)\\) \u65f6\u95f4\uff0c\u6392\u5e8f\u6240\u6709 \\(k\\) \u4f4d\u4f7f\u7528 \\(O((n + d)k)\\) \u65f6\u95f4\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\\(d\\) \u548c \\(k\\) \u90fd\u76f8\u5bf9\u8f83\u5c0f\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u8d8b\u5411 \\(O(n)\\) \u3002
    • \u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(n + d)\\) \u3001\u975e\u539f\u5730\u6392\u5e8f \uff1a\u4e0e\u8ba1\u6570\u6392\u5e8f\u76f8\u540c\uff0c\u57fa\u6570\u6392\u5e8f\u9700\u8981\u501f\u52a9\u957f\u5ea6\u4e3a \\(n\\) \u548c \\(d\\) \u7684\u6570\u7ec4 res \u548c counter \u3002
    • \u7a33\u5b9a\u6392\u5e8f\uff1a\u4e0e\u8ba1\u6570\u6392\u5e8f\u76f8\u540c\u3002
    "},{"location":"chapter_sorting/selection_sort/","title":"11.2. \u00a0 \u9009\u62e9\u6392\u5e8f","text":"

    \u300c\u9009\u62e9\u6392\u5e8f Selection Sort\u300d\u7684\u5de5\u4f5c\u539f\u7406\u975e\u5e38\u76f4\u63a5\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\u4e0b\uff1a

    1. \u521d\u59cb\u72b6\u6001\u4e0b\uff0c\u6240\u6709\u5143\u7d20\u672a\u6392\u5e8f\uff0c\u5373\u672a\u6392\u5e8f\uff08\u7d22\u5f15\uff09\u533a\u95f4\u4e3a \\([0, n-1]\\) \u3002
    2. \u9009\u53d6\u533a\u95f4 \\([0, n-1]\\) \u4e2d\u7684\u6700\u5c0f\u5143\u7d20\uff0c\u5c06\u5176\u4e0e\u7d22\u5f15 \\(0\\) \u5904\u5143\u7d20\u4ea4\u6362\u3002\u5b8c\u6210\u540e\uff0c\u6570\u7ec4\u524d 1 \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002
    3. \u9009\u53d6\u533a\u95f4 \\([1, n-1]\\) \u4e2d\u7684\u6700\u5c0f\u5143\u7d20\uff0c\u5c06\u5176\u4e0e\u7d22\u5f15 \\(1\\) \u5904\u5143\u7d20\u4ea4\u6362\u3002\u5b8c\u6210\u540e\uff0c\u6570\u7ec4\u524d 2 \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002
    4. \u4ee5\u6b64\u7c7b\u63a8\u3002\u7ecf\u8fc7 \\(n - 1\\) \u8f6e\u9009\u62e9\u4e0e\u4ea4\u6362\u540e\uff0c\u6570\u7ec4\u524d \\(n - 1\\) \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002
    5. \u4ec5\u5269\u7684\u4e00\u4e2a\u5143\u7d20\u5fc5\u5b9a\u662f\u6700\u5927\u5143\u7d20\uff0c\u65e0\u9700\u6392\u5e8f\uff0c\u56e0\u6b64\u6570\u7ec4\u6392\u5e8f\u5b8c\u6210\u3002
    <1><2><3><4><5><6><7><8><9><10><11>

    \u5728\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u7528 \\(k\\) \u6765\u8bb0\u5f55\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart selection_sort.java
    /* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(int[] nums) {\nint n = nums.length;\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor (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\nint k = i;\nfor (int j = i + 1; j < n; j++) {\nif (nums[j] < nums[k])\nk = 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\nint temp = nums[i];\nnums[i] = nums[k];\nnums[k] = temp;\n}\n}\n
    selection_sort.cpp
    /* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(vector<int> &nums) {\nint n = nums.size();\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor (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\nint k = i;\nfor (int j = i + 1; j < n; j++) {\nif (nums[j] < nums[k])\nk = 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\nswap(nums[i], nums[k]);\n}\n}\n
    selection_sort.py
    def selection_sort(nums: list[int]):\n\"\"\"\u9009\u62e9\u6392\u5e8f\"\"\"\nn = len(nums)\n# \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor i in range(n - 1):\n# \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\nk = i\nfor j in range(i + 1, n):\nif nums[j] < nums[k]:\nk = 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\nnums[i], nums[k] = nums[k], nums[i]\n
    selection_sort.go
    /* \u9009\u62e9\u6392\u5e8f */\nfunc selectionSort(nums []int) {\nn := len(nums)\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor i := 0; i < n-1; i++ {\n// \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\nk := i\nfor j := i + 1; j < n; j++ {\nif nums[j] < nums[k] {\n// \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\nk = j\n}\n}\n// \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\nnums[i], nums[k] = nums[k], nums[i]\n}\n}\n
    selection_sort.js
    /* \u9009\u62e9\u6392\u5e8f */\nfunction selectionSort(nums) {\nlet n = nums.length;\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor (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\nlet k = i;\nfor (let j = i + 1; j < n; j++) {\nif (nums[j] < nums[k]) {\nk = 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 {\nlet n = nums.length;\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor (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\nlet k = i;\nfor (let j = i + 1; j < n; j++) {\nif (nums[j] < nums[k]) {\nk = 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.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]\nfor (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\nint k = i;\nfor (int j = i + 1; j < n; j++) {\nif (nums[j] < nums[k])\nk = 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\nint temp = nums[i];\nnums[i] = nums[k];\nnums[k] = temp;\n}\n}\n
    selection_sort.cs
    /* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(int[] nums) {\nint n = nums.Length;\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor (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\nint k = i;\nfor (int j = i + 1; j < n; j++) {\nif (nums[j] < nums[k])\nk = 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.swift
    /* \u9009\u62e9\u6392\u5e8f */\nfunc selectionSort(nums: inout [Int]) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor i in nums.indices.dropLast() {\n// \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\nvar k = i\nfor j in nums.indices.dropFirst(i + 1) {\nif nums[j] < nums[k] {\nk = 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\nnums.swapAt(i, k)\n}\n}\n
    selection_sort.zig
    [class]{}-[func]{selectionSort}\n
    selection_sort.dart
    /* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(List<int> nums) {\nint n = nums.length;\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor (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\nint k = i;\nfor (int j = i + 1; j < n; j++) {\nif (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\nint temp = nums[i];\nnums[i] = nums[k];\nnums[k] = temp;\n}\n}\n
    "},{"location":"chapter_sorting/selection_sort/#1121","title":"11.2.1. \u00a0 \u7b97\u6cd5\u7279\u6027","text":"
    • \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3001\u975e\u81ea\u9002\u5e94\u6392\u5e8f\uff1a\u5916\u5faa\u73af\u5171 \\(n - 1\\) \u8f6e\uff0c\u7b2c\u4e00\u8f6e\u7684\u672a\u6392\u5e8f\u533a\u95f4\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u6700\u540e\u4e00\u8f6e\u7684\u672a\u6392\u5e8f\u533a\u95f4\u957f\u5ea6\u4e3a \\(2\\) \uff0c\u5373\u5404\u8f6e\u5916\u5faa\u73af\u5206\u522b\u5305\u542b \\(n\\) , \\(n - 1\\) , \\(\\cdots\\) , \\(2\\) \u8f6e\u5185\u5faa\u73af\uff0c\u6c42\u548c\u4e3a \\(\\frac{(n - 1)(n + 2)}{2}\\) \u3002
    • \u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(1)\\) \u3001\u539f\u5730\u6392\u5e8f\uff1a\u6307\u9488 \\(i\\) , \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002
    • \u975e\u7a33\u5b9a\u6392\u5e8f\uff1a\u5728\u4ea4\u6362\u5143\u7d20\u65f6\uff0c\u6709\u53ef\u80fd\u5c06 nums[i] \u4ea4\u6362\u81f3\u5176\u76f8\u7b49\u5143\u7d20\u7684\u53f3\u8fb9\uff0c\u5bfc\u81f4\u4e24\u8005\u7684\u76f8\u5bf9\u987a\u5e8f\u53d1\u751f\u6539\u53d8\u3002

    Fig. \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\u6709\u6548\u5730\u67e5\u627e\u3001\u5206\u6790\u548c\u5904\u7406\u3002

    \u5728\u6392\u5e8f\u7b97\u6cd5\u4e2d\uff0c\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u662f\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\u7b49\uff1b\u987a\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

    Fig. \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\u5373\u65f6\u95f4\u590d\u6742\u5ea6\u4e2d\u7684\u5e38\u6570\u9879\u964d\u4f4e\uff09\u3002\u5bf9\u4e8e\u5927\u6570\u636e\u91cf\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\u9700\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\u4f18\u826f\u7279\u6027\uff0c\u4e5f\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, 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\u3002

    # \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# \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\u3001\u6700\u5dee\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\u4e8e\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff08\\(<\\) , \\(=\\) , \\(>\\)\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":"
    • \u5192\u6ce1\u6392\u5e8f\u901a\u8fc7\u4ea4\u6362\u76f8\u90bb\u5143\u7d20\u6765\u5b9e\u73b0\u6392\u5e8f\u3002\u901a\u8fc7\u6dfb\u52a0\u4e00\u4e2a\u6807\u5fd7\u4f4d\u6765\u5b9e\u73b0\u63d0\u524d\u8fd4\u56de\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5192\u6ce1\u6392\u5e8f\u7684\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u5230 \\(O(n)\\) \u3002
    • \u63d2\u5165\u6392\u5e8f\u6bcf\u8f6e\u5c06\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u5143\u7d20\u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4\u7684\u6b63\u786e\u4f4d\u7f6e\uff0c\u4ece\u800c\u5b8c\u6210\u6392\u5e8f\u3002\u867d\u7136\u63d2\u5165\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u4f46\u7531\u4e8e\u5355\u5143\u64cd\u4f5c\u76f8\u5bf9\u8f83\u5c11\uff0c\u5b83\u5728\u5c0f\u6570\u636e\u91cf\u7684\u6392\u5e8f\u4efb\u52a1\u4e2d\u975e\u5e38\u53d7\u6b22\u8fce\u3002
    • \u5feb\u901f\u6392\u5e8f\u57fa\u4e8e\u54e8\u5175\u5212\u5206\u64cd\u4f5c\u5b9e\u73b0\u6392\u5e8f\u3002\u5728\u54e8\u5175\u5212\u5206\u4e2d\uff0c\u6709\u53ef\u80fd\u6bcf\u6b21\u90fd\u9009\u53d6\u5230\u6700\u5dee\u7684\u57fa\u51c6\u6570\uff0c\u5bfc\u81f4\u65f6\u95f4\u590d\u6742\u5ea6\u52a3\u5316\u81f3 \\(O(n^2)\\) \u3002\u5f15\u5165\u4e2d\u4f4d\u6570\u57fa\u51c6\u6570\u6216\u968f\u673a\u57fa\u51c6\u6570\u53ef\u4ee5\u964d\u4f4e\u8fd9\u79cd\u52a3\u5316\u7684\u6982\u7387\u3002\u5c3e\u9012\u5f52\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5730\u51cf\u5c11\u9012\u5f52\u6df1\u5ea6\uff0c\u5c06\u7a7a\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u5230 \\(O(\\log n)\\) \u3002
    • \u5f52\u5e76\u6392\u5e8f\u5305\u62ec\u5212\u5206\u548c\u5408\u5e76\u4e24\u4e2a\u9636\u6bb5\uff0c\u5178\u578b\u5730\u4f53\u73b0\u4e86\u5206\u6cbb\u7b56\u7565\u3002\u5728\u5f52\u5e76\u6392\u5e8f\u4e2d\uff0c\u6392\u5e8f\u6570\u7ec4\u9700\u8981\u521b\u5efa\u8f85\u52a9\u6570\u7ec4\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff1b\u7136\u800c\u6392\u5e8f\u94fe\u8868\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u4f18\u5316\u81f3 \\(O(1)\\) \u3002
    • \u6876\u6392\u5e8f\u5305\u542b\u4e09\u4e2a\u6b65\u9aa4\uff1a\u6570\u636e\u5206\u6876\u3001\u6876\u5185\u6392\u5e8f\u548c\u5408\u5e76\u7ed3\u679c\u3002\u5b83\u540c\u6837\u4f53\u73b0\u4e86\u5206\u6cbb\u7b56\u7565\uff0c\u9002\u7528\u4e8e\u6570\u636e\u4f53\u91cf\u5f88\u5927\u7684\u60c5\u51b5\u3002\u6876\u6392\u5e8f\u7684\u5173\u952e\u5728\u4e8e\u5bf9\u6570\u636e\u8fdb\u884c\u5e73\u5747\u5206\u914d\u3002
    • \u8ba1\u6570\u6392\u5e8f\u662f\u6876\u6392\u5e8f\u7684\u4e00\u4e2a\u7279\u4f8b\uff0c\u5b83\u901a\u8fc7\u7edf\u8ba1\u6570\u636e\u51fa\u73b0\u7684\u6b21\u6570\u6765\u5b9e\u73b0\u6392\u5e8f\u3002\u8ba1\u6570\u6392\u5e8f\u9002\u7528\u4e8e\u6570\u636e\u91cf\u5927\u4f46\u6570\u636e\u8303\u56f4\u6709\u9650\u7684\u60c5\u51b5\uff0c\u5e76\u4e14\u8981\u6c42\u6570\u636e\u80fd\u591f\u8f6c\u6362\u4e3a\u6b63\u6574\u6570\u3002
    • \u57fa\u6570\u6392\u5e8f\u901a\u8fc7\u9010\u4f4d\u6392\u5e8f\u6765\u5b9e\u73b0\u6570\u636e\u6392\u5e8f\uff0c\u8981\u6c42\u6570\u636e\u80fd\u591f\u8868\u793a\u4e3a\u56fa\u5b9a\u4f4d\u6570\u7684\u6570\u5b57\u3002

    Fig. \u6392\u5e8f\u7b97\u6cd5\u5bf9\u6bd4

    • \u603b\u7684\u6765\u8bf4\uff0c\u6211\u4eec\u5e0c\u671b\u627e\u5230\u4e00\u79cd\u6392\u5e8f\u7b97\u6cd5\uff0c\u5177\u6709\u9ad8\u6548\u7387\u3001\u7a33\u5b9a\u3001\u539f\u5730\u4ee5\u53ca\u6b63\u5411\u81ea\u9002\u5e94\u6027\u7b49\u4f18\u70b9\u3002\u7136\u800c\uff0c\u6b63\u5982\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u4e00\u6837\uff0c\u6ca1\u6709\u4e00\u79cd\u6392\u5e8f\u7b97\u6cd5\u80fd\u591f\u540c\u65f6\u6ee1\u8db3\u6240\u6709\u8fd9\u4e9b\u6761\u4ef6\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u6570\u636e\u7684\u7279\u6027\u6765\u9009\u62e9\u5408\u9002\u7684\u6392\u5e8f\u7b97\u6cd5\u3002
    "},{"location":"chapter_sorting/summary/#11111-q-a","title":"11.11.1. \u00a0 Q & A","text":"

    \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\u5c0f\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

    "},{"location":"chapter_stack_and_queue/","title":"5. \u00a0 \u6808\u4e0e\u961f\u5217","text":""},{"location":"chapter_stack_and_queue/deque/","title":"5.3. \u00a0 \u53cc\u5411\u961f\u5217","text":"

    \u5bf9\u4e8e\u961f\u5217\uff0c\u6211\u4eec\u4ec5\u80fd\u5728\u5934\u90e8\u5220\u9664\u6216\u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\u3002\u7136\u800c\uff0c\u300c\u53cc\u5411\u961f\u5217 Deque\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

    Fig. \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\u4e0b\u8868\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

    \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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart deque.java
    /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\nDeque<Integer> deque = new LinkedList<>();\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/* \u8bbf\u95ee\u5143\u7d20 */\nint peekFirst = deque.peekFirst();  // \u961f\u9996\u5143\u7d20\nint peekLast = deque.peekLast();    // \u961f\u5c3e\u5143\u7d20\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/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.size();\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = deque.isEmpty();\n
    deque.cpp
    /* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\ndeque<int> deque;\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/* \u8bbf\u95ee\u5143\u7d20 */\nint front = deque.front(); // \u961f\u9996\u5143\u7d20\nint back = deque.back();   // \u961f\u5c3e\u5143\u7d20\n/* \u5143\u7d20\u51fa\u961f */\ndeque.pop_front();  // \u961f\u9996\u5143\u7d20\u51fa\u961f\ndeque.pop_back();   // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.size();\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty = deque.empty();\n
    deque.py
    # \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217\ndeque: Deque[int] = collections.deque()\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# \u8bbf\u95ee\u5143\u7d20\nfront: int = deque[0]  # \u961f\u9996\u5143\u7d20\nrear: int = deque[-1]  # \u961f\u5c3e\u5143\u7d20\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# \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\nsize: int = len(deque)\n# \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = len(deque) == 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/* \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/* \u8bbf\u95ee\u5143\u7d20 */\nfront := deque.Front() // \u961f\u9996\u5143\u7d20\nrear := deque.Back()   // \u961f\u5c3e\u5143\u7d20\n/* \u5143\u7d20\u51fa\u961f */\ndeque.Remove(front)    // \u961f\u9996\u5143\u7d20\u51fa\u961f\ndeque.Remove(rear)     // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nsize := deque.Len()\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty := deque.Len() == 0\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/* \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/* \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/* \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/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nconst size = deque.length;\nconsole.log(\"\u53cc\u5411\u961f\u5217\u957f\u5ea6 size = \" + size);\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/* \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/* \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/* \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/* \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/* \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.c
    // C \u672a\u63d0\u4f9b\u5185\u7f6e\u53cc\u5411\u961f\u5217\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 LinkedList<int>();\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/* \u8bbf\u95ee\u5143\u7d20 */\nint peekFirst = deque.First.Value;  // \u961f\u9996\u5143\u7d20\nint peekLast = deque.Last.Value;    // \u961f\u5c3e\u5143\u7d20\n/* \u5143\u7d20\u51fa\u961f */\ndeque.RemoveFirst();  // \u961f\u9996\u5143\u7d20\u51fa\u961f\ndeque.RemoveLast();   // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.Count;\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = deque.Count == 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/* \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/* \u8bbf\u95ee\u5143\u7d20 */\nlet peekFirst = deque.first! // \u961f\u9996\u5143\u7d20\nlet peekLast = deque.last! // \u961f\u5c3e\u5143\u7d20\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/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = deque.count\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = deque.isEmpty\n
    deque.zig
    \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/* \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/* \u8bbf\u95ee\u5143\u7d20 */\nint peekFirst = deque.first; // \u961f\u9996\u5143\u7d20\nint peekLast = deque.last;   // \u961f\u5c3e\u5143\u7d20\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/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.length;\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = deque.isEmpty;W\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":"\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\u300c\u53cc\u5411\u94fe\u8868\u300d\u4f5c\u4e3a\u53cc\u5411\u961f\u5217\u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002

    \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()

    \u4ee5\u4e0b\u662f\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart linkedlist_deque.java
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\nint val; // \u8282\u70b9\u503c\nListNode next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\nListNode prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\nListNode(int val) {\nthis.val = val;\nprev = next = null;\n}\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\nprivate ListNode front, rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\nprivate int queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\npublic LinkedListDeque() {\nfront = rear = null;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic boolean isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u961f\u64cd\u4f5c */\nprivate void push(int num, boolean isFront) {\nListNode node = new ListNode(num);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (isEmpty())\nfront = rear = node;\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\nelse if (isFront) {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nfront.prev = node;\nnode.next = front;\nfront = 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\nrear.next = node;\nnode.prev = rear;\nrear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nqueSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n/* \u961f\u9996\u5165\u961f */\npublic void pushFirst(int num) {\npush(num, true);\n}\n/* \u961f\u5c3e\u5165\u961f */\npublic void pushLast(int num) {\npush(num, false);\n}\n/* \u51fa\u961f\u64cd\u4f5c */\nprivate Integer pop(boolean isFront) {\n// \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de null\nif (isEmpty())\nreturn null;\nint val;\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif (isFront) {\nval = front.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n// \u5220\u9664\u5934\u8282\u70b9\nListNode fNext = front.next;\nif (fNext != null) {\nfNext.prev = null;\nfront.next = null;\n}\nfront = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n// \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n} else {\nval = rear.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5c3e\u8282\u70b9\nListNode rPrev = rear.prev;\nif (rPrev != null) {\nrPrev.next = null;\nrear.prev = null;\n}\nrear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nqueSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val;\n}\n/* \u961f\u9996\u51fa\u961f */\npublic Integer popFirst() {\nreturn pop(true);\n}\n/* \u961f\u5c3e\u51fa\u961f */\npublic Integer popLast() {\nreturn pop(false);\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic Integer peekFirst() {\nreturn isEmpty() ? null : front.val;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npublic Integer peekLast() {\nreturn isEmpty() ? null : rear.val;\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\npublic int[] toArray() {\nListNode node = front;\nint[] res = new int[size()];\nfor (int i = 0; i < res.length; i++) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n
    linkedlist_deque.cpp
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nstruct DoublyListNode {\nint val;              // \u8282\u70b9\u503c\nDoublyListNode *next; // \u540e\u7ee7\u8282\u70b9\u6307\u9488\nDoublyListNode *prev; // \u524d\u9a71\u8282\u70b9\u6307\u9488\nDoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {\n}\n};\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\nprivate:\nDoublyListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\nint queSize = 0;              // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nLinkedListDeque() : front(nullptr), rear(nullptr) {\n}\n/* \u6790\u6784\u65b9\u6cd5 */\n~LinkedListDeque() {\n// \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\nDoublyListNode *pre, *cur = front;\nwhile (cur != nullptr) {\npre = cur;\ncur = cur->next;\ndelete pre;\n}\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u961f\u64cd\u4f5c */\nvoid push(int num, bool isFront) {\nDoublyListNode *node = new DoublyListNode(num);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (isEmpty())\nfront = rear = node;\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\nelse if (isFront) {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nfront->prev = node;\nnode->next = front;\nfront = 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\nrear->next = node;\nnode->prev = rear;\nrear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nqueSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(int num) {\npush(num, true);\n}\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(int num) {\npush(num, false);\n}\n/* \u51fa\u961f\u64cd\u4f5c */\nint pop(bool isFront) {\n// \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de -1\nif (isEmpty())\nreturn -1;\nint val;\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif (isFront) {\nval = front->val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n// \u5220\u9664\u5934\u8282\u70b9\nDoublyListNode *fNext = front->next;\nif (fNext != nullptr) {\nfNext->prev = nullptr;\nfront->next = nullptr;\ndelete front;\n}\nfront = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n// \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n} else {\nval = rear->val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5c3e\u8282\u70b9\nDoublyListNode *rPrev = rear->prev;\nif (rPrev != nullptr) {\nrPrev->next = nullptr;\nrear->prev = nullptr;\ndelete rear;\n}\nrear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nqueSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val;\n}\n/* \u961f\u9996\u51fa\u961f */\nint popFirst() {\nreturn pop(true);\n}\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast() {\nreturn pop(false);\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst() {\nreturn isEmpty() ? -1 : front->val;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast() {\nreturn isEmpty() ? -1 : rear->val;\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nvector<int> toVector() {\nDoublyListNode *node = front;\nvector<int> res(size());\nfor (int i = 0; i < res.size(); i++) {\nres[i] = node->val;\nnode = node->next;\n}\nreturn res;\n}\n};\n
    linkedlist_deque.py
    class ListNode:\n\"\"\"\u53cc\u5411\u94fe\u8868\u8282\u70b9\"\"\"\ndef __init__(self, val: int) -> None:\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.val: int = val\nself.next: ListNode | None = None  # \u540e\u7ee7\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\nself.prev: ListNode | None = None  # \u524d\u9a71\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\nclass LinkedListDeque:\n\"\"\"\u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\"\"\"\ndef __init__(self) -> None:\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.front: ListNode | None = None  # \u5934\u8282\u70b9 front\nself.rear: ListNode | None = None  # \u5c3e\u8282\u70b9 rear\nself.__size: int = 0  # \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\ndef size(self) -> int:\n\"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\"\"\"\nreturn self.__size\ndef is_empty(self) -> bool:\n\"\"\"\u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\nreturn self.size() == 0\ndef push(self, num: int, is_front: bool) -> None:\n\"\"\"\u5165\u961f\u64cd\u4f5c\"\"\"\nnode = ListNode(num)\n# \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif self.is_empty():\nself.front = self.rear = node\n# \u961f\u9996\u5165\u961f\u64cd\u4f5c\nelif is_front:\n# \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nself.front.prev = node\nnode.next = self.front\nself.front = node  # \u66f4\u65b0\u5934\u8282\u70b9\n# \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\nelse:\n# \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\nself.rear.next = node\nnode.prev = self.rear\nself.rear = node  # \u66f4\u65b0\u5c3e\u8282\u70b9\nself.__size += 1  # \u66f4\u65b0\u961f\u5217\u957f\u5ea6\ndef push_first(self, num: int) -> None:\n\"\"\"\u961f\u9996\u5165\u961f\"\"\"\nself.push(num, True)\ndef push_last(self, num: int) -> None:\n\"\"\"\u961f\u5c3e\u5165\u961f\"\"\"\nself.push(num, False)\ndef pop(self, is_front: bool) -> int:\n\"\"\"\u51fa\u961f\u64cd\u4f5c\"\"\"\n# \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de None\nif self.is_empty():\nreturn None\n# \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif is_front:\nval: int = self.front.val  # \u6682\u5b58\u5934\u8282\u70b9\u503c\n# \u5220\u9664\u5934\u8282\u70b9\nfnext: ListNode | None = self.front.next\nif fnext != None:\nfnext.prev = None\nself.front.next = None\nself.front = fnext  # \u66f4\u65b0\u5934\u8282\u70b9\n# \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\nelse:\nval: int = self.rear.val  # \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n# \u5220\u9664\u5c3e\u8282\u70b9\nrprev: ListNode | None = self.rear.prev\nif rprev != None:\nrprev.next = None\nself.rear.prev = None\nself.rear = rprev  # \u66f4\u65b0\u5c3e\u8282\u70b9\nself.__size -= 1  # \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val\ndef pop_first(self) -> int:\n\"\"\"\u961f\u9996\u51fa\u961f\"\"\"\nreturn self.pop(True)\ndef pop_last(self) -> int:\n\"\"\"\u961f\u5c3e\u51fa\u961f\"\"\"\nreturn self.pop(False)\ndef peek_first(self) -> int:\n\"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\nreturn None if self.is_empty() else self.front.val\ndef peek_last(self) -> int:\n\"\"\"\u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\"\"\"\nreturn None if self.is_empty() else self.rear.val\ndef to_array(self) -> list[int]:\n\"\"\"\u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370\"\"\"\nnode = self.front\nres = [0] * self.size()\nfor i in range(self.size()):\nres[i] = node.val\nnode = node.next\nreturn res\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\ndata *list.List\n}\n/* \u521d\u59cb\u5316\u53cc\u7aef\u961f\u5217 */\nfunc newLinkedListDeque() *linkedListDeque {\nreturn &linkedListDeque{\ndata: list.New(),\n}\n}\n/* \u961f\u9996\u5143\u7d20\u5165\u961f */\nfunc (s *linkedListDeque) pushFirst(value any) {\ns.data.PushFront(value)\n}\n/* \u961f\u5c3e\u5143\u7d20\u5165\u961f */\nfunc (s *linkedListDeque) pushLast(value any) {\ns.data.PushBack(value)\n}\n/* \u961f\u9996\u5143\u7d20\u51fa\u961f */\nfunc (s *linkedListDeque) popFirst() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Front()\ns.data.Remove(e)\nreturn e.Value\n}\n/* \u961f\u5c3e\u5143\u7d20\u51fa\u961f */\nfunc (s *linkedListDeque) popLast() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Back()\ns.data.Remove(e)\nreturn e.Value\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (s *linkedListDeque) peekFirst() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Front()\nreturn e.Value\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc (s *linkedListDeque) peekLast() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Back()\nreturn e.Value\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (s *linkedListDeque) size() int {\nreturn s.data.Len()\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListDeque) isEmpty() bool {\nreturn s.data.Len() == 0\n}\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListDeque) toList() *list.List {\nreturn s.data\n}\n
    linkedlist_deque.js
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\nprev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\nnext; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\nval; // \u8282\u70b9\u503c\nconstructor(val) {\nthis.val = val;\nthis.next = null;\nthis.prev = null;\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\nconstructor() {\nthis.#front = null;\nthis.#rear = null;\nthis.#queSize = 0;\n}\n/* \u961f\u5c3e\u5165\u961f\u64cd\u4f5c */\npushLast(val) {\nconst node = new ListNode(val);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (this.#queSize === 0) {\nthis.#front = node;\nthis.#rear = node;\n} else {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\nthis.#rear.next = node;\nnode.prev = this.#rear;\nthis.#rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nthis.#queSize++;\n}\n/* \u961f\u9996\u5165\u961f\u64cd\u4f5c */\npushFirst(val) {\nconst node = new ListNode(val);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (this.#queSize === 0) {\nthis.#front = node;\nthis.#rear = node;\n} else {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nthis.#front.prev = node;\nnode.next = this.#front;\nthis.#front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n}\nthis.#queSize++;\n}\n/* \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c */\npopLast() {\nif (this.#queSize === 0) {\nreturn null;\n}\nconst value = this.#rear.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5c3e\u8282\u70b9\nlet temp = this.#rear.prev;\nif (temp !== null) {\ntemp.next = null;\nthis.#rear.prev = null;\n}\nthis.#rear = temp; // \u66f4\u65b0\u5c3e\u8282\u70b9\nthis.#queSize--;\nreturn value;\n}\n/* \u961f\u9996\u51fa\u961f\u64cd\u4f5c */\npopFirst() {\nif (this.#queSize === 0) {\nreturn null;\n}\nconst value = this.#front.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5934\u8282\u70b9\nlet temp = this.#front.next;\nif (temp !== null) {\ntemp.prev = null;\nthis.#front.next = null;\n}\nthis.#front = temp; // \u66f4\u65b0\u5934\u8282\u70b9\nthis.#queSize--;\nreturn value;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npeekLast() {\nreturn this.#queSize === 0 ? null : this.#rear.val;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeekFirst() {\nreturn this.#queSize === 0 ? null : this.#front.val;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nsize() {\nreturn this.#queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty() {\nreturn this.#queSize === 0;\n}\n/* \u6253\u5370\u53cc\u5411\u961f\u5217 */\nprint() {\nconst arr = [];\nlet temp = this.#front;\nwhile (temp !== null) {\narr.push(temp.val);\ntemp = temp.next;\n}\nconsole.log('[' + arr.join(', ') + ']');\n}\n}\n
    linkedlist_deque.ts
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\nprev: ListNode; // \u524d\u9a71\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\nnext: ListNode; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\nval: number; // \u8282\u70b9\u503c\nconstructor(val: number) {\nthis.val = val;\nthis.next = null;\nthis.prev = null;\n}\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\nprivate front: ListNode; // \u5934\u8282\u70b9 front\nprivate rear: ListNode; // \u5c3e\u8282\u70b9 rear\nprivate queSize: number; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\nconstructor() {\nthis.front = null;\nthis.rear = null;\nthis.queSize = 0;\n}\n/* \u961f\u5c3e\u5165\u961f\u64cd\u4f5c */\npushLast(val: number): void {\nconst node: ListNode = new ListNode(val);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (this.queSize === 0) {\nthis.front = node;\nthis.rear = node;\n} else {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\nthis.rear.next = node;\nnode.prev = this.rear;\nthis.rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nthis.queSize++;\n}\n/* \u961f\u9996\u5165\u961f\u64cd\u4f5c */\npushFirst(val: number): void {\nconst node: ListNode = new ListNode(val);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (this.queSize === 0) {\nthis.front = node;\nthis.rear = node;\n} else {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nthis.front.prev = node;\nnode.next = this.front;\nthis.front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n}\nthis.queSize++;\n}\n/* \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c */\npopLast(): number {\nif (this.queSize === 0) {\nreturn null;\n}\nconst value: number = this.rear.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5c3e\u8282\u70b9\nlet temp: ListNode = this.rear.prev;\nif (temp !== null) {\ntemp.next = null;\nthis.rear.prev = null;\n}\nthis.rear = temp; // \u66f4\u65b0\u5c3e\u8282\u70b9\nthis.queSize--;\nreturn value;\n}\n/* \u961f\u9996\u51fa\u961f\u64cd\u4f5c */\npopFirst(): number {\nif (this.queSize === 0) {\nreturn null;\n}\nconst value: number = this.front.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5934\u8282\u70b9\nlet temp: ListNode = this.front.next;\nif (temp !== null) {\ntemp.prev = null;\nthis.front.next = null;\n}\nthis.front = temp; // \u66f4\u65b0\u5934\u8282\u70b9\nthis.queSize--;\nreturn value;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npeekLast(): number {\nreturn this.queSize === 0 ? null : this.rear.val;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeekFirst(): number {\nreturn this.queSize === 0 ? null : this.front.val;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nsize(): number {\nreturn this.queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty(): boolean {\nreturn this.queSize === 0;\n}\n/* \u6253\u5370\u53cc\u5411\u961f\u5217 */\nprint(): void {\nconst arr: number[] = [];\nlet temp: ListNode = this.front;\nwhile (temp !== null) {\narr.push(temp.val);\ntemp = temp.next;\n}\nconsole.log('[' + arr.join(', ') + ']');\n}\n}\n
    linkedlist_deque.c
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nstruct doublyListNode {\nint val;                     // \u8282\u70b9\u503c\nstruct doublyListNode *next; // \u540e\u7ee7\u8282\u70b9\nstruct doublyListNode *prev; // \u524d\u9a71\u8282\u70b9\n};\ntypedef struct doublyListNode doublyListNode;\n/* \u6784\u9020\u51fd\u6570 */\ndoublyListNode *newDoublyListNode(int num) {\ndoublyListNode *new = (doublyListNode *)malloc(sizeof(doublyListNode));\nnew->val = num;\nnew->next = NULL;\nnew->prev = NULL;\nreturn new;\n}\n/* \u6790\u6784\u51fd\u6570 */\nvoid delDoublyListNode(doublyListNode *node) {\nfree(node);\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nstruct linkedListDeque {\ndoublyListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\nint queSize;                  // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n};\ntypedef struct linkedListDeque linkedListDeque;\n/* \u6784\u9020\u51fd\u6570 */\nlinkedListDeque *newLinkedListDeque() {\nlinkedListDeque *deque = (linkedListDeque *)malloc(sizeof(linkedListDeque));\ndeque->front = NULL;\ndeque->rear = NULL;\ndeque->queSize = 0;\nreturn deque;\n}\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListdeque(linkedListDeque *deque) {\n// \u91ca\u653e\u6240\u6709\u8282\u70b9\nfor (int i = 0; i < deque->queSize && deque->front != NULL; i++) {\ndoublyListNode *tmp = deque->front;\ndeque->front = deque->front->next;\nfree(tmp);\n}\n// \u91ca\u653e deque \u7ed3\u6784\u4f53\nfree(deque);\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(linkedListDeque *deque) {\nreturn deque->queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(linkedListDeque *deque) {\nreturn (size(deque) == 0);\n}\n/* \u5165\u961f */\nvoid push(linkedListDeque *deque, int num, bool isFront) {\ndoublyListNode *node = newDoublyListNode(num);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411node\nif (empty(deque)) {\ndeque->front = deque->rear = node;\n}\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\nelse if (isFront) {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\ndeque->front->prev = node;\nnode->next = deque->front;\ndeque->front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n}\n// \u5bf9\u5c3e\u5165\u961f\u64cd\u4f5c\nelse {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\ndeque->rear->next = node;\nnode->prev = deque->rear;\ndeque->rear = node;\n}\ndeque->queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(linkedListDeque *deque, int num) {\npush(deque, num, true);\n}\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(linkedListDeque *deque, int num) {\npush(deque, num, false);\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst(linkedListDeque *deque) {\nassert(size(deque) && deque->front);\nreturn deque->front->val;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast(linkedListDeque *deque) {\nassert(size(deque) && deque->rear);\nreturn deque->rear->val;\n}\n/* \u51fa\u961f */\nint pop(linkedListDeque *deque, bool isFront) {\nif (empty(deque))\nreturn -1;\nint val;\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif (isFront) {\nval = peekFirst(deque); // \u6682\u5b58\u5934\u8282\u70b9\u503c\ndoublyListNode *fNext = deque->front->next;\nif (fNext) {\nfNext->prev = NULL;\ndeque->front->next = NULL;\ndelDoublyListNode(deque->front);\n}\ndeque->front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n}\n// \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\nelse {\nval = peekLast(deque); // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\ndoublyListNode *rPrev = deque->rear->prev;\nif (rPrev) {\nrPrev->next = NULL;\ndeque->rear->prev = NULL;\ndelDoublyListNode(deque->rear);\n}\ndeque->rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\ndeque->queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val;\n}\n/* \u961f\u9996\u51fa\u961f */\nint popFirst(linkedListDeque *deque) {\nreturn pop(deque, true);\n}\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast(linkedListDeque *deque) {\nreturn pop(deque, false);\n}\n/* \u6253\u5370\u961f\u5217 */\nvoid printLinkedListDeque(linkedListDeque *deque) {\nint arr[deque->queSize];\n// \u62f7\u8d1d\u94fe\u8868\u4e2d\u7684\u6570\u636e\u5230\u6570\u7ec4\nint i;\ndoublyListNode *node;\nfor (i = 0, node = deque->front; i < deque->queSize; i++) {\narr[i] = node->val;\nnode = node->next;\n}\nprintArray(arr, deque->queSize);\n}\n
    linkedlist_deque.cs
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\npublic int val;       // \u8282\u70b9\u503c\npublic ListNode? next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\npublic ListNode? prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\npublic ListNode(int val) {\nthis.val = val;\nprev = null;\nnext = null;\n}\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\nprivate ListNode? front, rear; // \u5934\u8282\u70b9 front, \u5c3e\u8282\u70b9 rear\nprivate int queSize = 0;      // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\npublic LinkedListDeque() {\nfront = null;\nrear = null;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic bool isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u961f\u64cd\u4f5c */\nprivate void push(int num, bool isFront) {\nListNode node = new ListNode(num);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (isEmpty()) {\nfront = node;\nrear = node;\n}\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\nelse if (isFront) {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nfront.prev = node;\nnode.next = front;\nfront = node; // \u66f4\u65b0\u5934\u8282\u70b9                           \n}\n// \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\nelse {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\nrear.next = node;\nnode.prev = rear;\nrear = node;  // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nqueSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n/* \u961f\u9996\u5165\u961f */\npublic void pushFirst(int num) {\npush(num, true);\n}\n/* \u961f\u5c3e\u5165\u961f */\npublic void pushLast(int num) {\npush(num, false);\n}\n/* \u51fa\u961f\u64cd\u4f5c */\nprivate int? pop(bool isFront) {\n// \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de null\nif (isEmpty()) {\nreturn null;\n}\nint val;\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif (isFront) {\nval = front.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n// \u5220\u9664\u5934\u8282\u70b9\nListNode fNext = front.next;\nif (fNext != null) {\nfNext.prev = null;\nfront.next = null;\n}\nfront = fNext;   // \u66f4\u65b0\u5934\u8282\u70b9\n}\n// \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\nelse {\nval = rear.val;  // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5c3e\u8282\u70b9\nListNode rPrev = rear.prev;\nif (rPrev != null) {\nrPrev.next = null;\nrear.prev = null;\n}\nrear = rPrev;    // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nqueSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val;\n}\n/* \u961f\u9996\u51fa\u961f */\npublic int? popFirst() {\nreturn pop(true);\n}\n/* \u961f\u5c3e\u51fa\u961f */\npublic int? popLast() {\nreturn pop(false);\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int? peekFirst() {\nreturn isEmpty() ? null : front.val;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npublic int? peekLast() {\nreturn isEmpty() ? null : rear.val;\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\npublic int[] toArray() {\nListNode node = front;\nint[] res = new int[size()];\nfor (int i = 0; i < res.Length; i++) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n
    linkedlist_deque.swift
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\nvar val: Int // \u8282\u70b9\u503c\nvar next: ListNode? // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\nvar prev: ListNode? // \u524d\u9a71\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\ninit(val: Int) {\nself.val = val\n}\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\nprivate var front: ListNode? // \u5934\u8282\u70b9 front\nprivate var rear: ListNode? // \u5c3e\u8282\u70b9 rear\nprivate var queSize: Int // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\ninit() {\nqueSize = 0\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nfunc size() -> Int {\nqueSize\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc isEmpty() -> Bool {\nsize() == 0\n}\n/* \u5165\u961f\u64cd\u4f5c */\nprivate func push(num: Int, isFront: Bool) {\nlet node = ListNode(val: num)\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif isEmpty() {\nfront = node\nrear = node\n}\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\nelse if isFront {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nfront?.prev = node\nnode.next = front\nfront = node // \u66f4\u65b0\u5934\u8282\u70b9\n}\n// \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\nelse {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\nrear?.next = node\nnode.prev = rear\nrear = node // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nqueSize += 1 // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n/* \u961f\u9996\u5165\u961f */\nfunc pushFirst(num: Int) {\npush(num: num, isFront: true)\n}\n/* \u961f\u5c3e\u5165\u961f */\nfunc pushLast(num: Int) {\npush(num: num, isFront: false)\n}\n/* \u51fa\u961f\u64cd\u4f5c */\nprivate func pop(isFront: Bool) -> Int {\nif isEmpty() {\nfatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n}\nlet val: Int\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif isFront {\nval = front!.val // \u6682\u5b58\u5934\u8282\u70b9\u503c\n// \u5220\u9664\u5934\u8282\u70b9\nlet fNext = front?.next\nif fNext != nil {\nfNext?.prev = nil\nfront?.next = nil\n}\nfront = fNext // \u66f4\u65b0\u5934\u8282\u70b9\n}\n// \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\nelse {\nval = rear!.val // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5c3e\u8282\u70b9\nlet rPrev = rear?.prev\nif rPrev != nil {\nrPrev?.next = nil\nrear?.prev = nil\n}\nrear = rPrev // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nqueSize -= 1 // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val\n}\n/* \u961f\u9996\u51fa\u961f */\nfunc popFirst() -> Int {\npop(isFront: true)\n}\n/* \u961f\u5c3e\u51fa\u961f */\nfunc popLast() -> Int {\npop(isFront: false)\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc peekFirst() -> Int? {\nisEmpty() ? nil : front?.val\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc peekLast() -> Int? {\nisEmpty() ? nil : rear?.val\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nfunc toArray() -> [Int] {\nvar node = front\nvar res = Array(repeating: 0, count: size())\nfor i in res.indices {\nres[i] = node!.val\nnode = node?.next\n}\nreturn res\n}\n}\n
    linkedlist_deque.zig
    // \u53cc\u5411\u94fe\u8868\u8282\u70b9\nfn ListNode(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nval: T = undefined,     // \u8282\u70b9\u503c\nnext: ?*Self = null,    // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\nprev: ?*Self = null,    // \u524d\u9a71\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\n// Initialize a list node with specific value\npub fn init(self: *Self, x: i32) void {\nself.val = x;\nself.next = null;\nself.prev = null;\n}\n};\n}\n// \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\nfn LinkedListDeque(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nfront: ?*ListNode(T) = null,                    // \u5934\u8282\u70b9 front\nrear: ?*ListNode(T) = null,                     // \u5c3e\u8282\u70b9 rear\nque_size: usize = 0,                             // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\nmem_arena: ?std.heap.ArenaAllocator = null,\nmem_allocator: std.mem.Allocator = undefined,   // \u5185\u5b58\u5206\u914d\u5668\n// \u6784\u9020\u65b9\u6cd5\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u961f\u5217\uff09\npub fn init(self: *Self, allocator: std.mem.Allocator) !void {\nif (self.mem_arena == null) {\nself.mem_arena = std.heap.ArenaAllocator.init(allocator);\nself.mem_allocator = self.mem_arena.?.allocator();\n}\nself.front = null;\nself.rear = null;\nself.que_size = 0;\n}\n// \u6790\u6784\u65b9\u6cd5\uff08\u91ca\u653e\u5185\u5b58\uff09\npub fn deinit(self: *Self) void {\nif (self.mem_arena == null) return;\nself.mem_arena.?.deinit();\n}\n// \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\npub fn size(self: *Self) usize {\nreturn self.que_size;\n}\n// \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\npub fn isEmpty(self: *Self) bool {\nreturn self.size() == 0;\n}\n// \u5165\u961f\u64cd\u4f5c\npub fn push(self: *Self, num: T, is_front: bool) !void {\nvar node = try self.mem_allocator.create(ListNode(T));\nnode.init(num);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (self.isEmpty()) {\nself.front = node;\nself.rear = node;\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\n} else if (is_front) {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nself.front.?.prev = node;\nnode.next = self.front;\nself.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\nself.rear.?.next = node;\nnode.prev = self.rear;\nself.rear = node;   // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nself.que_size += 1;      // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n} // \u961f\u9996\u5165\u961f\npub fn pushFirst(self: *Self, num: T) !void {\ntry self.push(num, true);\n} // \u961f\u5c3e\u5165\u961f\npub fn pushLast(self: *Self, num: T) !void {\ntry self.push(num, false);\n} // \u51fa\u961f\u64cd\u4f5c\npub fn pop(self: *Self, is_front: bool) T {\nif (self.isEmpty()) @panic(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\nvar val: T = undefined;\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif (is_front) {\nval = self.front.?.val;     // \u6682\u5b58\u5934\u8282\u70b9\u503c\n// \u5220\u9664\u5934\u8282\u70b9\nvar fNext = self.front.?.next;\nif (fNext != null) {\nfNext.?.prev = null;\nself.front.?.next = null;\n}\nself.front = fNext;         // \u66f4\u65b0\u5934\u8282\u70b9\n// \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n} else {\nval = self.rear.?.val;      // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5c3e\u8282\u70b9\nvar rPrev = self.rear.?.prev;\nif (rPrev != null) {\nrPrev.?.next = null;\nself.rear.?.prev = null;\n}\nself.rear = rPrev;          // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nself.que_size -= 1;              // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val;\n} // \u961f\u9996\u51fa\u961f\npub fn popFirst(self: *Self) T {\nreturn self.pop(true);\n} // \u961f\u5c3e\u51fa\u961f\npub fn popLast(self: *Self) T {\nreturn self.pop(false);\n} // \u8bbf\u95ee\u961f\u9996\u5143\u7d20\npub fn peekFirst(self: *Self) T {\nif (self.isEmpty()) @panic(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\nreturn self.front.?.val;\n}  // \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\npub fn peekLast(self: *Self) T {\nif (self.isEmpty()) @panic(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\nreturn self.rear.?.val;\n}\n// \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370\npub fn toArray(self: *Self) ![]T {\nvar node = self.front;\nvar res = try self.mem_allocator.alloc(T, self.size());\nstd.mem.set(T, res, @as(T, 0));\nvar i: usize = 0;\nwhile (i < res.len) : (i += 1) {\nres[i] = node.?.val;\nnode = node.?.next;\n}\nreturn res;\n}\n};\n}\n
    linkedlist_deque.dart
    /* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\nint val; // \u8282\u70b9\u503c\nListNode? next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\nListNode? prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\nListNode(this.val, {this.next, this.prev});\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u5bf9\u5217 */\nclass LinkedListDeque {\nlate ListNode? _front; // \u5934\u8282\u70b9 _front\nlate ListNode? _rear; // \u5c3e\u8282\u70b9 _rear\nint _queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\nLinkedListDeque() {\nthis._front = null;\nthis._rear = null;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u957f\u5ea6 */\nint size() {\nreturn this._queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u961f\u64cd\u4f5c */\nvoid push(int num, bool isFront) {\nfinal ListNode node = ListNode(num);\nif (isEmpty()) {\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 _front\uff0c_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;\nnode.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;\nnode.prev = _rear;\n_rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\n_queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(int num) {\npush(num, true);\n}\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(int num) {\npush(num, false);\n}\n/* \u51fa\u961f\u64cd\u4f5c */\nint? pop(bool isFront) {\n// \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de null\nif (isEmpty()) {\nreturn null;\n}\nfinal int val;\nif (isFront) {\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nval = _front!.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n// \u5220\u9664\u5934\u8282\u70b9\nListNode? fNext = _front!.next;\nif (fNext != null) {\nfNext.prev = null;\n_front!.next = null;\n}\n_front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n} else {\n// \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\nval = _rear!.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5c3e\u8282\u70b9\nListNode? rPrev = _rear!.prev;\nif (rPrev != null) {\nrPrev.next = null;\n_rear!.prev = null;\n}\n_rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\n_queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val;\n}\n/* \u961f\u9996\u51fa\u961f */\nint? popFirst() {\nreturn pop(true);\n}\n/* \u961f\u5c3e\u51fa\u961f */\nint? popLast() {\nreturn pop(false);\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint? peekFirst() {\nreturn _front?.val;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint? peekLast() {\nreturn _rear?.val;\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nList<int> toArray() {\nListNode? node = _front;\nfinal List<int> res = [];\nfor (int i = 0; i < _queSize; i++) {\nres.add(node!.val);\nnode = node.next;\n}\nreturn res;\n}\n}\n
    "},{"location":"chapter_stack_and_queue/deque/#_2","title":"\u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"

    \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\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\u3002

    ArrayDequepushLast()pushFirst()popLast()popFirst()

    \u4ee5\u4e0b\u662f\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart array_deque.java
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\nprivate int[] nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate int queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n/* \u6784\u9020\u65b9\u6cd5 */\npublic ArrayDeque(int capacity) {\nthis.nums = new int[capacity];\nfront = queSize = 0;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\npublic int capacity() {\nreturn nums.length;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic boolean isEmpty() {\nreturn queSize == 0;\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nprivate 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\nreturn (i + capacity()) % capacity();\n}\n/* \u961f\u9996\u5165\u961f */\npublic void pushFirst(int num) {\nif (queSize == capacity()) {\nSystem.out.println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\nreturn;\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\nfront = index(front - 1);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nnums[front] = num;\nqueSize++;\n}\n/* \u961f\u5c3e\u5165\u961f */\npublic void pushLast(int num) {\nif (queSize == capacity()) {\nSystem.out.println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nint rear = index(front + queSize);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num;\nqueSize++;\n}\n/* \u961f\u9996\u51fa\u961f */\npublic int popFirst() {\nint num = peekFirst();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfront = index(front + 1);\nqueSize--;\nreturn num;\n}\n/* \u961f\u5c3e\u51fa\u961f */\npublic int popLast() {\nint num = peekLast();\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int peekFirst() {\nif (isEmpty())\nthrow new IndexOutOfBoundsException();\nreturn nums[front];\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npublic int peekLast() {\nif (isEmpty())\nthrow new IndexOutOfBoundsException();\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nint last = index(front + queSize - 1);\nreturn nums[last];\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\npublic int[] toArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nint[] res = new int[queSize];\nfor (int i = 0, j = front; i < queSize; i++, j++) {\nres[i] = nums[index(j)];\n}\nreturn res;\n}\n}\n
    array_deque.cpp
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\nprivate:\nvector<int> nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nint front;        // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nint queSize;      // \u53cc\u5411\u961f\u5217\u957f\u5ea6\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nArrayDeque(int capacity) {\nnums.resize(capacity);\nfront = queSize = 0;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity() {\nreturn nums.size();\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty() {\nreturn queSize == 0;\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nint 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\nreturn (i + capacity()) % capacity();\n}\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(int num) {\nif (queSize == capacity()) {\ncout << \"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\" << endl;\nreturn;\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\nfront = index(front - 1);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nnums[front] = num;\nqueSize++;\n}\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(int num) {\nif (queSize == capacity()) {\ncout << \"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\" << endl;\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nint rear = index(front + queSize);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num;\nqueSize++;\n}\n/* \u961f\u9996\u51fa\u961f */\nint popFirst() {\nint num = peekFirst();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfront = index(front + 1);\nqueSize--;\nreturn num;\n}\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast() {\nint num = peekLast();\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst() {\nif (isEmpty())\nthrow out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\nreturn nums[front];\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast() {\nif (isEmpty())\nthrow out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nint last = index(front + queSize - 1);\nreturn nums[last];\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nvector<int> toVector() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvector<int> res(queSize);\nfor (int i = 0, j = front; i < queSize; i++, j++) {\nres[i] = nums[index(j)];\n}\nreturn res;\n}\n};\n
    array_deque.py
    class ArrayDeque:\n\"\"\"\u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\"\"\"\ndef __init__(self, capacity: int) -> None:\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.__nums: list[int] = [0] * capacity\nself.__front: int = 0\nself.__size: int = 0\ndef capacity(self) -> int:\n\"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf\"\"\"\nreturn len(self.__nums)\ndef size(self) -> int:\n\"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\"\"\"\nreturn self.__size\ndef is_empty(self) -> bool:\n\"\"\"\u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\nreturn self.__size == 0\ndef 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\nreturn (i + self.capacity()) % self.capacity()\ndef push_first(self, num: int) -> None:\n\"\"\"\u961f\u9996\u5165\u961f\"\"\"\nif self.__size == self.capacity():\nprint(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\nreturn\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\nself.__front = self.index(self.__front - 1)\n# \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nself.__nums[self.__front] = num\nself.__size += 1\ndef push_last(self, num: int) -> None:\n\"\"\"\u961f\u5c3e\u5165\u961f\"\"\"\nif self.__size == self.capacity():\nprint(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\nreturn\n# \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nrear = self.index(self.__front + self.__size)\n# \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nself.__nums[rear] = num\nself.__size += 1\ndef pop_first(self) -> int:\n\"\"\"\u961f\u9996\u51fa\u961f\"\"\"\nnum = self.peek_first()\n# \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nself.__front = self.index(self.__front + 1)\nself.__size -= 1\nreturn num\ndef pop_last(self) -> int:\n\"\"\"\u961f\u5c3e\u51fa\u961f\"\"\"\nnum = self.peek_last()\nself.__size -= 1\nreturn num\ndef peek_first(self) -> int:\n\"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\nif self.is_empty():\nraise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\nreturn self.__nums[self.__front]\ndef peek_last(self) -> int:\n\"\"\"\u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\"\"\"\nif self.is_empty():\nraise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n# \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nlast = self.index(self.__front + self.__size - 1)\nreturn self.__nums[last]\ndef 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\nres = []\nfor i in range(self.__size):\nres.append(self.__nums[self.index(self.__front + i)])\nreturn res\n
    array_deque.go
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntype arrayDeque struct {\nnums        []int // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nfront       int   // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nqueSize     int   // \u53cc\u5411\u961f\u5217\u957f\u5ea6\nqueCapacity int   // \u961f\u5217\u5bb9\u91cf\uff08\u5373\u6700\u5927\u5bb9\u7eb3\u5143\u7d20\u6570\u91cf\uff09\n}\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newArrayDeque(queCapacity int) *arrayDeque {\nreturn &arrayDeque{\nnums:        make([]int, queCapacity),\nqueCapacity: queCapacity,\nfront:       0,\nqueSize:     0,\n}\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (q *arrayDeque) size() int {\nreturn q.queSize\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (q *arrayDeque) isEmpty() bool {\nreturn q.queSize == 0\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\nreturn (i + q.queCapacity) % q.queCapacity\n}\n/* \u961f\u9996\u5165\u961f */\nfunc (q *arrayDeque) pushFirst(num int) {\nif q.queSize == q.queCapacity {\nfmt.Println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\nreturn\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\nq.front = q.index(q.front - 1)\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nq.nums[q.front] = num\nq.queSize++\n}\n/* \u961f\u5c3e\u5165\u961f */\nfunc (q *arrayDeque) pushLast(num int) {\nif q.queSize == q.queCapacity {\nfmt.Println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\nreturn\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nrear := q.index(q.front + q.queSize)\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nq.nums[rear] = num\nq.queSize++\n}\n/* \u961f\u9996\u51fa\u961f */\nfunc (q *arrayDeque) popFirst() any {\nnum := q.peekFirst()\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nq.front = q.index(q.front + 1)\nq.queSize--\nreturn num\n}\n/* \u961f\u5c3e\u51fa\u961f */\nfunc (q *arrayDeque) popLast() any {\nnum := q.peekLast()\nq.queSize--\nreturn num\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (q *arrayDeque) peekFirst() any {\nif q.isEmpty() {\nreturn nil\n}\nreturn q.nums[q.front]\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc (q *arrayDeque) peekLast() any {\nif q.isEmpty() {\nreturn nil\n}\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nlast := q.index(q.front + q.queSize - 1)\nreturn q.nums[last]\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\nres := make([]int, q.queSize)\nfor i, j := 0, q.front; i < q.queSize; i++ {\nres[i] = q.nums[q.index(j)]\nj++\n}\nreturn res\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/* \u6784\u9020\u65b9\u6cd5 */\nconstructor(capacity) {\nthis.#nums = new Array(capacity);\nthis.#front = 0;\nthis.#queSize = 0;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\ncapacity() {\nreturn this.#nums.length;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nsize() {\nreturn this.#queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty() {\nreturn this.#queSize === 0;\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nindex(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\nreturn (i + this.capacity()) % this.capacity();\n}\n/* \u961f\u9996\u5165\u961f */\npushFirst(num) {\nif (this.#queSize === this.capacity()) {\nconsole.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\nreturn;\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\nthis.#front = this.index(this.#front - 1);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nthis.#nums[this.#front] = num;\nthis.#queSize++;\n}\n/* \u961f\u5c3e\u5165\u961f */\npushLast(num) {\nif (this.#queSize === this.capacity()) {\nconsole.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nconst rear = this.index(this.#front + this.#queSize);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nthis.#nums[rear] = num;\nthis.#queSize++;\n}\n/* \u961f\u9996\u51fa\u961f */\npopFirst() {\nconst num = this.peekFirst();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nthis.#front = this.index(this.#front + 1);\nthis.#queSize--;\nreturn num;\n}\n/* \u961f\u5c3e\u51fa\u961f */\npopLast() {\nconst num = this.peekLast();\nthis.#queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeekFirst() {\nif (this.isEmpty()) throw new Error('The Deque Is Empty.');\nreturn this.#nums[this.#front];\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npeekLast() {\nif (this.isEmpty()) throw new Error('The Deque Is Empty.');\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nconst last = this.index(this.#front + this.#queSize - 1);\nreturn this.#nums[last];\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\ntoArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nconst res = [];\nfor (let i = 0, j = this.#front; i < this.#queSize; i++, j++) {\nres[i] = this.#nums[this.index(j)];\n}\nreturn res;\n}\n}\n
    array_deque.ts
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\nprivate nums: number[]; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate front: number; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate queSize: number; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor(capacity: number) {\nthis.nums = new Array(capacity);\nthis.front = 0;\nthis.queSize = 0;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\ncapacity(): number {\nreturn this.nums.length;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nsize(): number {\nreturn this.queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty(): boolean {\nreturn this.queSize === 0;\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nindex(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\nreturn (i + this.capacity()) % this.capacity();\n}\n/* \u961f\u9996\u5165\u961f */\npushFirst(num: number): void {\nif (this.queSize === this.capacity()) {\nconsole.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\nreturn;\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\nthis.front = this.index(this.front - 1);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nthis.nums[this.front] = num;\nthis.queSize++;\n}\n/* \u961f\u5c3e\u5165\u961f */\npushLast(num: number): void {\nif (this.queSize === this.capacity()) {\nconsole.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nconst rear: number = this.index(this.front + this.queSize);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nthis.nums[rear] = num;\nthis.queSize++;\n}\n/* \u961f\u9996\u51fa\u961f */\npopFirst(): number {\nconst num: number = this.peekFirst();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nthis.front = this.index(this.front + 1);\nthis.queSize--;\nreturn num;\n}\n/* \u961f\u5c3e\u51fa\u961f */\npopLast(): number {\nconst num: number = this.peekLast();\nthis.queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeekFirst(): number {\nif (this.isEmpty()) throw new Error('The Deque Is Empty.');\nreturn this.nums[this.front];\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npeekLast(): number {\nif (this.isEmpty()) throw new Error('The Deque Is Empty.');\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nconst last = this.index(this.front + this.queSize - 1);\nreturn this.nums[last];\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\ntoArray(): number[] {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nconst res: number[] = [];\nfor (let i = 0, j = this.front; i < this.queSize; i++, j++) {\nres[i] = this.nums[this.index(j)];\n}\nreturn res;\n}\n}\n
    array_deque.c
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nstruct arrayDeque {\nint *nums;       // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nint front;       // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nint queSize;     // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\nint queCapacity; // \u961f\u5217\u5bb9\u91cf\n};\ntypedef struct arrayDeque arrayDeque;\n/* \u6784\u9020\u51fd\u6570 */\narrayDeque *newArrayDeque(int capacity) {\narrayDeque *deque = (arrayDeque *)malloc(sizeof(arrayDeque));\n// \u521d\u59cb\u5316\u6570\u7ec4\ndeque->queCapacity = capacity;\ndeque->nums = (int *)malloc(sizeof(int) * deque->queCapacity);\ndeque->front = deque->queSize = 0;\nreturn deque;\n}\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayDeque(arrayDeque *deque) {\nfree(deque->nums);\ndeque->queCapacity = 0;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity(arrayDeque *deque) {\nreturn deque->queCapacity;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size(arrayDeque *deque) {\nreturn deque->queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(arrayDeque *deque) {\nreturn deque->queSize == 0;\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\nreturn ((i + capacity(deque)) % capacity(deque));\n}\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(arrayDeque *deque, int num) {\nif (deque->queSize == capacity(deque)) {\nprintf(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\\r\\n\");\nreturn;\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\ndeque->front = dequeIndex(deque, deque->front - 1);\n// \u5c06 num \u6dfb\u52a0\u5230\u961f\u9996\ndeque->nums[deque->front] = num;\ndeque->queSize++;\n}\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(arrayDeque *deque, int num) {\nif (deque->queSize == capacity(deque)) {\nprintf(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\\r\\n\");\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nint rear = dequeIndex(deque, deque->front + deque->queSize);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque->nums[rear] = num;\ndeque->queSize++;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst(arrayDeque *deque) {\n// \u8bbf\u95ee\u5f02\u5e38\uff1a\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\nassert(empty(deque) == 0);\nreturn deque->nums[deque->front];\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast(arrayDeque *deque) {\n// \u8bbf\u95ee\u5f02\u5e38\uff1a\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\nassert(empty(deque) == 0);\nint last = dequeIndex(deque, deque->front + deque->queSize - 1);\nreturn deque->nums[last];\n}\n/* \u961f\u9996\u51fa\u961f */\nint popFirst(arrayDeque *deque) {\nint num = peekFirst(deque);\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\ndeque->front = dequeIndex(deque, deque->front + 1);\ndeque->queSize--;\nreturn num;\n}\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast(arrayDeque *deque) {\nint num = peekLast(deque);\ndeque->queSize--;\nreturn num;\n}\n/* \u6253\u5370\u961f\u5217 */\nvoid printArrayDeque(arrayDeque *deque) {\nint arr[deque->queSize];\n// \u62f7\u8d1d\nfor (int i = 0, j = deque->front; i < deque->queSize; i++, j++) {\narr[i] = deque->nums[j % deque->queCapacity];\n}\nprintArray(arr, deque->queSize);\n}\n
    array_deque.cs
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\nprivate readonly int[] nums;  // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate int front;   // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate int queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n/* \u6784\u9020\u65b9\u6cd5 */\npublic ArrayDeque(int capacity) {\nthis.nums = new int[capacity];\nfront = queSize = 0;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\npublic int capacity() {\nreturn nums.Length;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic bool isEmpty() {\nreturn queSize == 0;\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nprivate 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\nreturn (i + capacity()) % capacity();\n}\n/* \u961f\u9996\u5165\u961f */\npublic void pushFirst(int num) {\nif (queSize == capacity()) {\nConsole.WriteLine(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\nreturn;\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\nfront = index(front - 1);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nnums[front] = num;\nqueSize++;\n}\n/* \u961f\u5c3e\u5165\u961f */\npublic void pushLast(int num) {\nif (queSize == capacity()) {\nConsole.WriteLine(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nint rear = index(front + queSize);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num;\nqueSize++;\n}\n/* \u961f\u9996\u51fa\u961f */\npublic int popFirst() {\nint num = peekFirst();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfront = index(front + 1);\nqueSize--;\nreturn num;\n}\n/* \u961f\u5c3e\u51fa\u961f */\npublic int popLast() {\nint num = peekLast();\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int peekFirst() {\nif (isEmpty()) {\nthrow new InvalidOperationException();\n}\nreturn nums[front];\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npublic int peekLast() {\nif (isEmpty()) {\nthrow new InvalidOperationException();\n}\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nint last = index(front + queSize - 1);\nreturn nums[last];\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\npublic int[] toArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nint[] res = new int[queSize];\nfor (int i = 0, j = front; i < queSize; i++, j++) {\nres[i] = nums[index(j)];\n}\nreturn res;\n}\n}\n
    array_deque.swift
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\nprivate var nums: [Int] // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate var front: Int // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate var queSize: Int // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n/* \u6784\u9020\u65b9\u6cd5 */\ninit(capacity: Int) {\nnums = Array(repeating: 0, count: capacity)\nfront = 0\nqueSize = 0\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\nfunc capacity() -> Int {\nnums.count\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nfunc size() -> Int {\nqueSize\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc isEmpty() -> Bool {\nsize() == 0\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nprivate 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/* \u961f\u9996\u5165\u961f */\nfunc pushFirst(num: Int) {\nif size() == capacity() {\nprint(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\nreturn\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\nfront = index(i: front - 1)\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nnums[front] = num\nqueSize += 1\n}\n/* \u961f\u5c3e\u5165\u961f */\nfunc pushLast(num: Int) {\nif size() == capacity() {\nprint(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\nreturn\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nlet rear = index(i: front + size())\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num\nqueSize += 1\n}\n/* \u961f\u9996\u51fa\u961f */\nfunc popFirst() -> Int {\nlet num = peekFirst()\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfront = index(i: front + 1)\nqueSize -= 1\nreturn num\n}\n/* \u961f\u5c3e\u51fa\u961f */\nfunc popLast() -> Int {\nlet num = peekLast()\nqueSize -= 1\nreturn num\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc peekFirst() -> Int {\nif isEmpty() {\nfatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n}\nreturn nums[front]\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc peekLast() -> Int {\nif isEmpty() {\nfatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n}\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nlet last = index(i: front + size() - 1)\nreturn nums[last]\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nfunc toArray() -> [Int] {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvar res = Array(repeating: 0, count: size())\nfor (i, j) in sequence(first: (0, front), next: { $0 < self.size() - 1 ? ($0 + 1, $1 + 1) : nil }) {\nres[i] = nums[index(i: j)]\n}\nreturn res\n}\n}\n
    array_deque.zig
    [class]{ArrayDeque}-[func]{}\n
    array_deque.dart
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\nlate List<int> _nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nlate int _front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nlate int _queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n/* \u6784\u9020\u65b9\u6cd5 */\nArrayDeque(int capacity) {\nthis._nums = List.filled(capacity, 0);\nthis._front = this._queSize = 0;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity() {\nreturn _nums.length;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size() {\nreturn _queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty() {\nreturn _queSize == 0;\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nint 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\nreturn (i + capacity()) % capacity();\n}\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(int num) {\nif (_queSize == capacity()) {\nthrow 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/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(int num) {\nif (_queSize == capacity()) {\nthrow Exception(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nint rear = index(_front + _queSize);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n_nums[rear] = num;\n_queSize++;\n}\n/* \u961f\u9996\u51fa\u961f */\nint popFirst() {\nint num = peekFirst();\n// \u961f\u9996\u6307\u9488\u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n_front = index(_front + 1);\n_queSize--;\nreturn num;\n}\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast() {\nint num = peekLast();\n_queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst() {\nif (isEmpty()) {\nthrow Exception(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n}\nreturn _nums[_front];\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast() {\nif (isEmpty()) {\nthrow Exception(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n}\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nint last = index(_front + _queSize - 1);\nreturn _nums[last];\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nList<int> toArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nList<int> res = List.filled(_queSize, 0);\nfor (int i = 0, j = _front; i < _queSize; i++, j++) {\nres[i] = _nums[index(j)];\n}\nreturn res;\n}\n}\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\u5373\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

    "},{"location":"chapter_stack_and_queue/queue/","title":"5.2. \u00a0 \u961f\u5217","text":"

    \u300c\u961f\u5217 Queue\u300d\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u5148\u51fa\uff08First In, First Out\uff09\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\u7684\u5c3e\u90e8\uff0c\u800c\u4f4d\u4e8e\u961f\u5217\u5934\u90e8\u7684\u4eba\u9010\u4e2a\u79bb\u5f00\u3002

    \u6211\u4eec\u628a\u961f\u5217\u7684\u5934\u90e8\u79f0\u4e3a\u300c\u961f\u9996\u300d\uff0c\u5c3e\u90e8\u79f0\u4e3a\u300c\u961f\u5c3e\u300d\uff0c\u628a\u5c06\u5143\u7d20\u52a0\u5165\u961f\u5c3e\u7684\u64cd\u4f5c\u79f0\u4e3a\u300c\u5165\u961f\u300d\uff0c\u5220\u9664\u961f\u9996\u5143\u7d20\u7684\u64cd\u4f5c\u79f0\u4e3a\u300c\u51fa\u961f\u300d\u3002

    Fig. \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\u4e0b\u8868\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

    \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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart queue.java
    /* \u521d\u59cb\u5316\u961f\u5217 */\nQueue<Integer> queue = new LinkedList<>();\n/* \u5143\u7d20\u5165\u961f */\nqueue.offer(1);\nqueue.offer(3);\nqueue.offer(2);\nqueue.offer(5);\nqueue.offer(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek = queue.peek();\n/* \u5143\u7d20\u51fa\u961f */\nint pop = queue.poll();\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.size();\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = queue.isEmpty();\n
    queue.cpp
    /* \u521d\u59cb\u5316\u961f\u5217 */\nqueue<int> queue;\n/* \u5143\u7d20\u5165\u961f */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint front = queue.front();\n/* \u5143\u7d20\u51fa\u961f */\nqueue.pop();\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.size();\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty = queue.empty();\n
    queue.py
    # \u521d\u59cb\u5316\u961f\u5217\n# \u5728 Python \u4e2d\uff0c\u6211\u4eec\u4e00\u822c\u5c06\u53cc\u5411\u961f\u5217\u7c7b deque \u770b\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\u5efa\u8bae\nque: Deque[int] = collections.deque()\n# \u5143\u7d20\u5165\u961f\nque.append(1)\nque.append(3)\nque.append(2)\nque.append(5)\nque.append(4)\n# \u8bbf\u95ee\u961f\u9996\u5143\u7d20\nfront: int = que[0];\n# \u5143\u7d20\u51fa\u961f\npop: int = que.popleft()\n# \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\nsize: int = len(que)\n# \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = len(que) == 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/* \u5143\u7d20\u5165\u961f */\nqueue.PushBack(1)\nqueue.PushBack(3)\nqueue.PushBack(2)\nqueue.PushBack(5)\nqueue.PushBack(4)\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeek := queue.Front()\n/* \u5143\u7d20\u51fa\u961f */\npop := queue.Front()\nqueue.Remove(pop)\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nsize := queue.Len()\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty := queue.Len() == 0\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/* \u5143\u7d20\u5165\u961f */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nconst peek = queue[0];\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/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nconst size = queue.length;\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/* \u5143\u7d20\u5165\u961f */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nconst peek = queue[0];\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/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nconst size = queue.length;\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nconst empty = queue.length === 0;\n
    queue.c
    // C \u672a\u63d0\u4f9b\u5185\u7f6e\u961f\u5217\n
    queue.cs
    /* \u521d\u59cb\u5316\u961f\u5217 */\nQueue<int> queue = new();\n/* \u5143\u7d20\u5165\u961f */\nqueue.Enqueue(1);\nqueue.Enqueue(3);\nqueue.Enqueue(2);\nqueue.Enqueue(5);\nqueue.Enqueue(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek = queue.Peek();\n/* \u5143\u7d20\u51fa\u961f */\nint pop = queue.Dequeue();\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.Count();\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = queue.Count() == 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/* \u5143\u7d20\u5165\u961f */\nqueue.append(1)\nqueue.append(3)\nqueue.append(2)\nqueue.append(5)\nqueue.append(4)\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nlet peek = queue.first!\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/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = queue.count\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = queue.isEmpty\n
    queue.zig
    \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/* \u5143\u7d20\u5165\u961f */\nqueue.add(1);\nqueue.add(3);\nqueue.add(2);\nqueue.add(5);\nqueue.add(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek = queue.first;\n/* \u5143\u7d20\u51fa\u961f */\nint pop = queue.removeFirst();\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.length;\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = queue.isEmpty;\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\u56e0\u6b64\uff0c\u94fe\u8868\u548c\u6570\u7ec4\u90fd\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u961f\u5217\u3002

    "},{"location":"chapter_stack_and_queue/queue/#_1","title":"\u57fa\u4e8e\u94fe\u8868\u7684\u5b9e\u73b0","text":"

    \u5bf9\u4e8e\u94fe\u8868\u5b9e\u73b0\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u94fe\u8868\u7684\u300c\u5934\u8282\u70b9\u300d\u548c\u300c\u5c3e\u8282\u70b9\u300d\u5206\u522b\u89c6\u4e3a\u961f\u9996\u548c\u961f\u5c3e\uff0c\u89c4\u5b9a\u961f\u5c3e\u4ec5\u53ef\u6dfb\u52a0\u8282\u70b9\uff0c\u800c\u961f\u9996\u4ec5\u53ef\u5220\u9664\u8282\u70b9\u3002

    LinkedListQueuepush()pop()

    \u4ee5\u4e0b\u662f\u7528\u94fe\u8868\u5b9e\u73b0\u961f\u5217\u7684\u793a\u4f8b\u4ee3\u7801\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart linkedlist_queue.java
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\nprivate ListNode front, rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\nprivate int queSize = 0;\npublic LinkedListQueue() {\nfront = null;\nrear = null;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic boolean isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u961f */\npublic void push(int num) {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nListNode 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\nif (front == null) {\nfront = node;\nrear = node;\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n} else {\nrear.next = node;\nrear = node;\n}\nqueSize++;\n}\n/* \u51fa\u961f */\npublic int pop() {\nint num = peek();\n// \u5220\u9664\u5934\u8282\u70b9\nfront = front.next;\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int peek() {\nif (size() == 0)\nthrow new IndexOutOfBoundsException();\nreturn front.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npublic int[] toArray() {\nListNode node = front;\nint[] res = new int[size()];\nfor (int i = 0; i < res.length; i++) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n
    linkedlist_queue.cpp
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\nprivate:\nListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\nint queSize;\npublic:\nLinkedListQueue() {\nfront = nullptr;\nrear = nullptr;\nqueSize = 0;\n}\n~LinkedListQueue() {\n// \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\nfreeMemoryLinkedList(front);\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty() {\nreturn queSize == 0;\n}\n/* \u5165\u961f */\nvoid push(int num) {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nListNode *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\nif (front == nullptr) {\nfront = node;\nrear = node;\n}\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\nelse {\nrear->next = node;\nrear = node;\n}\nqueSize++;\n}\n/* \u51fa\u961f */\nvoid pop() {\nint num = peek();\n// \u5220\u9664\u5934\u8282\u70b9\nListNode *tmp = front;\nfront = front->next;\n// \u91ca\u653e\u5185\u5b58\ndelete tmp;\nqueSize--;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek() {\nif (size() == 0)\nthrow out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\nreturn front->val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Vector \u5e76\u8fd4\u56de */\nvector<int> toVector() {\nListNode *node = front;\nvector<int> res(size());\nfor (int i = 0; i < res.size(); i++) {\nres[i] = node->val;\nnode = node->next;\n}\nreturn res;\n}\n};\n
    linkedlist_queue.py
    class LinkedListQueue:\n\"\"\"\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217\"\"\"\ndef __init__(self):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.__front: ListNode | None = None  # \u5934\u8282\u70b9 front\nself.__rear: ListNode | None = None  # \u5c3e\u8282\u70b9 rear\nself.__size: int = 0\ndef size(self) -> int:\n\"\"\"\u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\"\"\"\nreturn self.__size\ndef is_empty(self) -> bool:\n\"\"\"\u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\nreturn not self.__front\ndef push(self, num: int) -> None:\n\"\"\"\u5165\u961f\"\"\"\n# \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nnode = ListNode(num)\n# \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\nif self.__front is None:\nself.__front = node\nself.__rear = node\n# \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\nelse:\nself.__rear.next = node\nself.__rear = node\nself.__size += 1\ndef pop(self) -> int:\n\"\"\"\u51fa\u961f\"\"\"\nnum = self.peek()\n# \u5220\u9664\u5934\u8282\u70b9\nself.__front = self.__front.next\nself.__size -= 1\nreturn num\ndef peek(self) -> int:\n\"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\nif self.size() == 0:\nprint(\"\u961f\u5217\u4e3a\u7a7a\")\nreturn False\nreturn self.__front.val\ndef to_list(self) -> list[int]:\n\"\"\"\u8f6c\u5316\u4e3a\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\nqueue = []\ntemp = self.__front\nwhile temp:\nqueue.append(temp.val)\ntemp = temp.next\nreturn queue\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\ndata *list.List\n}\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newLinkedListQueue() *linkedListQueue {\nreturn &linkedListQueue{\ndata: list.New(),\n}\n}\n/* \u5165\u961f */\nfunc (s *linkedListQueue) push(value any) {\ns.data.PushBack(value)\n}\n/* \u51fa\u961f */\nfunc (s *linkedListQueue) pop() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Front()\ns.data.Remove(e)\nreturn e.Value\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (s *linkedListQueue) peek() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Front()\nreturn e.Value\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (s *linkedListQueue) size() int {\nreturn s.data.Len()\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListQueue) isEmpty() bool {\nreturn s.data.Len() == 0\n}\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListQueue) toList() *list.List {\nreturn s.data\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;\nconstructor() {\nthis.#front = null;\nthis.#rear = null;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nget size() {\nreturn this.#queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty() {\nreturn this.size === 0;\n}\n/* \u5165\u961f */\npush(num) {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nconst 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\nif (!this.#front) {\nthis.#front = node;\nthis.#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 {\nthis.#rear.next = node;\nthis.#rear = node;\n}\nthis.#queSize++;\n}\n/* \u51fa\u961f */\npop() {\nconst num = this.peek();\n// \u5220\u9664\u5934\u8282\u70b9\nthis.#front = this.#front.next;\nthis.#queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeek() {\nif (this.size === 0) throw new Error('\u961f\u5217\u4e3a\u7a7a');\nreturn this.#front.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\ntoArray() {\nlet node = this.#front;\nconst res = new Array(this.size);\nfor (let i = 0; i < res.length; i++) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n
    linkedlist_queue.ts
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\nprivate front: ListNode | null; // \u5934\u8282\u70b9 front\nprivate rear: ListNode | null; // \u5c3e\u8282\u70b9 rear\nprivate queSize: number = 0;\nconstructor() {\nthis.front = null;\nthis.rear = null;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nget size(): number {\nreturn this.queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty(): boolean {\nreturn this.size === 0;\n}\n/* \u5165\u961f */\npush(num: number): void {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nconst 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\nif (!this.front) {\nthis.front = node;\nthis.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 {\nthis.rear!.next = node;\nthis.rear = node;\n}\nthis.queSize++;\n}\n/* \u51fa\u961f */\npop(): number {\nconst num = this.peek();\nif (!this.front) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n// \u5220\u9664\u5934\u8282\u70b9\nthis.front = this.front.next;\nthis.queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeek(): number {\nif (this.size === 0) throw new Error('\u961f\u5217\u4e3a\u7a7a');\nreturn this.front!.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\ntoArray(): number[] {\nlet node = this.front;\nconst res = new Array<number>(this.size);\nfor (let i = 0; i < res.length; i++) {\nres[i] = node!.val;\nnode = node!.next;\n}\nreturn res;\n}\n}\n
    linkedlist_queue.c
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nstruct linkedListQueue {\nListNode *front, *rear;\nint queSize;\n};\ntypedef struct linkedListQueue linkedListQueue;\n/* \u6784\u9020\u51fd\u6570 */\nlinkedListQueue *newLinkedListQueue() {\nlinkedListQueue *queue = (linkedListQueue *)malloc(sizeof(linkedListQueue));\nqueue->front = NULL;\nqueue->rear = NULL;\nqueue->queSize = 0;\nreturn queue;\n}\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListQueue(linkedListQueue *queue) {\n// \u91ca\u653e\u6240\u6709\u8282\u70b9\nfor (int i = 0; i < queue->queSize && queue->front != NULL; i++) {\nListNode *tmp = queue->front;\nqueue->front = queue->front->next;\nfree(tmp);\n}\n// \u91ca\u653e queue \u7ed3\u6784\u4f53\nfree(queue);\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(linkedListQueue *queue) {\nreturn queue->queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(linkedListQueue *queue) {\nreturn (size(queue) == 0);\n}\n/* \u5165\u961f */\nvoid push(linkedListQueue *queue, int num) {\n// \u5c3e\u8282\u70b9\u5904\u6dfb\u52a0 node\nListNode *node = newListNode(num);\n// \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\nif (queue->front == NULL) {\nqueue->front = node;\nqueue->rear = node;\n}\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\nelse {\nqueue->rear->next = node;\nqueue->rear = node;\n}\nqueue->queSize++;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek(linkedListQueue *queue) {\nassert(size(queue) && queue->front);\nreturn queue->front->val;\n}\n/* \u51fa\u961f */\nvoid pop(linkedListQueue *queue) {\nint num = peek(queue);\nListNode *tmp = queue->front;\nqueue->front = queue->front->next;\nfree(tmp);\nqueue->queSize--;\n}\n/* \u6253\u5370\u961f\u5217 */\nvoid printLinkedListQueue(linkedListQueue *queue) {\nint arr[queue->queSize];\n// \u62f7\u8d1d\u94fe\u8868\u4e2d\u7684\u6570\u636e\u5230\u6570\u7ec4\nint i;\nListNode *node;\nfor (i = 0, node = queue->front; i < queue->queSize && queue->front != queue->rear; i++) {\narr[i] = node->val;\nnode = node->next;\n}\nprintArray(arr, queue->queSize);\n}\n
    linkedlist_queue.cs
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\nprivate ListNode? front, rear;  // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear \nprivate int queSize = 0;\npublic LinkedListQueue() {\nfront = null;\nrear = null;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic bool isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u961f */\npublic void push(int num) {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nListNode 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\nif (front == null) {\nfront = node;\nrear = 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) {\nrear.next = node;\nrear = node;\n}\nqueSize++;\n}\n/* \u51fa\u961f */\npublic int pop() {\nint num = peek();\n// \u5220\u9664\u5934\u8282\u70b9\nfront = front?.next;\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int peek() {\nif (size() == 0 || front == null)\nthrow new Exception();\nreturn front.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npublic int[] toArray() {\nif (front == null)\nreturn Array.Empty<int>();\nListNode node = front;\nint[] res = new int[size()];\nfor (int i = 0; i < res.Length; i++) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n
    linkedlist_queue.swift
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\nprivate var front: ListNode? // \u5934\u8282\u70b9\nprivate var rear: ListNode? // \u5c3e\u8282\u70b9\nprivate var _size = 0\ninit() {}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc size() -> Int {\n_size\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc isEmpty() -> Bool {\nsize() == 0\n}\n/* \u5165\u961f */\nfunc push(num: Int) {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nlet 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\nif front == nil {\nfront = node\nrear = node\n}\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\nelse {\nrear?.next = node\nrear = node\n}\n_size += 1\n}\n/* \u51fa\u961f */\n@discardableResult\nfunc pop() -> Int {\nlet num = peek()\n// \u5220\u9664\u5934\u8282\u70b9\nfront = front?.next\n_size -= 1\nreturn num\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc peek() -> Int {\nif isEmpty() {\nfatalError(\"\u961f\u5217\u4e3a\u7a7a\")\n}\nreturn front!.val\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\nfunc toArray() -> [Int] {\nvar node = front\nvar res = Array(repeating: 0, count: size())\nfor i in res.indices {\nres[i] = node!.val\nnode = node?.next\n}\nreturn res\n}\n}\n
    linkedlist_queue.zig
    // \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217\nfn LinkedListQueue(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nfront: ?*inc.ListNode(T) = null,                // \u5934\u8282\u70b9 front\nrear: ?*inc.ListNode(T) = null,                 // \u5c3e\u8282\u70b9 rear\nque_size: usize = 0,                             // \u961f\u5217\u7684\u957f\u5ea6\nmem_arena: ?std.heap.ArenaAllocator = null,\nmem_allocator: std.mem.Allocator = undefined,   // \u5185\u5b58\u5206\u914d\u5668\n// \u6784\u9020\u65b9\u6cd5\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u961f\u5217\uff09\npub fn init(self: *Self, allocator: std.mem.Allocator) !void {\nif (self.mem_arena == null) {\nself.mem_arena = std.heap.ArenaAllocator.init(allocator);\nself.mem_allocator = self.mem_arena.?.allocator();\n}\nself.front = null;\nself.rear = null;\nself.que_size = 0;\n}\n// \u6790\u6784\u65b9\u6cd5\uff08\u91ca\u653e\u5185\u5b58\uff09\npub fn deinit(self: *Self) void {\nif (self.mem_arena == null) return;\nself.mem_arena.?.deinit();\n}\n// \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\npub fn size(self: *Self) usize {\nreturn self.que_size;\n}\n// \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\npub fn isEmpty(self: *Self) bool {\nreturn self.size() == 0;\n}\n// \u8bbf\u95ee\u961f\u9996\u5143\u7d20\npub fn peek(self: *Self) T {\nif (self.size() == 0) @panic(\"\u961f\u5217\u4e3a\u7a7a\");\nreturn self.front.?.val;\n}  // \u5165\u961f\npub fn push(self: *Self, num: T) !void {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nvar node = try self.mem_allocator.create(inc.ListNode(T));\nnode.init(num);\n// \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\nif (self.front == null) {\nself.front = node;\nself.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 {\nself.rear.?.next = node;\nself.rear = node;\n}\nself.que_size += 1;\n} // \u51fa\u961f\npub fn pop(self: *Self) T {\nvar num = self.peek();\n// \u5220\u9664\u5934\u8282\u70b9\nself.front = self.front.?.next;\nself.que_size -= 1;\nreturn num;\n} // \u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\npub fn toArray(self: *Self) ![]T {\nvar node = self.front;\nvar res = try self.mem_allocator.alloc(T, self.size());\nstd.mem.set(T, res, @as(T, 0));\nvar i: usize = 0;\nwhile (i < res.len) : (i += 1) {\nres[i] = node.?.val;\nnode = node.?.next;\n}\nreturn res;\n}\n};\n}\n
    linkedlist_queue.dart
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\nListNode? _front; // \u5934\u8282\u70b9 _front\nListNode? _rear; // \u5c3e\u8282\u70b9 _rear\nint _queSize = 0; // \u961f\u5217\u957f\u5ea6\nLinkedListQueue() {\n_front = null;\n_rear = null;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size() {\nreturn _queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty() {\nreturn _queSize == 0;\n}\n/* \u5165\u961f */\nvoid push(int num) {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nfinal node = ListNode(num);\n// \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\nif (_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/* \u51fa\u961f */\nint pop() {\nfinal int num = peek();\n// \u5220\u9664\u5934\u8282\u70b9\n_front = _front!.next;\n_queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek() {\nif (_queSize == 0) {\nthrow Exception('\u961f\u5217\u4e3a\u7a7a');\n}\nreturn _front!.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\nList<int> toArray() {\nListNode? node = _front;\nfinal List<int> queue = [];\nwhile (node != null) {\nqueue.add(node.val);\nnode = node.next;\n}\nreturn queue;\n}\n}\n
    "},{"location":"chapter_stack_and_queue/queue/#_2","title":"\u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"

    \u7531\u4e8e\u6570\u7ec4\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 queSize \u7528\u4e8e\u8bb0\u5f55\u961f\u5217\u957f\u5ea6\u3002\u5b9a\u4e49 rear = front + queSize \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\u8fdb\u800c\uff1a

    • \u5bf9\u4e8e\u5165\u961f\u64cd\u4f5c\uff0c\u5c06\u8f93\u5165\u5143\u7d20\u8d4b\u503c\u7ed9 rear \u7d22\u5f15\u5904\uff0c\u5e76\u5c06 queSize \u589e\u52a0 1 \uff1b
    • \u5bf9\u4e8e\u51fa\u961f\u64cd\u4f5c\uff0c\u53ea\u9700\u5c06 front \u589e\u52a0 1 \uff0c\u5e76\u5c06 queSize \u51cf\u5c11 1 \uff1b

    \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()

    \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\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u89c6\u4e3a\u9996\u5c3e\u76f8\u63a5\u7684\u300c\u73af\u5f62\u6570\u7ec4\u300d\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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart array_queue.java
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\nprivate int[] nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate int queSize; // \u961f\u5217\u957f\u5ea6\npublic ArrayQueue(int capacity) {\nnums = new int[capacity];\nfront = queSize = 0;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\npublic int capacity() {\nreturn nums.length;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic boolean isEmpty() {\nreturn queSize == 0;\n}\n/* \u5165\u961f */\npublic void push(int num) {\nif (queSize == capacity()) {\nSystem.out.println(\"\u961f\u5217\u5df2\u6ee1\");\nreturn;\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\nint rear = (front + queSize) % capacity();\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num;\nqueSize++;\n}\n/* \u51fa\u961f */\npublic int pop() {\nint 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\nfront = (front + 1) % capacity();\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int peek() {\nif (isEmpty())\nthrow new IndexOutOfBoundsException();\nreturn nums[front];\n}\n/* \u8fd4\u56de\u6570\u7ec4 */\npublic int[] toArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nint[] res = new int[queSize];\nfor (int i = 0, j = front; i < queSize; i++, j++) {\nres[i] = nums[j % capacity()];\n}\nreturn res;\n}\n}\n
    array_queue.cpp
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\nprivate:\nint *nums;       // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nint front;       // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nint queSize;     // \u961f\u5217\u957f\u5ea6\nint queCapacity; // \u961f\u5217\u5bb9\u91cf\npublic:\nArrayQueue(int capacity) {\n// \u521d\u59cb\u5316\u6570\u7ec4\nnums = new int[capacity];\nqueCapacity = capacity;\nfront = queSize = 0;\n}\n~ArrayQueue() {\ndelete[] nums;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity() {\nreturn queCapacity;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty() {\nreturn size() == 0;\n}\n/* \u5165\u961f */\nvoid push(int num) {\nif (queSize == queCapacity) {\ncout << \"\u961f\u5217\u5df2\u6ee1\" << endl;\nreturn;\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\nint rear = (front + queSize) % queCapacity;\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num;\nqueSize++;\n}\n/* \u51fa\u961f */\nvoid pop() {\nint 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\nfront = (front + 1) % queCapacity;\nqueSize--;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek() {\nif (empty())\nthrow out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\nreturn nums[front];\n}\n/* \u5c06\u6570\u7ec4\u8f6c\u5316\u4e3a Vector \u5e76\u8fd4\u56de */\nvector<int> toVector() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvector<int> arr(queSize);\nfor (int i = 0, j = front; i < queSize; i++, j++) {\narr[i] = nums[j % queCapacity];\n}\nreturn arr;\n}\n};\n
    array_queue.py
    class ArrayQueue:\n\"\"\"\u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217\"\"\"\ndef __init__(self, size: int) -> None:\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.__nums: list[int] = [0] * size  # \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nself.__front: int = 0  # \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nself.__size: int = 0  # \u961f\u5217\u957f\u5ea6\ndef capacity(self) -> int:\n\"\"\"\u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf\"\"\"\nreturn len(self.__nums)\ndef size(self) -> int:\n\"\"\"\u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\"\"\"\nreturn self.__size\ndef is_empty(self) -> bool:\n\"\"\"\u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\nreturn self.__size == 0\ndef push(self, num: int) -> None:\n\"\"\"\u5165\u961f\"\"\"\nif self.__size == self.capacity():\nraise 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\u90e8F\nrear: int = (self.__front + self.__size) % self.capacity()\n# \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nself.__nums[rear] = num\nself.__size += 1\ndef pop(self) -> int:\n\"\"\"\u51fa\u961f\"\"\"\nnum: 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\nself.__front = (self.__front + 1) % self.capacity()\nself.__size -= 1\nreturn num\ndef peek(self) -> int:\n\"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\nif self.is_empty():\nraise IndexError(\"\u961f\u5217\u4e3a\u7a7a\")\nreturn self.__nums[self.__front]\ndef to_list(self) -> list[int]:\n\"\"\"\u8fd4\u56de\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\nres = [0] * self.size()\nj: int = self.__front\nfor i in range(self.size()):\nres[i] = self.__nums[(j % self.capacity())]\nj += 1\nreturn res\n
    array_queue.go
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\ntype arrayQueue struct {\nnums        []int // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nfront       int   // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nqueSize     int   // \u961f\u5217\u957f\u5ea6\nqueCapacity int   // \u961f\u5217\u5bb9\u91cf\uff08\u5373\u6700\u5927\u5bb9\u7eb3\u5143\u7d20\u6570\u91cf\uff09\n}\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newArrayQueue(queCapacity int) *arrayQueue {\nreturn &arrayQueue{\nnums:        make([]int, queCapacity),\nqueCapacity: queCapacity,\nfront:       0,\nqueSize:     0,\n}\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (q *arrayQueue) size() int {\nreturn q.queSize\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (q *arrayQueue) isEmpty() bool {\nreturn q.queSize == 0\n}\n/* \u5165\u961f */\nfunc (q *arrayQueue) push(num int) {\n// \u5f53 rear == queCapacity \u8868\u793a\u961f\u5217\u5df2\u6ee1\nif q.queSize == q.queCapacity {\nreturn\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\nrear := (q.front + q.queSize) % q.queCapacity\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nq.nums[rear] = num\nq.queSize++\n}\n/* \u51fa\u961f */\nfunc (q *arrayQueue) pop() any {\nnum := 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\nq.front = (q.front + 1) % q.queCapacity\nq.queSize--\nreturn num\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (q *arrayQueue) peek() any {\nif q.isEmpty() {\nreturn nil\n}\nreturn q.nums[q.front]\n}\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (q *arrayQueue) toSlice() []int {\nrear := (q.front + q.queSize)\nif rear >= q.queCapacity {\nrear %= q.queCapacity\nreturn append(q.nums[q.front:], q.nums[:rear]...)\n}\nreturn q.nums[q.front:rear]\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\nconstructor(capacity) {\nthis.#nums = new Array(capacity);\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nget capacity() {\nreturn this.#nums.length;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nget size() {\nreturn this.#queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nempty() {\nreturn this.#queSize == 0;\n}\n/* \u5165\u961f */\npush(num) {\nif (this.size == this.capacity) {\nconsole.log('\u961f\u5217\u5df2\u6ee1');\nreturn;\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\nconst rear = (this.#front + this.size) % this.capacity;\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nthis.#nums[rear] = num;\nthis.#queSize++;\n}\n/* \u51fa\u961f */\npop() {\nconst 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\nthis.#front = (this.#front + 1) % this.capacity;\nthis.#queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeek() {\nif (this.empty()) throw new Error('\u961f\u5217\u4e3a\u7a7a');\nreturn this.#nums[this.#front];\n}\n/* \u8fd4\u56de Array */\ntoArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nconst arr = new Array(this.size);\nfor (let i = 0, j = this.#front; i < this.size; i++, j++) {\narr[i] = this.#nums[j % this.capacity];\n}\nreturn arr;\n}\n}\n
    array_queue.ts
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\nprivate nums: number[]; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate front: number; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate queSize: number; // \u961f\u5217\u957f\u5ea6\nconstructor(capacity: number) {\nthis.nums = new Array(capacity);\nthis.front = this.queSize = 0;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nget capacity(): number {\nreturn this.nums.length;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nget size(): number {\nreturn this.queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nempty(): boolean {\nreturn this.queSize == 0;\n}\n/* \u5165\u961f */\npush(num: number): void {\nif (this.size == this.capacity) {\nconsole.log('\u961f\u5217\u5df2\u6ee1');\nreturn;\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\nconst rear = (this.front + this.queSize) % this.capacity;\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nthis.nums[rear] = num;\nthis.queSize++;\n}\n/* \u51fa\u961f */\npop(): number {\nconst 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\nthis.front = (this.front + 1) % this.capacity;\nthis.queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeek(): number {\nif (this.empty()) throw new Error('\u961f\u5217\u4e3a\u7a7a');\nreturn this.nums[this.front];\n}\n/* \u8fd4\u56de Array */\ntoArray(): number[] {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nconst arr = new Array(this.size);\nfor (let i = 0, j = this.front; i < this.size; i++, j++) {\narr[i] = this.nums[j % this.capacity];\n}\nreturn arr;\n}\n}\n
    array_queue.c
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nstruct arrayQueue {\nint *nums;       // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nint front;       // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nint queSize;     // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\nint queCapacity; // \u961f\u5217\u5bb9\u91cf\n};\ntypedef struct arrayQueue arrayQueue;\n/* \u6784\u9020\u51fd\u6570 */\narrayQueue *newArrayQueue(int capacity) {\narrayQueue *queue = (arrayQueue *)malloc(sizeof(arrayQueue));\n// \u521d\u59cb\u5316\u6570\u7ec4\nqueue->queCapacity = capacity;\nqueue->nums = (int *)malloc(sizeof(int) * queue->queCapacity);\nqueue->front = queue->queSize = 0;\nreturn queue;\n}\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayQueue(arrayQueue *queue) {\nfree(queue->nums);\nqueue->queCapacity = 0;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity(arrayQueue *queue) {\nreturn queue->queCapacity;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(arrayQueue *queue) {\nreturn queue->queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(arrayQueue *queue) {\nreturn queue->queSize == 0;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek(arrayQueue *queue) {\nassert(size(queue) != 0);\nreturn queue->nums[queue->front];\n}\n/* \u5165\u961f */\nvoid push(arrayQueue *queue, int num) {\nif (size(queue) == capacity(queue)) {\nprintf(\"\u961f\u5217\u5df2\u6ee1\\r\\n\");\nreturn;\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\nint rear = (queue->front + queue->queSize) % queue->queCapacity;\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nqueue->nums[rear] = num;\nqueue->queSize++;\n}\n/* \u51fa\u961f */\nvoid pop(arrayQueue *queue) {\nint 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\nqueue->front = (queue->front + 1) % queue->queCapacity;\nqueue->queSize--;\n}\n/* \u6253\u5370\u961f\u5217 */\nvoid printArrayQueue(arrayQueue *queue) {\nint arr[queue->queSize];\n// \u62f7\u8d1d\nfor (int i = 0, j = queue->front; i < queue->queSize; i++, j++) {\narr[i] = queue->nums[j % queue->queCapacity];\n}\nprintArray(arr, queue->queSize);\n}\n
    array_queue.cs
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\nprivate int[] nums;  // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate int front;   // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate int queSize; // \u961f\u5217\u957f\u5ea6\npublic ArrayQueue(int capacity) {\nnums = new int[capacity];\nfront = queSize = 0;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\npublic int capacity() {\nreturn nums.Length;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic bool isEmpty() {\nreturn queSize == 0;\n}\n/* \u5165\u961f */\npublic void push(int num) {\nif (queSize == capacity()) {\nConsole.WriteLine(\"\u961f\u5217\u5df2\u6ee1\");\nreturn;\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\nint rear = (front + queSize) % capacity();\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num;\nqueSize++;\n}\n/* \u51fa\u961f */\npublic int pop() {\nint 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\nfront = (front + 1) % capacity();\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int peek() {\nif (isEmpty())\nthrow new Exception();\nreturn nums[front];\n}\n/* \u8fd4\u56de\u6570\u7ec4 */\npublic int[] toArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nint[] res = new int[queSize];\nfor (int i = 0, j = front; i < queSize; i++, j++) {\nres[i] = nums[j % this.capacity()];\n}\nreturn res;\n}\n}\n
    array_queue.swift
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\nprivate var nums: [Int] // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate var front = 0 // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate var queSize = 0 // \u961f\u5217\u957f\u5ea6\ninit(capacity: Int) {\n// \u521d\u59cb\u5316\u6570\u7ec4\nnums = Array(repeating: 0, count: capacity)\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nfunc capacity() -> Int {\nnums.count\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc size() -> Int {\nqueSize\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc isEmpty() -> Bool {\nqueSize == 0\n}\n/* \u5165\u961f */\nfunc push(num: Int) {\nif size() == capacity() {\nprint(\"\u961f\u5217\u5df2\u6ee1\")\nreturn\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\nlet rear = (front + queSize) % capacity()\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num\nqueSize += 1\n}\n/* \u51fa\u961f */\n@discardableResult\nfunc pop() -> Int {\nlet 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\nfront = (front + 1) % capacity()\nqueSize -= 1\nreturn num\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc peek() -> Int {\nif isEmpty() {\nfatalError(\"\u961f\u5217\u4e3a\u7a7a\")\n}\nreturn nums[front]\n}\n/* \u8fd4\u56de\u6570\u7ec4 */\nfunc toArray() -> [Int] {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvar res = Array(repeating: 0, count: queSize)\nfor (i, j) in sequence(first: (0, front), next: { $0 < self.queSize - 1 ? ($0 + 1, $1 + 1) : nil }) {\nres[i] = nums[j % capacity()]\n}\nreturn res\n}\n}\n
    array_queue.zig
    // \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217\nfn ArrayQueue(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nnums: []T = undefined,                          // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4     \ncap: usize = 0,                                 // \u961f\u5217\u5bb9\u91cf\nfront: usize = 0,                               // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nque_size: usize = 0,                             // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\nmem_arena: ?std.heap.ArenaAllocator = null,\nmem_allocator: std.mem.Allocator = undefined,   // \u5185\u5b58\u5206\u914d\u5668\n// \u6784\u9020\u65b9\u6cd5\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u6570\u7ec4\uff09\npub fn init(self: *Self, allocator: std.mem.Allocator, cap: usize) !void {\nif (self.mem_arena == null) {\nself.mem_arena = std.heap.ArenaAllocator.init(allocator);\nself.mem_allocator = self.mem_arena.?.allocator();\n}\nself.cap = cap;\nself.nums = try self.mem_allocator.alloc(T, self.cap);\nstd.mem.set(T, self.nums, @as(T, 0));\n}\n// \u6790\u6784\u65b9\u6cd5\uff08\u91ca\u653e\u5185\u5b58\uff09\npub fn deinit(self: *Self) void {\nif (self.mem_arena == null) return;\nself.mem_arena.?.deinit();\n}\n// \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf\npub fn capacity(self: *Self) usize {\nreturn self.cap;\n}\n// \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\npub fn size(self: *Self) usize {\nreturn self.que_size;\n}\n// \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\npub fn isEmpty(self: *Self) bool {\nreturn self.que_size == 0;\n}\n// \u5165\u961f\npub fn push(self: *Self, num: T) !void {\nif (self.size() == self.capacity()) {\nstd.debug.print(\"\u961f\u5217\u5df2\u6ee1\\n\", .{});\nreturn;\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\nvar rear = (self.front + self.que_size) % self.capacity();\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nself.nums[rear] = num;\nself.que_size += 1;\n} // \u51fa\u961f\npub fn pop(self: *Self) T {\nvar 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\nself.front = (self.front + 1) % self.capacity();\nself.que_size -= 1;\nreturn num;\n} // \u8bbf\u95ee\u961f\u9996\u5143\u7d20\npub fn peek(self: *Self) T {\nif (self.isEmpty()) @panic(\"\u961f\u5217\u4e3a\u7a7a\");\nreturn self.nums[self.front];\n} // \u8fd4\u56de\u6570\u7ec4\npub fn toArray(self: *Self) ![]T {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvar res = try self.mem_allocator.alloc(T, self.size());\nstd.mem.set(T, res, @as(T, 0));\nvar i: usize = 0;\nvar j: usize = self.front;\nwhile (i < self.size()) : ({ i += 1; j += 1; }) {\nres[i] = self.nums[j % self.capacity()];\n}\nreturn res;\n}\n};\n}\n
    array_queue.dart
    /* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\nlate List<int> _nums; // \u7528\u4e8e\u50a8\u5b58\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nlate int _front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nlate int _queSize; // \u961f\u5217\u957f\u5ea6\nArrayQueue(int capacity) {\n_nums = List.filled(capacity, 0);\n_front = _queSize = 0;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nint capaCity() {\nreturn _nums.length;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size() {\nreturn _queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty() {\nreturn _queSize == 0;\n}\n/* \u5165\u961f */\nvoid push(int num) {\nif (_queSize == capaCity()) {\nthrow 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\nint rear = (_front + _queSize) % capaCity();\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n_nums[rear] = num;\n_queSize++;\n}\n/* \u51fa\u961f */\nint pop() {\nint 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--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek() {\nif (isEmpty()) {\nthrow Exception(\"\u961f\u5217\u4e3a\u7a7a\");\n}\nreturn _nums[_front];\n}\n/* \u8fd4\u56de Array */\nList<int> toArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nfinal List<int> res = List.filled(_queSize, 0);\nfor (int i = 0, j = _front; i < _queSize; i++, j++) {\nres[i] = _nums[j % capaCity()];\n}\nreturn res;\n}\n}\n

    \u4ee5\u4e0a\u5b9e\u73b0\u7684\u961f\u5217\u4ecd\u7136\u5177\u6709\u5c40\u9650\u6027\uff0c\u5373\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\u540c\u5b66\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":"
    • \u6dd8\u5b9d\u8ba2\u5355\u3002\u8d2d\u7269\u8005\u4e0b\u5355\u540e\uff0c\u8ba2\u5355\u5c06\u52a0\u5165\u961f\u5217\u4e2d\uff0c\u7cfb\u7edf\u968f\u540e\u4f1a\u6839\u636e\u987a\u5e8f\u4f9d\u6b21\u5904\u7406\u961f\u5217\u4e2d\u7684\u8ba2\u5355\u3002\u5728\u53cc\u5341\u4e00\u671f\u95f4\uff0c\u77ed\u65f6\u95f4\u5185\u4f1a\u4ea7\u751f\u6d77\u91cf\u8ba2\u5355\uff0c\u9ad8\u5e76\u53d1\u6210\u4e3a\u5de5\u7a0b\u5e08\u4eec\u9700\u8981\u91cd\u70b9\u653b\u514b\u7684\u95ee\u9898\u3002
    • \u5404\u7c7b\u5f85\u529e\u4e8b\u9879\u3002\u4efb\u4f55\u9700\u8981\u5b9e\u73b0\u201c\u5148\u6765\u540e\u5230\u201d\u529f\u80fd\u7684\u573a\u666f\uff0c\u4f8b\u5982\u6253\u5370\u673a\u7684\u4efb\u52a1\u961f\u5217\u3001\u9910\u5385\u7684\u51fa\u9910\u961f\u5217\u7b49\u3002\u961f\u5217\u5728\u8fd9\u4e9b\u573a\u666f\u4e2d\u53ef\u4ee5\u6709\u6548\u5730\u7ef4\u62a4\u5904\u7406\u987a\u5e8f\u3002
    "},{"location":"chapter_stack_and_queue/stack/","title":"5.1. \u00a0 \u6808","text":"

    \u300c\u6808 Stack\u300d\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u540e\u51fa\uff08First In, Last Out\uff09\u539f\u5219\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\u9700\u8981\u62ff\u51fa\u5e95\u90e8\u7684\u76d8\u5b50\uff0c\u5219\u9700\u8981\u5148\u5c06\u4e0a\u9762\u7684\u76d8\u5b50\u4f9d\u6b21\u53d6\u51fa\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\u6570\u636e\u7ed3\u6784\u3002

    \u5728\u6808\u4e2d\uff0c\u6211\u4eec\u628a\u5806\u53e0\u5143\u7d20\u7684\u9876\u90e8\u79f0\u4e3a\u300c\u6808\u9876\u300d\uff0c\u5e95\u90e8\u79f0\u4e3a\u300c\u6808\u5e95\u300d\u3002\u5c06\u628a\u5143\u7d20\u6dfb\u52a0\u5230\u6808\u9876\u7684\u64cd\u4f5c\u53eb\u505a\u300c\u5165\u6808\u300d\uff0c\u800c\u5220\u9664\u6808\u9876\u5143\u7d20\u7684\u64cd\u4f5c\u53eb\u505a\u300c\u51fa\u6808\u300d\u3002

    Fig. \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\u4e0b\u8868\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() , pop() , peek() \u547d\u540d\u4e3a\u4f8b\u3002

    \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\u300c\u6570\u7ec4\u300d\u6216\u300c\u94fe\u8868\u300d\u89c6\u4f5c\u6808\u6765\u4f7f\u7528\uff0c\u5e76\u901a\u8fc7\u201c\u8111\u8865\u201d\u6765\u5ffd\u7565\u4e0e\u6808\u65e0\u5173\u7684\u64cd\u4f5c\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart stack.java
    /* \u521d\u59cb\u5316\u6808 */\nStack<Integer> stack = new Stack<>();\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek = stack.peek();\n/* \u5143\u7d20\u51fa\u6808 */\nint pop = stack.pop();\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.size();\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = stack.isEmpty();\n
    stack.cpp
    /* \u521d\u59cb\u5316\u6808 */\nstack<int> stack;\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint top = stack.top();\n/* \u5143\u7d20\u51fa\u6808 */\nstack.pop(); // \u65e0\u8fd4\u56de\u503c\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.size();\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nbool empty = stack.empty();\n
    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# \u5143\u7d20\u5165\u6808\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n# \u8bbf\u95ee\u6808\u9876\u5143\u7d20\npeek: int = stack[-1]\n# \u5143\u7d20\u51fa\u6808\npop: int = stack.pop()\n# \u83b7\u53d6\u6808\u7684\u957f\u5ea6\nsize: int = len(stack)\n# \u5224\u65ad\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = len(stack) == 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/* \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/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npeek := stack[len(stack)-1]\n/* \u5143\u7d20\u51fa\u6808 */\npop := stack[len(stack)-1]\nstack = stack[:len(stack)-1]\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nsize := len(stack)\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nisEmpty := len(stack) == 0\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/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nconst peek = stack[stack.length-1];\n/* \u5143\u7d20\u51fa\u6808 */\nconst pop = stack.pop();\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nconst size = stack.length;\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/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nconst peek = stack[stack.length - 1];\n/* \u5143\u7d20\u51fa\u6808 */\nconst pop = stack.pop();\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nconst size = stack.length;\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nconst is_empty = stack.length === 0;\n
    stack.c
    // C \u672a\u63d0\u4f9b\u5185\u7f6e\u6808\n
    stack.cs
    /* \u521d\u59cb\u5316\u6808 */\nStack<int> stack = new ();\n/* \u5143\u7d20\u5165\u6808 */\nstack.Push(1);\nstack.Push(3);\nstack.Push(2);\nstack.Push(5);\nstack.Push(4);\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek = stack.Peek();\n/* \u5143\u7d20\u51fa\u6808 */\nint pop = stack.Pop();\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.Count();\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = stack.Count()==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/* \u5143\u7d20\u5165\u6808 */\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nlet peek = stack.last!\n/* \u5143\u7d20\u51fa\u6808 */\nlet pop = stack.removeLast()\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nlet size = stack.count\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = stack.isEmpty\n
    stack.zig
    \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/* \u5143\u7d20\u5165\u6808 */\nstack.add(1);\nstack.add(3);\nstack.add(2);\nstack.add(5);\nstack.add(4);\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek = stack.last;\n/* \u5143\u7d20\u51fa\u6808 */\nint pop = stack.removeLast();\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.length;\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = stack.isEmpty;\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\u88ab\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":"\u57fa\u4e8e\u94fe\u8868\u7684\u5b9e\u73b0","text":"

    \u4f7f\u7528\u94fe\u8868\u6765\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

    \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()

    \u4ee5\u4e0b\u662f\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u6808\u7684\u793a\u4f8b\u4ee3\u7801\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart linkedlist_stack.java
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\nprivate ListNode stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nprivate int stkSize = 0; // \u6808\u7684\u957f\u5ea6\npublic LinkedListStack() {\nstackPeek = null;\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\npublic int size() {\nreturn stkSize;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\npublic boolean isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u6808 */\npublic void push(int num) {\nListNode node = new ListNode(num);\nnode.next = stackPeek;\nstackPeek = node;\nstkSize++;\n}\n/* \u51fa\u6808 */\npublic int pop() {\nint num = peek();\nstackPeek = stackPeek.next;\nstkSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npublic int peek() {\nif (size() == 0)\nthrow new IndexOutOfBoundsException();\nreturn stackPeek.val;\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npublic int[] toArray() {\nListNode node = stackPeek;\nint[] res = new int[size()];\nfor (int i = res.length - 1; i >= 0; i--) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n
    linkedlist_stack.cpp
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\nprivate:\nListNode *stackTop; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nint stkSize;        // \u6808\u7684\u957f\u5ea6\npublic:\nLinkedListStack() {\nstackTop = nullptr;\nstkSize = 0;\n}\n~LinkedListStack() {\n// \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\nfreeMemoryLinkedList(stackTop);\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size() {\nreturn stkSize;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool empty() {\nreturn size() == 0;\n}\n/* \u5165\u6808 */\nvoid push(int num) {\nListNode *node = new ListNode(num);\nnode->next = stackTop;\nstackTop = node;\nstkSize++;\n}\n/* \u51fa\u6808 */\nvoid pop() {\nint num = top();\nListNode *tmp = stackTop;\nstackTop = stackTop->next;\n// \u91ca\u653e\u5185\u5b58\ndelete tmp;\nstkSize--;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint top() {\nif (size() == 0)\nthrow out_of_range(\"\u6808\u4e3a\u7a7a\");\nreturn stackTop->val;\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\nvector<int> toVector() {\nListNode *node = stackTop;\nvector<int> res(size());\nfor (int i = res.size() - 1; i >= 0; i--) {\nres[i] = node->val;\nnode = node->next;\n}\nreturn res;\n}\n};\n
    linkedlist_stack.py
    class LinkedListStack:\n\"\"\"\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\"\"\"\ndef __init__(self):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.__peek: ListNode | None = None\nself.__size: int = 0\ndef size(self) -> int:\n\"\"\"\u83b7\u53d6\u6808\u7684\u957f\u5ea6\"\"\"\nreturn self.__size\ndef is_empty(self) -> bool:\n\"\"\"\u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\"\"\"\nreturn not self.__peek\ndef push(self, val: int) -> None:\n\"\"\"\u5165\u6808\"\"\"\nnode = ListNode(val)\nnode.next = self.__peek\nself.__peek = node\nself.__size += 1\ndef pop(self) -> int:\n\"\"\"\u51fa\u6808\"\"\"\nnum: int = self.peek()\nself.__peek = self.__peek.next\nself.__size -= 1\nreturn num\ndef peek(self) -> int:\n\"\"\"\u8bbf\u95ee\u6808\u9876\u5143\u7d20\"\"\"\n# \u5224\u7a7a\u5904\u7406\nif not self.__peek:\nreturn None\nreturn self.__peek.val\ndef to_list(self) -> list[int]:\n\"\"\"\u8f6c\u5316\u4e3a\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\narr = []\nnode = self.__peek\nwhile node:\narr.append(node.val)\nnode = node.next\narr.reverse()\nreturn arr\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\ndata *list.List\n}\n/* \u521d\u59cb\u5316\u6808 */\nfunc newLinkedListStack() *linkedListStack {\nreturn &linkedListStack{\ndata: list.New(),\n}\n}\n/* \u5165\u6808 */\nfunc (s *linkedListStack) push(value int) {\ns.data.PushBack(value)\n}\n/* \u51fa\u6808 */\nfunc (s *linkedListStack) pop() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Back()\ns.data.Remove(e)\nreturn e.Value\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nfunc (s *linkedListStack) peek() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Back()\nreturn e.Value\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nfunc (s *linkedListStack) size() int {\nreturn s.data.Len()\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListStack) isEmpty() bool {\nreturn s.data.Len() == 0\n}\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListStack) toList() *list.List {\nreturn s.data\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\nconstructor() {\nthis.#stackPeek = null;\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nget size() {\nreturn this.#stkSize;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nisEmpty() {\nreturn this.size == 0;\n}\n/* \u5165\u6808 */\npush(num) {\nconst node = new ListNode(num);\nnode.next = this.#stackPeek;\nthis.#stackPeek = node;\nthis.#stkSize++;\n}\n/* \u51fa\u6808 */\npop() {\nconst num = this.peek();\nthis.#stackPeek = this.#stackPeek.next;\nthis.#stkSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npeek() {\nif (!this.#stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\nreturn this.#stackPeek.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\ntoArray() {\nlet node = this.#stackPeek;\nconst res = new Array(this.size);\nfor (let i = res.length - 1; i >= 0; i--) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n
    linkedlist_stack.ts
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\nprivate stackPeek: ListNode | null; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nprivate stkSize: number = 0; // \u6808\u7684\u957f\u5ea6\nconstructor() {\nthis.stackPeek = null;\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nget size(): number {\nreturn this.stkSize;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nisEmpty(): boolean {\nreturn this.size == 0;\n}\n/* \u5165\u6808 */\npush(num: number): void {\nconst node = new ListNode(num);\nnode.next = this.stackPeek;\nthis.stackPeek = node;\nthis.stkSize++;\n}\n/* \u51fa\u6808 */\npop(): number {\nconst num = this.peek();\nif (!this.stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\nthis.stackPeek = this.stackPeek.next;\nthis.stkSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npeek(): number {\nif (!this.stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\nreturn this.stackPeek.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\ntoArray(): number[] {\nlet node = this.stackPeek;\nconst res = new Array<number>(this.size);\nfor (let i = res.length - 1; i >= 0; i--) {\nres[i] = node!.val;\nnode = node!.next;\n}\nreturn res;\n}\n}\n
    linkedlist_stack.c
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nstruct linkedListStack {\nListNode *top; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nint size;      // \u6808\u7684\u957f\u5ea6\n};\ntypedef struct linkedListStack linkedListStack;\n/* \u6784\u9020\u51fd\u6570 */\nlinkedListStack *newLinkedListStack() {\nlinkedListStack *s = malloc(sizeof(linkedListStack));\ns->top = NULL;\ns->size = 0;\nreturn s;\n}\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListStack(linkedListStack *s) {\nwhile (s->top) {\nListNode *n = s->top->next;\nfree(s->top);\ns->top = n;\n}\nfree(s);\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size(linkedListStack *s) {\nassert(s);\nreturn s->size;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty(linkedListStack *s) {\nassert(s);\nreturn size(s) == 0;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek(linkedListStack *s) {\nassert(s);\nassert(size(s) != 0);\nreturn s->top->val;\n}\n/* \u5165\u6808 */\nvoid push(linkedListStack *s, int num) {\nassert(s);\nListNode *node = (ListNode *)malloc(sizeof(ListNode));\nnode->next = s->top; // \u66f4\u65b0\u65b0\u52a0\u8282\u70b9\u6307\u9488\u57df\nnode->val = num;     // \u66f4\u65b0\u65b0\u52a0\u8282\u70b9\u6570\u636e\u57df\ns->top = node;       // \u66f4\u65b0\u6808\u9876\ns->size++;           // \u66f4\u65b0\u6808\u5927\u5c0f\n}\n/* \u51fa\u6808 */\nint pop(linkedListStack *s) {\nif (s->size == 0) {\nprintf(\"stack is empty.\\n\");\nreturn INT_MAX;\n}\nassert(s);\nint val = peek(s);\nListNode *tmp = s->top;\ns->top = s->top->next;\n// \u91ca\u653e\u5185\u5b58\nfree(tmp);\ns->size--;\nreturn val;\n}\n
    linkedlist_stack.cs
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\nprivate ListNode? stackPeek;  // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nprivate int stkSize = 0;   // \u6808\u7684\u957f\u5ea6\npublic LinkedListStack() {\nstackPeek = null;\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\npublic int size() {\nreturn stkSize;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\npublic bool isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u6808 */\npublic void push(int num) {\nListNode node = new ListNode(num);\nnode.next = stackPeek;\nstackPeek = node;\nstkSize++;\n}\n/* \u51fa\u6808 */\npublic int pop() {\nif (stackPeek == null)\nthrow new Exception();\nint num = peek();\nstackPeek = stackPeek.next;\nstkSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npublic int peek() {\nif (size() == 0 || stackPeek == null)\nthrow new Exception();\nreturn stackPeek.val;\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npublic int[] toArray() {\nif (stackPeek == null)\nreturn Array.Empty<int>();\nListNode node = stackPeek;\nint[] res = new int[size()];\nfor (int i = res.Length - 1; i >= 0; i--) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n
    linkedlist_stack.swift
    /* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\nprivate var _peek: ListNode? // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nprivate var _size = 0 // \u6808\u7684\u957f\u5ea6\ninit() {}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nfunc size() -> Int {\n_size\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nfunc isEmpty() -> Bool {\nsize() == 0\n}\n/* \u5165\u6808 */\nfunc push(num: Int) {\nlet node = ListNode(x: num)\nnode.next = _peek\n_peek = node\n_size += 1\n}\n/* \u51fa\u6808 */\n@discardableResult\nfunc pop() -> Int {\nlet num = peek()\n_peek = _peek?.next\n_size -= 1\nreturn num\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nfunc peek() -> Int {\nif isEmpty() {\nfatalError(\"\u6808\u4e3a\u7a7a\")\n}\nreturn _peek!.val\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\nfunc toArray() -> [Int] {\nvar node = _peek\nvar res = Array(repeating: 0, count: _size)\nfor i in sequence(first: res.count - 1, next: { $0 >= 0 + 1 ? $0 - 1 : nil }) {\nres[i] = node!.val\nnode = node?.next\n}\nreturn res\n}\n}\n
    linkedlist_stack.zig
    // \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\nfn LinkedListStack(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nstack_top: ?*inc.ListNode(T) = null,             // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nstk_size: usize = 0,                             // \u6808\u7684\u957f\u5ea6\nmem_arena: ?std.heap.ArenaAllocator = null,\nmem_allocator: std.mem.Allocator = undefined,   // \u5185\u5b58\u5206\u914d\u5668\n// \u6784\u9020\u65b9\u6cd5\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u6808\uff09\npub fn init(self: *Self, allocator: std.mem.Allocator) !void {\nif (self.mem_arena == null) {\nself.mem_arena = std.heap.ArenaAllocator.init(allocator);\nself.mem_allocator = self.mem_arena.?.allocator();\n}\nself.stack_top = null;\nself.stk_size = 0;\n}\n// \u6790\u6784\u65b9\u6cd5\uff08\u91ca\u653e\u5185\u5b58\uff09\npub fn deinit(self: *Self) void {\nif (self.mem_arena == null) return;\nself.mem_arena.?.deinit();\n}\n// \u83b7\u53d6\u6808\u7684\u957f\u5ea6\npub fn size(self: *Self) usize {\nreturn self.stk_size;\n}\n// \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\npub fn isEmpty(self: *Self) bool {\nreturn self.size() == 0;\n}\n// \u8bbf\u95ee\u6808\u9876\u5143\u7d20\npub fn peek(self: *Self) T {\nif (self.size() == 0) @panic(\"\u6808\u4e3a\u7a7a\");\nreturn self.stack_top.?.val;\n}  // \u5165\u6808\npub fn push(self: *Self, num: T) !void {\nvar node = try self.mem_allocator.create(inc.ListNode(T));\nnode.init(num);\nnode.next = self.stack_top;\nself.stack_top = node;\nself.stk_size += 1;\n} // \u51fa\u6808\npub fn pop(self: *Self) T {\nvar num = self.peek();\nself.stack_top = self.stack_top.?.next;\nself.stk_size -= 1;\nreturn num;\n} // \u5c06\u6808\u8f6c\u6362\u4e3a\u6570\u7ec4\npub fn toArray(self: *Self) ![]T {\nvar node = self.stack_top;\nvar res = try self.mem_allocator.alloc(T, self.size());\nstd.mem.set(T, res, @as(T, 0));\nvar i: usize = 0;\nwhile (i < res.len) : (i += 1) {\nres[res.len - i - 1] = node.?.val;\nnode = node.?.next;\n}\nreturn res;\n}\n};\n}\n
    linkedlist_stack.dart
    /* \u57fa\u4e8e\u94fe\u8868\u7c7b\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\nListNode? _stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nint _stkSize = 0; // \u6808\u7684\u957f\u5ea6\nLinkedListStack() {\n_stackPeek = null;\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size() {\nreturn _stkSize;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty() {\nreturn _stkSize == 0;\n}\n/* \u5165\u6808 */\nvoid push(int num) {\nfinal ListNode node = ListNode(num);\nnode.next = _stackPeek;\n_stackPeek = node;\n_stkSize++;\n}\n/* \u51fa\u6808 */\nint pop() {\nfinal int num = peek();\n_stackPeek = _stackPeek!.next;\n_stkSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek() {\nif (_stackPeek == null) {\nthrow Exception(\"\u6808\u4e3a\u7a7a\");\n}\nreturn _stackPeek!.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a List \u5e76\u8fd4\u56de */\nList<int> toList() {\nListNode? node = _stackPeek;\nList<int> list = [];\nwhile (node != null) {\nlist.add(node.val);\nnode = node.next;\n}\nlist = list.reversed.toList();\nreturn list;\n}\n}\n
    "},{"location":"chapter_stack_and_queue/stack/#_2","title":"\u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"

    \u5728\u57fa\u4e8e\u300c\u6570\u7ec4\u300d\u5b9e\u73b0\u6808\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u7684\u5c3e\u90e8\u4f5c\u4e3a\u6808\u9876\u3002\u5728\u8fd9\u6837\u7684\u8bbe\u8ba1\u4e0b\uff0c\u5165\u6808\u4e0e\u51fa\u6808\u64cd\u4f5c\u5c31\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()

    \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\u9700\u81ea\u884c\u5904\u7406\u6570\u7ec4\u6269\u5bb9\u95ee\u9898\u3002\u4ee5\u4e0b\u4e3a\u793a\u4f8b\u4ee3\u7801\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart array_stack.java
    /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\nprivate ArrayList<Integer> stack;\npublic ArrayStack() {\n// \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\nstack = new ArrayList<>();\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\npublic int size() {\nreturn stack.size();\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\npublic boolean isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u6808 */\npublic void push(int num) {\nstack.add(num);\n}\n/* \u51fa\u6808 */\npublic int pop() {\nif (isEmpty())\nthrow new IndexOutOfBoundsException();\nreturn stack.remove(size() - 1);\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npublic int peek() {\nif (isEmpty())\nthrow new IndexOutOfBoundsException();\nreturn stack.get(size() - 1);\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npublic Object[] toArray() {\nreturn stack.toArray();\n}\n}\n
    array_stack.cpp
    /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\nprivate:\nvector<int> stack;\npublic:\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size() {\nreturn stack.size();\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool empty() {\nreturn stack.empty();\n}\n/* \u5165\u6808 */\nvoid push(int num) {\nstack.push_back(num);\n}\n/* \u51fa\u6808 */\nvoid pop() {\nint oldTop = top();\nstack.pop_back();\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint top() {\nif (empty())\nthrow out_of_range(\"\u6808\u4e3a\u7a7a\");\nreturn stack.back();\n}\n/* \u8fd4\u56de Vector */\nvector<int> toVector() {\nreturn stack;\n}\n};\n
    array_stack.py
    class ArrayStack:\n\"\"\"\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\"\"\"\ndef __init__(self) -> None:\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.__stack: list[int] = []\ndef size(self) -> int:\n\"\"\"\u83b7\u53d6\u6808\u7684\u957f\u5ea6\"\"\"\nreturn len(self.__stack)\ndef is_empty(self) -> bool:\n\"\"\"\u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\"\"\"\nreturn self.__stack == []\ndef push(self, item: int) -> None:\n\"\"\"\u5165\u6808\"\"\"\nself.__stack.append(item)\ndef pop(self) -> int:\n\"\"\"\u51fa\u6808\"\"\"\nif self.is_empty():\nraise IndexError(\"\u6808\u4e3a\u7a7a\")\nreturn self.__stack.pop()\ndef peek(self) -> int:\n\"\"\"\u8bbf\u95ee\u6808\u9876\u5143\u7d20\"\"\"\nif self.is_empty():\nraise IndexError(\"\u6808\u4e3a\u7a7a\")\nreturn self.__stack[-1]\ndef to_list(self) -> list[int]:\n\"\"\"\u8fd4\u56de\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\nreturn self.__stack\n
    array_stack.go
    /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\ntype arrayStack struct {\ndata []int // \u6570\u636e\n}\n/* \u521d\u59cb\u5316\u6808 */\nfunc newArrayStack() *arrayStack {\nreturn &arrayStack{\n// \u8bbe\u7f6e\u6808\u7684\u957f\u5ea6\u4e3a 0\uff0c\u5bb9\u91cf\u4e3a 16\ndata: make([]int, 0, 16),\n}\n}\n/* \u6808\u7684\u957f\u5ea6 */\nfunc (s *arrayStack) size() int {\nreturn len(s.data)\n}\n/* \u6808\u662f\u5426\u4e3a\u7a7a */\nfunc (s *arrayStack) isEmpty() bool {\nreturn s.size() == 0\n}\n/* \u5165\u6808 */\nfunc (s *arrayStack) push(v int) {\n// \u5207\u7247\u4f1a\u81ea\u52a8\u6269\u5bb9\ns.data = append(s.data, v)\n}\n/* \u51fa\u6808 */\nfunc (s *arrayStack) pop() any {\nval := s.peek()\ns.data = s.data[:len(s.data)-1]\nreturn val\n}\n/* \u83b7\u53d6\u6808\u9876\u5143\u7d20 */\nfunc (s *arrayStack) peek() any {\nif s.isEmpty() {\nreturn nil\n}\nval := s.data[len(s.data)-1]\nreturn val\n}\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (s *arrayStack) toSlice() []int {\nreturn s.data\n}\n
    array_stack.js
    /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n#stack;\nconstructor() {\nthis.#stack = [];\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nget size() {\nreturn this.#stack.length;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nempty() {\nreturn this.#stack.length === 0;\n}\n/* \u5165\u6808 */\npush(num) {\nthis.#stack.push(num);\n}\n/* \u51fa\u6808 */\npop() {\nif (this.empty()) throw new Error('\u6808\u4e3a\u7a7a');\nreturn this.#stack.pop();\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\ntop() {\nif (this.empty()) throw new Error('\u6808\u4e3a\u7a7a');\nreturn this.#stack[this.#stack.length - 1];\n}\n/* \u8fd4\u56de Array */\ntoArray() {\nreturn this.#stack;\n}\n}\n
    array_stack.ts
    /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\nprivate stack: number[];\nconstructor() {\nthis.stack = [];\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nget size(): number {\nreturn this.stack.length;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nempty(): boolean {\nreturn this.stack.length === 0;\n}\n/* \u5165\u6808 */\npush(num: number): void {\nthis.stack.push(num);\n}\n/* \u51fa\u6808 */\npop(): number | undefined {\nif (this.empty()) throw new Error('\u6808\u4e3a\u7a7a');\nreturn this.stack.pop();\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\ntop(): number | undefined {\nif (this.empty()) throw new Error('\u6808\u4e3a\u7a7a');\nreturn this.stack[this.stack.length - 1];\n}\n/* \u8fd4\u56de Array */\ntoArray() {\nreturn this.stack;\n}\n}\n
    array_stack.c
    /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nstruct arrayStack {\nint *data;\nint size;\n};\ntypedef struct arrayStack arrayStack;\n/* \u6784\u9020\u51fd\u6570 */\narrayStack *newArrayStack() {\narrayStack *s = malloc(sizeof(arrayStack));\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5927\u5bb9\u91cf\uff0c\u907f\u514d\u6269\u5bb9\ns->data = malloc(sizeof(int) * MAX_SIZE);\ns->size = 0;\nreturn s;\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size(arrayStack *s) {\nreturn s->size;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty(arrayStack *s) {\nreturn s->size == 0;\n}\n/* \u5165\u6808 */\nvoid push(arrayStack *s, int num) {\nif (s->size == MAX_SIZE) {\nprintf(\"stack is full.\\n\");\nreturn;\n}\ns->data[s->size] = num;\ns->size++;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek(arrayStack *s) {\nif (s->size == 0) {\nprintf(\"stack is empty.\\n\");\nreturn INT_MAX;\n}\nreturn s->data[s->size - 1];\n}\n/* \u51fa\u6808 */\nint pop(arrayStack *s) {\nif (s->size == 0) {\nprintf(\"stack is empty.\\n\");\nreturn INT_MAX;\n}\nint val = peek(s);\ns->size--;\nreturn val;\n}\n
    array_stack.cs
    /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\nprivate List<int> stack;\npublic ArrayStack() {\n// \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\nstack = new();\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\npublic int size() {\nreturn stack.Count();\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\npublic bool isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u6808 */\npublic void push(int num) {\nstack.Add(num);\n}\n/* \u51fa\u6808 */\npublic int pop() {\nif (isEmpty())\nthrow new Exception();\nvar val = peek();\nstack.RemoveAt(size() - 1);\nreturn val;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npublic int peek() {\nif (isEmpty())\nthrow new Exception();\nreturn stack[size() - 1];\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npublic int[] toArray() {\nreturn stack.ToArray();\n}\n}\n
    array_stack.swift
    /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\nprivate var stack: [Int]\ninit() {\n// \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\nstack = []\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nfunc size() -> Int {\nstack.count\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nfunc isEmpty() -> Bool {\nstack.isEmpty\n}\n/* \u5165\u6808 */\nfunc push(num: Int) {\nstack.append(num)\n}\n/* \u51fa\u6808 */\n@discardableResult\nfunc pop() -> Int {\nif isEmpty() {\nfatalError(\"\u6808\u4e3a\u7a7a\")\n}\nreturn stack.removeLast()\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nfunc peek() -> Int {\nif isEmpty() {\nfatalError(\"\u6808\u4e3a\u7a7a\")\n}\nreturn stack.last!\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\nfunc toArray() -> [Int] {\nstack\n}\n}\n
    array_stack.zig
    // \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\nfn ArrayStack(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nstack: ?std.ArrayList(T) = null,     // \u6784\u9020\u65b9\u6cd5\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u6808\uff09\npub fn init(self: *Self, allocator: std.mem.Allocator) void {\nif (self.stack == null) {\nself.stack = std.ArrayList(T).init(allocator);\n}\n}\n// \u6790\u6784\u65b9\u6cd5\uff08\u91ca\u653e\u5185\u5b58\uff09\npub fn deinit(self: *Self) void {\nif (self.stack == null) return;\nself.stack.?.deinit();\n}\n// \u83b7\u53d6\u6808\u7684\u957f\u5ea6\npub fn size(self: *Self) usize {\nreturn self.stack.?.items.len;\n}\n// \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\npub fn isEmpty(self: *Self) bool {\nreturn self.size() == 0;\n}\n// \u8bbf\u95ee\u6808\u9876\u5143\u7d20\npub fn peek(self: *Self) T {\nif (self.isEmpty()) @panic(\"\u6808\u4e3a\u7a7a\");\nreturn self.stack.?.items[self.size() - 1];\n}  // \u5165\u6808\npub fn push(self: *Self, num: T) !void {\ntry self.stack.?.append(num);\n} // \u51fa\u6808\npub fn pop(self: *Self) T {\nvar num = self.stack.?.pop();\nreturn num;\n} // \u8fd4\u56de ArrayList\npub fn toList(self: *Self) std.ArrayList(T) {\nreturn self.stack.?;\n}\n};\n}\n
    array_stack.dart
    /* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\nlate List<int> _stack;\nArrayStack() {\n_stack = [];\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size() {\nreturn _stack.length;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty() {\nreturn _stack.isEmpty;\n}\n/* \u5165\u6808 */\nvoid push(int num) {\n_stack.add(num);\n}\n/* \u51fa\u6808 */\nint pop() {\nif (isEmpty()) {\nthrow Exception(\"\u6808\u4e3a\u7a7a\");\n}\nreturn _stack.removeLast();\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek() {\nif (isEmpty()) {\nthrow Exception(\"\u6808\u4e3a\u7a7a\");\n}\nreturn _stack.last;\n}\n/* \u5c06\u6808\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\nList<int> toArray() => _stack;\n}\n
    "},{"location":"chapter_stack_and_queue/stack/#513","title":"5.1.3. \u00a0 \u4e24\u79cd\u5b9e\u73b0\u5bf9\u6bd4","text":""},{"location":"chapter_stack_and_queue/stack/#_3","title":"\u652f\u6301\u64cd\u4f5c","text":"

    \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

    "},{"location":"chapter_stack_and_queue/stack/#_4","title":"\u65f6\u95f4\u6548\u7387","text":"

    \u5728\u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0\u4e2d\uff0c\u5165\u6808\u548c\u51fa\u6808\u64cd\u4f5c\u90fd\u662f\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\u94fe\u8868\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\uff08\u5982 int , double \uff09\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\uff1a

    • \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\u5728\u89e6\u53d1\u6269\u5bb9\u65f6\u6548\u7387\u4f1a\u964d\u4f4e\uff0c\u4f46\u7531\u4e8e\u6269\u5bb9\u662f\u4f4e\u9891\u64cd\u4f5c\uff0c\u56e0\u6b64\u5e73\u5747\u6548\u7387\u66f4\u9ad8\uff1b
    • \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\u53ef\u4ee5\u63d0\u4f9b\u66f4\u52a0\u7a33\u5b9a\u7684\u6548\u7387\u8868\u73b0\uff1b
    "},{"location":"chapter_stack_and_queue/stack/#_5","title":"\u7a7a\u95f4\u6548\u7387","text":"

    \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\u8fc7\u5b9e\u9645\u9700\u6c42\u3002\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\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\u4e2d\u7684\u540e\u9000\u4e0e\u524d\u8fdb\u3001\u8f6f\u4ef6\u4e2d\u7684\u64a4\u9500\u4e0e\u53cd\u64a4\u9500\u3002\u6bcf\u5f53\u6211\u4eec\u6253\u5f00\u65b0\u7684\u7f51\u9875\uff0c\u6d4f\u89c8\u5668\u5c31\u4f1a\u5c06\u4e0a\u4e00\u4e2a\u7f51\u9875\u6267\u884c\u5165\u6808\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7\u300c\u540e\u9000\u300d\u64cd\u4f5c\u56de\u5230\u4e0a\u4e00\u9875\u9762\u3002\u540e\u9000\u64cd\u4f5c\u5b9e\u9645\u4e0a\u662f\u5728\u6267\u884c\u51fa\u6808\u3002\u5982\u679c\u8981\u540c\u65f6\u652f\u6301\u540e\u9000\u548c\u524d\u8fdb\uff0c\u90a3\u4e48\u9700\u8981\u4e24\u4e2a\u6808\u6765\u914d\u5408\u5b9e\u73b0\u3002
    • \u7a0b\u5e8f\u5185\u5b58\u7ba1\u7406\u3002\u6bcf\u6b21\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u7cfb\u7edf\u90fd\u4f1a\u5728\u6808\u9876\u6dfb\u52a0\u4e00\u4e2a\u6808\u5e27\uff0c\u7528\u4e8e\u8bb0\u5f55\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002\u5728\u9012\u5f52\u51fd\u6570\u4e2d\uff0c\u5411\u4e0b\u9012\u63a8\u9636\u6bb5\u4f1a\u4e0d\u65ad\u6267\u884c\u5165\u6808\u64cd\u4f5c\uff0c\u800c\u5411\u4e0a\u56de\u6eaf\u9636\u6bb5\u5219\u4f1a\u6267\u884c\u51fa\u6808\u64cd\u4f5c\u3002
    "},{"location":"chapter_stack_and_queue/summary/","title":"5.4. \u00a0 \u5c0f\u7ed3","text":"
    • \u6808\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u540e\u51fa\u539f\u5219\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u901a\u8fc7\u6570\u7ec4\u6216\u94fe\u8868\u6765\u5b9e\u73b0\u3002
    • \u4ece\u65f6\u95f4\u6548\u7387\u89d2\u5ea6\u770b\uff0c\u6808\u7684\u6570\u7ec4\u5b9e\u73b0\u5177\u6709\u8f83\u9ad8\u7684\u5e73\u5747\u6548\u7387\uff0c\u4f46\u5728\u6269\u5bb9\u8fc7\u7a0b\u4e2d\uff0c\u5355\u6b21\u5165\u6808\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f1a\u964d\u4f4e\u81f3 \\(O(n)\\) \u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\u5177\u6709\u66f4\u4e3a\u7a33\u5b9a\u7684\u6548\u7387\u8868\u73b0\u3002
    • \u5728\u7a7a\u95f4\u6548\u7387\u65b9\u9762\uff0c\u6808\u7684\u6570\u7ec4\u5b9e\u73b0\u53ef\u80fd\u5bfc\u81f4\u4e00\u5b9a\u7a0b\u5ea6\u7684\u7a7a\u95f4\u6d6a\u8d39\u3002\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u94fe\u8868\u8282\u70b9\u6240\u5360\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u6bd4\u6570\u7ec4\u5143\u7d20\u66f4\u5927\u3002
    • \u961f\u5217\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u5148\u51fa\u539f\u5219\u7684\u6570\u636e\u7ed3\u6784\uff0c\u540c\u6837\u53ef\u4ee5\u901a\u8fc7\u6570\u7ec4\u6216\u94fe\u8868\u6765\u5b9e\u73b0\u3002\u5728\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u7684\u5bf9\u6bd4\u4e0a\uff0c\u961f\u5217\u7684\u7ed3\u8bba\u4e0e\u524d\u8ff0\u6808\u7684\u7ed3\u8bba\u76f8\u4f3c\u3002
    • \u53cc\u5411\u961f\u5217\u662f\u4e00\u79cd\u5177\u6709\u66f4\u9ad8\u81ea\u7531\u5ea6\u7684\u961f\u5217\uff0c\u5b83\u5141\u8bb8\u5728\u4e24\u7aef\u8fdb\u884c\u5143\u7d20\u7684\u6dfb\u52a0\u548c\u5220\u9664\u64cd\u4f5c\u3002
    "},{"location":"chapter_stack_and_queue/summary/#541-q-a","title":"5.4.1. \u00a0 Q & A","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\u5b9e\u73b0\u4e00\u4e9b\u989d\u5916\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u5728\u53cc\u5411\u961f\u5217\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\u8005\u662f\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

    "},{"location":"chapter_tree/","title":"7. \u00a0 \u6811","text":""},{"location":"chapter_tree/array_representation_of_tree/","title":"7.3. \u00a0 \u4e8c\u53c9\u6811\u6570\u7ec4\u8868\u793a","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\u5728\u4e0a\u8282\u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u5728\u94fe\u8868\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7684\u5404\u9879\u57fa\u672c\u64cd\u4f5c\u3002

    \u90a3\u4e48\uff0c\u80fd\u5426\u7528\u300c\u6570\u7ec4\u300d\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\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u5b8c\u7f8e\u4e8c\u53c9\u6811\uff0c\u6211\u4eec\u5c06\u8282\u70b9\u6309\u7167\u5c42\u5e8f\u904d\u5386\u7684\u987a\u5e8f\u7f16\u53f7\uff08\u4ece \\(0\\) \u5f00\u59cb\uff09\uff0c\u6b64\u65f6\u6bcf\u4e2a\u8282\u70b9\u90fd\u5bf9\u5e94\u552f\u4e00\u7684\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\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

    Fig. \u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a

    \u6620\u5c04\u516c\u5f0f\u7684\u4f5c\u7528\u76f8\u5f53\u4e8e\u94fe\u8868\u4e2d\u7684\u6307\u9488\u3002\u5982\u679c\u6211\u4eec\u5c06\u8282\u70b9\u6309\u7167\u5c42\u5e8f\u904d\u5386\u7684\u987a\u5e8f\u5b58\u50a8\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\uff0c\u90a3\u4e48\u5bf9\u4e8e\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u8282\u70b9\uff0c\u6211\u4eec\u90fd\u53ef\u4ee5\u901a\u8fc7\u6620\u5c04\u516c\u5f0f\u6765\u8bbf\u95ee\u5176\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":"

    \u7136\u800c\uff0c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u53ea\u662f\u4e00\u4e2a\u7279\u4f8b\u3002\u5728\u4e8c\u53c9\u6811\u7684\u4e2d\u95f4\u5c42\uff0c\u901a\u5e38\u5b58\u5728\u8bb8\u591a \\(\\text{null}\\) \uff0c\u800c\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5e76\u4e0d\u5305\u542b\u8fd9\u4e9b \\(\\text{null}\\) \u3002\u6211\u4eec\u65e0\u6cd5\u4ec5\u51ed\u8be5\u5e8f\u5217\u6765\u63a8\u6d4b \\(\\text{null}\\) \u7684\u6570\u91cf\u548c\u5206\u5e03\u4f4d\u7f6e\uff0c\u8fd9\u610f\u5473\u7740\u5b58\u5728\u591a\u79cd\u4e8c\u53c9\u6811\u7ed3\u6784\u90fd\u7b26\u5408\u8be5\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u3002\u663e\u7136\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e0a\u8ff0\u7684\u6570\u7ec4\u8868\u793a\u65b9\u6cd5\u5df2\u7ecf\u5931\u6548\u3002

    Fig. \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{null}\\)\u3002\u5982\u4e0b\u56fe\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    /* \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 \u6700\u5927\u503c\u6807\u8bb0\u7a7a\u4f4d\uff0c\u56e0\u6b64\u8981\u6c42\u8282\u70b9\u503c\u4e0d\u80fd\u4e3a INT_MAX\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# \u76f4\u63a5\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 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// \u76f4\u63a5\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// \u76f4\u63a5\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 \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
    /* \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 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
    \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

    Fig. \u4efb\u610f\u7c7b\u578b\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a

    "},{"location":"chapter_tree/array_representation_of_tree/#733","title":"7.3.3. \u00a0 \u4f18\u52bf\u4e0e\u5c40\u9650\u6027","text":"

    \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a\u5b58\u5728\u4ee5\u4e0b\u4f18\u70b9\uff1a

    • \u6570\u7ec4\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\uff0c\u7f13\u5b58\u53cb\u597d\uff0c\u8bbf\u95ee\u4e0e\u904d\u5386\u901f\u5ea6\u8f83\u5feb\uff1b
    • \u4e0d\u9700\u8981\u5b58\u50a8\u6307\u9488\uff0c\u6bd4\u8f83\u8282\u7701\u7a7a\u95f4\uff1b
    • \u5141\u8bb8\u968f\u673a\u8bbf\u95ee\u8282\u70b9\uff1b

    \u7136\u800c\uff0c\u6570\u7ec4\u8868\u793a\u4e5f\u5177\u6709\u4e00\u4e9b\u5c40\u9650\u6027\uff1a

    • \u6570\u7ec4\u5b58\u50a8\u9700\u8981\u8fde\u7eed\u5185\u5b58\u7a7a\u95f4\uff0c\u56e0\u6b64\u4e0d\u9002\u5408\u5b58\u50a8\u6570\u636e\u91cf\u8fc7\u5927\u7684\u6811\u3002
    • \u589e\u5220\u8282\u70b9\u9700\u8981\u901a\u8fc7\u6570\u7ec4\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u5b9e\u73b0\uff0c\u6548\u7387\u8f83\u4f4e\uff1b
    • \u5f53\u4e8c\u53c9\u6811\u4e2d\u5b58\u5728\u5927\u91cf \\(\\text{null}\\) \u65f6\uff0c\u6570\u7ec4\u4e2d\u5305\u542b\u7684\u8282\u70b9\u6570\u636e\u6bd4\u91cd\u8f83\u4f4e\uff0c\u7a7a\u95f4\u5229\u7528\u7387\u8f83\u4f4e\u3002

    \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{null}\\) \u53ea\u51fa\u73b0\u5728\u6700\u5e95\u5c42\u4e14\u9760\u53f3\u7684\u4f4d\u7f6e\uff0c\u8fd9\u610f\u5473\u7740\u6240\u6709 \\(\\text{null}\\) \u4e00\u5b9a\u51fa\u73b0\u5728\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u7684\u672b\u5c3e\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528\u6570\u7ec4\u8868\u793a\u5b8c\u5168\u4e8c\u53c9\u6811\u65f6\uff0c\u53ef\u4ee5\u7701\u7565\u5b58\u50a8\u6240\u6709 \\(\\text{null}\\) \u3002

    Fig. \u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a

    "},{"location":"chapter_tree/avl_tree/","title":"7.5. \u00a0 AVL \u6811 *","text":"

    \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u63d0\u5230\u4e86\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\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\u4e0b\u56fe\u6240\u793a\uff0c\u7ecf\u8fc7\u4e24\u6b21\u5220\u9664\u8282\u70b9\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u4e8c\u53c9\u641c\u7d22\u6811\u4fbf\u4f1a\u9000\u5316\u4e3a\u94fe\u8868\u3002

    Fig. AVL \u6811\u5728\u5220\u9664\u8282\u70b9\u540e\u53d1\u751f\u9000\u5316

    \u518d\u4f8b\u5982\uff0c\u5728\u4ee5\u4e0b\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

    Fig. AVL \u6811\u5728\u63d2\u5165\u8282\u70b9\u540e\u53d1\u751f\u9000\u5316

    G. M. Adelson-Velsky \u548c E. M. Landis \u5728\u5176 1962 \u5e74\u53d1\u8868\u7684\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":"

    \u300cAVL \u6811\u300d\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\u300d\u3002

    "},{"location":"chapter_tree/avl_tree/#_1","title":"\u8282\u70b9\u9ad8\u5ea6","text":"

    \u5728\u64cd\u4f5c AVL \u6811\u65f6\uff0c\u6211\u4eec\u9700\u8981\u83b7\u53d6\u8282\u70b9\u7684\u9ad8\u5ea6\uff0c\u56e0\u6b64\u9700\u8981\u4e3a AVL \u6811\u7684\u8282\u70b9\u7c7b\u6dfb\u52a0 height \u53d8\u91cf\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\npublic int val;        // \u8282\u70b9\u503c\npublic int height;     // \u8282\u70b9\u9ad8\u5ea6\npublic TreeNode left;  // \u5de6\u5b50\u8282\u70b9\npublic TreeNode right; // \u53f3\u5b50\u8282\u70b9\npublic TreeNode(int x) { val = x; }\n}\n
    /* AVL \u6811\u8282\u70b9\u7c7b */\nstruct TreeNode {\nint val{};          // \u8282\u70b9\u503c\nint height = 0;     // \u8282\u70b9\u9ad8\u5ea6\nTreeNode *left{};   // \u5de6\u5b50\u8282\u70b9\nTreeNode *right{};  // \u53f3\u5b50\u8282\u70b9\nTreeNode() = default;\nexplicit TreeNode(int x) : val(x){}\n};\n
    class TreeNode:\n\"\"\"AVL \u6811\u8282\u70b9\u7c7b\"\"\"\ndef __init__(self, val: int):\nself.val: int = val                    # \u8282\u70b9\u503c\nself.height: int = 0                   # \u8282\u70b9\u9ad8\u5ea6\nself.left: Optional[TreeNode] = None   # \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nself.right: Optional[TreeNode] = None  # \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n
    /* AVL \u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype TreeNode struct {\nVal    int       // \u8282\u70b9\u503c\nHeight int       // \u8282\u70b9\u9ad8\u5ea6\nLeft   *TreeNode // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nRight  *TreeNode // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\n
    /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nval; // \u8282\u70b9\u503c\nheight; //\u8282\u70b9\u9ad8\u5ea6\nleft; // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nright; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\nconstructor(val, left, right, height) {\nthis.val = val === undefined ? 0 : val;\nthis.height = height === undefined ? 0 : height;\nthis.left = left === undefined ? null : left;\nthis.right = right === undefined ? null : right;\n}\n}\n
    /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nval: number;            // \u8282\u70b9\u503c\nheight: number;         // \u8282\u70b9\u9ad8\u5ea6\nleft: TreeNode | null;  // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nright: TreeNode | null; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\nconstructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {\nthis.val = val === undefined ? 0 : val;\nthis.height = height === undefined ? 0 : height; this.left = left === undefined ? null : left; this.right = right === undefined ? null : right; }\n}\n
    /* AVL \u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\nint val;\nint height;\nstruct TreeNode *left;\nstruct TreeNode *right;\n};\ntypedef struct TreeNode TreeNode;\n/* \u6784\u9020\u51fd\u6570 */\nTreeNode *newTreeNode(int val) {\nTreeNode *node;\nnode = (TreeNode *)malloc(sizeof(TreeNode));\nnode->val = val;\nnode->height = 0;\nnode->left = NULL;\nnode->right = NULL;\nreturn node;\n}\n
    /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\npublic int val;          // \u8282\u70b9\u503c\npublic int height;       // \u8282\u70b9\u9ad8\u5ea6\npublic TreeNode? left;   // \u5de6\u5b50\u8282\u70b9\npublic TreeNode? right;  // \u53f3\u5b50\u8282\u70b9\npublic TreeNode(int x) { val = x; }\n}\n
    /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nvar val: Int // \u8282\u70b9\u503c\nvar height: Int // \u8282\u70b9\u9ad8\u5ea6\nvar left: TreeNode? // \u5de6\u5b50\u8282\u70b9\nvar right: TreeNode? // \u53f3\u5b50\u8282\u70b9\ninit(x: Int) {\nval = x\nheight = 0\n}\n}\n
    \n
    /* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nint val;         // \u8282\u70b9\u503c\nint height;      // \u8282\u70b9\u9ad8\u5ea6\nTreeNode? left;  // \u5de6\u5b50\u8282\u70b9\nTreeNode? right; // \u53f3\u5b50\u8282\u70b9\nTreeNode(this.val, [this.height = 0, this.left, this.right]);\n}\n

    \u300c\u8282\u70b9\u9ad8\u5ea6\u300d\u662f\u6307\u4ece\u8be5\u8282\u70b9\u5230\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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nreturn node == null ? -1 : node.height;\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\nnode.height = Math.max(height(node.left), height(node.right)) + 1;\n}\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\nreturn node == nullptr ? -1 : node->height;\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\nnode->height = max(height(node->left), height(node->right)) + 1;\n}\n
    avl_tree.py
    def 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\nif node is not None:\nreturn node.height\nreturn -1\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\nnode.height = max([self.height(node.left), self.height(node.right)]) + 1\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\nif node != nil {\nreturn node.Height\n}\nreturn -1\n}\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfunc (t *aVLTree) updateHeight(node *TreeNode) {\nlh := t.height(node.Left)\nrh := t.height(node.Right)\n// \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nif lh > rh {\nnode.Height = lh + 1\n} else {\nnode.Height = rh + 1\n}\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\nreturn node === null ? -1 : node.height;\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\nnode.height =\nMath.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\nreturn node === null ? -1 : node.height;\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\nnode.height =\nMath.max(this.height(node.left), this.height(node.right)) + 1;\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\nif (node != NULL) {\nreturn node->height;\n}\nreturn -1;\n}\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode *node) {\nint lh = height(node->left);\nint rh = height(node->right);\n// \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nif (lh > rh) {\nnode->height = lh + 1;\n} else {\nnode->height = rh + 1;\n}\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\nreturn node == null ? -1 : node.height;\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\nnode.height = Math.Max(height(node.left), height(node.right)) + 1;\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\nnode == nil ? -1 : node!.height\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\nnode?.height = max(height(node: node?.left), height(node: node?.right)) + 1\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\nreturn if (node == null) -1 else node.?.height;\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\nnode.?.height = std.math.max(self.height(node.?.left), self.height(node.?.right)) + 1;\n}\n
    avl_tree.dart
    /* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode? node) {\nreturn node == null ? -1 : node.height;\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\nnode!.height = max(height(node.left), height(node.right)) + 1;\n}\n
    "},{"location":"chapter_tree/avl_tree/#_2","title":"\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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart avl_tree.java
    /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode node) {\n// \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nif (node == null)\nreturn 0;\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn height(node.left) - height(node.right);\n}\n
    avl_tree.cpp
    /* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode *node) {\n// \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nif (node == nullptr)\nreturn 0;\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn height(node->left) - height(node->right);\n}\n
    avl_tree.py
    def balance_factor(self, node: TreeNode | None) -> int:\n\"\"\"\u83b7\u53d6\u5e73\u8861\u56e0\u5b50\"\"\"\n# \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nif node is None:\nreturn 0\n# \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn self.height(node.left) - self.height(node.right)\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\nif node == nil {\nreturn 0\n}\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn t.height(node.Left) - t.height(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\nif (node === null) return 0;\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn 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\nif (node === null) return 0;\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn this.height(node.left) - this.height(node.right);\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\nif (node == NULL) {\nreturn 0;\n}\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn 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\nif (node == null) return 0;\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn height(node.left) - 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\nguard let node = node else { return 0 }\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn height(node: node.left) - height(node: 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\nif (node == null) return 0;\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn self.height(node.?.left) - self.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\nif (node == null) return 0;\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn height(node.left) - 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\u300c\u65cb\u8f6c Rotation\u300d\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\u6811\u7684\u300c\u4e8c\u53c9\u641c\u7d22\u6811\u300d\u5c5e\u6027\uff0c\u4e5f\u80fd\u4f7f\u6811\u91cd\u65b0\u53d8\u4e3a\u300c\u5e73\u8861\u4e8c\u53c9\u6811\u300d\u3002

    \u6211\u4eec\u5c06\u5e73\u8861\u56e0\u5b50\u7edd\u5bf9\u503c \\(> 1\\) \u7684\u8282\u70b9\u79f0\u4e3a\u300c\u5931\u8861\u8282\u70b9\u300d\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\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65cb\u8f6c\u64cd\u4f5c\u3002

    "},{"location":"chapter_tree/avl_tree/#_3","title":"\u53f3\u65cb","text":"

    \u5982\u4e0b\u56fe\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\u300c\u53f3\u65cb\u300d\u64cd\u4f5c\u3002\u5b8c\u6210\u53f3\u65cb\u540e\uff0c\u5b50\u6811\u5df2\u7ecf\u6062\u590d\u5e73\u8861\uff0c\u5e76\u4e14\u4ecd\u7136\u4fdd\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u7279\u6027\u3002

    <1><2><3><4>

    \u6b64\u5916\uff0c\u5982\u679c\u8282\u70b9 child \u672c\u8eab\u6709\u53f3\u5b50\u8282\u70b9\uff08\u8bb0\u4e3a grandChild \uff09\uff0c\u5219\u9700\u8981\u5728\u300c\u53f3\u65cb\u300d\u4e2d\u6dfb\u52a0\u4e00\u6b65\uff1a\u5c06 grandChild \u4f5c\u4e3a node \u7684\u5de6\u5b50\u8282\u70b9\u3002

    Fig. \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\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart avl_tree.java
    /* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode rightRotate(TreeNode node) {\nTreeNode child = node.left;\nTreeNode grandChild = child.right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.right = node;\nnode.left = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.cpp
    /* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode *rightRotate(TreeNode *node) {\nTreeNode *child = node->left;\nTreeNode *grandChild = child->right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild->right = node;\nnode->left = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.py
    def __right_rotate(self, node: TreeNode | None) -> TreeNode | None:\n\"\"\"\u53f3\u65cb\u64cd\u4f5c\"\"\"\nchild = node.left\ngrand_child = child.right\n# \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.right = node\nnode.left = grand_child\n# \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nself.__update_height(node)\nself.__update_height(child)\n# \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child\n
    avl_tree.go
    /* \u53f3\u65cb\u64cd\u4f5c */\nfunc (t *aVLTree) rightRotate(node *TreeNode) *TreeNode {\nchild := node.Left\ngrandChild := child.Right\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.Right = node\nnode.Left = grandChild\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nt.updateHeight(node)\nt.updateHeight(child)\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child\n}\n
    avl_tree.js
    /* \u53f3\u65cb\u64cd\u4f5c */\n#rightRotate(node) {\nconst child = node.left;\nconst grandChild = child.right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.right = node;\nnode.left = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nthis.#updateHeight(node);\nthis.#updateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.ts
    /* \u53f3\u65cb\u64cd\u4f5c */\nrightRotate(node: TreeNode): TreeNode {\nconst child = node.left;\nconst grandChild = child.right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.right = node;\nnode.left = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nthis.updateHeight(node);\nthis.updateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.c
    /* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode *rightRotate(TreeNode *node) {\nTreeNode *child, *grandChild;\nchild = node->left;\ngrandChild = child->right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild->right = node;\nnode->left = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.cs
    /* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode? rightRotate(TreeNode? node) {\nTreeNode? child = node.left;\nTreeNode? grandChild = child?.right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.right = node;\nnode.left = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.swift
    /* \u53f3\u65cb\u64cd\u4f5c */\nfunc rightRotate(node: TreeNode?) -> TreeNode? {\nlet child = node?.left\nlet grandChild = child?.right\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild?.right = node\nnode?.left = grandChild\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node: node)\nupdateHeight(node: child)\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child\n}\n
    avl_tree.zig
    // \u53f3\u65cb\u64cd\u4f5c\nfn rightRotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\nvar child = node.?.left;\nvar grandChild = child.?.right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.?.right = node;\nnode.?.left = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nself.updateHeight(node);\nself.updateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.dart
    /* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode? rightRotate(TreeNode? node) {\nTreeNode? child = node!.left;\nTreeNode? grandChild = child!.right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.right = node;\nnode.left = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    "},{"location":"chapter_tree/avl_tree/#_4","title":"\u5de6\u65cb","text":"

    \u76f8\u5e94\u7684\uff0c\u5982\u679c\u8003\u8651\u4e0a\u8ff0\u5931\u8861\u4e8c\u53c9\u6811\u7684\u201c\u955c\u50cf\u201d\uff0c\u5219\u9700\u8981\u6267\u884c\u300c\u5de6\u65cb\u300d\u64cd\u4f5c\u3002

    Fig. \u5de6\u65cb\u64cd\u4f5c

    \u540c\u7406\uff0c\u82e5\u8282\u70b9 child \u672c\u8eab\u6709\u5de6\u5b50\u8282\u70b9\uff08\u8bb0\u4e3a grandChild \uff09\uff0c\u5219\u9700\u8981\u5728\u300c\u5de6\u65cb\u300d\u4e2d\u6dfb\u52a0\u4e00\u6b65\uff1a\u5c06 grandChild \u4f5c\u4e3a node \u7684\u53f3\u5b50\u8282\u70b9\u3002

    Fig. \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\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece\u53f3\u65cb\u7684\u4ee3\u7801\u63a8\u5bfc\u51fa\u5de6\u65cb\u7684\u4ee3\u7801\u3002\u5177\u4f53\u5730\uff0c\u53ea\u9700\u5c06\u300c\u53f3\u65cb\u300d\u4ee3\u7801\u4e2d\u7684\u628a\u6240\u6709\u7684 left \u66ff\u6362\u4e3a right \uff0c\u5c06\u6240\u6709\u7684 right \u66ff\u6362\u4e3a left \uff0c\u5373\u53ef\u5f97\u5230\u300c\u5de6\u65cb\u300d\u4ee3\u7801\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart avl_tree.java
    /* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode leftRotate(TreeNode node) {\nTreeNode child = node.right;\nTreeNode grandChild = child.left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.left = node;\nnode.right = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.cpp
    /* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode *leftRotate(TreeNode *node) {\nTreeNode *child = node->right;\nTreeNode *grandChild = child->left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild->left = node;\nnode->right = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.py
    def __left_rotate(self, node: TreeNode | None) -> TreeNode | None:\n\"\"\"\u5de6\u65cb\u64cd\u4f5c\"\"\"\nchild = node.right\ngrand_child = child.left\n# \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.left = node\nnode.right = grand_child\n# \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nself.__update_height(node)\nself.__update_height(child)\n# \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child\n
    avl_tree.go
    /* \u5de6\u65cb\u64cd\u4f5c */\nfunc (t *aVLTree) leftRotate(node *TreeNode) *TreeNode {\nchild := node.Right\ngrandChild := child.Left\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.Left = node\nnode.Right = grandChild\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nt.updateHeight(node)\nt.updateHeight(child)\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child\n}\n
    avl_tree.js
    /* \u5de6\u65cb\u64cd\u4f5c */\n#leftRotate(node) {\nconst child = node.right;\nconst grandChild = child.left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.left = node;\nnode.right = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nthis.#updateHeight(node);\nthis.#updateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.ts
    /* \u5de6\u65cb\u64cd\u4f5c */\nleftRotate(node: TreeNode): TreeNode {\nconst child = node.right;\nconst grandChild = child.left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.left = node;\nnode.right = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nthis.updateHeight(node);\nthis.updateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.c
    /* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode *leftRotate(TreeNode *node) {\nTreeNode *child, *grandChild;\nchild = node->right;\ngrandChild = child->left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild->left = node;\nnode->right = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.cs
    /* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode? leftRotate(TreeNode? node) {\nTreeNode? child = node.right;\nTreeNode? grandChild = child?.left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.left = node;\nnode.right = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.swift
    /* \u5de6\u65cb\u64cd\u4f5c */\nfunc leftRotate(node: TreeNode?) -> TreeNode? {\nlet child = node?.right\nlet grandChild = child?.left\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild?.left = node\nnode?.right = grandChild\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node: node)\nupdateHeight(node: child)\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child\n}\n
    avl_tree.zig
    // \u5de6\u65cb\u64cd\u4f5c\nfn leftRotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\nvar child = node.?.right;\nvar grandChild = child.?.left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.?.left = node;\nnode.?.right = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nself.updateHeight(node);\nself.updateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    avl_tree.dart
    /* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode? leftRotate(TreeNode? node) {\nTreeNode? child = node!.right;\nTreeNode? grandChild = child!.left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.left = node;\nnode.right = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n
    "},{"location":"chapter_tree/avl_tree/#_5","title":"\u5148\u5de6\u65cb\u540e\u53f3\u65cb","text":"

    \u5bf9\u4e8e\u4e0b\u56fe\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\u5de6\u65cb\u540e\u53f3\u65cb\uff0c\u5373\u5148\u5bf9 child \u6267\u884c\u300c\u5de6\u65cb\u300d\uff0c\u518d\u5bf9 node \u6267\u884c\u300c\u53f3\u65cb\u300d\u3002

    Fig. \u5148\u5de6\u65cb\u540e\u53f3\u65cb

    "},{"location":"chapter_tree/avl_tree/#_6","title":"\u5148\u53f3\u65cb\u540e\u5de6\u65cb","text":"

    \u540c\u7406\uff0c\u5bf9\u4e8e\u4e0a\u8ff0\u5931\u8861\u4e8c\u53c9\u6811\u7684\u955c\u50cf\u60c5\u51b5\uff0c\u9700\u8981\u5148\u53f3\u65cb\u540e\u5de6\u65cb\uff0c\u5373\u5148\u5bf9 child \u6267\u884c\u300c\u53f3\u65cb\u300d\uff0c\u7136\u540e\u5bf9 node \u6267\u884c\u300c\u5de6\u65cb\u300d\u3002

    Fig. \u5148\u53f3\u65cb\u540e\u5de6\u65cb

    "},{"location":"chapter_tree/avl_tree/#_7","title":"\u65cb\u8f6c\u7684\u9009\u62e9","text":"

    \u4e0b\u56fe\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\u5de6\u65cb\u3001\u5148\u53f3\u540e\u5de6\u3001\u5148\u5de6\u540e\u53f3\u7684\u65cb\u8f6c\u64cd\u4f5c\u3002

    Fig. AVL \u6811\u7684\u56db\u79cd\u65cb\u8f6c\u60c5\u51b5

    \u5728\u4ee3\u7801\u4e2d\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\u4e0a\u56fe\u4e2d\u7684\u54ea\u79cd\u60c5\u51b5\u3002

    \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 \\(>0\\) \uff08\u5373\u5de6\u504f\u6811\uff09 \\(\\geq 0\\) \u53f3\u65cb \\(>0\\) \uff08\u5373\u5de6\u504f\u6811\uff09 \\(<0\\) \u5148\u5de6\u65cb\u540e\u53f3\u65cb \\(<0\\) \uff08\u5373\u53f3\u504f\u6811\uff09 \\(\\leq 0\\) \u5de6\u65cb \\(<0\\) \uff08\u5373\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nint balanceFactor = balanceFactor(node);\n// \u5de6\u504f\u6811\nif (balanceFactor > 1) {\nif (balanceFactor(node.left) >= 0) {\n// \u53f3\u65cb\nreturn rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.left = leftRotate(node.left);\nreturn rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (balanceFactor < -1) {\nif (balanceFactor(node.right) <= 0) {\n// \u5de6\u65cb\nreturn leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.right = rightRotate(node.right);\nreturn leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\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\nint _balanceFactor = balanceFactor(node);\n// \u5de6\u504f\u6811\nif (_balanceFactor > 1) {\nif (balanceFactor(node->left) >= 0) {\n// \u53f3\u65cb\nreturn rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode->left = leftRotate(node->left);\nreturn rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (_balanceFactor < -1) {\nif (balanceFactor(node->right) <= 0) {\n// \u5de6\u65cb\nreturn leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode->right = rightRotate(node->right);\nreturn leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\n
    avl_tree.py
    def __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\nbalance_factor = self.balance_factor(node)\n# \u5de6\u504f\u6811\nif balance_factor > 1:\nif self.balance_factor(node.left) >= 0:\n# \u53f3\u65cb\nreturn self.__right_rotate(node)\nelse:\n# \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.left = self.__left_rotate(node.left)\nreturn self.__right_rotate(node)\n# \u53f3\u504f\u6811\nelif balance_factor < -1:\nif self.balance_factor(node.right) <= 0:\n# \u5de6\u65cb\nreturn self.__left_rotate(node)\nelse:\n# \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.right = self.__right_rotate(node.right)\nreturn self.__left_rotate(node)\n# \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node\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\nbf := t.balanceFactor(node)\n// \u5de6\u504f\u6811\nif bf > 1 {\nif t.balanceFactor(node.Left) >= 0 {\n// \u53f3\u65cb\nreturn t.rightRotate(node)\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.Left = t.leftRotate(node.Left)\nreturn t.rightRotate(node)\n}\n}\n// \u53f3\u504f\u6811\nif bf < -1 {\nif t.balanceFactor(node.Right) <= 0 {\n// \u5de6\u65cb\nreturn t.leftRotate(node)\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.Right = t.rightRotate(node.Right)\nreturn t.leftRotate(node)\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn 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\nconst balanceFactor = this.balanceFactor(node);\n// \u5de6\u504f\u6811\nif (balanceFactor > 1) {\nif (this.balanceFactor(node.left) >= 0) {\n// \u53f3\u65cb\nreturn this.#rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.left = this.#leftRotate(node.left);\nreturn this.#rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (balanceFactor < -1) {\nif (this.balanceFactor(node.right) <= 0) {\n// \u5de6\u65cb\nreturn this.#leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.right = this.#rightRotate(node.right);\nreturn this.#leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn 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\nconst balanceFactor = this.balanceFactor(node);\n// \u5de6\u504f\u6811\nif (balanceFactor > 1) {\nif (this.balanceFactor(node.left) >= 0) {\n// \u53f3\u65cb\nreturn this.rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.left = this.leftRotate(node.left);\nreturn this.rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (balanceFactor < -1) {\nif (this.balanceFactor(node.right) <= 0) {\n// \u5de6\u65cb\nreturn this.leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.right = this.rightRotate(node.right);\nreturn this.leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\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\nint bf = balanceFactor(node);\n// \u5de6\u504f\u6811\nif (bf > 1) {\nif (balanceFactor(node->left) >= 0) {\n// \u53f3\u65cb\nreturn rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode->left = leftRotate(node->left);\nreturn rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (bf < -1) {\nif (balanceFactor(node->right) <= 0) {\n// \u5de6\u65cb\nreturn leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode->right = rightRotate(node->right);\nreturn leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn 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\nint balanceFactorInt = balanceFactor(node);\n// \u5de6\u504f\u6811\nif (balanceFactorInt > 1) {\nif (balanceFactor(node.left) >= 0) {\n// \u53f3\u65cb\nreturn rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.left = leftRotate(node?.left);\nreturn rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (balanceFactorInt < -1) {\nif (balanceFactor(node.right) <= 0) {\n// \u5de6\u65cb\nreturn leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.right = rightRotate(node?.right);\nreturn leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn 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\nlet balanceFactor = balanceFactor(node: node)\n// \u5de6\u504f\u6811\nif balanceFactor > 1 {\nif self.balanceFactor(node: node?.left) >= 0 {\n// \u53f3\u65cb\nreturn rightRotate(node: node)\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode?.left = leftRotate(node: node?.left)\nreturn rightRotate(node: node)\n}\n}\n// \u53f3\u504f\u6811\nif balanceFactor < -1 {\nif self.balanceFactor(node: node?.right) <= 0 {\n// \u5de6\u65cb\nreturn leftRotate(node: node)\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode?.right = rightRotate(node: node?.right)\nreturn leftRotate(node: node)\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn 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\nvar balance_factor = self.balanceFactor(node);\n// \u5de6\u504f\u6811\nif (balance_factor > 1) {\nif (self.balanceFactor(node.?.left) >= 0) {\n// \u53f3\u65cb\nreturn self.rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.?.left = self.leftRotate(node.?.left);\nreturn self.rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (balance_factor < -1) {\nif (self.balanceFactor(node.?.right) <= 0) {\n// \u5de6\u65cb\nreturn self.leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.?.right = self.rightRotate(node.?.right);\nreturn self.leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn 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\nint factor = balanceFactor(node);\n// \u5de6\u504f\u6811\nif (factor > 1) {\nif (balanceFactor(node!.left) >= 0) {\n// \u53f3\u65cb\nreturn rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.left = leftRotate(node.left);\nreturn rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (factor < -1) {\nif (balanceFactor(node!.right) <= 0) {\n// \u5de6\u65cb\nreturn leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.right = rightRotate(node.right);\nreturn leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn 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/#_8","title":"\u63d2\u5165\u8282\u70b9","text":"

    \u300cAVL \u6811\u300d\u7684\u8282\u70b9\u63d2\u5165\u64cd\u4f5c\u4e0e\u300c\u4e8c\u53c9\u641c\u7d22\u6811\u300d\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart avl_tree.java
    /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\nroot = insertHelper(root, val);\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode insertHelper(TreeNode node, int val) {\nif (node == null)\nreturn new TreeNode(val);\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif (val < node.val)\nnode.left = insertHelper(node.left, val);\nelse if (val > node.val)\nnode.right = insertHelper(node.right, val);\nelse\nreturn node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nupdateHeight(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 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.cpp
    /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\nroot = insertHelper(root, val);\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode *insertHelper(TreeNode *node, int val) {\nif (node == nullptr)\nreturn new TreeNode(val);\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif (val < node->val)\nnode->left = insertHelper(node->left, val);\nelse if (val > node->val)\nnode->right = insertHelper(node->right, val);\nelse\nreturn node;    // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nupdateHeight(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 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.py
    def insert(self, val) -> None:\n\"\"\"\u63d2\u5165\u8282\u70b9\"\"\"\nself.__root = self.__insert_helper(self.__root, val)\ndef __insert_helper(self, node: TreeNode | None, val: int) -> TreeNode:\n\"\"\"\u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\"\"\"\nif node is None:\nreturn TreeNode(val)\n# 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9\nif val < node.val:\nnode.left = self.__insert_helper(node.left, val)\nelif val > node.val:\nnode.right = self.__insert_helper(node.right, val)\nelse:\n# \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node\n# \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nself.__update_height(node)\n# 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\nreturn self.__rotate(node)\n
    avl_tree.go
    /* \u63d2\u5165\u8282\u70b9 */\nfunc (t *aVLTree) insert(val int) {\nt.root = t.insertHelper(t.root, val)\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc (t *aVLTree) insertHelper(node *TreeNode, val int) *TreeNode {\nif node == nil {\nreturn NewTreeNode(val)\n}\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif val < node.Val {\nnode.Left = t.insertHelper(node.Left, val)\n} else if val > node.Val {\nnode.Right = t.insertHelper(node.Right, val)\n} else {\n// \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node\n}\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nt.updateHeight(node)\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = t.rotate(node)\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node\n}\n
    avl_tree.js
    /* \u63d2\u5165\u8282\u70b9 */\ninsert(val) {\nthis.root = this.#insertHelper(this.root, val);\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\n#insertHelper(node, val) {\nif (node === null) return new TreeNode(val);\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif (val < node.val) node.left = this.#insertHelper(node.left, val);\nelse if (val > node.val)\nnode.right = this.#insertHelper(node.right, val);\nelse return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nthis.#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 */\nnode = this.#rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.ts
    /* \u63d2\u5165\u8282\u70b9 */\ninsert(val: number): void {\nthis.root = this.insertHelper(this.root, val);\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\ninsertHelper(node: TreeNode, val: number): TreeNode {\nif (node === null) return new TreeNode(val);\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif (val < node.val) {\nnode.left = this.insertHelper(node.left, val);\n} else if (val > node.val) {\nnode.right = this.insertHelper(node.right, val);\n} else {\nreturn node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n}\nthis.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 */\nnode = this.rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.c
    /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(aVLTree *tree, int val) {\ntree->root = insertHelper(tree->root, val);\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode *insertHelper(TreeNode *node, int val) {\nif (node == NULL) {\nreturn newTreeNode(val);\n}\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif (val < node->val) {\nnode->left = insertHelper(node->left, val);\n} else if (val > node->val) {\nnode->right = insertHelper(node->right, val);\n} else {\n// \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.cs
    /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\nroot = insertHelper(root, val);\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? insertHelper(TreeNode? node, int val) {\nif (node == null) return new TreeNode(val);\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif (val < node.val)\nnode.left = insertHelper(node.left, val);\nelse if (val > node.val)\nnode.right = insertHelper(node.right, val);\nelse\nreturn node;     // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nupdateHeight(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 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.swift
    /* \u63d2\u5165\u8282\u70b9 */\n@discardableResult\nfunc insert(val: Int) {\nroot = insertHelper(node: root, val: val)\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc insertHelper(node: TreeNode?, val: Int) -> TreeNode? {\nvar node = node\nif node == nil {\nreturn TreeNode(x: val)\n}\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif val < node!.val {\nnode?.left = insertHelper(node: node?.left, val: val)\n} else if val > node!.val {\nnode?.right = insertHelper(node: node?.right, val: val)\n} else {\nreturn node // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n}\nupdateHeight(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 */\nnode = rotate(node: node)\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node\n}\n
    avl_tree.zig
    // \u63d2\u5165\u8282\u70b9\nfn insert(self: *Self, val: T) void {\nself.root = try self.insertHelper(self.root, val);\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) {\nvar node = node_;\nif (node == null) {\nvar tmp_node = try self.mem_allocator.create(inc.TreeNode(T));\ntmp_node.init(val);\nreturn tmp_node;\n}\n// 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9\nif (val < node.?.val) {\nnode.?.left = try self.insertHelper(node.?.left, val);\n} else if (val > node.?.val) {\nnode.?.right = try self.insertHelper(node.?.right, val);\n} else {\nreturn node;            // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n}\nself.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\nnode = self.rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.dart
    /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\nroot = insertHelper(root, val);\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? insertHelper(TreeNode? node, int val) {\nif (node == null) return TreeNode(val);\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif (val < node.val)\nnode.left = insertHelper(node.left, val);\nelse if (val > node.val)\nnode.right = insertHelper(node.right, val);\nelse\nreturn node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nupdateHeight(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 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    "},{"location":"chapter_tree/avl_tree/#_9","title":"\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\u5730\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u6240\u6709\u5931\u8861\u8282\u70b9\u6062\u590d\u5e73\u8861\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart avl_tree.java
    /* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\nroot = removeHelper(root, val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode removeHelper(TreeNode node, int val) {\nif (node == null)\nreturn null;\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val < node.val)\nnode.left = removeHelper(node.left, val);\nelse if (val > node.val)\nnode.right = removeHelper(node.right, val);\nelse {\nif (node.left == null || node.right == null) {\nTreeNode child = node.left != null ? node.left : node.right;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child == null)\nreturn null;\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse\nnode = 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\nTreeNode temp = node.right;\nwhile (temp.left != null) {\ntemp = temp.left;\n}\nnode.right = removeHelper(node.right, temp.val);\nnode.val = temp.val;\n}\n}\nupdateHeight(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 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.cpp
    /* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\nroot = removeHelper(root, val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode *removeHelper(TreeNode *node, int val) {\nif (node == nullptr)\nreturn nullptr;\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val < node->val)\nnode->left = removeHelper(node->left, val);\nelse if (val > node->val)\nnode->right = removeHelper(node->right, val);\nelse {\nif (node->left == nullptr || node->right == nullptr) {\nTreeNode *child = node->left != nullptr ? node->left : node->right;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child == nullptr) {\ndelete node;\nreturn nullptr;\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse {\ndelete node;\nnode = 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\nTreeNode *temp = node->right;\nwhile (temp->left != nullptr) {\ntemp = temp->left;\n}\nint tempVal = temp->val;\nnode->right = removeHelper(node->right, temp->val);\nnode->val = tempVal;\n}\n}\nupdateHeight(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 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.py
    def remove(self, val: int) -> None:\n\"\"\"\u5220\u9664\u8282\u70b9\"\"\"\nself.__root = self.__remove_helper(self.__root, val)\ndef __remove_helper(self, node: TreeNode | None, val: int) -> TreeNode | None:\n\"\"\"\u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\"\"\"\nif node is None:\nreturn None\n# 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b\nif val < node.val:\nnode.left = self.__remove_helper(node.left, val)\nelif val > node.val:\nnode.right = self.__remove_helper(node.right, val)\nelse:\nif node.left is None or node.right is None:\nchild = node.left or node.right\n# \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif child is None:\nreturn None\n# \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse:\nnode = child\nelse:\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\ntemp = node.right\nwhile temp.left is not None:\ntemp = temp.left\nnode.right = self.__remove_helper(node.right, temp.val)\nnode.val = temp.val\n# \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nself.__update_height(node)\n# 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\nreturn self.__rotate(node)\n
    avl_tree.go
    /* \u5220\u9664\u8282\u70b9 */\nfunc (t *aVLTree) remove(val int) {\nt.root = t.removeHelper(t.root, val)\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode {\nif node == nil {\nreturn nil\n}\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif val < node.Val {\nnode.Left = t.removeHelper(node.Left, val)\n} else if val > node.Val {\nnode.Right = t.removeHelper(node.Right, val)\n} else {\nif node.Left == nil || node.Right == nil {\nchild := node.Left\nif node.Right != nil {\nchild = node.Right\n}\nif child == nil {\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nreturn nil\n} else {\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nnode = 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\ntemp := node.Right\nfor temp.Left != nil {\ntemp = temp.Left\n}\nnode.Right = t.removeHelper(node.Right, temp.Val)\nnode.Val = temp.Val\n}\n}\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nt.updateHeight(node)\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = t.rotate(node)\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node\n}\n
    avl_tree.js
    /* \u5220\u9664\u8282\u70b9 */\nremove(val) {\nthis.root = this.#removeHelper(this.root, val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\n#removeHelper(node, val) {\nif (node === null) return null;\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val < node.val) node.left = this.#removeHelper(node.left, val);\nelse if (val > node.val)\nnode.right = this.#removeHelper(node.right, val);\nelse {\nif (node.left === null || node.right === null) {\nconst child = node.left !== null ? node.left : node.right;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child === null) return null;\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse 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\nlet temp = node.right;\nwhile (temp.left !== null) {\ntemp = temp.left;\n}\nnode.right = this.#removeHelper(node.right, temp.val);\nnode.val = temp.val;\n}\n}\nthis.#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 */\nnode = this.#rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.ts
    /* \u5220\u9664\u8282\u70b9 */\nremove(val: number): void {\nthis.root = this.removeHelper(this.root, val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nremoveHelper(node: TreeNode, val: number): TreeNode {\nif (node === null) return null;\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val < node.val) {\nnode.left = this.removeHelper(node.left, val);\n} else if (val > node.val) {\nnode.right = this.removeHelper(node.right, val);\n} else {\nif (node.left === null || node.right === null) {\nconst child = node.left !== null ? node.left : node.right;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child === null) {\nreturn null;\n} else {\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nnode = 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\nlet temp = node.right;\nwhile (temp.left !== null) {\ntemp = temp.left;\n}\nnode.right = this.removeHelper(node.right, temp.val);\nnode.val = temp.val;\n}\n}\nthis.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 */\nnode = this.rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\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 removeNode(aVLTree *tree, int val) {\nTreeNode *root = removeHelper(tree->root, val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode *removeHelper(TreeNode *node, int val) {\nTreeNode *child, *grandChild;\nif (node == NULL) {\nreturn NULL;\n}\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val < node->val) {\nnode->left = removeHelper(node->left, val);\n} else if (val > node->val) {\nnode->right = removeHelper(node->right, val);\n} else {\nif (node->left == NULL || node->right == NULL) {\nchild = node->left;\nif (node->right != NULL) {\nchild = node->right;\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child == NULL) {\nreturn NULL;\n} else {\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nnode = 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\nTreeNode *temp = node->right;\nwhile (temp->left != NULL) {\ntemp = temp->left;\n}\nint tempVal = temp->val;\nnode->right = removeHelper(node->right, temp->val);\nnode->val = tempVal;\n}\n}\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.cs
    /* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\nroot = removeHelper(root, val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? removeHelper(TreeNode? node, int val) {\nif (node == null) return null;\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val < node.val)\nnode.left = removeHelper(node.left, val);\nelse if (val > node.val)\nnode.right = removeHelper(node.right, val);\nelse {\nif (node.left == null || node.right == null) {\nTreeNode? child = node.left != null ? node.left : node.right;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child == null)\nreturn null;\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse\nnode = 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\nTreeNode? temp = node.right;\nwhile (temp.left != null) {\ntemp = temp.left;\n}\nnode.right = removeHelper(node.right, temp.val);\nnode.val = temp.val;\n}\n}\nupdateHeight(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 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.swift
    /* \u5220\u9664\u8282\u70b9 */\n@discardableResult\nfunc remove(val: Int) {\nroot = removeHelper(node: root, val: val)\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc removeHelper(node: TreeNode?, val: Int) -> TreeNode? {\nvar node = node\nif node == nil {\nreturn nil\n}\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif val < node!.val {\nnode?.left = removeHelper(node: node?.left, val: val)\n} else if val > node!.val {\nnode?.right = removeHelper(node: node?.right, val: val)\n} else {\nif node?.left == nil || node?.right == nil {\nlet child = node?.left != nil ? node?.left : node?.right\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif child == nil {\nreturn nil\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse {\nnode = 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\nvar temp = node?.right\nwhile temp?.left != nil {\ntemp = temp?.left\n}\nnode?.right = removeHelper(node: node?.right, val: temp!.val)\nnode?.val = temp!.val\n}\n}\nupdateHeight(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 */\nnode = rotate(node: node)\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node\n}\n
    avl_tree.zig
    // \u5220\u9664\u8282\u70b9\nfn remove(self: *Self, val: T) void {\nself.root = self.removeHelper(self.root, val);\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) {\nvar node = node_;\nif (node == null) return null;\n// 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b\nif (val < node.?.val) {\nnode.?.left = self.removeHelper(node.?.left, val);\n} else if (val > node.?.val) {\nnode.?.right = self.removeHelper(node.?.right, val);\n} else {\nif (node.?.left == null or node.?.right == null) {\nvar 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\nif (child == null) {\nreturn null;\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n} else {\nnode = 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\nvar temp = node.?.right;\nwhile (temp.?.left != null) {\ntemp = temp.?.left;\n}\nnode.?.right = self.removeHelper(node.?.right, temp.?.val);\nnode.?.val = temp.?.val;\n}\n}\nself.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\nnode = self.rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    avl_tree.dart
    /* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\nroot = removeHelper(root, val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? removeHelper(TreeNode? node, int val) {\nif (node == null) return null;\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val < node.val)\nnode.left = removeHelper(node.left, val);\nelse if (val > node.val)\nnode.right = removeHelper(node.right, val);\nelse {\nif (node.left == null || node.right == null) {\nTreeNode? child = node.left ?? node.right;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child == null)\nreturn null;\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse\nnode = 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\nTreeNode? temp = node.right;\nwhile (temp!.left != null) {\ntemp = temp.left;\n}\nnode.right = removeHelper(node.right, temp.val);\nnode.val = temp.val;\n}\n}\nupdateHeight(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 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n
    "},{"location":"chapter_tree/avl_tree/#_10","title":"\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":"
    • \u7ec4\u7ec7\u548c\u5b58\u50a8\u5927\u578b\u6570\u636e\uff0c\u9002\u7528\u4e8e\u9ad8\u9891\u67e5\u627e\u3001\u4f4e\u9891\u589e\u5220\u7684\u573a\u666f\uff1b
    • \u7528\u4e8e\u6784\u5efa\u6570\u636e\u5e93\u4e2d\u7684\u7d22\u5f15\u7cfb\u7edf\uff1b

    \u4e3a\u4ec0\u4e48\u7ea2\u9ed1\u6811\u6bd4 AVL \u6811\u66f4\u53d7\u6b22\u8fce\uff1f

    \u7ea2\u9ed1\u6811\u7684\u5e73\u8861\u6761\u4ef6\u76f8\u5bf9\u5bbd\u677e\uff0c\u56e0\u6b64\u5728\u7ea2\u9ed1\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\u6240\u9700\u7684\u65cb\u8f6c\u64cd\u4f5c\u76f8\u5bf9\u8f83\u5c11\uff0c\u5728\u8282\u70b9\u589e\u5220\u64cd\u4f5c\u4e0a\u7684\u5e73\u5747\u6548\u7387\u9ad8\u4e8e AVL \u6811\u3002

    "},{"location":"chapter_tree/binary_search_tree/","title":"7.4. \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811","text":"

    \u300c\u4e8c\u53c9\u641c\u7d22\u6811 Binary Search Tree\u300d\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\uff1a

    1. \u5bf9\u4e8e\u6839\u8282\u70b9\uff0c\u5de6\u5b50\u6811\u4e2d\u6240\u6709\u8282\u70b9\u7684\u503c \\(<\\) \u6839\u8282\u70b9\u7684\u503c \\(<\\) \u53f3\u5b50\u6811\u4e2d\u6240\u6709\u8282\u70b9\u7684\u503c\uff1b
    2. \u4efb\u610f\u8282\u70b9\u7684\u5de6\u3001\u53f3\u5b50\u6811\u4e5f\u662f\u4e8c\u53c9\u641c\u7d22\u6811\uff0c\u5373\u540c\u6837\u6ee1\u8db3\u6761\u4ef6 1. \uff1b

    Fig. \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":""},{"location":"chapter_tree/binary_search_tree/#_1","title":"\u67e5\u627e\u8282\u70b9","text":"

    \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\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

    • \u82e5 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 \uff1b
    • \u82e5 cur.val > num \uff0c\u8bf4\u660e\u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\uff0c\u56e0\u6b64\u6267\u884c cur = cur.left \uff1b
    • \u82e5 cur.val = num \uff0c\u8bf4\u660e\u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\u5e76\u8fd4\u56de\u8be5\u8282\u70b9\uff1b
    <1><2><3><4>

    \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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart binary_search_tree.java
    /* \u67e5\u627e\u8282\u70b9 */\nTreeNode search(int num) {\nTreeNode cur = root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num)\ncur = cur.right;\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse if (cur.val > num)\ncur = cur.left;\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse\nbreak;\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn cur;\n}\n
    binary_search_tree.cpp
    /* \u67e5\u627e\u8282\u70b9 */\nTreeNode *search(int num) {\nTreeNode *cur = root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != nullptr) {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur->val < num)\ncur = cur->right;\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse if (cur->val > num)\ncur = cur->left;\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse\nbreak;\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn cur;\n}\n
    binary_search_tree.py
    def search(self, num: int) -> TreeNode | None:\n\"\"\"\u67e5\u627e\u8282\u70b9\"\"\"\ncur: TreeNode | None = self.root\n# \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile cur is not None:\n# \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif cur.val < num:\ncur = cur.right\n# \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelif cur.val > num:\ncur = cur.left\n# \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse:\nbreak\nreturn cur\n
    binary_search_tree.go
    /* \u67e5\u627e\u8282\u70b9 */\nfunc (bst *binarySearchTree) search(num int) *TreeNode {\nnode := bst.root\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nfor node != nil {\nif node.Val < num {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nnode = node.Right\n} else if node.Val > num {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nnode = node.Left\n} else {\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nbreak\n}\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn node\n}\n
    binary_search_tree.js
    /* \u67e5\u627e\u8282\u70b9 */\nfunction search(num) {\nlet cur = root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur !== null) {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num) cur = cur.right;\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse if (cur.val > num) cur = cur.left;\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse break;\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn cur;\n}\n
    binary_search_tree.ts
    /* \u67e5\u627e\u8282\u70b9 */\nfunction search(num: number): TreeNode | null {\nlet cur = root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur !== null) {\nif (cur.val < num) {\ncur = cur.right; // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n} else if (cur.val > num) {\ncur = cur.left; // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n} else {\nbreak; // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n}\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn cur;\n}\n
    binary_search_tree.c
    /* \u67e5\u627e\u8282\u70b9 */\nTreeNode *search(binarySearchTree *bst, int num) {\nTreeNode *cur = bst->root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != NULL) {\nif (cur->val < num) {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\ncur = cur->right;\n} else if (cur->val > num) {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\ncur = cur->left;\n} else {\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nbreak;\n}\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn cur;\n}\n
    binary_search_tree.cs
    /* \u67e5\u627e\u8282\u70b9 */\nTreeNode? search(int num) {\nTreeNode? cur = root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num) cur =\ncur.right;\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse if (cur.val > num)\ncur = cur.left;\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse\nbreak;\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn cur;\n}\n
    binary_search_tree.swift
    /* \u67e5\u627e\u8282\u70b9 */\nfunc search(num: Int) -> TreeNode? {\nvar cur = root\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile cur != nil {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif cur!.val < num {\ncur = cur?.right\n}\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse if cur!.val > num {\ncur = cur?.left\n}\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse {\nbreak\n}\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn cur\n}\n
    binary_search_tree.zig
    // \u67e5\u627e\u8282\u70b9\nfn search(self: *Self, num: T) ?*inc.TreeNode(T) {\nvar cur = self.root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.?.val < num) {\ncur = cur.?.right;\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n} else if (cur.?.val > num) {\ncur = cur.?.left;\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n} else {\nbreak;\n}\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn cur;\n}\n
    binary_search_tree.dart
    [class]{BinarySearchTree}-[func]{search}\n
    "},{"location":"chapter_tree/binary_search_tree/#_2","title":"\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\u5206\u4e3a\u4e24\u6b65\uff1a

    1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff1a\u4e0e\u67e5\u627e\u64cd\u4f5c\u76f8\u4f3c\uff0c\u4ece\u6839\u8282\u70b9\u51fa\u53d1\uff0c\u6839\u636e\u5f53\u524d\u8282\u70b9\u503c\u548c 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{null}\\) \uff09\u65f6\u8df3\u51fa\u5faa\u73af\uff1b
    2. \u5728\u8be5\u4f4d\u7f6e\u63d2\u5165\u8282\u70b9\uff1a\u521d\u59cb\u5316\u8282\u70b9 num \uff0c\u5c06\u8be5\u8282\u70b9\u7f6e\u4e8e \\(\\text{null}\\) \u7684\u4f4d\u7f6e\uff1b

    \u4e8c\u53c9\u641c\u7d22\u6811\u4e0d\u5141\u8bb8\u5b58\u5728\u91cd\u590d\u8282\u70b9\uff0c\u5426\u5219\u5c06\u8fdd\u53cd\u5176\u5b9a\u4e49\u3002\u56e0\u6b64\uff0c\u82e5\u5f85\u63d2\u5165\u8282\u70b9\u5728\u6811\u4e2d\u5df2\u5b58\u5728\uff0c\u5219\u4e0d\u6267\u884c\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\u3002

    Fig. \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u63d2\u5165\u8282\u70b9

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart binary_search_tree.java
    /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root == null)\nreturn;\nTreeNode cur = root, pre = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif (cur.val == num)\nreturn;\npre = cur;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num)\ncur = cur.right;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse\ncur = cur.left;\n}\n// \u63d2\u5165\u8282\u70b9\nTreeNode node = new TreeNode(num);\nif (pre.val < num)\npre.right = node;\nelse\npre.left = node;\n}\n
    binary_search_tree.cpp
    /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root == nullptr)\nreturn;\nTreeNode *cur = root, *pre = nullptr;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != nullptr) {\n// \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif (cur->val == num)\nreturn;\npre = cur;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur->val < num)\ncur = cur->right;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse\ncur = cur->left;\n}\n// \u63d2\u5165\u8282\u70b9\nTreeNode *node = new TreeNode(num);\nif (pre->val < num)\npre->right = node;\nelse\npre->left = node;\n}\n
    binary_search_tree.py
    def insert(self, num: int) -> None:\n\"\"\"\u63d2\u5165\u8282\u70b9\"\"\"\n# \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif self.root is None:\nreturn\n# \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\ncur, pre = self.root, None\nwhile cur is not None:\n# \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif cur.val == num:\nreturn\npre = cur\n# \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif cur.val < num:\ncur = cur.right\n# \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse:\ncur = cur.left\n# \u63d2\u5165\u8282\u70b9\nnode = TreeNode(num)\nif pre.val < num:\npre.right = node\nelse:\npre.left = node\n
    binary_search_tree.go
    /* \u63d2\u5165\u8282\u70b9 */\nfunc (bst *binarySearchTree) insert(num int) {\ncur := bst.root\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif cur == nil {\nreturn\n}\n// \u5f85\u63d2\u5165\u8282\u70b9\u4e4b\u524d\u7684\u8282\u70b9\u4f4d\u7f6e\nvar pre *TreeNode = nil\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nfor cur != nil {\nif cur.Val == num {\nreturn\n}\npre = cur\nif cur.Val < num {\ncur = cur.Right\n} else {\ncur = cur.Left\n}\n}\n// \u63d2\u5165\u8282\u70b9\nnode := NewTreeNode(num)\nif pre.Val < num {\npre.Right = node\n} else {\npre.Left = node\n}\n}\n
    binary_search_tree.js
    /* \u63d2\u5165\u8282\u70b9 */\nfunction insert(num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root === null) return;\nlet cur = root,\npre = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur !== null) {\n// \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif (cur.val === num) return;\npre = cur;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num) cur = cur.right;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse cur = cur.left;\n}\n// \u63d2\u5165\u8282\u70b9\nlet node = new TreeNode(num);\nif (pre.val < num) pre.right = node;\nelse pre.left = node;\n}\n
    binary_search_tree.ts
    /* \u63d2\u5165\u8282\u70b9 */\nfunction insert(num: number): void {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root === null) {\nreturn;\n}\nlet cur = root,\npre: TreeNode | null = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur !== null) {\nif (cur.val === num) {\nreturn; // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n}\npre = cur;\nif (cur.val < num) {\ncur = cur.right as TreeNode; // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n} else {\ncur = cur.left as TreeNode; // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n}\n}\n// \u63d2\u5165\u8282\u70b9\nlet node = new TreeNode(num);\nif (pre!.val < num) {\npre!.right = node;\n} else {\npre!.left = node;\n}\n}\n
    binary_search_tree.c
    /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(binarySearchTree *bst, int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (bst->root == NULL)\nreturn;\nTreeNode *cur = bst->root, *pre = NULL;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != NULL) {\n// \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif (cur->val == num) {\nreturn;\n}\npre = cur;\nif (cur->val < num) {\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\ncur = cur->right;\n} else {\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\ncur = cur->left;\n}\n}\n// \u63d2\u5165\u8282\u70b9\nTreeNode *node = newTreeNode(num);\nif (pre->val < num) {\npre->right = node;\n} else {\npre->left = node;\n}\n}\n
    binary_search_tree.cs
    /* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root == null)\nreturn;\nTreeNode? cur = root, pre = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif (cur.val == num)\nreturn;\npre = cur;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num)\ncur = cur.right;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse\ncur = cur.left;\n}\n// \u63d2\u5165\u8282\u70b9\nTreeNode node = new TreeNode(num);\nif (pre != null) {\nif (pre.val < num)\npre.right = node;\nelse\npre.left = node;\n}\n}\n
    binary_search_tree.swift
    /* \u63d2\u5165\u8282\u70b9 */\nfunc insert(num: Int) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif root == nil {\nreturn\n}\nvar cur = root\nvar pre: TreeNode?\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile cur != nil {\n// \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif cur!.val == num {\nreturn\n}\npre = cur\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif cur!.val < num {\ncur = cur?.right\n}\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse {\ncur = cur?.left\n}\n}\n// \u63d2\u5165\u8282\u70b9\nlet node = TreeNode(x: num)\nif pre!.val < num {\npre?.right = node\n} else {\npre?.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\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (self.root == null) return;\nvar cur = self.root;\nvar pre: ?*inc.TreeNode(T) = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif (cur.?.val == num) return;\npre = cur;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.?.val < num) {\ncur = cur.?.right;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n} else {\ncur = cur.?.left;\n}\n}\n// \u63d2\u5165\u8282\u70b9\nvar node = try self.mem_allocator.create(inc.TreeNode(T));\nnode.init(num);\nif (pre.?.val < num) {\npre.?.right = node;\n} else {\npre.?.left = node;\n}\n}\n
    binary_search_tree.dart
    [class]{BinarySearchTree}-[func]{insert}\n

    \u4e3a\u4e86\u63d2\u5165\u8282\u70b9\uff0c\u6211\u4eec\u9700\u8981\u5229\u7528\u8f85\u52a9\u8282\u70b9 pre \u4fdd\u5b58\u4e0a\u4e00\u8f6e\u5faa\u73af\u7684\u8282\u70b9\uff0c\u8fd9\u6837\u5728\u904d\u5386\u81f3 \\(\\text{null}\\) \u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u5230\u5176\u7236\u8282\u70b9\uff0c\u4ece\u800c\u5b8c\u6210\u8282\u70b9\u63d2\u5165\u64cd\u4f5c\u3002

    \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":"\u5220\u9664\u8282\u70b9","text":"

    \u4e0e\u63d2\u5165\u8282\u70b9\u7c7b\u4f3c\uff0c\u6211\u4eec\u9700\u8981\u5728\u5220\u9664\u64cd\u4f5c\u540e\u7ef4\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u201c\u5de6\u5b50\u6811 < \u6839\u8282\u70b9 < \u53f3\u5b50\u6811\u201d\u7684\u6027\u8d28\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u4e8c\u53c9\u6811\u4e2d\u6267\u884c\u67e5\u627e\u64cd\u4f5c\uff0c\u83b7\u53d6\u5f85\u5220\u9664\u8282\u70b9\u3002\u63a5\u4e0b\u6765\uff0c\u6839\u636e\u5f85\u5220\u9664\u8282\u70b9\u7684\u5b50\u8282\u70b9\u6570\u91cf\uff0c\u5220\u9664\u64cd\u4f5c\u9700\u5206\u4e3a\u4e09\u79cd\u60c5\u51b5\uff1a

    \u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5b50\u8282\u70b9\u6570\u91cf \\(= 0\\) \u65f6\uff0c\u8868\u793a\u5f85\u5220\u9664\u8282\u70b9\u662f\u53f6\u8282\u70b9\uff0c\u53ef\u4ee5\u76f4\u63a5\u5220\u9664\u3002

    Fig. \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 0\uff09

    \u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5b50\u8282\u70b9\u6570\u91cf \\(= 1\\) \u65f6\uff0c\u5c06\u5f85\u5220\u9664\u8282\u70b9\u66ff\u6362\u4e3a\u5176\u5b50\u8282\u70b9\u5373\u53ef\u3002

    Fig. \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 1\uff09

    \u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5b50\u8282\u70b9\u6570\u91cf \\(= 2\\) \u65f6\uff0c\u5220\u9664\u64cd\u4f5c\u5206\u4e3a\u4e09\u6b65\uff1a

    1. \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\u5728\u201c\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u201d\u4e2d\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\uff0c\u8bb0\u4e3a tmp \uff1b
    2. \u5728\u6811\u4e2d\u9012\u5f52\u5220\u9664\u8282\u70b9 tmp \uff1b
    3. \u7528 tmp \u7684\u503c\u8986\u76d6\u5f85\u5220\u9664\u8282\u70b9\u7684\u503c\uff1b
    <1><2><3><4>

    \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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart binary_search_tree.java
    /* \u5220\u9664\u8282\u70b9 */\nvoid remove(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root == null)\nreturn;\nTreeNode cur = root, pre = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur.val == num)\nbreak;\npre = cur;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num)\ncur = cur.right;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse\ncur = cur.left;\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur == null)\nreturn;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif (cur.left == null || cur.right == null) {\n// \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\nTreeNode child = cur.left != null ? cur.left : cur.right;\n// \u5220\u9664\u8282\u70b9 cur\nif (cur != root) {\nif (pre.left == cur)\npre.left = child;\nelse\npre.right = child;\n} else {\n// \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nroot = child;\n}\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 2\nelse {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\nTreeNode tmp = cur.right;\nwhile (tmp.left != null) {\ntmp = tmp.left;\n}\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nremove(tmp.val);\n// \u7528 tmp \u8986\u76d6 cur\ncur.val = tmp.val;\n}\n}\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\nif (root == nullptr)\nreturn;\nTreeNode *cur = root, *pre = nullptr;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != nullptr) {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur->val == num)\nbreak;\npre = cur;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur->val < num)\ncur = cur->right;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse\ncur = cur->left;\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur == nullptr)\nreturn;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif (cur->left == nullptr || cur->right == nullptr) {\n// \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u8282\u70b9\nTreeNode *child = cur->left != nullptr ? cur->left : cur->right;\n// \u5220\u9664\u8282\u70b9 cur\nif (cur != root) {\nif (pre->left == cur)\npre->left = child;\nelse\npre->right = child;\n} else {\n// \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nroot = child;\n}\n// \u91ca\u653e\u5185\u5b58\ndelete cur;\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 2\nelse {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\nTreeNode *tmp = cur->right;\nwhile (tmp->left != nullptr) {\ntmp = tmp->left;\n}\nint tmpVal = tmp->val;\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nremove(tmp->val);\n// \u7528 tmp \u8986\u76d6 cur\ncur->val = tmpVal;\n}\n}\n
    binary_search_tree.py
    def remove(self, num: int) -> None:\n\"\"\"\u5220\u9664\u8282\u70b9\"\"\"\n# \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif self.root is None:\nreturn\n# \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\ncur, pre = self.root, None\nwhile cur is not None:\n# \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif cur.val == num:\nbreak\npre = cur\n# \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif cur.val < num:\ncur = cur.right\n# \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse:\ncur = cur.left\n# \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif cur is None:\nreturn\n# \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif 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\nchild = cur.left or cur.right\n# \u5220\u9664\u8282\u70b9 cur\nif cur != self.root:\nif pre.left == cur:\npre.left = child\nelse:\npre.right = child\nelse:\n# \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nself.__root = cur\n# \u5b50\u8282\u70b9\u6570\u91cf = 2\nelse:\n# \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\ntmp: TreeNode = cur.right\nwhile tmp.left is not None:\ntmp = tmp.left\n# \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nself.remove(tmp.val)\n# \u7528 tmp \u8986\u76d6 cur\ncur.val = tmp.val\n
    binary_search_tree.go
    /* \u5220\u9664\u8282\u70b9 */\nfunc (bst *binarySearchTree) remove(num int) {\ncur := bst.root\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif cur == nil {\nreturn\n}\n// \u5f85\u5220\u9664\u8282\u70b9\u4e4b\u524d\u7684\u8282\u70b9\u4f4d\u7f6e\nvar pre *TreeNode = nil\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nfor cur != nil {\nif cur.Val == num {\nbreak\n}\npre = cur\nif cur.Val < num {\n// \u5f85\u5220\u9664\u8282\u70b9\u5728\u53f3\u5b50\u6811\u4e2d\ncur = cur.Right\n} else {\n// \u5f85\u5220\u9664\u8282\u70b9\u5728\u5de6\u5b50\u6811\u4e2d\ncur = cur.Left\n}\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif cur == nil {\nreturn\n}\n// \u5b50\u8282\u70b9\u6570\u4e3a 0 \u6216 1\nif cur.Left == nil || cur.Right == nil {\nvar child *TreeNode = nil\n// \u53d6\u51fa\u5f85\u5220\u9664\u8282\u70b9\u7684\u5b50\u8282\u70b9\nif cur.Left != nil {\nchild = cur.Left\n} else {\nchild = cur.Right\n}\n// \u5220\u9664\u8282\u70b9 cur\nif cur != bst.root {\nif pre.Left == cur {\npre.Left = child\n} else {\npre.Right = child\n}\n} else {\n// \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nbst.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\ntmp := cur.Right\nfor tmp.Left != nil {\ntmp = tmp.Left\n}\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nbst.remove(tmp.Val)\n// \u7528 tmp \u8986\u76d6 cur\ncur.Val = tmp.Val\n}\n}\n
    binary_search_tree.js
    /* \u5220\u9664\u8282\u70b9 */\nfunction remove(num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root === null) return;\nlet cur = root,\npre = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur !== null) {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur.val === num) break;\npre = cur;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num) cur = cur.right;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse cur = cur.left;\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur === null) return;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif (cur.left === null || cur.right === null) {\n// \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\nlet child = cur.left !== null ? cur.left : cur.right;\n// \u5220\u9664\u8282\u70b9 cur\nif (cur != root) {\nif (pre.left === cur) pre.left = child;\nelse pre.right = child;\n} else {\n// \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nroot = child;\n}\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 2\nelse {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\nlet tmp = cur.right;\nwhile (tmp.left !== null) {\ntmp = tmp.left;\n}\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nremove(tmp.val);\n// \u7528 tmp \u8986\u76d6 cur\ncur.val = tmp.val;\n}\n}\n
    binary_search_tree.ts
    /* \u5220\u9664\u8282\u70b9 */\nfunction remove(num: number): void {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root === null) {\nreturn;\n}\nlet cur = root,\npre: TreeNode | null = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur !== null) {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur.val === num) {\nbreak;\n}\npre = cur;\nif (cur.val < num) {\ncur = cur.right as TreeNode; // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n} else {\ncur = cur.left as TreeNode; // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n}\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur === null) {\nreturn;\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif (cur.left === null || cur.right === null) {\n// \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\nlet child = cur.left !== null ? cur.left : cur.right;\n// \u5220\u9664\u8282\u70b9 cur\nif (cur != root) {\nif (pre!.left === cur) {\npre!.left = child;\n} else {\npre!.right = child;\n}\n} else {\n// \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nroot = child;\n}\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 2\nelse {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\nlet tmp = cur.right;\nwhile (tmp.left !== null) {\ntmp = tmp.left;\n}\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nremove(tmp!.val);\n// \u7528 tmp \u8986\u76d6 cur\ncur.val = tmp.val;\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 removeNode(binarySearchTree *bst, int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (bst->root == NULL)\nreturn;\nTreeNode *cur = bst->root, *pre = NULL;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != NULL) {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur->val == num)\nbreak;\npre = cur;\nif (cur->val < num) {\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 root \u7684\u53f3\u5b50\u6811\u4e2d\ncur = cur->right;\n} else {\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 root \u7684\u5de6\u5b50\u6811\u4e2d\ncur = cur->left;\n}\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur == NULL)\nreturn;\n// \u5224\u65ad\u5f85\u5220\u9664\u8282\u70b9\u662f\u5426\u5b58\u5728\u5b50\u8282\u70b9\nif (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\nTreeNode *child = cur->left != NULL ? cur->left : cur->right;\n// \u5220\u9664\u8282\u70b9 cur\nif (pre->left == cur) {\npre->left = child;\n} else {\npre->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\nTreeNode *tmp = cur->right;\nwhile (tmp->left != NULL) {\ntmp = tmp->left;\n}\nint tmpVal = tmp->val;\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nremoveNode(bst, tmp->val);\n// \u7528 tmp \u8986\u76d6 cur\ncur->val = tmpVal;\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\nif (root == null)\nreturn;\nTreeNode? cur = root, pre = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur.val == num)\nbreak;\npre = cur;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num)\ncur = cur.right;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse\ncur = cur.left;\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur == null || pre == null)\nreturn;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif (cur.left == null || cur.right == null) {\n// \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\nTreeNode? child = cur.left != null ? cur.left : cur.right;\n// \u5220\u9664\u8282\u70b9 cur\nif (cur != root) {\nif (pre.left == cur)\npre.left = child;\nelse\npre.right = child;\n} else {\n// \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nroot = child;\n}\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 2\nelse {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\nTreeNode? tmp = cur.right;\nwhile (tmp.left != null) {\ntmp = tmp.left;\n}\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nremove(tmp.val);\n// \u7528 tmp \u8986\u76d6 cur\ncur.val = tmp.val;\n}\n}\n
    binary_search_tree.swift
    /* \u5220\u9664\u8282\u70b9 */\n@discardableResult\nfunc remove(num: Int) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif root == nil {\nreturn\n}\nvar cur = root\nvar pre: TreeNode?\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile cur != nil {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif cur!.val == num {\nbreak\n}\npre = cur\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif cur!.val < num {\ncur = cur?.right\n}\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse {\ncur = cur?.left\n}\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif cur == nil {\nreturn\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif cur?.left == nil || cur?.right == nil {\n// \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\nlet child = cur?.left != nil ? cur?.left : cur?.right\n// \u5220\u9664\u8282\u70b9 cur\nif cur != root {\nif pre?.left === cur {\npre?.left = child\n} else {\npre?.right = child\n}\n} else {\n// \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nroot = cur;\n}\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 2\nelse {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\nvar tmp = cur?.right\nwhile tmp?.left != nil {\ntmp = tmp?.left\n}\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nremove(num: tmp!.val)\n// \u7528 tmp \u8986\u76d6 cur\ncur?.val = tmp!.val\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\nif (self.root == null) return;\nvar cur = self.root;\nvar pre: ?*inc.TreeNode(T) = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur.?.val == num) break;\npre = cur;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.?.val < num) {\ncur = cur.?.right;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n} else {\ncur = cur.?.left;\n}\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur == null) return;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif (cur.?.left == null or cur.?.right == null) {\n// \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\nvar child = if (cur.?.left != null) cur.?.left else cur.?.right;\n// \u5220\u9664\u8282\u70b9 cur\nif (pre.?.left == cur) {\npre.?.left = child;\n} else {\npre.?.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\nvar tmp = cur.?.right;\nwhile (tmp.?.left != null) {\ntmp = tmp.?.left;\n}\nvar tmpVal = tmp.?.val;\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n_ = self.remove(tmp.?.val);\n// \u7528 tmp \u8986\u76d6 cur\ncur.?.val = tmpVal;\n}\n}\n
    binary_search_tree.dart
    [class]{BinarySearchTree}-[func]{remove}\n
    "},{"location":"chapter_tree/binary_search_tree/#_4","title":"\u6392\u5e8f","text":"

    \u6211\u4eec\u77e5\u9053\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\u56e0\u6b64\uff0c\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\u9700\u989d\u5916\u6392\u5e8f\uff0c\u975e\u5e38\u9ad8\u6548\u3002

    Fig. \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\u53ef\u77e5\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\u8868\u73b0\u3002\u53ea\u6709\u5728\u9ad8\u9891\u6dfb\u52a0\u3001\u4f4e\u9891\u67e5\u627e\u5220\u9664\u7684\u6570\u636e\u9002\u7528\u573a\u666f\u4e0b\uff0c\u6570\u7ec4\u6bd4\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6548\u7387\u66f4\u9ad8\u3002

    \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\u94fe\u8868\uff0c\u8fd9\u65f6\u5404\u79cd\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u4f1a\u9000\u5316\u4e3a \\(O(n)\\) \u3002

    Fig. \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u5e73\u8861\u4e0e\u9000\u5316

    "},{"location":"chapter_tree/binary_search_tree/#743","title":"7.4.3. \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u5e38\u89c1\u5e94\u7528","text":"
    • \u7528\u4f5c\u7cfb\u7edf\u4e2d\u7684\u591a\u7ea7\u7d22\u5f15\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684\u67e5\u627e\u3001\u63d2\u5165\u3001\u5220\u9664\u64cd\u4f5c\u3002
    • \u4f5c\u4e3a\u67d0\u4e9b\u641c\u7d22\u7b97\u6cd5\u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002
    • \u7528\u4e8e\u5b58\u50a8\u6570\u636e\u6d41\uff0c\u4ee5\u4fdd\u6301\u5176\u6709\u5e8f\u72b6\u6001\u3002
    "},{"location":"chapter_tree/binary_tree/","title":"7.1. \u00a0 \u4e8c\u53c9\u6811","text":"

    \u300c\u4e8c\u53c9\u6811 Binary Tree\u300d\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u4ee3\u8868\u7740\u7956\u5148\u4e0e\u540e\u4ee3\u4e4b\u95f4\u7684\u6d3e\u751f\u5173\u7cfb\uff0c\u4f53\u73b0\u7740\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\u4e00\u4e2a\u300c\u503c\u300d\u548c\u4e24\u4e2a\u300c\u6307\u9488\u300d\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart
    /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nint val;         // \u8282\u70b9\u503c\nTreeNode left;   // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nTreeNode right;  // \u53f3\u5b50\u8282\u70b9\u6307\u9488\nTreeNode(int x) { val = x; }\n}\n
    /* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\nint val;          // \u8282\u70b9\u503c\nTreeNode *left;   // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nTreeNode *right;  // \u53f3\u5b50\u8282\u70b9\u6307\u9488\nTreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n};\n
    class TreeNode:\n\"\"\"\u4e8c\u53c9\u6811\u8282\u70b9\u7c7b\"\"\"\ndef __init__(self, val: int):\nself.val: int = val                   # \u8282\u70b9\u503c\nself.left: Optional[TreeNode] = None  # \u5de6\u5b50\u8282\u70b9\u6307\u9488\nself.right: Optional[TreeNode] = None # \u53f3\u5b50\u8282\u70b9\u6307\u9488\n
    /* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype TreeNode struct {\nVal   int\nLeft  *TreeNode\nRight *TreeNode\n}\n/* \u8282\u70b9\u521d\u59cb\u5316\u65b9\u6cd5 */\nfunc NewTreeNode(v int) *TreeNode {\nreturn &TreeNode{\nLeft:  nil,\nRight: nil,\nVal:   v,\n}\n}\n
    /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nfunction TreeNode(val, left, right) {\nthis.val = (val === undefined ? 0 : val); // \u8282\u70b9\u503c\nthis.left = (left === undefined ? null : left); // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nthis.right = (right === undefined ? null : right); // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n}\n
    /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nval: number;\nleft: TreeNode | null;\nright: TreeNode | null;\nconstructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {\nthis.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\nthis.left = left === undefined ? null : left; // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nthis.right = right === undefined ? null : right; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n}\n}\n
    /* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\nint val;                // \u8282\u70b9\u503c\nint height;             // \u8282\u70b9\u9ad8\u5ea6\nstruct TreeNode *left;  // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nstruct TreeNode *right; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n};\ntypedef struct TreeNode TreeNode;\n/* \u6784\u9020\u51fd\u6570 */\nTreeNode *newTreeNode(int val) {\nTreeNode *node;\nnode = (TreeNode *)malloc(sizeof(TreeNode));\nnode->val = val;\nnode->height = 0;\nnode->left = NULL;\nnode->right = NULL;\nreturn node;\n}\n
    /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nint val;          // \u8282\u70b9\u503c\nTreeNode? left;   // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nTreeNode? right;  // \u53f3\u5b50\u8282\u70b9\u6307\u9488\nTreeNode(int x) { val = x; }\n}\n
    /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nvar val: Int // \u8282\u70b9\u503c\nvar left: TreeNode? // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nvar right: TreeNode? // \u53f3\u5b50\u8282\u70b9\u6307\u9488\ninit(x: Int) {\nval = x\n}\n}\n
    \n
    /* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nint val;         // \u8282\u70b9\u503c\nTreeNode? left;  // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nTreeNode? right; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\nTreeNode(this.val, [this.left, this.right]);\n}\n

    \u8282\u70b9\u7684\u4e24\u4e2a\u6307\u9488\u5206\u522b\u6307\u5411\u300c\u5de6\u5b50\u8282\u70b9\u300d\u548c\u300c\u53f3\u5b50\u8282\u70b9\u300d\uff0c\u540c\u65f6\u8be5\u8282\u70b9\u88ab\u79f0\u4e3a\u8fd9\u4e24\u4e2a\u5b50\u8282\u70b9\u7684\u300c\u7236\u8282\u70b9\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\u300d\uff0c\u540c\u7406\u53ef\u5f97\u300c\u53f3\u5b50\u6811\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\u4f8b\u5982\uff0c\u5728\u4ee5\u4e0b\u793a\u4f8b\u4e2d\uff0c\u82e5\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

    Fig. \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\u6d89\u53ca\u7684\u672f\u8bed\u8f83\u591a\uff0c\u5efa\u8bae\u5c3d\u91cf\u7406\u89e3\u5e76\u8bb0\u4f4f\u3002

    • \u300c\u6839\u8282\u70b9 Root Node\u300d\uff1a\u4f4d\u4e8e\u4e8c\u53c9\u6811\u9876\u5c42\u7684\u8282\u70b9\uff0c\u6ca1\u6709\u7236\u8282\u70b9\uff1b
    • \u300c\u53f6\u8282\u70b9 Leaf Node\u300d\uff1a\u6ca1\u6709\u5b50\u8282\u70b9\u7684\u8282\u70b9\uff0c\u5176\u4e24\u4e2a\u6307\u9488\u5747\u6307\u5411 \\(\\text{null}\\) \uff1b
    • \u8282\u70b9\u7684\u300c\u5c42 Level\u300d\uff1a\u4ece\u9876\u81f3\u5e95\u9012\u589e\uff0c\u6839\u8282\u70b9\u6240\u5728\u5c42\u4e3a 1 \uff1b
    • \u8282\u70b9\u7684\u300c\u5ea6 Degree\u300d\uff1a\u8282\u70b9\u7684\u5b50\u8282\u70b9\u7684\u6570\u91cf\u3002\u5728\u4e8c\u53c9\u6811\u4e2d\uff0c\u5ea6\u7684\u8303\u56f4\u662f 0, 1, 2 \uff1b
    • \u300c\u8fb9 Edge\u300d\uff1a\u8fde\u63a5\u4e24\u4e2a\u8282\u70b9\u7684\u7ebf\u6bb5\uff0c\u5373\u8282\u70b9\u6307\u9488\uff1b
    • \u4e8c\u53c9\u6811\u7684\u300c\u9ad8\u5ea6\u300d\uff1a\u4ece\u6839\u8282\u70b9\u5230\u6700\u8fdc\u53f6\u8282\u70b9\u6240\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\uff1b
    • \u8282\u70b9\u7684\u300c\u6df1\u5ea6 Depth\u300d \uff1a\u4ece\u6839\u8282\u70b9\u5230\u8be5\u8282\u70b9\u6240\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\uff1b
    • \u8282\u70b9\u7684\u300c\u9ad8\u5ea6 Height\u300d\uff1a\u4ece\u6700\u8fdc\u53f6\u8282\u70b9\u5230\u8be5\u8282\u70b9\u6240\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\uff1b

    Fig. \u4e8c\u53c9\u6811\u7684\u5e38\u7528\u672f\u8bed

    \u9ad8\u5ea6\u4e0e\u6df1\u5ea6\u7684\u5b9a\u4e49

    \u8bf7\u6ce8\u610f\uff0c\u6211\u4eec\u901a\u5e38\u5c06\u300c\u9ad8\u5ea6\u300d\u548c\u300c\u6df1\u5ea6\u300d\u5b9a\u4e49\u4e3a\u201c\u8d70\u8fc7\u8fb9\u7684\u6570\u91cf\u201d\uff0c\u4f46\u6709\u4e9b\u9898\u76ee\u6216\u6559\u6750\u53ef\u80fd\u4f1a\u5c06\u5176\u5b9a\u4e49\u4e3a\u201c\u8d70\u8fc7\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":"

    \u521d\u59cb\u5316\u4e8c\u53c9\u6811\u3002\u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u9996\u5148\u521d\u59cb\u5316\u8282\u70b9\uff0c\u7136\u540e\u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\u3002

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\u5f15\u7528\u6307\u5411\uff08\u5373\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\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n
    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\u5f15\u7528\u6307\u5411\uff08\u5373\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\u5f15\u7528\u6307\u5411\uff08\u5373\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),\nn2 = new TreeNode(2),\nn3 = new TreeNode(3),\nn4 = new TreeNode(4),\nn5 = new TreeNode(5);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\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),\nn2 = new TreeNode(2),\nn3 = new TreeNode(3),\nn4 = new TreeNode(4),\nn5 = new TreeNode(5);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = 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\u5f15\u7528\u6307\u5411\uff08\u5373\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 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\u5f15\u7528\u6307\u5411\uff08\u5373\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\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
    binary_tree.zig
    \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\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n

    \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\u3002\u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u901a\u8fc7\u4fee\u6539\u6307\u9488\u6765\u5b9e\u73b0\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\u3002

    Fig. \u5728\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart binary_tree.java
    TreeNode 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.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.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.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.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.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.cs
    /* \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.swift
    let 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.zig
    \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

    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\u64cd\u4f5c\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","title":"\u5b8c\u7f8e\u4e8c\u53c9\u6811","text":"

    \u300c\u5b8c\u7f8e\u4e8c\u53c9\u6811 Perfect Binary Tree\u300d\u9664\u4e86\u6700\u5e95\u5c42\u5916\uff0c\u5176\u4f59\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\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

    \u5728\u4e2d\u6587\u793e\u533a\u4e2d\uff0c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u5e38\u88ab\u79f0\u4e3a\u300c\u6ee1\u4e8c\u53c9\u6811\u300d\uff0c\u8bf7\u6ce8\u610f\u533a\u5206\u3002

    Fig. \u5b8c\u7f8e\u4e8c\u53c9\u6811

    "},{"location":"chapter_tree/binary_tree/#_2","title":"\u5b8c\u5168\u4e8c\u53c9\u6811","text":"

    \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

    Fig. \u5b8c\u5168\u4e8c\u53c9\u6811

    "},{"location":"chapter_tree/binary_tree/#_3","title":"\u5b8c\u6ee1\u4e8c\u53c9\u6811","text":"

    \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

    Fig. \u5b8c\u6ee1\u4e8c\u53c9\u6811

    "},{"location":"chapter_tree/binary_tree/#_4","title":"\u5e73\u8861\u4e8c\u53c9\u6811","text":"

    \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

    Fig. \u5e73\u8861\u4e8c\u53c9\u6811

    "},{"location":"chapter_tree/binary_tree/#714","title":"7.1.4. \u00a0 \u4e8c\u53c9\u6811\u7684\u9000\u5316","text":"

    \u5f53\u4e8c\u53c9\u6811\u7684\u6bcf\u5c42\u8282\u70b9\u90fd\u88ab\u586b\u6ee1\u65f6\uff0c\u8fbe\u5230\u300c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u300d\uff1b\u800c\u5f53\u6240\u6709\u8282\u70b9\u90fd\u504f\u5411\u4e00\u4fa7\u65f6\uff0c\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u300c\u94fe\u8868\u300d\u3002

    • \u5b8c\u7f8e\u4e8c\u53c9\u6811\u662f\u7406\u60f3\u60c5\u51b5\uff0c\u53ef\u4ee5\u5145\u5206\u53d1\u6325\u4e8c\u53c9\u6811\u201c\u5206\u6cbb\u201d\u7684\u4f18\u52bf\uff1b
    • \u94fe\u8868\u5219\u662f\u53e6\u4e00\u4e2a\u6781\u7aef\uff0c\u5404\u9879\u64cd\u4f5c\u90fd\u53d8\u4e3a\u7ebf\u6027\u64cd\u4f5c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u9000\u5316\u81f3 \\(O(n)\\) \uff1b

    Fig. \u4e8c\u53c9\u6811\u7684\u6700\u4f73\u4e0e\u6700\u5dee\u7ed3\u6784

    \u5982\u4e0b\u8868\u6240\u793a\uff0c\u5728\u6700\u4f73\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\u6216\u6781\u5c0f\u503c\u3002

    \u5b8c\u7f8e\u4e8c\u53c9\u6811 \u94fe\u8868 \u7b2c \\(i\\) \u5c42\u7684\u8282\u70b9\u6570\u91cf \\(2^{i-1}\\) \\(1\\) \u6811\u7684\u9ad8\u5ea6\u4e3a \\(h\\) \u65f6\u7684\u53f6\u8282\u70b9\u6570\u91cf \\(2^h\\) \\(1\\) \u6811\u7684\u9ad8\u5ea6\u4e3a \\(h\\) \u65f6\u7684\u8282\u70b9\u603b\u6570 \\(2^{h+1} - 1\\) \\(h + 1\\) \u6811\u7684\u8282\u70b9\u603b\u6570\u4e3a \\(n\\) \u65f6\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":"

    \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\u641c\u7d22 Breadth-First Traversal\u300d\uff0c\u5b83\u4f53\u73b0\u4e86\u4e00\u79cd\u201c\u4e00\u5708\u4e00\u5708\u5411\u5916\u6269\u5c55\u201d\u7684\u9010\u5c42\u641c\u7d22\u65b9\u5f0f\u3002

    Fig. \u4e8c\u53c9\u6811\u7684\u5c42\u5e8f\u904d\u5386

    "},{"location":"chapter_tree/binary_tree_traversal/#_1","title":"\u7b97\u6cd5\u5b9e\u73b0","text":"

    \u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u901a\u5e38\u501f\u52a9\u300c\u961f\u5217\u300d\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

    JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart 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\nQueue<TreeNode> queue = new LinkedList<>() {{ add(root); }};\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nList<Integer> list = new ArrayList<>();\nwhile (!queue.isEmpty()) {\nTreeNode node = queue.poll(); // \u961f\u5217\u51fa\u961f\nlist.add(node.val);           // \u4fdd\u5b58\u8282\u70b9\u503c\nif (node.left != null)\nqueue.offer(node.left);   // \u5de6\u5b50\u8282\u70b9\u5165\u961f\nif (node.right != null)\nqueue.offer(node.right);  // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n}\nreturn list;\n}\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\nqueue<TreeNode *> queue;\nqueue.push(root);\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nvector<int> vec;\nwhile (!queue.empty()) {\nTreeNode *node = queue.front();\nqueue.pop();              // \u961f\u5217\u51fa\u961f\nvec.push_back(node->val); // \u4fdd\u5b58\u8282\u70b9\u503c\nif (node->left != nullptr)\nqueue.push(node->left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\nif (node->right != nullptr)\nqueue.push(node->right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n}\nreturn vec;\n}\n
    binary_tree_bfs.py
    def level_order(root: TreeNode | None) -> list[int]:\n\"\"\"\u5c42\u5e8f\u904d\u5386\"\"\"\n# \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\nqueue: deque[TreeNode] = deque()\nqueue.append(root)\n# \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nres = []\nwhile queue:\nnode: TreeNode = queue.popleft()  # \u961f\u5217\u51fa\u961f\nres.append(node.val)  # \u4fdd\u5b58\u8282\u70b9\u503c\nif node.left is not None:\nqueue.append(node.left)  # \u5de6\u5b50\u8282\u70b9\u5165\u961f\nif node.right is not None:\nqueue.append(node.right)  # \u53f3\u5b50\u8282\u70b9\u5165\u961f\nreturn res\n
    binary_tree_bfs.go
    /* \u5c42\u5e8f\u904d\u5386 */\nfunc levelOrder(root *TreeNode) []int {\n// \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\nqueue := list.New()\nqueue.PushBack(root)\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5207\u7247\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nnums := make([]int, 0)\nfor queue.Len() > 0 {\n// \u961f\u5217\u51fa\u961f\nnode := queue.Remove(queue.Front()).(*TreeNode)\n// \u4fdd\u5b58\u8282\u70b9\u503c\nnums = append(nums, node.Val)\nif node.Left != nil {\n// \u5de6\u5b50\u8282\u70b9\u5165\u961f\nqueue.PushBack(node.Left)\n}\nif node.Right != nil {\n// \u53f3\u5b50\u8282\u70b9\u5165\u961f\nqueue.PushBack(node.Right)\n}\n}\nreturn nums\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\nconst queue = [root];\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nconst list = [];\nwhile (queue.length) {\nlet node = queue.shift(); // \u961f\u5217\u51fa\u961f\nlist.push(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\nif (node.left) queue.push(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\nif (node.right) queue.push(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n}\nreturn 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\nconst queue = [root];\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nconst list: number[] = [];\nwhile (queue.length) {\nlet node = queue.shift() as TreeNode; // \u961f\u5217\u51fa\u961f\nlist.push(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\nif (node.left) {\nqueue.push(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n}\nif (node.right) {\nqueue.push(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n}\n}\nreturn list;\n}\n
    binary_tree_bfs.c
    /* \u5c42\u5e8f\u904d\u5386 */\nint *levelOrder(TreeNode *root, int *size) {\n/* \u8f85\u52a9\u961f\u5217 */\nint front, rear;\nint index, *arr;\nTreeNode *node;\nTreeNode **queue;\n/* \u8f85\u52a9\u961f\u5217 */\nqueue = (TreeNode **)malloc(sizeof(TreeNode *) * MAX_NODE_SIZE);\n// \u961f\u5217\u6307\u9488\nfront = 0, rear = 0;\n// \u52a0\u5165\u6839\u8282\u70b9\nqueue[rear++] = root;\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n/* \u8f85\u52a9\u6570\u7ec4 */\narr = (int *)malloc(sizeof(int) * MAX_NODE_SIZE);\n// \u6570\u7ec4\u6307\u9488\nindex = 0;\nwhile (front < rear) {\n// \u961f\u5217\u51fa\u961f\nnode = queue[front++];\n// \u4fdd\u5b58\u8282\u70b9\u503c\narr[index++] = node->val;\nif (node->left != NULL) {\n// \u5de6\u5b50\u8282\u70b9\u5165\u961f\nqueue[rear++] = node->left;\n}\nif (node->right != NULL) {\n// \u53f3\u5b50\u8282\u70b9\u5165\u961f\nqueue[rear++] = node->right;\n}\n}\n// \u66f4\u65b0\u6570\u7ec4\u957f\u5ea6\u7684\u503c\n*size = index;\narr = realloc(arr, sizeof(int) * (*size));\n// \u91ca\u653e\u8f85\u52a9\u6570\u7ec4\u7a7a\u95f4\nfree(queue);\nreturn arr;\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\nQueue<TreeNode> queue = new();\nqueue.Enqueue(root);\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nList<int> list = new();\nwhile (queue.Count != 0) {\nTreeNode node = queue.Dequeue(); // \u961f\u5217\u51fa\u961f\nlist.Add(node.val);              // \u4fdd\u5b58\u8282\u70b9\u503c\nif (node.left != null)\nqueue.Enqueue(node.left);    // \u5de6\u5b50\u8282\u70b9\u5165\u961f\nif (node.right != null)\nqueue.Enqueue(node.right);   // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n}\nreturn list;\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\nvar queue: [TreeNode] = [root]\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nvar list: [Int] = []\nwhile !queue.isEmpty {\nlet node = queue.removeFirst() // \u961f\u5217\u51fa\u961f\nlist.append(node.val) // \u4fdd\u5b58\u8282\u70b9\u503c\nif let left = node.left {\nqueue.append(left) // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n}\nif let right = node.right {\nqueue.append(right) // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n}\n}\nreturn list\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\nconst L = std.TailQueue(*inc.TreeNode(T));\nvar queue = L{};\nvar root_node = try mem_allocator.create(L.Node);\nroot_node.data = root;\nqueue.append(root_node); // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nvar list = std.ArrayList(T).init(std.heap.page_allocator);\nwhile (queue.len > 0) {\nvar queue_node = queue.popFirst().?;    // \u961f\u5217\u51fa\u961f\nvar node = queue_node.data;\ntry list.append(node.val);              // \u4fdd\u5b58\u8282\u70b9\u503c\nif (node.left != null) {\nvar tmp_node = try mem_allocator.create(L.Node);\ntmp_node.data = node.left.?;\nqueue.append(tmp_node);             // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n}\nif (node.right != null) {\nvar tmp_node = try mem_allocator.create(L.Node);\ntmp_node.data = node.right.?;\nqueue.append(tmp_node);             // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n}        }\nreturn 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\nQueue<TreeNode?> queue = Queue();\nqueue.add(root);\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nList<int> res = [];\nwhile (queue.isNotEmpty) {\nTreeNode? node = queue.removeFirst(); // \u961f\u5217\u51fa\u961f\nres.add(node!.val); // \u4fdd\u5b58\u8282\u70b9\u503c\nif (node.left != null) queue.add(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\nif (node.right != null) queue.add(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n}\nreturn res;\n}\n
    "},{"location":"chapter_tree/binary_tree_traversal/#_2","title":"\u590d\u6742\u5ea6\u5206\u6790","text":"

    \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u6240\u6709\u8282\u70b9\u88ab\u8bbf\u95ee\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff0c\u5176\u4e2d \\(n\\) \u4e3a\u8282\u70b9\u6570\u91cf\u3002

    \u7a7a\u95f4\u590d\u6742\u5ea6\uff1a\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u5373\u6ee1\u4e8c\u53c9\u6811\u65f6\uff0c\u904d\u5386\u5230\u6700\u5e95\u5c42\u4e4b\u524d\uff0c\u961f\u5217\u4e2d\u6700\u591a\u540c\u65f6\u5b58\u5728 \\(\\frac{n + 1}{2}\\) \u4e2a\u8282\u70b9\uff0c\u5360\u7528 \\(O(n)\\) \u7a7a\u95f4\u3002

    "},{"location":"chapter_tree/binary_tree_traversal/#722","title":"7.2.2. \u00a0 \u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386","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\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

    \u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5de6\u4fa7\u662f\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u793a\u610f\u56fe\uff0c\u53f3\u4e0a\u65b9\u662f\u5bf9\u5e94\u7684\u9012\u5f52\u5b9e\u73b0\u4ee3\u7801\u3002\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u5c31\u50cf\u662f\u7ed5\u7740\u6574\u4e2a\u4e8c\u53c9\u6811\u7684\u5916\u56f4\u201c\u8d70\u201d\u4e00\u5708\uff0c\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\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

    Fig. \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u524d\u3001\u4e2d\u3001\u540e\u5e8f\u904d\u5386

    \u4f4d\u7f6e \u542b\u4e49 \u6b64\u5904\u8bbf\u95ee\u8282\u70b9\u65f6\u5bf9\u5e94 \u6a59\u8272\u5706\u5708\u5904 \u521a\u8fdb\u5165\u6b64\u8282\u70b9\uff0c\u5373\u5c06\u8bbf\u95ee\u8be5\u8282\u70b9\u7684\u5de6\u5b50\u6811 \u524d\u5e8f\u904d\u5386 Pre-Order Traversal \u84dd\u8272\u5706\u5708\u5904 \u5df2\u8bbf\u95ee\u5b8c\u5de6\u5b50\u6811\uff0c\u5373\u5c06\u8bbf\u95ee\u53f3\u5b50\u6811 \u4e2d\u5e8f\u904d\u5386 In-Order Traversal \u7d2b\u8272\u5706\u5708\u5904 \u5df2\u8bbf\u95ee\u5b8c\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\uff0c\u5373\u5c06\u8fd4\u56de \u540e\u5e8f\u904d\u5386 Post-Order Traversal"},{"location":"chapter_tree/binary_tree_traversal/#_3","title":"\u7b97\u6cd5\u5b9e\u73b0","text":"JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZigDart binary_tree_dfs.java
    /* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode root) {\nif (root == null)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nlist.add(root.val);\npreOrder(root.left);\npreOrder(root.right);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode root) {\nif (root == null)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\ninOrder(root.left);\nlist.add(root.val);\ninOrder(root.right);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode root) {\nif (root == null)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\npostOrder(root.left);\npostOrder(root.right);\nlist.add(root.val);\n}\n
    binary_tree_dfs.cpp
    /* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode *root) {\nif (root == nullptr)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nvec.push_back(root->val);\npreOrder(root->left);\npreOrder(root->right);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode *root) {\nif (root == nullptr)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\ninOrder(root->left);\nvec.push_back(root->val);\ninOrder(root->right);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode *root) {\nif (root == nullptr)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\npostOrder(root->left);\npostOrder(root->right);\nvec.push_back(root->val);\n}\n
    binary_tree_dfs.py
    def pre_order(root: TreeNode | None) -> None:\n\"\"\"\u524d\u5e8f\u904d\u5386\"\"\"\nif root is None:\nreturn\n# \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nres.append(root.val)\npre_order(root=root.left)\npre_order(root=root.right)\ndef in_order(root: TreeNode | None) -> None:\n\"\"\"\u4e2d\u5e8f\u904d\u5386\"\"\"\nif root is None:\nreturn\n# \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\nin_order(root=root.left)\nres.append(root.val)\nin_order(root=root.right)\ndef post_order(root: TreeNode | None) -> None:\n\"\"\"\u540e\u5e8f\u904d\u5386\"\"\"\nif root is None:\nreturn\n# \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\npost_order(root=root.left)\npost_order(root=root.right)\nres.append(root.val)\n
    binary_tree_dfs.go
    /* \u524d\u5e8f\u904d\u5386 */\nfunc preOrder(node *TreeNode) {\nif node == nil {\nreturn\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nnums = append(nums, node.Val)\npreOrder(node.Left)\npreOrder(node.Right)\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc inOrder(node *TreeNode) {\nif node == nil {\nreturn\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\ninOrder(node.Left)\nnums = append(nums, node.Val)\ninOrder(node.Right)\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfunc postOrder(node *TreeNode) {\nif node == nil {\nreturn\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\npostOrder(node.Left)\npostOrder(node.Right)\nnums = append(nums, node.Val)\n}\n
    binary_tree_dfs.js
    /* \u524d\u5e8f\u904d\u5386 */\nfunction preOrder(root) {\nif (root === null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nlist.push(root.val);\npreOrder(root.left);\npreOrder(root.right);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunction inOrder(root) {\nif (root === null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\ninOrder(root.left);\nlist.push(root.val);\ninOrder(root.right);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfunction postOrder(root) {\nif (root === null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\npostOrder(root.left);\npostOrder(root.right);\nlist.push(root.val);\n}\n
    binary_tree_dfs.ts
    /* \u524d\u5e8f\u904d\u5386 */\nfunction preOrder(root: TreeNode | null): void {\nif (root === null) {\nreturn;\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nlist.push(root.val);\npreOrder(root.left);\npreOrder(root.right);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunction inOrder(root: TreeNode | null): void {\nif (root === null) {\nreturn;\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\ninOrder(root.left);\nlist.push(root.val);\ninOrder(root.right);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfunction postOrder(root: TreeNode | null): void {\nif (root === null) {\nreturn;\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\npostOrder(root.left);\npostOrder(root.right);\nlist.push(root.val);\n}\n
    binary_tree_dfs.c
    /* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode *root, int *size) {\nif (root == NULL)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\narr[(*size)++] = root->val;\npreOrder(root->left, size);\npreOrder(root->right, size);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode *root, int *size) {\nif (root == NULL)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\ninOrder(root->left, size);\narr[(*size)++] = root->val;\ninOrder(root->right, size);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode *root, int *size) {\nif (root == NULL)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\npostOrder(root->left, size);\npostOrder(root->right, size);\narr[(*size)++] = root->val;\n}\n
    binary_tree_dfs.cs
    /* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode? root) {\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nlist.Add(root.val);\npreOrder(root.left);\npreOrder(root.right);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode? root) {\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\ninOrder(root.left);\nlist.Add(root.val);\ninOrder(root.right);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode? root) {\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\npostOrder(root.left);\npostOrder(root.right);\nlist.Add(root.val);\n}\n
    binary_tree_dfs.swift
    /* \u524d\u5e8f\u904d\u5386 */\nfunc preOrder(root: TreeNode?) {\nguard let root = root else {\nreturn\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nlist.append(root.val)\npreOrder(root: root.left)\npreOrder(root: root.right)\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc inOrder(root: TreeNode?) {\nguard let root = root else {\nreturn\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\ninOrder(root: root.left)\nlist.append(root.val)\ninOrder(root: root.right)\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfunc postOrder(root: TreeNode?) {\nguard let root = root else {\nreturn\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\npostOrder(root: root.left)\npostOrder(root: root.right)\nlist.append(root.val)\n}\n
    binary_tree_dfs.zig
    // \u524d\u5e8f\u904d\u5386\nfn preOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\ntry list.append(root.?.val);\ntry preOrder(T, root.?.left);\ntry preOrder(T, root.?.right);\n}\n// \u4e2d\u5e8f\u904d\u5386\nfn inOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\ntry inOrder(T, root.?.left);\ntry list.append(root.?.val);\ntry inOrder(T, root.?.right);\n}\n// \u540e\u5e8f\u904d\u5386\nfn postOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\ntry postOrder(T, root.?.left);\ntry postOrder(T, root.?.right);\ntry list.append(root.?.val);\n}\n
    binary_tree_dfs.dart
    /* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode? node) {\nif (node == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nlist.add(node.val);\npreOrder(node.left);\npreOrder(node.right);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode? node) {\nif (node == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\ninOrder(node.left);\nlist.add(node.val);\ninOrder(node.right);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode? node) {\nif (node == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\npostOrder(node.left);\npostOrder(node.right);\nlist.add(node.val);\n}\n

    Note

    \u6211\u4eec\u4e5f\u53ef\u4ee5\u4ec5\u57fa\u4e8e\u5faa\u73af\u5b9e\u73b0\u524d\u3001\u4e2d\u3001\u540e\u5e8f\u904d\u5386\uff0c\u6709\u5174\u8da3\u7684\u540c\u5b66\u53ef\u4ee5\u81ea\u884c\u5b9e\u73b0\u3002

    \u9012\u5f52\u8fc7\u7a0b\u53ef\u5206\u4e3a\u201c\u9012\u201d\u548c\u201c\u5f52\u201d\u4e24\u4e2a\u76f8\u53cd\u7684\u90e8\u5206\u3002\u201c\u9012\u201d\u8868\u793a\u5f00\u542f\u65b0\u65b9\u6cd5\uff0c\u7a0b\u5e8f\u5728\u6b64\u8fc7\u7a0b\u4e2d\u8bbf\u95ee\u4e0b\u4e00\u4e2a\u8282\u70b9\uff1b\u201c\u5f52\u201d\u8868\u793a\u51fd\u6570\u8fd4\u56de\uff0c\u4ee3\u8868\u8be5\u8282\u70b9\u5df2\u7ecf\u8bbf\u95ee\u5b8c\u6bd5\u3002\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u4e3a\u524d\u5e8f\u904d\u5386\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b\u3002

    <1><2><3><4><5><6><7><8><9><10><11>

    "},{"location":"chapter_tree/binary_tree_traversal/#_4","title":"\u590d\u6742\u5ea6\u5206\u6790","text":"

    \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u6240\u6709\u8282\u70b9\u88ab\u8bbf\u95ee\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff0c\u5176\u4e2d \\(n\\) \u4e3a\u8282\u70b9\u6570\u91cf\u3002

    \u7a7a\u95f4\u590d\u6742\u5ea6\uff1a\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u5373\u6811\u9000\u5316\u4e3a\u94fe\u8868\u65f6\uff0c\u9012\u5f52\u6df1\u5ea6\u8fbe\u5230 \\(n\\) \uff0c\u7cfb\u7edf\u5360\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002

    "},{"location":"chapter_tree/summary/","title":"7.6. \u00a0 \u5c0f\u7ed3","text":"
    • \u4e8c\u53c9\u6811\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u4f53\u73b0\u201c\u4e00\u5206\u4e3a\u4e8c\u201d\u7684\u5206\u6cbb\u903b\u8f91\u3002\u6bcf\u4e2a\u4e8c\u53c9\u6811\u8282\u70b9\u5305\u542b\u4e00\u4e2a\u503c\u4ee5\u53ca\u4e24\u4e2a\u6307\u9488\uff0c\u5206\u522b\u6307\u5411\u5176\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\u3002
    • \u5bf9\u4e8e\u4e8c\u53c9\u6811\u4e2d\u7684\u67d0\u4e2a\u8282\u70b9\uff0c\u5176\u5de6\uff08\u53f3\uff09\u5b50\u8282\u70b9\u53ca\u5176\u4ee5\u4e0b\u5f62\u6210\u7684\u6811\u88ab\u79f0\u4e3a\u8be5\u8282\u70b9\u7684\u5de6\uff08\u53f3\uff09\u5b50\u6811\u3002
    • \u4e8c\u53c9\u6811\u7684\u76f8\u5173\u672f\u8bed\u5305\u62ec\u6839\u8282\u70b9\u3001\u53f6\u8282\u70b9\u3001\u5c42\u3001\u5ea6\u3001\u8fb9\u3001\u9ad8\u5ea6\u548c\u6df1\u5ea6\u7b49\u3002
    • \u4e8c\u53c9\u6811\u7684\u521d\u59cb\u5316\u3001\u8282\u70b9\u63d2\u5165\u548c\u8282\u70b9\u5220\u9664\u64cd\u4f5c\u4e0e\u94fe\u8868\u64cd\u4f5c\u65b9\u6cd5\u7c7b\u4f3c\u3002
    • \u5e38\u89c1\u7684\u4e8c\u53c9\u6811\u7c7b\u578b\u6709\u5b8c\u7f8e\u4e8c\u53c9\u6811\u3001\u5b8c\u5168\u4e8c\u53c9\u6811\u3001\u6ee1\u4e8c\u53c9\u6811\u548c\u5e73\u8861\u4e8c\u53c9\u6811\u3002\u5b8c\u7f8e\u4e8c\u53c9\u6811\u662f\u6700\u7406\u60f3\u7684\u72b6\u6001\uff0c\u800c\u94fe\u8868\u662f\u9000\u5316\u540e\u7684\u6700\u5dee\u72b6\u6001\u3002
    • \u4e8c\u53c9\u6811\u53ef\u4ee5\u7528\u6570\u7ec4\u8868\u793a\uff0c\u65b9\u6cd5\u662f\u5c06\u8282\u70b9\u503c\u548c\u7a7a\u4f4d\u6309\u5c42\u5e8f\u904d\u5386\u987a\u5e8f\u6392\u5217\uff0c\u5e76\u6839\u636e\u7236\u8282\u70b9\u4e0e\u5b50\u8282\u70b9\u4e4b\u95f4\u7684\u7d22\u5f15\u6620\u5c04\u5173\u7cfb\u6765\u5b9e\u73b0\u6307\u9488\u3002
    • \u4e8c\u53c9\u6811\u7684\u5c42\u5e8f\u904d\u5386\u662f\u4e00\u79cd\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u65b9\u6cd5\uff0c\u5b83\u4f53\u73b0\u4e86\u201c\u4e00\u5708\u4e00\u5708\u5411\u5916\u201d\u7684\u5206\u5c42\u904d\u5386\u65b9\u5f0f\uff0c\u901a\u5e38\u901a\u8fc7\u961f\u5217\u6765\u5b9e\u73b0\u3002
    • \u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386\u7686\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff0c\u5b83\u4eec\u4f53\u73b0\u4e86\u201c\u8d70\u5230\u5c3d\u5934\uff0c\u518d\u56de\u5934\u7ee7\u7eed\u201d\u7684\u56de\u6eaf\u904d\u5386\u65b9\u5f0f\uff0c\u901a\u5e38\u4f7f\u7528\u9012\u5f52\u6765\u5b9e\u73b0\u3002
    • \u4e8c\u53c9\u641c\u7d22\u6811\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u5143\u7d20\u67e5\u627e\u6570\u636e\u7ed3\u6784\uff0c\u5176\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(\\log n)\\) \u3002\u5f53\u4e8c\u53c9\u641c\u7d22\u6811\u9000\u5316\u4e3a\u94fe\u8868\u65f6\uff0c\u5404\u9879\u65f6\u95f4\u590d\u6742\u5ea6\u4f1a\u52a3\u5316\u81f3 \\(O(n)\\) \u3002
    • AVL \u6811\uff0c\u4e5f\u79f0\u4e3a\u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811\uff0c\u5b83\u901a\u8fc7\u65cb\u8f6c\u64cd\u4f5c\uff0c\u786e\u4fdd\u5728\u4e0d\u65ad\u63d2\u5165\u548c\u5220\u9664\u8282\u70b9\u540e\uff0c\u6811\u4ecd\u7136\u4fdd\u6301\u5e73\u8861\u3002
    • AVL \u6811\u7684\u65cb\u8f6c\u64cd\u4f5c\u5305\u62ec\u53f3\u65cb\u3001\u5de6\u65cb\u3001\u5148\u53f3\u65cb\u518d\u5de6\u65cb\u3001\u5148\u5de6\u65cb\u518d\u53f3\u65cb\u3002\u5728\u63d2\u5165\u6216\u5220\u9664\u8282\u70b9\u540e\uff0cAVL \u6811\u4f1a\u4ece\u5e95\u5411\u9876\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\u3002
    "}]} \ No newline at end of file diff --git a/sitemap.xml b/sitemap.xml index 11868b592..1f921c2e3 100644 --- a/sitemap.xml +++ b/sitemap.xml @@ -2,382 +2,382 @@ https://www.hello-algo.com/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_appendix/contribution/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_appendix/installation/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_array_and_linkedlist/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_array_and_linkedlist/array/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_array_and_linkedlist/linked_list/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_array_and_linkedlist/list/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_array_and_linkedlist/summary/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_backtracking/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_backtracking/backtracking_algorithm/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_backtracking/n_queens_problem/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_backtracking/permutations_problem/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_computational_complexity/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_computational_complexity/performance_evaluation/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_computational_complexity/space_complexity/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_computational_complexity/summary/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_computational_complexity/time_complexity/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_data_structure/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_data_structure/basic_data_types/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_data_structure/character_encoding/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_data_structure/classification_of_data_structure/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_data_structure/number_encoding/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_data_structure/summary/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_graph/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_graph/graph/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_graph/graph_operations/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_graph/graph_traversal/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_graph/summary/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_hashing/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_hashing/hash_collision/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_hashing/hash_map/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_hashing/summary/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_heap/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_heap/build_heap/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_heap/heap/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_heap/summary/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_heap/top_k/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_introduction/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_introduction/algorithms_are_everywhere/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_introduction/summary/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_introduction/what_is_dsa/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_preface/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_preface/about_the_book/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_preface/suggestions/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_preface/summary/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_reference/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_searching/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_searching/binary_search/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_searching/binary_search_edge/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_searching/replace_linear_by_hashing/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_searching/searching_algorithm_revisited/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_searching/summary/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_sorting/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_sorting/bubble_sort/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_sorting/bucket_sort/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_sorting/counting_sort/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_sorting/heap_sort/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_sorting/insertion_sort/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_sorting/merge_sort/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_sorting/quick_sort/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_sorting/radix_sort/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_sorting/selection_sort/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_sorting/sorting_algorithm/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_sorting/summary/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_stack_and_queue/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_stack_and_queue/deque/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_stack_and_queue/queue/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_stack_and_queue/stack/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_stack_and_queue/summary/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_tree/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_tree/array_representation_of_tree/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_tree/avl_tree/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_tree/binary_search_tree/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_tree/binary_tree/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_tree/binary_tree_traversal/ - 2023-06-13 + 2023-06-14 daily https://www.hello-algo.com/chapter_tree/summary/ - 2023-06-13 + 2023-06-14 daily \ No newline at end of file diff --git a/sitemap.xml.gz b/sitemap.xml.gz index 38d42916a94d3e91087318d2495049d16e580de1..f681eeaa4d9862205f0a31f476519b401a3cf3e3 100644 GIT binary patch delta 241 zcmV;H*l0{?SqbY*Q}a4vXlYyj1o&63+741n+Z6q(!;XSbbB z+exywK0$kCWUSasFgQr$=j{uQH$R6x^wL2-1W*{}1B8%(_4~QEvm*vF-fiZu%hh~_ zPW#5Vb~FF;``6<8{C)efj=>W0ELC>2nRm>?&#F3|PD?UDj6Dpr^r5Xtcd0 r4YV&bK

    6.2.   哈希冲突