From 55931131dc5efec7fea16bc195eb572849067e0c Mon Sep 17 00:00:00 2001 From: krahets Date: Tue, 17 Oct 2023 20:29:42 +0800 Subject: [PATCH] deploy --- .../backtracking_algorithm/index.html | 13 +- .../iteration_and_recursion/index.html | 19 +- .../space_complexity/index.html | 54 ++--- chapter_hashing/hash_collision/index.html | 2 +- chapter_hashing/hash_map/index.html | 170 ++++++++------- .../hello_algo_mindmap.jpg | Bin 161136 -> 164629 bytes search/search_index.json | 2 +- sitemap.xml | 204 +++++++++--------- sitemap.xml.gz | Bin 981 -> 981 bytes 9 files changed, 250 insertions(+), 214 deletions(-) diff --git a/chapter_backtracking/backtracking_algorithm/index.html b/chapter_backtracking/backtracking_algorithm/index.html index 0bbed2339..197626f83 100644 --- a/chapter_backtracking/backtracking_algorithm/index.html +++ b/chapter_backtracking/backtracking_algorithm/index.html @@ -3545,7 +3545,18 @@
-
preorder_traversal_i_compact.c
[class]{}-[func]{preOrder}
+
preorder_traversal_i_compact.c
/* 前序遍历:例题一 */
+void preOrder(TreeNode *root) {
+    if (root == NULL) {
+        return;
+    }
+    if (root->val == 7) {
+        // 记录解
+        vectorPushback(res, root, sizeof(int));
+    }
+    preOrder(root->left);
+    preOrder(root->right);
+}
 
diff --git a/chapter_computational_complexity/iteration_and_recursion/index.html b/chapter_computational_complexity/iteration_and_recursion/index.html index 2ba268477..32c039c9d 100644 --- a/chapter_computational_complexity/iteration_and_recursion/index.html +++ b/chapter_computational_complexity/iteration_and_recursion/index.html @@ -4908,7 +4908,24 @@
-
recursion.c
[class]{}-[func]{forLoopRecur}
+
recursion.c
/* 使用迭代模拟递归 */
+int forLoopRecur(int n) {
+    int stack[1000]; // 借助一个大数组来模拟栈
+    int top = 0;
+    int res = 0;
+    // 递:递归调用
+    for (int i = n; i > 0; i--) {
+        // 通过“入栈操作”模拟“递”
+        stack[top++] = i;
+    }
+    // 归:返回结果
+    while (top >= 0) {
+        // 通过“出栈操作”模拟“归”
+        res += stack[top--];
+    }
+    // res = 1+2+3+...+n
+    return res;
+}
 
diff --git a/chapter_computational_complexity/space_complexity/index.html b/chapter_computational_complexity/space_complexity/index.html index d8c60a3ad..cb905c082 100644 --- a/chapter_computational_complexity/space_complexity/index.html +++ b/chapter_computational_complexity/space_complexity/index.html @@ -4366,31 +4366,35 @@ O(1) < O(\log n) < O(n) < O(n^2) < O(2^n) \newline
-
space_complexity.zig
[class]{}-[func]{function}
-
-// 常数阶
-fn constant(n: i32) void {
-    // 常量、变量、对象占用 O(1) 空间
-    const a: i32 = 0;
-    var b: i32 = 0;
-    var nums = [_]i32{0}**10000;
-    var node = inc.ListNode(i32){.val = 0};
-    var i: i32 = 0;
-    // 循环中的变量占用 O(1) 空间
-    while (i < n) : (i += 1) {
-        var c: i32 = 0;
-        _ = c;
-    }
-    // 循环中的函数占用 O(1) 空间
-    i = 0;
-    while (i < n) : (i += 1) {
-        _ = function();
-    }
-    _ = a;
-    _ = b;
-    _ = nums;
-    _ = node;
-}
+
space_complexity.zig
// 函数
+fn function() i32 {
+    // 执行某些操作
+    return 0;
+}
+
+// 常数阶
+fn constant(n: i32) void {
+    // 常量、变量、对象占用 O(1) 空间
+    const a: i32 = 0;
+    var b: i32 = 0;
+    var nums = [_]i32{0}**10000;
+    var node = inc.ListNode(i32){.val = 0};
+    var i: i32 = 0;
+    // 循环中的变量占用 O(1) 空间
+    while (i < n) : (i += 1) {
+        var c: i32 = 0;
+        _ = c;
+    }
+    // 循环中的函数占用 O(1) 空间
+    i = 0;
+    while (i < n) : (i += 1) {
+        _ = function();
+    }
+    _ = a;
+    _ = b;
+    _ = nums;
+    _ = node;
+}
 
diff --git a/chapter_hashing/hash_collision/index.html b/chapter_hashing/hash_collision/index.html index 28bad5d5a..3015f49fe 100644 --- a/chapter_hashing/hash_collision/index.html +++ b/chapter_hashing/hash_collision/index.html @@ -4521,7 +4521,7 @@
hash_map_chaining.c
/* 链表节点 */
 struct node {
     Pair *pair;
-    struct Node *next;
+    struct node *next;
 };
 
 typedef struct node Node;
diff --git a/chapter_hashing/hash_map/index.html b/chapter_hashing/hash_map/index.html
index 6285382f5..742c1c648 100644
--- a/chapter_hashing/hash_map/index.html
+++ b/chapter_hashing/hash_map/index.html
@@ -4261,94 +4261,98 @@
 
array_hash_map.swift
/* 键值对 */
-class Pair {
-    var key: Int
-    var val: String
+class Pair: Equatable {
+    public var key: Int
+    public var val: String
 
-    init(key: Int, val: String) {
+    public init(key: Int, val: String) {
         self.key = key
         self.val = val
     }
-}
-
-/* 基于数组简易实现的哈希表 */
-class ArrayHashMap {
-    private var buckets: [Pair?] = []
+
+    public static func == (lhs: Pair, rhs: Pair) -> Bool {
+        lhs.key == rhs.key && lhs.val == rhs.val
+    }
+}
 
-    init() {
-        // 初始化数组,包含 100 个桶
-        for _ in 0 ..< 100 {
-            buckets.append(nil)
-        }
-    }
-
-    /* 哈希函数 */
-    private func hashFunc(key: Int) -> Int {
-        let index = key % 100
-        return index
-    }
-
-    /* 查询操作 */
-    func get(key: Int) -> String? {
-        let index = hashFunc(key: key)
-        let pair = buckets[index]
-        return pair?.val
-    }
-
-    /* 添加操作 */
-    func put(key: Int, val: String) {
-        let pair = Pair(key: key, val: val)
-        let index = hashFunc(key: key)
-        buckets[index] = pair
-    }
-
-    /* 删除操作 */
-    func remove(key: Int) {
-        let index = hashFunc(key: key)
-        // 置为 nil ,代表删除
-        buckets[index] = nil
-    }
-
-    /* 获取所有键值对 */
-    func pairSet() -> [Pair] {
-        var pairSet: [Pair] = []
-        for pair in buckets {
-            if let pair = pair {
-                pairSet.append(pair)
-            }
-        }
-        return pairSet
-    }
-
-    /* 获取所有键 */
-    func keySet() -> [Int] {
-        var keySet: [Int] = []
-        for pair in buckets {
-            if let pair = pair {
-                keySet.append(pair.key)
-            }
-        }
-        return keySet
-    }
-
-    /* 获取所有值 */
-    func valueSet() -> [String] {
-        var valueSet: [String] = []
-        for pair in buckets {
-            if let pair = pair {
-                valueSet.append(pair.val)
-            }
-        }
-        return valueSet
-    }
-
-    /* 打印哈希表 */
-    func print() {
-        for pair in pairSet() {
-            Swift.print("\(pair.key) -> \(pair.val)")
-        }
-    }
-}
+/* 基于数组简易实现的哈希表 */
+class ArrayHashMap {
+    private var buckets: [Pair?] = []
+
+    init() {
+        // 初始化数组,包含 100 个桶
+        for _ in 0 ..< 100 {
+            buckets.append(nil)
+        }
+    }
+
+    /* 哈希函数 */
+    private func hashFunc(key: Int) -> Int {
+        let index = key % 100
+        return index
+    }
+
+    /* 查询操作 */
+    func get(key: Int) -> String? {
+        let index = hashFunc(key: key)
+        let pair = buckets[index]
+        return pair?.val
+    }
+
+    /* 添加操作 */
+    func put(key: Int, val: String) {
+        let pair = Pair(key: key, val: val)
+        let index = hashFunc(key: key)
+        buckets[index] = pair
+    }
+
+    /* 删除操作 */
+    func remove(key: Int) {
+        let index = hashFunc(key: key)
+        // 置为 nil ,代表删除
+        buckets[index] = nil
+    }
+
+    /* 获取所有键值对 */
+    func pairSet() -> [Pair] {
+        var pairSet: [Pair] = []
+        for pair in buckets {
+            if let pair = pair {
+                pairSet.append(pair)
+            }
+        }
+        return pairSet
+    }
+
+    /* 获取所有键 */
+    func keySet() -> [Int] {
+        var keySet: [Int] = []
+        for pair in buckets {
+            if let pair = pair {
+                keySet.append(pair.key)
+            }
+        }
+        return keySet
+    }
+
+    /* 获取所有值 */
+    func valueSet() -> [String] {
+        var valueSet: [String] = []
+        for pair in buckets {
+            if let pair = pair {
+                valueSet.append(pair.val)
+            }
+        }
+        return valueSet
+    }
+
+    /* 打印哈希表 */
+    func print() {
+        for pair in pairSet() {
+            Swift.print("\(pair.key) -> \(pair.val)")
+        }
+    }
+}
 
diff --git a/chapter_preface/about_the_book.assets/hello_algo_mindmap.jpg b/chapter_preface/about_the_book.assets/hello_algo_mindmap.jpg index 2c379e27c43b6d355de9caadd89e0fe35cab0bae..9b0727f47f1daa45f38f01c67c23fbcb42d87f71 100644 GIT binary patch literal 164629 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

Tt{HJH94(jJ4*6yJ66E5y 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 z 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 TS|ktc$ShE0dsl$v+vtA-peq;G literal 161136 zcmeFZ2UJtrwm%&8*cC;jHwEb=p-69v0#YL-^nefo(tB@5MUfh*p$RG_v=9lsE4?E% z^cH#(0-=|G&bjB@ckg)jym8<6y)nN3H^y6x!CrgrHRqap?YY*PYp&nQ+3?v6fKI{7 z+7bXzR^|m<0sKcgTLVx&b%)z}1I__{A(L4EfU}tkf-l|NoFu_u2Un1pg`+tF1b4It zdzm?b1wjH}fV7;KlNsC=;daj)VP)+g!}hbPj_sbcg$$dXh`NBf(-Xu?>t{YL2yGt? z9k`DzT*87)PWGO(m!y}ylRd)C?4Fmsor9~SmkitQ!X?T1-^gILd%qWPvz1|c{9CQ} z^wpo=d*bMVxF-P;1i}Rb1n-GSfXu`#9*T(wKa}9VCnO*w3>FXu3kd@Sg(U?j}{l6}1Z~t2x z{4UnjO$+fK`uq3$U;EY))tcD67r9Q1qH~B zE%;baP(eXnOzi0sk%x-Hih_#r55>j)-~0WoC9VHj7m>e|_Xn@VU+W_Jm%L!IGT`67?LU0$KS^W*`Yro+<0aqx z-TV;_WSe&(8~51;fZ}(P1z3C|ccOD=V}KyQ?d#XC-?)DJ#*N#ww{G5|WxRX)_FYC+ z+TRi@3oQdV(Xz0xb8vC6vw*?E!eH>De@^EpZ{4D#qNKiahx#rh`H$f)4b5E!P6h@h z1_n+6b{2AC7vKYNadCn8L_`DxL_{7vlKV#yXWsynS1vue^ytDlHo$qxa~CMjoizf! zk$d|23+K-L#~1)Ock#k6m(E{4_qX(q4=$Yl<>GC?`Sa&4oV)m2y7bG1^S1!!&R_V2 z^3KKkf{!(+E}1#A3Eh1aRVXj~S<8i;`g^arHqFfI=r0d+HgQiLy2fybY}NNYg~HQk z-&nZCie|WTQj!%o5BTN$l?#_IT=?bu1#*L(r@U}qQ1i~?U(B3eMX?EKQGJ%bTSykg zF0AcBmh}WavsF))=lbB{*(l(~1+oCj3zPskz~Q-@nS*b1$C&2796V=PjGDMr-8eo+ zvOihO{&$U0D~s;`>CH4&H%YJQP8j`>0t&Vn{$nU`W6{iz<>{c><;93f6h$p2gD-ApqrH ze*ORU^?qTBgya?3bk(-!=$h$?sKGfu%bd#iEU^ixRc<;TuoTQeH=vpqO{A$>Fk+A3uI-%w79a|=ajj3X10L>zYCoz43Vf~KI6=^+lvx_IxbddY) zyiUOe>EoSnPYn$l zBR`R6=4$nucXwTkKQhH7loA|Ta3FpBlUyUcQ}io6u;zB&aw$Pm1jTg?K`)wudi)0T zmD&%?j!K3RJ0hIiy1pMc>ZAnNyFdIYqkP>3w-{%~`#pRYt^(uWVoeOEce}UfR0D~S zRTat)4<&>dA^TN}JT+=m$L*4Ya`oU{20A4wnrq8H5{z;GYzXJ;q80?bR{09%c{B#b z*)QfT>9q{HO9#_N*tFPAmuVE3Fx;Sb17st!3%SDLqKr8w*)-bcN^7&O$*3c`Tk{!H zR)ef?lA;4u*F)5>ue2k&CZha!P^?i}$-N`u$9$9UP7<@1?ih4<$T29^?8~wUXs7LM z`_f&g51Ph76PGwB(#LBkW3;v=+>)G9o_oMeP_cm!f+WjZU?QL!m{$f-JzIltjp3=w z=8^L{snPiF>9kjS8scJCouDIEa$cs+^>f}RIH$_0ZPp;n#mIGF6Jj48?J}{)_|SFS zQsGBd9wW~283yA{CscS51j)INOxjH4=qoB1itbbP#F}N-yhwf>`Cuwx?(-I?%2zc+ zts(QGv3H_D6G$cBd$>+S^B@|MC%v#;(i@^)PO8EQJtR6GJvesXlj?mcW{#mPe9Oa)%oNixZ8mz@-QH8w|iN+|o*MvUD&o8fo_K&9uS`1ip zmbLuZWrwPj-vB@T(Vx#>y?5?LG#%ZXnQ0E|tZ7J+BK2X^DF=Db`#ESr7=VZcQMVZ6kMMd=ZnJaCO_-E6`_F@ltdDNkM!7wL|>z`hb~#Bnsc zn8`(la&XtwUJNUQC{{BfAt^SIXE|lgchWJG8tKyVv-1q#5MPIm1y5>7u9xzi%*p04 z9uEgLhAJ%U%cZfYA5qFJs|5C-#)B=9M98sTo##H`RCTW3upk@VUME?7a3ZSUF~}{# z?=f^Zhf<=RH0;;P%7)U6zU~zpb@l5M$X+bu(wRXfj6Qd>Cf^b#Re^n6z_P)6k79-h~2~;cUCPp zZD!9F7UHB=(eeStD=T};gKenSB0>ins!N6F+J}y|%JnK*xa%2d+Hx1Wj4WZRTpS}$ z`6j3z##ValG94;AdnGkLPfOnHR+rJ+r&?1;L-S|4Nc5(T0pnCV!gr4iJuvVe@K^Ce ztl2L7p52Lg1+@-3gJPNPP?n^2*l0qbM6IQ9q?#wZBVV`8AcOt*R`mb|_C~`V)xscL zDTInmis|)G;28Ju(k1kVC5nWI8*vAM1oz{A7CX_TEDE9`lHcms;^gkQjt-6a_+@W} zb-Gj-rz(`k9mKx;e2qUkGExS^Zkk3Q3LV&4#c*BY5`kWm=}`BiTSRuuT8+d-M2K87 zHlyF!|5R&*4r98k+Tfz=BEF3qJf^^HndT?QB4GoD(HHZ5fUa+F>I7x+dE0 zdV`EM(H~t6n^nI?e@#Zu4@$|ofJ|KmX0u7MuBAtg;b(wE?FfO;eVwsBAt@u>m8{a7 zRoczkmj17^JM)CLImxZn+~L0H7DE+Y;8JGZ=K5__5LX^AO`YFC*6BG9r*$g-|bP*P7sg9E<{e*>sgPQus~ePUE#bb9WAQD?qqp ze(^OUC-2IaNzFLz0@v{6$Ow`C##>q>>YZ%^&?qK?y00X2c~j(IXE&;4n`uzmeZ)fx z#}^idi_I#N2j@nnHri-5SwNCo%Yf|$nt3bC&TBF8bsc+jqQqfw1C{{kyma0wzje1| z2|w)G8WRO*isB5A(}ZSMueD>Bkro50Euim~Zk6ZNX9vzJj&%Gv%KS0d{PpDmGamYs zIlfK)ddXKIh7svV7O*NN9C1{G-QaQY4xg0AN3=Z7CTaMudmY^vPEEoD3X3|7)DENj z*R3RaIO`nvnzxbjk%-#tOjY~4?LD^3M_1AZ=siuZ@ay3qibIu4M%r(J<@<8stPV+a zqgI-#UzEXS&N5o|*!t81ipvAJB`Z>8+)Jy$4oSA?;-5)_4^xx&QZ!62kZlCZ;9($n zA7~Y0$5tgJS~BW>BU9Z5-JTltu2SaV7YmrIj4N)2uGk z2%X{^Sl5h!D%hD;@1ml<&xzNH@o4)M0iF6nceCyN)2@Xe*r-XCE(!ao8j|U;?>OiE z`+da4ca=8y%JU<2WdqrDsqf^;HkIfPXhkn)+^pK4)TZsXUbAE#KPeDRrH{osOOx}*Pf#tak)-1*T|(0oB?0s%g-`iKZxIq z)M=6z(H}2<+qU=cs1GRQ;=Ps%re$TNZ(!#I(bGWCD750vZcN+m|d=0!)xFF`KmVM@=C1VO=)#J6t== zgTn+zjM;YfeftCY@wCfF0>t%?yLpAh&R?>s?xte3oa(o9nsv1OcL~H2MX|1~9K8yR z|H2-thlNDApRXg9Gi*7oFy%%1$gttUR?zS)ENEVS`;q*?wCni2V~U|O z0A$9%KhZ(fQm4czE}~s9pf!%>(C7p1aKoipqsA>1`>exs5;h;A90UtSm?Y>yqY2V4 z+m(C4uMKOP%}aZh$Kxpwg7L5ls0;}kvWm9wyN+F!5Z&B$#v2%OSSj$Pm-0&PGi>in zl-DU+(c44}SH+jEasW^A5l-P6!lEN{R-@^`$!>-Pl~d}he)tf*76|L*@U%gAen&v& zP|UguV&fKLYk{J1@O?Kc5mVH)Rdof>ggT#$T&uJ)6#C(9oz~CrG@TFB!_N9NZHrqz zdND50w&hKSHN_UpeLBcBXB7dX*tx2Qz1CGvT={|*aWLNF_Kkunc8MpJJ*ZKL;Bt-f^P)sz`=!0N89tmKE9z=kdrTV8?YV51-s|WL?(Q z4C=3Pn;&8bFtzr4^9uP&8_lfzxMu2ZO65sLqW-&9K!fx0j`GV&j$xz%h5?RClUpd;ct@STo_chg7GGCuVv}2G{#i$dtt$G*?zsgY z0N{M>BvoYP*m`BA1@YnJJYHSS^V?T|wo>1?1x;Mleby^o8IEx00XSThri|;VZC=dy zDcDx@TiaWA<6HDF5hyP-H!g$eu!eCU$xA~vd84*+0Jb&4T@V7cdiCOxg3ZnufSSpd z`|UN})^f7n)w?hH!X$2%EpN;?HqW}*D9r}+s58N4Zdx-AL1R#`=Iwxm+;RPC(~WK` z)e3jV>&xVi0Bp^t3FdHq!xwRUF!RGztV70&Y0X#0dFjxr%00F|*{kCbp8)+R`Uw7H z93t-{=|J0XXVd|}i(c*)HPP{CgK8w(X!YGh7vNxPIAY$)fR?>=!ACclUwPJc>ZmQs zG=yXDo(8qy9juV8!Y^LLAhgb0CU?6!32A0|$)X<1-d_zR>8WPQSI*ZxbF*JY>uP|} z`)ktW^Mvp9txYW@CFy(dFDx1i#qTOfdas&FIzj7hQU=QfH_q{g6PZSlN*6iPL8sYM z0Pk@4mFy;KI?SD2_8ntfa>*%pTkDRsx13@k|@N$Zpq`(SP`)=_% z%6Bgrz$}=UNVxY8np}DC4>$jA_Ln=4&eJ~-5Nxfl(~@hfqk0(5M`Ax#-MYXrm0Q!2 zo$7D`-H>^D<9#&cy{q>={JT>BD92(Yu6J3Ygs#bE>^!jaE}oCyN1ji2q_fGWhX{h` z&kjzko$d-3;UrSxg5M{ln_ydxwRV{)D5&*y6q8U^U7+1#h?>$!fz=TzlC|TObkAfd z`;mL9To-={{@Q1T>C$?>r%`JcI(Pn?di{&y7jI=tT$K?&4k$H1C!*i1h1jN{Zb%zU zaSa05suAPbn{+O%m45L`OrYyUzG7PKl-zWF*5_j5rLJ5R7p`ZJ^P(~c?5DB(NvG+uF~YhiCG+@ZzM55p=7YMpm~S*{xvKn(i?CbxAT z_T;;Z&&r4m5=*i$E9T!x)IBuEqg4iW`EFd|uVV7Cp4IG*ArQ ztTv|r8#t`J8E>XA2jg$cvo2_Tk(IItbvDYh{1|336KSc79ZWPG)RB!m1E7h?UPp4t zRi??~hNeBe6oT^;^kyTL`~9#EWeKHWmq0zt>W9N)w9UIg&;GiYsT}7{M14R?bDn8_ zdnb};m*N&bIRY8^@uKwv3f@_BEEw*I84`$fTZ_#&s(Z`mepp2zGKAx;6!wMlh+8xF zeD2cJS)N(xYAX;R`b-ZOo7m+$$kB$qF^rcE<}4Bq%)l8D3xxy;_DNa!gU1~j>x%a} ziXl2lKUb6&SReSeE)?;Q7~2Z*e9R6I%QkD~nu$e2(q3nayEQVWyX#{V)tIf-vfG?! z^x%#X=Mo9@5jM3ISUgn6nxGlr=iuyLS=yD}-gpL>Ko`ayBqEPIcPst6;g|0K+nXd# z=UDPHKOUz^S;ZOqrNDe@t9-m_iE%uNw$Su50PQZyYin^mN80wwDlFNeSaHg-GF?z) z;}F{}^StG!k=?BMM4h^{ziqcvWI7Vvfz6-l5gX%lU|Nh?3EY-jc+yIeJ4oAR@3DvV z3ro3o_GB(a)D%)Ols;AZOkOGwWgw?k$F1HhWM^IY)B;k-I**s*>HQYD2*g9 zDH6{BLM%$;bp$yumw4;<0 zgXzkFB2+#_qFq;6A#144^5cB_yx?l{+k>>7^@BRs&xQ+e^PO^3>RjWIoL&iH3j~!To41LAIFF^i2(9BN1bOOD3k%>F?k%7uQtNToM45If`}J-O z#9Y7Aa!bEiuQ^j?zgTtlr=y}h{1AOjZ$eiU%DafmD)H^sdT-{hT7h@d%`G>KUUl_% zS3KonjZzXsHd%0wWTrtEtS8WB&Q}xN490h5x$Yx_D}hGXgt1kMq?Vr#b4f5fmt>*# z>zGgnYgy#?q?D6VNJ?{rc&>1sug7rw*d3VZ@bk8kFda{P#aPYv#}yemF8i1_8J^=NJ^ zYj9&mt4XTfY_skN=R6O>sIZA-N)fdePVZrCDgt54cOIix)O!A|?ru73r|#6sm^E%? zk}=?EEax6*#19>Leq1S8+6J?o7t7MZ3v=zSk%%*pwxsr|h#x`N-XG6oh$T7a-olQ3 zYNSU!3Cy8}c6kF$wQ{;|?ws0gnOqz&S<3Os=mrbX>1y02a9n)?)UMP@O%|2y!d9=} zD{U6sRKBCj^6M|p|NVL6f_6-&zLy8=pt$e)vD)trX(%eL)DOjFauIUiWkF8!1f^M+4*Pxg$r{|yUXPapg^s_N~3lpwTq>4 zjP8o0c<|HTqVlm<+b%T|E30%~8V_nq3}_3fIo&GF)uY0l0f630gEDmMHAM`|+eRrn zL0gk)o^rW!$+3%D-u`ERpR^^K>VlfNltFsts!#5TkCg7E3a=oqc&fW+I3bPqTU|~+ z*Uw8(sJwmi^s}@*2UCv7JZaan*c~v-+!59Iopg)d1PblFj&oUv>C{@Am?X?lS-mGn z$lj6^AH?Q(Rbbd56f09LSG2f0t)az@+k11at zZQd0hH1)cafRNdBwEQEspgVxUcEVTU7lOTY*#SyvFIIw%ejJG8qR?;)K&+7$T9U6h z=-NR-hUnfbTdaFteE@HdWDlq3ppi53U2pr3UV?;*_q7sagI$1^TodD>aGP_PGb;ZW z2vuI?#&(?6ZzFrql5g!DXOFjjhJhyB8}0DT@n?W69|=6p^USyt}QGxQOgh7q>(zh))CjQMdN z4WlE&+^P%}H$mFhq|?-A$J*d?^|=J3A?l;aN6RTVe;}GLNXu{d8MgK2t!sagwjC8; zuplG@7&N3bBtDmO5TYp80jZm@Y}lFTi|LBj_WP2$G#3D+J`ml1Y5ICW`}XH~ag8TY zZ4uFJ;d})YBAF~z8O8Bg3@0MV3fu9VYCJi3h8?HL$-(q>$wxwF$`Or3uWB8ErP^|n zta&M8cO%o+{sh*(eU-d`!s`66Q(ts1c4iYqUchNwrf;1doyNrthXUn=L#JpD0>I}h z&j9K!w9QRYf}}E7AM_aJOOkXFGVADFPL+QzFHpp4xHHCOf`v}H{m^%aFeMgKuO5BQ*Kd6@wShTTyEkU+74C`Q4%}hN-bJ!9Xs9IyG35I0?z=RXMl$x ziA$?%wp&vW3$@B86b=Y+@gaoCPC-dte{t`uSLe8tm2@~#RLGEfWy+K#t1jPGdwM+{ zW9RGxI;~mMYaKKQm8FgD2UOT2gA6Vti%|N`I5>N5vjA>+#dR| zv<#nXdd->B&U4n63}xPR-W zpTT1PxO=SjjWHLy+KyGjW=H*k)At37aKD3UA6z>18xDyk&`2erplI((B-fi?PE5&b zk3pD7Q;+#7kL-j9(5%C%B{UvtQ{7=}Kay3F$t?0yf=`>nm`Hf6^xuIwW|?>D=E~ zI^J!0h~rXi?o);LyTX!^3+IG3>R!cS;>!YZAsIDGWD@>KcMCm^pq74Q9S8dYoq9MwhR5jUr#lI{wfvT!Gk ztzrX$;@-g_vF&BI&X0$hqY<`H+TpHx^uSK@B*RI!s`+?n4S6H&1FoQzBx_1!)-P3Z zh-uT1JOh9=hLevy!7*ux4+`gXX;GClkaQObX%pGCj(I4>ifI(BoGk3?ahI?3bvae@ zgAqCH>qhDH8Ws4O8sC_)4atOHL7}czpE5q0Ww>I}M6`oIbn09+Q@GnXqL61R;^kF=;Mm7hB+$&5Uel!&we%)!g^L5voICYsHPWB}mqk_ygI`Zed z^LV`W_j85XtLzr0mhqQI$3w$TV6BR@8r1!CgIv@@>NPA$iQeS~mJn7QGdgDvaSW}p zszQ}r&HGQ$+LavRUFV^x%O)mI8RA>fhR^%;3VvQ_ z<^pyv&g577+1O%88m{ZA#Xn|JKr^nZt+h}*eF^#E9qSOMldm8~-=;e2W-Pb%tIcHB0h$cX$V=>A2%aWPR+kZwc?8E?7_ps%4p? zM|>#?sj0P3@lU-CxMB34hjHm{O5B~Gp~C21^mMm*>%=!A<0^S#CCtq1GAm<~gH0Ty zvx{&rWQ`mZ%t*@8eB9cB207W`P`ea@y^fDS#OK*omX8@tTm@?jn z+v3|dR}U~gZ2^sj&ihlU!BCkkQ`(x{Sp6KAl)0bQl{PEYvMf=O@C1S|(8EszBR3&i z0l2L8>%WxwH{$J4RlQ!&kZVesZ~*iS@T7T#n6se;2DFr48%%XSRLoZ z$|KndWHEjFxCgZoUcQu_TPaTlRq#-Dht`dfS{2B8JcU&M@cyzv-0^CfNgeMgafah= zv?0fE)V)H=8=`hKi()Cio-e;a@0D;+tjfwMwUXAw>YQ>eGp_Lkf}j`Sui~x~jqz*( zeWYJt0yEy=nQo3PTst5Tep>EdXcCc6N{8Co-79VKdZ0(F=9iGlF4dJhjuT(1+QVEI zxOn%34=MfDrqaD$q&d6!76esO1i$dAsj1;mYNzcrPPfJ9XOzwLM1cl=o9d8WW2kei zv0G>_Q1_jz*wq(i_|VKc%Q{*s+kw$vsLud>pDC2B2wekn#kD2Gk?E=8uK+TNMv6*C zUILm&?J{xGH;Izq^f_TWUCRI@<#}}T)1~NH0jgG;f(n1|$|)OrgLALfIyAlUgaSJw z_A7(H%>I%EEQ&!d@k5IJW^_P9GuOE$vndkYPWEsW3ubwP{}IFZJGQQdBI*wFLhGt; zIYoW35N7Q~WjRq%m7dOB%tJ8QwUT8;TQ_#atCV^)yI0O%m)t#4W%A=!gJ#JHV#lZl zx&eJHggB;m8V)BW@}XmB%RUKNp#u5xRW2B*W)Q}j#6Jn}E)WW5ruNx>1~P$ z?JcG;TtN6J$&f*Kr$8E$rLv5_Wog(u4=bAN=}@(6bltsJFkvCONI?^nS-t zUQ+#i*(N=4m^be8o}Tgn+oL;=@BLLLpm0n{?w-pgtfHRPa2!L*cA0OMD(0f?4i&Gq z9YysV`jo{bN2Qsouk6;@)eSqQ&xEa~wK2&J@{1j7!vc-BNKZZ$T&pA)``(My8}8l{uc?W6_qmwB5Vq#*uoIj(t`NmF`Q&GLsG2BE-$DZC-a%=8lRztg9%# zluZiEJdoz+lV~?6EVGk3)>7&3=w_iEbrB(~9kwZyGM_>bz!BLkx$&{rQR$>5u6;WJ zi`bFKoSZ!a+%+wvArz3xzIdso)ridC;;~uc4VjlEPSI_qqTqGgqw^$%(KEn$pzZ0* zp?w{MaQ*NjEbywEWh6xKRTw;XRkZ72QvY5ReThP?yFf`(ix0$hUFp_}A;OafuBRBk z$S{C-ND7b)&;$NwY*SOyVxYdI;)U+BL|gl zrJ_@}c&p;SBB2(fA#IeeQpH4sOE_az@jPwq-FBg+W7VLJ^45j&Y9p8gy+FZWR-u25 znl_1+@~U{peVZC2S-F)wip~8?14e6NG77ra zI-8oti9(nl>(RsJa_en{fNeUVrGf>oQM=rVDP{-z$5}~(9xF#=_?JQGGH!p29ko(!KzuwI`6_sLVt5+)SpQG23zqtbCD zf$xrl!yp5iR(xZbi+Vb7l`6WvtAGYRLGK-=-vX^VrFvb*hPR}y(A5*;oi9Z*Z|2Iy zOTi}{opuQ$owFmA<>HQgB7@ux`|hhbPKW{UbEobA!;Uv;(6WL`?AMml%o&t}JZwT+ zqChStm?RyZFeC3aH@Olt8PZ^9q^$I$>EJ7hBD;#EM`B_DCT5ast2Ht9sU9)OnWXL^ zn;k;|B+bF0{-$UPopkIQML6owUyUArr=K(JnkT!RZ3@d0$CO8b!}?59QWm%a2B&+c zbR|^~`oOR8sT3qV(Qsm<@S4M+D{Wi-He%bDv^rgN-L=UHif15<+>JiEWzK#(zRW$# z=hyQUf0zBAGS_MyVNlP|aUdlrjL$Kp zf3)opWoJ*2tIxxgaQ}(4G3<$UZ^<`jtsH+3t)U~y29trbz#3GL#~uA}p%L5|P_&F3 zT)Q^?pX&S64yK3*P0 zVpk(}#@#h-mDL^NYy0w&9fjjt8o~yZp5osv=A8s9(`7d)cyN3Xt82)L+p24kt9UC5 z#FzDMD*b%+SA#EE^~d*aNx`Sz4ex`?cK~lbEO>PuPnvf)IrXbhP<`wTed9xpAX~k- zPW9TXMHU>>Nm!b_AGY%${UF{5uBDaaFSX?eJ`lhqeaBZR2f-|u_ULXU4h@) z&01mg(-U2q5y&?d+&LX9{PDSW4Bf_ns^y!;4iA7uwDjK5LFr|#`!TSO1}`tEiA#51 z{t!zY_{2o!noyBSNeT%T6`N)&vRF?PcphN#bgwwNAMM$-Cj0)f^XRfZ)7rE3tR6IH5yy54+y|Vyf+R|0%eLj@fVIBq}+J39Mz{x7t zHeJDETHEQa`Zg-}IHMYK4$V^IS75`G`SlTfdTIpU<_|ln?XH$*^B-^IHDYLK2~!x(0tbw3oH6eG;@0 ziIG#UqF+m8(t{K=<}@yTwm%RbfvPebTg;cjqF*1k#TQMKtQ{2lx~~n);Wp-p*tXei z?t+ewD$Ycf=xBVA^MF8nmn`nfTKLQL2&j>SVw(o+1qyzo@h&_`h;REPPE_w`%cmf? z2WumpB`UX5nTLzZC>%5GJmqxLJp<^T2D^)w3w|%G$t^GW^5!6;nE(5ov8F}4j^O+D zI#@0$C^YGKgS+AFifXVzXf}>*a@`JR1x4Nu(`^kcNsp>0v8MWdZ zTIG#S!?_x8YhVSEwwNd>%8nINpj?z8%CJ-XV@v%=s= zSy}myMF;SV_&OiyIagKMm`EC^P(iXD(M1Z*<5}?W9g;FJMpn0zsgvK&*XDKViY^vg z>>_bo%Eg0(4fac`t~+Ng=lAt*(Fs9@r2P$cHOZ$*AXkn>GeJeLLfa>{Z%Gt#JnGRt z@+&2=tA;iAs#tZLtFug?F(YwO_u&=o)ZW&>${p)H-0B-#Tw-jdRA!uml)aH@d}$L6 z{IykRKi5E9li66vEzy)@4&&ryjZtra;NQbg>D=0VRWTfVeJ2LHkNv(!OU>GYMArx6> z05Mc@81m{&i(5)@%vhDeh6H&68ghJ@4;Ni+v!GBqT$DM&o&n-c8=6w4#E*<3Q>m9v zf@L;$PJ^umv6E|z$~KcBo^jI1Y-eZpg0w~BDpY$~NlL9^fWwmj|02}BQLlfqgRD1o zM}X`@$HD+>;HZDl&!y2?MLy;+uDS#G4AQ6LR%pk(qrv!_z8v@GYxbTGs-pVpZx`_1 zRuK&CB`*!T19eH~&j3Wz1G-bp%B>^Q1A23F$)d45hqmxB9o;&h`S|_5H%Wgy`?Cz) z22xe7&vb>1bmLN?ustlxmRk>;ga3K0l;K(ilWflbF`1V|PK*Xvck76Pe_kh8Vs=o6 zC1-%Pz64>R&%sFPkY=rL8jONX7YmnIOe@a0Gri-rVU9Ig+cx^JCnLGuDIza#o2GEI zN^`*33>TxSZfS0HU0yLR*}Uwt>_fA-()2FFC-kcY0U*3{QWS z&w1ZSUMFn1*yW1!@-AeohPzG94TT7U(XrfJ-#&g#`q-|SvlMHRH!`rwL_gm@w>_-C z@sY7qOn-TTB3B6MkR`Sf$?P%q02R>ex=-pdcx%#wTwPCkvz)8=6ByU7dVx)J%PF^v z=3}Vxy2*q3dGDBxS@S`KT?e8IgJvPnykyN~^Whpv;}k+sA+k%iut zExcaf$;0DZbmVN3ZxxA0_zYlpxR*;v>FYq(N@=&6FNZe<#d(PJ2v zbFxlgE!=#}9=Y>gan;f(xeHvJsU-?4RS$=cReW7|{1ioOMcY(_-?)kU39OE0 zuF$upC^89kS9^P zC!o7P!#Y8U%u3ageb75HY+~j{q)Hf(b73|)S5k&;s#HloSOTSaVnk_a5Da%8Go0AM z+(m^OWSrt{@V^EEV)v=#Z+XQ8U-XE->v77T}Py!?!|GV&vT$sYO?6YC1d4`*>1|v{yNo z+j56j=PsH%sOFAVb*3m(3~cp!l?!%gmySq?#;4J0^qINTd|wa{UW?Q3cS)hasf+Zl zi$z+!D`M+;KG(s{Y(6sAa$vnWHTGjU=mUfHyt8fhA)`^jL{&Nukey>HT};C_w(dh- zTvkqV-pdMuIba==Au{AXA~hg)_A}#HTGP{59qm;a-&uX7sD?NwY@tbhH{A$d3MoH0 zd^<$E{pEQ~YbYv_u31KAqN&MAIuvbhySb#HB=JU)+N-EL@(<2Exbx4R z$KUVX^{Ll34MtlN@h5Vu{z*%_>&TiNAYAnQs<^5Kn^guvwLxLQm3BdSBb?DWU@s%k ztp9Fp!e{(jzNA&V63B2RtzxaYE6!X(dj<&ygX!>8((7B0VgOy~->dka%m`S{DKXM? zWbBO5qU*{j=~K9c4d)J8;Z3Vh7~`IMR9{APuLoOjzj&X`_{i??IDBuVgQ$nqO|}Zx zmCzCZTc?>fL-j*N)}cefLZhWCId5brlYA7G0R-_6Fdz%O5 zId`;ebx&IHd8|>31>24e`Rcy3R*(r3t|!5-BHkJnyyqZ*J5SaY!-+GuAPyKgIg*FJ zTzqZGT`v5q%G2ZC|J1aQ=hG{pPkY9sT?6zyj|Q=pvhGtHeIsh>^bjBV>#z4UXF_Zfm!aT z8nh^FuS;n?d{zkJWa5;X9s+*oey4DN^3%1DoUar*16=j}hR`_WJwzi=k~-=1Beb+- zN?%q-P3@7d8NlaauE{-9C}81Lg+%(ZR{vO+;Jl_*%NvoV<7g55t6=dz!UbBSIi@4WZ< zXInI+PQwFC`A}`$iTzIrWY7Ukw2h32i1(@l|AwN#x0cwttU4iWf2IulJid6P`MpKM z;$9C66p!fO>{jXuUoTyfjWUgwq-CNW_H-$oSg^1#wP4U#*=@dUiW+ri00W|7WS)eU zx8q1Y{===0UVeTECZC;=1%s&{!+uke{ic0fQwL!u57!=8)W7K+9HS7j8J3(|Pafk) zCmY38wxEY;HfkR(l^lD}$oGxH!464s!&(c6K5L#NCi`eDm|u-sAY4*F`F(ul-Wr;x z(_gsxuAiygLYMgh7DU?2kqw26O~c}t@Yk7ItG0&56G+PkXMj=ow6_7on5m`o@bGa* zN#?}Q2WpM8hIV+>K97}f_#zsj=0p4I1$4r1-QwYaLNY(!{nuZ9rq!oZ-SKA>126^o zOSPHRgjQ~U`b5w{b1^4Z1+a8)!^38`mpYnp$J}U@kwqzy(P|xy6j~ngH`#c?9$hAT z64##4Ud;{4u|1eG>-t6lDFS0v*%B3Pm8h9Tjx>U9Bxn+uKpKveVQ(0zwbAh8Q|fVR zf{&IX1(Dpsgo?wyo&GEYXiIyrj5yiM7mKrkBBG~}c0Np%JN*a4* zYzsYb5G+!kobc7CCbyHcthy6v#=O@=B*uYizl1n_WKnGpbLhzz0@WY4AH*v-Dd)84 z&QErb-$<$1c|6k?bCB2{blg6`F7CFr%cw8nqCXPf>sEa%;pYzH+ES=%?YO5_a3OU; zJk=<=)1$t%-$b&X-m|OC&#;#88Q{X&d+@9sQ!bK?r)b_Qfn5ab3&(6a2 zkbW7n@#|x7(8%g!#}DJIyK!57Su`Tz31rimxXPR7pt-r+$GtEiSv<&O+202IkX1}vv^Tiv_Fl%HG39k*L^pL(wjP)H?A$z-W70a8hojo4n52uP6{ zlJ=O*PC3H-rtwe zP3SdgC;z~~luT{f)7wFOp+R@T;pD{(@)5K2uYd8L=bo%k0LgIXY`C8&e#qK$dD8TD zpRLlMkQyQj6OZ>Q_J!xW>1~duxNe<-PcCSqGrc5~4I8~IauH;wFv*rL|WiXtc6mxZl& z!QU?3{4e!=W`4Em__}^-YwluL31)XAxyk zQm4XJOUAq*blccIPGd?|Fv7(t2~|IU@(6DUS)t(L#fpkG7mpcP?$)L@e4OL>KdilX zSX)=N|4rh=-8jZn(_+)R=^f$%m?lgWO~`c9dvA#iw&|gYPN)J2B${Xf5raX1C&w+^fLf?iiB%SRg@74R?J==Kd4cQ7HD@;HTJ;qTe(i52vaUgr;yl>O?|Bs8%89- zZuzFEezizk$$(v}uNy-8O=4)^_%+fa^zWn7xr`nesBwkr3580b$RlS%n)Ou$cy?bZ z8=PoY*SuDh**?3+EFk$C#JG`JkWYYZE`7IZ4Ws&%baBB1KJTs*Zb z7Xlo4c3*^(ciMr1*euU9ZF|pIa3XHZMrm$u(77YLHhs#64E_kw2R}@7a?CP>>``@$ z;#WtGS?f>2xzzgC`U;4a`?_6ILw@Qa%JS+A0d~Vz)GSZr`l{=bH+z#!33lk47I$<` zuv)BTP5xn_xaL>=yJ}?%l%!oAxrHiISt!VAL{;S$8^yk}$$dQzp)P}3L8BzHFY^2#VAMl!2Fv7u#lgc>yJp$Omp zD7LJ*)Ac4p9%1t+?I$M?y_P)I-cdz&ooHw1f!aO?2&iW32bj{%d`Xm40wd8-X>3lr5k^WqB%NPV!g8?7^()WToY>= z#aj*%P||o^oMzA^==LK@EQ~%Vz9(Ua z;o{5!qEh~l66bJjlg=QpFiENS)%uya&HaH&?H>k}|2p`EDeux5|EgJQ8U4j( zbLUUnHg_hWY5~95i}>d1{xnKUmLYwE(S9UlPr6K-H+3C!Z@?!H)ztL@nTQd(=%e{g zzrFspxaCEwL0`#EY^dS2@gr4bhd~Bomy&lxr|9eG;`sG)UW<~dILkw3BCWoy!@|b|80v6 zkexGu)p|xxByzM*p5)Pk^8xieX>0S)=%kuGrFjQy(YSQ5YlI=c6bZ0O$jaI*!?@zC zuB7%1SL;-UgLCep*qv3gS4ptiMW03mT@(;e=qD-eoYgt1n6B9EKRr>1y05Ub8Q$)N z)h{NVXXaUQJ{Gj6wt1*2IThAFn>&SHITAgC96?%@S}w%D(iH=p<3$k_EkwTSh>6(` zqvVqWo2R}AS}K_ch!_1{R>>Yai7XD0)h&fryGhU@PYeC~C;{dq;;I;N#?(c}&(NT@ zX-Y%hG{SYt32KezMbooOKgxB*(#Qd=RhGhjMLObsGjK5by8MM${>^z)slLY09!vX^ zsPDJNUo`tbOhr}`VcGg34teS9)X^;!U5XQKZuH8G)c?;(bWAqAV|I?OGG+Hd`PBtp8IWNY&>Hz)Gu2NBUVZ?aZlr;dIsw0Bz8hQQLkqGcchzDwGiZtr4B+G&I zy%UyYS+&d99RE16AOAB(ROEcytNy=G6kGprAOqfn?nMCcs}M_Q6#D4m(n!Z%Y6 zjlS(bcBoKUua2rswH7TKXDS4Bc^o?t@zMn(jFnV-Yzmclj1|`ta;L z?L#;ANA(W6#?!diQ!_Fv3Fl))<%KNaDB2CePs0nB(w79#cCrO!_f7%DHqMnVhJyy|4Dn((>Mo@3ck$-{`LAF|M0l~ zpD5sOA`NFKJ85wnbvo+KE@{7&nf92HRh=$JJI0NDPs4)``3%T&Z@sNoH(gka`WV z0xgjQAOdXV_m zx4((lhj;Q+%@!=;vp?*Vv55^agt_DQczS|vy?-|^8m?Ps6?!hGmKdDR%JvxgWk+g0 z(9F}(6)siUfCO14n8h0dm68ln`h78w+qV_HVzkeJL#pqeU)58jGDqxYQXM4P`t$?vJZ{Y|n8wYsgR$)G&5?GFb zA%36imdNunJxiu+gr~0YVdXii_)p*L%5sT&$@C0xzu*%G2STeb3YH2mcxI2E&F_XX zwcEeXS#SkeLjEx2M=)#|Xg^PyX*a2|0cQ^%2j zk0HN@F_u9=z>pc5PvkbNMSG_?&r{5ko;v0$9(C#imO9F{kZOwm2xqy)9CG7Hr*c?9 zeiiBpKa+|LzZdt%&bWCt$8JUUwAEGvKmCjHOo4ktSmE<*2jJ8Gocx-&>C+%i+V4kM z`?%z-$X(K9`D!NRfSz*l>M~e81fTj(FUdbVige$V;FhXXgoeRO`W|l%>;0Gf_{70k zNh(|oCi`A!jXf#7?I1);qjN?uE)1Uq$P00p?8pvsPMSM}2xRp z)N*Dbo6722C(@10KUC%U5!rskBRR0AD7UNlnJn4bBXvBr?z?psJ@jxk5$tY_bryX5t4rLB%}udq7`&6B6C zu8AIp8 z(O3mV9hdfg$#h+Db`f@#lz05}&zi1|Z;w0hVOpZz=?8K?FJcStQx6@B0l83%7|)F~ z`fc@l#8jX3@r`)=22V+I{w^P4k1)m#Sgrc#m${xJXZ_>u8TlIO7@$_iV>P8LwfWoI zX>rMw)@f#bWr8q6OYPN$`^(; z9K4K+RN@)cb}Po)jHJ=qr&3A+nGO)*D>^dp6wHY<5K5-`}USv ziT}gUd=J5o;WHLvKe9q$+UobxzWe8Z`Tqf{?E015#SUuw6MfF{4e;f?QI0u|OX!%q z^=S`#PUPl?RsD19-6n(bl&#DM3P&Vr+a=w<5p_Is;h%esHdovXO3%Psd)}XOm`zKM z+AYyns;--<-6*`CR039hwCSg&5PVt{ItQHI-$+x^vv#c&jA0Vv`4QTFoYSVx#-+}t zv9d`^kiLldt^A@^fq;D19^NiJ{xcm{z(2n#|4+w~4q!7`sa8VcPnL~WlNEa-xWuPs zAy(ViNi>oCEBP{)n<{$YHi5KRIbxbLAr0&r;aZ~b6Q|;vg`!bys}R74C554gL9cCK zo#HTi)DOSfV@_+S85217pHc3s>CXoiR4i5Icx!xY2LYbQqm zmWLG`TQc6T`rJhdgeFWVuRIq)LmdG5yqWPe-)=l?Mm)2;90)%o*F`xR(J6)e^8eJU z|Lu?d^M~UK=icsW9rna-ZaU#zvb0x5&zuD7##g%dqH!Kt^h1)~L!oN;MYdTeZNPC@ zR72+^ALidMT!l6Iz@nD4JJ3a*K}!P49xHiad9M>(k>%1`bXWChoqMtn!U#m1@o*{! zxLl7dc=iyPQusb$+N<$#`m1#xFGTMl?wO1un}&{sx9JEQEIWKOfs#fz1 z74Y#8XK-cUgyZJYypx7cUZ_F~??}%~zvFO0(<6nw(^Zrf_br}nO{I-=;!;^si`o-s zpS?vfGciY_FuVO@%+6c!rMx(aq*M3A6a#&`1Iw(n!Ja1mX`Q9A2qEQ%XO)MH1+nAqRkI^Wmb<-kRKph=_) zuGZBDHpHc`;YYM=Dn2;_)3Tso@cAw>xy%>n-)7JoSYhFnIqnfY1OJoaUX1D;zx$|+g?XB3>f@R9!OEi>5Im2gZ>Xkr#}9bbyu$L= zop3ob9VX+*nT86`lA`BHW=_7wL}wf{Ao;};MeVbnupC-U?|w0&0cV@%W|}UUE*8~I zcc%Pf5|3X)4s#R_wpXi?q{;aQl->HvyqX5Q4M&qp8~zA z+l_u4*dsL=T>s}B4d=51?vOYJ90DC(4ja~pOy?6>WLx)}g$EK$XS-kNqN6Hg>4c|Y592k?_}u@%(*zaLUan7*`Pta+K8l}tS@(Fv@}L^Cq{k0_@<{G!=z3}< zp=4yOL~+w~y;%uf%=S}xg~Sp;u=F%&b6YWiWBPb#<;xHuESH9sLmv0s#-vWgd9*WZ zxbH0iQM$fUXIyYiKU0j6O<~`j{L{<2x*%1PVge|Kk+929f92K#KjVxW+RduWgb_`f zk+Zt5#xi}@HA4%gyim7=!3@Hrd^eH`sq(2 z1L9X}?Mp`QMah&tcAW0?&I@{~V;HeYjb6SkhEI*#@p|q(?MZ_d3dE>AA)nm$PB4*o z+Zby(kSTAre6=jpGIY~S!}UDqHO-U408sP=Na~)V`uHw&SBu=Z3|4~W3zaF^9YB-A z6%wSsfmNy{aH4o|Dcye5z_j?NAiYGqr?7dx%=zbbd53YGfF<1sh+??aG@4PD{_4;( z9$BYxEl4sS#ini0Y^7S>gJur$C}^YD_{p@Sw&7sEH#u)pXD47v2cN&#=X!9Uk}QJ8 zkab2Kxavz_slP!3k>V%ix=4m-Iyee`rr1=vF(G>iA3j1nLyX_1y-A&JZC)FLcjP1q z+|!Im2|@>Iy>Nl2_usDE&E_b}9{GgBA_97D0L!=De|m*O{U~2v6|?|2@@Vu-OCmMQ zuy7^Uv^@ItZ_@*f3v%^z<$G7*q>Sb6L^b($98t(vCm&uy09L0uq-(7{*XP)7UHYhRJ=peEl)biJP#JO})-i~R3i;e4 z8Ex_^-44xQ$ulx`g2vu41evaylQ+_CD>QF+7c-iXqM74uswnLa)yPNXTN&ormd_sc z2yG-pv?HejD1in)Vx62Kob)*BwfSynuUiIfs1U!|4yo45cOBW%mn?c`kz^m9dd|tz z)YbCEc5KJ9p${_|w|FN^#=4EQe{|B`LP=mV@?%o?zf6$JUo&m7S#qZ#RQtE zS;I+@@~i#ocMYyVz5Cwt^(wnkst5ZP*ASnkMPft->SWi5j2N@f1;Rt8sE4ArPix5- zt;qO8C14YDOyZ!OcMGvY4y~uBie4mF~qh`0y*;p48I)J9l-5g?tp@Or6u? z{?jG}De0(<$S%_$<&pka9or|&t#PhWb57-+;cz8(lN}{H*P|O zWtNBMaT)HJr&zG!8@5Mf5zFJO%;XBFAT6x4`a2E-W2_#`17@az{6%cLcIp>=D2YlNl=91>mtW})9<}zj z!_m*``^h`IH8yw9ZP^-6R`xUFr)>ZHa*R=}v>Rca>^R`Ih*;0I7Q7HM<99M`8LA2u zxYpQauB9vN)~JtZ2bA~Ed-x){Tkq>@2ba5GSduS`xHM(g`;uDwB{cZY79lPeY3$>t z&JM?Fy7lb1Y z;u#Na=ghv^u!n~48HD>c?dU0DBu=?}O^MH4d)FsPBWLm86>H$ht-~Da;VbOM{hd}4 z$-wLTGtl=91-G&@wpQv+9kfbmRCBrf;VG9ok5Isjvc!jzRk=Kx1o7F#^kqzU)bAay z|GIH`bys4RbgBE5t{-^wE8Smjocz~KCSAlv$708MX=leN3)AJ(i;}-?%6{bz^E#2B z@s1vkz~7NwYb`+cjFU|U`_5C3q(`pFwO9Ax9v$?PiSCOt6%M`ahP~~fX?G_1DkQ7AQle2 zpIe5s1xt(Pi}5w+MDJ)5TP9X4Mce$%8rQ{QX!J;xrhQoABZ4!e*jwo02{-0OoUHzVy~^igdKC@Gm7A2*Xz8Cs)X z4eqkG1>M*2015<}pi5XA$=nT7^DUO!Q-3A|%>|mw3kCor2}??@6~%?4h{v19Mo;WN zSM8blm_=U3gWG75niC7QTaWrY8jTS_TA}gBCS}_P(iSlsBo9h$#aw)BEugV*uVc_T zq*3kG<|A3c_2wR7$xv$QNWcc$<7LHLkLzo_6$S*gwfNw+^-#$50K%3`Q9!*3Zq3(3 z3}Zg(ah{g!JR5iqR?*ztDb(%4oVwC>Z+W$LAK#yp+q7DI(iTDHXS{^u2}60ltJLCo zKnzKNxJ{qxf?YyGOgp90$&65m9PNP6;DQf-{B4%|<|a%;u6%|kl( z2<_ZZj)~~7}(Y6+&k<$`Yw-FqTyLO(Cr1GcTrNZ z3WlyS%oz4?>;A3ouJSzXJG?ipD$eQqB?!o}S&ZJmAg7<#y-wW0rjNF|GfA01kzO^IyM*n?B` ze#pA?g(ed5AhOVFM8u9=Tke!^%4OcybV=uBg0;0-y8*`%Xp-h>g6gl9?TDEruDDof zl*s0FDVvSwSxQQHWs&cXED~T}>F(unOS6as;YsX)=Omj}4rdWavYuMzx$EoRbbKIz zNz65sVEScn&H(N0`x=X=Fx511bT)|V6Fjf3uTsPnLxiwBVSM3?t^0(dFue>Xdn-2@ zEVL@YBoF9Gm>jwya95ru#T5f10$dqJrKSqaCR!ObTP+kB%t*-@LO_sExN;F5v!OWa8B60PiH3=K7gabC#P~}g%lPJH zNde2KJ3gto8TB)OHhSh=UWy_peG`;>z9W@AlR^1utPdZn6H3Y-rW=B>6MFZ*cPih12_ zR;j6SDiS7d%%`-3N`Tlu2d8MN$j>>rB|FuH^5if0g&g}#eujR3zwC+NAhE;^`%E3~ zRyQP9x$fndk{>G;SlXK4BmL3k0!+(sYeio2x*Mhdo~!ipSG{*yX*b+!{|r<)vc61u z`7#lsgAYQ3=<7#KEr2XL-CY5x>Bg3>h4%M<(AlQb`0pp|uY0uT@jpyerFT}gPBt93 z{7Q%Su?)lTlqq7=FH6RNGiSMlGuyc*k=(v>fR=M}FESAf+c{Qx`paWa*3H&d;f#s) znm6N(4vND@OFC;JWXXf{MSQIU%Weti0o17%DY)#idT$^T!zt(2mRGlBX*YIhv%!Mn z-cl+})#fV(F~}aDE7;lrles35vB!Of^$r+&uZUV3MrhZIeO|gNO#j^1Z^t*e!r~x+ zXLmd>O;uxI;06p&1Ky0})@F&BT;u%F>qdXod4#Bo18~%5=P=V*qHn>fYYF@NrwbwZ zR@FYt*hRw1V?W_RY~q1hDhL=Fc{TR{c&`b3xb>AzZ~uim-Cd<$|J8r}yK~?8gfDon zdp)1?%m>_u^y6qk{4n3zoG!l%^Ra#_j?xZg=U_jZno8lFx@kBuUO>P!S^pIADaDo# zw4e5sj%LrEBM-BKrzAHhBAu<-T6RI|l<8!I(=~l{* zmOIlOHr9%e`Pg&vv2QC$-OV!a#b+#K<Z8n0-(BYD{;WKwCF z7!YJt=Ti{-8q}2r8v=y;+fC2^XrB{`Xva#^TdF|VOcX^$b{(0gT5o9}+6Nl;*L@xa z!19zU>I?y9Gl)8y2erH+ii!)k!oGLzTf1XCbuvw)!v_k5Bfgv+W_4wh$#Eq`-519l zS8Yr)0P2eUj+S}w^2Z5{)x(uQfF)u&KKT5k6TTj(Bb+!TKT~P=4y^zw^f1Z<<9mcY zR_aD>UKmbR4XAJ_u@LQJ?!B2>kGc<@^*a~05mgK^5a7393z0Of=&#OsO=GOtaD`Pb zc15L=-fR-4wara-(j9!y-ybA2EA3EM-=B#njxM~#={QcPM3p5|4` zb69}(_k-l2?wB2pzQs98bqD{Zno3n<(*3-Ql!Q}{&GFnZ+f%dmEjrn_BdFN$VaAc| zoHHNRr!5|M5=KF8nYC*9r15+pmrzyQ1D=~o!vHTgJ$~>X17wf0y-d`4vPLXgYthb_3<4NlB>xpZ+_a5DS}FvZniU8hgbw^s)<&9`h-qF zQ+=&=4_4YDK46{tV&jaYL(WyDNM;pTxwAemjt2JM4lK6Fjj2Nbm_<+HPP>l+PK~-9 zHl3Z436Dx>unjw{A`sJZ6xFJ_<2R2(ioq(OEGisDejCfxUvk8C31L64i0VxE1K-%r zGd;dD(IfOZ~|0rB2ENInzp)9G2b_^vvhLSW8-+wJZPw* z&)`wulm0%`bm4F9-9QsX>I@Uul%0P(NT4)eGnROd=Gjp?h6ueM_Ek8KudA!-?yBqO zI7pmhNasO*DlT+!gxCO_0BWA%ylqL6TE4X|$}$@xVe}t)(R6+9RkNL<_uw@oDFE8tmi~wq=s_;`wQZa2!)qGY!FCpNEFebSfMguM6 zN0`h}q{K+|1542u6Gf}*n#yBk^00B_l|~ZxkAw!AMtRJkD|=zD)*k0Tq!{Q`N{F0z zxD;<~OYP<@pzjMXux8WyCBWl}c$=<2NKDhQY>$_YhXU+g874b%J_dic>$-pOxze3VvZl;4UoQGxX2sX@2s}KZ0jy%F*1nla&F8U*@oUhj z*P%8IHO|iAOJC`1+ZA3D)lDX2 zGd1c;RNGmUEo9unz|mZ`BMIATXv3LuQP#m3ds{_mC7EK`N1J5|DlQ?J+2^F?dzT-U zw+xs+)9qILi)Ma1b)_2x4Df|@)7E}6LuR@Xcw#HoV!`}0SoUJs(yp%uDh$;hF^WcK zHzv+xSC^ncm#&w`+998CB~n>4dQBRKi2ZZ6JPMNB!u4QF{d=Rj_Y;P+s6#A!b3sLS ztsq4M?`*%N6|{?&oGXvq5P%MHHA}T!MOnXNf?ICBJ=23xEyn>$VI#{qQ;MIkwHDva zTUdn(@t@zu zImZ}@Ez=nss@cG&vWwl-Oa|eB$LXwU6+?|coj`T*=AnrACWGi#xaMn$oqoh1K!PZY zcO&1EXfuU(-eM(G+KX%P^~{Vtp^dk3(-a3YgcnWTf`1!pQ$k_pmBOGO7^kNO!_)_~ z+v!K|^xsR{aFOG`4*A2jzxSP-YWDpT*Z8!vn^iY0O5T0}+R zLh;ql^li5R1_q%kW;>Wt%JTC^%eMrCWXcq@G01LzR;i6A27?Yu^9$tV&QM`T?#jyW zx`bBsW)#Awu9?VHn`30Nq!D(tF@j(}PYU>HPb@khTE${;$@iS>U2F_zSjvcRf`J0p z$m_HpyMg0SKx`1S4^a}SXW_T9o=4Q$h_UX5(1&t#{TVYRsGzgb#MP^Djy!*M6(p$8 zv8FOsyX@Pa;7lR2CXPDu2&+U3V!i@_-h#28;AA^~BltyxeFKDHV<2`QdEw zbqjern3+r=Iuf%D)0cVk($XwnCnbK1`^5rpv^z5Kqh&1}wSq<#ZC^RYJ<*VlT zsvKDmwN8=jBB}2SV|phjDyCJw466=dm{D?l&Fw5d4jT_vQ-@u)ot(^oQR0s*LJHX8 z6<1A_ygkv5UwiTyEpuyaQr|)n?9S-B3D?SN4FGV{$rpX^A9gz!cF!rFgokB@&vl0- z9lKm|D$~acWcHL?S=-OZkln86-V8nTm*->cbK%X|j?nD&AfY@!@qoAd9J8_^ifnfa zDSuGQV`%KagiM6crBj;$$FQmv-@D`Un;UiOnSUI-)z-kY)>pX5mRKUM47edA%)12^ zQlIg2NxJO%xE5W$5bUUZZR|JJi4LALzEhKUy2>=dvJ7Fx>G!EB%RZ_&ZrvPQs%ugH zV;%(VEtXo@2TKbGsUHADMphSbl_ABTXpi{J4Q9&Z87{r+GhN(!BEF$CBpkspmwU}Y zb^{Ndil-@00CGki36?IBBjxCBznt!cQ20(ro*RZeDns4N)|O33%o~zj;yeo3vUEhvK zn3Jtudp+nZ>ZNPs^SbkwYk*(=hmr8xSlHN^Lvff*i5suSf74LMsjSBT?YPBdH! zOkb3T1qK4Ep)vMV4dY16j0lH^>cgdGE1Z+E01W%~qJY~ajJ4;UVce)l@e6EXvW=`E z3{?z5b{}WQ!olu(!|bHQvcUDtthnv6U^BY!;6MM5E&00(|8Y{>kHCucmt;q-lRjR@ zFYuQOt2?Q_QEoD-rs|6$IveS^mz~S>9U{y$d_dqmaLw$rN;^$XIouKxywz|a^Wmue z8{}#-Rwg75Sw=?Gs`5`vhEYEEXM97CZAji3%K}(KD+IzD0$!4mLrGjImwS~$s$%gJ zTb&azzHk({i>q^;G&m*Tjh6v^7Q3&H+~#M)9vK<|z7b1HRCB%R?7S4QwP2N)HdV9Xhn@GMcLRlU(|jr; z1Bz_J*ZQ%`($ux*MK@g|hCTD&8(s(`o9|&`enOz{8<7|bjkQnhuk=AV0lgzY4;@QQ zFHk_vmLe`s*pI+=>vf1v6aUMe#JVC~o#J9Np#c|vq4dGQe$l@93&`46c=pJwNg_kN zAGfBEcMFu7p&NiYk#=jc+Y}+MswsLTK>gR=>H~$ExbpL9NT<46T*C&Sz(*NzaCx2h zMM}LF#WLuDikXU{h+5o=v7i-80AXxD-{nxF9??5rgDCdb6#rnwC&FpqR#4bpRCOWN zwPA>W8tZzlXqV%6cEt6BBLrAg&YRJuNG>&fk@kt~3V1JeaaT&fOid!2t+QECfRB^g zyhn9wDnKOq&zP)3)4RgvICl=N9O#V1efIt3pQ!nfPXRsCRpVQ0^WvipT`B2Oz$<#Q zOp?&>C@Kvgbd1^SfbSQbi);%;jhw3uOXvnhZe@$9Es(iB*c;yzAviFnj8pfIVpAVh z4vi}}hqun}=`=x!V`JsX)X2Q@9M4@IN1uc$P+xd&tBVYtTBsREJ=S||I}?NP;#yVo zAn3QYTgHR_eCa(&SoHlJI*_D!m!$>hZ(qJDTTUPsEi>d+gqwIq!yyyahkdX2Jq|vv zNj~1GsYqn~Y)atz)WPg3ewTCimKsj9(c9Cs(|6)0ucMcJ?@Y zb*^R5fj<)Nf6QbC?^6-2zKw3H?lCYXg-Z``oYJN4o6f}z*g*c2DdP9|sRE1=SM`@> zocF~2GR>@mclmrQ3uRN*w*vm%0)o%O|pkq3M>jrf^bt85C@{rDUa`!sZ0bwFa?#ToW*! zg0826%6l~L$Qyb{P82FH5 zbKm@qXIRmBWiYk|ADq|7K9P<78O;!S*#o#}I9Z$HHdTLf(GEP3Agb$srHk8UtX-;e z4fA!O(pILmV8!APYCSRClRY@!M0Pd@RMh*hhv zbYJ#arA{r=!S>(~0hZMqm!`~gM>ZjQY`D$I3TY=HvCzcIT8cX@-xrr^_>NOG=eRaW zLHZ+pBu(>adPPpHMspt)kr>2A|7hs0a^id${|n zXaB&Y8jaLO_V?UI9BFm4uHt$+g3{cbYc~p#YYSET_Z&sf@`2`XRGGii*h4~XwO$bL zX02OYl@%-_Nx5n-z?d*u(5^d`9U&%j##NfZm0NCVtF067L&`*ZW}c|!S7ru*_3zF3 zk3Bl&A9Aut!=++)l9#Wl)={FQDvdE=5i(BgjmD;*WOsYl*)J6+ofI&6$!K9Yao zYpCD10&#N$O&_WRm}*R(gP*@1Ixlz%9&DAtf0>Qn+>&m}D)mNUy|i{*z+xd}uQ%<^ zg)N2Zzt@Womwp=WZs&k}vm0%(U!KudD|<8cOiEq z_Z{-D=eYm+2XkN;X0MKR`}V8l!)iP-#Ued&!TNzK4QmoLRDOYU%D^YALYFrO!?Ir?_iw-ECf23qHX5JI!%xbl zEknK1hns}g!{h)>{;0Hi3k??{(`>Xyk+H4Np{(W)%2rvk!<3CYAeYCw z6}f#nNnHM9C;QrCqCqPphZA^1v7{&}N6ODQR zhHj6{0LBGLhJ^jGvM-MvXW!-?a6ildGMR1%+Rur@aifMH|4}1hpA&#)3q@V|k#BkF zno+U_KiYw&)=7Q2ooyk%YazVaA|c%35fO_b6ovnt3;sWMzWYV~aTz2O|8&FBwS9eM zbc7e3-E>9jIL~$&uzNIFk*7K4V4fM7lfe4Joq;!ZnK&6PvykB>ca4CAK(5?InU+B zat(%Aw7qsVn&(RaY8G~1ET;`J)sy}d>3SWaKyDT?`T?E zJ3UK46!2B>hC~=D#(k@Fltq$^ZBlU+LC^ zztU|37c6!o3ro_P1{%-c@m7u?-_bi^X79SE0jh1z`=6!-Nwu<5skC$gY1{HZrpAyR z_{y4%lTsLp2Il8{>6WzQl!37(-$`q65eiUF2z{u*G{RMj5EjDBFnq>S6s#kS?J&59 z*8@9SJZRG8XHsbnl4?VD$hJaZVqIl%Fmog7dg!QH)5Piik2Mb(r!Aky42Ea zzOkH5%OqcUvhPk4yO?NRwqQ@i6CEEL(FXWvv%=BS0XJm%0_?^LH5Ft8nyCu z)Ey}aOP#?`8swi=ljRE~Q(meip%v3=)Y=*}q@%QzyjhW{mR0AM*kX)JAYbp0ON_h! z7PR$rIR1-IGikjyCKD6@*9H(hL)oHAUWvx<^4O51Z0jZpVxjA_AFdX*SUoR=_!JX} z517!IS_e5Zo_Yww)1ksVi8iW4X5INBO{+f$Tdw=c?5aXhRT?e{P6j-ev*N@ZmMDkl>_Km=dOjX4@SH~W zC=sIJFj{qlH~E3zG=&9U>Hb)V+`rt@>E8{GA6xS@(Y{EY#8p}6?83|@PSwU|vj`xw z>}u3R6H?*wD$b)y170^#)?%=k3EAf`m@S-D|I7Dx2OeA`f;B86OS}k<&i{DjZY_MJ z@-(pnlu`KEM?Ewu`0{P+8U76SSgi+H45i9l1iIS!jM-@qwOi-l%?4C9%!t@Yc<-sl zecwU=+yH`%rIqCPgdHdwN&C^!qkQ4wT}HJVYn2__)I`G)KHD2fpVOQqydo$fQQOkL zyzTE*a>yPD@k!?K^X<9N<}JRI$A6{MoeTi_s6-r?>NjYb8csB;XGBDgcl$ID*|ZWR(39tYk@R+l;HXtlJc!k#-&vzEJ@l|ADqUSK6NZ-w2skbXb{qwp2q`HIz(+M>w520V^6I_j7pxjVK!K7 z<2FT-Fy8r-+jXW`_aHI(MEj$L4!lGjnuRvrR5I9D_sd#*s+~oevs#vGEPd*Oy)Vn#^+g85aJGP~918H2%sf=mEbTwKMUjYV%#n zB1(W(zkzk5IJfbn#(45b%M_9PDeg;I&+bn{nYZ^7cmOA6>tT#40ZLK&&LYPXWvm1n z>D?0EOBU86syXfoLx@y=zAI3N%OrQ*d;2rwH(I9I2Ux=E)DZ$x@kLDbi}!4Os_CTo z9*cu}<$aEGzTh8hS}WFOt>+yUjMKDuJgB&n>p(DslPeY9&Hy+z6Z<@?X^T$SV&8N{vWukc!htYD}+9J+0-}OY0`rY>}IgD0)1nxhEpj;KR zGc}pd{SBgQQfZY$=|z&?>D6W23mT*@O(4Ne$%+Tz_4My=G#X%;<%Q9NJ&pU4VL}2yRSQ7JPN0f{a`QgpfQ@1I5 z8ri~oYHfdnfUOu{%}g#AdRfnpW6Z#l;CW-Dxb4p?`YHgCD>t!DvN|n&ucrt#)4Wyx zJ*)o5Men%83n3=UXSH6NzxcHQvN;YlzS5yvF1DVPDbDF)1G43h8X>a>N<2~07XuNp zakVsYH)(j>CG$3ELXD3gU*0QM*n~bz$D|dV04dtxX1cDBq*pR7b}>@j^t;;*`{hVoZ+#w zSnF9yYnuc(Gs^E9%HPdXrfHD7J+q~!SLov!o0zdGV|b+Awl7~y+NjvM)F#a)win02 z9c)@ClAR-?#-1%@wL=1l&Q-j0uLu5ZYyNieKZR+?oLH$KlpjTXaobw)WU?u(cV^=| z+cmtzw;SeXBrm;Z7hr7oN+((yYJ=jqVQ@q@v(cLLl_ zWtSoXgXsR}5F4$d-z}Lv<(5g0*!o5F%9Vjn$%FnZm=_T=OX`4T9K@46Q=1DCzC`0K zp|FUAB1#Asz`9x?+|e^qXHV+>N-4So{z&3u|Bf|OS=mpfXYX)of~YScaW1&(v+TYa zz4;zu4`#ei(Av6x)#AzXmRmK7b4C3fgC$~~P@5^r8D?SUx1Y&n!_LICaK7I0*az!A z34F%6BVh>*zNRPJ^*lL#Q+V%~Fnft_F|-;c#Wpy0aeKutK@u6CUM60!S%v(x6ljGT zvXD%Lg_1Rm^j)A7R;ktBpxq8c2zDgJGj~0y4qr3<=+}4qGy+gYQ|1z~NnL=UeA_J= z{;pc1yE`Cf>=h1vO}T5=dZyWY$Q zQvHmmaJN5KwtZWa5k9z2Cctu-t1Bp6M*O}Ao#9tH^C;T10d^NL-?`MR*0gNSQJCI9 zc`L|?kUc?Z38KcX#v^@eFj{WFZ?|e0{?7dQ*N?E4*V7ia*lg%!UdL672YE48)LG2y zfU~<2xut_Vj%pFobKtoio7SlR83z1N^f!F{KX7B|7KkuC^`gk6RaDK#C|!XS3?4qtt@MSmISmW z4sb2MBdxEucv4eM&z_fz1kLU)7(Z^iCf|3OE5ITwgHPmp06kw;RTGSg?z(Gk6=)&= zkSSodl*NY?*4XKF!LG$EEn7R`<5OvDe9MB~KUDfwna`P_e0PCP#1`0^wvM58`6aN( z+mZ0*ZS?Z4BFCDfShQWyowE{TQvQdQc}4WTu$0=`bi(3_9}!99bkW>1XZ z)1-)?;0NXfl}5e&L7iR?81P)CAKQ&KJQCRaUzPP8Fq$ef&KvEjA&e4zyxb;Kp?LQ`1eh z*VBk>hy$^(p4-SL!v^`3iU7d5Wm_&k+%G1#!E0J^%Sh!G^-~u~B@>U*t&`j)bkiE} zGi455$1uL|QT%Y7<|y(J*E~I5aWY``tFv^|(AQo$J7ATnxh7@+_Z98K_;fY~r&se9 zXRc@mZaN;a@FZvJ%XP13?9AOWk+! zj>r2CQJ!*q%b}G~;E}fPF#ocYDEiiboAR*AAg@y|1rkVxAVEWmg@gh{LhuxV26ty}X77FOJ!jv&&prE@=fcBUtS=8N{nq-{ z`@a9*zf5J0y}{zxDuZ(RoBjbG*3;Z^(swP&7C-M4Kgs&w@&{|W>KR(1vmm+qSNsfh z%op8y3bsd6>neQ`{bEu|=%dPd3*E>N?lQw$Yix-sP1Gd-*_vy(^ z&AybtZgN&;a9Nke0R~HQpiTQ(>vB=V-%_d+?KN zU+=NDmc7=i3789d6ZwZr;w1FI1ozWC(?FZnJl#mO&=;l^*@8lAlZ zc~js+ zfUacdT#A?ng04Jr;pdMBKEi1Zo4(U zui1h^)IKMMy#u+UUnD^&+O|$xJOLI#`_@+%9K}W7gz)o(OLn%?(dc_o^t3#i^l-}- zmz(mt8Y!ZACt@e`d{%PPt#Gc0bG;48lv$x{k|+}S0@?S(H2l8b2Hj7&MToP|J3mto z+gUq}0%>&EpNoHroRn;3z|4uU1>C7`W-rS2x#&pK_!pr2W`k_C(-!;M6HW+QhKtgg z)GTCdOsP%RATpgY3ukEV{-S*uicT^Y5K^zA7h|B0>IE2#d3?Ms0#VD5G$yh$)FBOl z)0)za&H|2UiR$>>nl#gdgf+0X+gAu926C9fEaW)q8}4*5)LGI2Bg`Gq(esY!;alD9 zU?)dML!BvmC{GuM66-V0W~f(uvOK6J1f7&EtFGR?=%oK>tT$|L_rB8*tl-IbEc@G= zr7)yjn8OE7jV5jNoSRuMfL-$tjuZY?rN6iR=2vc?(#=Q zchSmi0}OFgv6lP*ozKv>X(0c&A+y*dZOtLFO|**VShJi9Ob8J)rIs~M=Jdwci|ZMd zul`YahD72OQ)Brf!p&Ra5l#<sV);>5Ul{LIKwWt{f3nb=4#SjJ|Gk;K-K_c&ciy&Owbu_@4#bc zmsCSF-kUsy|QBa*||F1aL&%Qbnz z$aCJkgFDf+)=9ZaP4a_%?i8b(*B;w5xp3YztS~8kSN^12S6qzJ@o~t7vs3Twh`$ph zU1a7tbVtJ5X0<}Ooo}>-*~1(*Mw?VvbyunM3?K%z8s(l#Ecal~ZuUy&x4MoiQ1Lo> zkp_#7!boM+mzC*xdyK)rL3Nf?69I~j|B&jC+4p#UFL@>OZXTw0t~mSc&CsfJd`W&V zZ8T`Ifcv`R| zEe11HyByA<$hknXPOi$-a2Wm^lbd#`cSD`K?#uSBhf%iIXo%*K-9x{3QnGKL#IfS#17)59njVMgeuk8EC^xPRtpSR})R!l>s))j$v9 zMhTjN<&D(9hhEOg#=PT~zx#?8K-4iRI>oOsk~*e2JH7{Npm@8;)0vL?PZlPz2KP&f zh9;LZQ!u`i7NlLyXY1R-P`K!PJ;O_IFp&FTTa-V=D%etQm48$bpKiJVf2!Bs*^#L_ z`+OngvNJL44x`Y!B~b)};A+B(?N+@R952hs?m=^B6lZj|6Z5kmWtF+FlvmVEVXbm@ zLj(8lF3PJaR(Gi$j@Jyw(PI)vMYJs10`ZlaRUcrgSiBFs(`wkGdiR>z&EJ_yX{3N+mC zO(U-!Q|v5o~v^n5SD(z$F4Tc zaBUSLmi?1}kbtmXc0jA@TQOT(@NhjxQ*G(Eki>KksR2{^DJajPQ18&o6daLrfG%ZJ zgR#MfcbUaCR5iaX06BLP^lWwnh93dJ>)YEZiMP0dvV!Ht19Jn+G7ye7;!Z8MgIWXy zg8XO;mN(;u>MKZ^GX`1EvEP|ag)wi-JX8Stv8RA-Po62>Mz}zsd8P9+E`LLM&+3eHL~WruC*V@ujATfL8N`V&}D1#Z0lF zui?Hi-#NhH9O8Y4tAbi~@R}D}VEPxgbh+RN(*ns`)@=T8XP?cCB-93mFMIkAeU6vm=`E4FZDiZ# z+))m5x510e!W#4at+AVVWhCHmvVzCI4WbVq?v1E zFJ#WaQF`vrtp7i4>z}+5zL^^@w@}9Sbm0#83-?+Af$6^MAp!^Wy~%(9(GSGQ2= zvu8v5>!0!_R+aJid2b^w1!J;2>c%z|t&vp+SC5tLkVG!+Kp%i|Cn65(B07HkTxS;N zkHOwaKaW-{LAvK|Mi%G^pEgIbv})c;qO9Ki`C!*mZT1V(ZINI8wj(?YoNf3s`v)TB zMPzPr>aN2ab%*+zmr41;b>t4>1~>neqez7@Ven-2*?_jw*T!65EVderZEh(mwa5(6 zyFJb*!4>`Jdqz=CCG3pVT5U8ZH(8Yg~(4AgKtH*%t3udwi3ny zTC!z+T@CFE#pF=tTSD23*kBcDXt6$$)N4-dN1s-jg#Yb5_7Mh*sG%5I~N}Yq7}8Rkj?g1iY0r-7(;_> zyJBJ?4;C_4kq4!vJiZWj#F)-9!bN`#tURoRRWV8*Uh2;MU7GfawX)>&o@nE36G+w_ zF96B|lsP(5fKYTXmZ#@tvi?N-y1K2=91_&qn!CDlfa~&J8B(60HctD0-Y$%3b(%j` zYcp9SB!4t?K#-4Cj!V~6W@M9#le%KmMXSS1H#3?ir6D{TlB_8UrZ~fkQcS(UZ`?S5 zgZwIkrpw@ygpg*oHJO6|ns=C9{w-cvycqsdB&Slhl5ZYQ1_)Eahv)ku`ORnQ>NPD2 zm(SvY@{<+8<}yXu5i^9p0TLdA%t@NP0+MSHN~a2nYJt;b4qKa`F$ zXy0SaK5KzM836tGyv-0It|&UPr4OjC1McZTuv`z8>JBj)lgHJ7bfZaotQNiz-mJH@ zTY?87Pzy) zoHaT}TYV^^<7TUMI+sR~mAFL-FcgNA2@(3WGdtGcI>U&pQXX(pnOm<4hx1NwY_~q^ zc5*zva7S4_E_Uq@cQB9u{8HMn{~a`_GI6~+fL4Jsw`=7eKg?x^GHlFu;;#|St9JIc zhkyN)`Cfi|hhC3#7S>h(u?jE)S^;Gr|JGao)%4OWLpx8Ilt}l(?03qo4*NL+qS^uu z5NYYJ!?dc=TZmnqnklc$dWP)Kr2_pPesBO|u-|nGi8G<%NUB0^K&YC)%ovR=*)p+& zDJ1)sf3=!_qy5ijSwvcOy!sVPHuEwfYkNPD7q&Cd$3l{=Ief^PV>vUbuyXjcW^H39 zhSY^@n~t*lJB<<}yYW{TrQsv>^#>+X^tnp?`tA=03dRTqYy;WiG3vb@xhAJ>?dU@w zB&-T6yV0f+KI@%T+JM)_zej9-*`VIeD2YsWnwd-+MRrmh?6<@d<*pHLp>35QziR+tT=MY9$9&S{!mo`-6V1FDytt76EL?JoEO&UK#ANHxaFw8zeN(_0JoqG~NK0o%&$qO~*a z*>H*wmm}lW@DGwUuoT6zNT%kAnfZI-?QEz3MVmPzzJ)b8n4uc}hLuG*@Y ze_dK2wsqeAGH4hJdh*u(-p~AmYeXZB;%1SMN~5PliY@i|0(+%C#zH5(?G{H6t18430=m#mIU=_x?6DG^ZxV8$;;|Otu!@;@h8uy*Zm*)V@K3#avC!~`d@I-x3$0GG z*z~^Hj@xWO1GnzM9MVHJ^^H5X7=v#%``?*rQs(?^g_!wd;#38eoM#T5Y)_re)Vm}F z-oibrX;MMQ?-XMM4w@Yf3-9ZWcjGesG*ElSiZ{*#(vzQUEI;@3MwHVW# z$sb0$IMmfgl=te{4$JSZ<_-v*I8bdy!|O96S#Yr2*497Q zG%xTU{z~gHlH@i^R+|pP(mphc2Lyf9~A0nXfyIgA{3+mQAPN1+LvaJG& z|H(k4{UIDOmN5mtmI_Z5eT{9JbpX865l}(qS!o=2a z!`dn?ZNW#hjcd$-fspi!aDW_U9$lpO67mpshM}e66HUW|r?=i-VqcMVH125i&nv1& z2uX2TIA`@nA>)yzbY^h_iRQn0SkKXurgtbBfr#O>DS31j4c2}xQ zs`svQ4$g__wx+DyFuy3XPF7Yu>nv)6m7a->!OJ%CO0!+e3k6G?nosXLv`J9EU688S z7m(}CeG}!KR{|e-B7}3@Ak2UT-t5Yrjm?k13P64eX~0mSTDnXFu*S=`r#GjnOH!mQ z+DjirsU0jH4l9tu_(8b=Is=s~WH-cDnOCSJwV)GYUfWXPKBCF@^?3-?@NXrZ=ocT2 zCR|E3l6%d(cm+y|C~*@IHMK3DQSk$@Xdtw-VWq|l6cR%8*-XkO>Q1avS*I47e@eft z3^85Wq6svrxs|~qpT6aE8g>cy6Wv}V7`Cg>QJX_{yQLj_3qfJg!I@O2KUgGIhXF^j zS1il0l0;&YavGWwYUk6{;co!uj0hNH9yLytV4` zj`{r$h84%yf`S{J1FqkY5YNHAQLCM>wMdx<_d}ajCOn}n_vWSTdumFGN<%?|>w`B5 z-*u#&@Kgd{Mc4`#wKRAqHVGcf zGT*sxOYbQXxsDrmStw4XvOG&lixIp@zC-G2HB!DeS-INCNJX~O3$9ch)h|qn#m#m6 z+-*GTdwza%+$!6`@i@#l^I1kN!+^m!(Azawroj}lZ+Tna)ksRuxX(z}-%TS%RVgtn z(qKII+SnRQ#!DvG=cv9d16LeXkG|C}Kh-j6jo?DCN<_NF7>Op*q$;|I)vKva4yb&r=LQ&5EqVQV5Z_Zvhj1^4rbJ<3%4J?|h>f^) zgzgVa{|{qPuJPLA+Isx!{pT-1i)M9g@w^%x&Q*X!w9VWC!#N{zBIqzuKU`6#H*#g| zd}y?gRj@#M+rsJl#|M9P5&dt*PCs1xj_L6ox1I5T*{{HGsQ_!%t&+OIqc(?2emu?* z=y{mhtdfN)KQHT-NV!5sleLu@t1a6lp~CmeP4^p2;rSkq_e0Pl32_EZKbVp~JCE$} zS=^cc!!kiB+ZBSRa@*Qv50FA?Q^@tQakFaz29CgK)(h#k5*hbx4>k8#zf^d!jXzNq z)@ob8Q)@dkU+l0iS{pyGxQD!(h_lxeZk_3tavpqB_-@gggb}g+E!vySMwcwU4K@rl zR0+2Z*PN<-sX&qV*USpF8?gp zl!vNuc45ZO-C=y3Jy=?#|8cR>n16&WiLAmL0}uw@&C&jrrLvo^5J~luRoffBJcM&g z&7`DhX4GbAa@L}cCcRbPzfDdQ>UQL|Rg6&GwdDu26`O~y_ti38B7NvRfse2t?T3{P zn*}48(^5SA&rlOqCka`x>@f#zCkbXrjM{-Me0%dy%i~`Qg>!KBfn7FmK>FFtL5SZ_ zZRN%|=et?GbHD|&@ZGyqZzq3Pq|u2NhqQGtIKqixC-wnzkpKM^RRy4P*=SyGf{my0 zdnTWM9Or+tXp%TLE63oKbA>1I{la4@+cEExt)EE)npc38BzxBmc{N!`w4JPKJbYUM z_6#qhu#RN66sgpEkps!F4{>GnkWm-yJL3-~DNUjk zOh<5{+HCbXpACZKfAii2PfF(SK4_Q_^)T%q&fs+~E+;ybWWdc&Z-$|XIsK^LnS6~q zG_tnfpBJ|LtQ_A@N?DnReT_jDi;Qs3iFRcV!%jQGV@I*`(CbcM=jmgIZ9k$Z8Ah^<`Px>4KRzFi}~caF-FcNc8MhSY<}`| zNh@K6p}8QCids-C#58#B@o2#?IC*yKXj#{cT~zE`-*sEh?!X|+a+_^8*eeenAhzv1 z>fTVNuBN_tODC}}R#{@ zmJi^YMys?m@(qa)RjkxzCffU+gk+$ldKg3X*0gsb!nPJ3wtOb6S$^Eyk795n)-{Ze zXh&>jG@qQ%*J#=BbQ2rI+^e~xQo`|YCKkWC(BupMN#?oOKe7AzbHd)(;R->q>~hCR z#o?cEr{8J2ez%p_c)@;rHflJ4&drJRihULykMyX!__;B>*EC_M4#$&F2zC=XwRY|Y zEH6H3=eRl0yR9fRe2RchK>D{%(R!5==#PKis7mPG6+>3gUO{b_z z+UO)++*-9;eJ%?5;98-)l&bVQ)8LoWQw4O_L4K`6QHDdUe$mgBdxu;N8wyPm7oG{> zDHFZVjHR*SUK3|I7YvOO>$pch{{rTFrYpDp*^D?e;k;jsyhq^MgvK|O6g9YM&iwnBNwHy`)yAVRi}YN|-xNAw1IERWvHNImR1~P3z@)Ee-085ssH$vL4$mamM-=Rpam0Ysi)xkX@E(*I( zsC75=RkZJ)ASt$;okLTIj6K-0e6>umUjQB^L7wv{ND39jR)-ilSWnbgcm8sz_Ft?| zzDCAEYVyGd-1Rq_3u(1vX80YF;gk9cdF^g$vGm(?F%MUOW^k6@n%6h|noj>6Ep2 zMy;P~x5dIsliS}59qvCl|9SchCMXnWe7t}-;9fpzq4@12*v!nOCi$lUOA$vj4SUtd4Bksiv@O|VR9~(!K$=~<1*J8D)c73k49kGj z5H0os)WC^`2qa;M!IFbNSXVnSPO-|f^-LR@N=atC)J%o=M;dD#M1UDJig9yJ1#D+laP2dIsrEap* zskQZsH~27!kH7z<@LJPOH3+Gh-Nh)Y4d%^e7Mcjrw2(cYF!L^RWZ85(^xXxBpW4Qr zOe$z<+-_d9AJf~%>$`db8?`L^*j=h^b6 znlj1<5t4LxfB;NY7(Ep>f{IQS4fj3FfAv#-r}M5_6fLi%Z{MF19dskL=Tb=9}IT)WUa#-%EV_`|-M!-GD6kH$>6)B zA}XVJT$!5_l}@5bT_9tyV7y2dUsHKo;Rh27Pphp0!;x(?2~vvfZCNaQ7I>>^h14Id zT5P^`Sgx%6dcL)6!?FyV)`L~HzRreI9=uYUzV&7FBj(ZRF2-uqOs{(f3=kI(gysM8 za<2`(|69Rx3n+$DEuv5^QmsxQT&VB{wq_+&!i8sk9l}(Z9P=^E$Ver zV09KoN%f=oKUUJVrLjk$Fr8ii2&Tpv;CXIc2K`eOEf&8Zl=P= z?^Bfkauul@wMdn1z9Jvs1vj2=)v(xX-J&*a+|KCnt0D&Q`Ro}et~&~ej9P1?l5#F1M zj!J!CwwwcQIu+yL0;AEam-xB^N>Tmlm>(}${-8zzIxKw6Jxz|#K-MoCS&gxcbKovJ z40|nndFl7#qExF^Z#(X0zsjTpx%O-AQFvGE1NzOd7TZMBuiGCI#OpU#5mUB;fUh4q z7J|XfnU?RXGW-ezDwHTn`ZXwNXXr(7C!s*=NM2;shFHOHDj0l&kj+D}yh{%6)1G`) z<$pv(&%s&9Q}IO_5%PhC`!nb$70s08^>RKc%>~BsYde?sta09DljWZ z;Rd6va6nexeiE&ceL~9FrQ@0#}@V=-vr&s%SxZ> znswGVWIo?=|D6fo)IOC7xP+~=R%f>;BZAXBQe}~;ZU`nZ|2`XtOD9yx&r)IiX+=PE5jVd<}KQ-v#k0#B$UI>OK0hG@C1*~s^>_p5c>zE8abw`~Sc52RZA()*Hr%7V1-_G(k( zlV&z#{pe=HG}AYTo}D0uabJKzEocygS-a%JdV8eBo#?j-WVH*-MNT3%PW$>*)4Jzh zThe-q>2nv(o6oKQUzWg;4a@yLFY1AhX+j(db=nl(zB#C(QI0vy8Eh@wMJPT@89Lc?*h*~z^LP*#?KhaYgBQR`9ltL zPYIruW8)lBdhyzvF(8oKRB=Hpulv-RGCb8YXD`<1 zn*_ll8f`qRS(E`EySwo2^BilB@2Gy}J~NNMXA|8`S7K?B9~*W5A%_lDSm1ysrQ}Hg zAi`MK-5W6Cbmlw}a23&K0B}PpyQ789&HBn_I55Kx-Z80t{`h~6#s8PzII0N9g>3>R zeqCPRFaF*(LI(kCOdZ}ZGS*kV3y<8YXkm>b-voLrFSBoFS9 z-M#NX_<+tXs$t!qb)u`C^D_(lFg3sYQMq{{8`pOdqI?4)D^Z`vRY|?+;a-xFyINV2 zJYiV!hh5#HH#5L&Xoe-TutbGEl#ZuSZkAdTp;HpzWFvubbLU~+y7BLI(fLE#3*^!j zvD_(#s-BorXO6Y0{dI6BW01PoFtPD=&e=QHu?A#190+mD4}4ho(WQG$*m1IKf^N@} zwle;~L_eQn9R3$j=5%HoiO+G=vkpOS3MA`z>ar}h)RSydrO`du zGdo~&mPK(wSn8G_2Q?4-F|Z>4t)r~b4#AKoDE@?rJ&>d1tDlUo_NXqY-I707ga7Q} zhK%Sh|4E#oM>T;iiZ@x1?)bmX%af;>rt%b)PoPNkj_D*s+mk$+uMeQw<>Z`c*boUm$-G%6 zh@VB)Yyu=nR-7J{6ZMrgZnDS4d2*GV21c^W7UwR3jOansu2>6Ul7Y1jd^(nNmsTi* zW8^RyKLcRom5-V|}2vdA866FF3nGMh8%u3S2$*lQ~m> zsy3oZ`TnoNjRKD&fBW0?w=0FE3}VIDzg(-Pb|>fb@26x zLWsPnn^olmr4Tta&LHq_u(@sTbMZ4&>|WI5xwv%5#m@kb_=DGp9BOIT<8+};VE9o0(s9fZbFCGWvYi*ybx%DzRfM<70*pH>mnaQ4~m(lW#*ScKc( zTdNZ9&@B39fmKNrRF} zLPz;ca!Nul-iTM{HUYy)x64k#MW=U;@#G0lh+2R*Yxng14_@Z;OI&k%F_=aLf0@OH z&s?5%(F7~K5NeD2YoTUjxUDZB%T)l@OD`{383;EYQHewGJC)xn}KciLa{Y1RBi+)I&Pb!KrEwU6S!Q% zWGbq=i2_oN`{$*OmnWx z6#8;XB?H;4^CSeUa!Q>tY{9T)al0y#G47cukDhh*Q4J~2w!+S?DpFp6LjB;D9hV=; zo04+#$usTg%%`FExz+}Y3L^667cy^*iu{4USEHc3+f2S=goQW<6h#)UJuSVynvgR{ z?BeCr{W!b$YTQGdU{01RSl{p@hW29hKK{YT{M*-`8+QD#c;vjVHe;l}mb`S>dSk6u ztx~^o4HA`exNLR;Xa(aBCxrzt>x$$Kz@&dy_?iM_65T564IO|rnh>0<#}4FLH;Y8i zMU6glPEde?dl8)}b`{O8JSh8<8r@U4Szs)JNQ2DME(={8cAX<4B{P0!Iu3|n^_-hl z{+-F%lm+>aT#h=uJ+ZW1G(PxB~OImQyZz;-oC8 zC7cmkFr2M45`5=YQikiLJ{sBODA&>o(0YyU7))AMsH@Vqb4)6BGE{cFw$_8(Dh%=x<)c zRwtYP+l%-S{!o+d)?8B!c}PB}TTlP8@{sj=?Hfh$y5X^M@X?Uc)loTo$`SivzA`oY z$PHmXjA;rF@Z^(Zx8_3TV^=tYgcxJa(pO>|j*@9Ljji}&nI*Dvp?)W@_-Orz|KK3& zoIh^w?784*T4Q6|nqb@8-nF)5ViJVD?Po<*3xvcF zl~yzuYMR!Vg_dMir9l#QxjSy9zJICwzxhv$AfUsLwS8$t`mO!#~kCi$YY8whs#;){;)&$R}UiqB~(US{U zX?9P5>ukvX&O{EA_0n*{Nsz1Q(t?&hRQ=BM0%FpgYyl7U>o=0lNVlmw;6Ts|*i_^r z_C}k!#rHVUMSUz^`sPJG^iEGw6keY$HuT%3J6%-8?$^K4V-zfkm_i42FmGRay}kaq z(^J5YJN%xhHs)bJ$?EbZb~0Pveq?0}DUPn9pr2-VQgllufRRBn9rIGV zsLY0X)I;`?l4h1u?JGTU?u$~M1qSyIdRgq{^$}V#Ee2VfN5@Bl#A161o0aC`!BD1& z%l=b`tOi2DPEU<$Cll?kD^scWLt@`GmD^w)FUzCHTOYSBza;fAg5R4gu?{GqNAi7L zxis)!GHROxgJ27k&sv4KBi|}EiBlH6;2u~GSeF`qun}q&-vrS+GeYJH>F90%ppQj zYT!@cCtxzaM8$#BV~&T}2~Gk^z^mWudz2yvbpvHbpw$Qgj>&7f2Lg}BIRCM4{wciJ z!&bImee((#8Go%C*w!Y@arT#Sc@Q*r?Tz<_j%pV;v!D9@q+D6 z2k}W=_tU#-)6F<44{D20^Y-=pyt;E}PhTeuMV~a%pA;%mX>oVdR`=_Ajf#)An}_m5 z{?25+4{BugLdHx`&eZQtuzwDdBcoO@ekW^_uHRRMXuD1@-Io2~pI=T z`O<}bLh7om#cBpVV4p9fWRT_8b9D$#N8}-R5OHyvMX`JXt1MIh8HhwaxSh)}N^`}{ z(Ou?_!paw4g7&HjD87=l8vA%C82Q?_MZvKbA(0|0<25~FUXa-?+Wga8kn-A-X`4p9 z-2JE)Vky^&HYgXzyYygT5wfpZ-O^Augt`}=pMrRrs~I)#JIk{QMsvOg+XmsVB#M)h zRZGFES6{Y$K5)wok(|Zv%dLGeW{oLu5fs^1V0O=XqKD#6CKrRpfb-_t)n2g6QkD&^ zaRR*dV4$YojkSBm9j1pd-90E46@}4RQL*Yz8heyymQj1_ z?(sV^0^xxbjVgzAd^3gDpjqy_X_;XKo|)=c%spY$;(EvODx~H1^w^*N=4wjGL-8e*MJ&FC;HpGqyGS|zVT>Rn!=fTZy$C)MO%cU87bIG} zf-ca%l~^E=6H#PHaS~i8js+Rad_Zu8#b$h{Um03&B5Z40d}2U<33$z`5^HDrd}7r; zTNy2NPB=r7ON8+yA<^uFC>yj@JzzajB%{z!C;sTkefM5sqDX{+%u71PU$?3v0DNAG zzF7$`@miyP-+%3sr#E;ofNN`S^WiTmq)AYlA<738a7?c~o4&y(8~?fRnw>mM{}x5r z$ma1(60VLe@UUkBX|KC&QMpjs>WZ?7r1M@ab%66{b0{^nFzP))g)X!K{*})?^RJ#b z87-4W5$dTIro)TNz*!&ooRjuauIkIWX|qI!(#j*n>j2V%v%aa9tS;;z-P{-8(sH8 z$Pdgnd;~bX39ytyV^K+o0$7K|oYniG@K$57H5j{^s=ENr)y?6QV)QO9uK6pko6tWiy94GOMi(U2+t=vNm~DFs-V- z8K(Niv#%;7D7x(X!2ScrlMB3(OC*p0>{8F6SNuUu?!l8*tB*M zDNFP*o;}b#C^+`e0DLAP-s%-Jcr`Zefh-*k3f8(iK7FKZ&hu>VlvlKih*}C#dc~Vk zt(1t1vmstvxas1t6jyXpYLVLPe(i|tDp$nSVY%2Pz6vENl2Q&%C$xVWe@lT{FT6X&vyZ+i^KtiE4syN8A-~0#ePd=ok#MP zmip2()YNI6^z5Km#IqC{wJSn44?R{eRuSOuT&BZH)cM@F6QF&=`gKvkyX%pfyhe9x zgx+I5r$sr-7m@MNvE|D?1>!@Qqm~>^XUKxIyym>@V&?(H6(14&)XMpD&rIyw_^4OY z=xB;+BnK)+@gWHIH1fU$Bz^6m^Sbx4nH5+@>USozHx1QKU)`FXbEf5woq0d(F;V@Y zegy<*?QiVBq1Hw3D{0cxyb@DZE=(tCYwTt1N{8E$;Foe!2v13?Xy!U6r>5S?A4Kz3 zqjw2As2mcKlU)>nz2zCu6_ogT&f1$hQ|oRKv?mifE;wwW&{niKZDI z;7Z|7DwC`uFrdGo75aG>%_IIVn%ZGS=eZHW5yNDp!;H(M5UioDjn>XX$9N$L#Rp9{ zxd0lxav0)w_+fU*jB2H+gvnJ7`3IrPUOWR;*n>HX*sI%Aahn5vW{dn>6w7f{_BnPY zv#;pxaw4b!=b{^CssH@^9i3U?0jmR~Vb85NkpV!y?Sj0~G2s>xnE-1Wf#O9yR&By3E*!%x z;wc-{jIioaAiWaM7I0R)kc{nX-%9O_@96It9)1~y=(%quQ=)sLxol0Sp)2f>(Y7ySUE1GR2VU9 z-zp%dqTK z6oM2DPSHSs1Pc;uiUks+5JG?;1rprd`(@7FXJ6m`&OZCv`#RrQe`Kv|B}?*VWv%yp zp8NUTJ2}1c-^Z4G$F>Mp_oQ-4vWMO{1~!0iJF8ZUUq92uxNaVAZI8$gfnLdw%9|J3 zJ6|s;hyK=wvjz1#DV-OeeNJ<^j*-k zNCyv(o0bBn@lqgI_)jWmx}U5QofC8mzIw(Leu5)4QuD zq={E9w=NazFz}8|P6@NmGv2!L?TJy~p6kW&#qzzk5u<5kt1bG!Gbok+K8DmR+9z9Y zD_PFH60ml>3_@C%|fNd`pV!?k?Hg@&+lPmhhDpO$|sX65Qba8Cz+ikOqz zj7TXsBa(hv<~EuWlq=&GI4nBNZ+m#?VL5Be6|{Wq3j@O+Q!Q^ETqmjQD)oBq9*LfU z|2fNd`}c&rAYKM~+Vu(tuj&gh$(jLGPq~#Q+rw0Ylm+=Py3+H%eCT(&QTZ(?X$N5R zL{1aK&bKh4sWKG6{qCP8`@cB-pF{{;A^sOCSI3S@>sq(+K`SBtw>4+QC=SG_*s#*x zFrb{*NkwqxkuRvO{%gMN9LaQ3%_B5-%yV~nf1_dUwu<{@9Q52lV>OuT#BBUoIAaiI zx+3?}vNQ5I@n+#Jz0a{OKPaK1k=rb~MjN5PHbBjc^a=yjt5BCy;c3qU^E&w<3&#_s z5O3Q{rDzw#vA*5PxpY*wzV&oXia%iXNp^oqQ-bkJE}3Oq65y-*D*e_x`7~+R?@{x2 zBHA>rz0A{-k9_Q6ad2Gx`xQK$p75(83i(N%B7GfoxGz|9p&xhe!-KmkZI@mCy8i71 zMzIyt(TT0oq4;ZeF>-$Y1*mkdUA@7GC&)4!%%_q^^H@txE@^GGic{&*7^+F#*I;)! ze5?|hf-aCFo>rK0&y;?RVqOt}LaA#ht0&;$xJa=0hc?&SYsD7|aZM_%N$lOB_Bg=@@w zgZe{IKRIwQMxHZdhAz5z6K9Ci9E7|d!4(caJ-hWCb}k0s`8>B_KBDut#?$?iuhJd({Ahhe!{Dl*#EL z6RbF<(DcF-7e;H}M*m=7o9UnT@~JSf{}b=xf^_#iyA!@oX1ug&UvR|aiBq_S!?rk; zng8dKbn$Ax^scep(Q@4FJR{m<ZqJRZ zf9D!s_|J#_9)hpbZvR77+6iwLKh`*f!!Az3I~@O=C%yCk_>dIs)a{(@Uysiid{34~ zrolt0K~0&*-w^aKdoCH15oN2|U49=C?5iPrCU^Y!FKbT+C z0qCl^ak136+$!tPV7q(23u{PmOy6rZ86mbxmdx<;uLcTBLY#zG3PUQi)kICr&LVu0 z0$++>OR4fXqSpcNwZzAtbXYrqJRKw3LZsxHYw2dd$w+*|iD3}NK6t+6W*djUX+{rG z_mILqh6ttet{H*P9rt&5r23uIaRT~kVXt_OizmP3+G%QM)u!QErvZ6No|59=$%4~m za&(rkl$30AC!v0#!`iLT%^Bdkznt`~TO6>^nT5DE)NGgEN}1Wrd2Zbqu6XMTY(cQ6 zU1)N_s%Q;*zUSHeg=x2=Ud(^mJ;iZj_7%TuGalGHxh~SG>SHpU>OO`kd`{+=h}_fy~0b>&PsgJ$}=$in_TcUF2Ifi_;ZCuIrY>W4qactp_h^;$KRdP$7-* z(w5jyL=Fp5-Ht{2?5O9y8QYX)9!L+u2Nlf{hU=22si&b+;F9A`r;10 zxQ!&ZuDhBAC>X_bYp5wZ3VJV z1^`^X4zi8Ica3^v2m1&PT$P%~s?P7Nd9&=B8VCEE=d9Y{z~(SsF6s4c1#1miVeaJ) z)JyJ9a%!d1tgZVeZN!}BzM6CPP~ZG&jBB>9ukR65H1wrX@J=DjL}|BXKO(jIBeolQ z0vlGSgMc1o;pe*@^?lJ1YjLRvjMzRwd*?(&ib%Gz7_-#ukxbA{J)?(1Bo{)wtM}Hv z2Ecyp?ZcIPwQ|Q~skoN*=D`;{g24s0H`kxom8o1n!F+{Zns;ji?-_yRM~Z>S*=@T1a-?647=nn#M7$E20QBeG+$&pS4uC=ww<0tLO&d6+U1uqwxADln9x*s+n_1kTsv+U{!%N&3ShTZuE7AZN0){vA^u)w#^P?0WpN9oeB&9;ihPi~ZZBW&4U`4}z zk21I3t7-k|+tC|oB&6mM)5OGcAn1{ATS#{1!YevtKqyGF;21%71De>)brRNlQU2J( zR3la3ASv|m{-%N+P)q4?yirctxneQX=@9Pqf&4ka1jK*WHr9M7zQ@b7PgDuVGgx@} zfX*=d6$R#CPj8rD5A^JNlN@0`3yQw6{iGXjwidxFD9Z!~$Ca+gX-lr|5BNlSQ0j6& z(}yFJ9YLvQ&gGx7cQ<+wvK4k}$qE zIa^i~9@#ZEqW~RxLWrGe_9vjH+;#%#E*1k2ra&3H9zETm0Mfn`R+_R-yBd zAC;U$xfd)7mZ>vQa=HOlvvmx4A{28#4SlghBE399M4aDE$@q3jUFX5`%b#^DSJkq$QRP@m=h0N<3{A;(%92^#Q zjOcKu?2(<&kI;nnMLwq#3O1BEsu1YooDj@kP|Yha+h<8HF879o2RoU7zt5ASqD`d1 zugOepGkb4`!iJh^#!( zHCe$9(#NkR<{LS7eB1d?n(m>kk$eYai&r4e1OkBlpaF#)d3+WUW)Uz{nXkdv2dOb< zKU7!^H3E6$b^+(T<>aCRjrn;`Xr!!&>{;kWCk;)s{%!FL?ZG1&mbo4A;HTu@85nkG z#!Uo!f9;}NnCX`CJ7KZ?Bj!Z7qqUk4MMP6tJk{lUY$K=2JuZiO+n0pBx@oR3X2f4D zzv@VDb@cRN%Qrr*2?UYFe;qL2=7gqEEumqaqZr{QVgq&)`SuJrU1>WKhf7SPmxa1; z^^=zuid_Vu{$>FapxxBe3=Lh)^7f8Zo9r!Fo|PJ*Sfuvr%IB1Gm0w*2(A??z1|!&! zSX0ke{}K`a)8-ZyjaJ))G!In{Hl3bdHV`Lrq-$9`*OM;yDxcA8iEy1VtYV?~#D+Sy z{_tGpxXi=*oMV+j*`~}FH{rJRWYo-!UYzDuWs<_ZQ)u4ez29qRc(<%9c{Ei9cosGE z+10%bU}NG)=Pc+U;I4E72i~1Lp(Hmp1s4C~ip*R3b$VS%!No5Sb>C0AWP=48-2d(# zGtpR2uTen0)dAyDei&_RHJb7zGjl`}McPn)r}P3@t|k3dP0cyU!nx(1mV|Cyl4srL zYf;8}-Qf3o4~7AH*x;@eB#zadY0A2q1TIv5_xm1ol)5;_HQ-DuAQg^Z!N(f;6{VQ^ zd&irlj|AaMW#{K(|Gqx#Qj(+iTq0CV_JmHLj%0tw{RjxH6po46)_yt8aPKabLh1&i zT44=chYX!cI+nax-wuP&O~DK!TkO-cocq6DchQvQMTVB;f|1z5n#NK!KXu_(!3+U4 z$PG)k_nF&CqkGme$^a--{lShVp94c+$|LEb`Jxt%(VhL{9}KjYmrV4P(63*|=Ce75 zaeA58d!EI_K8Q7a1-GS#$ME;FtsLote#6yhIt0*ycRZRQFw(11`g&=}L6E3Iu1Xdc zhTjO9U^j_%rSB&W+Vk)UQ$8Kv_Wrks$NBXC+c^AH5QAM!jJtO#rpH57I0^7_;`e)u z$g)wT!hUz?H#f2=nAL*@)WHlp=S`pOR6_;JWWcMwdM?2Q#Osz+vPR`E+_FmURf8M0mC-uXp##ah4aYpk#69;4!0wLI zk*BIyGXVYm2g6|b(ZW%IodAL6a+X_s?bMV`;{lzkZivXW@p=z>4*1C*sYG*5W)|>) zYq3Fob0p7tCl)e=E4YS00J?Mfsg1UZMm+p*m~F58vR@7?k|C%T0)Fj#fzVxz8%lTs zgX5mp_lW6P59UNYs4BiDKy?Au50C}KnoXuB53^r3L37inwR;u4=6->nV_;=`pnYm{^#El-B9F`p!&S^T<91uhHnf}aX&g=`5rB<1CCJ|?O6Y{Vs{v}d-rEH{b- zSau8&cI?>ntnFP%=a#`dUFIF?#_YKTi@+4qTl0uk%^qewQQWzfHd3?^jn9fqE-!gkV%*cYCUVy^b z+}47?-s$gUQ2{bM@h(J+aHus>@7k!;f2K=h({?EM!G(mD_xtk^s}i zXYq?34jq9LJZD@WZ3TOxo$lW|3XXxmt?^WXOY$JeSJ9Ng%@54-44X0gjbh9 zbKsypZJc(_4&BKf&#uH~Hwfud z2lF;50OQ5lGU;Pmm&8FTsb@}4MqNHbV!AM*C=-Yk$S#_rP0K1E<1p>QE3(=?o~=;D z+t|W{PD8iqkv8PvS9MEZFdSGJ{Ybr4t$*lquqQ0yQBgs8ZnwCbFWE5vdlT3vXOuc9 z+1ARP7?EEDieE_RG?$j|=vEeiVH?YeG69R(XCT9>?;idO1o6k|q?9y*8;qnUvF;N6 z$m_P;M8FC3Bz@21CGa6|fIS{4uzO-{yj#C>%yvMby^u;lh_*|7U_QkrNcAJjkNWV6 z>!6$Asm_iI`wgSnk=3jD*G*wZMZmoMaxCE>bytBp>!-QI6VBo<;0 zY{nFr03_?3Y?A}il!>nHu9rItLX9|T6U;M0_tSRJ; zYryWYlO?eT9u16&R^!~ff zutG>FSwgriM?R)DyG|u<4IeFgYNt`FL*-6x@!pc;OzT_OZtPpm{q!ZLr9I2Cr&=qA zpIX}KN_UP3RwP(8Gq>dwr22ZKk~<{c#7{pDTBocFNsAoZ6Aj&Y-;YEKYc{ZHYp$Ju zUJSik@(H{RzYTMxljEvmE#RHA<;zA8Vq2S@-f6CA{4KWy+gJxSw7-k@idN2)173H(8r~w~IYVGek{H`D3Q#?TUm5UO9zMT!0pAZTpO$dz?Fb}yV zZ)yf>HFOTb)!U5Z6i2`>~urKBq@;)BBD_)$8a-J=%o$q!b0^rx%ew zFP$j5cA`U8rgabw@ehmTMT5@b)?8Dqtf8EaI_MmO6bQFAWHKd;m&L&T?XQzq_tb_v{kyVGwhPm)HRa_g#TC9mxO3p5$dBC7yh20Y=yblX`U_ZTq_8V&$B5ufFdG zQLEh6Ef%yyU%zgCdxX(c-C{lxU&Ny2he`ZB!!{iomG{qmmS!S#P#%$}8X zt3A0Huo_g_9(M-voQi*ctGdqH$T;DKj;eMV()y-p9^ z058ZPemTo;?NhcznBe})aK6>XsT+WTIEj&4-G@&NPn}o@sD|YBilV8vv%%p~mz_?d zY#|?gN@AVYUIbU9l|k3nHbo`8=U@pyX7tB8_)%@!DUmS9$M3ju+{&CVPv&_q1u5_B zZs5Zefnnh;_#hC=phi-AdY?rxe=TSxe9G(;&6+ZR%y!c{(ia4~B?FbD4_T%h?}UUg zn^mna}mk#BOHZ(!Z6^EFdH&;iw^&im+kLmkca$7eXU_yz~sqo|7#XUS)fZL;}N~H&X`+E3t09(Sli}@Rp#&HHecYFGpIeBnv;MWJVbq2H}9uNvpu0 z5<$_Owl=eh)kj}-pRY|#v9zv8&GK~3?QHhxvo2ata*~4j);47SV5YnFRu7tw>CvBa z5R_C}^@nh5LZFq3jEQ?-8s5WL{QZq{!OO#3-goRw!}|?PQ^kqw_U58cItHyWf;*of8uroR&cIzhIx#UQ( zP=nA0n7BfIC{YW3Nvb;$U2n#{Kn8mAas z7IxB)oBXK56-v~Gu{PcC;Fn$u(%y3CA==_KDWf&KZ9S(4GViVcWMgpy*&@|=8KX>; zcaYrXmU_0bXgAcq$6Q9bFsB9*ZsYZ)ZN6?fK-x+JW56g$*wgMP6NR{W!(kp{Q}ozmjhvl@)RGG!1@ct+Y1R1^yb~^VLsz z2)QF5^5L92+T5zR;`Fd+*4vGKinEAD9Lz)ty>Afb$6pKs>^#Od3eB;JTEu9AN&ECn zIKzLVwCKUG^XvcR-tdR^i`QE}YYp0^p?`+6DMK9Ft{(@RFTodA|F$-^F#mk^WSS?i zxZ9i60eGR2bX+VR!`$ATwMM?r5YyTl1=%4dq=0smHvGf0SUJ*$)p5WgQu5PNhl+ot-j5K(^ zTy*Apbd>XF{G?!;qWk;o5RTEw<~Htu#97eY>eV#T^!A}!{OK_?!KiNMWKiakV8jz~ z`p~-%2Pe3J&U!0*_sLv7(^rM^jzNG)SHg(bd6T3y*DXpxpfzb<$067kAUxR?9gA*R z{g+(2y0%#zIYxeVpH6MXhSR~db@dB$DALNlLI}=b3u+0C%+NX@Ve-nQJv0TvaP+MQjx7yEE1?dvsUt*sUyGgG3@AVcp^rN8=3p*@us}U zJlaeKvA`OaYUTPIoEE-^0gj@aAhXntYdxio?+b+B?l7+ky3mU zzxMEnb!TuQI(XsRXE0Y=&`o`5=STI_OOb09#fgch72)!+!p*Xk z8$?xC`meG&Pa+JkAc)L6K_6Z!|>Pmwc`t(Dd=5E@9PHvl8q=In<4d>TMxF@ zX5QAOQRfC6h;bIZCBbJxlreNUDR7!Pp(?iFL=@G2p)5TD}=N z<4cBIK}D3H*SUqi*O#X(^1D3}VH4;p=Bv~#p4nH1a7L1xhONca3|uzaSv)nJPdBaS z3$nGGX^mz2Iy(rv>S-M+bws&ksU-&b;dmZ%dvJnX_?AxW zn_>N7DiZsYkM=h1Ge+y~Mf=IRXbdyE(2@{3o~+83xl6Ok_9GNwLnN*Dh7e6HZrQh8 zcO?szR!WM0y_7i!?Lp8*+luekj6~Zx;5YF4q%wEUR}-}>Rw5c*w)P4(OUttv7!R`P zjG7D0M}Bs6w>r(;EOq$lmx}nx)mX^l$xSpaHt;6IxW0!m;!)x7(Wg$CkXhdSs-8^4 z_^q;|Fhd?*>k#*jKEeI443$LzChG*=93 zqNr!B>yvYhjo!+(e+IBWqSnv60gFW;;lbf6wK+6c>zT zR;*pYLsaO1=+}xHTBmyS^mP~ZrKrUF39LQc_Y+Um9yg0E8szyS*FzMlwPML^#GRDe z%D64rL$9wXIH1>+ow0X2YE53KnlM%xI;!nV@bijGnKt#+nGI*GDm2}cqcC&08F(n{ zM+Dtw_m!m@##z>%l>X@p3C+<=-Tx}6=N$t#s~;I%3I(QS)0qg~R@E3-AV;M%x%Q<- z8umP@C)9%z4phmNT$3xNe0xhkowk7X^668kG<&GW6d3-S-1M`D4W|0p-jw1ZLa=f z`=KEm-Dh|U64+dqGr?#yZ;#hTj(7Z9{p1SDWP+FNPq3g$SG-+R=OW^-IH5oRQ^OdF;d^)SE8x+llg49HgKA*o zE?Sf7nxuR7t6YZli`9xU;0=C5%arE2ohLkxvoljib+57TUDtOv8yH~ z$evnAn0+y^KqjD{N8fgioRHwB5DBJjxU^N$A-?I%+<8L-gm@z~m(eX?L47~DxhM#@R%bFn*PSJ3!D0Km zNNs-k6F3+dB}W+i!N$0*KiiqEoZ4yN^1YmCE$Y-^jT1~gzCWJi3YJgm28JJm4?PA)6`pfeLDc~^> zXVO{XCK}*9YUz;DJyuysl2bbuRiVS$oPV|NXn*)&e#gl;MW4-FYeWjz?iszrAJK#F zk2l_{vE)c`ojz|Rw7b~TGh)sQJJq`=e5I=56Wxv0V5K|YfZGor`ATQ26ofgTBwgwW z&#pqVxv!?D*fh2ya7ig(a#7vI7Nhm7_6{*QRS*Aordc>ypF9rxENNpN*eI(WSNqfHp-L{jpnOX~jg^hKValw)krask z-PgXFGK|m~((*iAxhmyg8mrw^`wLrnPeHjzeWSnxc&txsLVDie->#+q?ZP7r6NR44 zsi_qoWn&w1b65g&aODM(T)=w5j4BA3Rxo>Am*`J}?S`{5sdANT6Dahy_*x3W%0XZw zS+nt&qeOxm=MW?-i6g}=sqhPldws(~TEQ;x8- zl5)tNTjYVB-)NzL;Wdi{>uGk=ft|)FJrRjQ4v*^kjXuwl)^-tF33e$tbMKi7nrJ>g z;B)IHIH;caBFo;l#m}l%!Y1TKCD=q17y*%e-b?P)6w8(hEpF4 z=gkVsw$!1BAr{kViVNCdBY`dcZXM2je*{zC`!(1?gV*Uaik>$ZqQi9K=i8QollP81 z=w1%(lVFfwZm_WG$~=C?le5hE2ZQOgmpQAle9H^pmt=`ynHRkWJzj=>B7=W!)qoyU z9;_`qM#RA%k>0M)&Q3HRrg5AW{$O~hx1raY3C2h-WlzpBHC3K1iU-Xc)0h)Ja<sxv z!K(8sKMky{CY(DUbWy^Z*u z{r53J(7czC;fAp^?T_(yR*MV4)~)8*FE!yVQFL4#LB>?K%$bsv&{0Rnz<^1|;btq) z0eytFU#tJ+Cef0}&T80xh2UMYj6YpZ9uv3D=}Ad8#YJHyt#TSp(ELEwnm326zc~bJ zlx;g@gtBTqCmY05p6*Z0x_%qz^+_N1nUo;`2L2>6L62{(P;NzD^bzm$9rfS)8=O{4PvVT@1V<}(Q>KzgXZM?<2MtrB z!VHjJW*a?kf;`*e6K=Igb@B2icEeh4VW9B zUT3&D1a%-oD%0y*_w<9YzG2)WxGPx*g&z!j&nGO2K*uDF`wr=QG+ySLG##ihSWt)@ zQH%zQHwp!uw_p)Q(}8C0Cy5rNlX;64M>jq_b{O6Ic#)NMAx%L8m2m~ldg7<};P%r5 z_=rM#@|0ON*41g(Y%0{d_EDcYK&@U37}ojn!nEs{P8K4-k66V74~s6SfzZp^tOR&Xee6Q{}$mM?J zTa(eVVg332^st{)c+!&d*RLNU9u>m4~JWoqj)MXg%35@@H5G8CIq%ZeG|P` z(~V&`<0$ITV^V{M(HIuG4mpJXmXX?KD$jxHLZz!O)5_!qvPRo=4fVF zhLx>W7V>(g@n}Y#TdMLd6(bGYbX6e>)(sRLxRP1S2^NE@f1-$zOixl28(cNxZ{-SC z{rS{bEw|`vxuh8+_DJ2e2>^)HJkv4fE-9#(<&##Fc*J|F2Htl=Wj;Zr(J~Bb7$ms( zRhg%|B&Ub3aarHpIMj2vgcKv8$E65$GNY^H+{287y3=%?XB+uzAhkd)FIwH-mFJ$i znZDfRZIWG2lGtUkvD-F6?jqEmryY}}+f2ZuSBpCkx7hNg2?51o+6u>57He3}`YT4U zvsh}b$34>E&3ok&?4CiPIW$Ml3a_2=+{p@m-t7*sYq$%H*hrc4xyy;z=XvzLKsDscP>hS>bnhP4cXa3*p5B) z!+c!(>*J_lN^UgNEt!R0%g9aPYf!)Mj%zT2+b|n~ z%C@JQ-sG9t4XEF3j~kHQbN;A-eD)~nc155%PQEk{5^SFwB#Wq`?xcnA=D}atC2-K% z0J<`(*q0mXW*hK_2{-g_kmbn&)Y1bIVIg8-?9reDdC8}-Q4v^`T_{kt^?po2)x{D} zS>+U(I#`AzYGv@2=e7wqcCKcdEp+k-F(wD6QXOY4RN`M#a1GtqPfGBokId zgXJx$u!4sL#XBdE6vfbJX5$?b;7VRavsR&zolxPmf*QetlyiHX7mGlTYy1bMvveV< zaSGw}fv%ovOWYoOzst2*D9~(F$eW2tIGf^=P1hJ(9C7`T#PAan1H*qkJ@xY+3@*E6 zDk{_^Inh=JgRyX%-cG4cX^zs+1*@HBDLHXp@rE_p8m(L<*1B?ydu@vgR;*nkq-3|G#4Z7%ALhL%>A!(31 z(i|>vcT{K3aWR3W(cNSm-M;Ad#F~|j^hx`3iqdy7d?jnXM!Nl;xQ2To5RCc3V57f9 zYRR5aPM2O7Se~c(So{fH8672{lNw~>5PZkP5U^kQ@7NcDEY`>YTpG`}?uKOWhlmHB z#%0C6PH_K6sRE7UsMykv<$KaQ*Qwg3f)f``jMm0_mQE!ay26K{h?mjv6&>wsJ|-U0 zFiq`hHL;n}qOZO4X?L^+#Rz=#iu{*AP<+V|0%0Ym6pb&_tK7TP0$PRp|M^sKTl~8X zo**Xdp(5KEr(>ACBtH9%LVeOf$#LUNNQKsD`MgOcYKBKl;+iEb#KfcG6*lWPSaOMX ztRfpZH?2@~aBFe|Q%fpR=eC*AcH&@9v7LjyJ+0DmaTJI8 z&i9IaPg-I=|8IN0|Np#krMsT23#sW7=nyuUSgA`x7XT|3eRe$-ebVkq5xK?ZleUQU zIz*GUlLfiTfd82XtL&&=k(As!Y0hEO?Bq~>dO$Q@5Z@H33RR7S6|Gx`mbZa~j$Tc7 zb|P*@>I%PV28HCoWbUHrT_yB)L}B*n6j8^i*$Ed85I9ik!OrcfX-p+4GuTcH*te8l zsOcWenR8UxIi4&hRbz;BFbJyo9(;&O523wnVHLp0`i++SVE79~tiOu4=?}7PX^J?m>TwexI-Rp}%<{WVA_GGV+Xk+HR*dNZPhup0 zVvK*)6WDZ42Zp&)WdO~(=KQUi}Ejg~A>iy}42@&!vL3gO_81`>}8~A`tZQxT* zIBJdEH*~XBl-XGcXR~oP)Z8Lu!L^9l`Avt*#IGy+*0S~WQ{tJ@s6TT7>{c?>z zWmddN7j$BhmkhW9mWre9?p7@+3LD$5*exHVmO;;MeUY^3w?FkvqI#z3SsOWHy1xq9 zm6V?YTBbS{0^K}h0`%)kwM2#7VuRez2erGJ=}p{qA(6T`eAQm0L`E1@qpjH_PGJ6S zX5)3jCVvSvEtjBXrtmYjg2}J@{8_%+cd`=gPUu4h8g7W_nStm=d@Ol$9Xazdrdg8a z5SX^`v~I$3`Jx1U8Z1l~+GVH)i8J&?I*LDpM;TKjmlE9;&KvxVSzL&ny8*7|MwDA8 z5A*ga8ff|#Ct^^TRZkzcR;}wwPf&O=^3&+s_0`5B=#)qmApZSNNEk!ZNxCYW(ae z4oTixr48F-BO9*|y1dx~>M4HE%aA2zqvy+X9oHe8!NCi)P*t;ysz-QcMO+H`D?72A znKo84yic0d{!Ynobfd}oE-i{-pPoN|Uii1qaA!M53x3WRrEAz=o%Dm@{^-w(hIei@ zPR{b^$-H`^=!$l;A45tKb@eU90kjwKDw&9DBH9l2i>JQel26NjN;VpMPnqaFAk_N{ zLu#xyD1O~4n~}zs8B~Z^&Y>RMA;J845#}s|M7MpTld_fj=In8g%LNUA4_z7>+UsWV z3sR7#AjyhsySUh5TIu;|pEH4;jUAd^G2=a8I~qET_YAVF!HVF zt!3q;b=QsNfgDDJ(sFWpLM_yf_hz|DFpFQZV zr@59WC+h>5K6+jW_U`#QVa>T|I^7J!YaVKhgWl7g6)y%jZps@GofFxViFJo7Y|@Uic0a#{N>>2r~shgAIGqNPvOpg z@^aghByPqj@r__C)?8&9^yyv zzwAm{i_-WRQ(J|D2He@B4Y#e5u{@gL+DOo?&>HRL8CY`eVl{ts>Rk<C#^^t94fqta6r$WS8XN# zQ}W^N(xm}&-Vp{T49KB_UoW*G(rFIokyPS5R{XfRzj~jPbm5kE?yGxnrq^sMPZ&Cd z&Bj-nw1k@Ejsq{$cosV^p9aMi@>W>TE5i+vFT_nRy4vSJGv?mMzx`cIVsBbKa8I&T z;ukxZ^`)?qnc`a~=ol?j*qu?+=$lh+27cxq$lvsLoC~e;Kn_6q1LIW?MmZGhOzpO-=HI!U-bZUaUIUD%1lwvj_ zL04UUDb`C?S%)=n{!z{zu~)k}RY@_sGb$4&L2G-W!GuhgUX;8Xt7A)+QjU$TzOfu) zmL(pPJbV2@WUbF1fQ+b|9S-Qe&g<6p4^A5Vdza~*r+1B=zk zb? z1^6>G{(zCXdFrMUiL=u zm1$pL*Jmz*Fwu~p~zojc|c)CfWIxd9nf7e)2(W&e=jE~ z7}07#-QQN56eQ8bB5tWM#4#TiO}S&J{I!c0W*JSXvqr03xxIwGs6B41>^Ss>aJ*~| z-I2bf`5Yc6w4_>E=$m2{x|j4eaDQ~xHm>2T9REpf?&H@U z9N9^|r^43K@#VG+f~A|G3j6BAmA!^p(1*`ZMwV#vJ`alkXUGcS!c$uA?F8T{;`SOB z5UlwXHLdT^HJc}Lp1C;?XlyIWm+C4=(Ca$aLcN4ht7_y{({#hRw(yiYOnhS~Ko|D! z`6f5*V*XT9xU8T#8@Xf!GYJ*5(d_kU$#!rvMrBDl6bS+r3Q8;eT^62*a}7e|+2YHk zR(pC;7Ug2^U7?umi(*Fy7GrBWyvJI3o8_kdH0$=h!8Q;eXQc0RKiLgQqN6L^rk=+f z*9EEG|7#NDDY7Gw$3vx3c+dL=8oT#5;y&6O>AF&{Ars(aq89}M~q7)+LxT^j~re=hszm&!(^L6b{u zVIJYf>~hC_Y7^H5`0X~&?g-0I9q$Di_t3pFkrJ_DNyet4vt^>H0X6%hjpN?K=$j<^ z9k1D6vzf<(HtA#fU0^o@PFF0*k(Tr%&Nn^Xz-l`w+mUfIUucLP$FeumDR|=x6UF1- z7Z5;@Vd4B{{iO=bJvQI|OLoeo#~{7-TA&M$j>W?~VBY27hDHY?y_Q8^$bJYP+z*Cz zZ#n_S`}~IRozMRSH2$MW@Io_vxzp&+6~)|yV|@=F^9Lb$${clLNbZDb?%7Fq+8Sfn z%83gXaow(s$g~mfpF4h7*=WTZ{oxmSrTS33=Zx8JN!n`T(NdU_u`EC=rqUJ@gNtCe zlKStDc8`SwB;m_hmNlJ;%J1aeVs!@Z39m|Eauq{EXC8>lU7E5g+~}}$J14afJ@jJ> z7M^BQzj~BQlb!s>hcrTEdSpUm<4xDr0?))zKIr+1Jp{nun3GHj`f#PV z%KuMafbJ}Yn_RY)pe`78Ju7v0{t;*EuUVa1NV>XdrRwk0V|Zp zG}~?vrvjVP{vXo5I;^d9-IuYpROnRDQkV**cyWSG1sE(8mk?6C5TpbT)~Qi6g#al& z#YrF(Cj>&V1PD?zIE4@(xD#~lntjf>&)xg%{oEsek@YRIJn#D6_x%a|c?`q(2{#7M zAixJkd<4?o?m4&I{&_4}CC0WaoGU|FGCxAXfYm8Kuhb%ZtH!;)s+_%X3QFhKC%OfB zDzKW(<{loTdO#9FL*L9J0+nR3^*A#Gfq4n6eF0VeV&IJHv~EmMY!X?rmFQTtQpIM|h z5wh04iZ^s|RM>@H{gW-Ca#?z@IxMBg-o%EXRN#TI7Qq9EG5srIR77EcfR#zYmcz{+ zICh|Q*Pv?JRI+55J@0;_gA&PlN}0zePg^w2%E`F^0rfkqlR$D)v09~ zB69C8)>+Ow^ojwk?(e)mMWz&>_Z!tU%?zx>*~(vtIx zQ-`jsS%)N3uk8B4ft1(HU8PK4Ax5E-XTMRo-a2T_@TxDwU7{6Dy1Kna7{w(aS+{=H zC;c9u5*w>WI=U`usI9(gh2sc*pFVg8eO5&6>R?f<1QM>GU>~aH$>B#f*;aI`n#750 zNHcpnDU`%zqQ6}7hd3I$>p(!|GiqHNSZ*%of*pVn$W4<;*Zu*lHj#f_1i?Wlh{RC3GiYG;ZL^z>{1A`>njr&JmX$_4 zlb79CfK^IZ^PoyecHcDtajWZ(Z>J2{56fE1L`T#Gf`+~K)o$I}Tc)25WZyq|{BG~D zG9F#RR3Q4UxtKB4r8H8CDS(jP7~v33zwpJ)n*f)+%5~}?2XytWD)cxlmShh_Yl~_9 z%J%gCp7Y;*_@1pL1!H?p6<<-V7Tw0WZmCoQz_F-QT6;88VBz#S=!S47!l*g#Pd3q- zoY3i6mxr31^Z`D&wbq3v0b7)OTm8_JimX+)bcdyyQ`FNpVo`7YMt%fXi!;k?Y|pn; zwWNeZr%ck`h99~aXu(=hRyMW*=C@nQ`HXHVkTp0aRpbB|g6UzvnW(iGAw#AdKL5xUg5r$r(E7Qd=1xcwlw@+ z{yts4s_DkF^TmeXO;++jnLjw#=Jvw9JuT z?}{*#z7$Wj>gv2`4Vea2-+`iPHcoS7@b}9uPQ>}*itdbH61SAw1~<|xrb>w{gT0zs z?ne{m=_uVu{=>1wd`%YBo{+)X9le`qnsl+BEW|$1R)Dk2-F9Xs;Pin6oxl;r&iu9 zKUUi+c-q`c@C*t(9`zEE_nAU^8 zatS?{YXE81+QjxsadaZY4XICI9Y#sG z+UDtyhzw`7>dHSDPCj9dkky#%!{_cu+>L~;>Qs@4v}*NCApk z(nUE|%@h#%>eHCC<)f+Qt-~N24vxUG{v$wl=|Fny6R+UB&&QBThcQN9lmCLq&*@+_#3UMBBB>s-(P`nm5pDv9{7I#3I>s5rG z;#2oae+VDEZo&@b4}rUPb-#L@&fS#X_>p;z_rd%Dk|5^r_V-8>DX{PDcDdni%Rg-u z%$hIzeLQG{Y3cQJ#ZlxhIh<=-O8C0^vLizHcS9*moAXz6(!NrY7#e@&U24d^yjoS- z&6H^8ArT{s=E8_B3aD6|7W=(OGht>56gjE_nj(0!Za8m#>V9SuUzf`p!jwRV9fIqd zRiDAJ`tFFY=H)`~0-L2?T+VA<>o79J&PiK}kDIbB6&a|$jq`4+t0&n8ON3rK5U$H& zCVydLEdBFKbV(8-#e7g8x06od2_CKUsjp{bbPT;dyP=sU6}+lk>}+|Efj00#rB448 ziWuA36U4-A6%14B3Qn(n+cfjW*3y2;eU<>TTS0@;i9>k#{z}xjYtK$E!+)$P@{om8Of z?BcESRze#qiJ&g|9rZsl#BwD(r1nWdM_`vXtw&rSeZ>>)*j;|JIteEjnbb2q;;=)q zZj6cbSXPF--uKTQYuD;z*wm4A3ii?)3+`?lxTmv|P*4)9aZP2z&MU&hV^`a~?(=sy zq#cbeMhWX5lHrd~ud4%7k~jC&l#i8JrR&O9UuRM>xM}TXeff!Fo7!fZr50&jt;6uT z`rY0(X7M)c^%((unX(yQ!LGU^!Ju@;Vr1m|kMwJ;Y(M?8%VytBFnY3zalAdM!(4Lm z+jp@fLX5oH_EH*jKF~D-zaEreOzW>Sk{v=NIn7*iD~E0O$NV}Qmx5dlGo9~jG2_?>70#xW+)?;gjD&ny$OF>l%li7E+;MS_yLN@L#BIV_ z8?WIzHb1@-#N{4WjfU?GeSMudFgPW3YD|kgT)%bVpXuD7B0n(x>0a$-1ag0Wyk>Q! zAiwWDd|P!W#3H_qqgOfD8j4lh>hn+CLl$sh*UQI>-@tXv6uFPvBzwIJmhT_H43kVf ztbRsnEuC8C@3VY;{wvK_cbD2I4uM)!Dx0r`29{Zg(}Y{(NzaYW?yW znSzgAdF2oBxSAFD!x-~etK2SJ&&+G}~ zmg^K$q$R+z-Vm;}Xv&z4o^tr(T|XnI1h^$2*BWluM7ezDd#Yi~uj4?L!pFC}3dtfKGk zJgqT+)qLvGLse}lS#I=D&76kB9P=EeDpxd13yO+5mp`H79hU*QGOy&-l7K~>TgY>( zD|e%E7@;~GD)%hcsuXXQLMMA`R;B-B!!8=~nLo6!eVoh$xy|tGslSyf!evjH`;A`^ zhI@~Fx+GT;(hKW)R+r*HZ%J-D^*nD(c_SWW0;%&4?kVH&ktFxHuzL?9u6IMska-33 zn6(_^bt7fhRvMrA90PtFm5PXl`XZCkt0j1*KDOa$N?<IxWrIbT1z{kRDfkw+}y+2#%UKl$F`ei{zm@I!)sbOhg(oX|(69j=g6eM=qt7 z-!iYQ5gE-VIux6PO@H6=p{Mcc&q+$j48k6{L>|>>5=Boo;OaY{gW|6RCKl+jZe2A+ zU6Dm!n<=$$lUT9xaxtP1mTs z`o`>xb`heC!81;t6qJG7!ojZH7RaZTFW-;g^)10IM8VC9x*57e8BB_bM>;}S>=UoT zj4{#;<2}19jcb(bbSFGQM70uwS=ChpoeaAkXpfOck|#cekqdJoc`$czHq93lMvplq_E@4T`WPk9|88xvT$((MRib7^Q~( zC}e|sVH}IKUKT#kXYpf(9p|6UR4~QOmP@;_G-FI`DaUCT@l!0HgZT3ioVT9iN$ays zGncLUJapYyMnDOt$yk;FGUj0l)cd&DM<}^~)70BDNhNJ{e99|7bx6%*#YMPvgB6rw zlSDwIV{!N3ofAV|U}A3kVBg%q15>#ro%BKCo&2v$%ZeFXP-han;Z*h{c#! zqIyxyfhoN-6+tr`am{ZS7ZXT7N?EJ%BpT-IRUEpZq)^*}9_3yzJI`m$MJADEuCCQ$ zu%PrbiOFC{F}`lEzY1Vy%#8fAJ^fF{{^xl+8=DX5Q*@4sprm>?tXh0As??}&esc{8 z7tPIW=*ftBgpTofZfynYp?PNBTj$K; zpK!WIO9Ps6dl`_JfZ!>QYs#z25;GK=-O|(8Z{JY||ML~RGCGLX#&jL(i*$!^;|Ak zEYL&1(!4ygk~CHoSW^nc=PO~H9ZtRdglixkbp~yQ~wYt^CuSXgPg{JYDaq8oFh?oIr*?$4t8N< z;7fPFH8kLTU~vnXSd@8W$`xM$?$&!IQ)^<;wlLadk%3koQrb)udBY7Y-&A1IZyDb4 z_waFrT`$HFI>Hvz3dVVRwC2WR6|-)b7g zwJYyV%^s!9tEYCGIy2_vU)`m6md!ER9$saW&Q!gkgHNY~WbR4+kd5uVyVuJ^W4y36P?Io$>B=eK3=AmV6H#+3b3tdV z(@Mng) zHEDwHKffq!HALi}zfteIt^j~W{_`*JuMrM-;-74|g(Avoj6wmcB6kHu5;?I@3Dfpf z)L79vg++v(rW%qgz38Kc*v++w-|8gT{{@5XJGS4%wg^2coVA_Z+faVHp`?mkN96;{ ze$2a4!!Ki*5I?$nE`6Y7F9k4gZyXBvlWi=#>rb}Y@>!7Umb!fgM@hy>QSzyUpO`|G z_Z8+FYS>DY_sl$0{v503Q<1hJdCo!a9xm|{=RNwtslaqZ<>0QFXNYZCiPilc2?=85 z@=z{fXXt_$wSter=axt9-AWW-^gmr+m41H@N)i-#n<2~}tZ1NV(6kfmH2O6)=ptl= zRBNamD^X4G)oFx-8g(6~@sQ1^bNmK9MJpm~fGIrCw;#kc)$y3w4 zdqBN8X@L$&alU>d!0gl|Dz(bQTbSO{y}Y|ss|hh1EF?WlA(hcm|s^J-nvE0ch^ zJ|OrH&GorEi5ADkqq=4X2G$Z z>`qtUf@4)rxuO_@(O=wWS+zLfyIeHN{)=kQi&vAN`=wkv+Sw7q7Xq&3M_G?@pt3`M z!|@Tq)F;xs?R-NzjeR`w1V~rc6@=oSh$OAK(te!C&BST0DebnF;QnOGx7!QPJXT{w zqqVjAc#UH3%qKEJXdAbRIW!XfDGDDR)p(m{(59r^>&drt zU^{GAPbbKs77KbnF(A!!=f&}>JqOEhn z&O$OoK39{C27dPgWw!@TL$LN)hgm}f6Jtt*m98N7H;FlK(Y#5}Vh${oeo!5_=woxj zE4ML7(+mS^BsJcaFS$ive#H~nO~CpD@!^u#D~G>{CTxjmnIa2Ma2 z$93rI{5W%`^+iZ3L(6fENVRpknz`6ADM0^px6sW6VHtQ{O0-uhxL-|va2zCM?&JXT zlhc}(Tycd9-S5@Lt|c1T3B5ScYmpq^v6Ykvw_sS!Mj1U4Zw3X%}_yn=Xv`NA_+V>CmOc8% zN%KLyJsod5#)^tOWpblg&yAY-wy@}o^%MCZ3+!6gCb z+G!Gc^Xoo3D>gc&9G^U9J1LaksA#7{b##O=CrVYW0Mz8dCMFHL{B?+dn|ElcN%2rH zJ#iKI zF2IJy3aTE4scjlHcED`6_`|YvJiS-9jzT!Le{ks=D6i4?mk`6aBmLE$G+iBGyYh(r zm+vkn@bJvp!HNWDVUej?oQLtJLag5t)`x8AgNBNKQ)B-;LVvQ! zD2r(7N=lWL2#)!HZL>GiDV9da$9KBEWL&N;I_=+>6`U}m4~d-p7U-nkFk*6U$W^TH;%=sv|VQ~@Q82?d>)inx@WsgC| zst{-G`^J$QRkmM#bpOYfH~)f|J3&CVp0BRCCO373)^PGROS)ZL+y%h?EBAbWjhWg( zIApo30z#~;E*v}!tqZKEqo4@%ZOH8bzI<;>R<)rnC8DaK%cWS?;cfI`xvsM02<0=i zc{BQIsq)bA5jQkkbVEKcOuZ`_b_X+TjBL_qCEe}0Q>})t?DvPP+e**i8}1acfsil% z@!0+w%RI8;TV);Lno*2-38^&>E}PiJC=Ikhj9Y|e#Fla$ z#j8&F5f1dCTUa>+k&b}IHBH(LiV5W_t^}@lh+apl`IYz@E7+=3!(zo$v(<~h9v7wb zXH9r|g&`%Y3ACBP>URoiNO!FCK*f`oKbnuKZGGVSxSkP~RBd9yiuKitZ7!f#e=W6O zdA}ifkIw+Itpz`lKB>xmN7W`=@@kP&&x=kxG`^@8iBNfAlh04$Qk018f}v10ICA1d z{czRm7`tkX%IF5g%rYOZid&OOAF6y?W=h(Ze(i|3;ppVNoOL5)TgX&2DivJYN$TLT z$>U8nYXvp+EUn`$JYbU1rFLJxLvv`P#Euq;21Y(=pD0jKpjeTmN~fTk1Jx7oPpF6! zWn{>W>`KID9~aOVdmPo-MIzABOiFJ@x~F40G&Y}1B^XCXufQKY-PtOmC~*Iq=8XAPh*Ut&WDN+S3d0p8gART(J=p2IdI~jW>N%C?-kp zi(`e9X{B$<0yQo};UqcD;iNHqE5CSH!<`yv*B$a!t1#_?fRIvyQKhr&{VJ zb#3>-!?Qz~0&~)VUGvPtD4C?MMsFk`#-)_0xNjFfKCA0+uqP^XF4%-w9`}DO`j!*# zb*J6M>d7MRRtwc!;_hDd&J{D;+=APu_r|=J{YMqAXA^^UyT%$GW|5VJBhP>O=`4@u zaEFpc?b3GulVNb0y5M4;hjJEBlg6aa4JUZG3cSlWwsle+M94@qO3E9rOj?hP8#Aqj zf$pCCHy@2bSmB)rP8uv{aXKmHeD=6^+> z1rY4e(??*ZGG?#(bh3CcO2gFbI8_Sz868JDZ5)BjLKf_Yyw;mEMtu0^)o`kxy0!#& zYzwfb?bfr?<9!)c-NRh!y-@g3hw zwg*nLCMkN@A#5nBLBsCRO#2HA=rP0_vOKfLx+yYm6f^s87^-N82qlM_C${!|YM4?n z?RD3gEmn~0eX3K&21d&>2^5w|u7+&RF|%H+$PI<;3l`gz4DE;6&k83Vjh?L5u{>xB zs8X08XGQbZ!pwe$cIy~H-X(`i{Z6hGr-sxx z6OnNIyKV*YQjWX2Rf&1_1X6OK8?OM+WQ7Z+5YwO-N)+zIswsS;l#o`-kH37kKwL$& z4bl&l8!!?6Iukxl176stbRkm{FcwR>%1Fd>@>fh^_R^s^Nk zaljNy-{A(?&uLx2pXM}1!6*zqdwl5a9 zJ@4lz;fg&k9fgG7B?*ps1G1bS1FNmfmD8+=>B&Vjskg4B&N1;9WL6Okk0W55>e_rp zRoaZ#!I0$wJ13N{S2i&xKUq^RhgRhlvNsmUXzk$|y6+;I$i78gUz#_MS6apl{CN7)NcAHPvfl)L4jzXv8$zh@bl5AeR8I_)cj8W0L0qI z_F6OaR!LeG>aFcj9&M}1zkQ)(S0H5I>9}>+1&bqHszf!DFfI&IAD${O!iQJ__HZZX z>3p-!$liP12PiS2xjDFG<4^F4k!S&ps78f#T(807oEX9Ih>_Q+PwuO8pXC{M_00MGioQWtWv+VQb_u&-+)fs_B{1Kb7mDJ<0_aiGs% zb5~c0JLxU#GAhL7vYPPxV80LCViTA_uP)EAj;`Cthz0MA)kxUpceEmrN+8}azdh4J zLtOwd?w(kWyjlD4wsBD6%umIsdSgh?wAG`kw`Tp;9hQG&mfX!7-%8*z|H=qFk}gQP zk2`o~Vd_sM+-z#;GKU9Z=S;%C(E%o|GGG|ek!5JawT6mVFAxOHE|8Iy)T z-AzmPV~#y=8Zn`6)>zztca*xN6hYIKCmr*(z7~2*TG)9EppKr_>!QFxVKFhS6hE%7)AikyDSZ!@Mi zxxK`1d38gS5M7jieDlT7N30(?<|7pzjj`Y7$9CpT1}7K~kIGF11PAK?fW@e-vic(E zJf*V)4$7~z-L^PfzXHhaz>c*+l)A=&^!h)hP519r>eTM@1aT@WxzRZkYKxi+Tg37g zMDCM%f%21;p)?uh?uNTZ7*))*&ob{GNTsjGh9oNp9YX_$N6#u*S9;Ar8#odZKL3*m z_uq#8*fof4j^bWGdxW*x%$j1!*s4*U+^2}cx$TcTQXN$qjo$r2*Pk&lu1YNYePrRO`UQ z8*Wg#1&><}Th#G7^omPXyDZ5#9zUem4himI9Pj>nY;vh_T&bT5F=gZbSI7Ua$NyIt z2{yL#YyPFOF*V>3SD`)G-h$O_r4dP%B9|w_0suqVrmnxiOhtL=jU-CtkYtQcdsjeN zscH{Mm$FQZ7Q7W2yM8ri_ih{{iIsU2od(JuoxiL-He2rPG|np` zbi_OTns=tIV%OYZg8A}Cw|_gGZP6L2RFR{;*janh*t0*`Cbgbjg575|d;c`{5BJZ>6$dK6$l`@NikHCt`o0X@5S>x zy6~H(W}wg?9T8VL>}ljp3-5zitK{*xAB+%LiHyG==o+ zT$+Qc+T}-%?*lXBMs~|F?#e9kIKt9%86O1Fsi<&}?`xg6j zI$m%&lqzyVM4@HJeW%-nzbr{Spm-$>m8)A8pP0=^s+`rh!?~sd(GBOn3nbWU%I@OD z;!FEOJWHhKa#fld(v*6!WRD96GT~hj0k-Qt@(k$5G?4h&p1|E{ z=V$|oo4ch-o2!SoML%^j7(tM5S+sNlH4&r4TQyH7FZ%C+)h~|9gz|NJTzTHwx*37` z+vv?a$m-`skgw{X6@&7-mQDNAW8^{KvqZeqFIAmA2%=BbK`BXd;# ziQ;4Z&H}#U6*0LtgBBEajqWw^Uprpr?wYF+czi94(Vi@~*(z`AJL$_WV zIx2SPLiKGd3Pju6nj3Emm|(#jro(ZDum~Sz0AL7hpIexjG#ebow=!?QqgeFPdCe$f z?lJP!Zzb*%7i|ucKu@Qw-5|lr+0h9zFLJ1xp9`+4KS@7k@EogUDBhI*;T|V7vZ-mi ze53Gb!|-elN8}Dr{%ma}f0_B@A0}3JP`$v@5`so6%K({T(rkl@ttTgdA(*>ADaOz5 zo0wDDoZ{Cpo%Ige$W`U37Al!1h4OMP+vj3)Y`!J25z`Mx7;29X;&yL>KuXfBl-HO+ zD;~=~+5GvIM%{Ec+k1)}e0U+XOlMm4l{b~v5;*X-;*}fWct=O=GnOw7eRIy#frMx4 zZYoKda0H-Q3%H|`VM0->byOk8KR%K4e3)Mc>4E17{GiUIOjV(T6{AGobev&sXoU3{ zebBVAgNw6h;KX_L^9faBQ<3hAIfAYa)kTQvs(PQYqXs=if7baI9F$+sZ52=PKB3o5+&KcSofZuG8&;W*8%};@f_(<0{T=6~ z4)=xEU9`OB!vahDEOCNTTLvVe{>vZEeEG*X{iCj~NJqm}>*}8@3M)?0?6k9aHOFG3 zf?bq?&+2G)FkH`zmA3+I#}?^j%tHYZpZcp*1dF9Uc;_;VYc? zi-6O8K-M*3(acbtpx@37tw?(w4EwT5j7EcQ>SxIJiPf=0CfPpUxs?tN0Tq$-( zhQ(nLcVnDzeTApn*M8LVjqCYJRNhPFpZxgYODeV8bVxGOQGOyTz>bqN1}(k^z%neN zl;X4%Iw)7D@#6-5APeMc&klHv&K-^1?fHzr{w@R!Y1vcLZ?KF@d6D9OBe~A43R>ui ziUNd$3p-2GdaVZDn(5}^U!aE`+h)R8QL!{J)rL_4-3mmrc57{)T`l2n%|;D9B55mH`S-HEuv>*;^L&2WuJW~~DxWmvp2 zQ6K_EQa&B8xjjvTVJeD-6U@KAp;xu2^LPonnqf(Fx^G%?>YKgCys0w6ot85j`PW=? zblmeqwZnsEn6U>x*4AvXQ1U)b1_Wcr_!|>v?#%=Ite9H;I{$v_n(VV@)3V~76(rBs zjnWBTRyS^Bfjh_6pHDZ>frQborSM5Rj+V&cH!arb7IPI8vrOPS z(c{W90K^b6A~pHFT=lxRa>*(qPtug@OwA@}@17gh1PxC4WIqbK#f^PuWO z=IC|T%>(Yi+76npXc~TqG--qE#;{|Mk6Zz50sNp(dVNJHqwE2bK- z{jll$`0^h@&Ua^3+QJ963D^BaJoSiWOK~v^GP{)BoBoni{&2Ux^zr6UF#*ZSDdh<>RNZlz^xUg)C`?p?rcv#FD&)UqmSN_-K@7R~BHt)v%`VV{gw?&>c zEVr3xepO6z%mRxLDiB)luWeltY#f@pAG+|H(P8uz=EbEtaCPF?20)p~;wV*2DQ>m! zaF`wH9f0w;jb5nL-*uD^tXA)Ji5T%UorLacjZJ+{7?K6&8A^~T-B8C$uV%gaVtP4# zb}N$$rki`pb;Mm+K{0*E=A4l)-aLZWxCRjX_8J%M9S3p1|J?${oN% z@X80_5r&PQYE2GPC)2}vx9?&m2iHwqoE#25kw?iy!>vv=X_I#s0aD2B-LZYiw;g#c zLk#ahZL;#oi9!EbjX%|JTL&wRFFXE&t^426T9;L_^Qx){dlJY9$7_8Z!ufTI6KDT; zl#nniA$P>b_M0l%J9YSyPGCXN;$H{!-Qy*&Ce6Bul3ZiGlZtXfMT~hNCaP5ZG#Wxk zzlFp1I75U5RZ1`t8GSW%4U^oPekTz)t=i(yllTqV917c^#uXyP|M|h7)8TN4xrSH7 zh&yA!!LB8A+XzZ+IEpuGN5<7EYMtNq487hc8a@V}-LMK8IK+d=HEJQGKiT%USM|GA zx4DkIU3)a-4*%HYM!r^&>=lavm`K;G8~3gVTu7z{Z_Dt@ho;uBZeHRUba?Fa^HWqA zua3I2NQ;3OUcHF!aMRQbX~LoAUhWfmLiS_gCH$KWPi9AWdsC)W_aLK6(&rm@7C28$ zfRs5+$;R^@$9lm#nnp?uPNAv0#=X*>R!PxGvhX4Bu#TP9PKmQK)&u4XLfOB4%ap_8 ziy-nB-%~pMs5voq0;vVFy?GD=i~G1|`PqXxSeh2G%fg$bNDUo)ga|G7woZRCb5DjI z3W5zF##a)?&*$$;C@*u4eyWIVH;ZkBoO|5WJ?CC-YL;K#@8vn~2Z+0d%1)cB-cs*2 z-jvb4#&s=*Zfg3inye#Xyz%%Rf8#A%1>pU-Gvf%k(W=}^nw5OE&W(2} zuGqzu>hs=1YIm!wv&G)Gb;g0!xj7MW^1gaq?d4Okc@mvsCSK89<(bwK%BHI6(F{cd zd=9QzGv>$rY-Uc4X2MQw%PX2E$)d1sotrbflyV(E%}%?{S-XJ~$cJ?P1NH%HKPoIb zIMjG;(N!ybeW|>(qtnMsn6U=gp{;#gTWlp$fGPvu#1wGB+arzT$aQT_Rb(F{Z{tv+)7U$GP?QilmWhK_nnC?(JVTtYO5@mmUkhb(Or)i3fqVM7&y((K_F5viM6 zRGc_|+VRd9Gt_tz5l+&G^tEpkNKc)#XtIK=Mz1VthA@=(!V-g3@64<#H|oPKhJ(dc zkdJB{vqq?1%vwIzon7YWR?cK#;Ly{}Wvq1Ga*1n*TCko>JjdhP7IO~I<`~wSSki$m zp~++G+tKBx@6>*G%2)h1hH>2-O~n&5dZ zy4fzE88(q~Usu;SDy-#ml3AyBdUpEE7PWy#i2O}Yc!}{u;L-BD{GelV&Fk6i7h4F4 z_HxfGv_O$wKr^v>MZh?n=t1%JoipA^GFcs{(;M;PA!VX8mK3B^42VZN8@J2TQSyQs>2W*b|3Hc8KoS0{*jyHf&o} zk90YaLRaE;KLvr8@96(R3WE3D_5IM)^2VPW)ejwiT6*DXA(?aF99hOEY9JfE`rNS@ zbNNk;>{-z@?R?wtwm;dL_2%>@9A^oX1MRA z2guXlYrGHWffHilXD?$B8`eqMN4j~YxjnU@P54& z<;b~SU*}|$_KMfD@r9YcT11+sEzgQ}x~IIZ8PhDz%${zINC?YE0>o71zkBQc`@%)5 z>*PaWQ{uf;nV0A*($eaZ;RE}+G0~shk-FJ62KR?85Vg@>n$fC_WZO4gL>ov|1{>h{z5fUzVzdOr$uS?i_d3JyLiRp-F;c-m?*@p zc@udh)~CqDexitLn+L=&+v*{%-6$nglqcXsK6PoKCcyCfnnM?$p0(I`@H`Ila-fw= zeJ5T-ZQm|Q#&t)H^@p7m67_Lwyd5pGRiGNIe!n^VTHa|OerrZ%?YAL#enSX{!>ibe zdCx7Gwf2^5c11YNib`C0utl&*Qk^XQRw5p>!b)Xn1hW82`=ogtoc0C1@U)u=*`6!* z?dUi_K@V9Na~CI)U>ufX6!sy_pA4}cKk9cl12z9!8lbaXJDK4LZ;u^b$yT>1_Rkw4 zwNuM7E?>rEL8xuzvdK@xEG)$G>TU*e6+WY88>mxCYZa1H8{}7%_yu71cv8fcl2wwb zt+)jv2~0e#)IO9vIa*KB3^7Cd8A2R#bA$=KU~DV#%thj_k7$~uUO{*N1zY+*KQs#s zhKgd+h0t|#(j>>5dBWLU^*{3SMj=t*E+*j;Cq3)g70&7obCjXyxKH`#uQy*Pm)6_k zJ$JVm0&I&1Ayc*q-#cvRI&hJ zSe)zhtK)86j$R2HhMR6Uwa-z1vc?4tAhxR}i6`_3v0i+e|2!ux?$IRd&?(P0Ji*pwKp%1E-&$YG8dbK>Roh@6Zw8VIkx+1CcNw1!oFxcmr zGVco3(YDNthp6S%t(|lydE6RSjpEfjo)s==Ht|aLNL}LUz>_0o_FKRy#zrNUi^sY_ zxfb&CFE6XYPKLsRP{}hh9oT)#Wj}b19@GygSBi!Aa{{$rz0;pg<`0>S{CE`w>*~$- zs1xp}&Ys|?h|@aEMt*14v>C`^nVHX^hOU&m7~yjC(7%HTCZVhq=4&sogW&fR~%kH-W595U**K<(Kh`RY&ZqZvzU%r^$u;%?39{tAa)_X8FW# z!&!EQ%%IxhC_#7Y@Db{KmG$|pSA9MF(>bgC_319-?C^pWWs}62@wQzqu*ynWa#oab zMUhi>*u_xU2W;p532g#6p6_7YvnSw-JJvmoQ5r6;sF$7F8%bXCd8=V-vpqUGN~EG4 z7Z7mk3VdWKNNfpGBDBG1(7TudmXe%iuIKjx(l+2}LjRR9b`Ua^=FJLd@W4v2n1jQZ z@5od@B(>DX>4T&TPQ{JpsgUO~0>E7jy&H*AtQ2B&q?S;a*#&n$T5HdbUA-VN9MnZV z53XjDTt@qf} z!dLi@qTh&jII{t2#brYTf0&RF-SbVEj~|O}zw9`T!~_+Daj9kImcbHNbW;q#2Cn*5 zOu^4f#bipgcX=lr)U?blKcr#zcGJc;qdaf}kgOjduFK595F!9a=b!P*_)>1Wl zkfapJ@Nr3JC+^0p6203g5G4;|I3lxACMwcHKn1}5i3wCeR^x{Lh!A{jBZj~J@KOFG z+`zwPrm!R8hv;Z0i^W7?F>F$&3Iu35Gp$1lv|Bn)A4fJ`VrT`FzEa4gJNL2|3~~Ey zvdkQNCF>mcBX^P_{hWVu=z}Iy*U@hXZ+A%C*pE!O^`(zu;=%RJp4OplS9r2C4TM50gRJEL~ zB5yygq27}jRiJQD|DwK=m?u zK0+^EW$K1B+=k5ytvL2iC7r@OM=p%T8wbMKm7f@#S{}C_X$B}(%Xs$pygxjAM>e|% zAF?Hc1@j9w`{PoB-<;Y$+9(}-8xdr$6iK8rplbO_+J(fKo@Fk8;4oV(j3PuvV6^fS z%aztsOjD7-HDG>d?nPDaqSZONR4sGFdI`FwoiJ%&3Da1b)Nv(@M4a)7{mc2K0{VOZ#5!UAg1>UL!9w=3=LSqjk)2{h+GaGFK6`|qTZ>k>ERAuTWWDoXW;p0iZJV%Om<5sbPHX&`Re z$wIAC&p*=nsBQkOL<`Rf=inYE#TXA2PM=Ymsa%7{4kiD>F#9Ymq{-Y-n~Qj2s27d> z`UmEwGJa2^fY6RSLn5i-lLJOxW9q7mRf_Pw`VDMAx%nWguso={s-(jO%;btT9Z;(r zvj$>%((WT;(vQ2(lk7CheESA^-+!qgrX3lx-c3E3=*kTIV`WeITx5mq;roxtId&io>4Ngh zr;+`VF=gz3EczVUJMH_F=$Tc#XILt#07kBZ*lhRIHc;~`nFbc-&UFc%#%o#IQ5_4W zqMrkl`3i_IB|d{7i!Ds!l=ot>bADfPUHjgta*(ao;eernfoRI`EvK({V8}Tf>b>pw^<^NY7|Nb|F zrtzY_PblnY`~ZH${y~*S_mC;CbdoO;GZYd#_BzknG;C>&x?>k*2Hq8|3#u!IzttuT z>>L~2)abj7)uxk67H5ugf2wt~JB`deTX0)Ocly}@zE}ABv;6P3-1iku?R@sg+KYAJ zluEi$mj8La_Y!4sjBTSmA$nPVN3jvvlBe-!^m|QQIZjgYs`;vi=K_Bwd~Qq6=WPVf z09p0c`6hn#r1Z@jyQvb4M7A;x<%=V8hh)#srHWc>b=;-o%#3i3=byZ zT^BC1gTUwl1nz2i8-D6Vp3IrL^`wNOl6f`d(Y4-Bf7ku*Vnq2`Dnf_;2A&FUE&bgp zGd4Dn^B{W2 zv4qvptKxj26>pILXN^Asx&hOR#Jn9^b*IqIV62PtbFPBF8R%MEMTS#1rDGp+*=Kf? zlB0b3XCi|J%77ANj)OIKu+jIT8ZCA%O6K0&;Aqmm^MQvz+2YBX(uG#6b~9Tg0T)XI zRt)DOq+Rf~uQ`d**g=J$pneiCTstJg*v-GeVaRSzx;od=khOs^xN^HGS5hF=@P@kZ zBIzM_k)0E36TUr5Ax!^g$EIu{y)j1Jn5UsKWx*PQ8jabWc4UToju0Xp49s+qoBqWV057_(rH#0BSHE7Tj5!!PX+!#tYpjgYd+ZS%&7#p4_El4G$B4LSg3?$Iq+g-JMxd}Jrf^n)Cva)z)zny^ zpPxM<&nPx8(zsAr!D%?1LS~f9I)Y~W78W(2!!7}}8ne49)qotkANxd!tqUYS--6xC z3hJyVK$~T9oE$EWF?_;Wvi@*>pxtJ~YH$j1?Jz{GMY`M_zc&b-p`lx?gs8jlth$J&BX_551eVT7A?QB4@WP3#7 z!yK&asY+S%nhbLy$$P_Zs=yES*Dl54w7$o-R)0QKUu#1L3Nl)7zzaGVzBoHoR3ivb`xO+*|ZeBCy~3{(-L~ zkW0O+M`bmI-0;kZKOVuY(uaLC&ke`xo$}y97#EWdujhacE8Hopj;}Dv_bPq!FgD_W zDvLk7+I8@*N-(zQUl*p0RsYZiapAL*1su`YTYBjp&2YO^uN`&K^^|V<#UYrLM^j?v>VT zMfk7;nQVxdgdC^*Cl~c!$^z4u;R9i>L)Fq1bP{tdPeA8}u@IZIsB-kFob5Tr|9qS- z{huG~>n`=$e=2#zgYOqrE3jV6Qk$g`-7pyVs@(26G(3qVRE;Osbb){CFAy4D z8&9>@7>O(%PeMkPeOgjF-;q4qxC?{ium@AVLU^25HbR3eCeg>`oRzpHx(4Ye<`;$* zzF_u;w}65MtqB@Em+#ZuZb6`F@ai?)*hC*11`Zt`lKka+)$iUaN>qq>$y7y?Dm@Q2 z<;qv8B^Oo*W1M=(!#V!G+V8)x!UO0Y1s|7YtFq-L;r*yBb;kg!Q)^7eI~Q)J>rUhv zZ7CPWAC;%nb(Qvx7Ra2|b`Oe0{U>L#jtNIUt4Pt`|4=C%B*WOnWz&k^Yxf@(6XW6Rt$71;n*DY-qnaOP#%F5l4rh~eS9i@r;71#Dw13V@qybD+ z$dcF6+5jQBp`#*y*$+d3~hlU=^itR}@q~cIMWnK&C*27#U93*yTB)j*+)Z^pq zYf*zu7@v7!TiMbt0Ga>V*T1Fu$5gy+Iy}13q5V7cgHWpYVpZ5ve6j7u%qeIx@l(N} z#1DucSB7!fMg!!ns&9bL2d7@ok3Ddtdg-->cg5(}1)Y1hHJKC?FD5P&Bt5qk`y{PP zO6ymwJN~%#4}q-zdj5ZG|Etf3%VZjINTp5)Q0^Vby|wB)#W;i|`FX?;KB{A+WN zLhE{Y5iMTv_W`cMcXLKgFIM)$<|Eh{J+pR|4Oy;IN0m7S3-gw6`vMiphNf3ZqqWWw z$u+*Xx2hjh8-r|V4V#eP-@N$mV`TKJ5ojoS^qnod-NAYN37%Me<$mO~B?m%)=g*W? zBz;jkDgX#u#;!xOXz561mC$KO6kh%EW-8qo9TXAotIBpkuhu(PqpVl8@rTymQpf~G zf$W=uEt}OECtXNqBZ@v&C-)5P14k>MVT2B`k%RuxNLRo61y7uuwTNX_wUJpGG3FYI z41E(ThOHDSTPKA%t*s3<|3(W}%Urtm-I}M()2>O6J3F0r{-l^rYfet9tNg5RX^fz9 z+h+M%e2%R<{xHb4-oJ{^%mf_v@T^VpMhj)7qZm_Rqt_2I-`bGhyHxFRofCs*DNbo- z+0rdPG6)|ogU14SXjvH1Hc5K~V{&{Zj6|J-yNw9h<|apu=@eVy3MgOwCvqSX&=;YQ zhNenedRna1?K^GXKnyzjRhkpK$=|XF77uk}1me}mU9O>EaNPTROJFWv$`IL+JQ1u0 z`UY^NuVZ%Tmb@^omi${hlT;vEq-0i_43A?rrfx`Q!piuidm+|oT)tb&Yo7H}=0i@v z5C7Xq2LK>eo})>ltM1X*Ql;6~87Eq?4ao25xU4j<+bxf-mj}@Dko((I85!qh`Jr>l z@ck$_%2zPGjY8UyWQ^!q@y3tDoo9xI^fcz96pmX4^zH?u4rzJLH7`roteNJa(FOQu)hG z*f0O^gk0+@;_U9xw<6F9*8pW*R3nq|fN+a#KBW%V&8uZWhWKNegrDl04aI0Ow#W%pAy!FoU1f=^x+A`jEA5uEP0T%iGm6M$;>rFu(vm_NmST6H%we9p2v5;c4zROGusWRs^eyDUP`OKGW!OQc8KkDSkX4= zuCwJ(f3!F)<%0Q6{#R=W&yvj4cn-gGdC%^;(iJo{ZdVReAY3%05SAcf~nuNl$uW(onUT5+gdwG3p}bdC-JiaW1(fR64M-{rFDh>!Ntl zg0q8Kw|u?SoHNTuGRBH4w=^L=6r?iZN26klL#kWuNx_T z-RYkIa;JPxc_7kQ;)=?2O7>E7DFuJ-Wvv$A z{3vCd?OI02YKc~3E^0-Vaaa}omBqj2?ci6J?f4we9B)VubIQ0)aN)ws%I`pa(n*#A zJhy7I-)2`69@TES=H?-K-fbIyI9sh}RlT21#_dPGYlnT6!!$+k*jZ5r`bUX+BEOx~ zrEaZSQr22t%-B6J7^woOXLC)_ks|hQpWV#p8mJr5tJW2tD5%easT%R=wGa-008c? zX&E;CdZa6VoTibH`o2j{zs~*OSRwNZ&o*ZX&yf~zcsAucSRgwf)`}_tU2b*I{|!$} z@)oSS64En2OX43~s2cK|FQ$)WJQN@3@X#tL;085{R{`Ds1Km0AvuPOJ3R^Yqo$b1* z|M~3{mEz@U_1^(E|MBtvhw0@IH&*hHPzr zrR<@;UXSR#VX5UNLd%vtltbN&%j8IL?5eZ$o!QR?tc7tylK%Z+VNEW3)|~rmMO8Bq zE?1ohbw028jS>E%a{Q%f&kMkRH_`zB*O&~&InA-?H#wlBBhJ^3u0qmE89db4tlmQ3 zS(RfpY*|Cmd7tN?*>5S68|TG=Uk`M$TY4mId=6$?Oqu9*!R6loz4S;KTZFcbXmIA` z)5_S0Bm?gA+fo6Mhjp&iD4^`3)q(@4UnlDEL{Fv>*x$1CXRtrC$Vil%?Uv{u~(RGxoP?#wozxTR)J-vsV^TSu}V;cc7u*M+Qstnkp3_e3`e!S zWyhLV|Hih|s|d&`zj=hLcfsQWY#7QaYy$az>gRFO{OC3}&ruFR4++j1d$3|ks7t`&po>aES3b5-q^(*8TSQ8@${Y>aWV zwW^__IO~FH53JQm=uQ)ESQD-i=~);9C$+LC9~>a(?G?mWt%J)@OH+|7Ckc5rdB6Uy zbL|?J;#0^eI6Eb=U-e*{j$Jz#u;=A84n|p6*bXBneH-zV@=4Z4HLd$9GUACFNTm4| zJ$4lM@JyL2l*4&pupi&>V($6e3-oNPP+Uh}!J$eYg41OvRb>8_woHj&eRv7NEqqv`PrYm=~;guW!sApJp%SsnYqqY|>C(8uKV2Yu5?0bzt6s0cZ`y7cm?|m%vGL@h4 zg*EB~zokRq=0k13MMPBEenomFtLDhw9Myh-DuwP#7o0^*CB#)`L1D9Q?ey~L*3sd5 zHo@FmPs=EW^3f7^Pl0(FH?<^0MHS(gi?sqCE!!#)$%>I}*Y|B%Z8F5dIFoM78PzU0 zn${ih>@jEPpV*U}Cwka<&9ZkT3}N`9u~pN3<@ZaL2pLAHsoni=Y<^n5i zAd&dzo(ujHjj7l`yzZR%xNfVhYa1g^qJ&E=MN^Xj#2ci0@yc&6`~^xNQZkhvJGRQBzQ**B-_ZJ zhVk+%XWK{LGa$DwjHq&jkDso>y@L*HvO=!lRNPx5;w9nPxcc|;x<($?UPLvzhmLCeJR|xdnRBeU6 zOlHtfkOu|}h>7XV*n9&>1Yd}V8o_mD9sjB#;Gs}aBq21&i#zY*NVw#ylUvS~FgMYc z5u}YEOEY=^e8h0n^Xr)>Et1jt0HO6$wN*oqE}`-0PKFB=VGyw_TF(S|#_Uf=lHy{o zfK1^Jjni#=-UoGLlESKmMV&w&<~CK;qGTqUylMmysa!=Ca`GFxkkK*J?c@O}A~iRO zCj#=aTu*kIirS=2JbbAbBG#f-@#7_PeyZi(Vb)1DkedAVE)!HN#K{To^U6l>vSI-I zZ))xT6JOr{My^kvl$A*bMWdhd11D)0`W{0$D`o7k-i0}Opg7$oXD&TVZ)_+7BVuXn zVzl3lZsmM#%Z?uGJb&l*y>VzC1h+y2fOvdBBJC!mw-5UFCQkT+;K?y$)=C z2izAz8T^i(`8Dx-WLl+$Ew>A>iyCQ_48 z0dyMfG}-CLy^h!qHmd#t(xRpxGn96ezCdT-Vp6y1zBnEi<0JJ_)-Q{khNmReETSizF}z z!QYgEVS(n4J7=><@IWZXAn*%+Z->^?L?z7wo86KWVJ0kfX-V;@Ha_` z+Z@(-Ja>KVc8yOB|3j4B1worFG)!9>7#DTN@gM z{*X_tDFV4ks&sgIZx8f52dCI2 zsUBk&l)%_8%*(8QN7%LhM3vcFLp_`nrDx}Cp=ngsITo;eQE8j)f&aJC2?}>fl^Ue`{ zSCMV}&VtTIq$e zd4sc=8aE@M5&q#b_%v947F>JwHlyL;bm-E8xr^!E)uSuLs{9|@;lDHaiRSn1iSLbi z)$q%-SnDsFLfhO%Y5%_x9EN=3C6$ol>r-A+N5PpPYSD;Al+kj2&(&~EZW^XZEkfx~ zpDrLSe0To=heU5Ne8Mj}#z1s2bUS~#BcE-3YBjO4`jp}9Um4~ z(RlD4tB9B$nEx=#qJJqHd#GRe!{g#KxRXuci~lGv`p>IYvvwAFo10?%F0B6Sfy+lE zGse%`vCbza(a_MZY(FZS-^%gV!^p{~gZQ5ojyJoWyN(dKL-We9Bp?q^!waZbJKMJ8 zki@?hu~dm&(Thw5Po3ZU;(uW;#BWdhIW5a}`O1U)tvXL6$~IwB1Gy6;d>lxqBvmcV2hpZ|b^%#*n|o@$y9<{@|STn@%Nv7cYEgP|GB#nexDOch2N zHx1H>MZ4p)(sNX*QM1Vk3vRpg!wcu~aUhms?hq1}nvtMb$#uKR?D9ba?vbKcFCE10gJf63mk5~mq3-0N zar{=C`r@k)Gn3ur+ylQkU{HiHnsP6dN!aqeOGM;E-Si$CY=$eHpm=rT+}eZoqTg+L z##&fZu%h_thYK;fb8<9c$83+^5SD^}&sd8*0Zot{l5USEg;+D3`Snict#Ddb&;{vT zwQF`eFU3XMvW1i^<%P!&a!l9RH)Xh(7m!y)OiwdayoXRld?d@0gnhVygqHv4h+50! zdP-%)h%OF1O7;J2FOZ~}{M=ssNK=-#TOMd~>Y6;_`fAk8g`5miwl?>g$CFRPaiQ76 zRZSLaX{MExJT^20ovZab9?j^~{A%$>vNOb_@=?B#1JV5(pnxnc50z~&IMLD=50rle zSsY5%7C$ltZd-M$n@6e2OGTGTUu9M3B>N@bpgOaR^!uT^uG1Gwr%DQv@wS_(+n&7r z=Xcez<~NtYv>Y=OrufrZBGv|bR5j=^+;*2qS0xYH^s4Ab zQ5mqH%9Ou<-Wc0A00i=-rY5GlA<2rZ$(jTGhR`~$4IG|Xc>MdH>ARH#2XgLvSNW7Q z2l-v4R(?Y)r?haMhI-%t-uqK+5WU#eC1r-4pS6Isdu*w5M5+fWAc~O;G|Muy@mnuC zde}Kq-PxIFf^!aX4tnS}ajG zVtaWN^z2Q~Rgr}D>A*Q;ysUqn>l@(xJ;JM31HFl6k~Lf>-Q!eH1So)0s(?B!Hs7Ot zK~8?&vN|XMxRr2TOK+eRb%u(oZ0qJk2$Gbh+~EuHi!a@9nzSo!|&&J$u)y znVtwfZkC%5FS5qh(P2%RB>tJCH=I(V3pEl;N*;@3N3=!;R*bx75I9y}HJ=+Ocq^QB?nYRI>y-C{;1Zu1IN7TwyCIs^n7&{axTcN3y^F zOO2>^4-9|NO%IeGSnM}}4Df?G#TzFAM@P9PHC|dyJ+GFJ;>LY6eyUgy5ZiLxpyQ_M z+h+fy>7k7rPdV;opH`maH#g1^+mt4bKoZ0S>M@sZ-ezvyMPo^1qMYUZ*u0<<#N)~`WYkJ>tO5BgLy0yBZZVri;;T*DR-W zLDgu-1Cf;PYJT7umBXQja-lr1=h7wDQ6||bv;u$O4Er~E zNyZCHd!kGw6$*rA?xFHuFPdL6ANt-8Xv=oKs8pfUtv7elg>0_uUn`eRvvx%A)mjE1 zrB3am#vV`xdEiikO{CgltlnIy!+1>Gta;>!e#)FRb5m3NfFEjI$JRF^;I`~zSz zw`LcHQOlRG=>fm50xE3Uto?z#u6KAttZa^h02C>S5P@qi@_Qskm3Ei5ST9*VX+d6e zH!K*hm(s(^qMriB&Twqz4Ju+mLUm7Z@j&}{^wUoMRIK?_l&{?9)eniX)uoeB!a;X9 zt=^=nj@kLj8|SiEGlTVh`x`8Z68VA7oMZ7W_>rC`^Lc`=!nQ3qzO4Aw<`gN~-@x*i zxrk)D=-sA^$D7(~AQtBY)O4I#r7I1beOH&h0iLwOzNne|1Y2@@rsN2Y%K)KAe}5DF zN9+II80$X{a8tQES|G0%fe$;+Lud!UX*%VW$|fXEi>JnCC-+RAPkm^U zR!Je{T5J_V5?M9?Km12z{6Eg^0j3Q7)s8v35|?|ko4RpJ_8yDu*WyDyNK@!<3(|a5 z2B8TdZ42A+i=KmkFclTd5Udy6RpYqwXaH!LRvxtlCl?QMhHGRp*au3#pIR|4}i?tzL26LD;^_$<+ zWdFg1d_Co&dJ1%?&p5}5LaIfHMwhaWwOiVGB{`euZ#;=K&N#AsZ8{z#FI+M|-L`i` z`dbLw??t=9Ts04wm2|X|={hptCi4mK$A9VgzcuezE}15nRke9y7pH_Kz%5Bg@VE-o8Tt%4>_eKR1t^R0aIC`uy^a*gTTj4|e)J`oAWPHH<1+!a zyG5IH>I7+eBYSP#2-K$YEWX?SBzZXQ0l#k+Zqh4B9R2Rtx1nerg`<%xS7w-8!s17p zBwt1tlpm?1&vi!6CL;xGL0kB9!|q4v@r94_%Znqh5FK<}WbaOionQ8P(oA%qe%1GO z8!+5hiar89By@iJ4|;+3Sk`4LWH7-iRv#ptIEhEHUJUXF3&by(N0hBs)L*%rSEhw1 z?jJ5$cP%{Q5@i!6R0dT>*RG!axID<_-=601@l4xf!(Pz9&QcU_-jDV<6-uSK^?c@<4ML>VPVE3V&TrG&it1wX#^Y4iv%2P*^KQT}}lh_6z1u z)|EEyJ~TDOf?=uoL8phJ;s;w!ix^SpqS!_@3{!pNp09OuXIHx2DA};Oz*((VUa2Q` zBE47lC{PRi^NOW(U|Jo9!)S4PEI4oP#5ek}oc0EGtE%~d;&tA9(Oog29*UdX{^KGs z4^yhH#`tA7(P7ZhdFIyc__Gwu>5{&F8F)-*Lq=}yHbKWJD_!42w|JP`P#zwAH(twk zR+Bk{im&w5*DC+is-Eq}EvD}SbxV%U{s(7clsAp9Z~1W=onG1GyR92?C&MBj!X;^Ss~5btnQQKy zJ?>>c(~~b4AB%UK`jc+6ME#gQO628{KQ&8i$X?tYM03g2<|=9vyyjVSQWll3o;Ez+ zDlsWV@UoES!wQ(`p*B@%u~I*7W-AYmCOuU}bya;C+-?3;n1y*S<(9Vb6uYH%?80tM zHEWu)^@=Gwz1~N0e{(09we1xTdf1w%rQMglqO6)eTOR#7IR6)s&nL}`gqhxDLj|iV z1_9RLpRRV9_4Cq@q4yFD2~Rl`v{ch6M<*OLIT3jhGBXv`k3M~>qr?AnjQYM(mpWfQ zF<&;6Xco)w=g^#y_dd8X%m5b&n)?bGR@wy%p*_GE;0(DbF;7}eUlj@bfP=0eZ>ZC} z58=xxJa@_1!-x?Qf{3u&e5*9R-6YxuajVB-NqgE;EU59xX84>#-P_msF-bY7SC(_$ z8zpV1g^f{qT(o66eEX0khTB|aVKnWcd&f&NIJ5?Do!%7lI#*kx8Z`h#CYh6;g(oa* zF7Aq_q%>$;y1Arm1neC1Da~psyUq0OG4Ta{MZFWg7Hf` zg2sitcGYJ{6DFo&6zUQfq#w_P9O2Ugl{5u+t-5rss3ks=QRsGF(y~mDSo`Whrse6M zh5ywcbDMW<+KH*ksHzg@0gm5Nw*S0-_UF^)z;%YU=gqYW1{xyJiU7g05 zgU1Ez=S)t^u)N>;04h4dYg{Y%y;`uj&I>|ie@NBbMBoANPMib8&&ohpvUY4)swn%l zkGR*PhG!?qmb8~EB5tqU^o64tj@a9w5!{c}6#SEH3jy~&|9>v&dnSE&eFR<>#IHY| zm6zgG1L`Afd3{X0aXo$gf*@Xp-qqX^jTFW=nyQ^!?n}~coCh7_YOBW*2jhmS)PoKs zX~_FyTqv8Tw;r|PKmYh|>vuXMN*Wv)>v`3YXkWGHl(tK~ZDvC5n>de9IC>q%@Fc7` zG0$?Ohpmx@tZ~|moC-IXzOrxOd@2#|voFx3vakei`JMRJed$A2u@#Z|gk<5)mAJQK z;r*w3qhx%=DOt&4(vaCk*YjZQ!<~k9T^*zpI_OD_ymJuQL{{n{ zu^~&xm!w=;7`_44IkrWGc(v+c8ILHZ;qsHMrD>7C9>P;wiQlTrdVK4PD)QpX+F`45 zvdVdyH^vMNn#EG4i;5x{Ra&ik4A({T&gK~vtVV{zPVR65-&CKyO*qqaeG0~8=sV{; zTpBs%LXS;5y>l~@sMe#`0dtL=%Wra|O0`}$yjdn)10Lt`#5B9yly6fJy8pLw-GBc1 zIfaAK0vmktJ@d%i?BFlIJo*ms@rUn!`RNuQV0wYr--o(^TtXTss`Jf&YFqH790wg4 zfl%fb#Ah~Hw$A-tFQ8h{oNEi%T`ybZ?5KM~gJ-Gqibths*O^qWKK&^QGdaHf!;MG6 zAWff?-Bfw06H6nZAjq(V=Tx~yrQ$3pz+g(0i-xjWuI3%jtIz+s3gCm#JF8|miCS0g z(K!0Z*A5C+?J^$jLyqhiS)Mbhs)IT#>83Wq-vD=PZj< z5m6D!nh$?Ah%aw2U~Lun0lSq~JhdiPaiu}fkIl_6F)*~L#7fLHE;7liu~z45_<*?l zXLvOg4c|v|4wz?)mSdY7mloGHkY!lpwRZh|u(&?6xWv89<8HH}0=YY!A5n@sm z;un?3qTYHkDD(|r0@3JJSAlZcd_9ZHiK`;?#uYKKg_=#zt*HlYNU#_@&MQBB`4w14 z`Qx)VwJcH0BCgSl>{h$3uE~?JYleU5UstkYs1Avtj?%5(rA)$7VM|Jrv~*Stw~Wc0 zNwD#V0b6|63cYfY?jL(fH!h_1&VgVQOtt6HLKw+rt*%HqCWwi_zKXsJ$@m-K*n711 zP*d3zX{kbXGPX4k==I6x(y1GD$5}-(PthHMEe53Gwo|_pE?qjjjhnHGU-R9tiO4!X zA8=y652iDR*LaH1mMBw`3(&LA3$2=+3q6X)tPxo>?=1MVIKm=-D|jQnRG_|X&qFVQ z(on?lo{`5-ag96T;*>l-A`4&IAJ_ihA^8Ixz1pR#wPxtBFm@1fNk@IEt2(h?7W8kjR`j7{E8(jBmht+2 zT=1gENy#i7pq&8yhb=iySJF`=Qng#`4oHTM2{8s6+6xCiKO}~|W%*7?>$IWO51{{>qesxU(nC?RgcVHWM`|?k3~@y#5scM1-Ap;;%{1@0@DcG_^i>VC zr}OSL-pEgBPB-^aG0b_1wx(ecv zNHXzY?tvo)>yEs&RwS?3gqV}O$0%6TxhK<3jhK2;Kd4cQiUZn^8y39Rz#A(wlk-o} z4~oedSK>D7<@)ioma_}&c42w9k4a5L!=vWKuMqarF(=K^H<$_W2e;JaMxhGqSa*DolBB{p+a|i>$!JnB zot_mLT$f9qRScQ^n%(|@O0soPlQd*yBDgDwKE(0Esj&#q=apTTl*TQYphm1`K$=3t zP-4pIKw_e%qR{stnc@CII32a9Jm>JMeZxV+Ig6HP{t_ic!9zvD7DhM9s_Lt}665~T zj@Vegm~Od#gZA=lLhkFn-5echynVI%s}BrK6B}e}m4j2mjFevh5C8b5qYHg-VWvy` zse0nISw2A}bg0?0G^f(#^j5awKYjo_`l*D|s}6ziefI}|uBv+h_?^(#-Sy!j-pMYy zYam#4+}LNNdqlJ{I=3o*8cz4bR8@$mnfMtno%P`IQ<pxW! zVz~>aTkDZ7WfSQndi2$OpKFQdgQ~W(E7&m7_Z^+_2F5mC)nz*8(5^Qx`}T8+@|X7m zFk5g&{FtCJdc|43*y-j}k(|uwOFLhYh{voJgMvB)(6u2_>l$QI9J0MDK<650p%dQ< zf5^KIZeQ?xS2$f>uE;utv{?l=IL`Uq?%m7$2|nsFQQuvKya(4Rn2O=3E7C>Zpj+E7 zgC!yi(yI7+6-|+T88jt1GD}?6(5Y8J%dnR=HspsC8^OXIPN|RIu`)?VmTQK7 z=o3?g>_4fpIaR%5B>3gT9sD1S-97nD(iCMTbThvCDUzb;K`KpK@HPUuK{GJa@#)MH_zIO9uPzVdPGHlD+}nH4lWb@8@MSkg$>hx0~Bk!ukCXQ)A7EIABxt?9rebGc63y1URzBRznHB^LS!0H)u;e@l#2 zSy&x)+#(va3s~?BvO7CHp;{I<%cs0#{6aS_7aD!WX_Z^AHE<6I(XZ_|%@a3hUwQ@( zw;|*HV*Ki^5fE#nlgbz&ujnNBq+DyAQ{41ON*(abLmc${hqw4#%WeGj^PX35(xc14 z-_))F~zwoAWOH;j(SsvGAzT)pu{&Z%z~j5QO>y66?RAtOK@ z`9g}^Y%s{aag~9VenUkwVBjvpWKMl~+JFs)3~s zgHsR*Jj968>cWjQM^CoTvv3Q)_*}c*)L3WET~RNM^m#I9pU^gdhz}Lxs;Y{Yh{Mb! zdrf`f{$;#-NoxQ8tU7W~ypd$M!Z&iiDN#Xb`>|!WIopU|w>tkXimywXq7*E8V97U~ z$5$)E6F&bJM|J?-5E$Df*zeF!InAq6#eX8c)6F$L{=VkI^_! zT6%I-8SZ}eg>?yha4*x#d+SxwR_FZr0SBCA*<(I4@M5scf5$<@-LtQ%n{`ab{>aRK z%$Ux5o9;&S`#~TY7{Bg9I9r1c%C^9)?zD97m{*9OmZMtREFkC-Tz7M1lRjOFN2U)v z^e4)8aDeF`eOLpnkw^vl73D{_wk>qVAB=JIgEB8JV#W~T!tnb}#tGMftUZlYtCe?( zNnvY?GjusY$4`Gl#M61=|AayFPiL%uQ)_($aL7tvpkCt02A_}duPTEGCp;)%*qnNN1NhJJh+WLBT_nMAhiuIXC(+Q)DTkSGvFSKz@j)-Xq~(@ME^vNe zp#6>G@7x~bL-O1Ngw;Y1D+P<^r~3_LT71se6)uIF&+>vUgFSeIJMfzaom_D~ z>(J(DbIQTLhwI#ES{ToPwYg>&olo8~&k>T&e!0){N&*)-V4#J5eYHNiCM8oz!dpx| z-z`De+ntB;;BTi6jF>l>{oX;W5nAphh6j$)LZ(h{ucg)Oj;qIBNJASu-+i10+%vPw zNA852Z}d<^{m{jQSn)`z+2yk}Cf!rJDA`Kgh)K_@qd;`*Vo9=ErZFc_u>v88HuaT) zvL)+@wu|4@3ZfP3_*ShuL1#nUMu){;B(nB>1IQ-h57MJt;n6YEt!(Y6sPPFT z{gs$8_5?Q@>2jR?Y0)i%62+o~9dDSCzWomN)!3OBPPp^sSC`NuA7uIzFT@y)<>t!>q(ceNtMdlRR+gUBU|n(;5D zydC}BuiW@rZTo&pA#`kPhRJ{FZ4E#S#yo0Wv3&AWUsp>v+yW7kka+I-mY!B--*(Rn z(_Hj;+m0HnW=duX=Q&nvHjuN)I(%E_hJLnDxf!$Agasq^Z7_fu zNGEfKrb@fwGlfL-m{e|#;Lba{;v?Gqx@Z~E>c$M}UhPD+g@y#Pp^2A{PeRlUV?x@# zdst*0(HOUPY0=iz99O>{%B10~&^&|v28aPM-rf+SJ54Gk0c%ivibTSIikGSijh`9Y z!eoJ3Beli$H2G!)cI)W}kiAE)ndllGCv!$i3zG_MO-zQw)e(?n3!jO`btdZbbiBei z7H?R#7?o$JFeg%#Xt$K(K+jFX>NI*<5e~_3C3ILgRoxQjG5DIIFiBYKgWu^a?>q^5 zenq6>^l@!^u8QoWjX0z4)9JOcnYU(s4WJYw&o9)GXP3gASJeCqdIVcdgPm8H63;12 za6i^1?aH+^kA4GO`Oz6q8GVhJ5f^(8?&OH9m*5ip^C~6%LlD;$EPU#~`m#BNgAy@x zHk7$nAiI4^>aKud%2}cgza$hn8L(7eM&bR8)wnZ56@n9;Rn%h1_yzsQeXoT&JQ8|O ze$Gv_Z^7D79p>byZD@i~h4@uJEWC=fRdEn4!i=Zpumd?i4pAisdXS0>zuI!mHdvmP z@b6@s*`Z6>wUo+B1nC}&NUxpJ+HHJO=f z&N1$B-^QqjZ>Y62quo?bxs?op^b9gz&rDGy3HhWTIg>lIKnX=7BC-;YDY0Twa2HfB zrSV;mnP*H4t*qTTus0+14Xtd7oL9se4a9h&$kgP=6vw#XTliw2Uzle;mx5>k0A58)Knewii-#f`Q+Ro2*reHwN=u_~1VixRK633*Hb4R=Bj9bQb^!0w= ziDLJuQ9v*PfuMlZxknHM}@zk7OFFj%jnVY;Xrb*kCXo-FAZ8)u4s zwu*1X_e|F+-4GG9P@H#L_bSLrx7+d3I(qP(oF&4#(=Wzkanm!>=q(3SXRN;e6HX7Q z57TdnZoy%(R+otpNwGG2bM^UH5$9#ayV}hU{;{6%KVEmy%}W~P4*43?;Vw)yO)E%d z?Fa+?$o85Tm-IcUf!+hB9Af(Ciz!}1Zz%`*4=o&Z#WxBQHu)@19b+37zH(e}yaVob98iL6<+Etq&vYxuub#_}a9GrK zh;S9Ei0vpoP%oM_*ghqf7)H8l@lHW_YjuC4RgZt!m^D1LR-M*(pl+KiDm5Hma6QN_ z`dX8|r-it)Nl~^Ba`EWuQ;uGU(hT3X&4wEWtYVS|s?4BFD0*gHV%l?5#&{=KMrVq9 zD8I1+cFa1~ceDHk6W={8=X==p7)?c{HT}(-J*~sfhJ8F&cWI#_+aaC)5JRn1GKn1H z>pC5nb0~c1Zi%=onq_CHis9sB#>ZKfry_r)k;tbRx04SV*$7yd2(5V?O2h7_N1vUIMB;FXtdz4{Yd#bE}ruDAzQFk%4yu+Dg7QR&2CACfxOLJQZ z?_>npmYR_!M(iw?aa$SN`F`zFi&BZpvTt8UyJyTE(_tZMiy&(n;#rj{NE9uLUw&@? z&oAkJY@xv`e=fSXjBp7phJNTI1Im#;`$vSBVmz9S(NMTv06S{TPt;$mXmXaqFNYLV z32?r9S4%c#qsW%z!OhW?WpY|kwW0>$v-iH$jn0sXESlzW+E(zI$sc9%!7fGRk>X_2 zGFW+F<7RB-qJ%iLv%tr?+FUvh{$G)=I|ji9ZA6LcSSo`?e6VW zlnQ@!Q2>YA_RF}6?$VkQ-6m+wDX4N$C4rq9ite@voxnPUW?fAEVQT@Y>-r4d)q7 zy1y}he&TPB|G4n+C)oqlCGpE=r(k~UoOPN~Z>SS{ARfq5f6A3tE%(IB<2s1~ijgVa6|D@d&SA zCesWMj}v43Hp*z9`^{>Yivmr4L8WlM(Htj_=QuRq5)9+Yt|#v@S&RKzu@1mA&8fSU zg+(w0mJ!#-eEY-M4%3cf3%+=>mt$tF4_9NsdpXF3My zpPeO{+zl7w=j8VEeEA7EX-8U&o3Dzv%gN01rpJqQWS3k6CPZ(T`_y&m>!6!0=HfcL zfC+IS7I2$o)RHJH(?y}hPQ?aFRBnFS3#>}I&)V3t zLnrBh)Hr&EvDM9^%H6bs45(XWek}VvR3s08r1cQP)e)rEuRrZLGdCa{E74Vo z_BD=4XMhx^tSebEPGomGP~3_Qn?$wYvZHzcBVkl4=bepI-)2^#f46Z8FG#9y>jK*xQDaYLzGV(Bk zn`w1_`rEN|4Il9_VYh>Ry+W_ki->!)h$(X3%l#yel|E5b z4N;CukJmZ1_X(GxP_4Y_Es&(d{bTkSb|y z)%AZx_lfQjn~2mZ@0odfRo{@-c)E8ir9N7;D43_w!eHp;gPZ>wFctmq@!Y*OnL#b= zaD8(Wcry>P{!byVzxVi0XTN^Dm?;&QFhs|x`IOF=^TC-RU%HD%3j6VN^Dk?os0ZnC z#|HweGRQza#OetJEw%%=&_pbAko!(6>kbjU%Hy(tVgeXQo1mJOou2$j zx3VaC04LsMAX4sTk$)QEP4bu!lObM(Btb>Krwt%h1sC6yC&Eu!rsiWimbCB@t z-YPFKHAkiAk0Pa-h8mbpR>G=}UR7R*k)`P_bv-0>$qrg%a5HmRR?Ae!$ z#KSk??HD}N-$B-%aq_@yWi{vmMDd*7_oEpVaVpT?5M zoa*xehw1C|(b1$4PuO3V+pIC}dG<1E$=Z4c2+j-gbqep@fQi^C=FUmi1I?zOYd2-T zAyYfZw#`N3lcWOVn@FOC&X+BC*R-9QaaAu=`+i!iS-xoRQ}$PH@meU{&__Bps8>po zJqac+?I3-C5A=Du_j}EichnfpQqyusn|kWn-2QU#GY2d8q!G+({0wAY<66m?Tv145 zw9tY5H?uLT^tBKliRn!ii-E*4(daHQExEljie|#%)dJRV?5ALM9{2H`_6e;sNhOi*jFF*i^TxX!{86Q1;hygbILB@ z9;$8h9)gAj%4qJtXHQ29C;ij`2hd<;I=RDqRReEP2Eq(z^jNIyBpk@9 zVQ2&!7At-+Fy~+6^$gXNJhFqbnffLB_c^`@)y056=+1rGZT4E-mDmDX96?vIlc$8Y zw-sJJz={i9V4$>w8vknb9#4E=@LXEn5~WIZ0L zjX{_coRlpK`#98zll+epa0Gx&*!5Z4`d^z;4O$x~=CQX$D9>@;!@kE1M~c_$5Z6jx z^j9(AKxH|wD391V;TDerZ#R+9{badeFoMbaH0#hc7Vh!Q14Myq(8&iT9`7-f7avCQ z6qtsXWtQvIY<%6K=L_N2!Si!QZ;!)w;SS8wE&ASv%iQ62IUN}B(9ESV57Z7Ol&~Md zw5a+GTR*&WWF_|}JMfA-PbOMWI2cWt#UX4W^}?LJ90Dy9NH1CdgF{&)4Z1Vke|=&8 zPlw$c(=i{oa||z}L5(sT=7FPliowxOu4d0?-Ni=43^R%9#9T(2Y;oH*U{iio<~_bT z81AExbf0{XWvSiMS?3%=6GvQ*aEM*RDkN>Rk;GUfLDj|C%14NNRoGuGvruIIc5#H( zk*)`URgBWQv=%mQLwpxu-l@y8%Omub+1}{r;y9iI<3t3nI>%U6ELCLCNH}Q{XQO}6 z&7IElf1rr=PtAR&j*c8IoNnUOe@K0wBND|Umm>SEyVG6&$LdNaTvlRButbCeB51HG zchiLaOz-vrmiR%&uEJ{Gun5McOLY}m-Fz*0j`s#G&w zk)a(`5@2q^Bhqa{DMD~v6u2ulGz+Hb{4V8(nhpl8#5CRAOSIoleve8vI7iZ-patU}@ zh>OD|gXmcIVmOyaBMPOib78!=%t-@msAZNhxLXBwsaX$B3gdN%Cv)C*_FazvtLjg0 zY{URThH=7vb#;ELOUoL0FXb&ZWC&L>p>y^g1G~-FcfZ^#s!(@}E7w~#rRIvJsWjTx zF>z&E$+HApUnv`@^e`1+)-c%4a$@=!U(8qu^e8PdBa_Sa(yh&|j~u^OO@jGKyS=O( z%{8DAraWhpwi8uNM#(t1*7=Ug%khw=)!jpF9`o^WEuc25)6j|v z48>T0>v5ZyVnMlK*w|gIchwiZD5W+CN6d<*k5if;_%fz^b9P~vU{!wjHUlZoK7OEE zf8rJ#)}-RnCAl;>W)n%+9H~#^MFtOCMCs*i8QSis-P_O8 z82zy?Mq0#JN%oS;yv)%#21L1b5>5@k<5p(l^4iM2@(()vf|Ym8dPfWsAnG@5?M^Fi z?LbXbeh)c)AKV*#@-?I>FgQeh!3hms-i=~~?wg5LW zmvYc)PZRKXWtCv9*oDdbh@QHC^(y#JtImJk)ATSBEg79yxn!M)*@d#oDVjDoD1<)# zaoXaffL6RgrIno1uM35UK{nmTNd3@O!coD(_E*jDw-TOQXya zd_|^V`87y;3QOI1lkYaj)?Z>})P^yo(x+PHaCUzVV?j&Lw#J&y+gkBmS$msz#1QXV z5E}iBm_IC5(+yj+7IysrVs}maw4=3<-1PiMnOiN{@@SZ*=H3t_5KAwyP-=Ul7xa6t z=1|6R`6L6Gyd!Rve|-XH<}C#|E$hHncP{*vhX(Y04)C3|xKi$zjFA^HURNfiM(9>e z_yFQ97X^8@<*vmRY*u_uEI0@c+f<|_<~*x2Hh6jmPQd1t?N)f=9KN8ArZe9LHFv$U z6raIupR=RxA>G{{oh590UOLXMv}$`mWfP9y-W=XAw)XA%5HRRl&r@%Cl9tk_7u%ua zdPUMbI`wwFOF6*Bau<4=Lw6lG&`V11*138mU&$p5^xLk$^lhiqzC&Sk5tNN<5|As} zW3lK_v&RqlfDL(x34ODK0^-!&euC-|TiE{2DNpOVxq(cNiy7l_6MpJ(ex3FW_opFt zdB=ujID0R~ExG;Od8HoLNIoB{$k-Y}SKBPIDK0wLyW_o`cD-+G3toSVpVKH8PA;nd zk?DUs8fH@lsVpp}6_Foof(-58Lvr_)@Qy2r=f@EO>YZE@aQ^HHb}i8^cK&>BtWjDAH`cJUm5euo`e@pq}X`P{+8J4DW?qH994`aOKZX zNb-;Nb5C%Rxvt)MM{>29?BGm+(KgpB3*N^jvpHxjY_BnBne|2OA@+qt4vTd zrzfK!aAQ+Ku0dMkaqb;5r^80V4T*(C%07u&MHP$wnD1VOLk;=I%C~odyJVKG6%dSc z3XpXMHHGBg6lz6XSbQJ$DFpaR=dt4}9vgX?nkv0;R!@<^00gub=_SN8c~ic@El+e> z+tD)6ns2?|WSNHzSH4d)_qF%u((s~(=@4j|ug%u{cKg_J8-m@c1T~inME~&eBxHi} zWW%(RXA>Kiq;<-aAAe(LJRb(=HgDIcmQ&<&tmDIfDgA@aoCd1!5Zj0EaS}DUHGLr| z$(VuifeFhwzJaqDp{Q^QF3dGb=CIAGT^_xVY;pyP+(u>o7O>Y%SY;a;>t-G4YfJ6CvqK{MEu| zc?kCQ;$h4-Ds@_@Oy!a)a*ePqZ@yG%J=W{A^_i)u$v3saLUkx_rEXVVVSiB6>-miBGNP%593bx_J5CGBiN9K3}i!# z_qcK{Jl4AO@h430UmiET8w^gw)V#|MXh<^Zm}^;Fj4bIn~%0OW&jZ=hr;AA`E(xJfUJegTyr{fo4-Du@n7H4;v7C zwo`yRz>#k2lG-i9*M^bHS}#6i`u6K>7?B7rn1QUXTzsa+@rl!{+NuC&nZ|aGagJar z*-Xt{JU(q;aaj3%$1e;6FJ2ga+ZeKP`~C)Mo#7;?)wu$vO~p)fS4P)4^pea?%Enr0Eh^@&^7w#9*D%!9|d@d-j^TpCrGis0yceE*l1DxEy(#SIwgx*AW@L)82t9 z(sFrzn$S-Bp_>0ecSV*CW-0f$ht^4@9Csa+O9fgzX+XqI>6vJ8uTc3?#tGoAsp@z;D0L6CIazPQB%Wz>AsdCzQPeK@EUUnwB5-)e|M;uaq(^d1qAG4!-px zFPHU98Z2IKS0ph0+^e5n26(@Yhs7I(z-&GR(!Jv4;!Hy1{=9DZ56g!CLWYW#qf)0v zS{0-dX@r|iU@<*Wn^@JDI!>x09ILGLqDLDiW6hql?=x1n`kFX3()&6aZ+?&>iXfS! z9dR4|cs%!O)^obTpHBbx4q|^kDBRaz>mho?Fi>R<+0mA;WR20;<>aAO%Iiy>!iK7! zo?3Cl9|h*%1RW~gEqWMuG6Gf}EUziQ^g9^$4csM`aF4kEg-~WQ77tZe z?P!I26N2#H%i1dzoWkDp3{J8`q_V9&edzu)as2aRNN=u}RRCUMX|hCnXm6P_f8x?i zib%<5ov|iNX7{$HS4Y<_lB$8+M^D0)yJmvrQ&zS77Vrw-p3khMyom{`5Mz&~EOr1e zE6aGzZEvf^0aAhQ3ZYt_n7-ZK0%U-~0x|;=U?CNdWk*7W<9Vdk0DajD+hBU8qZgn` zr|y1ALttJ%JiD6))5aafM*cxZ+cPxxmZ<35R`kR7dx!T_ekanZKsP|Mr^Bu@p8F|Q z*euNTiITY~CvN)C5V@B0kc?pEgKhGaHB&Fpqjqg^VsFQ(3EL_WlR@~1WS8uY+7xch zsUyn*qn%v`1`n%l!Y>BJ>$SDD?JLvvcex+hJ19lC)};7iHmgkj$=)#t%TXUs!sWTF z+x*8dR&0Ly#))RSx8d$1i#Y(k9Wj0#i`B#Td$dwv>?S_8m?e1JC6ksT-Q@pF=pXDHUJ;g3%i@A_T7_Eaa_RZ?<;;71@RgY?!3|+r>+I0qY zX$klzi({Tw7+31~Wz1P1RV;m$z2BN(8qiwt{(5@WjVcYqy8PPGx-)^{$ukk3r7;&T z3k$iLqm35Xv2VgYQ`xz;40yMURsbTubMF`-n~$ zjRiUvnv1Srg{yxYh}!na9k6Uwa|1ti#*_G_8}E$gAI(P~gg2q%S@lwzG~l%lhlYdb zC|<>2$;ob?r`ml&(PC)Rn}dLv^}6G2vDy>)B6)}+=rT+g%>k6Hl{12^(NTOmW=#q} zFtO>8-`UIEiGA5z)2a4Sh7~*(n#lb)1kC1fZZz;I%KQY_ZBN|XS)&nM?C^r!Rmv${H^GLcyV-bTD7q>5a8+cP;TwV?8dOAX}vdQJ_g6|X-nP(ap z=wFw|oH2ALi(VZ8#ks{Ins=yQNz?4X2tg!~9DZuFK3}7lxFv5L$((*s?!XPISGXsE zkDgte1tdiJPFRuLj1E3^0tvlme?o~0xehUxg8xh-4rS4Fpkx78 zx?OW*1YC!Bi4@}JrJ;d$ES1wkpNjB5aGq|RsNOo}w*EJ%0rEHdP@YvTK{Bi3J7e`R z6_H89>%=bO@*@FMoY6jUy&}=tgLiW`L|AD}px4A@hlHL1y_BBqzAc)e)m{?8x9-e$ zPnLw5<`wdEgQsR4*Oyi$9v5}>0s;D^$p8lbp^bo#omew_i}UslGBfyHw(WJox6M8o zJqsA#D@>(~P!v*NfKJv>y%oJZJX~DG-4nm>fOy4OC?!U*jn2Gg{Xt;~6DG8`DIdDn z;I-I)h)nI7$r3KB9P|pcw|48PE=Gz|q=vC%Ntq(8UaO>zN%a{TH(Xl-P zF7biTq@a4Y97P3r3RVsboX8dJ}iX%&+V`F-%UOKn+1@3^HiRJO z&<%j-q-ec3h%?m}u6;O^E2yJay0fC_rTvd!tp6N}MWdkSdK{<o`zQg9^4QH$}reEWP660Ep`zEVaP5XuwEQS9g)fg zuZMl2mF3>0eT2%Ag6Z}Vdh0L~uiHb{rZ0U5GD~FJQ+B{OBb9Jh85oSB?;AyB;C0Fk zFsWa;IDKo89L8x2`3Mueq~;NY<;X<7fwmppZ91IZodM3oW@%hA;PG#IPs`2Ju1HqQ z#{+G$_#e=e&rZoL3Acf;rHmGWGnV|E2)UmX%x)SSQ7 zFqRAPm`>rr-@A3w)Qe+>rQBz*VF@1ImCA1?pJKVof=G&{&l=G*V2Zp%-U>!+38xnz zM!Cu?gQ9j0ZAw+sgY%BIEpJOIefH#%s0_~L%8I3ph&bs@7Beli`e{Lx)@-B3K=4KV zii$PRxP2~uM2f_2oRPs*BvVOT=S(+3m`2qFa3tY)dP zj9$&fl^ru_*!15{uSn@XMJSfc{&rC}^Zvdi%%Do2ZO>7N zZN4g0s|zS7Te0xpLyB0+nco?1+{9N>Il8DNa1Z9-%>!+Ub=HVIe*K>6FS!Z5O^eQk zJ>|>Q7b&DzKDK-o>?mZ?nrvvpe?hws3tP~EetZjN*DJw=R$ZZqtpHopdkqv@q zU4HJyO`R~j_4~&MSForh$~=E@X{R=)qIG?bd~I#9yqTr|-(+c`%Uf0b`!MQk#}7|e zbW$@RIHI<(SP(K%-_X510yjT#2V}QiOc~Dji_h3Q()hjQ9PPylkUV%3VP%0`IZ}t^fNo6M030S=( zYUaj)oS2nDK|IVc#d|>07SP=`D{8$xWz6j`v>v&0{Y#@2>xsmo47ebnyH-BWz;SV}_CEW&;kd56T#6fjV!_vodL(l^q+tz#P zAlhKjso}in6%UjZMAV6V;0tS3N~?8k(D4$+G7_w3N}N}`R+h#3ykDc)P~I{uKYw76DI~Bgz+D8tj)s%Jy!6cbXUB-hTAtpJ*<;yR69Tl39gVhHO z*0xjD89p+#bxNxWgsJn5VRa^fYVHhO?IC5!$mIo}iq{!@7YVe&@4?Mv)0h*^3bI9U zHGc1)ArQ7wRCAJ9SSi0|&^wSG7a^KiCqUC!`JOz`Zrnwx*Yoacx^_fM&A3V(-aW3|}|n+YO*?GahsR_?Z?o4Y3)%qHNgg znKF8-Gv^)fi=F^yQ1AMM#+qltv*t;15MPDmY%tZ8?m}aVT=HbOYq11Iy~W$|Sgd9U zj_?nVIZXrD%#Nw2oK?f#jzXmlurb5I`kro$)qe5DU8tx&WueRW?sg|1V2;Q&hoN3+@n< z`%*u;UPh(qa>f}LH&=;^tvY~4nywDrmUwmHk}a8-;AjHQC-Nf17`a-Q1Q_3h`@#oo zNNxvnB#&yFt(Rv?LobM`57aHVXWS(-FTiR*3n`zn-ty-DCPL_d`Jd2vxBLz z(Cy;)6EBx?yjFM)FVg=(2caK(_In&6yzTjOlA@=Jsdj5(OJ%1wf`u0% zLB>xfr0q4UYF|v1CHpgTTAA7Wk~sHiW7vk++F3cC>S84gYQBAO)Xd83;ir1;TKk_Qe!1q^flK5d zyISAfPE1UWK+S#ay7saor#HBsN0}=YVoVph}^(Z=_}Oq0ez99?_A$v{7kKLFZYRO9IA=r&7Ooe4s1Ml!c+aFb#uem zdE(qqm2_=zfBocz=MU$5k>HBJk$Q*X0q2#oBjN|!%4rtdbEny+8)5VjG8Vw6P%FMm znkD%HIXYt_T;Jv--{$1A3NT^xRH7Nqy?VevFVr<|otO~iOF#@bkNI1$tlp0=7nNxQ z1eVeszGddZQ*hOxGik0*dd)?Ad~n#HOyo%gfUU@_YD7)TWhgHJk1J$-c)5U<-a2Ca z^{@SSiT6KabN}<5F7ck}nJs#G+>8YrF#AGAP91hRg?|U}I$RHmh znN5G5@{LBZ^4ACc|7UrfqThFWdYNl7=Z4M~4UjG7Xw0w20|vW{TqVdb)+W8+S^@O? z#_QM%Yl@LL_*>}7#MK{N4KiaYg%M7(g@-iFh81@abRgCI5W)jh+L)p#m9K`ai0b>@ zK7Er_4j*Kq=B~n@CCjzqFj;GFpru*2PZWa!6z-qt9{l$n(_+?U+cx~y#>eg#L1{ET z?*UZ?BZjh_TV}xuco2+)VpgV$+PlN&mBt?9wD)N<> zG~%)9iU<#_zF)Mc)O^fER3HYX2W~MP{Z1O-%UR|ne=8^?u^v@uEs}1~6QBf9OS4k@ z&y3c|6J|~Bv~)Tg05;MW%d2+Psn|EmM@UBNO0HzC2msSF88zHRMRrGJ2iTHkh_1gS zI4%eJ>8Y(GB=Y6%gZb+#6=L-B$Ga@xKL%7}I@I*kt6i|1OIXaVlFK_Lh;*kxfBcV-9t?C_qZ0OmtYMkPDU0Rm3JrxhMHdd{JBk=wlSLYuD^gv$U4wMIxR2aji!d+AyBoJ z0+NjRAypc2N%TOzdqm0LyevDe4@Xfa08qnipmfJF8-|yGkTpephVJ=x!VY7AjOH^} z>Y@4q)*Znf8yoh$c0e&S4VLzw1q^s)kfk}ZcE^;h4_F_i6_-FaN;2{RAkE^{$TeXHYH;BG zm`lrq(!`gy&!@#q*g(HR0Wx$J3cEW)7nO zeuCgG6@xb-CU2?KN2EuZ%WHM3Bibqn(*dEXu{0sg(k-4$hw`KqBL9?4rq9{1xb9d) zP1-{H+tq5wJ0KbzoYxn2*MFozpEJ@1>U(gjqpI>$UODJ$k8VTMDvu)n_O7fGek~^c z>GzV(HXQmr+50$uuDR=&WHhXOdG7;X0Igibtc;Z1(IlGs@W5Z(62rX_7LjO1*;sGs ziQyJNM}#vwy4wTZ&S6MwzfFGqTD08?DRp$zFNqap zpPtiuvAwPyp69@73!?On8r5mnYE`fAL1FSLDiI0-xKY<)E6yzKw{q9DGz|~)ONha7ATF?Tws(8ZgT|jKL zWNb0V+YK%=$)MmKzkG;KRvHdk=VENUBRVC7bdAMJ3;O~-=W*17s+WbgDweB1c6Ak( zic3K89I#Im%gsai7tWg8F|WIE#fR2z1ArBnhXz*QT>Xu4l(PQjahA$n(k1+y$)M*r zAtJP3b^9r0-riv}GgJ@7)K$}b-;|0tKT6#6hjU;BI062J=7PZBd|hjW8ttTms$I5V za+LfH?G%!ORW3_&+H{@F3#^68%tO%xJS(%kt$PLE?7hNvK_X>&02&6H1m3Yp!ZO*` z5q779c2~_>x@pkb*hn!grD1uxU+(?yto&aeqNo1tMJ(aE8R*PG>Qv1qP9WtbKE6?L@x zDXgr(LVAO)Jo_;K4H{fSLiJT_^o*mNtbo#)MvFP38--oD1Qra%K145DM_Cwe5bmee z^{DUyNAXGpTi0STiI3^C*ah#f?+56~Kj?PCZ%cBq`rTUe70D-Ti(4f{8%;QrMebI5 z8%@h+Dl~?jOc#YQcbZ_ziz@4htuc=HZ4^)Omh6z}qXU1#t&)(~n&Gw?uY;?hi1@@@ z?(U0Y#NE5e;SSJ+l1+OWu>yfvdSkrzi!oYUpT1n%EjJ}K%QIVzeB*uHv?JU33`BO*Lysf4^)h$Wv;-N#Ivgiknf4Cq%p{kn_gZjU>JG7DI)7IWDcMe}( z;;U_Zx|$Ua9&B*qLoUD}@=J|69>e{MF6Pg{mF2=9Rh!X=etDqLOQY7v_+be>={32A zLSRIaQERdiDyLtl7+?@&6p{d(e>rEh7l@FxDrv{POCgN1k!AVw1yuUp&kIt|zexy> zEiqY149yXdEtdecaemx9trzu*a%<^~#+p;IV8u_y=GS_8EM{MyrBu-xEmt`w{VVGVG(;RZeCYbt0s^5#t0L%=RA`Q&Q9 zPHgf6Jx_VfZ*?%4*Tg__IRr&ELaXIUgSp;UT|`QF6qN24L;G_*)3kP0VN^3GjpaTq zJFo*GHO?n3!I$?l_slNKy|%8Fj%QaoO1hRw){H`WxV^n=4^yGFqZXCqX8%MDQhot- zUivz7W6~dVLnn`Dp!LJ1g8^~xn*BH=lSNswzV*0sWK@Q%EJ@^+O^vSV5^GA?+xaV= zWC!1X_=Bm+k0|7orBz(irY##POxiO(9^@i(#Y6wmFD#X+stgy1Mw1dz zGx;=yz!9Q}x|Vv!;p;=1+(-;co=bZofr!ekr)RRspox-hLwgMseiL$}n2b?Rds_BZ9(JLjn z=@mAe^NQ*0j_oz>nkpS|kFE=d(9s)k7*-KW?f78A6)+mNO<|s!OaJf;E2asaMLfG=1J8Qxc#p5h!QyL=MedQ;=OEVdmyr z@}fAOiNd+q*<_FT-rG6Xyvm=>*P(IKdCd#ymqC2X+JABZ(vF6wqvVdNT#!IOSmxKq z4<(lOQAT}gGWsR~_rvY01qJrl_Pnf@-7;Lhp?nL51m@LU8hoUcW%Y;yAD644=kQSs zr6&1kSG>BT89loPbBBW+!68gXB*|({tEh6&Lk%2B8L)NQVw%-p_4mc=5Pc&F{MhCp ziV#_@A$VMukPMdm9a9Q*sx3 z2H7sbhAlUCv*=KVyd72@X@8(S7RuPLN>Gkl?h z7{tL$vowSgKl8DWlsVXlBmGZz+}xk$Ji}WUlgg`zt=X=a)$elwLZ+$o^ozMT<>J6m zd9BK$YsWUC?ES6Zuyy51^LzkXK@Tgl+?$R-$1q7pPSmZ|^8G@O#OZ`Hn!NnMKh^6o1GGo{N42@t5)DHYG7-U98uhO3?(pVS9NK;*Nj$rCN>l8`^nOOzMJ*e5k>p|?S3YQZrgOoz%#|Kp8-b|u zG@-YUjz;C^os3FgD8EJAga2JI{N*p35V0(7k;*hlhghLHJV9N6oTL0oNxP16uglOVF|ZATw3)HCJ2G+dB`PAoJb)Nc zE5^`^s0m?v(?nFrBWF1T3mTzn;>7C5aVo$dsH>H#pE_bZh|c!V8nkIkL()mOzD}kv z){5N5qp~i3I(ME0N6~^17HB>|-g0Ez&6(Xv(9r``#y{Xvs@q*2t2$kUJ;DvrG}{yC z_YG6dkh_px$sMbagJSB%HeZn_32#GfqXQZX#G3M}U;nmH?7vWf{MP|-ha>z}7pXVw z>9@K&xGvLbzS@(v{j>4i+Xz1R@Y2G>(Eu7kJi&X%PS;un1bRz~y(1Xw6o>*KI<+r+ zey!F0xpqElEE(0(12g;Pv6{BXYpAUhe%8w~)Ll4x-^g- ze%1$nrbsC1{juTYMeRi&u$pS=^n|;@-w1zYWDE?tO_dPqq7}6(^h*ey+JrY?xdJTq z49*yDttG@wi+{Y{2GJ0!Pqxt0MhckeaUn2yI@iz;Ip#aTohtS0l@Q-o@I<-)=(KXj{3(Oh1mSX65=y4cho}T`3UN=m?CZ7rZCN4SV7n2## zn`=mwWJ12@TB+Yc_bclHM%K`buN}?rd3h)bL*T1M`^m&2#+S92K8E0=!6>#Mn>49< z*ehKWqkk}$GHa?lv~a>J9K(v}&`H=LQ-9Yml_+f% z3i?0Hy?0bo`@S!Vy6lQekR}LAk=_KPw=D>4YNRAULWp!B^iF6tR9cW4I!llmNFKJ^PJu=RcYGjhWxfZzjL?{eFu5{YAVAQV-8^ z>Yu;T5@`l=P&e}2p@V5rx$88&Kc%dJ5sf6~zWkcSJ(OnKQ}{9BVBLWWYj~96yGGXJJ{^BIit6Z6RNyxDEj}pBKMv})#@a8{T3zxVel8 zM(^)|_QwwPp8y7`2)4aw31Sz2Jg5`ATv^=j$M(89;YP6G4POUJYEqJNYCqfk#2Vh} zS6#2K{XZT0f1dp@AJF4jXAq(iNfOUGF93N3Cmh2}`u!cw8Y;h7CvQARUJmemfAQ~> zo<1vyjLA`hJ{S&0PtTzT+1*Y7iZ`hR^pVNGwx?;~%!B>^g#a=7^}eJHYNt)f4t9srR1vE z$`WzMQ;N--yn)EndA4#~>A8jX8!ipo# zCnYM$cRw8xuiC>2Iw6I-i{x{=&^==bgGs*9R0;pF4m!}kx#8g|J!H;~rOen9y?QbeT1wzu*_c zO(D;{ED>xugp*RASC;-w@9|wcwIQ(T#7#+B#)kFcR7LZV} zIQM$1%hPHkfYKdi@yb$mioytfX#k0yQGkvwfr|p^CSRBu-yfg#s`iFOk3@B}oZZIm z$0P#)5e>KVmB$n^0>%?m4OwhS7QR?$*@jkjKCLS+#&vu$;E28tyQ9qkAAYVa*C-k) zbzyl-MD)$!>@y|u%iBNvmb)+9 zmgJ`%Ht|IM&YbP!el?G%eYt-#^$RT1pqhh=V%j(=H@|#s-~AaKh{)|P6z7_)-DP7S zHW03RyUTg?3sod-&UX1UxYY)!nY@-kWf{d}-i_i$yYv+=ZSOW?(gZm91PhDw+}$-` zkJ_8b0YRlZgQaO?O)*3R&&3?x-W^;_*NWYQ0Q>OQNAvu{*0yvDn7Hl^j~=42GouwG zQm@j$MDwyP>^Wvy(Fjp?P<9N-wh-Y}a>yCj8K;n}NI&`^t~fdnWhSdauYwUik{ zRWyE|r;=h?TQUyD5ovFsbsSN0~egVVa6q=*&ZvlH^)fXsCwM-p_rdKSTiV=`_E=# z;GR6$1pPqEq<*xp8b4YyVZ~u6PnZ5Vhq)T(v-F#;Bil;O?Q%};QICAZc)J`iil-UT z0oB?-p8T|O!))oTAGycyKtl~aAeYR%V6~wWUd+rddbAZSetvO9@(a{B2^T(qHYI4| zOSLb&K}4zcig2f8W?Q%%L&ca9nOC>jOuZGlN2n;v z9d7;erwx09Fenc#X5wbK%ltRNp3-3lr7~^U1i9elPftIFVYD3U>KX~-G-=ky*cS5+ zrF(AV(4j6L=xfvCi%LnvTQ$Yr5y|Ro;7{r>x|5&P(|c3r#LSqS*){`omy3U!h(>;f zq`5afPVKSY8aWT+-hq#m^C{@w#&3n1n$N|rI#u##DRxW-OL+-Le_g!%6(MG(k(oQl zh17KJ*h#6{fZ~bc@uK3v+B~Ek-GZrOS6+`~*ajgN!2wiFg$ zpAN;r#iIm=K9xQw;DKSQvP+Gh1>V=0lbC;&r!4^BjA$nt-X8bFaq*=Vn76u3J_x>< z4YaXUW_Qq-%{qohCs14_yx)#En9OoKYf#1>nXOhz9xstBLu4O1pCK( zh9lum{xabF?^+@MiMRBfxZ7Owb(Y|qqy|nAbnVB<^D8=euC*GClt%?(p4|~!KETMNhV5_X_5mP33 z6&%KjuKE@dy{Mw_=3@fLzOw)3;~kd)W1Tt~C9iL7`)tf{&&7{F4cEbO?m7~I*%Bz~ z)+GE9g-6EiBb*Yj$J0-1H~}}zXbPyrLMMhESlN)meat70omeJ_$g8Y-Ar>vVN2ZH> z?4>}A1pI(iKH~$wy>Sh9(CWwTJ@U3{Fx9IyOEJOA3!(C3Y^bh4CoU#EW_elr3dN^X zXVkXe_5zn>uQ9* zZc3CAH9TZ*dw+i0ci5OrPLYMM@A%DG*!NFD-jTI(`}y^sCDl!|-BvJZ!6Bg6lW#NtYOdJQ3bW$^N`9&twr*}_?D!BH#PZqpDQ{>W&qTIDGGi3i645ZV96=D-hrVg7J)~GJ#*?Tn(@_!2-UM# z_4H_5k%w~0Y+6Fp#P~31_#5)s4&K95T{2((psDH^-Fs)H?G|?XOuNl)@`iBY#2P}| z{AvBp;GzePE>^9ZFLx9Gfy)ZU%c-K4wW6f+>j9?)&NXVCh1{zU5* zu^V9QnF>|z6NelK_PiS3AWsGLELN@2WgOZwl4?u|ZFKMBs@cNs;?p!J9!@%w9f z5?QW<(ohWkD;$E-kw0L&jxxW&%{F+q6NgkZwE#|m=9O3@bww)IpB@EozVJ*kT*Z|Q zLY#>Mw7G1DPcOTQ4OB_ks)Tn`sk&zoF7nIJTY*p>#UdC*FARDQnP1sMCh3gM!?J5; z^k&{6b6!SRIO{acIC{f*5>Wtg>9R?O&B*FfgvMymhjH9YxB^LucSVGReWcShbMENT zhzi4G{<&DEkquSNju!CiMi;+FuWsv<64NXJ659KU1;9hdtw=Ox%&%HYHZPx8-}1zM z*cs&{z~(QMo*Mqqm*Av^^{wRjXx_R9(5PVk?eEtnYK<5@^*{qwJ0ks8>Lxz0+?}-V z*K1UPO+*@M4>Hw&JyoyC@S zFu^r&l;xzxF&qv{j$2N4sdrn8QcaJEN=zqrpU-;KsZ}41?s%uKmcv?;Q&QwdJVy0> z+z9}Dx`4{aj1^t;zIH6=urH)m3v5-OjU>ks;ze8I%Oz{8E; z*X!q&?uF1RFyT@WX#)`&M_DjLA%EP`t&UHi@kS~n_Lh@{bMbYOrrfr`$rOL_UhKVG zd|-Oe(}|Ot-=+EQbA>U-lJ;`^nOmE=Nt zhxL(60JiSK*xd47U(B*X&F<6gt%6r_IVutkcvac1M}^1jv-EaYHt6M`ucJx;A)$_opSo}vcJg z*fp!y^Rm`*KKWyQ&H__Z2L@E%;^w@18PE~sMG&UzLt&l4uiRgYJfShF-Q=?y$g5W@ z6w<*zm?ZK_@U`@J2%-z@?wNqqAtr#9sY|?!;xgum8r(^FzNL?1f{~oa&W_`fNMQ03 zs%0uo!n&5OI!fj;sAh#JPOUzr*$S|iDl^G`3#fJBvY0i(CaFLuE9bmEw^HCU*1+%> zE@$5zDkkkzCcMFktfvUH+T~Fl3}nirmbA6WRlAr4J4|_2~0{byD&WU zs*2|7F5baCTrkZ0`e?d&^GB9OWXl{cxbEo+VpvcGqq=_k$7TD_Y$}|TrAiVUo0V4T3^+*(u2n6mc7FV;i+8RzJjCdRZDYY{U_Zc6IxfK zs?ET|4=Y{rD0LwVbpId>>1RN9FkCWMH&OgdTO5ZPGsuwTOJUjG^p7*m72IDf;YJs| zyrQXjo=TMxP#7x^+q4kd1XPWDpV^`vbq1X5$My7x>wgsbaFlfIuBg zO}#_!`wLB71E&?}j9kqZ`6knQ)}I!NwN5lzUA? zydD4@lIR5Xz3ZLc{;4;kBNlJq1NP5s=Xf&rgqEet1p+-5O!BVxp!8r-R>W6sVfu64 zQpFZ8fIok}S}Ib3;v$zu`{`dn)H#rA%DHweBP_q*ezp2tcQXO;UAN&5>duP{we0Y) zW_drw>`e)4yX#5cO}zsGQx9xy2IE^N>yPA48bo~#lYcV8q~CO93ZKNccV&K?$cc{@ zjyke&fwXwBk65Inc$<)_B_37oKI=HugI)Uba;G)P!DDn3ndZ60tC=#Uz2buEMwQob z1R#|@&wA(C)ASzs>$wloE$biAVZjr=ukD&M=RJ=!yYvLEWNw(Bn>tKIN+qnH-16Jz z$Ox7{d9rexN_F=a^YO~e#GUk}+)8;-jfkw5N`sdIqz?J)<59W^A!^oaTOjiC;Tz=X zL^z*I`f@#D!=A?RQ> zGmgbIqNw)aQ5szh;?n-pmC)?M=B0QdR%)-}>+0Ukfq% zgAow!&N*lyzwSC+Q?RbsoX`zmqsW*~H`@X>mwfG$!2z1+y(wb`-WLp42%szC-~pPD z%&0i&Fv$$z+6F0e^El9>>yL|`Hj-%p&d^stl@AKW9GsMlIu^s8Ecgt}=apGl;vw%RV2=w+#W=(9N3QBHpD zQtx~8VkaRo8Wv{4GLY#cBoNI(6PzwZCZLOT4 zWh2J{oLp9n{O;FfxN1tVLIZ4$Mqzu=h(oIF!X zEONqpF2@-SpITjMJymtPb)Fkr)+vIH4Li}yktQD~_Sd+=vqnb2TNNNBcbIL?W$~+O zH}4ln27(Ug3#HKMi}Ny%(+~wC{VgpdxpJ#9m*@fS6YD?zS@`|m_pJYT{;y#Y{Ks1V zHSfKq(AdtNmQJYR*Xim^850u>Gc;c3Y@P?bJWXnv-dUt2TdH)P%W?Pk(wM>=)oD`L z+m*;NR=3yxiEY*eXtyU?Hs)n~ef>9761g=t zle@3*hw--Dl48EDn|+N4+tp~{oxqyHdLL43JO0^H$y*4lDk^XVJ||hmN01~kCE*e) zeDOzik(MV%Hv(_9mz;oPpPH_XC6{@JJgOt1pKQC$Z_jMaxwPQM*Wo4ed|RCwj5|x- z3NDtqs{~z8)1)6U`Ml5b#hKHG^4e_iFhFI=LEV(&#=6Wm$8_$ivEdvtvlQnB0*ex~ z*LhZ{ac&AC_5_^Vs`cN$`4APw)wvn2*M%YRAfUw%s3aNZs zA20$}bo+RR`m^mLCBw;yGeAR_a?W1A6tcvL`int#_mARC9Kr3TRRk%Ycyf)thbj+f z_{ETsJ^Q2@iV1p1HSrA&Ip}OTY`ZxZzYZ`Nx}&lm$V#<{a*)A48=*R68ui-Q3EWyO zXtw|vn8^TdAo%fHVv>#uUPFD0=}iFiIwByNLuCJL#tcjJ9?tMCEt!aY`4K&@9Gh?6 zzys*5SlLsUK+Nq}UmHqH@P3;V!9TwGQ#$v zgN-{VN2}WC*m-uTYODP40J!k674pc4a0iCkWzStX4U=jWwwvMp=D7|x{KarF>yUk0 zrg~$1e|Olu=YVC8;tGQ*iIi!$#f8;i9kxG7i;v$-=;@3n1h_?OKbW$cs|+|TY(=dg zWzz%{OF$47`f3M~{RY}-JfK;L?<3u8fxF< zk+dDPGM%t$e>D_k0!O`V?aAd&pOC0IdhzA-MfwKCpP^&l@UlX{-mrXc4Foibracw& z^p@Ne*_B)TOw zGjiJ3^7WGT^zxGJ)PwTWSWj<6J>jQy?uZaZY`cUd^QBKa8U}yZut}|XD3pk+4}(3znZa4A(wd!a=(J|G*4~-s8D`&tR6sQOGLY;BaZ-jL()1M`WodVX_v#1am+>jP+5N&0hSq zbpEGDGb;90NnpQfN2$zKNO$)|{pT$$+Yvt(X}2P%a}W}=KYxPxY<>*+dhzHqkU9r! zC->(kj-AbqoDZxrKjJXos(+9G#{WF&r&P^OT7`*8TM(kWhuiAY`uJb|8#V6tzh8{E zN}{e2Pq3NE$`J~>;$ZF3(VxyX2>;LD^EdN|j3?uzy@Q?JQ zZ5A5TY8%xICb^Wsk|i2DNl(oG8Sm$V$GR;G&PJyoESt; z>jTqWE2rb^*22D^X>j<6)tgjF#?X46N!=pAHsix26>c29KiA&2m<32N4pYVNIo!BB=&@vg$Ob$W~O$>V1jmBwit(_S?)ZD1DE!V1eTKn z%T1!o{o9t?%nBaJ2{u^wUtBP`(d{O?qM)BqL7g8r6)E*E;9>6LXqk)+868a)DR-j? zNhJTi5v}(^A9L>qHINHwSkiqW;%n=+V-4O6S?h{xg~oL@D}uxlhp5k=9y$DCkbHGT zU!fLe=;a$YzPo3^$$Vu)?a{PUgwBzHPkfYEqgTqS?SRn~ykT@Y z@W=a#{IGOKKdSEm)3svGD$XL%^hX&t`;%M|_f-PZ^4H*7cmyKC^WOFmz&8>5VQDYX ze!RrO>tb)K6mNPjM*avz(R%7b6N>%l5OeV0caA4yDy`c}iD!A6 zniHm+ZJ|RW0OyWfClfPui_2_F;`OMBW8_)KzgVLuE&?%>>n} zdf^6xHp2#Fkj3TPG_XLyx89qH|3eLdZYh3|qv!JZfC#^mFKrU#ozto`u~Ck{ihxEUt9Rc+!nrh-ylgf4v#>05eEiN8}6O@&UwukGOQH|Km zu!<#JtFVj4k-fQj_~0JDrJD2R;=cE^+9{iIX6WD%eiw<~4*&5~K1*F;WEDoiYT9fc zq%TUd|C~aVuCkUah%#LI^S{#S|No+E|CMj?&#zq!vDOHvm=hnHFBs|lgk6+8&yBAC zORBN%+5*+ZAvsnyRLXV<&tKH#^>O7uM>vx}x+&ECPc7M}hy9mnQPL@ck9E&N^l8%Z_E(^JD&+m9a zaCGq}pbO>J<8zWaWD@|mzOvI>m*y%3140=-s4&eFSqj|!HoKGhQvyw}&jT$`0m5=LF_Ypo}YZAO-Ka9x+#)zN}Ny#2e-PnO6 z&Kv1!i7~sxNb67VD(=wiaa-%ZQSBx>FJSmwbOawCMagh~nAsqaoaO5*SJf8%icWUY zIXG)$;m?cJ2O8dkvIpYDLCY89X|oiRh^bReo);WXx3C@LGMt=#Q88>t z8uL2B@GNieDHQ5TB@Pws8@XFGYgL0*5|vx?hN^X1)llxypBqs@A3I+z>X29|wvm)& ztTn+a3kea!;W5+vOgaapJ$s6`l!J+-^%fl5KBCEZ4FhHTPo3}&qqp6^H!pJt_Z-}O5u*ZsvQX&0(e1sBmxMIB++AWw(N;|(EJnTxT;Bmt2q|Ce)xCciD( z{dpdc*U$gq7FH?f76->j%GlAN)@`vdxnY23!E$wPFEf+3o3Y2rwAol^EKq%Heb+U{ z4IPGp7)?6l{w2Gvoi#hRYBzK-BrJD+Y1?fY|Izgc_&kFD+YU@S*jJ57VCz z)47vIIU252^kolg6htHKidr++de{UOx-e<@qL3C_Tk7#!k2Ykx-hF0I1AGZF;+=R3 zgQjif-q0Sdy_Kf(6;AH*JcQUksaIM?06-{rI$T3PST1c7cT73CXRTR7SUwqR$$vot zKs`c;3aIO-9}ivq#NhF1P>clZ`(W>a{JE+JR+w_V4<~{?tW&xXD#GN^*dBWab=96A zqzhKTizybafO_jSqSFIqj8YvePqB&}Ztk$gTO58raIAElG&-B5#EzIBqn`ysw!wPp z6^jJd0T#|@;c_=g-sUJcC;Ed3%F^zzkaQ|qv`~zz-Q=e|8=EUV*H^4sof>IGS)6%C zP1^AVTMK{9r~U{u?8XUu0StyAz3w_5Np2B0gfvJh@hBH7SJg0Tz;+1zI@uZRkYk4 z9{y+xN|IPZN4i(6L9iZHSDmFFb>kOh`6V(e^=z^wMXAV{G>XQd9zSj+;+J$ zJP-8sP}C`tmfhTi!p(KC)20H~s=~D$E_y~+#Or;Y%k1%*-c0HJd(+5mYt}+zkMZgz z!;mZib8plSFYj$hr0*1NJ& zT5WOfpib#tGzUoA?Q&u(g+s_SGd$Dy{ZXyO z0MC_D$DD8|KIA=>wJU~B@bU!eGRi`RD-9==Z}jmShkr{-TyJVi6d56OQdEibWO$7{ z;@zi=+-r84oq)e%>L9YNa~?wJn2As7aDupwG^)6}M6qhQV|E-$#?+NXP9@g#g<^_} zcSLYRTf2hmfoV>dh9um}&I4rFr-sn(6tVcRLls5rB|m!n`$nfj$-A{gtHPs92``hF zP`7U*>5-A|@n;47RVzj48---{HYF2hl{EaI{>zB2R}7l}Cx^%G$UZY>3EhrgTVP{v1iW_jO6=#_UX*3j((e)Q8U9v#`?t7~%y^4J+PX_;N< zcBI2=rFS-|nOI4)Fl^bgat!1vRf;YqIWmeN8Sx+=Nl&B1SR8oRZ!;sYJyb2@=u= zr)GwxAb#9pc(NT&w(Idq#gQ#@{w01E{QM%hqt-I*kUCjXNWA&g=d@h3{c&XOqHD&& z*7i3mEnrzcRIv0jpuzv!kDggJ9&JuRYKS@sb*9yxKUH8#U*|^Mlsb$}^=?X(#E%0d zBg5kjdTG`!Ge>#(xel+31_x(L@Wi&0b`iQ=d_)mI;5*dp zj`z`A@qnZ$7U}5~TeU*p`^<{Z;OhZ zE3X|?WE+p%O7YS=R&6cdh`Y(OEZ@2jwXOecZ_O=60ci5PtZXnn2ZTi$hv4E6bn*+W zwz~^jHA-=5|FX1zYP1PK@XK5zT;?O&8rTQBjsaa$1osgNP)>Ggm0=u0Kh2(M) zH1gwO9bzya``-j5FoJv4;<5`wV*EPuZt$dPP1?&k>4nb=++qRxL2mpxx2Uj==m|zQ zYY;B+)06Y`^oL33u&dgwoS`Xa6ecTPYsZTvyy4$jnW~ zLNJ9@7C>?1#EBY#&B2wb8>I!G+At$kJ=!HO=N0n{QqLd%_lAmVk9P%rTtdBB^!|gT z*Q7e8`}J=dw%&hSP)eC-XE0ollVV{0Ke}+$TScHKI;H-rC63|Z>wmf;Rp>LL!Zj5< zkI>0O!|%icy2BIR8FZ`?MPLZ!oe{J|EHP8YO^h@}OtOmWn2d6GT<>gOY}E$4q9(qn zxk_vP!f^N7KdtzGqwv2I%?U(VFYStd*ZaxE*Jh^EY z9<&v^7wo&n?k(GzQ4qL|Mb4w$vj(nXdx*H85njZe>F2yridl=k98a+2cQR z`{CqoFYz^=O#xedrCtn-uWy`NS$O5y+CEzjEgt{HAm#y+ZFs3?_{cG>k#1nSl5MNu z<2znFIbIOhNnNne$WjS@_d&&IC_<~w^yFY4$}y_|>ab{6Zng4;03n=iB8F+}PR>?C zuuw$=t-16v?5=1=iiWTSj`#qWSjG3XVvQ2O;axQ@QMpnkk;GvH7QlQ%gn}YI#5tuM zYKFw@+{=|nDk#v-9qg}(YhZOwwHc3-ZxaU%^Sm{NW5OMo+xxg{20g_pLvUH&VB!r) zCt6H?Ab#Fqlx8s$_N;h~!Va3~HV}J{j@fbsb}b<51r%V>_gy&BN4NfI~ zF>p8j4smpo_sC{5z80M;U(~wF@tC}=|9S29SG@c48pzWlMlE^quPaHoYCL;$rRgij zogqkg2;j!?;Lq7bzAci~!IRl>5e{k_n^QiXE>w2^GC!M`y}6I+H*DH_UKV*we7|2# zbL^aL=0cunagwo7qT@K4T?AMDsgJ`oj;NIA3(2Y*3xVz~WILQAE1^q6amPW3ahuf(AH z;!oBxs}jfIw8d+kfO7QnrlD=8Hi2Y|PsUNM{ONSz!zO?2){uF?EppdfLGggutNNZs zhmkbWAc5ZA5T;Ge+}Zk_nsg%kzP!(Vdlw3I`JAV_X>24SE9!TnMHtkhmAWm(XIV^m zIw>fdX5e=M;P`##+Yr+cyXs(K`$m)Xkj#NE&Mt661!T{fGk??;)H z2_4r0Rs2xF*Rhj+>*S>wR-{h`QQg2wPRRZz8FAVVKIyN}ic|QMvdwVeZ~su~|4zYw zN89*Pcg-E`RKJT>441ta7#8dF+$zb&VUM}e4(J4{Z@XQ#iUDhE4?rX9$+7i%Vbr3< z#LnjAka{;I9I!6Emh`~ygo(p)(!y8Rt!?OfenmReY{Z}A=g}daf4ruYqw>b?)&YW( z3u99Lp>?~#ZS#-~HfD|G_{C7*=xA5lC*$-ps~H?Pk_?1OwIzfru35pCt5;59KJ6xv z*p^v^cl_4KLBrbXKWxJ8ZRVB2!P_J=JCCe@eNoy?Xoio?6sB5N&9S?Isiov+aFOk( zy^P`)q2=6!tWxnQ^q55kK(12zpoqHln&Gc7%mtklxDO`Mm|?I@HqG-F!b} zUM3y`QZR#|UYJT_0vxI>2c5-73rinGb<8)0((Fi$^ki{Zlk*9;fxm_vxZ z{R~$H7e!NaJ5&f>3f!i28t>lfI{^G}rJh=hsiIsPzWnu%M4P&(FMW#&dYDqWcTHdv(c#i$>u9FP7#Zz3cnawR2e~rs%~L%QKFecb>mj) zK3)gh^*N_Ve?~KE*L__v!H$(N8Zn(q;Cwg!7XzKQ>hi(;7sG_S8oi;Xk7y@cEdYB* zWu>FAss)_AXi2CFWDe-w8bVy&qQMXDr~1x#7T4Cx2|yk`F2Q!2OtX#u7+Dkq&Qljh z&9Y-I<7iG;L!#Q4_TaIX@q1wssJAV71hfx6EaSdhH8z^ovN)5vxCU+0H8Q5d5U`a+ zCM~}h4hr~A;oJ5^T_L`H@ra*CO;FujEGsI*fIlD@R*f)q zq<`d)=Tz!^ppW|)=M=M7N9ilEN(;Jyv7(EtGp^+WIsO=x9A}=Hv^X{6 zV=?{3VA0!=tUte3wKEDeLhv8&hk3#y+HD*Z&Ym6~A>5XR-D`_l+p(YKqzb!+?oCTb zp`W_?(}KFVg#shiyH7c&gZvz!jMCg$Z|1eq~dg13c{qWOE&=xt|N~lmG80N;awIP6&XY< zMo8~LoQ6gY~?q!Ta0WBHzgad*Qylj}CD1TfmH$5g%YE0Hod zA;o#j;t6)7vrU?%ul3W+FNVd-sC@dv@uM9LtglW)Cwn5JvrAg(Qq7i?cIxgvkMXp6 z8xPxd6Psf5L@JdqG3iBFEqL=W8D|m5;`vmSAfucr(RF!PJpey*pDHOWGsDfCY?-AU zmjlwVcFQ4*RU&j+Lv1x8I_8RVymN0)LJxFX#m0jWuk07R1&8W|S%r&fpy$(b?}K??GrD^jlBJxtwZ<$hqkN)WMV;84ZMURRzR z;1{{8yoH_*J3OWTo9JVmwep7cnN$;Yw!O>w?%VZoEGTfE99ZiWH8zRW@mt!1>W?VH z$m60V>D*eDCoUmzR(Ca*W^EkY%}>c3S#ckcB1+}Rj@X1Gbt9vV4BL^}z}c|F4Ad_M z&XXRbHWDrSBQ=B_Aa-vgH^drC(fC@)gAdjpj* zx&a=D{#fqxy4BjI)p_rXz#F`C>M`8n(iHc|^QYQX_?)25*ey=BX!P*YXom-X}9bQ34Oe$nGbq?l%CAUjb;*4jyho` zS98|Ja@;Br><2X3-&gh@#gl5*IaX$!Acjuntzj$Ktn&0w-g~NiTTl)e#<>nPF!Txs zu1YB|BMlb$?Ia4bi2oC za?)-mz7V(9#8XA0)W4PC`1FsEhW5U05@48~=5Bk;_E{y1|KWD0W!NKsG z&p%%|+&>yA>u#y!h9)2*XsXng7i}dYw!O|#opc+U4e^Z;!ps`HVujIcP=4sABc-#y zU$%0YdQQ%5>cmR3)<&K~SnC(3qEsxLGaM~ji#C;!9$U{1l}3?-f_&)GS^co5U)p!C@mdp_r~Pepc+*^dTEl-{$HnP4Tl=!^*_w ziNv_XhFE|V_*}1hw`@{Li3iFv^#ngoU)={BC#Nk_l2Wa5XFc6_1mKCN-k%xNJJk3! z>7A;oBl&m>rVQayg|4oIC@1mWkGI_$_4(e2a8ihW7L65PH|2J(ee{<20`xU&w9u#8 z@c;$ujHuG$8KQx6)5Vy2oRf2J&_DpK922P6%VA2umcnn4QBYawZUvXx4QFf$%eBSbklX?7P zl*liJ%2ycKcP^t(wzbZO$7kh6@Li zGL~GRb$jvVUYJOD#C5~bRh-d#DtDvzPpL~K{nv8;W5_5T=zWJpJIHz!%Pm!n4kx|S@ViI!$UGQ zISgICxmK&wzD_krSgi*e>@J)}+rxMfQ8+MEsOVMRwszS@=;cqH`ZMDeE85-egNp0`<`in(V${AoCqeP}fXYfjfbQhyl%oA;v0R}FMz90IQw z&&_N*@$ap;0Fz?8bGGu?>EyfQa&MODg}?Z+aT4xFA7iPr;-AwsXpB&7@P zq)l=-ai#M;u>I<{K7P4o3b>XbO5nElOo}_oRT)QAPGah3XQYlns+t_8ZKr`~;cW9B zKWQQ8cQ;EzsrbkNMX|aVak~CPnZiFEhW=YhN!<1$2<5JCOj`^hpA`fP1V&lVhkwAm zq%)qlCo(4<$lN2=*>C+~c=cBx;H!NHT~A}5sjf|W$PnUyj~Z_2Ia60VpldB}sr_PL z-`4kjm5>DNk*z=sV*!Ub-P0zWp=%9CbfJxwr@?96B!!BwN(r7pUb>QdES;kFi(xz3 zYz`f-n^xSvTI$)~5C(Dy0SKMoelg6`_kO>(x=7>rGn0Cak3gTtX^(H}ciZrj&nFbE z?qfw_^89-g8V*y5fyE8tg`k>;EIie(5Yi}0UJx9-_vtmm1Nu0c&^q8tQTRF@4bkx< zd{*%ET0Jk%*p8-)?k3+k$#FY=8x4N-4pZ1P?l2w?1?!oeSbTilRTSb_`iv;OLWQI$y4~3--FOD+;!@qoH z7<%+b^6u+@8~m>nJbYaaFVzYK;tdRIo(^k$;J(z-@Th&X)C{jCVsOuXiacC9en~vY zR%K-NG0H~IYVt}H)CY_q6}-6g=kERDcW%E&L)M+>+AG#e49dO4#e7;D*w0PpT;R_O zkCP)_)!t=_wj*>135=ytm>51a{SHkvEq3>H%n`f^!(RfzgBM0ff)X#R_RBgdJritv) zwzY)o5H60Xe0b*h0k%>7*k|d77e!O#NTvS2jF%F>73napyam;X)U~nHkqkIV(2VsF9zzuM+TWSQdVK9C_UOjxo-9 z)}<>hE{x}yRDRp2TiKz>;X$9SZ&sNVND9Q?x|0y$JV3r;`8gJrYfbD^YpfrvSO1%of+z;_t% zXaC%GD{m_DT-ft8i&WmnVn29oHzNHJ7B%)7RhM5JcFGrK^R@e_x%SrE-f;enclT@k z9M%%+*>kRu3EJMiDRuk6AS|by5JCEsW4i_Rq)A20 zU1_k4n+|#C_5qce3(O|lkE3}?zxW&jxC{F4Qo z$>S>U@PT7S+Z+!wn}HDlMmpDB7wpFHGannIEYdR^;2U#fl?r_XAkHjxKxk38S|00IWXL%+HdJb&R%?VVPGHd zm)=fq6r}vd63;6s@9ATdRn=K(zEY*har|6hH5sMv%zOU>si(bh2yDb4d+M$UCn}1_jHg zKWdA(stASHRGw^B?dPTQ=l9+mK|Nq96B>xAZAwZ?^G;WumeU&0X&^L8{0BZc8!!~+ zwD5IV_B&h6(k?U;3uq8I>ASwH^jSDv$BP+K;mOsoB`9>+(!YiS(Oj10hw{7IhU{;s zS$h`fpZ%H~5UIn-T6a7wg0g~!Q{m`-@v3I1fv9T8GJ>>36O9jWaCsJuePNwYbbF}A z+Z@BVMHsXc+8>;{4^;__>53S)2Gtauk!I#}P@LBUSSdw+tZlHj zL#ZLuXXAMq|9nBzm!*&#uzO?n>7TUKz8s8&agBUuL(!J5OfXX8+=5Np7Z$3iuhiyc zMA}-uN~t@vr`dv7w#aY@~w*1c|-i!|DQNaAIE!#TX>k7QDhH`Zr4tmlEKe z{&?*oI|pnMzG{{e!7&=pCrolb=bP3R0&Z7XgPUjeV1{Srw%uN-mK~|F@JR*5+z$8- zNM?Ok^)~@(8RB&X3H7PR7pelj;W1aBymURcgV*oNoks7E9v-6Iv%~B*ZukrFLwQ=Vq!}~9k82=ZDRaFU0Z}mU;FGVxCjz$Hzv+MK+a#-KxVsACCg4; z=NvDevi1f+bMw3pmiQOBM$R!yYA|BP6OBMJE>y``Y2)!kK%R5K{pl)Fi zij?znjQyPGPGLFqS%mHFkrB8!CKi_~s+8#2;*P>>ST}ERP_(0UE(HyvYp(jxQ(#Xr z&G)lJa-uTo3KEiiWO0MpwHA>^>llMi87!Ez$RNm}c5KH?Kq1?s<=1M!^*z|zto!lb z+2}_n+s8nXu|jfoNrs8toXcHTt~-UBbLf^5AwqK0=gT7N>H0eaJHOnxw`EMKjwPxnAsG=$aW}Smqa(IQ zEL(|S;g?KZ%t03q#88o@3fnkoGYC*Ay7zHwUVVXbzDV|&f2gB4-+h$Nc%DiEca5D< zm<~>Izu}(gc7i<_U8;kEf&SoAP;}3_Xl!d-ESzDz=_Dbb5iL!d$5{ugN?usYn}(Rn zifs_NlYwRRzR(#xc(AY(4z+G)EB7fTRk;7NgoF}T{)@9cL|X&jA-D1$X5TAu@E3g4 zpHg-nemH8ZRUs55ej6*&PRXWTXdS4yjz#Q0l915oPNG*Vcr@45k8!?u!~&WBpj57U zp^NrADhVwFTlqsBxRH*%Q~Dn+k=$hTxDt5MrF~1g;7ny}G_Ow4G^`U(J}B8@sbW^+ z*8BoNtKyZY9^U3iF<*ib2XF20r~)U6jpOD2oT{<0Rnc_x?*Cj&h?9{DH3~JEwXq-A znbEX$w_#LX1%4*%?i&e3&@56GsLB<*)1_8R#aNFhluqvMZLhufbGvp#P4a;<>~25< z2hyoNlc49SQoHbO{YLf{3K6PP`Po=XtjVP89>{>IgXh9uv7XQl6G)B@ta!0Dh>I$t zCpT-#Pm~3b97FNJ6^8Z`G51ZdC}Uy^ToY#L@sL&P6AI;()lfr7X#m$rWhx3gewejG z_7oZS)Ph=XW2r+6yn!Uw8%J=YUplqsm8T1EfGpIk1Bzdoo^W)`8u9ReCg1LoenUHy zafL@)ANL+qc|zW4DZH(-Tm03$+)T?|{v%!{=&jE}_^7;nPpfqX&F!7swCdi5u;lOS zqN#cOf~WUWPLY@L3}?J{!-+ z1pBPOF7@cnfKK~Y9ya13I)&faM14DAc|Luid3CKn$4@Za8LYUZjt zWtDO51l5L(v7l6#SF-5%r^C0~;upisYnbEc#u)t%DuQngzpYnd7C=!FxUwVUJ(h^a zrzC@X`nS<3Iyu zy;7~fU#dT_ECoNQG4OB+^}urs>gevDAe}BZ8xIg`U+?(^o$ie7rs*AH4l7P*UnGO~ za(Bv2M?InH-`QlPTiHz^+FG)e0-icDM7txSJ;ni_dYYbyM3HQ#R#WcO9=tw;t`-sP zlTx#ee!lV9%!*J!DPzd+a`dD7y*3K962Hazn7qej!4$ZlT#Znfv?)w?!M<#eLj7BR zznp7inOiT9TBc7bMa+p`DSl}$V97iIhoyLUZ%Ro0$Y@V%@Ygjjv}gwO1)icT%9mdh zl(`eRL2SUuk&UBs(l}I?=U0R6fOlJg`vw{@Lsn0oB@`6X^oICN7%_THq~1SESg_c;PUVd1B75t|~uvv)zJJosW2#i}mQpYLXDKVHXcuO*oE2gL#ZZ%PNE28$7^MHqc z#<2aiiH9BcDUXle)X-V`%-%*(0feTO>+M0!4y{X0J}r};w)On^J!PVGp3>7Sa;>Je zW!o{hnwk5QXkXk*BhR^~!71mf3n8KAyK8GKqrXeqeT2|v=19A?qwV8*x)HF~I5K2i z&Wxp^hSnLA=uj&HA{?J%0gXy~KF<@M*#$3UK?UM=0|*TVYkB(uIYw^VlE8SbEXt$-43l zkX&>hpk+w6IUAhz!#g-F_ii1)l_h2MkT7;h=5EwzMAqTFzja??Xjc=a?cjhIVLE_=0BlgXZ{X|{d#=%DXxF5lVGBp`*T1b%n_ zBn#VMlNk-usK9{e8mk~y>+tG0f1q@6eyA;X?b7Ays9?>p3wsS%iBR8?*~qeb^}Vm~ zg;9%ns`~MG4KSv#5b1kwrN@{r3p>&xBU+ic<}g1l{T0>MDrW=n$Rc7Jt!g4!&Z7Vuj4n2orB|XV&wk@Mudi z69i$Uw4TJxyoCtb^6f88IH^HtrwzQYN_3@o9PlSp1BV|X4HWD%JKURQr@iWDc*N=i z2o(x!j**q&X~``P;Hg{3v@E0q^gS(KT|Lqh_1@n1sv=E$f5kUILX&G>N!^B%7Ffj- z$gzywPKcIj1i1^~+>wwf2nomYqKn;*!3?esl4(IQrgI3y_2ze#t+faD{j^;~CnhN) zr%QTKr#r6PG!+2K!iwNSnxCsia(a$mhNGH;<{D;`&78~Se;qu;C+c_lue$A7rq9CX zwe(OFnq5scYSJQ^5bK01X9?bZv!B^oPbEO*h8z)IS?S58?0Y$(0}=HBjqpR>Kqs99 zh=O{bWZqFOR${zSgERF6r*sUKjOGWlcrJKZLsZhZC(*!y;mTye3&(O&QCHp?N2R&IKe#$-l zuz%6Nj!IF9w$8S3Ha2Hf0!;ZXe`r&vFs0i@bg{cjRyk2FKd>CO{DCimbE*ldE0Nc7 z4@BiCz37eu2*{DS6k-CvruJQ!cvtp1XnHqR&h~Bf`U3WpO60N)_ycpVE5E3;#eUXI z_+A9N(P75)ESAJgBF(_9VKNEl(`{~scy-AmRx3N-&npZD9ebtGpBiDwA| zgj&nxecpQ#lRoueUaup}08^>pIM(e|m3Avg1;Mg-;O4(8FH^7F1iB%(`1&pN`mS}M z)j?=x)d=v7<~qu-#Sz`rXy-#)sBQPMn$29H0xsU_yO=WQQ$R+!dx7md-hC@?(fxfU zGB(Ku;RJdz9+l?>wpr3MC$(*_b8lD~+6#LG$H<^B+169qw)gqz@BOLb3FbXIIEf>; zT!?#`;PQ@pahabg*30|jqYiZ1aTb-g@tF_=|C!0a^? z+P8gnp`wm6xv^#?x3IuH6~4q{mON-nCpzJn@vg!H<}JST{jssC?ho~SGh{YFomVp0 zYsQRU+}Jq&i>9yt$ZO8B*9TO17D>OPpB!9YIw%MRc~QPRI4cxPu+BnrJjt2_TYbk#Tgu( zB|S2B*8b82e|tzWSMJlobORk-@snjcxW_{m+T?nF9WYFHZ^M1e&KkuXo;GC$VqevrxOyt6d!e9DC;1=D=cI6j#m-)%DVmOOI{hYSiO)V8^oL_e*zLFAwoV z&FH#KrsO+xNCfaC?l+T-ypP20X1igNfx5j(akhc5UwcOG!_-6%E%tpGo+|DkF0_b% z5`+BwJ%WHxs7ghn?A_ruqI&i1<`&&*_=I0P&KJZSReL(nC7RchgU_DPt>g$~Sf&+9 z*p00@nNBv&Y=f<4PJy#0%6y7Pk_E6$KnG{^0%h~&bj2~2rY94Pdt+j_H?iT@qKecG zdt!_;cgc2_Q%);pssH)?&VvhKsAy>K^(~l)4&~GZr~HgXG|2ebQc2TXK<^UNN1A%4 z{`pfDM>~?#KLmzKn;XBPa5E%-Tb$L^r>be!B1Wi--3vy#Z5|K_sAK^sQbP_F`X)zR z4Qr%EuIvcb4J*>UJ9^j2I2%=*MY_FCBFUke#)M7e`871u^KLt`oZ;_e`UTP>;a~c^ z@}eW`CZ>&9M#;Rk(`jMIHi?@0V-lpWPD{S-jol)%q)JcVCOf;w=I4+(H@ZdPj_Oc{ z!V^H5_jBu~uNPh;%crx}(xMUv2T-X4{XKmRgP9b4*i(rRo_E=0BgIHy^L zTK>GZHj_b!krC6EN-PZjYq5rKNBLBbAjcv776eY7eN71!UtS?3DEtf zbr1s~Nqobr>mJwHxf=;osfolm?>v(V^TO_^UQyr%#-y(GgTATK( zvU7Zz7_5kV*aCcm;T`7;)U8-*tmr*-RJSTqHPbX2(5;VL0Xc8aO@Uj8pJ{M6l=gGHtAZkM`{$Q3q<|{TsHc=ZHcf+rOKI*|+)Z z#g$0yzj`8nIA8#mEQ8Xw{-z`!0eC;gr6N|l+B!-c?NHw>j&rSj@H<;X%*t-`G|^`*vj)aR_cm`i6>Tbifn%wJe_e~nO0uXoSS>ff zczJM&KU%(k2!PC6Mi%&cbcaqC1!eBD%Fnuufudu^(Jtu3K0-Hd>q ze8_nIA)k%B)# z-D2;|xCC#6W~DSS#;ab*2LKHQB$zx^BCFBO+k{|49MwCoFf(_#rSZOeJW?QCDdVDj zbQLrx+f_@&KbN@0&L4(*8F1-J=`LMEq_%Bs#s(QptZZvKG#lb*NjCAbn-_vjNnFT% zc;@;)qZZ9#m3JdD31_~r+5h|>OY{HB8@on$&JtnCVS0R>Uw&e|?nFzfE_1ZLyAi0l zx;ZiTLt?%sLX*M5GfJ!z^T#mk4pRh|iJIzY8I&KkWp?-_2L;ak{CUbf|L6a?5cn^w zs^fh0%l~l``Ma4$WZzU_-R7m%L==G*8 zK9XRa`nR+J`ybI`S$-ZkNj~Q2hc)lc7TZtOXlQ+&&A>BTA|{Vph=P+-`P2&0VE@TE zVe~_*qkw9RZLZJS%PN8-LuxHWJP)#LTvO0GD9;$=jPFp!w;zcGC^);(Q!VWrcLu~! z2U)T{5x?3?FU$*PB%9n*blzd)yrX1Yx_JTK%=bCB(cf=joM}e9rE&0u%5o|u0Dc>a zgFP{OGj^hs9(QUEJHAayapil{;H1kl2jEdP04N-c=|_EDt3HVZT3D0~&E=GhBTTTo zs`Cp_z7NyR>fKApMim>1_O>DK?IAV~+ivAPZ#C78DsWq0421Gf)F4fjYe&5tS>IY7 z`FhsE8|vk|YwQW}<+AH9o2=+D(T>yl!b$iPnG_pbQ|5ZLLS??x?Yj!rhnYt3P5wLO zdj{CWua?aTojhh&gei{tSx@rP)99TE5M_KYl9R4g;OaVBVTwvdYpPt1?7!cBju z^Bw65r2+Jc)7gI2jd&)laGa61Fet3s4wf1~iDs0@uX|J&;^YLh9PEGk@(N<(ESw}$ zW_WRHiYp7bwVquH4*;d)2RStK^ep)!6?5I3>-hHGhdgyBOY!apzO8!U;CL+r#k-A& zlbzcrSbN`H8MRh+t&pCXWIEuk0>+Yl&`A#t0j(Tc48-df?IBl>K;Ce0g)$XyU&dGq zPAIs`IX);24E4r}$#%j;#D|o?f^x>3m}5|AA45~m*v-{4AtxiINUQ=EGDbsR>YEJ) zoWEkH@2UoEz0R2P|&AE^8KBT_>< zN`A;2^?KSG^x;NmFtjpHmhcJP(|?!z>UOU%MAgJ_m!VY@5U`Q$9uk0jwcRQgtZj1k zC%BR2k8QCwiq1yE#NFd_0f7Ecu%u8!ZjyQXlB7pLcIOgL>2JHNahpa^h>ztVO9@gT z4oLmZ##sN(_UuBYv%%^`qDVC+wi#IcW}QNpzZ3Dm9p)?dBG_%>h$#QyoC zOSoXt=sj=*Zu4-G&7x& zEjdkS{vowDmLXL#t{T0;eONR>d_zlEE8{&iTw1QU(+{Z2y=6Q`!GCOK(a6=l=zN2{ zwX!y*xye;-Rw=V`N{hiGoXJPi#pf2T{s)0Mi_6PB#H`Gw-eg9l!ur{;#3i|GRt0|` zN67L-6X>|vAofwi%=UWSh{FPsI&NZC%_1VXT38*8ih*>psUpG`$J?XjPP!g%pO^f8 zD7Ailz{8Or3<3=W&u&yfHN6`wb6!|w3%^Cc%lEHst>l!z(egbc)!eS|foJ9l1*yr; zoW#}~`E!3YEL7cyY9eE%tQPUT&R_NJ<%Dhrc`K&aHCkumPWs$dYwTnoTn#c0M$pxK zxj)`$8MDzJAwrpUxRm@yfu+Fv92XW*f}FpT&4-t&JoU4?QpTRWYu1MtZ*MD?Rd;+& zX+GXQDE-w-Hx<6A?P#F-&Ui`+H@l*Woj$fmln^YvnjmOqc-FP{TnN5~?x8_-B}o~F^jZ~?B_?rw?^E&vx<0Q$ z#3J;{(rU&O2({R4m9JY`f?D2fJg7$}Qa$QXO^Olzq$LJe8SfWb@|SBVw_cqqXRT*b zPY15;vfX<6PY9Hp<7C^Weub`j9Z2`T9Ul@P(lc?nz&I=RX=YChY@!QX- zhA$ud`-6Xl>tVF?mb74Xc9@i;gGld)m8|YZK7#9P)`v#@L1L_ofMr5D><5{b8*TuV znS3|u3mB$})FyJmj0>Whw$ctW|9Cm|;|9yeE$pq*hCqtI)XX1a?|kq0x(1!qoqzY9 z)%(rPjHmucQ@ium)A|2C{qM{2zbPF2T%*qFE~xQ^HK@H+(%@TC>Rpz69ocndXXZ8% eH#G0`N!w7d0VEoD%_1r3+S7+ki7QvXll~8YCC)4W diff --git a/search/search_index.json b/search/search_index.json index 50b2b6ea6..7c858c2f4 100644 --- a/search/search_index.json +++ b/search/search_index.json @@ -1 +1 @@ -{"config":{"lang":["en"],"separator":"[\\s\\u200b\\u3000\\-\u3001\u3002\uff0c\uff0e\uff1f\uff01\uff1b]+","pipeline":["stemmer"]},"docs":[{"location":"","title":"Home","text":"\u300a Hello \u7b97\u6cd5 \u300b

\u52a8\u753b\u56fe\u89e3\u3001\u4e00\u952e\u8fd0\u884c\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u6559\u7a0b

\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

\u5168\u4e66\u52a8\u753b\u56fe\u89e3

\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

\u4ee3\u7801\u4e00\u952e\u8fd0\u884c

\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\u3001C++\u3001Python\u3001Go\u3001JS\u3001TS\u3001C#\u3001Swift\u3001Rust\u3001Dart\u3001Zig \u7b49\u8bed\u8a00

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

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

\u53ef\u8ba8\u8bba\u4e0e\u63d0\u95ee

\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\u540c\u5b66\u7684\u559c\u7231\u548c\u652f\u6301\u3002\u5728\u4e0e\u8bfb\u8005\u7684\u4ea4\u6d41\u671f\u95f4\uff0c\u6700\u5e38\u6536\u5230\u7684\u4e00\u4e2a\u95ee\u9898\u662f\u201c\u5982\u4f55\u5165\u95e8\u5b66\u4e60\u7b97\u6cd5\u201d\u3002\u6211\u9010\u6e10\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\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\u5373\u4f7f\u4e0d\u662f\u6700\u4f18\u89e3\uff0c\u4e5f\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\u53d1\u8868\u7684\u300a\u56fe\u89e3\u7b97\u6cd5\u6570\u636e\u7ed3\u6784\u300b\u5df2\u88ab\u8ba2\u9605 27 \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\u7684\u65e0\u79c1\u5949\u732e\u4f7f\u8fd9\u672c\u4e66\u53d8\u5f97\u66f4\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, night-cruise, 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 hpstoryC# justin-tseJS, TS krahetsJava, Python night-cruiseRust nuomi1Swift ReanonGo, C sjinzhRust, Zig"},{"location":"chapter_appendix/","title":"\u7b2c 16 \u7ae0 \u00a0 \u9644\u5f55","text":""},{"location":"chapter_appendix/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 16.1 \u00a0 \u7f16\u7a0b\u73af\u5883\u5b89\u88c5
  • 16.2 \u00a0 \u4e00\u8d77\u53c2\u4e0e\u521b\u4f5c
"},{"location":"chapter_appendix/contribution/","title":"16.2 \u00a0 \u4e00\u8d77\u53c2\u4e0e\u521b\u4f5c","text":"

\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\u7ed9\u8bfb\u8005\u63d0\u4f9b\u66f4\u4f18\u8d28\u7684\u5b66\u4e60\u8d44\u6e90\u3002

\u6240\u6709\u64b0\u7a3f\u4eba\u7684 GitHub ID \u5c06\u88ab\u5c55\u793a\u5728\u672c\u4e66\u7684\u4ed3\u5e93\u4e3b\u9875\u4e0a\uff0c\u4ee5\u611f\u8c22\u4ed6\u4eec\u5bf9\u5f00\u6e90\u793e\u533a\u7684\u65e0\u79c1\u5949\u732e\u3002

\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

\u7136\u800c\u5728\u672c\u5f00\u6e90\u4e66\u4e2d\uff0c\u5185\u5bb9\u66f4\u8fed\u7684\u65f6\u95f4\u88ab\u7f29\u77ed\u81f3\u6570\u65e5\u751a\u81f3\u51e0\u4e2a\u5c0f\u65f6\u3002

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

\u5982\u56fe 16-1 \u6240\u793a\uff0c\u6bcf\u4e2a\u9875\u9762\u7684\u53f3\u4e0a\u89d2\u90fd\u6709\u201c\u7f16\u8f91\u56fe\u6807\u201d\u3002\u60a8\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u4fee\u6539\u6587\u672c\u6216\u4ee3\u7801\u3002

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

\u56fe 16-1 \u00a0 \u9875\u9762\u7f16\u8f91\u6309\u952e

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

"},{"location":"chapter_appendix/contribution/#2","title":"2. \u00a0 \u5185\u5bb9\u521b\u4f5c","text":"

\u5982\u679c\u60a8\u6709\u5174\u8da3\u53c2\u4e0e\u6b64\u5f00\u6e90\u9879\u76ee\uff0c\u5305\u62ec\u5c06\u4ee3\u7801\u7ffb\u8bd1\u6210\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u3001\u6269\u5c55\u6587\u7ae0\u5185\u5bb9\u7b49\uff0c\u90a3\u4e48\u9700\u8981\u5b9e\u65bd\u4ee5\u4e0b Pull Request \u5de5\u4f5c\u6d41\u7a0b\u3002

  1. \u767b\u5f55 GitHub \uff0c\u5c06\u672c\u4ed3\u5e93 Fork \u5230\u4e2a\u4eba\u8d26\u53f7\u4e0b\u3002
  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\u3002
  3. \u5728\u672c\u5730\u8fdb\u884c\u5185\u5bb9\u521b\u4f5c\uff0c\u5e76\u8fdb\u884c\u5b8c\u6574\u6d4b\u8bd5\uff0c\u9a8c\u8bc1\u4ee3\u7801\u7684\u6b63\u786e\u6027\u3002
  4. \u5c06\u672c\u5730\u6240\u505a\u66f4\u6539 Commit \uff0c\u7136\u540e Push \u81f3\u8fdc\u7a0b\u4ed3\u5e93\u3002
  5. \u5237\u65b0\u4ed3\u5e93\u7f51\u9875\uff0c\u70b9\u51fb\u201cCreate pull request\u201d\u6309\u94ae\u5373\u53ef\u53d1\u8d77\u62c9\u53d6\u8bf7\u6c42\u3002
"},{"location":"chapter_appendix/contribution/#3-docker","title":"3. \u00a0 Docker \u90e8\u7f72","text":"

\u5728 hello-algo \u6839\u76ee\u5f55\u4e0b\uff0c\u6267\u884c\u4ee5\u4e0b Docker \u811a\u672c\uff0c\u5373\u53ef\u5728 http://localhost:8000 \u8bbf\u95ee\u672c\u9879\u76ee\u3002

docker-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":"16.1 \u00a0 \u7f16\u7a0b\u73af\u5883\u5b89\u88c5","text":""},{"location":"chapter_appendix/installation/#1-vscode","title":"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/#2-java","title":"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/#3-cc","title":"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\u987b\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/#4-python","title":"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/#5-go","title":"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/#6-javascript","title":"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/#7-c","title":"7. \u00a0 C# \u73af\u5883","text":"
  1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 .Net 6.0 \u3002
  2. \u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 C# Dev Kit \uff0c\u5b89\u88c5 C# Dev Kit \uff08\u914d\u7f6e\u6559\u7a0b\uff09\u3002
  3. \u4e5f\u53ef\u4f7f\u7528 Visual Studio\uff08\u5b89\u88c5\u6559\u7a0b\uff09\u3002
"},{"location":"chapter_appendix/installation/#8-swift","title":"8. \u00a0 Swift \u73af\u5883","text":"
  1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 Swift \u3002
  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/#9-dart","title":"9. \u00a0 Dart \u73af\u5883","text":"
  1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 Dart \u3002
  2. \u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 dart \uff0c\u5b89\u88c5 Dart \u3002
"},{"location":"chapter_appendix/installation/#10-rust","title":"10. \u00a0 Rust \u73af\u5883","text":"
  1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 Rust \u3002
  2. \u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 rust \uff0c\u5b89\u88c5 rust-analyzer \u3002
"},{"location":"chapter_array_and_linkedlist/","title":"\u7b2c 4 \u7ae0 \u00a0 \u6570\u7ec4\u4e0e\u94fe\u8868","text":"

Abstract

\u6570\u636e\u7ed3\u6784\u7684\u4e16\u754c\u5982\u540c\u4e00\u5835\u539a\u5b9e\u7684\u7816\u5899\u3002

\u6570\u7ec4\u7684\u7816\u5757\u6574\u9f50\u6392\u5217\uff0c\u9010\u4e2a\u7d27\u8d34\u3002\u94fe\u8868\u7684\u7816\u5757\u5206\u6563\u5404\u5904\uff0c\u8fde\u63a5\u7684\u85e4\u8513\u81ea\u7531\u5730\u7a7f\u68ad\u4e8e\u7816\u7f1d\u4e4b\u95f4\u3002

"},{"location":"chapter_array_and_linkedlist/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 4.1 \u00a0 \u6570\u7ec4
  • 4.2 \u00a0 \u94fe\u8868
  • 4.3 \u00a0 \u5217\u8868
  • 4.4 \u00a0 \u5c0f\u7ed3
"},{"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\u8be5\u5143\u7d20\u7684\u300c\u7d22\u5f15 index\u300d\u3002\u56fe 4-1 \u5c55\u793a\u4e86\u6570\u7ec4\u7684\u4e3b\u8981\u672f\u8bed\u548c\u6982\u5ff5\u3002

\u56fe 4-1 \u00a0 \u6570\u7ec4\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f

"},{"location":"chapter_array_and_linkedlist/array/#411","title":"4.1.1 \u00a0 \u6570\u7ec4\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/array/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u6570\u7ec4","text":"

\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u7528\u6570\u7ec4\u7684\u4e24\u79cd\u521d\u59cb\u5316\u65b9\u5f0f\uff1a\u65e0\u521d\u59cb\u503c\u3001\u7ed9\u5b9a\u521d\u59cb\u503c\u3002\u5728\u672a\u6307\u5b9a\u521d\u59cb\u503c\u7684\u60c5\u51b5\u4e0b\uff0c\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4f1a\u5c06\u6570\u7ec4\u5143\u7d20\u521d\u59cb\u5316\u4e3a \\(0\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig array.py
# \u521d\u59cb\u5316\u6570\u7ec4\narr: list[int] = [0] * 5  # [ 0, 0, 0, 0, 0 ]\nnums: list[int] = [1, 3, 2, 5, 4]  \n
array.cpp
/* \u521d\u59cb\u5316\u6570\u7ec4 */\n// \u5b58\u50a8\u5728\u6808\u4e0a\nint arr[5];\nint nums[5] { 1, 3, 2, 5, 4 };\n// \u5b58\u50a8\u5728\u5806\u4e0a\uff08\u9700\u8981\u624b\u52a8\u91ca\u653e\u7a7a\u95f4\uff09\nint* arr1 = new int[5];\nint* nums1 = new int[5] { 1, 3, 2, 5, 4 };\n
array.java
/* \u521d\u59cb\u5316\u6570\u7ec4 */\nint[] arr = new int[5]; // { 0, 0, 0, 0, 0 }\nint[] nums = { 1, 3, 2, 5, 4 };\n
array.cs
/* \u521d\u59cb\u5316\u6570\u7ec4 */\nint[] arr = new int[5]; // { 0, 0, 0, 0, 0 }\nint[] nums = { 1, 3, 2, 5, 4 };\n
array.go
/* \u521d\u59cb\u5316\u6570\u7ec4 */\nvar arr [5]int\n// \u5728 Go \u4e2d\uff0c\u6307\u5b9a\u957f\u5ea6\u65f6\uff08[5]int\uff09\u4e3a\u6570\u7ec4\uff0c\u4e0d\u6307\u5b9a\u957f\u5ea6\u65f6\uff08[]int\uff09\u4e3a\u5207\u7247\n// \u7531\u4e8e Go \u7684\u6570\u7ec4\u88ab\u8bbe\u8ba1\u4e3a\u5728\u7f16\u8bd1\u671f\u786e\u5b9a\u957f\u5ea6\uff0c\u56e0\u6b64\u53ea\u80fd\u4f7f\u7528\u5e38\u91cf\u6765\u6307\u5b9a\u957f\u5ea6\n// \u4e3a\u4e86\u65b9\u4fbf\u5b9e\u73b0\u6269\u5bb9 extend() \u65b9\u6cd5\uff0c\u4ee5\u4e0b\u5c06\u5207\u7247\uff08Slice\uff09\u770b\u4f5c\u6570\u7ec4\uff08Array\uff09\nnums := []int{1, 3, 2, 5, 4}\n
array.swift
/* \u521d\u59cb\u5316\u6570\u7ec4 */\nlet arr = Array(repeating: 0, count: 5) // [0, 0, 0, 0, 0]\nlet nums = [1, 3, 2, 5, 4]\n
array.js
/* \u521d\u59cb\u5316\u6570\u7ec4 */\nvar arr = new Array(5).fill(0);\nvar nums = [1, 3, 2, 5, 4];\n
array.ts
/* \u521d\u59cb\u5316\u6570\u7ec4 */\nlet arr: number[] = new Array(5).fill(0);\nlet nums: number[] = [1, 3, 2, 5, 4];\n
array.dart
/* \u521d\u59cb\u5316\u6570\u7ec4 */\nList<int> arr = List.filled(5, 0); // [0, 0, 0, 0, 0]\nList<int> nums = [1, 3, 2, 5, 4];\n
array.rs
/* \u521d\u59cb\u5316\u6570\u7ec4 */\nlet arr: Vec<i32> = vec![0; 5]; // [0, 0, 0, 0, 0]\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
array.c
/* \u521d\u59cb\u5316\u6570\u7ec4 */\nint arr[5] = { 0 }; // { 0, 0, 0, 0, 0 }\nint nums[5] = { 1, 3, 2, 5, 4 };\n
array.zig
// \u521d\u59cb\u5316\u6570\u7ec4\nvar arr = [_]i32{0} ** 5; // { 0, 0, 0, 0, 0 }\nvar nums = [_]i32{ 1, 3, 2, 5, 4 };\n
"},{"location":"chapter_array_and_linkedlist/array/#2","title":"2. \u00a0 \u8bbf\u95ee\u5143\u7d20","text":"

\u6570\u7ec4\u5143\u7d20\u88ab\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\uff0c\u8fd9\u610f\u5473\u7740\u8ba1\u7b97\u6570\u7ec4\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\u975e\u5e38\u5bb9\u6613\u3002\u7ed9\u5b9a\u6570\u7ec4\u5185\u5b58\u5730\u5740\uff08\u5373\u9996\u5143\u7d20\u5185\u5b58\u5730\u5740\uff09\u548c\u67d0\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u56fe 4-2 \u6240\u793a\u7684\u516c\u5f0f\u8ba1\u7b97\u5f97\u5230\u8be5\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\uff0c\u4ece\u800c\u76f4\u63a5\u8bbf\u95ee\u6b64\u5143\u7d20\u3002

\u56fe 4-2 \u00a0 \u6570\u7ec4\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\u8ba1\u7b97

\u89c2\u5bdf\u56fe 4-2 \uff0c\u6211\u4eec\u53d1\u73b0\u6570\u7ec4\u9996\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u4e3a \\(0\\) \uff0c\u8fd9\u4f3c\u4e4e\u6709\u4e9b\u53cd\u76f4\u89c9\uff0c\u56e0\u4e3a\u4ece \\(1\\) \u5f00\u59cb\u8ba1\u6570\u4f1a\u66f4\u81ea\u7136\u3002\u4f46\u4ece\u5730\u5740\u8ba1\u7b97\u516c\u5f0f\u7684\u89d2\u5ea6\u770b\uff0c\u7d22\u5f15\u7684\u542b\u4e49\u672c\u8d28\u4e0a\u662f\u5185\u5b58\u5730\u5740\u7684\u504f\u79fb\u91cf\u3002\u9996\u4e2a\u5143\u7d20\u7684\u5730\u5740\u504f\u79fb\u91cf\u662f \\(0\\) \uff0c\u56e0\u6b64\u5b83\u7684\u7d22\u5f15\u4e3a \\(0\\) \u4e5f\u662f\u5408\u7406\u7684\u3002

\u5728\u6570\u7ec4\u4e2d\u8bbf\u95ee\u5143\u7d20\u662f\u975e\u5e38\u9ad8\u6548\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u968f\u673a\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u5143\u7d20\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig array.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.cpp
/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int *nums, int size) {\n// \u5728\u533a\u95f4 [0, size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nint randomIndex = rand() % size;\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nint randomNum = nums[randomIndex];\nreturn randomNum;\n}\n
array.java
/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int[] nums) {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\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.cs
/* \u968f\u673a\u8bbf\u95ee\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.go
/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunc randomAccess(nums []int) (randomNum int) {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nrandomIndex := rand.Intn(len(nums))\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nrandomNum = nums[randomIndex]\nreturn\n}\n
array.swift
/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunc randomAccess(nums: [Int]) -> Int {\n// \u5728\u533a\u95f4 [0, nums.count) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nlet randomIndex = nums.indices.randomElement()!\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nlet randomNum = nums[randomIndex]\nreturn randomNum\n}\n
array.js
/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunction randomAccess(nums) {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\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\u8bbf\u95ee\u5143\u7d20 */\nfunction randomAccess(nums: number[]): number {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\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.dart
/* \u968f\u673a\u8bbf\u95ee\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
array.rs
/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfn random_access(nums: &[i32]) -> i32 {\n// \u5728\u533a\u95f4 [0, nums.len()) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nlet random_index = rand::thread_rng().gen_range(0..nums.len());\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nlet random_num = nums[random_index];\nrandom_num\n}\n
array.c
/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int *nums, int size) {\n// \u5728\u533a\u95f4 [0, size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nint randomIndex = rand() % size;\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nint randomNum = nums[randomIndex];\nreturn randomNum;\n}\n
array.zig
// \u968f\u673a\u8bbf\u95ee\u5143\u7d20\nfn randomAccess(nums: []i32) i32 {\n// \u5728\u533a\u95f4 [0, nums.len) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6574\u6570\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
"},{"location":"chapter_array_and_linkedlist/array/#3","title":"3. \u00a0 \u63d2\u5165\u5143\u7d20","text":"

\u6570\u7ec4\u5143\u7d20\u5728\u5185\u5b58\u4e2d\u662f\u201c\u7d27\u6328\u7740\u7684\u201d\uff0c\u5b83\u4eec\u4e4b\u95f4\u6ca1\u6709\u7a7a\u95f4\u518d\u5b58\u653e\u4efb\u4f55\u6570\u636e\u3002\u5982\u56fe 4-3 \u6240\u793a\uff0c\u5982\u679c\u60f3\u8981\u5728\u6570\u7ec4\u4e2d\u95f4\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\uff0c\u5219\u9700\u8981\u5c06\u8be5\u5143\u7d20\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u4e4b\u540e\u518d\u628a\u5143\u7d20\u8d4b\u503c\u7ed9\u8be5\u7d22\u5f15\u3002

\u56fe 4-3 \u00a0 \u6570\u7ec4\u63d2\u5165\u5143\u7d20\u793a\u4f8b

\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u56fa\u5b9a\u7684\uff0c\u56e0\u6b64\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\u5fc5\u5b9a\u4f1a\u5bfc\u81f4\u6570\u7ec4\u5c3e\u90e8\u5143\u7d20\u7684\u201c\u4e22\u5931\u201d\u3002\u6211\u4eec\u5c06\u8fd9\u4e2a\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u7559\u5728\u5217\u8868\u7ae0\u8282\u4e2d\u8ba8\u8bba\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig array.py
def insert(nums: list[int], num: int, index: int):\n\"\"\"\u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num\"\"\"\n# \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\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.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.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.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.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.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.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.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
array.rs
/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfn insert(nums: &mut Vec<i32>, num: i32, index: usize) {\n// \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor i in (index + 1..nums.len()).rev() {\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.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
"},{"location":"chapter_array_and_linkedlist/array/#4","title":"4. \u00a0 \u5220\u9664\u5143\u7d20","text":"

\u540c\u7406\uff0c\u5982\u56fe 4-4 \u6240\u793a\uff0c\u82e5\u60f3\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

\u56fe 4-4 \u00a0 \u6570\u7ec4\u5220\u9664\u5143\u7d20\u793a\u4f8b

\u8bf7\u6ce8\u610f\uff0c\u5220\u9664\u5143\u7d20\u5b8c\u6210\u540e\uff0c\u539f\u5148\u672b\u5c3e\u7684\u5143\u7d20\u53d8\u5f97\u201c\u65e0\u610f\u4e49\u201d\u4e86\uff0c\u6240\u4ee5\u6211\u4eec\u65e0\u987b\u7279\u610f\u53bb\u4fee\u6539\u5b83\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig array.py
def remove(nums: list[int], index: int):\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.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.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.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.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.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.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.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
array.rs
/* \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 */\nfn remove(nums: &mut Vec<i32>, index: usize) {\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor i in index..nums.len() - 1 {\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.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

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

  • \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\u201c\u65e0\u610f\u4e49\u201d\u7684\uff0c\u4f46\u8fd9\u6837\u505a\u4e5f\u4f1a\u9020\u6210\u90e8\u5206\u5185\u5b58\u7a7a\u95f4\u7684\u6d6a\u8d39\u3002
"},{"location":"chapter_array_and_linkedlist/array/#5","title":"5. \u00a0 \u904d\u5386\u6570\u7ec4","text":"

\u5728\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u65e2\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u904d\u5386\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig array.py
def traverse(nums: list[int]):\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.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.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.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.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.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.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 (const 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 (const num of 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
array.rs
/* \u904d\u5386\u6570\u7ec4 */\nfn traverse(nums: &[i32]) {\nlet mut _count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor _ in 0..nums.len() {\n_count += 1;\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor _ in nums {\n_count += 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.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
"},{"location":"chapter_array_and_linkedlist/array/#6","title":"6. \u00a0 \u67e5\u627e\u5143\u7d20","text":"

\u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\u9700\u8981\u904d\u5386\u6570\u7ec4\uff0c\u6bcf\u8f6e\u5224\u65ad\u5143\u7d20\u503c\u662f\u5426\u5339\u914d\uff0c\u82e5\u5339\u914d\u5219\u8f93\u51fa\u5bf9\u5e94\u7d22\u5f15\u3002

\u56e0\u4e3a\u6570\u7ec4\u662f\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u6240\u4ee5\u4e0a\u8ff0\u67e5\u627e\u64cd\u4f5c\u88ab\u79f0\u4e3a\u201c\u7ebf\u6027\u67e5\u627e\u201d\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig array.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.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.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.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.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.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.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.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
array.rs
/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfn find(nums: &[i32], target: i32) -> Option<usize> {\nfor i in 0..nums.len() {\nif nums[i] == target {\nreturn Some(i);\n}\n}\nNone\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.zig
// \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\nfn find(nums: []i32, target: i32) i32 {\nfor (nums, 0..) |num, i| {\nif (num == target) return @intCast(i);\n}\nreturn -1;\n}\n
"},{"location":"chapter_array_and_linkedlist/array/#7","title":"7. \u00a0 \u6269\u5bb9\u6570\u7ec4","text":"

\u5728\u590d\u6742\u7684\u7cfb\u7edf\u73af\u5883\u4e2d\uff0c\u7a0b\u5e8f\u96be\u4ee5\u4fdd\u8bc1\u6570\u7ec4\u4e4b\u540e\u7684\u5185\u5b58\u7a7a\u95f4\u662f\u53ef\u7528\u7684\uff0c\u4ece\u800c\u65e0\u6cd5\u5b89\u5168\u5730\u6269\u5c55\u6570\u7ec4\u5bb9\u91cf\u3002\u56e0\u6b64\u5728\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u4e0d\u53ef\u53d8\u7684\u3002

\u5982\u679c\u6211\u4eec\u5e0c\u671b\u6269\u5bb9\u6570\u7ec4\uff0c\u5219\u9700\u91cd\u65b0\u5efa\u7acb\u4e00\u4e2a\u66f4\u5927\u7684\u6570\u7ec4\uff0c\u7136\u540e\u628a\u539f\u6570\u7ec4\u5143\u7d20\u4f9d\u6b21\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\u3002\u8fd9\u662f\u4e00\u4e2a \\(O(n)\\) \u7684\u64cd\u4f5c\uff0c\u5728\u6570\u7ec4\u5f88\u5927\u7684\u60c5\u51b5\u4e0b\u662f\u975e\u5e38\u8017\u65f6\u7684\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
array.rs
/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfn extend(nums: Vec<i32>, enlarge: usize) -> Vec<i32> {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\nlet mut res: Vec<i32> = vec![0; nums.len() + enlarge];\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\nfor i in 0..nums.len() {\nres[i] = nums[i];\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nres\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.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);\n@memset(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
"},{"location":"chapter_array_and_linkedlist/array/#412","title":"4.1.2 \u00a0 \u6570\u7ec4\u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"

\u6570\u7ec4\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u5185\uff0c\u4e14\u5143\u7d20\u7c7b\u578b\u76f8\u540c\u3002\u8fd9\u79cd\u505a\u6cd5\u5305\u542b\u4e30\u5bcc\u7684\u5148\u9a8c\u4fe1\u606f\uff0c\u7cfb\u7edf\u53ef\u4ee5\u5229\u7528\u8fd9\u4e9b\u4fe1\u606f\u6765\u4f18\u5316\u6570\u636e\u7ed3\u6784\u7684\u64cd\u4f5c\u6548\u7387\u3002

  • \u7a7a\u95f4\u6548\u7387\u9ad8: \u6570\u7ec4\u4e3a\u6570\u636e\u5206\u914d\u4e86\u8fde\u7eed\u7684\u5185\u5b58\u5757\uff0c\u65e0\u987b\u989d\u5916\u7684\u7ed3\u6784\u5f00\u9500\u3002
  • \u652f\u6301\u968f\u673a\u8bbf\u95ee: \u6570\u7ec4\u5141\u8bb8\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u8bbf\u95ee\u4efb\u4f55\u5143\u7d20\u3002
  • \u7f13\u5b58\u5c40\u90e8\u6027: \u5f53\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

\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u662f\u4e00\u628a\u53cc\u5203\u5251\uff0c\u5176\u5b58\u5728\u4ee5\u4e0b\u7f3a\u70b9\u3002

  • \u63d2\u5165\u4e0e\u5220\u9664\u6548\u7387\u4f4e:\u5f53\u6570\u7ec4\u4e2d\u5143\u7d20\u8f83\u591a\u65f6\uff0c\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u9700\u8981\u79fb\u52a8\u5927\u91cf\u7684\u5143\u7d20\u3002
  • \u957f\u5ea6\u4e0d\u53ef\u53d8: \u6570\u7ec4\u5728\u521d\u59cb\u5316\u540e\u957f\u5ea6\u5c31\u56fa\u5b9a\u4e86\uff0c\u6269\u5bb9\u6570\u7ec4\u9700\u8981\u5c06\u6240\u6709\u6570\u636e\u590d\u5236\u5230\u65b0\u6570\u7ec4\uff0c\u5f00\u9500\u5f88\u5927\u3002
  • \u7a7a\u95f4\u6d6a\u8d39: \u5982\u679c\u6570\u7ec4\u5206\u914d\u7684\u5927\u5c0f\u8d85\u8fc7\u4e86\u5b9e\u9645\u6240\u9700\uff0c\u90a3\u4e48\u591a\u4f59\u7684\u7a7a\u95f4\u5c31\u88ab\u6d6a\u8d39\u4e86\u3002
"},{"location":"chapter_array_and_linkedlist/array/#413","title":"4.1.3 \u00a0 \u6570\u7ec4\u5178\u578b\u5e94\u7528","text":"

\u6570\u7ec4\u662f\u4e00\u79cd\u57fa\u7840\u4e14\u5e38\u89c1\u7684\u6570\u636e\u7ed3\u6784\uff0c\u65e2\u9891\u7e41\u5e94\u7528\u5728\u5404\u7c7b\u7b97\u6cd5\u4e4b\u4e2d\uff0c\u4e5f\u53ef\u7528\u4e8e\u5b9e\u73b0\u5404\u79cd\u590d\u6742\u6570\u636e\u7ed3\u6784\u3002

  • \u968f\u673a\u8bbf\u95ee\uff1a\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
  • \u6392\u5e8f\u548c\u641c\u7d22\uff1a\u6570\u7ec4\u662f\u6392\u5e8f\u548c\u641c\u7d22\u7b97\u6cd5\u6700\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u3002\u5feb\u901f\u6392\u5e8f\u3001\u5f52\u5e76\u6392\u5e8f\u3001\u4e8c\u5206\u67e5\u627e\u7b49\u90fd\u4e3b\u8981\u5728\u6570\u7ec4\u4e0a\u8fdb\u884c\u3002
  • \u67e5\u627e\u8868\uff1a\u5f53\u6211\u4eec\u9700\u8981\u5feb\u901f\u67e5\u627e\u4e00\u4e2a\u5143\u7d20\u6216\u8005\u9700\u8981\u67e5\u627e\u4e00\u4e2a\u5143\u7d20\u7684\u5bf9\u5e94\u5173\u7cfb\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u4f5c\u4e3a\u67e5\u627e\u8868\u3002\u5047\u5982\u6211\u4eec\u60f3\u8981\u5b9e\u73b0\u5b57\u7b26\u5230 ASCII \u7801\u7684\u6620\u5c04\uff0c\u5219\u53ef\u4ee5\u5c06\u5b57\u7b26\u7684 ASCII \u7801\u503c\u4f5c\u4e3a\u7d22\u5f15\uff0c\u5bf9\u5e94\u7684\u5143\u7d20\u5b58\u653e\u5728\u6570\u7ec4\u4e2d\u7684\u5bf9\u5e94\u4f4d\u7f6e\u3002
  • \u673a\u5668\u5b66\u4e60\uff1a\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
  • \u6570\u636e\u7ed3\u6784\u5b9e\u73b0\uff1a\u6570\u7ec4\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u5806\u3001\u56fe\u7b49\u6570\u636e\u7ed3\u6784\u3002\u4f8b\u5982\uff0c\u56fe\u7684\u90bb\u63a5\u77e9\u9635\u8868\u793a\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\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\u5728\u4e00\u4e2a\u590d\u6742\u7684\u7cfb\u7edf\u8fd0\u884c\u73af\u5883\u4e0b\uff0c\u7a7a\u95f2\u7684\u5185\u5b58\u7a7a\u95f4\u53ef\u80fd\u6563\u843d\u5728\u5185\u5b58\u5404\u5904\u3002\u6211\u4eec\u77e5\u9053\uff0c\u5b58\u50a8\u6570\u7ec4\u7684\u5185\u5b58\u7a7a\u95f4\u5fc5\u987b\u662f\u8fde\u7eed\u7684\uff0c\u800c\u5f53\u6570\u7ec4\u975e\u5e38\u5927\u65f6\uff0c\u5185\u5b58\u53ef\u80fd\u65e0\u6cd5\u63d0\u4f9b\u5982\u6b64\u5927\u7684\u8fde\u7eed\u7a7a\u95f4\u3002\u6b64\u65f6\u94fe\u8868\u7684\u7075\u6d3b\u6027\u4f18\u52bf\u5c31\u4f53\u73b0\u51fa\u6765\u4e86\u3002

\u300c\u94fe\u8868 linked list\u300d\u662f\u4e00\u79cd\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a\u8282\u70b9\u5bf9\u8c61\uff0c\u5404\u4e2a\u8282\u70b9\u901a\u8fc7\u201c\u5f15\u7528\u201d\u76f8\u8fde\u63a5\u3002\u5f15\u7528\u8bb0\u5f55\u4e86\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u5185\u5b58\u5730\u5740\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u4ece\u5f53\u524d\u8282\u70b9\u8bbf\u95ee\u5230\u4e0b\u4e00\u4e2a\u8282\u70b9\u3002

\u94fe\u8868\u7684\u8bbe\u8ba1\u4f7f\u5f97\u5404\u4e2a\u8282\u70b9\u53ef\u4ee5\u88ab\u5206\u6563\u5b58\u50a8\u5728\u5185\u5b58\u5404\u5904\uff0c\u5b83\u4eec\u7684\u5185\u5b58\u5730\u5740\u662f\u65e0\u987b\u8fde\u7eed\u7684\u3002

\u56fe 4-5 \u00a0 \u94fe\u8868\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f

\u89c2\u5bdf\u56fe 4-5 \uff0c\u94fe\u8868\u7684\u7ec4\u6210\u5355\u4f4d\u662f\u300c\u8282\u70b9 node\u300d\u5bf9\u8c61\u3002\u6bcf\u4e2a\u8282\u70b9\u90fd\u5305\u542b\u4e24\u9879\u6570\u636e\uff1a\u8282\u70b9\u7684\u201c\u503c\u201d\u548c\u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u201c\u5f15\u7528\u201d\u3002

  • \u94fe\u8868\u7684\u9996\u4e2a\u8282\u70b9\u88ab\u79f0\u4e3a\u201c\u5934\u8282\u70b9\u201d\uff0c\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u88ab\u79f0\u4e3a\u201c\u5c3e\u8282\u70b9\u201d\u3002
  • \u5c3e\u8282\u70b9\u6307\u5411\u7684\u662f\u201c\u7a7a\u201d\uff0c\u5b83\u5728 Java\u3001C++ \u548c Python \u4e2d\u5206\u522b\u88ab\u8bb0\u4e3a \\(\\text{null}\\)\u3001\\(\\text{nullptr}\\) \u548c \\(\\text{None}\\) \u3002
  • \u5728 C\u3001C++\u3001Go \u548c Rust \u7b49\u652f\u6301\u6307\u9488\u7684\u8bed\u8a00\u4e2d\uff0c\u4e0a\u8ff0\u7684\u201c\u5f15\u7528\u201d\u5e94\u88ab\u66ff\u6362\u4e3a\u201c\u6307\u9488\u201d\u3002

\u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u94fe\u8868\u8282\u70b9 ListNode \u9664\u4e86\u5305\u542b\u503c\uff0c\u8fd8\u9700\u989d\u5916\u4fdd\u5b58\u4e00\u4e2a\u5f15\u7528\uff08\u6307\u9488\uff09\u3002\u56e0\u6b64\u5728\u76f8\u540c\u6570\u636e\u91cf\u4e0b\uff0c\u94fe\u8868\u6bd4\u6570\u7ec4\u5360\u7528\u66f4\u591a\u7684\u5185\u5b58\u7a7a\u95f4\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig
class ListNode:\n\"\"\"\u94fe\u8868\u8282\u70b9\u7c7b\"\"\"\ndef __init__(self, val: int):\nself.val: int = val               # \u8282\u70b9\u503c\nself.next: ListNode | None = None # \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\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\nListNode(int x) : val(x), next(nullptr) {}  // \u6784\u9020\u51fd\u6570\n};\n
/* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\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 {\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\u7ed3\u6784\u4f53 */\ntype ListNode struct {\nVal  int       // \u8282\u70b9\u503c\nNext *ListNode // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\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 {\nvar val: Int // \u8282\u70b9\u503c\nvar next: ListNode? // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\ninit(x: Int) { // \u6784\u9020\u51fd\u6570\nval = x\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\u7c7b */\nclass ListNode {\nint val; // \u8282\u70b9\u503c\nListNode? next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\nListNode(this.val, [this.next]); // \u6784\u9020\u51fd\u6570\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n/* \u94fe\u8868\u8282\u70b9\u7c7b */\n#[derive(Debug)]\nstruct ListNode {\nval: i32, // \u8282\u70b9\u503c\nnext: Option<Rc<RefCell<ListNode>>>, // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\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\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\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\n// \u6784\u9020\u51fd\u6570\npub fn init(self: *Self, x: i32) void {\nself.val = x;\nself.next = null;\n}\n};\n}\n
"},{"location":"chapter_array_and_linkedlist/linked_list/#421","title":"4.2.1 \u00a0 \u94fe\u8868\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/linked_list/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u94fe\u8868","text":"

\u5efa\u7acb\u94fe\u8868\u5206\u4e3a\u4e24\u6b65\uff0c\u7b2c\u4e00\u6b65\u662f\u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\u5bf9\u8c61\uff0c\u7b2c\u4e8c\u6b65\u662f\u6784\u5efa\u5f15\u7528\u6307\u5411\u5173\u7cfb\u3002\u521d\u59cb\u5316\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4ece\u94fe\u8868\u7684\u5934\u8282\u70b9\u51fa\u53d1\uff0c\u901a\u8fc7\u5f15\u7528\u6307\u5411 next \u4f9d\u6b21\u8bbf\u95ee\u6240\u6709\u8282\u70b9\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.cs
/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode n0 = new(1);\nListNode n1 = new(3);\nListNode n2 = new(2);\nListNode n3 = new(5);\nListNode n4 = new(4);\n// \u6784\u5efa\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.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.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.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
linked_list.rs
/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nlet n0 = Rc::new(RefCell::new(ListNode { val: 1, next: None }));\nlet n1 = Rc::new(RefCell::new(ListNode { val: 3, next: None }));\nlet n2 = Rc::new(RefCell::new(ListNode { val: 2, next: None }));\nlet n3 = Rc::new(RefCell::new(ListNode { val: 5, next: None }));\nlet n4 = Rc::new(RefCell::new(ListNode { val: 4, next: None }));\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.borrow_mut().next = Some(n1.clone());\nn1.borrow_mut().next = Some(n2.clone());\nn2.borrow_mut().next = Some(n3.clone());\nn3.borrow_mut().next = Some(n4.clone());\n
linked_list.c
/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode* n0 = newListNode(1);\nListNode* n1 = newListNode(3);\nListNode* n2 = newListNode(2);\nListNode* n3 = newListNode(5);\nListNode* n4 = newListNode(4);\n// \u6784\u5efa\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

\u6570\u7ec4\u6574\u4f53\u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u6bd4\u5982\u6570\u7ec4 nums \u5305\u542b\u5143\u7d20 nums[0] \u548c nums[1] \u7b49\uff0c\u800c\u94fe\u8868\u662f\u7531\u591a\u4e2a\u72ec\u7acb\u7684\u8282\u70b9\u5bf9\u8c61\u7ec4\u6210\u7684\u3002\u6211\u4eec\u901a\u5e38\u5c06\u5934\u8282\u70b9\u5f53\u4f5c\u94fe\u8868\u7684\u4ee3\u79f0\uff0c\u6bd4\u5982\u4ee5\u4e0a\u4ee3\u7801\u4e2d\u7684\u94fe\u8868\u53ef\u88ab\u8bb0\u505a\u94fe\u8868 n0 \u3002

"},{"location":"chapter_array_and_linkedlist/linked_list/#2","title":"2. \u00a0 \u63d2\u5165\u8282\u70b9","text":"

\u5728\u94fe\u8868\u4e2d\u63d2\u5165\u8282\u70b9\u975e\u5e38\u5bb9\u6613\u3002\u5982\u56fe 4-6 \u6240\u793a\uff0c\u5047\u8bbe\u6211\u4eec\u60f3\u5728\u76f8\u90bb\u7684\u4e24\u4e2a\u8282\u70b9 n0 \u548c n1 \u4e4b\u95f4\u63d2\u5165\u4e00\u4e2a\u65b0\u8282\u70b9 P \uff0c\u5219\u53ea\u9700\u8981\u6539\u53d8\u4e24\u4e2a\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\u5373\u53ef\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002

\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u5728\u6570\u7ec4\u4e2d\u63d2\u5165\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u5728\u5927\u6570\u636e\u91cf\u4e0b\u7684\u6548\u7387\u8f83\u4f4e\u3002

\u56fe 4-6 \u00a0 \u94fe\u8868\u63d2\u5165\u8282\u70b9\u793a\u4f8b

PythonC++JavaC#GoSwiftJSTSDartRustCZig linked_list.py
def insert(n0: ListNode, P: ListNode):\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.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.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.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.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.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.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.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
linked_list.rs
/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\n#[allow(non_snake_case)]\npub fn insert<T>(n0: &Rc<RefCell<ListNode<T>>>, P: Rc<RefCell<ListNode<T>>>) {\nlet n1 =  n0.borrow_mut().next.take();\nP.borrow_mut().next = n1;\nn0.borrow_mut().next = Some(P);\n}\n
linked_list.c
/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode *n0, ListNode *P) {\nListNode *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
"},{"location":"chapter_array_and_linkedlist/linked_list/#3","title":"3. \u00a0 \u5220\u9664\u8282\u70b9","text":"

\u5982\u56fe 4-7 \u6240\u793a\uff0c\u5728\u94fe\u8868\u4e2d\u5220\u9664\u8282\u70b9\u4e5f\u975e\u5e38\u65b9\u4fbf\uff0c\u53ea\u9700\u6539\u53d8\u4e00\u4e2a\u8282\u70b9\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\u5373\u53ef\u3002

\u8bf7\u6ce8\u610f\uff0c\u5c3d\u7ba1\u5728\u5220\u9664\u64cd\u4f5c\u5b8c\u6210\u540e\u8282\u70b9 P \u4ecd\u7136\u6307\u5411 n1 \uff0c\u4f46\u5b9e\u9645\u4e0a\u904d\u5386\u6b64\u94fe\u8868\u5df2\u7ecf\u65e0\u6cd5\u8bbf\u95ee\u5230 P \uff0c\u8fd9\u610f\u5473\u7740 P \u5df2\u7ecf\u4e0d\u518d\u5c5e\u4e8e\u8be5\u94fe\u8868\u4e86\u3002

\u56fe 4-7 \u00a0 \u94fe\u8868\u5220\u9664\u8282\u70b9

PythonC++JavaC#GoSwiftJSTSDartRustCZig linked_list.py
def remove(n0: ListNode):\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.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.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.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.go
/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunc removeItem(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.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.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.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
linked_list.rs
/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\n#[allow(non_snake_case)]\npub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {\nif n0.borrow().next.is_none() {return};\n// n0 -> P -> n1\nlet P = n0.borrow_mut().next.take();\nif let Some(node) = P {\nlet n1 = node.borrow_mut().next.take();\nn0.borrow_mut().next = n1;\n}\n}\n
linked_list.c
/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nvoid removeItem(ListNode *n0) {\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.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
"},{"location":"chapter_array_and_linkedlist/linked_list/#4","title":"4. \u00a0 \u8bbf\u95ee\u8282\u70b9","text":"

\u5728\u94fe\u8868\u8bbf\u95ee\u8282\u70b9\u7684\u6548\u7387\u8f83\u4f4e\u3002\u5982\u4e0a\u8282\u6240\u8ff0\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u4e0b\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u5143\u7d20\u3002\u94fe\u8868\u5219\u4e0d\u7136\uff0c\u7a0b\u5e8f\u9700\u8981\u4ece\u5934\u8282\u70b9\u51fa\u53d1\uff0c\u9010\u4e2a\u5411\u540e\u904d\u5386\uff0c\u76f4\u81f3\u627e\u5230\u76ee\u6807\u8282\u70b9\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8bbf\u95ee\u94fe\u8868\u7684\u7b2c \\(i\\) \u4e2a\u8282\u70b9\u9700\u8981\u5faa\u73af \\(i - 1\\) \u8f6e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig linked_list.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.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.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.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.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.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.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.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
linked_list.rs
/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\npub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Rc<RefCell<ListNode<T>>> {\nif index <= 0 {return head};\nif let Some(node) = &head.borrow_mut().next {\nreturn access(node.clone(), index - 1);\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.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
"},{"location":"chapter_array_and_linkedlist/linked_list/#5","title":"5. \u00a0 \u67e5\u627e\u8282\u70b9","text":"

\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\u6b64\u8fc7\u7a0b\u4e5f\u5c5e\u4e8e\u7ebf\u6027\u67e5\u627e\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
linked_list.rs
/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\npub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T, index: i32) -> i32 {\nif head.borrow().val == target {return index};\nif let Some(node) = &head.borrow_mut().next {\nreturn find(node.clone(), target, index + 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.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
"},{"location":"chapter_array_and_linkedlist/linked_list/#422-vs","title":"4.2.2 \u00a0 \u6570\u7ec4 VS \u94fe\u8868","text":"

\u8868 4-1 \u603b\u7ed3\u5bf9\u6bd4\u4e86\u6570\u7ec4\u548c\u94fe\u8868\u7684\u5404\u9879\u7279\u70b9\u4e0e\u64cd\u4f5c\u6548\u7387\u3002\u7531\u4e8e\u5b83\u4eec\u91c7\u7528\u4e24\u79cd\u76f8\u53cd\u7684\u5b58\u50a8\u7b56\u7565\uff0c\u56e0\u6b64\u5404\u79cd\u6027\u8d28\u548c\u64cd\u4f5c\u6548\u7387\u4e5f\u5448\u73b0\u5bf9\u7acb\u7684\u7279\u70b9\u3002

\u8868 4-1 \u00a0 \u6570\u7ec4\u4e0e\u94fe\u8868\u7684\u6548\u7387\u5bf9\u6bd4

\u6570\u7ec4 \u94fe\u8868 \u5b58\u50a8\u65b9\u5f0f \u8fde\u7eed\u5185\u5b58\u7a7a\u95f4 \u5206\u6563\u5185\u5b58\u7a7a\u95f4 \u7f13\u5b58\u5c40\u90e8\u6027 \u53cb\u597d \u4e0d\u53cb\u597d \u5bb9\u91cf\u6269\u5c55 \u957f\u5ea6\u4e0d\u53ef\u53d8 \u53ef\u7075\u6d3b\u6269\u5c55 \u5185\u5b58\u6548\u7387 \u5360\u7528\u5185\u5b58\u5c11\u3001\u6d6a\u8d39\u90e8\u5206\u7a7a\u95f4 \u5360\u7528\u5185\u5b58\u591a \u8bbf\u95ee\u5143\u7d20 \\(O(1)\\) \\(O(n)\\) \u6dfb\u52a0\u5143\u7d20 \\(O(n)\\) \\(O(1)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(1)\\)"},{"location":"chapter_array_and_linkedlist/linked_list/#423","title":"4.2.3 \u00a0 \u5e38\u89c1\u94fe\u8868\u7c7b\u578b","text":"

\u5982\u56fe 4-8 \u6240\u793a\uff0c\u5e38\u89c1\u7684\u94fe\u8868\u7c7b\u578b\u5305\u62ec\u4e09\u79cd\u3002

  • \u5355\u5411\u94fe\u8868\uff1a\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\u5f15\u7528\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\u79f0\u4e3a\u5c3e\u8282\u70b9\uff0c\u5c3e\u8282\u70b9\u6307\u5411\u7a7a \\(\\text{None}\\) \u3002
  • \u73af\u5f62\u94fe\u8868\uff1a\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\uff1a\u4e0e\u5355\u5411\u94fe\u8868\u76f8\u6bd4\uff0c\u53cc\u5411\u94fe\u8868\u8bb0\u5f55\u4e86\u4e24\u4e2a\u65b9\u5411\u7684\u5f15\u7528\u3002\u53cc\u5411\u94fe\u8868\u7684\u8282\u70b9\u5b9a\u4e49\u540c\u65f6\u5305\u542b\u6307\u5411\u540e\u7ee7\u8282\u70b9\uff08\u4e0b\u4e00\u4e2a\u8282\u70b9\uff09\u548c\u524d\u9a71\u8282\u70b9\uff08\u4e0a\u4e00\u4e2a\u8282\u70b9\uff09\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\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
PythonC++JavaC#GoSwiftJSTSDartRustCZig
class ListNode:\n\"\"\"\u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b\"\"\"\ndef __init__(self, val: int):\nself.val: int = val                # \u8282\u70b9\u503c\nself.next: ListNode | None = None  # \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\nself.prev: ListNode | None = None  # \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct ListNode {\nint val;         // \u8282\u70b9\u503c\nListNode *next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\nListNode *prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\nListNode(int x) : val(x), next(nullptr), prev(nullptr) {}  // \u6784\u9020\u51fd\u6570\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\u5f15\u7528\nListNode prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\nListNode(int x) { val = x; }  // \u6784\u9020\u51fd\u6570\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\u5f15\u7528\nListNode prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\nListNode(int x) => val = x;  // \u6784\u9020\u51fd\u6570\n}\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\nPrev *DoublyListNode // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\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 {\nvar val: Int // \u8282\u70b9\u503c\nvar next: ListNode? // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\nvar prev: ListNode? // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\ninit(x: Int) { // \u6784\u9020\u51fd\u6570\nval = x\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\u5f15\u7528\nthis.prev = prev  ===  undefined ? null : prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n}\n}\n
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\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\u5f15\u7528\nthis.prev = prev  ===  undefined ? null : prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n}\n}\n
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nint val;        // \u8282\u70b9\u503c\nListNode next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\nListNode prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\nListNode(this.val, [this.next, this.prev]);  // \u6784\u9020\u51fd\u6570\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b\u578b */\n#[derive(Debug)]\nstruct ListNode {\nval: i32, // \u8282\u70b9\u503c\nnext: Option<Rc<RefCell<ListNode>>>, // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\nprev: Option<Rc<RefCell<ListNode>>>, // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n}\n/* \u6784\u9020\u51fd\u6570 */\nimpl ListNode {\nfn new(val: i32) -> Self {\nListNode {\nval,\nnext: None,\nprev: None,\n}\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\nstruct ListNode *prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\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\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\nprev: ?*Self = null, // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\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

\u56fe 4-8 \u00a0 \u5e38\u89c1\u94fe\u8868\u79cd\u7c7b

"},{"location":"chapter_array_and_linkedlist/linked_list/#424","title":"4.2.4 \u00a0 \u94fe\u8868\u5178\u578b\u5e94\u7528","text":"

\u5355\u5411\u94fe\u8868\u901a\u5e38\u7528\u4e8e\u5b9e\u73b0\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u548c\u56fe\u7b49\u6570\u636e\u7ed3\u6784\u3002

  • \u6808\u4e0e\u961f\u5217\uff1a\u5f53\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u90fd\u5728\u94fe\u8868\u7684\u4e00\u7aef\u8fdb\u884c\u65f6\uff0c\u5b83\u8868\u73b0\u51fa\u5148\u8fdb\u540e\u51fa\u7684\u7684\u7279\u6027\uff0c\u5bf9\u5e94\u6808\uff1b\u5f53\u63d2\u5165\u64cd\u4f5c\u5728\u94fe\u8868\u7684\u4e00\u7aef\u8fdb\u884c\uff0c\u5220\u9664\u64cd\u4f5c\u5728\u94fe\u8868\u7684\u53e6\u4e00\u7aef\u8fdb\u884c\uff0c\u5b83\u8868\u73b0\u51fa\u5148\u8fdb\u5148\u51fa\u7684\u7279\u6027\uff0c\u5bf9\u5e94\u961f\u5217\u3002
  • \u54c8\u5e0c\u8868\uff1a\u94fe\u5730\u5740\u6cd5\u662f\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u7684\u4e3b\u6d41\u65b9\u6848\u4e4b\u4e00\uff0c\u5728\u8be5\u65b9\u6848\u4e2d\uff0c\u6240\u6709\u51b2\u7a81\u7684\u5143\u7d20\u90fd\u4f1a\u88ab\u653e\u5230\u4e00\u4e2a\u94fe\u8868\u4e2d\u3002
  • \u56fe\uff1a\u90bb\u63a5\u8868\u662f\u8868\u793a\u56fe\u7684\u4e00\u79cd\u5e38\u7528\u65b9\u5f0f\uff0c\u5728\u5176\u4e2d\uff0c\u56fe\u7684\u6bcf\u4e2a\u9876\u70b9\u90fd\u4e0e\u4e00\u4e2a\u94fe\u8868\u76f8\u5173\u8054\uff0c\u94fe\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u4ee3\u8868\u4e0e\u8be5\u9876\u70b9\u76f8\u8fde\u7684\u5176\u4ed6\u9876\u70b9\u3002

\u53cc\u5411\u94fe\u8868\u5e38\u88ab\u7528\u4e8e\u9700\u8981\u5feb\u901f\u67e5\u627e\u524d\u4e00\u4e2a\u548c\u4e0b\u4e00\u4e2a\u5143\u7d20\u7684\u573a\u666f\u3002

  • \u9ad8\u7ea7\u6570\u636e\u7ed3\u6784\uff1a\u6bd4\u5982\u5728\u7ea2\u9ed1\u6811\u3001B \u6811\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u8bbf\u95ee\u8282\u70b9\u7684\u7236\u8282\u70b9\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5728\u8282\u70b9\u4e2d\u4fdd\u5b58\u4e00\u4e2a\u6307\u5411\u7236\u8282\u70b9\u7684\u5f15\u7528\u6765\u5b9e\u73b0\uff0c\u7c7b\u4f3c\u4e8e\u53cc\u5411\u94fe\u8868\u3002
  • \u6d4f\u89c8\u5668\u5386\u53f2\uff1a\u5728\u7f51\u9875\u6d4f\u89c8\u5668\u4e2d\uff0c\u5f53\u7528\u6237\u70b9\u51fb\u524d\u8fdb\u6216\u540e\u9000\u6309\u94ae\u65f6\uff0c\u6d4f\u89c8\u5668\u9700\u8981\u77e5\u9053\u7528\u6237\u8bbf\u95ee\u8fc7\u7684\u524d\u4e00\u4e2a\u548c\u540e\u4e00\u4e2a\u7f51\u9875\u3002\u53cc\u5411\u94fe\u8868\u7684\u7279\u6027\u4f7f\u5f97\u8fd9\u79cd\u64cd\u4f5c\u53d8\u5f97\u7b80\u5355\u3002
  • LRU \u7b97\u6cd5\uff1a\u5728\u7f13\u5b58\u6dd8\u6c70\u7b97\u6cd5\uff08LRU\uff09\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5feb\u901f\u627e\u5230\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684\u6570\u636e\uff0c\u4ee5\u53ca\u652f\u6301\u5feb\u901f\u5730\u6dfb\u52a0\u548c\u5220\u9664\u8282\u70b9\u3002\u8fd9\u65f6\u5019\u4f7f\u7528\u53cc\u5411\u94fe\u8868\u5c31\u975e\u5e38\u5408\u9002\u3002

\u5faa\u73af\u94fe\u8868\u5e38\u88ab\u7528\u4e8e\u9700\u8981\u5468\u671f\u6027\u64cd\u4f5c\u7684\u573a\u666f\uff0c\u6bd4\u5982\u64cd\u4f5c\u7cfb\u7edf\u7684\u8d44\u6e90\u8c03\u5ea6\u3002

  • \u65f6\u95f4\u7247\u8f6e\u8f6c\u8c03\u5ea6\u7b97\u6cd5\uff1a\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u65f6\u95f4\u7247\u8f6e\u8f6c\u8c03\u5ea6\u7b97\u6cd5\u662f\u4e00\u79cd\u5e38\u89c1\u7684 CPU \u8c03\u5ea6\u7b97\u6cd5\uff0c\u5b83\u9700\u8981\u5bf9\u4e00\u7ec4\u8fdb\u7a0b\u8fdb\u884c\u5faa\u73af\u3002\u6bcf\u4e2a\u8fdb\u7a0b\u88ab\u8d4b\u4e88\u4e00\u4e2a\u65f6\u95f4\u7247\uff0c\u5f53\u65f6\u95f4\u7247\u7528\u5b8c\u65f6\uff0cCPU \u5c06\u5207\u6362\u5230\u4e0b\u4e00\u4e2a\u8fdb\u7a0b\u3002\u8fd9\u79cd\u5faa\u73af\u7684\u64cd\u4f5c\u5c31\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u94fe\u8868\u6765\u5b9e\u73b0\u3002
  • \u6570\u636e\u7f13\u51b2\u533a\uff1a\u5728\u67d0\u4e9b\u6570\u636e\u7f13\u51b2\u533a\u7684\u5b9e\u73b0\u4e2d\uff0c\u4e5f\u53ef\u80fd\u4f1a\u4f7f\u7528\u5230\u5faa\u73af\u94fe\u8868\u3002\u6bd4\u5982\u5728\u97f3\u9891\u3001\u89c6\u9891\u64ad\u653e\u5668\u4e2d\uff0c\u6570\u636e\u6d41\u53ef\u80fd\u4f1a\u88ab\u5206\u6210\u591a\u4e2a\u7f13\u51b2\u5757\u5e76\u653e\u5165\u4e00\u4e2a\u5faa\u73af\u94fe\u8868\uff0c\u4ee5\u4fbf\u5b9e\u73b0\u65e0\u7f1d\u64ad\u653e\u3002
"},{"location":"chapter_array_and_linkedlist/list/","title":"4.3 \u00a0 \u5217\u8868","text":"

\u300c\u5217\u8868 list\u300d\u662f\u4e00\u4e2a\u62bd\u8c61\u7684\u6570\u636e\u7ed3\u6784\u6982\u5ff5\uff0c\u5b83\u8868\u793a\u5143\u7d20\u7684\u6709\u5e8f\u96c6\u5408\uff0c\u652f\u6301\u5143\u7d20\u8bbf\u95ee\u3001\u4fee\u6539\u3001\u6dfb\u52a0\u3001\u5220\u9664\u548c\u904d\u5386\u7b49\u64cd\u4f5c\uff0c\u65e0\u9700\u4f7f\u7528\u8005\u8003\u8651\u5bb9\u91cf\u9650\u5236\u7684\u95ee\u9898\u3002\u5217\u8868\u53ef\u4ee5\u57fa\u4e8e\u94fe\u8868\u6216\u6570\u7ec4\u5b9e\u73b0\u3002

  • \u94fe\u8868\u5929\u7136\u53ef\u4ee5\u88ab\u770b\u4f5c\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u652f\u6301\u5143\u7d20\u589e\u5220\u67e5\u6539\u64cd\u4f5c\uff0c\u5e76\u4e14\u53ef\u4ee5\u7075\u6d3b\u52a8\u6001\u6269\u5bb9\u3002
  • \u6570\u7ec4\u4e5f\u652f\u6301\u5143\u7d20\u589e\u5220\u67e5\u6539\uff0c\u4f46\u7531\u4e8e\u5176\u957f\u5ea6\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u53ea\u80fd\u88ab\u770b\u4f5c\u662f\u4e00\u4e2a\u5177\u6709\u957f\u5ea6\u9650\u5236\u7684\u5217\u8868\u3002

\u5f53\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u5217\u8868\u65f6\uff0c\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6027\u8d28\u4f1a\u5bfc\u81f4\u5217\u8868\u7684\u5b9e\u7528\u6027\u964d\u4f4e\u3002\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u901a\u5e38\u65e0\u6cd5\u4e8b\u5148\u786e\u5b9a\u9700\u8981\u5b58\u50a8\u591a\u5c11\u6570\u636e\uff0c\u4ece\u800c\u96be\u4ee5\u9009\u62e9\u5408\u9002\u7684\u5217\u8868\u957f\u5ea6\u3002\u82e5\u957f\u5ea6\u8fc7\u5c0f\uff0c\u5219\u5f88\u53ef\u80fd\u65e0\u6cd5\u6ee1\u8db3\u4f7f\u7528\u9700\u6c42\uff1b\u82e5\u957f\u5ea6\u8fc7\u5927\uff0c\u5219\u4f1a\u9020\u6210\u5185\u5b58\u7a7a\u95f4\u7684\u6d6a\u8d39\u3002

\u4e3a\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u300c\u52a8\u6001\u6570\u7ec4 dynamic array\u300d\u6765\u5b9e\u73b0\u5217\u8868\u3002\u5b83\u7ee7\u627f\u4e86\u6570\u7ec4\u7684\u5404\u9879\u4f18\u70b9\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u52a8\u6001\u6269\u5bb9\u3002

\u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u6807\u51c6\u5e93\u63d0\u4f9b\u7684\u5217\u8868\u90fd\u662f\u57fa\u4e8e\u52a8\u6001\u6570\u7ec4\u5b9e\u73b0\u7684\uff0c\u4f8b\u5982 Python \u4e2d\u7684 list \u3001Java \u4e2d\u7684 ArrayList \u3001C++ \u4e2d\u7684 vector \u548c C# \u4e2d\u7684 List \u7b49\u3002\u5728\u63a5\u4e0b\u6765\u7684\u8ba8\u8bba\u4e2d\uff0c\u6211\u4eec\u5c06\u628a\u201c\u5217\u8868\u201d\u548c\u201c\u52a8\u6001\u6570\u7ec4\u201d\u89c6\u4e3a\u7b49\u540c\u7684\u6982\u5ff5\u3002

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

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

PythonC++JavaC#GoSwiftJSTSDartRustCZig list.py
# \u521d\u59cb\u5316\u5217\u8868\n# \u65e0\u521d\u59cb\u503c\nnums1: nums[int] = []\n# \u6709\u521d\u59cb\u503c\nnums: nums[int] = [1, 3, 2, 5, 4]\n
list.cpp
/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u9700\u6ce8\u610f\uff0cC++ \u4e2d vector \u5373\u662f\u672c\u6587\u63cf\u8ff0\u7684 nums\n// \u65e0\u521d\u59cb\u503c\nvector<int> nums1;\n// \u6709\u521d\u59cb\u503c\nvector<int> nums = { 1, 3, 2, 5, 4 };\n
list.java
/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<Integer> nums1 = new ArrayList<>();\n// \u6709\u521d\u59cb\u503c\uff08\u6ce8\u610f\u6570\u7ec4\u7684\u5143\u7d20\u7c7b\u578b\u9700\u4e3a int[] \u7684\u5305\u88c5\u7c7b Integer[]\uff09\nInteger[] numbers = new Integer[] { 1, 3, 2, 5, 4 };\nList<Integer> nums = new ArrayList<>(Arrays.asList(numbers));\n
list.cs
/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<int> nums1 = new();\n// \u6709\u521d\u59cb\u503c\nint[] numbers = new int[] { 1, 3, 2, 5, 4 };\nList<int> nums = numbers.ToList();\n
list_test.go
/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nnums1 := []int\n// \u6709\u521d\u59cb\u503c\nnums := []int{1, 3, 2, 5, 4}\n
list.swift
/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nlet nums1: [Int] = []\n// \u6709\u521d\u59cb\u503c\nvar nums = [1, 3, 2, 5, 4]\n
list.js
/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nconst nums1 = [];\n// \u6709\u521d\u59cb\u503c\nconst nums = [1, 3, 2, 5, 4];\n
list.ts
/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nconst nums1: number[] = [];\n// \u6709\u521d\u59cb\u503c\nconst nums: number[] = [1, 3, 2, 5, 4];\n
list.dart
/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<int> nums1 = [];\n// \u6709\u521d\u59cb\u503c\nList<int> nums = [1, 3, 2, 5, 4];\n
list.rs
/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nlet nums1: Vec<i32> = Vec::new();\n// \u6709\u521d\u59cb\u503c\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
list.c
// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
list.zig
// \u521d\u59cb\u5316\u5217\u8868\nvar nums = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer nums.deinit();\ntry nums.appendSlice(&[_]i32{ 1, 3, 2, 5, 4 });\n
"},{"location":"chapter_array_and_linkedlist/list/#2","title":"2. \u00a0 \u8bbf\u95ee\u5143\u7d20","text":"

\u5217\u8868\u672c\u8d28\u4e0a\u662f\u6570\u7ec4\uff0c\u56e0\u6b64\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u8bbf\u95ee\u548c\u66f4\u65b0\u5143\u7d20\uff0c\u6548\u7387\u5f88\u9ad8\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig list.py
# \u8bbf\u95ee\u5143\u7d20\nnum: int = nums[1]  # \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n# \u66f4\u65b0\u5143\u7d20\nnums[1] = 0    # \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.cpp
/* \u8bbf\u95ee\u5143\u7d20 */\nint num = nums[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.java
/* \u8bbf\u95ee\u5143\u7d20 */\nint num = nums.get(1);  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums.set(1, 0);  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.cs
/* \u8bbf\u95ee\u5143\u7d20 */\nint num = nums[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list_test.go
/* \u8bbf\u95ee\u5143\u7d20 */\nnum := nums[1]  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0     // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.swift
/* \u8bbf\u95ee\u5143\u7d20 */\nlet num = nums[1] // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0 // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.js
/* \u8bbf\u95ee\u5143\u7d20 */\nconst num = nums[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.ts
/* \u8bbf\u95ee\u5143\u7d20 */\nconst num: number = nums[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.dart
/* \u8bbf\u95ee\u5143\u7d20 */\nint num = nums[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.rs
/* \u8bbf\u95ee\u5143\u7d20 */\nlet num: i32 = nums[1];    // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0;               // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.c
// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
list.zig
// \u8bbf\u95ee\u5143\u7d20\nvar num = nums.items[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n// \u66f4\u65b0\u5143\u7d20\nnums.items[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0  \n
"},{"location":"chapter_array_and_linkedlist/list/#3","title":"3. \u00a0 \u63d2\u5165\u4e0e\u5220\u9664\u5143\u7d20","text":"

\u76f8\u8f83\u4e8e\u6570\u7ec4\uff0c\u5217\u8868\u53ef\u4ee5\u81ea\u7531\u5730\u6dfb\u52a0\u4e0e\u5220\u9664\u5143\u7d20\u3002\u5728\u5217\u8868\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff0c\u4f46\u63d2\u5165\u548c\u5220\u9664\u5143\u7d20\u7684\u6548\u7387\u4ecd\u4e0e\u6570\u7ec4\u76f8\u540c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

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

\u4e0e\u6570\u7ec4\u4e00\u6837\uff0c\u5217\u8868\u53ef\u4ee5\u6839\u636e\u7d22\u5f15\u904d\u5386\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u904d\u5386\u5404\u5143\u7d20\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig list.py
# \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868\ncount = 0\nfor i in range(len(nums)):\ncount += 1\n# \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20\ncount = 0\nfor num in nums:\ncount += 1\n
list.cpp
/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (int num : nums) {\ncount++;\n}\n
list.java
/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (int num : nums) {\ncount++;\n}\n
list.cs
/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < nums.Count; i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nforeach (int num in nums) {\ncount++;\n}\n
list_test.go
/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\ncount := 0\nfor i := 0; i < len(nums); i++ {\ncount++\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0\nfor range nums {\ncount++\n}\n
list.swift
/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nvar count = 0\nfor _ in nums.indices {\ncount += 1\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0\nfor _ in nums {\ncount += 1\n}\n
list.js
/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (const num of nums) {\ncount++;\n}\n
list.ts
/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (const num of nums) {\ncount++;\n}\n
list.dart
/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < nums.length; i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (int num in nums) {\ncount++;\n}\n
list.rs
/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nlet mut count = 0;\nfor (index, value) in nums.iter().enumerate() {\ncount += 1;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\nlet mut count = 0;\nfor value in nums.iter() {\ncount += 1;\n}\n
list.c
// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
list.zig
// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868\nvar count: i32 = 0;\nvar i: i32 = 0;\nwhile (i < nums.items.len) : (i += 1) {\ncount += 1;\n}\n// \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20\ncount = 0;\nfor (nums.items) |_| {\ncount += 1;\n}\n
"},{"location":"chapter_array_and_linkedlist/list/#5","title":"5. \u00a0 \u62fc\u63a5\u5217\u8868","text":"

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

PythonC++JavaC#GoSwiftJSTSDartRustCZig list.py
# \u62fc\u63a5\u4e24\u4e2a\u5217\u8868\nnums1: nums[int] = [6, 8, 7, 10, 9]\nnums += nums1  # \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.cpp
/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nvector<int> nums1 = { 6, 8, 7, 10, 9 };\n// \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\nnums.insert(nums.end(), nums1.begin(), nums1.end());\n
list.java
/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<Integer> nums1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));\nnums.addAll(nums1);  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.cs
/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<int> nums1 = new() { 6, 8, 7, 10, 9 };\nnums.AddRange(nums1);  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list_test.go
/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nnums1 := []int{6, 8, 7, 10, 9}\nnums = append(nums, nums1...)  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.swift
/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nlet nums1 = [6, 8, 7, 10, 9]\nnums.append(contentsOf: nums1) // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.js
/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nconst nums1 = [6, 8, 7, 10, 9];\nnums.push(...nums1);  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.ts
/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nconst nums1: number[] = [6, 8, 7, 10, 9];\nnums.push(...nums1);  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.dart
/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<int> nums1 = [6, 8, 7, 10, 9];\nnums.addAll(nums1);  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.rs
/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nlet nums1: Vec<i32> = vec![6, 8, 7, 10, 9];\nnums.extend(nums1);\n
list.c
// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
list.zig
// \u62fc\u63a5\u4e24\u4e2a\u5217\u8868\nvar nums1 = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer nums1.deinit();\ntry nums1.appendSlice(&[_]i32{ 6, 8, 7, 10, 9 });\ntry nums.insertSlice(nums.items.len, nums1.items); // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
"},{"location":"chapter_array_and_linkedlist/list/#6","title":"6. \u00a0 \u6392\u5e8f\u5217\u8868","text":"

\u5b8c\u6210\u5217\u8868\u6392\u5e8f\u540e\uff0c\u6211\u4eec\u4fbf\u53ef\u4ee5\u4f7f\u7528\u5728\u6570\u7ec4\u7c7b\u7b97\u6cd5\u9898\u4e2d\u7ecf\u5e38\u8003\u5bdf\u7684\u201c\u4e8c\u5206\u67e5\u627e\u201d\u548c\u201c\u53cc\u6307\u9488\u201d\u7b97\u6cd5\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig list.py
# \u6392\u5e8f\u5217\u8868\nnums.sort()  # \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.cpp
/* \u6392\u5e8f\u5217\u8868 */\nsort(nums.begin(), nums.end());  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.java
/* \u6392\u5e8f\u5217\u8868 */\nCollections.sort(nums);  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.cs
/* \u6392\u5e8f\u5217\u8868 */\nnums.Sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list_test.go
/* \u6392\u5e8f\u5217\u8868 */\nsort.Ints(nums)  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.swift
/* \u6392\u5e8f\u5217\u8868 */\nnums.sort() // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.js
/* \u6392\u5e8f\u5217\u8868 */  nums.sort((a, b) => a - b);  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.ts
/* \u6392\u5e8f\u5217\u8868 */\nnums.sort((a, b) => a - b);  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.dart
/* \u6392\u5e8f\u5217\u8868 */\nnums.sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.rs
/* \u6392\u5e8f\u5217\u8868 */\nnums.sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.c
// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
list.zig
// \u6392\u5e8f\u5217\u8868\nstd.sort.sort(i32, nums.items, {}, comptime std.sort.asc(i32));\n
"},{"location":"chapter_array_and_linkedlist/list/#432","title":"4.3.2 \u00a0 \u5217\u8868\u5b9e\u73b0","text":"

\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u90fd\u63d0\u4f9b\u5185\u7f6e\u7684\u5217\u8868\uff0c\u4f8b\u5982 Java\u3001C++\u3001Python \u7b49\u3002\u5b83\u4eec\u7684\u5b9e\u73b0\u6bd4\u8f83\u590d\u6742\uff0c\u5404\u4e2a\u53c2\u6570\u7684\u8bbe\u5b9a\u4e5f\u975e\u5e38\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

\u4e3a\u4e86\u52a0\u6df1\u5bf9\u5217\u8868\u5de5\u4f5c\u539f\u7406\u7684\u7406\u89e3\uff0c\u6211\u4eec\u5c1d\u8bd5\u5b9e\u73b0\u4e00\u4e2a\u7b80\u6613\u7248\u5217\u8868\uff0c\u5305\u62ec\u4ee5\u4e0b\u4e09\u4e2a\u91cd\u70b9\u8bbe\u8ba1\u3002

  • \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\u82e5\u63d2\u5165\u5143\u7d20\u65f6\u5217\u8868\u5bb9\u91cf\u5df2\u6ee1\uff0c\u5219\u9700\u8981\u8fdb\u884c\u6269\u5bb9\u3002\u9996\u5148\u6839\u636e\u6269\u5bb9\u500d\u6570\u521b\u5efa\u4e00\u4e2a\u66f4\u5927\u7684\u6570\u7ec4\uff0c\u518d\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
PythonC++JavaC#GoSwiftJSTSDartRustCZig 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._arr: 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._arr[index]\ndef set(self, num: int, index: int):\n\"\"\"\u66f4\u65b0\u5143\u7d20\"\"\"\nif index < 0 or index >= self._size:\nraise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\nself._arr[index] = num\ndef add(self, num: int):\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._arr[self._size] = num\nself._size += 1\ndef insert(self, num: int, index: int):\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# \u5c06\u7d22\u5f15 index \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._arr[j + 1] = self._arr[j]\nself._arr[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._arr[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._arr[j] = self._arr[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):\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._arr = self._arr + [0] * self.capacity() * (self._extend_ratio - 1)\n# \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nself._capacity = len(self._arr)\ndef to_array(self) -> list[int]:\n\"\"\"\u8fd4\u56de\u6709\u6548\u957f\u5ea6\u7684\u5217\u8868\"\"\"\nreturn self._arr[: self._size]\n
my_list.cpp
/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate:\nint *arr;             // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nint arrCapacity = 10; // \u5217\u8868\u5bb9\u91cf\nint arrSize = 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() {\narr = new int[arrCapacity];\n}\n/* \u6790\u6784\u65b9\u6cd5 */\n~MyList() {\ndelete[] arr;\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\nint size() {\nreturn arrSize;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nint capacity() {\nreturn arrCapacity;\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 arr[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\");\narr[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();\narr[size()] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\narrSize++;\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// \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--) {\narr[j + 1] = arr[j];\n}\narr[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\narrSize++;\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 = arr[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++) {\narr[j] = arr[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\narrSize--;\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 = arr;\narr = 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++) {\narr[i] = tmp[i];\n}\n// \u91ca\u653e\u5185\u5b58\ndelete[] tmp;\narrCapacity = 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] = arr[i];\n}\nreturn vec;\n}\n};\n
my_list.java
/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate int[] arr; // \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() {\narr = 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 arr[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\");\narr[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();\narr[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--) {\narr[j + 1] = arr[j];\n}\narr[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 = arr[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++) {\narr[j] = arr[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\narr = Arrays.copyOf(arr, capacity() * extendRatio);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\ncapacity = arr.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[] arr = new int[size];\nfor (int i = 0; i < size; i++) {\narr[i] = get(i);\n}\nreturn arr;\n}\n}\n
my_list.cs
/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate int[] arr;           // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nprivate int arrCapacity = 10;    // \u5217\u8868\u5bb9\u91cf\nprivate int arrSize = 0;         // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nprivate readonly int extendRatio = 2;  // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n/* \u6784\u9020\u65b9\u6cd5 */\npublic MyList() {\narr = new int[arrCapacity];\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\npublic int Size() {\nreturn arrSize;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\npublic int Capacity() {\nreturn arrCapacity;\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 >= arrSize)\nthrow new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\nreturn arr[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\npublic void Set(int index, int num) {\nif (index < 0 || index >= arrSize)\nthrow new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\narr[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 (arrSize == arrCapacity)\nExtendCapacity();\narr[arrSize] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\narrSize++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\npublic void Insert(int index, int num) {\nif (index < 0 || index >= arrSize)\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 (arrSize == arrCapacity)\nExtendCapacity();\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (int j = arrSize - 1; j >= index; j--) {\narr[j + 1] = arr[j];\n}\narr[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\narrSize++;\n}\n/* \u5220\u9664\u5143\u7d20 */\npublic int Remove(int index) {\nif (index < 0 || index >= arrSize)\nthrow new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\nint num = arr[index];\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (int j = index; j < arrSize - 1; j++) {\narr[j] = arr[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\narrSize--;\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 arrCapacity * extendRatio \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nArray.Resize(ref arr, arrCapacity * extendRatio);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\narrCapacity = arr.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[] arr = new int[arrSize];\nfor (int i = 0; i < arrSize; i++) {\narr[i] = Get(i);\n}\nreturn arr;\n}\n}\n
my_list.go
/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\ntype myList struct {\narrCapacity int\narr         []int\narrSize     int\nextendRatio int\n}\n/* \u6784\u9020\u51fd\u6570 */\nfunc newMyList() *myList {\nreturn &myList{\narrCapacity: 10,              // \u5217\u8868\u5bb9\u91cf\narr:         make([]int, 10), // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\narrSize:     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.arrSize\n}\n/*  \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nfunc (l *myList) capacity() int {\nreturn l.arrCapacity\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.arrSize {\npanic(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nreturn l.arr[index]\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nfunc (l *myList) set(num, index int) {\nif index < 0 || index >= l.arrSize {\npanic(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nl.arr[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.arrSize == l.arrCapacity {\nl.extendCapacity()\n}\nl.arr[l.arrSize] = num\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nl.arrSize++\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nfunc (l *myList) insert(num, index int) {\nif index < 0 || index >= l.arrSize {\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.arrSize == l.arrCapacity {\nl.extendCapacity()\n}\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor j := l.arrSize - 1; j >= index; j-- {\nl.arr[j+1] = l.arr[j]\n}\nl.arr[index] = num\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nl.arrSize++\n}\n/* \u5220\u9664\u5143\u7d20 */\nfunc (l *myList) remove(index int) int {\nif index < 0 || index >= l.arrSize {\npanic(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nnum := l.arr[index]\n// \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor j := index; j < l.arrSize-1; j++ {\nl.arr[j] = l.arr[j+1]\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nl.arrSize--\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.arr = append(l.arr, make([]int, l.arrCapacity*(l.extendRatio-1))...)\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nl.arrCapacity = len(l.arr)\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.arr[:l.arrSize]\n}\n
my_list.swift
/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate var arr: [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() {\narr = 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 arr[index]\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nfunc set(index: Int, num: Int) {\nif index < 0 || index >= _size {\nfatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n}\narr[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}\narr[_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 }) {\narr[j + 1] = arr[j]\n}\narr[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 = arr[index]\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor j in index ..< (_size - 1) {\narr[j] = arr[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\narr = arr + Array(repeating: 0, count: _capacity * (extendRatio - 1))\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n_capacity = arr.count\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\nfunc toArray() -> [Int] {\nvar arr = Array(repeating: 0, count: _size)\nfor i in 0 ..< _size {\narr[i] = get(index: i)\n}\nreturn arr\n}\n}\n
my_list.js
/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\n#arr = new Array(); // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n#capacity = 10; // \u5217\u8868\u5bb9\u91cf\n#size = 0; // \u5217\u8868\u957f\u5ea6\uff08\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.#arr = 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.#arr[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nset(index, num) {\nif (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\nthis.#arr[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.#arr[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.#arr[j + 1] = this.#arr[j];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nthis.#arr[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.#arr[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.#arr[j] = this.#arr[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.#arr = this.#arr.concat(\nnew Array(this.capacity() * (this.#extendRatio - 1))\n);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nthis.#capacity = this.#arr.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 arr = new Array(size);\nfor (let i = 0; i < size; i++) {\narr[i] = this.get(i);\n}\nreturn arr;\n}\n}\n
my_list.ts
/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate arr: 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.arr = 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.arr[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.arr[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.arr[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.arr[j + 1] = this.arr[j];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nthis.arr[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.arr[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.arr[j] = this.arr[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.arr = this.arr.concat(\nnew Array(this.capacity() * (this.extendRatio - 1))\n);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nthis._capacity = this.arr.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 arr = new Array(size);\nfor (let i = 0; i < size; i++) {\narr[i] = this.get(i);\n}\nreturn arr;\n}\n}\n
my_list.dart
/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nlate List<int> _arr; // \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_arr = 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 _arr[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nvoid set(int index, int num) {\nif (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n_arr[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_arr[_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_arr[j + 1] = _arr[j];\n}\n_arr[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 = _arr[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_arr[j] = _arr[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, _arr);\n// \u66f4\u65b0 _arr \u7684\u5f15\u7528\n_arr = _newNums;\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n_capacity = _arr.length;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\nList<int> toArray() {\nList<int> arr = [];\nfor (var i = 0; i < _size; i++) {\narr.add(get(i));\n}\nreturn arr;\n}\n}\n
my_list.rs
/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\n#[allow(dead_code)]\nstruct MyList {\narr: Vec<i32>,       // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\ncapacity: usize,      // \u5217\u8868\u5bb9\u91cf\nsize: usize,          // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nextend_ratio: usize,  // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n}\n#[allow(unused,unused_comparisons)]\nimpl MyList {\n/* \u6784\u9020\u65b9\u6cd5 */\npub fn new(capacity: usize) -> Self {\nlet mut vec = Vec::new(); vec.resize(capacity, 0);\nSelf {\narr: vec,\ncapacity,\nsize: 0,\nextend_ratio: 2,\n}\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\npub fn size(&self) -> usize {\nreturn self.size;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\npub fn capacity(&self) -> usize {\nreturn self.capacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\npub fn get(&self, index: usize) -> i32 {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif index < 0 || index >= self.size {panic!(\"\u7d22\u5f15\u8d8a\u754c\")};\nreturn self.arr[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\npub fn set(&mut self, index: usize, num: i32) {\nif index < 0 || index >= self.size {panic!(\"\u7d22\u5f15\u8d8a\u754c\")};\nself.arr[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\npub fn add(&mut self, num: i32) {\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}\nself.arr[self.size] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.size += 1;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\npub fn insert(&mut self, index: usize, num: i32) {\nif index < 0 || 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() {\nself.extend_capacity();\n}\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor j in (index..self.size).rev() {\nself.arr[j + 1] = self.arr[j];\n}\nself.arr[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.size += 1;\n}\n/* \u5220\u9664\u5143\u7d20 */\npub fn remove(&mut self, index: usize) -> i32 {\nif index < 0 || index >= self.size() {panic!(\"\u7d22\u5f15\u8d8a\u754c\")};\nlet num = self.arr[index];\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor j in (index..self.size - 1) {\nself.arr[j] = self.arr[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.size -= 1;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\npub fn extend_capacity(&mut self) {\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extend_ratio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nlet new_capacity = self.capacity * self.extend_ratio;\nself.arr.resize(new_capacity, 0);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nself.capacity = new_capacity;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\npub fn to_array(&mut self) -> Vec<i32> {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nlet mut arr = Vec::new();\nfor i in 0..self.size {\narr.push(self.get(i));\n}\narr\n}\n}\n
my_list.c
/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nstruct myList {\nint *arr;        // \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 *nums = malloc(sizeof(myList));\nnums->capacity = 10;\nnums->arr = malloc(sizeof(int) * nums->capacity);\nnums->size = 0;\nnums->extendRatio = 2;\nreturn nums;\n}\n/* \u6790\u6784\u51fd\u6570 */\nvoid delMyList(myList *nums) {\nfree(nums->arr);\nfree(nums);\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6 */\nint size(myList *nums) {\nreturn nums->size;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nint capacity(myList *nums) {\nreturn nums->capacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\nint get(myList *nums, int index) {\nassert(index >= 0 && index < nums->size);\nreturn nums->arr[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nvoid set(myList *nums, int index, int num) {\nassert(index >= 0 && index < nums->size);\nnums->arr[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nvoid add(myList *nums, int num) {\nif (size(nums) == capacity(nums)) {\nextendCapacity(nums); // \u6269\u5bb9\n}\nnums->arr[size(nums)] = num;\nnums->size++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nvoid insert(myList *nums, int index, int num) {\nassert(index >= 0 && index < size(nums));\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (size(nums) == capacity(nums)) {\nextendCapacity(nums); // \u6269\u5bb9\n}\nfor (int i = size(nums); i > index; --i) {\nnums->arr[i] = nums->arr[i - 1];\n}\nnums->arr[index] = num;\nnums->size++;\n}\n/* \u5220\u9664\u5143\u7d20 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nint removeNum(myList *nums, int index) {\nassert(index >= 0 && index < size(nums));\nint num = nums->arr[index];\nfor (int i = index; i < size(nums) - 1; i++) {\nnums->arr[i] = nums->arr[i + 1];\n}\nnums->size--;\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\nvoid extendCapacity(myList *nums) {\n// \u5148\u5206\u914d\u7a7a\u95f4\nint newCapacity = capacity(nums) * nums->extendRatio;\nint *extend = (int *)malloc(sizeof(int) * newCapacity);\nint *temp = nums->arr;\n// \u62f7\u8d1d\u65e7\u6570\u636e\u5230\u65b0\u6570\u636e\nfor (int i = 0; i < size(nums); i++)\nextend[i] = nums->arr[i];\n// \u91ca\u653e\u65e7\u6570\u636e\nfree(temp);\n// \u66f4\u65b0\u65b0\u6570\u636e\nnums->arr = extend;\nnums->capacity = newCapacity;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a Array \u7528\u4e8e\u6253\u5370 */\nint *toArray(myList *nums) {\nreturn nums->arr;\n}\n
my_list.zig
// \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0\nfn MyList(comptime T: type) type {\nreturn struct {\nconst Self = @This();\narr: []T = undefined,                        // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\narrCapacity: usize = 10,                     // \u5217\u8868\u5bb9\u91cf\nnumSize: usize = 0,                           // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nextendRatio: 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\u51fd\u6570\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.arr = try self.mem_allocator.alloc(T, self.arrCapacity);\n@memset(self.arr, @as(T, 0));\n}\n// \u6790\u6784\u51fd\u6570\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.numSize;\n}\n// \u83b7\u53d6\u5217\u8868\u5bb9\u91cf\npub fn capacity(self: *Self) usize {\nreturn self.arrCapacity;\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.arr[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.arr[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.arr[self.size()] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.numSize += 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// \u5c06\u7d22\u5f15 index \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.arr[j + 1] = self.arr[j];\n}\nself.arr[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.numSize += 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.arr[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.arr[j] = self.arr[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.numSize -= 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 size * extendRatio \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nvar newCapacity = self.capacity() * self.extendRatio;\nvar extend = try self.mem_allocator.alloc(T, newCapacity);\n@memset(extend, @as(T, 0));\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nstd.mem.copy(T, extend, self.arr);\nself.arr = extend;\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nself.arrCapacity = 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 arr = try self.mem_allocator.alloc(T, self.size());\n@memset(arr, @as(T, 0));\nfor (arr, 0..) |*num, i| {\nnum.* = self.get(i);\n}\nreturn arr;\n}\n};\n}\n
"},{"location":"chapter_array_and_linkedlist/summary/","title":"4.4 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_array_and_linkedlist/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
  • \u6570\u7ec4\u548c\u94fe\u8868\u662f\u4e24\u79cd\u57fa\u672c\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5206\u522b\u4ee3\u8868\u6570\u636e\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u4e24\u79cd\u5b58\u50a8\u65b9\u5f0f\uff1a\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u548c\u5206\u6563\u7a7a\u95f4\u5b58\u50a8\u3002\u4e24\u8005\u7684\u7279\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\u5f15\u7528\uff08\u6307\u9488\uff09\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
"},{"location":"chapter_array_and_linkedlist/summary/#2-q-a","title":"2. \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

\u5b58\u50a8\u5728\u6808\u4e0a\u548c\u5806\u4e0a\u7684\u6570\u7ec4\u90fd\u88ab\u5b58\u50a8\u5728\u8fde\u7eed\u5185\u5b58\u7a7a\u95f4\u5185\uff0c\u6570\u636e\u64cd\u4f5c\u6548\u7387\u662f\u57fa\u672c\u4e00\u81f4\u7684\u3002\u7136\u800c\uff0c\u6808\u548c\u5806\u5177\u6709\u5404\u81ea\u7684\u7279\u70b9\uff0c\u4ece\u800c\u5bfc\u81f4\u4ee5\u4e0b\u4e0d\u540c\u70b9\u3002

  1. \u5206\u914d\u548c\u91ca\u653e\u6548\u7387\uff1a\u6808\u662f\u4e00\u5757\u8f83\u5c0f\u7684\u5185\u5b58\uff0c\u5206\u914d\u7531\u7f16\u8bd1\u5668\u81ea\u52a8\u5b8c\u6210\uff1b\u800c\u5806\u5185\u5b58\u76f8\u5bf9\u66f4\u5927\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u52a8\u6001\u5206\u914d\uff0c\u66f4\u5bb9\u6613\u788e\u7247\u5316\u3002\u56e0\u6b64\uff0c\u5806\u4e0a\u7684\u5206\u914d\u548c\u91ca\u653e\u64cd\u4f5c\u901a\u5e38\u6bd4\u6808\u4e0a\u7684\u6162\u3002
  2. \u5927\u5c0f\u9650\u5236\uff1a\u6808\u5185\u5b58\u76f8\u5bf9\u8f83\u5c0f\uff0c\u5806\u7684\u5927\u5c0f\u4e00\u822c\u53d7\u9650\u4e8e\u53ef\u7528\u5185\u5b58\u3002\u56e0\u6b64\u5806\u66f4\u52a0\u9002\u5408\u5b58\u50a8\u5927\u578b\u6570\u7ec4\u3002
  3. \u7075\u6d3b\u6027\uff1a\u6808\u4e0a\u7684\u6570\u7ec4\u7684\u5927\u5c0f\u9700\u8981\u5728\u7f16\u8bd1\u65f6\u786e\u5b9a\uff0c\u800c\u5806\u4e0a\u7684\u6570\u7ec4\u7684\u5927\u5c0f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u786e\u5b9a\u3002

\u4e3a\u4ec0\u4e48\u6570\u7ec4\u8981\u6c42\u76f8\u540c\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u800c\u5728\u94fe\u8868\u4e2d\u5374\u6ca1\u6709\u5f3a\u8c03\u540c\u7c7b\u578b\u5462\uff1f

\u94fe\u8868\u7531\u7ed3\u70b9\u7ec4\u6210\uff0c\u7ed3\u70b9\u4e4b\u95f4\u901a\u8fc7\u5f15\u7528\uff08\u6307\u9488\uff09\u8fde\u63a5\uff0c\u5404\u4e2a\u7ed3\u70b9\u53ef\u4ee5\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u4f8b\u5982 int\u3001double\u3001string\u3001object \u7b49\u3002

\u76f8\u5bf9\u5730\uff0c\u6570\u7ec4\u5143\u7d20\u5219\u5fc5\u987b\u662f\u76f8\u540c\u7c7b\u578b\u7684\uff0c\u8fd9\u6837\u624d\u80fd\u901a\u8fc7\u8ba1\u7b97\u504f\u79fb\u91cf\u6765\u83b7\u53d6\u5bf9\u5e94\u5143\u7d20\u4f4d\u7f6e\u3002\u4f8b\u5982\uff0c\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\u957f\u5ea6\u7684\u5143\u7d20\u3002

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

\u5220\u9664\u8282\u70b9\u540e\uff0c\u662f\u5426\u9700\u8981\u628a P.next \u8bbe\u4e3a \\(\\text{None}\\) \u5462\uff1f

\u4e0d\u4fee\u6539 P.next \u4e5f\u53ef\u4ee5\u3002\u4ece\u8be5\u94fe\u8868\u7684\u89d2\u5ea6\u770b\uff0c\u4ece\u5934\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\u3001Python\u3001Go \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 \u548c C++ \u7b49\u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u624b\u52a8\u91ca\u653e\u8282\u70b9\u5185\u5b58\u3002

\u5728\u94fe\u8868\u4e2d\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u3002\u4f46\u662f\u589e\u5220\u4e4b\u524d\u90fd\u9700\u8981 \\(O(n)\\) \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\u7684\u793a\u610f\u56fe\u53ea\u662f\u5b9a\u6027\u8868\u793a\uff0c\u5b9a\u91cf\u8868\u793a\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u5206\u6790\u3002

  • \u4e0d\u540c\u7c7b\u578b\u7684\u7ed3\u70b9\u503c\u5360\u7528\u7684\u7a7a\u95f4\u662f\u4e0d\u540c\u7684\uff0c\u6bd4\u5982 int\u3001long\u3001double \u548c\u5b9e\u4f8b\u5bf9\u8c61\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

\u5728 Python \u4e2d\u521d\u59cb\u5316 n = [1, 2, 3] \u540e\uff0c\u8fd9 3 \u4e2a\u5143\u7d20\u7684\u5730\u5740\u662f\u76f8\u8fde\u7684\uff0c\u4f46\u662f\u521d\u59cb\u5316 m = [2, 1, 3] \u4f1a\u53d1\u73b0\u5b83\u4eec\u6bcf\u4e2a\u5143\u7d20\u7684 id \u5e76\u4e0d\u662f\u8fde\u7eed\u7684\uff0c\u800c\u662f\u5206\u522b\u8ddf n \u4e2d\u7684\u76f8\u540c\u3002\u8fd9\u4e9b\u5143\u7d20\u5730\u5740\u4e0d\u8fde\u7eed\uff0c\u90a3\u4e48 m \u8fd8\u662f\u6570\u7ec4\u5417\uff1f

\u5047\u5982\u628a\u5217\u8868\u5143\u7d20\u6362\u6210\u94fe\u8868\u8282\u70b9 n = [n1, n2, n3, n4, n5] \uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u8fd9\u4e94\u4e2a\u8282\u70b9\u5bf9\u8c61\u4e5f\u662f\u88ab\u5206\u6563\u5b58\u50a8\u5728\u5185\u5b58\u5404\u5904\u7684\u3002\u7136\u800c\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u5217\u8868\u7d22\u5f15\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u83b7\u53d6\u5230\u8282\u70b9\u5185\u5b58\u5730\u5740\uff0c\u4ece\u800c\u8bbf\u95ee\u5230\u5bf9\u5e94\u7684\u8282\u70b9\u3002\u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u4e2d\u5b58\u50a8\u7684\u662f\u8282\u70b9\u7684\u5f15\u7528\uff0c\u800c\u975e\u8282\u70b9\u672c\u8eab\u3002

\u4e0e\u8bb8\u591a\u8bed\u8a00\u4e0d\u540c\u7684\u662f\uff0c\u5728 Python \u4e2d\u6570\u5b57\u4e5f\u88ab\u5305\u88c5\u4e3a\u5bf9\u8c61\uff0c\u5217\u8868\u4e2d\u5b58\u50a8\u7684\u4e0d\u662f\u6570\u5b57\u672c\u8eab\uff0c\u800c\u662f\u5bf9\u6570\u5b57\u7684\u5f15\u7528\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4f1a\u53d1\u73b0\u4e24\u4e2a\u6570\u7ec4\u4e2d\u7684\u76f8\u540c\u6570\u5b57\u62e5\u6709\u540c\u4e00\u4e2a id \uff0c\u5e76\u4e14\u8fd9\u4e9b\u6570\u5b57\u7684\u5185\u5b58\u5730\u5740\u662f\u65e0\u987b\u8fde\u7eed\u7684\u3002

C++ STL \u91cc\u9762\u7684 std::list \u5df2\u7ecf\u5b9e\u73b0\u4e86\u53cc\u5411\u94fe\u8868\uff0c\u4f46\u597d\u50cf\u4e00\u4e9b\u7b97\u6cd5\u7684\u4e66\u4e0a\u90fd\u4e0d\u600e\u4e48\u76f4\u63a5\u7528\u8fd9\u4e2a\uff0c\u662f\u4e0d\u662f\u6709\u4ec0\u4e48\u5c40\u9650\u6027\u5462?

\u4e00\u65b9\u9762\uff0c\u6211\u4eec\u5f80\u5f80\u66f4\u9752\u7750\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u7b97\u6cd5\uff0c\u800c\u53ea\u6709\u5728\u5fc5\u8981\u65f6\u624d\u4f7f\u7528\u94fe\u8868\uff0c\u4e3b\u8981\u6709\u4e24\u4e2a\u539f\u56e0\u3002

  • \u7a7a\u95f4\u5f00\u9500\uff1a\u7531\u4e8e\u6bcf\u4e2a\u5143\u7d20\u9700\u8981\u4e24\u4e2a\u989d\u5916\u7684\u6307\u9488\uff08\u4e00\u4e2a\u7528\u4e8e\u524d\u4e00\u4e2a\u5143\u7d20\uff0c\u4e00\u4e2a\u7528\u4e8e\u540e\u4e00\u4e2a\u5143\u7d20\uff09\uff0c\u6240\u4ee5 std::list \u901a\u5e38\u6bd4 std::vector \u66f4\u5360\u7528\u7a7a\u95f4\u3002
  • \u7f13\u5b58\u4e0d\u53cb\u597d\uff1a\u7531\u4e8e\u6570\u636e\u4e0d\u662f\u8fde\u7eed\u5b58\u653e\u7684\uff0cstd::list \u5bf9\u7f13\u5b58\u7684\u5229\u7528\u7387\u8f83\u4f4e\u3002\u4e00\u822c\u60c5\u51b5\u4e0b\uff0cstd::vector \u7684\u6027\u80fd\u4f1a\u66f4\u597d\u3002

\u53e6\u4e00\u65b9\u9762\uff0c\u5fc5\u8981\u4f7f\u7528\u94fe\u8868\u7684\u60c5\u51b5\u4e3b\u8981\u662f\u4e8c\u53c9\u6811\u548c\u56fe\u3002\u6808\u548c\u961f\u5217\u5f80\u5f80\u4f1a\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b\u7684 stack \u548c queue \uff0c\u800c\u975e\u94fe\u8868\u3002

"},{"location":"chapter_backtracking/","title":"\u7b2c 13 \u7ae0 \u00a0 \u56de\u6eaf","text":"

Abstract

\u6211\u4eec\u5982\u540c\u8ff7\u5bab\u4e2d\u7684\u63a2\u7d22\u8005\uff0c\u5728\u524d\u8fdb\u7684\u9053\u8def\u4e0a\u53ef\u80fd\u4f1a\u9047\u5230\u56f0\u96be\u3002

\u56de\u6eaf\u7684\u529b\u91cf\u8ba9\u6211\u4eec\u80fd\u591f\u91cd\u65b0\u5f00\u59cb\uff0c\u4e0d\u65ad\u5c1d\u8bd5\uff0c\u6700\u7ec8\u627e\u5230\u901a\u5f80\u5149\u660e\u7684\u51fa\u53e3\u3002

"},{"location":"chapter_backtracking/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 13.1 \u00a0 \u56de\u6eaf\u7b97\u6cd5
  • 13.2 \u00a0 \u5168\u6392\u5217\u95ee\u9898
  • 13.3 \u00a0 \u5b50\u96c6\u548c\u95ee\u9898
  • 13.4 \u00a0 N \u7687\u540e\u95ee\u9898
  • 13.5 \u00a0 \u5c0f\u7ed3
"},{"location":"chapter_backtracking/backtracking_algorithm/","title":"13.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\u201c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u201d\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\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5229\u7528\u524d\u5e8f\u904d\u5386\u6784\u9020\u4e00\u4e2a\u56de\u6eaf\u95ee\u9898\uff0c\u9010\u6b65\u4e86\u89e3\u56de\u6eaf\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u3002

\u4f8b\u9898\u4e00

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

\u5bf9\u4e8e\u6b64\u9898\uff0c\u6211\u4eec\u524d\u5e8f\u904d\u5386\u8fd9\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\u76f8\u5173\u8fc7\u7a0b\u5b9e\u73b0\u5982\u56fe 13-1 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig preorder_traversal_i_compact.py
def pre_order(root: TreeNode):\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.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.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.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.go
/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunc preOrderI(root *TreeNode, res *[]*TreeNode) {\nif root == nil {\nreturn\n}\nif (root.Val).(int) == 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.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.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.dart
/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode? root, List<TreeNode> res) {\nif (root == null) {\nreturn;\n}\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.add(root);\n}\npreOrder(root.left, res);\npreOrder(root.right, res);\n}\n
preorder_traversal_i_compact.rs
/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfn pre_order(res: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<Rc<RefCell<TreeNode>>>) {\nif root.is_none() {\nreturn;\n}\nif let Some(node) = root {\nif node.borrow().val == 7 {\n// \u8bb0\u5f55\u89e3\nres.push(node.clone());\n}\npre_order(res, node.borrow().left.clone());\npre_order(res, node.borrow().right.clone());\n}\n}\n
preorder_traversal_i_compact.c
[class]{}-[func]{preOrder}\n
preorder_traversal_i_compact.zig
[class]{}-[func]{preOrder}\n

\u56fe 13-1 \u00a0 \u5728\u524d\u5e8f\u904d\u5386\u4e2d\u641c\u7d22\u8282\u70b9

"},{"location":"chapter_backtracking/backtracking_algorithm/#1311","title":"13.1.1 \u00a0 \u5c1d\u8bd5\u4e0e\u56de\u9000","text":"

\u4e4b\u6240\u4ee5\u79f0\u4e4b\u4e3a\u56de\u6eaf\u7b97\u6cd5\uff0c\u662f\u56e0\u4e3a\u8be5\u7b97\u6cd5\u5728\u641c\u7d22\u89e3\u7a7a\u95f4\u65f6\u4f1a\u91c7\u7528\u201c\u5c1d\u8bd5\u201d\u4e0e\u201c\u56de\u9000\u201d\u7684\u7b56\u7565\u3002\u5f53\u7b97\u6cd5\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u9047\u5230\u67d0\u4e2a\u72b6\u6001\u65e0\u6cd5\u7ee7\u7eed\u524d\u8fdb\u6216\u65e0\u6cd5\u5f97\u5230\u6ee1\u8db3\u6761\u4ef6\u7684\u89e3\u65f6\uff0c\u5b83\u4f1a\u64a4\u9500\u4e0a\u4e00\u6b65\u7684\u9009\u62e9\uff0c\u9000\u56de\u5230\u4e4b\u524d\u7684\u72b6\u6001\uff0c\u5e76\u5c1d\u8bd5\u5176\u4ed6\u53ef\u80fd\u7684\u9009\u62e9\u3002

\u5bf9\u4e8e\u4f8b\u9898\u4e00\uff0c\u8bbf\u95ee\u6bcf\u4e2a\u8282\u70b9\u90fd\u4ee3\u8868\u4e00\u6b21\u201c\u5c1d\u8bd5\u201d\uff0c\u800c\u8d8a\u8fc7\u53f6\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\u4ec5\u4ec5\u5305\u62ec\u51fd\u6570\u8fd4\u56de\u3002\u4e3a\u89e3\u91ca\u8fd9\u4e00\u70b9\uff0c\u6211\u4eec\u5bf9\u4f8b\u9898\u4e00\u7a0d\u4f5c\u62d3\u5c55\u3002

\u4f8b\u9898\u4e8c

\u5728\u4e8c\u53c9\u6811\u4e2d\u641c\u7d22\u6240\u6709\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\uff0c\u8bf7\u8fd4\u56de\u6839\u8282\u70b9\u5230\u8fd9\u4e9b\u8282\u70b9\u7684\u8def\u5f84\u3002

\u5728\u4f8b\u9898\u4e00\u4ee3\u7801\u7684\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u5217\u8868 path \u8bb0\u5f55\u8bbf\u95ee\u8fc7\u7684\u8282\u70b9\u8def\u5f84\u3002\u5f53\u8bbf\u95ee\u5230\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\u65f6\uff0c\u5219\u590d\u5236 path \u5e76\u6dfb\u52a0\u8fdb\u7ed3\u679c\u5217\u8868 res \u3002\u904d\u5386\u5b8c\u6210\u540e\uff0cres \u4e2d\u4fdd\u5b58\u7684\u5c31\u662f\u6240\u6709\u7684\u89e3\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig preorder_traversal_ii_compact.py
def pre_order(root: TreeNode):\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.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.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.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.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 root.Val.(int) == 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.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.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.dart
/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(\nTreeNode? root,\nList<TreeNode> path,\nList<List<TreeNode>> res,\n) {\nif (root == null) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.add(root);\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.add(List.from(path));\n}\npreOrder(root.left, path, res);\npreOrder(root.right, path, res);\n// \u56de\u9000\npath.removeLast();\n}\n
preorder_traversal_ii_compact.rs
/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfn pre_order(res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>, path: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<Rc<RefCell<TreeNode>>>) {\nif root.is_none() {\nreturn;\n}\nif let Some(node) = root {\n// \u5c1d\u8bd5\npath.push(node.clone());\nif node.borrow().val == 7 {\n// \u8bb0\u5f55\u89e3\nres.push(path.clone());\n}\npre_order(res, path, node.borrow().left.clone());\npre_order(res, path, node.borrow().right.clone());\n// \u56de\u9000\npath.remove(path.len() -  1);\n}\n}\n
preorder_traversal_ii_compact.c
/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(TreeNode *root, vector *path, vector *res) {\nif (root == NULL) {\nreturn;\n}\n// \u5c1d\u8bd5\nvectorPushback(path, root, sizeof(TreeNode));\nif (root->val == 7) {\n// \u8bb0\u5f55\u89e3\nvector *newPath = newVector();\nfor (int i = 0; i < path->size; i++) {\nvectorPushback(newPath, path->data[i], sizeof(int));\n}\nvectorPushback(res, newPath, sizeof(vector));\n}\npreOrder(root->left, path, res);\npreOrder(root->right, path, res);\n// \u56de\u9000\nvectorPopback(path);\n}\n
preorder_traversal_ii_compact.zig
[class]{}-[func]{preOrder}\n

\u5728\u6bcf\u6b21\u201c\u5c1d\u8bd5\u201d\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5c06\u5f53\u524d\u8282\u70b9\u6dfb\u52a0\u8fdb path \u6765\u8bb0\u5f55\u8def\u5f84\uff1b\u800c\u5728\u201c\u56de\u9000\u201d\u524d\uff0c\u6211\u4eec\u9700\u8981\u5c06\u8be5\u8282\u70b9\u4ece path \u4e2d\u5f39\u51fa\uff0c\u4ee5\u6062\u590d\u672c\u6b21\u5c1d\u8bd5\u4e4b\u524d\u7684\u72b6\u6001\u3002

\u89c2\u5bdf\u56fe 13-2 \u6240\u793a\u7684\u8fc7\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5c1d\u8bd5\u548c\u56de\u9000\u7406\u89e3\u4e3a\u201c\u524d\u8fdb\u201d\u4e0e\u201c\u64a4\u9500\u201d\uff0c\u4e24\u4e2a\u64cd\u4f5c\u662f\u4e92\u4e3a\u9006\u5411\u7684\u3002

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

\u56fe 13-2 \u00a0 \u5c1d\u8bd5\u4e0e\u56de\u9000

"},{"location":"chapter_backtracking/backtracking_algorithm/#1312","title":"13.1.2 \u00a0 \u526a\u679d","text":"

\u590d\u6742\u7684\u56de\u6eaf\u95ee\u9898\u901a\u5e38\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a\u7ea6\u675f\u6761\u4ef6\uff0c\u7ea6\u675f\u6761\u4ef6\u901a\u5e38\u53ef\u7528\u4e8e\u201c\u526a\u679d\u201d\u3002

\u4f8b\u9898\u4e09

\u5728\u4e8c\u53c9\u6811\u4e2d\u641c\u7d22\u6240\u6709\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\uff0c\u8bf7\u8fd4\u56de\u6839\u8282\u70b9\u5230\u8fd9\u4e9b\u8282\u70b9\u7684\u8def\u5f84\uff0c\u5e76\u8981\u6c42\u8def\u5f84\u4e2d\u4e0d\u5305\u542b\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\u3002

\u4e3a\u4e86\u6ee1\u8db3\u4ee5\u4e0a\u7ea6\u675f\u6761\u4ef6\uff0c\u6211\u4eec\u9700\u8981\u6dfb\u52a0\u526a\u679d\u64cd\u4f5c\uff1a\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u82e5\u9047\u5230\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\uff0c\u5219\u63d0\u524d\u8fd4\u56de\uff0c\u505c\u6b62\u7ee7\u7eed\u641c\u7d22\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig preorder_traversal_iii_compact.py
def pre_order(root: TreeNode):\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.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.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.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.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 root.Val.(int) == 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.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.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.dart
/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(\nTreeNode? root,\nList<TreeNode> path,\nList<List<TreeNode>> res,\n) {\nif (root == null || root.val == 3) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.add(root);\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.add(List.from(path));\n}\npreOrder(root.left, path, res);\npreOrder(root.right, path, res);\n// \u56de\u9000\npath.removeLast();\n}\n
preorder_traversal_iii_compact.rs
/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfn pre_order(res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>, path: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<Rc<RefCell<TreeNode>>>) {\n// \u526a\u679d\nif root.is_none() || root.as_ref().unwrap().borrow().val == 3 {\nreturn;\n}\nif let Some(node) = root {\n// \u5c1d\u8bd5\npath.push(node.clone());\nif node.borrow().val == 7 {\n// \u8bb0\u5f55\u89e3\nres.push(path.clone());\n}\npre_order(res, path, node.borrow().left.clone());\npre_order(res, path, node.borrow().right.clone());\n// \u56de\u9000\npath.remove(path.len() -  1);\n}\n}\n
preorder_traversal_iii_compact.c
/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(TreeNode *root, vector *path, vector *res) {\n// \u526a\u679d\nif (root == NULL || root->val == 3) {\nreturn;\n}\n// \u5c1d\u8bd5\nvectorPushback(path, root, sizeof(TreeNode));\nif (root->val == 7) {\n// \u8bb0\u5f55\u89e3\nvector *newPath = newVector();\nfor (int i = 0; i < path->size; i++) {\nvectorPushback(newPath, path->data[i], sizeof(int));\n}\nvectorPushback(res, newPath, sizeof(vector));\nres->depth++;\n}\npreOrder(root->left, path, res);\npreOrder(root->right, path, res);\n// \u56de\u9000\nvectorPopback(path);\n}\n
preorder_traversal_iii_compact.zig
[class]{}-[func]{preOrder}\n

\u526a\u679d\u662f\u4e00\u4e2a\u975e\u5e38\u5f62\u8c61\u7684\u540d\u8bcd\u3002\u5982\u56fe 13-3 \u6240\u793a\uff0c\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u201c\u526a\u6389\u201d\u4e86\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u641c\u7d22\u5206\u652f\uff0c\u907f\u514d\u8bb8\u591a\u65e0\u610f\u4e49\u7684\u5c1d\u8bd5\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u641c\u7d22\u6548\u7387\u3002

\u56fe 13-3 \u00a0 \u6839\u636e\u7ea6\u675f\u6761\u4ef6\u526a\u679d

"},{"location":"chapter_backtracking/backtracking_algorithm/#1313","title":"13.1.3 \u00a0 \u6846\u67b6\u4ee3\u7801","text":"

\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c1d\u8bd5\u5c06\u56de\u6eaf\u7684\u201c\u5c1d\u8bd5\u3001\u56de\u9000\u3001\u526a\u679d\u201d\u7684\u4e3b\u4f53\u6846\u67b6\u63d0\u70bc\u51fa\u6765\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u901a\u7528\u6027\u3002

\u5728\u4ee5\u4e0b\u6846\u67b6\u4ee3\u7801\u4e2d\uff0cstate \u8868\u793a\u95ee\u9898\u7684\u5f53\u524d\u72b6\u6001\uff0cchoices \u8868\u793a\u5f53\u524d\u72b6\u6001\u4e0b\u53ef\u4ee5\u505a\u51fa\u7684\u9009\u62e9\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig
def backtrack(state: State, choices: list[choice], res: list[state]):\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)\n# \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\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 */\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);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\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, List<Choice> choices, List<State> res) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\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, List<Choice> choices, List<State> res) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif (IsSolution(state)) {\n// \u8bb0\u5f55\u89e3\nRecordSolution(state, res);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\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 *State, choices []Choice, res *[]State) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif isSolution(state) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res)\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\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 */\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)\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\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
/* \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);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\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);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\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, List<Choice>, List<State> res) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\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
/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfn backtrack(state: &mut State, choices: &Vec<Choice>, res: &mut Vec<State>) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif is_solution(state) {\n// \u8bb0\u5f55\u89e3\nrecord_solution(state, res);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\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 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}\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);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\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
\n

\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u57fa\u4e8e\u6846\u67b6\u4ee3\u7801\u6765\u89e3\u51b3\u4f8b\u9898\u4e09\u3002\u72b6\u6001 state \u4e3a\u8282\u70b9\u904d\u5386\u8def\u5f84\uff0c\u9009\u62e9 choices \u4e3a\u5f53\u524d\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\uff0c\u7ed3\u679c res \u662f\u8def\u5f84\u5217\u8868\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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)\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.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);\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.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);\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.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);\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.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)\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.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)\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.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);\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);\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.dart
/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool isSolution(List<TreeNode> state) {\nreturn state.isNotEmpty && state.last.val == 7;\n}\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\nres.add(List.from(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.removeLast();\n}\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid backtrack(\nList<TreeNode> state,\nList<TreeNode?> choices,\nList<List<TreeNode>> res,\n) {\n// \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (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, [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.rs
/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfn is_solution(state: &mut Vec<Rc<RefCell<TreeNode>>>) -> bool {\nreturn !state.is_empty() && state.get(state.len() - 1).unwrap().borrow().val == 7;\n}\n/* \u8bb0\u5f55\u89e3 */\nfn record_solution(state: &mut Vec<Rc<RefCell<TreeNode>>>, res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>) {\nres.push(state.clone());\n}\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfn is_valid(_: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Rc<RefCell<TreeNode>>) -> bool {\nreturn choice.borrow().val != 3;\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nfn make_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Rc<RefCell<TreeNode>>) {\nstate.push(choice);\n}\n/* \u6062\u590d\u72b6\u6001 */\nfn undo_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, _: Rc<RefCell<TreeNode>>) {\nstate.remove(state.len() - 1);\n}\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfn backtrack(state: &mut Vec<Rc<RefCell<TreeNode>>>, choices: &mut Vec<Rc<RefCell<TreeNode>>>, res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>) {\n// \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\nif is_solution(state) {\n// \u8bb0\u5f55\u89e3\nrecord_solution(state, res);\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 is_valid(state, choice.clone()) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmake_choice(state, choice.clone());\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, &mut vec![choice.borrow().left.clone().unwrap(), choice.borrow().right.clone().unwrap()], res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundo_choice(state, choice.clone());\n}\n}\n}\n
preorder_traversal_iii_template.c
/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool isSolution(vector *state) {\nreturn state->size != 0 && ((TreeNode *)(state->data[state->size - 1]))->val == 7;\n}\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(vector *state, vector *res) {\nvector *newPath = newVector();\nfor (int i = 0; i < state->size; i++) {\nvectorPushback(newPath, state->data[i], sizeof(int));\n}\nvectorPushback(res, newPath, sizeof(vector));\n}\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nbool isValid(vector *state, TreeNode *choice) {\nreturn choice != NULL && choice->val != 3;\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(vector *state, TreeNode *choice) {\nvectorPushback(state, choice, sizeof(TreeNode));\n}\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(vector *state, TreeNode *choice) {\nvectorPopback(state);\n}\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid backtrack(vector *state, vector *choices, vector *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 (int i = 0; i < choices->size; i++) {\nTreeNode *choice = choices->data[i];\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 *nextChoices = newVector();\nvectorPushback(nextChoices, choice->left, sizeof(TreeNode));\nvectorPushback(nextChoices, choice->right, sizeof(TreeNode));\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.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

\u6839\u636e\u9898\u610f\uff0c\u6211\u4eec\u5728\u627e\u5230\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\u540e\u5e94\u8be5\u7ee7\u7eed\u641c\u7d22\uff0c\u56e0\u6b64\u9700\u8981\u5c06\u8bb0\u5f55\u89e3\u4e4b\u540e\u7684 return \u8bed\u53e5\u5220\u9664\u3002\u56fe 13-4 \u5bf9\u6bd4\u4e86\u4fdd\u7559\u6216\u5220\u9664 return \u8bed\u53e5\u7684\u641c\u7d22\u8fc7\u7a0b\u3002

\u56fe 13-4 \u00a0 \u4fdd\u7559\u4e0e\u5220\u9664 return \u7684\u641c\u7d22\u8fc7\u7a0b\u5bf9\u6bd4

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

"},{"location":"chapter_backtracking/backtracking_algorithm/#1314","title":"13.1.4 \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

\u8868 13-1 \u00a0 \u5e38\u89c1\u7684\u56de\u6eaf\u7b97\u6cd5\u672f\u8bed

\u540d\u8bcd \u5b9a\u4e49 \u4f8b\u9898\u4e09 \u89e3 Solution \u89e3\u662f\u6ee1\u8db3\u95ee\u9898\u7279\u5b9a\u6761\u4ef6\u7684\u7b54\u6848\uff0c\u53ef\u80fd\u6709\u4e00\u4e2a\u6216\u591a\u4e2a \u6839\u8282\u70b9\u5230\u8282\u70b9 \\(7\\) \u7684\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u6240\u6709\u8def\u5f84 \u7ea6\u675f\u6761\u4ef6 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 \u8def\u5f84\u4e2d\u4e0d\u5305\u542b\u8282\u70b9 \\(3\\) \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 \u5c1d\u8bd5 Attempt \u5c1d\u8bd5\u662f\u6839\u636e\u53ef\u7528\u9009\u62e9\u6765\u63a2\u7d22\u89e3\u7a7a\u95f4\u7684\u8fc7\u7a0b\uff0c\u5305\u62ec\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\uff0c\u68c0\u67e5\u662f\u5426\u4e3a\u89e3 \u9012\u5f52\u8bbf\u95ee\u5de6\uff08\u53f3\uff09\u5b50\u8282\u70b9\uff0c\u5c06\u8282\u70b9\u6dfb\u52a0\u8fdb path \uff0c\u5224\u65ad\u8282\u70b9\u7684\u503c\u662f\u5426\u4e3a \\(7\\) \u56de\u9000 Backtracking \u56de\u9000\u6307\u9047\u5230\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u72b6\u6001\u65f6\uff0c\u64a4\u9500\u524d\u9762\u505a\u51fa\u7684\u9009\u62e9\uff0c\u56de\u5230\u4e0a\u4e00\u4e2a\u72b6\u6001 \u5f53\u8d8a\u8fc7\u53f6\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

\u95ee\u9898\u3001\u89e3\u3001\u72b6\u6001\u7b49\u6982\u5ff5\u662f\u901a\u7528\u7684\uff0c\u5728\u5206\u6cbb\u3001\u56de\u6eaf\u3001\u52a8\u6001\u89c4\u5212\u3001\u8d2a\u5fc3\u7b49\u7b97\u6cd5\u4e2d\u90fd\u6709\u6d89\u53ca\u3002

"},{"location":"chapter_backtracking/backtracking_algorithm/#1315","title":"13.1.5 \u00a0 \u4f18\u52bf\u4e0e\u5c40\u9650\u6027","text":"

\u56de\u6eaf\u7b97\u6cd5\u672c\u8d28\u4e0a\u662f\u4e00\u79cd\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u7b97\u6cd5\uff0c\u5b83\u5c1d\u8bd5\u6240\u6709\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\u76f4\u5230\u627e\u5230\u6ee1\u8db3\u6761\u4ef6\u7684\u89e3\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u80fd\u591f\u627e\u5230\u6240\u6709\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u800c\u4e14\u5728\u5408\u7406\u7684\u526a\u679d\u64cd\u4f5c\u4e0b\uff0c\u5177\u6709\u5f88\u9ad8\u7684\u6548\u7387\u3002

\u7136\u800c\uff0c\u5728\u5904\u7406\u5927\u89c4\u6a21\u6216\u8005\u590d\u6742\u95ee\u9898\u65f6\uff0c\u56de\u6eaf\u7b97\u6cd5\u7684\u8fd0\u884c\u6548\u7387\u53ef\u80fd\u96be\u4ee5\u63a5\u53d7\u3002

  • \u65f6\u95f4\uff1a\u56de\u6eaf\u7b97\u6cd5\u901a\u5e38\u9700\u8981\u904d\u5386\u72b6\u6001\u7a7a\u95f4\u7684\u6240\u6709\u53ef\u80fd\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u8fbe\u5230\u6307\u6570\u9636\u6216\u9636\u4e58\u9636\u3002
  • \u7a7a\u95f4\uff1a\u5728\u9012\u5f52\u8c03\u7528\u4e2d\u9700\u8981\u4fdd\u5b58\u5f53\u524d\u7684\u72b6\u6001\uff08\u4f8b\u5982\u8def\u5f84\u3001\u7528\u4e8e\u526a\u679d\u7684\u8f85\u52a9\u53d8\u91cf\u7b49\uff09\uff0c\u5f53\u6df1\u5ea6\u5f88\u5927\u65f6\uff0c\u7a7a\u95f4\u9700\u6c42\u53ef\u80fd\u4f1a\u53d8\u5f97\u5f88\u5927\u3002

\u5373\u4fbf\u5982\u6b64\uff0c\u56de\u6eaf\u7b97\u6cd5\u4ecd\u7136\u662f\u67d0\u4e9b\u641c\u7d22\u95ee\u9898\u548c\u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\u7684\u6700\u4f73\u89e3\u51b3\u65b9\u6848\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u95ee\u9898\uff0c\u7531\u4e8e\u65e0\u6cd5\u9884\u6d4b\u54ea\u4e9b\u9009\u62e9\u53ef\u751f\u6210\u6709\u6548\u7684\u89e3\uff0c\u56e0\u6b64\u6211\u4eec\u5fc5\u987b\u5bf9\u6240\u6709\u53ef\u80fd\u7684\u9009\u62e9\u8fdb\u884c\u904d\u5386\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5173\u952e\u662f\u5982\u4f55\u8fdb\u884c\u6548\u7387\u4f18\u5316\uff0c\u5e38\u89c1\u7684\u6548\u7387\u4f18\u5316\u65b9\u6cd5\u6709\u4e24\u79cd\u3002

  • \u526a\u679d\uff1a\u907f\u514d\u641c\u7d22\u90a3\u4e9b\u80af\u5b9a\u4e0d\u4f1a\u4ea7\u751f\u89e3\u7684\u8def\u5f84\uff0c\u4ece\u800c\u8282\u7701\u65f6\u95f4\u548c\u7a7a\u95f4\u3002
  • \u542f\u53d1\u5f0f\u641c\u7d22\uff1a\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u5f15\u5165\u4e00\u4e9b\u7b56\u7565\u6216\u8005\u4f30\u8ba1\u503c\uff0c\u4ece\u800c\u4f18\u5148\u641c\u7d22\u6700\u6709\u53ef\u80fd\u4ea7\u751f\u6709\u6548\u89e3\u7684\u8def\u5f84\u3002
"},{"location":"chapter_backtracking/backtracking_algorithm/#1316","title":"13.1.6 \u00a0 \u56de\u6eaf\u5178\u578b\u4f8b\u9898","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

\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

\u8bf7\u6ce8\u610f\uff0c\u5bf9\u4e8e\u8bb8\u591a\u7ec4\u5408\u4f18\u5316\u95ee\u9898\uff0c\u56de\u6eaf\u90fd\u4e0d\u662f\u6700\u4f18\u89e3\u51b3\u65b9\u6848\u3002

  • 0-1 \u80cc\u5305\u95ee\u9898\u901a\u5e38\u4f7f\u7528\u52a8\u6001\u89c4\u5212\u89e3\u51b3\uff0c\u4ee5\u8fbe\u5230\u66f4\u9ad8\u7684\u65f6\u95f4\u6548\u7387\u3002
  • \u65c5\u884c\u5546\u662f\u4e00\u4e2a\u8457\u540d\u7684 NP-Hard \u95ee\u9898\uff0c\u5e38\u7528\u89e3\u6cd5\u6709\u9057\u4f20\u7b97\u6cd5\u548c\u8681\u7fa4\u7b97\u6cd5\u7b49\u3002
  • \u6700\u5927\u56e2\u95ee\u9898\u662f\u56fe\u8bba\u4e2d\u7684\u4e00\u4e2a\u7ecf\u5178\u95ee\u9898\uff0c\u53ef\u7528\u8d2a\u5fc3\u7b49\u542f\u53d1\u5f0f\u7b97\u6cd5\u6765\u89e3\u51b3\u3002
"},{"location":"chapter_backtracking/n_queens_problem/","title":"13.4 \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\u56fe 13-15 \u6240\u793a\uff0c\u5f53 \\(n = 4\\) \u65f6\uff0c\u5171\u53ef\u4ee5\u627e\u5230\u4e24\u4e2a\u89e3\u3002\u4ece\u56de\u6eaf\u7b97\u6cd5\u7684\u89d2\u5ea6\u770b\uff0c\\(n \\times n\\) \u5927\u5c0f\u7684\u68cb\u76d8\u5171\u6709 \\(n^2\\) \u4e2a\u683c\u5b50\uff0c\u7ed9\u51fa\u4e86\u6240\u6709\u7684\u9009\u62e9 choices \u3002\u5728\u9010\u4e2a\u653e\u7f6e\u7687\u540e\u7684\u8fc7\u7a0b\u4e2d\uff0c\u68cb\u76d8\u72b6\u6001\u5728\u4e0d\u65ad\u5730\u53d8\u5316\uff0c\u6bcf\u4e2a\u65f6\u523b\u7684\u68cb\u76d8\u5c31\u662f\u72b6\u6001 state \u3002

\u56fe 13-15 \u00a0 4 \u7687\u540e\u95ee\u9898\u7684\u89e3

\u56fe 13-16 \u5c55\u793a\u4e86\u672c\u9898\u7684\u4e09\u4e2a\u7ea6\u675f\u6761\u4ef6\uff1a\u591a\u4e2a\u7687\u540e\u4e0d\u80fd\u5728\u540c\u4e00\u884c\u3001\u540c\u4e00\u5217\u3001\u540c\u4e00\u5bf9\u89d2\u7ebf\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5bf9\u89d2\u7ebf\u5206\u4e3a\u4e3b\u5bf9\u89d2\u7ebf \\ \u548c\u6b21\u5bf9\u89d2\u7ebf / \u4e24\u79cd\u3002

\u56fe 13-16 \u00a0 n \u7687\u540e\u95ee\u9898\u7684\u7ea6\u675f\u6761\u4ef6

"},{"location":"chapter_backtracking/n_queens_problem/#1","title":"1. \u00a0 \u9010\u884c\u653e\u7f6e\u7b56\u7565","text":"

\u7687\u540e\u7684\u6570\u91cf\u548c\u68cb\u76d8\u7684\u884c\u6570\u90fd\u4e3a \\(n\\) \uff0c\u56e0\u6b64\u6211\u4eec\u5bb9\u6613\u5f97\u5230\u4e00\u4e2a\u63a8\u8bba\uff1a\u68cb\u76d8\u6bcf\u884c\u90fd\u5141\u8bb8\u4e14\u53ea\u5141\u8bb8\u653e\u7f6e\u4e00\u4e2a\u7687\u540e\u3002

\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u53d6\u9010\u884c\u653e\u7f6e\u7b56\u7565\uff1a\u4ece\u7b2c\u4e00\u884c\u5f00\u59cb\uff0c\u5728\u6bcf\u884c\u653e\u7f6e\u4e00\u4e2a\u7687\u540e\uff0c\u76f4\u81f3\u6700\u540e\u4e00\u884c\u7ed3\u675f\u3002

\u5982\u56fe 13-17 \u6240\u793a\uff0c\u4e3a \\(4\\) \u7687\u540e\u95ee\u9898\u7684\u9010\u884c\u653e\u7f6e\u8fc7\u7a0b\u3002\u53d7\u753b\u5e45\u9650\u5236\uff0c\u56fe 13-17 \u4ec5\u5c55\u5f00\u4e86\u7b2c\u4e00\u884c\u7684\u5176\u4e2d\u4e00\u4e2a\u641c\u7d22\u5206\u652f\uff0c\u5e76\u4e14\u5c06\u4e0d\u6ee1\u8db3\u5217\u7ea6\u675f\u548c\u5bf9\u89d2\u7ebf\u7ea6\u675f\u7684\u65b9\u6848\u90fd\u8fdb\u884c\u4e86\u526a\u679d\u3002

\u56fe 13-17 \u00a0 \u9010\u884c\u653e\u7f6e\u7b56\u7565

\u672c\u8d28\u4e0a\u770b\uff0c\u9010\u884c\u653e\u7f6e\u7b56\u7565\u8d77\u5230\u4e86\u526a\u679d\u7684\u4f5c\u7528\uff0c\u5b83\u907f\u514d\u4e86\u540c\u4e00\u884c\u51fa\u73b0\u591a\u4e2a\u7687\u540e\u7684\u6240\u6709\u641c\u7d22\u5206\u652f\u3002

"},{"location":"chapter_backtracking/n_queens_problem/#2","title":"2. \u00a0 \u5217\u4e0e\u5bf9\u89d2\u7ebf\u526a\u679d","text":"

\u4e3a\u4e86\u6ee1\u8db3\u5217\u7ea6\u675f\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u5e03\u5c14\u578b\u6570\u7ec4 cols \u8bb0\u5f55\u6bcf\u4e00\u5217\u662f\u5426\u6709\u7687\u540e\u3002\u5728\u6bcf\u6b21\u51b3\u5b9a\u653e\u7f6e\u524d\uff0c\u6211\u4eec\u901a\u8fc7 cols \u5c06\u5df2\u6709\u7687\u540e\u7684\u5217\u8fdb\u884c\u526a\u679d\uff0c\u5e76\u5728\u56de\u6eaf\u4e2d\u52a8\u6001\u66f4\u65b0 cols \u7684\u72b6\u6001\u3002

\u90a3\u4e48\uff0c\u5982\u4f55\u5904\u7406\u5bf9\u89d2\u7ebf\u7ea6\u675f\u5462\uff1f\u8bbe\u68cb\u76d8\u4e2d\u67d0\u4e2a\u683c\u5b50\u7684\u884c\u5217\u7d22\u5f15\u4e3a \\((row, col)\\) \uff0c\u9009\u5b9a\u77e9\u9635\u4e2d\u7684\u67d0\u6761\u4e3b\u5bf9\u89d2\u7ebf\uff0c\u6211\u4eec\u53d1\u73b0\u8be5\u5bf9\u89d2\u7ebf\u4e0a\u6240\u6709\u683c\u5b50\u7684\u884c\u7d22\u5f15\u51cf\u5217\u7d22\u5f15\u90fd\u76f8\u7b49\uff0c\u5373\u5bf9\u89d2\u7ebf\u4e0a\u6240\u6709\u683c\u5b50\u7684 \\(row - col\\) \u4e3a\u6052\u5b9a\u503c\u3002

\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4e24\u4e2a\u683c\u5b50\u6ee1\u8db3 \\(row_1 - col_1 = row_2 - col_2\\) \uff0c\u5219\u5b83\u4eec\u4e00\u5b9a\u5904\u5728\u540c\u4e00\u6761\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u3002\u5229\u7528\u8be5\u89c4\u5f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u501f\u52a9\u56fe 13-18 \u6240\u793a\u7684\u6570\u7ec4 diag1 \uff0c\u8bb0\u5f55\u6bcf\u6761\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\u3002

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

\u56fe 13-18 \u00a0 \u5904\u7406\u5217\u7ea6\u675f\u548c\u5bf9\u89d2\u7ebf\u7ea6\u675f

"},{"location":"chapter_backtracking/n_queens_problem/#3","title":"3. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u8bf7\u6ce8\u610f\uff0c\\(n\\) \u7ef4\u65b9\u9635\u4e2d \\(row - col\\) \u7684\u8303\u56f4\u662f \\([-n + 1, n - 1]\\) \uff0c\\(row + col\\) \u7684\u8303\u56f4\u662f \\([0, 2n - 2]\\) \uff0c\u6240\u4ee5\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\u7684\u6570\u91cf\u90fd\u4e3a \\(2n - 1\\) \uff0c\u5373\u6570\u7ec4 diag1 \u548c diag2 \u7684\u957f\u5ea6\u90fd\u4e3a \\(2n - 1\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\u7687\u540e\nif not cols[col] and not diags1[diag1] and not 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.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\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\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.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\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\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.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();\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\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\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();\nfor (int i = 0; i < n; i++) {\nList<string> row = new();\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();\nBacktrack(0, n, state, res, cols, diags1, diags2);\nreturn res;\n}\n
n_queens.go
/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nfunc backtrack(row, n int, state *[][]string, res *[][][]string, cols, diags1, diags2 *[]bool) {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\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\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\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\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\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.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\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\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.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\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\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\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\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.dart
/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nvoid backtrack(\nint row,\nint n,\nList<List<String>> state,\nList<List<List<String>>> res,\nList<bool> cols,\nList<bool> diags1,\nList<bool> diags2,\n) {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (row == n) {\nList<List<String>> copyState = [];\nfor (List<String> sRow in state) {\ncopyState.add(List.from(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\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\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 + 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] = false;\ndiags1[diag1] = false;\ndiags2[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 = List.generate(n, (index) => List.filled(n, \"#\"));\nList<bool> cols = List.filled(n, false); // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nList<bool> diags1 = List.filled(2 * n - 1, false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nList<bool> diags2 = List.filled(2 * n - 1, false); // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nList<List<List<String>>> res = [];\nbacktrack(0, n, state, res, cols, diags1, diags2);\nreturn res;\n}\n
n_queens.rs
/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nfn backtrack(row: usize, n: usize, state: &mut Vec<Vec<String>>, res: &mut Vec<Vec<Vec<String>>>,\ncols: &mut [bool], diags1: &mut [bool], diags2: &mut [bool]) {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif row == n {\nlet mut copy_state: Vec<Vec<String>> = Vec::new();\nfor s_row in state.clone() {\ncopy_state.push(s_row);\n}\nres.push(copy_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 + n - 1 - col;\nlet diag2 = row + col;\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\u7687\u540e\nif !cols[col] && !diags1[diag1] && !diags2[diag2] {\n// \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\nstate.get_mut(row).unwrap()[col] = \"Q\".into();\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\nstate.get_mut(row).unwrap()[col] = \"#\".into();\n(cols[col], diags1[diag1], diags2[diag2]) = (false, false, false);\n}\n}\n}\n/* \u6c42\u89e3 N \u7687\u540e */\nfn n_queens(n: usize) -> Vec<Vec<Vec<String>>> {\n// \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\nlet mut state: Vec<Vec<String>> = Vec::new();\nfor _ in 0..n {\nlet mut row: Vec<String> = Vec::new();\nfor _ in 0..n {\nrow.push(\"#\".into());\n}\nstate.push(row);\n}\nlet mut cols = vec![false; n]; // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nlet mut diags1 = vec![false; 2 * n - 1]; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nlet mut diags2 = vec![false; 2 * n - 1]; // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nlet mut res: Vec<Vec<Vec<String>>> = Vec::new();\nbacktrack(0, n, &mut state, &mut res, &mut cols, &mut diags1, &mut diags2);\nres\n}\n
n_queens.c
/* \u653e\u7f6e\u7ed3\u679c */\nstruct result {\nchar ***data;\nint size;\n};\ntypedef struct result Result;\n/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nvoid backtrack(int row, int n, char state[MAX_N][MAX_N], Result *res, bool cols[MAX_N], bool diags1[2 * MAX_N - 1], bool diags2[2 * MAX_N - 1]) {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (row == n) {\nres->data[res->size] = (char **)malloc(sizeof(char *) * n);\nfor (int i = 0; i < n; ++i) {\nres->data[res->size][i] = (char *)malloc(sizeof(char) * (n + 1));\nstrcpy(res->data[res->size][i], state[i]);\n}\nres->size++;\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\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\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 */\nResult *nQueens(int n) {\nchar state[MAX_N][MAX_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\nfor (int i = 0; i < n; ++i) {\nfor (int j = 0; j < n; ++j) {\nstate[i][j] = '#';\n}\nstate[i][n] = '\\0';\n}\nbool cols[MAX_N] = {false};           // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nbool diags1[2 * MAX_N - 1] = {false}; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nbool diags2[2 * MAX_N - 1] = {false}; // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nResult *res = malloc(sizeof(Result));\nres->data = (char ***)malloc(sizeof(char **) * MAX_RES);\nres->size = 0;\nbacktrack(0, n, state, res, cols, diags1, diags2);\nreturn res;\n}\n
n_queens.zig
[class]{}-[func]{backtrack}\n[class]{}-[func]{nQueens}\n

\u9010\u884c\u653e\u7f6e \\(n\\) \u6b21\uff0c\u8003\u8651\u5217\u7ea6\u675f\uff0c\u5219\u4ece\u7b2c\u4e00\u884c\u5230\u6700\u540e\u4e00\u884c\u5206\u522b\u6709 \\(n\\)\u3001\\(n-1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \u4e2a\u9009\u62e9\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n!)\\) \u3002\u5b9e\u9645\u4e0a\uff0c\u6839\u636e\u5bf9\u89d2\u7ebf\u7ea6\u675f\u7684\u526a\u679d\u4e5f\u80fd\u591f\u5927\u5e45\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

\u6570\u7ec4 state \u4f7f\u7528 \\(O(n^2)\\) \u7a7a\u95f4\uff0c\u6570\u7ec4 cols\u3001diags1 \u548c diags2 \u7686\u4f7f\u7528 \\(O(n)\\) \u7a7a\u95f4\u3002\u6700\u5927\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002\u56e0\u6b64\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002

"},{"location":"chapter_backtracking/permutations_problem/","title":"13.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

\u8868 13-2 \u5217\u4e3e\u4e86\u51e0\u4e2a\u793a\u4f8b\u6570\u636e\uff0c\u5305\u62ec\u8f93\u5165\u6570\u7ec4\u548c\u5bf9\u5e94\u7684\u6240\u6709\u6392\u5217\u3002

\u8868 13-2 \u00a0 \u5168\u6392\u5217\u793a\u4f8b

\u8f93\u5165\u6570\u7ec4 \u6240\u6709\u6392\u5217 \\([1]\\) \\([1]\\) \\([1, 2]\\) \\([1, 2], [2, 1]\\) \\([1, 2, 3]\\) \\([1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]\\)"},{"location":"chapter_backtracking/permutations_problem/#1321","title":"13.2.1 \u00a0 \u65e0\u76f8\u7b49\u5143\u7d20\u7684\u60c5\u51b5","text":"

Question

\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\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\u4ee3\u7801\u7684\u89d2\u5ea6\u770b\uff0c\u5019\u9009\u96c6\u5408 choices \u662f\u8f93\u5165\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u72b6\u6001 state \u662f\u76f4\u81f3\u76ee\u524d\u5df2\u88ab\u9009\u62e9\u7684\u5143\u7d20\u3002\u8bf7\u6ce8\u610f\uff0c\u6bcf\u4e2a\u5143\u7d20\u53ea\u5141\u8bb8\u88ab\u9009\u62e9\u4e00\u6b21\uff0c\u56e0\u6b64 state \u4e2d\u7684\u6240\u6709\u5143\u7d20\u90fd\u5e94\u8be5\u662f\u552f\u4e00\u7684\u3002

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

\u56fe 13-5 \u00a0 \u5168\u6392\u5217\u7684\u9012\u5f52\u6811

"},{"location":"chapter_backtracking/permutations_problem/#1","title":"1. \u00a0 \u91cd\u590d\u9009\u62e9\u526a\u679d","text":"

\u4e3a\u4e86\u5b9e\u73b0\u6bcf\u4e2a\u5143\u7d20\u53ea\u88ab\u9009\u62e9\u4e00\u6b21\uff0c\u6211\u4eec\u8003\u8651\u5f15\u5165\u4e00\u4e2a\u5e03\u5c14\u578b\u6570\u7ec4 selected \uff0c\u5176\u4e2d selected[i] \u8868\u793a choices[i] \u662f\u5426\u5df2\u88ab\u9009\u62e9\uff0c\u5e76\u57fa\u4e8e\u5b83\u5b9e\u73b0\u4ee5\u4e0b\u526a\u679d\u64cd\u4f5c\u3002

  • \u5728\u505a\u51fa\u9009\u62e9 choice[i] \u540e\uff0c\u6211\u4eec\u5c31\u5c06 selected[i] \u8d4b\u503c\u4e3a \\(\\text{True}\\) \uff0c\u4ee3\u8868\u5b83\u5df2\u88ab\u9009\u62e9\u3002
  • \u904d\u5386\u9009\u62e9\u5217\u8868 choices \u65f6\uff0c\u8df3\u8fc7\u6240\u6709\u5df2\u88ab\u9009\u62e9\u8fc7\u7684\u8282\u70b9\uff0c\u5373\u526a\u679d\u3002

\u5982\u56fe 13-6 \u6240\u793a\uff0c\u5047\u8bbe\u6211\u4eec\u7b2c\u4e00\u8f6e\u9009\u62e9 1 \uff0c\u7b2c\u4e8c\u8f6e\u9009\u62e9 3 \uff0c\u7b2c\u4e09\u8f6e\u9009\u62e9 2 \uff0c\u5219\u9700\u8981\u5728\u7b2c\u4e8c\u8f6e\u526a\u6389\u5143\u7d20 1 \u7684\u5206\u652f\uff0c\u5728\u7b2c\u4e09\u8f6e\u526a\u6389\u5143\u7d20 1 \u548c\u5143\u7d20 3 \u7684\u5206\u652f\u3002

\u56fe 13-6 \u00a0 \u5168\u6392\u5217\u526a\u679d\u793a\u4f8b

\u89c2\u5bdf\u56fe 13-6 \u53d1\u73b0\uff0c\u8be5\u526a\u679d\u64cd\u4f5c\u5c06\u641c\u7d22\u7a7a\u95f4\u5927\u5c0f\u4ece \\(O(n^n)\\) \u964d\u4f4e\u81f3 \\(O(n!)\\) \u3002

"},{"location":"chapter_backtracking/permutations_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u60f3\u6e05\u695a\u4ee5\u4e0a\u4fe1\u606f\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u6846\u67b6\u4ee3\u7801\u4e2d\u505a\u201c\u5b8c\u5f62\u586b\u7a7a\u201d\u4e86\u3002\u4e3a\u4e86\u7f29\u77ed\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

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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();\nBacktrack(new List<int>(), nums, new bool[nums.Length], res);\nreturn res;\n}\n
permutations_i.go
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunc backtrackI(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\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.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.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.dart
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(\nList<int> state,\nList<int> choices,\nList<bool> selected,\nList<List<int>> res,\n) {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (state.length == choices.length) {\nres.add(List.from(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.removeLast();\n}\n}\n}\n/* \u5168\u6392\u5217 I */\nList<List<int>> permutationsI(List<int> nums) {\nList<List<int>> res = [];\nbacktrack([], nums, List.filled(nums.length, false), res);\nreturn res;\n}\n
permutations_i.rs
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfn backtrack(mut state: Vec<i32>, choices: &[i32], selected: &mut [bool], res: &mut Vec<Vec<i32>>) {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif state.len() == choices.len() {\nres.push(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor i in 0..choices.len() {\nlet 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(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state.clone(), 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.len() - 1);\n}\n}\n}\n/* \u5168\u6392\u5217 I */\nfn permutations_i(nums: &mut [i32]) -> Vec<Vec<i32>> {\nlet mut res = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nbacktrack(Vec::new(), nums, &mut vec![false; nums.len()], &mut res);\nres\n}\n
permutations_i.c
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(vector *state, vector *choices, vector *selected, vector *res) {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (state->size == choices->size) {\nvector *newState = newVector();\nfor (int i = 0; i < state->size; i++) {\nvectorPushback(newState, state->data[i], sizeof(int));\n}\nvectorPushback(res, newState, sizeof(vector));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i < choices->size; i++) {\nint *choice = malloc(sizeof(int));\n*choice = *((int *)(choices->data[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\nbool select = *((bool *)(selected->data[i]));\nif (!select) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n*((bool *)selected->data[i]) = true;\nvectorPushback(state, choice, sizeof(int));\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\n*((bool *)selected->data[i]) = false;\nvectorPopback(state);\n}\n}\n}\n/* \u5168\u6392\u5217 I */\nvector *permutationsI(vector *nums) {\nvector *iState = newVector();\nint select[3] = {false, false, false};\nvector *bSelected = newVector();\nfor (int i = 0; i < nums->size; i++) {\nvectorPushback(bSelected, &select[i], sizeof(int));\n}\nvector *res = newVector();\n// \u524d\u5e8f\u904d\u5386\nbacktrack(iState, nums, bSelected, res);\nreturn res;\n}\n
permutations_i.zig
[class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsI}\n
"},{"location":"chapter_backtracking/permutations_problem/#1322","title":"13.2.2 \u00a0 \u8003\u8651\u76f8\u7b49\u5143\u7d20\u7684\u60c5\u51b5","text":"

Question

\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u8fd4\u56de\u6240\u6709\u4e0d\u91cd\u590d\u7684\u6392\u5217\u3002

\u5047\u8bbe\u8f93\u5165\u6570\u7ec4\u4e3a \\([1, 1, 2]\\) \u3002\u4e3a\u4e86\u65b9\u4fbf\u533a\u5206\u4e24\u4e2a\u91cd\u590d\u5143\u7d20 \\(1\\) \uff0c\u6211\u4eec\u5c06\u7b2c\u4e8c\u4e2a \\(1\\) \u8bb0\u4e3a \\(\\hat{1}\\) \u3002

\u5982\u56fe 13-7 \u6240\u793a\uff0c\u4e0a\u8ff0\u65b9\u6cd5\u751f\u6210\u7684\u6392\u5217\u6709\u4e00\u534a\u90fd\u662f\u91cd\u590d\u7684\u3002

\u56fe 13-7 \u00a0 \u91cd\u590d\u6392\u5217

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

"},{"location":"chapter_backtracking/permutations_problem/#1_1","title":"1. \u00a0 \u76f8\u7b49\u5143\u7d20\u526a\u679d","text":"

\u89c2\u5bdf\u56fe 13-8 \uff0c\u5728\u7b2c\u4e00\u8f6e\u4e2d\uff0c\u9009\u62e9 \\(1\\) \u6216\u9009\u62e9 \\(\\hat{1}\\) \u662f\u7b49\u4ef7\u7684\uff0c\u5728\u8fd9\u4e24\u4e2a\u9009\u62e9\u4e4b\u4e0b\u751f\u6210\u7684\u6240\u6709\u6392\u5217\u90fd\u662f\u91cd\u590d\u7684\u3002\u56e0\u6b64\u5e94\u8be5\u628a \\(\\hat{1}\\) \u526a\u679d\u6389\u3002

\u540c\u7406\uff0c\u5728\u7b2c\u4e00\u8f6e\u9009\u62e9 \\(2\\) \u4e4b\u540e\uff0c\u7b2c\u4e8c\u8f6e\u9009\u62e9\u4e2d\u7684 \\(1\\) \u548c \\(\\hat{1}\\) \u4e5f\u4f1a\u4ea7\u751f\u91cd\u590d\u5206\u652f\uff0c\u56e0\u6b64\u4e5f\u5e94\u5c06\u7b2c\u4e8c\u8f6e\u7684 \\(\\hat{1}\\) \u526a\u679d\u3002

\u672c\u8d28\u4e0a\u770b\uff0c\u6211\u4eec\u7684\u76ee\u6807\u662f\u5728\u67d0\u4e00\u8f6e\u9009\u62e9\u4e2d\uff0c\u4fdd\u8bc1\u591a\u4e2a\u76f8\u7b49\u7684\u5143\u7d20\u4ec5\u88ab\u9009\u62e9\u4e00\u6b21\u3002

\u56fe 13-8 \u00a0 \u91cd\u590d\u6392\u5217\u526a\u679d

"},{"location":"chapter_backtracking/permutations_problem/#2_1","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u5728\u4e0a\u4e00\u9898\u7684\u4ee3\u7801\u7684\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u8003\u8651\u5728\u6bcf\u4e00\u8f6e\u9009\u62e9\u4e2d\u5f00\u542f\u4e00\u4e2a\u54c8\u5e0c\u8868 duplicated \uff0c\u7528\u4e8e\u8bb0\u5f55\u8be5\u8f6e\u4e2d\u5df2\u7ecf\u5c1d\u8bd5\u8fc7\u7684\u5143\u7d20\uff0c\u5e76\u5c06\u91cd\u590d\u5143\u7d20\u526a\u679d\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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();\nBacktrack(new List<int>(), nums, new bool[nums.Length], res);\nreturn res;\n}\n
permutations_ii.go
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunc backtrackII(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\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.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.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.dart
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid backtrack(\nList<int> state,\nList<int> choices,\nList<bool> selected,\nList<List<int>> res,\n) {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (state.length == choices.length) {\nres.add(List.from(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nSet<int> duplicated = {};\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.removeLast();\n}\n}\n}\n/* \u5168\u6392\u5217 II */\nList<List<int>> permutationsII(List<int> nums) {\nList<List<int>> res = [];\nbacktrack([], nums, List.filled(nums.length, false), res);\nreturn res;\n}\n
permutations_ii.rs
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfn backtrack(mut state: Vec<i32>, choices: &[i32], selected: &mut [bool], res: &mut Vec<Vec<i32>>) {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif state.len() == choices.len() {\nres.push(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nlet mut duplicated = HashSet::<i32>::new();\nfor i in 0..choices.len() {\nlet 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.insert(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.clone(), 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.len() - 1);\n}\n}\n}\n/* \u5168\u6392\u5217 II */\nfn permutations_ii(nums: &mut [i32]) -> Vec<Vec<i32>> {\nlet mut res = Vec::new();\nbacktrack(Vec::new(), nums, &mut vec![false; nums.len()], &mut res);\nres\n}\n
permutations_ii.c
[class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsII}\n
permutations_ii.zig
[class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsII}\n

\u5047\u8bbe\u5143\u7d20\u4e24\u4e24\u4e4b\u95f4\u4e92\u4e0d\u76f8\u540c\uff0c\u5219 \\(n\\) \u4e2a\u5143\u7d20\u5171\u6709 \\(n!\\) \u79cd\u6392\u5217\uff08\u9636\u4e58\uff09\uff1b\u5728\u8bb0\u5f55\u7ed3\u679c\u65f6\uff0c\u9700\u8981\u590d\u5236\u957f\u5ea6\u4e3a \\(n\\) \u7684\u5217\u8868\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n!n)\\) \u3002

\u6700\u5927\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002selected \u4f7f\u7528 \\(O(n)\\) \u7a7a\u95f4\u3002\u540c\u4e00\u65f6\u523b\u6700\u591a\u5171\u6709 \\(n\\) \u4e2a duplicated \uff0c\u4f7f\u7528 \\(O(n^2)\\) \u7a7a\u95f4\u3002\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002

"},{"location":"chapter_backtracking/permutations_problem/#3","title":"3. \u00a0 \u4e24\u79cd\u526a\u679d\u5bf9\u6bd4","text":"

\u8bf7\u6ce8\u610f\uff0c\u867d\u7136 selected \u548c duplicated \u90fd\u7528\u4f5c\u526a\u679d\uff0c\u4f46\u4e24\u8005\u7684\u76ee\u6807\u662f\u4e0d\u540c\u7684\u3002

  • \u91cd\u590d\u9009\u62e9\u526a\u679d\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
  • \u76f8\u7b49\u5143\u7d20\u526a\u679d\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\u3002

\u56fe 13-9 \u5c55\u793a\u4e86\u4e24\u4e2a\u526a\u679d\u6761\u4ef6\u7684\u751f\u6548\u8303\u56f4\u3002\u6ce8\u610f\uff0c\u6811\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u4ee3\u8868\u4e00\u4e2a\u9009\u62e9\uff0c\u4ece\u6839\u8282\u70b9\u5230\u53f6\u8282\u70b9\u7684\u8def\u5f84\u4e0a\u7684\u5404\u4e2a\u8282\u70b9\u6784\u6210\u4e00\u4e2a\u6392\u5217\u3002

\u56fe 13-9 \u00a0 \u4e24\u79cd\u526a\u679d\u6761\u4ef6\u7684\u4f5c\u7528\u8303\u56f4

"},{"location":"chapter_backtracking/subset_sum_problem/","title":"13.3 \u00a0 \u5b50\u96c6\u548c\u95ee\u9898","text":""},{"location":"chapter_backtracking/subset_sum_problem/#1331","title":"13.3.1 \u00a0 \u65e0\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"

Question

\u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570\u6570\u7ec4 nums \u548c\u4e00\u4e2a\u76ee\u6807\u6b63\u6574\u6570 target \uff0c\u8bf7\u627e\u51fa\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\uff0c\u4f7f\u5f97\u7ec4\u5408\u4e2d\u7684\u5143\u7d20\u548c\u7b49\u4e8e target \u3002\u7ed9\u5b9a\u6570\u7ec4\u65e0\u91cd\u590d\u5143\u7d20\uff0c\u6bcf\u4e2a\u5143\u7d20\u53ef\u4ee5\u88ab\u9009\u53d6\u591a\u6b21\u3002\u8bf7\u4ee5\u5217\u8868\u5f62\u5f0f\u8fd4\u56de\u8fd9\u4e9b\u7ec4\u5408\uff0c\u5217\u8868\u4e2d\u4e0d\u5e94\u5305\u542b\u91cd\u590d\u7ec4\u5408\u3002

\u4f8b\u5982\uff0c\u8f93\u5165\u96c6\u5408 \\(\\{3, 4, 5\\}\\) \u548c\u76ee\u6807\u6574\u6570 \\(9\\) \uff0c\u89e3\u4e3a \\(\\{3, 3, 3\\}, \\{4, 5\\}\\) \u3002\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u4e24\u70b9\u3002

  • \u8f93\u5165\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u88ab\u65e0\u9650\u6b21\u91cd\u590d\u9009\u53d6\u3002
  • \u5b50\u96c6\u662f\u4e0d\u533a\u5206\u5143\u7d20\u987a\u5e8f\u7684\uff0c\u6bd4\u5982 \\(\\{4, 5\\}\\) \u548c \\(\\{5, 4\\}\\) \u662f\u540c\u4e00\u4e2a\u5b50\u96c6\u3002
"},{"location":"chapter_backtracking/subset_sum_problem/#1","title":"1. \u00a0 \u53c2\u8003\u5168\u6392\u5217\u89e3\u6cd5","text":"

\u7c7b\u4f3c\u4e8e\u5168\u6392\u5217\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u5b50\u96c6\u7684\u751f\u6210\u8fc7\u7a0b\u60f3\u8c61\u6210\u4e00\u7cfb\u5217\u9009\u62e9\u7684\u7ed3\u679c\uff0c\u5e76\u5728\u9009\u62e9\u8fc7\u7a0b\u4e2d\u5b9e\u65f6\u66f4\u65b0\u201c\u5143\u7d20\u548c\u201d\uff0c\u5f53\u5143\u7d20\u548c\u7b49\u4e8e target \u65f6\uff0c\u5c31\u5c06\u5b50\u96c6\u8bb0\u5f55\u81f3\u7ed3\u679c\u5217\u8868\u3002

\u800c\u4e0e\u5168\u6392\u5217\u95ee\u9898\u4e0d\u540c\u7684\u662f\uff0c\u672c\u9898\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u88ab\u65e0\u9650\u6b21\u9009\u53d6\uff0c\u56e0\u6b64\u65e0\u987b\u501f\u52a9 selected \u5e03\u5c14\u5217\u8868\u6765\u8bb0\u5f55\u5143\u7d20\u662f\u5426\u5df2\u88ab\u9009\u62e9\u3002\u6211\u4eec\u53ef\u4ee5\u5bf9\u5168\u6392\u5217\u4ee3\u7801\u8fdb\u884c\u5c0f\u5e45\u4fee\u6539\uff0c\u521d\u6b65\u5f97\u5230\u89e3\u9898\u4ee3\u7801\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig subset_sum_i_naive.py
def backtrack(\nstate: list[int],\ntarget: int,\ntotal: int,\nchoices: list[int],\nres: list[list[int]],\n):\n\"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I\"\"\"\n# \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif total == target:\nres.append(list(state))\nreturn\n# \u904d\u5386\u6240\u6709\u9009\u62e9\nfor i in range(len(choices)):\n# \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif total + choices[i] > target:\ncontinue\n# \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.append(choices[i])\n# \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target, total + choices[i], choices, res)\n# \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop()\ndef subset_sum_i_naive(nums: list[int], target: int) -> list[list[int]]:\n\"\"\"\u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09\"\"\"\nstate = []  # \u72b6\u6001\uff08\u5b50\u96c6\uff09\ntotal = 0  # \u5b50\u96c6\u548c\nres = []  # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, total, nums, res)\nreturn res\n
subset_sum_i_naive.cpp
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(vector<int> &state, int target, int total, vector<int> &choices, vector<vector<int>> &res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (total == target) {\nres.push_back(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (size_t i = 0; i < choices.size(); i++) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif (total + choices[i] > target) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.push_back(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target, total + choices[i], choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop_back();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nvector<vector<int>> subsetSumINaive(vector<int> &nums, int target) {\nvector<int> state;       // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nint total = 0;           // \u5b50\u96c6\u548c\nvector<vector<int>> res; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, total, nums, res);\nreturn res;\n}\n
subset_sum_i_naive.java
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(List<Integer> state, int target, int total, int[] choices, List<List<Integer>> res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (total == target) {\nres.add(new ArrayList<>(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i < choices.length; i++) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif (total + choices[i] > target) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target, total + choices[i], choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.remove(state.size() - 1);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nList<List<Integer>> subsetSumINaive(int[] nums, int target) {\nList<Integer> state = new ArrayList<>(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nint total = 0; // \u5b50\u96c6\u548c\nList<List<Integer>> res = new ArrayList<>(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, total, nums, res);\nreturn res;\n}\n
subset_sum_i_naive.cs
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid Backtrack(List<int> state, int target, int total, int[] choices, List<List<int>> res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (total == target) {\nres.Add(new List<int>(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i < choices.Length; i++) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif (total + choices[i] > target) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.Add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nBacktrack(state, target, total + choices[i], choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.RemoveAt(state.Count - 1);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nList<List<int>> SubsetSumINaive(int[] nums, int target) {\nList<int> state = new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nint total = 0; // \u5b50\u96c6\u548c\nList<List<int>> res = new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nBacktrack(state, target, total, nums, res);\nreturn res;\n}\n
subset_sum_i_naive.go
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrackSubsetSumINaive(total, target int, state, choices *[]int, res *[][]int) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == total {\nnewState := append([]int{}, *state...)\n*res = append(*res, newState)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor i := 0; i < len(*choices); i++ {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif total+(*choices)[i] > target {\ncontinue\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n*state = append(*state, (*choices)[i])\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrackSubsetSumINaive(total+(*choices)[i], target, state, choices, res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n*state = (*state)[:len(*state)-1]\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunc subsetSumINaive(nums []int, target int) [][]int {\nstate := make([]int, 0) // \u72b6\u6001\uff08\u5b50\u96c6\uff09\ntotal := 0              // \u5b50\u96c6\u548c\nres := make([][]int, 0) // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrackSubsetSumINaive(total, target, &state, &nums, &res)\nreturn res\n}\n
subset_sum_i_naive.swift
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrack(state: inout [Int], target: Int, total: Int, choices: [Int], res: inout [[Int]]) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif total == target {\nres.append(state)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor i in stride(from: 0, to: choices.count, by: 1) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif total + choices[i] > target {\ncontinue\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.append(choices[i])\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state: &state, target: target, total: total + choices[i], choices: choices, res: &res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.removeLast()\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunc subsetSumINaive(nums: [Int], target: Int) -> [[Int]] {\nvar state: [Int] = [] // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nlet total = 0 // \u5b50\u96c6\u548c\nvar res: [[Int]] = [] // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state: &state, target: target, total: total, choices: nums, res: &res)\nreturn res\n}\n
subset_sum_i_naive.js
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(state, target, total, choices, res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (total === target) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (let i = 0; i < choices.length; i++) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif (total + choices[i] > target) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target, total + choices[i], choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunction subsetSumINaive(nums, target) {\nconst state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nconst total = 0; // \u5b50\u96c6\u548c\nconst res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, total, nums, res);\nreturn res;\n}\n
subset_sum_i_naive.ts
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(\nstate: number[],\ntarget: number,\ntotal: number,\nchoices: number[],\nres: number[][]\n): void {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (total === target) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (let i = 0; i < choices.length; i++) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif (total + choices[i] > target) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target, total + choices[i], choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunction subsetSumINaive(nums: number[], target: number): number[][] {\nconst state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nconst total = 0; // \u5b50\u96c6\u548c\nconst res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, total, nums, res);\nreturn res;\n}\n
subset_sum_i_naive.dart
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(\nList<int> state,\nint target,\nint total,\nList<int> choices,\nList<List<int>> res,\n) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (total == target) {\nres.add(List.from(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i < choices.length; i++) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif (total + choices[i] > target) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target, total + choices[i], choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.removeLast();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nList<List<int>> subsetSumINaive(List<int> nums, int target) {\nList<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nint total = 0; // \u5143\u7d20\u548c\nList<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, total, nums, res);\nreturn res;\n}\n
subset_sum_i_naive.rs
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfn backtrack(mut state: Vec<i32>, target: i32, total: i32, choices: &[i32], res: &mut Vec<Vec<i32>>) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif total == target {\nres.push(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor i in 0..choices.len() {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif total + choices[i] > target {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state.clone(), target, total + choices[i], choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfn subset_sum_i_naive(nums: &[i32], target: i32) -> Vec<Vec<i32>> {\nlet state = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nlet total = 0; // \u5b50\u96c6\u548c\nlet mut res = Vec::new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, total, nums, &mut res);\nres\n}\n
subset_sum_i_naive.c
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(vector *state, int target, int total, vector *choices, vector *res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (total == target) {\nvector *tmpVector = newVector();\nfor (int i = 0; i < state->size; i++) {\nvectorPushback(tmpVector, state->data[i], sizeof(int));\n}\nvectorPushback(res, tmpVector, sizeof(vector));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (size_t i = 0; i < choices->size; i++) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif (total + *(int *)(choices->data[i]) > target) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nvectorPushback(state, choices->data[i], sizeof(int));\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target, total + *(int *)(choices->data[i]), choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nvectorPopback(state);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nvector *subsetSumINaive(vector *nums, int target) {\nvector *state = newVector(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nint total = 0;               // \u5b50\u96c6\u548c\nvector *res = newVector();   // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, total, nums, res);\nreturn res;\n}\n
subset_sum_i_naive.zig
[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumINaive}\n

\u5411\u4ee5\u4e0a\u4ee3\u7801\u8f93\u5165\u6570\u7ec4 \\([3, 4, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a \\([3, 3, 3], [4, 5], [5, 4]\\) \u3002\u867d\u7136\u6210\u529f\u627e\u51fa\u4e86\u6240\u6709\u548c\u4e3a \\(9\\) \u7684\u5b50\u96c6\uff0c\u4f46\u5176\u4e2d\u5b58\u5728\u91cd\u590d\u7684\u5b50\u96c6 \\([4, 5]\\) \u548c \\([5, 4]\\) \u3002

\u8fd9\u662f\u56e0\u4e3a\u641c\u7d22\u8fc7\u7a0b\u662f\u533a\u5206\u9009\u62e9\u987a\u5e8f\u7684\uff0c\u7136\u800c\u5b50\u96c6\u4e0d\u533a\u5206\u9009\u62e9\u987a\u5e8f\u3002\u5982\u56fe 13-10 \u6240\u793a\uff0c\u5148\u9009 \\(4\\) \u540e\u9009 \\(5\\) \u4e0e\u5148\u9009 \\(5\\) \u540e\u9009 \\(4\\) \u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u5206\u652f\uff0c\u4f46\u4e24\u8005\u5bf9\u5e94\u540c\u4e00\u4e2a\u5b50\u96c6\u3002

\u56fe 13-10 \u00a0 \u5b50\u96c6\u641c\u7d22\u4e0e\u8d8a\u754c\u526a\u679d

\u4e3a\u4e86\u53bb\u9664\u91cd\u590d\u5b50\u96c6\uff0c\u4e00\u79cd\u76f4\u63a5\u7684\u601d\u8def\u662f\u5bf9\u7ed3\u679c\u5217\u8868\u8fdb\u884c\u53bb\u91cd\u3002\u4f46\u8fd9\u4e2a\u65b9\u6cd5\u6548\u7387\u5f88\u4f4e\uff0c\u6709\u4e24\u65b9\u9762\u539f\u56e0\u3002

  • \u5f53\u6570\u7ec4\u5143\u7d20\u8f83\u591a\uff0c\u5c24\u5176\u662f\u5f53 target \u8f83\u5927\u65f6\uff0c\u641c\u7d22\u8fc7\u7a0b\u4f1a\u4ea7\u751f\u5927\u91cf\u7684\u91cd\u590d\u5b50\u96c6\u3002
  • \u6bd4\u8f83\u5b50\u96c6\uff08\u6570\u7ec4\uff09\u7684\u5f02\u540c\u975e\u5e38\u8017\u65f6\uff0c\u9700\u8981\u5148\u6392\u5e8f\u6570\u7ec4\uff0c\u518d\u6bd4\u8f83\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u5f02\u540c\u3002
"},{"location":"chapter_backtracking/subset_sum_problem/#2","title":"2. \u00a0 \u91cd\u590d\u5b50\u96c6\u526a\u679d","text":"

\u6211\u4eec\u8003\u8651\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u901a\u8fc7\u526a\u679d\u8fdb\u884c\u53bb\u91cd\u3002\u89c2\u5bdf\u56fe 13-11 \uff0c\u91cd\u590d\u5b50\u96c6\u662f\u5728\u4ee5\u4e0d\u540c\u987a\u5e8f\u9009\u62e9\u6570\u7ec4\u5143\u7d20\u65f6\u4ea7\u751f\u7684\uff0c\u4f8b\u5982\u4ee5\u4e0b\u60c5\u51b5\u3002

  1. \u5f53\u7b2c\u4e00\u8f6e\u548c\u7b2c\u4e8c\u8f6e\u5206\u522b\u9009\u62e9 \\(3\\) \u548c \\(4\\) \u65f6\uff0c\u4f1a\u751f\u6210\u5305\u542b\u8fd9\u4e24\u4e2a\u5143\u7d20\u7684\u6240\u6709\u5b50\u96c6\uff0c\u8bb0\u4e3a \\([3, 4, \\dots]\\) \u3002
  2. \u4e4b\u540e\uff0c\u5f53\u7b2c\u4e00\u8f6e\u9009\u62e9 \\(4\\) \u65f6\uff0c\u5219\u7b2c\u4e8c\u8f6e\u5e94\u8be5\u8df3\u8fc7 \\(3\\) \uff0c\u56e0\u4e3a\u8be5\u9009\u62e9\u4ea7\u751f\u7684\u5b50\u96c6 \\([4, 3, \\dots]\\) \u548c 1. \u4e2d\u751f\u6210\u7684\u5b50\u96c6\u5b8c\u5168\u91cd\u590d\u3002

\u5728\u641c\u7d22\u4e2d\uff0c\u6bcf\u4e00\u5c42\u7684\u9009\u62e9\u90fd\u662f\u4ece\u5de6\u5230\u53f3\u88ab\u9010\u4e2a\u5c1d\u8bd5\u7684\uff0c\u56e0\u6b64\u8d8a\u9760\u53f3\u7684\u5206\u652f\u88ab\u526a\u6389\u7684\u8d8a\u591a\u3002

  1. \u524d\u4e24\u8f6e\u9009\u62e9 \\(3\\) \u548c \\(5\\) \uff0c\u751f\u6210\u5b50\u96c6 \\([3, 5, \\dots]\\) \u3002
  2. \u524d\u4e24\u8f6e\u9009\u62e9 \\(4\\) \u548c \\(5\\) \uff0c\u751f\u6210\u5b50\u96c6 \\([4, 5, \\dots]\\) \u3002
  3. \u82e5\u7b2c\u4e00\u8f6e\u9009\u62e9 \\(5\\) \uff0c\u5219\u7b2c\u4e8c\u8f6e\u5e94\u8be5\u8df3\u8fc7 \\(3\\) \u548c \\(4\\) \uff0c\u56e0\u4e3a\u5b50\u96c6 \\([5, 3, \\dots]\\) \u548c \\([5, 4, \\dots]\\) \u4e0e\u7b2c 1. \u548c 2. \u6b65\u4e2d\u63cf\u8ff0\u7684\u5b50\u96c6\u5b8c\u5168\u91cd\u590d\u3002

\u56fe 13-11 \u00a0 \u4e0d\u540c\u9009\u62e9\u987a\u5e8f\u5bfc\u81f4\u7684\u91cd\u590d\u5b50\u96c6

\u603b\u7ed3\u6765\u770b\uff0c\u7ed9\u5b9a\u8f93\u5165\u6570\u7ec4 \\([x_1, x_2, \\dots, x_n]\\) \uff0c\u8bbe\u641c\u7d22\u8fc7\u7a0b\u4e2d\u7684\u9009\u62e9\u5e8f\u5217\u4e3a \\([x_{i_1}, x_{i_2}, \\dots, x_{i_m}]\\) \uff0c\u5219\u8be5\u9009\u62e9\u5e8f\u5217\u9700\u8981\u6ee1\u8db3 \\(i_1 \\leq i_2 \\leq \\dots \\leq i_m\\) \uff0c\u4e0d\u6ee1\u8db3\u8be5\u6761\u4ef6\u7684\u9009\u62e9\u5e8f\u5217\u90fd\u4f1a\u9020\u6210\u91cd\u590d\uff0c\u5e94\u5f53\u526a\u679d\u3002

"},{"location":"chapter_backtracking/subset_sum_problem/#3","title":"3. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u4e3a\u5b9e\u73b0\u8be5\u526a\u679d\uff0c\u6211\u4eec\u521d\u59cb\u5316\u53d8\u91cf start \uff0c\u7528\u4e8e\u6307\u793a\u904d\u5386\u8d77\u70b9\u3002\u5f53\u505a\u51fa\u9009\u62e9 \\(x_{i}\\) \u540e\uff0c\u8bbe\u5b9a\u4e0b\u4e00\u8f6e\u4ece\u7d22\u5f15 \\(i\\) \u5f00\u59cb\u904d\u5386\u3002\u8fd9\u6837\u505a\u5c31\u53ef\u4ee5\u8ba9\u9009\u62e9\u5e8f\u5217\u6ee1\u8db3 \\(i_1 \\leq i_2 \\leq \\dots \\leq i_m\\) \uff0c\u4ece\u800c\u4fdd\u8bc1\u5b50\u96c6\u552f\u4e00\u3002

\u9664\u6b64\u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u5bf9\u4ee3\u7801\u8fdb\u884c\u4e86\u4ee5\u4e0b\u4e24\u9879\u4f18\u5316\u3002

  • \u5728\u5f00\u542f\u641c\u7d22\u524d\uff0c\u5148\u5c06\u6570\u7ec4 nums \u6392\u5e8f\u3002\u5728\u904d\u5386\u6240\u6709\u9009\u62e9\u65f6\uff0c\u5f53\u5b50\u96c6\u548c\u8d85\u8fc7 target \u65f6\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\uff0c\u56e0\u4e3a\u540e\u8fb9\u7684\u5143\u7d20\u66f4\u5927\uff0c\u5176\u5b50\u96c6\u548c\u90fd\u4e00\u5b9a\u4f1a\u8d85\u8fc7 target \u3002
  • \u7701\u53bb\u5143\u7d20\u548c\u53d8\u91cf total \uff0c\u901a\u8fc7\u5728 target \u4e0a\u6267\u884c\u51cf\u6cd5\u6765\u7edf\u8ba1\u5143\u7d20\u548c\uff0c\u5f53 target \u7b49\u4e8e \\(0\\) \u65f6\u8bb0\u5f55\u89e3\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig subset_sum_i.py
def backtrack(\nstate: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n\"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I\"\"\"\n# \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == 0:\nres.append(list(state))\nreturn\n# \u904d\u5386\u6240\u6709\u9009\u62e9\n# \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor i in range(start, len(choices)):\n# \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n# \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif target - choices[i] < 0:\nbreak\n# \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.append(choices[i])\n# \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i, res)\n# \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop()\ndef subset_sum_i(nums: list[int], target: int) -> list[list[int]]:\n\"\"\"\u6c42\u89e3\u5b50\u96c6\u548c I\"\"\"\nstate = []  # \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort()  # \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nstart = 0  # \u904d\u5386\u8d77\u59cb\u70b9\nres = []  # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res)\nreturn res\n
subset_sum_i.cpp
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(vector<int> &state, int target, vector<int> &choices, int start, vector<vector<int>> &res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nres.push_back(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor (int i = start; i < choices.size(); i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] < 0) {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.push_back(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop_back();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nvector<vector<int>> subsetSumI(vector<int> &nums, int target) {\nvector<int> state;              // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nsort(nums.begin(), nums.end()); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0;                  // \u904d\u5386\u8d77\u59cb\u70b9\nvector<vector<int>> res;        // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_i.java
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nres.add(new ArrayList<>(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor (int i = start; i < choices.length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] < 0) {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.remove(state.size() - 1);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nList<List<Integer>> subsetSumI(int[] nums, int target) {\nList<Integer> state = new ArrayList<>(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nArrays.sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nList<List<Integer>> res = new ArrayList<>(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_i.cs
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nres.Add(new List<int>(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor (int i = start; i < choices.Length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] < 0) {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.Add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nBacktrack(state, target - choices[i], choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.RemoveAt(state.Count - 1);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nList<List<int>> SubsetSumI(int[] nums, int target) {\nList<int> state = new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nArray.Sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nList<List<int>> res = new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nBacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_i.go
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrackSubsetSumI(start, target int, state, choices *[]int, res *[][]int) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == 0 {\nnewState := append([]int{}, *state...)\n*res = append(*res, newState)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor i := start; i < len(*choices); i++ {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif target-(*choices)[i] < 0 {\nbreak\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n*state = append(*state, (*choices)[i])\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrackSubsetSumI(i, target-(*choices)[i], state, choices, res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n*state = (*state)[:len(*state)-1]\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunc subsetSumI(nums []int, target int) [][]int {\nstate := make([]int, 0) // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nsort.Ints(nums)         // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nstart := 0              // \u904d\u5386\u8d77\u59cb\u70b9\nres := make([][]int, 0) // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrackSubsetSumI(start, target, &state, &nums, &res)\nreturn res\n}\n
subset_sum_i.swift
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == 0 {\nres.append(state)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor i in stride(from: start, to: choices.count, by: 1) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif target - choices[i] < 0 {\nbreak\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.append(choices[i])\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state: &state, target: target - choices[i], choices: choices, start: i, res: &res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.removeLast()\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunc subsetSumI(nums: [Int], target: Int) -> [[Int]] {\nvar state: [Int] = [] // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nlet nums = nums.sorted() // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nlet start = 0 // \u904d\u5386\u8d77\u59cb\u70b9\nvar res: [[Int]] = [] // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state: &state, target: target, choices: nums, start: start, res: &res)\nreturn res\n}\n
subset_sum_i.js
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(state, target, choices, start, res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target === 0) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor (let i = start; i < choices.length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] < 0) {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunction subsetSumI(nums, target) {\nconst state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort((a, b) => a - b); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nconst start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nconst res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_i.ts
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(\nstate: number[],\ntarget: number,\nchoices: number[],\nstart: number,\nres: number[][]\n): void {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target === 0) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor (let i = start; i < choices.length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] < 0) {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunction subsetSumI(nums: number[], target: number): number[][] {\nconst state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort((a, b) => a - b); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nconst start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nconst res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_i.dart
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(\nList<int> state,\nint target,\nList<int> choices,\nint start,\nList<List<int>> res,\n) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nres.add(List.from(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor (int i = start; i < choices.length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] < 0) {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.removeLast();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nList<List<int>> subsetSumI(List<int> nums, int target) {\nList<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nList<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_i.rs
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfn backtrack(mut state: Vec<i32>, target: i32, choices: &[i32], start: usize, res: &mut Vec<Vec<i32>>) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == 0 {\nres.push(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor i in start..choices.len() {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif target - choices[i] < 0 {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state.clone(), target - choices[i], choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfn subset_sum_i(nums: &mut [i32], target: i32) -> Vec<Vec<i32>> {\nlet state = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nlet start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nlet mut res = Vec::new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, &mut res);\nres\n}\n
subset_sum_i.c
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(vector *state, int target, vector *choices, int start, vector *res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nvector *tmpVector = newVector();\nfor (int i = 0; i < state->size; i++) {\nvectorPushback(tmpVector, state->data[i], sizeof(int));\n}\nvectorPushback(res, tmpVector, sizeof(vector));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor (int i = start; i < choices->size; i++) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif (target - *(int *)(choices->data[i]) < 0) {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nvectorPushback(state, choices->data[i], sizeof(int));\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - *(int *)(choices->data[i]), choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nvectorPopback(state);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nvector *subsetSumI(vector *nums, int target) {\nvector *state = newVector();                        // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nqsort(nums->data, nums->size, sizeof(int *), comp); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0;                                      // \u5b50\u96c6\u548c\nvector *res = newVector();                          // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_i.zig
[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumI}\n

\u5982\u56fe 13-12 \u6240\u793a\uff0c\u4e3a\u5c06\u6570\u7ec4 \\([3, 4, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \u8f93\u5165\u5230\u4ee5\u4e0a\u4ee3\u7801\u540e\u7684\u6574\u4f53\u56de\u6eaf\u8fc7\u7a0b\u3002

\u56fe 13-12 \u00a0 \u5b50\u96c6\u548c I \u56de\u6eaf\u8fc7\u7a0b

"},{"location":"chapter_backtracking/subset_sum_problem/#1332","title":"13.3.2 \u00a0 \u8003\u8651\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"

Question

\u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570\u6570\u7ec4 nums \u548c\u4e00\u4e2a\u76ee\u6807\u6b63\u6574\u6570 target \uff0c\u8bf7\u627e\u51fa\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\uff0c\u4f7f\u5f97\u7ec4\u5408\u4e2d\u7684\u5143\u7d20\u548c\u7b49\u4e8e target \u3002\u7ed9\u5b9a\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u6bcf\u4e2a\u5143\u7d20\u53ea\u53ef\u88ab\u9009\u62e9\u4e00\u6b21\u3002\u8bf7\u4ee5\u5217\u8868\u5f62\u5f0f\u8fd4\u56de\u8fd9\u4e9b\u7ec4\u5408\uff0c\u5217\u8868\u4e2d\u4e0d\u5e94\u5305\u542b\u91cd\u590d\u7ec4\u5408\u3002

\u76f8\u6bd4\u4e8e\u4e0a\u9898\uff0c\u672c\u9898\u7684\u8f93\u5165\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u8fd9\u5f15\u5165\u4e86\u65b0\u7684\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u7ed9\u5b9a\u6570\u7ec4 \\([4, \\hat{4}, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \uff0c\u5219\u73b0\u6709\u4ee3\u7801\u7684\u8f93\u51fa\u7ed3\u679c\u4e3a \\([4, 5], [\\hat{4}, 5]\\) \uff0c\u51fa\u73b0\u4e86\u91cd\u590d\u5b50\u96c6\u3002

\u9020\u6210\u8fd9\u79cd\u91cd\u590d\u7684\u539f\u56e0\u662f\u76f8\u7b49\u5143\u7d20\u5728\u67d0\u8f6e\u4e2d\u88ab\u591a\u6b21\u9009\u62e9\u3002\u5728\u56fe 13-13 \u4e2d\uff0c\u7b2c\u4e00\u8f6e\u5171\u6709\u4e09\u4e2a\u9009\u62e9\uff0c\u5176\u4e2d\u4e24\u4e2a\u90fd\u4e3a \\(4\\) \uff0c\u4f1a\u4ea7\u751f\u4e24\u4e2a\u91cd\u590d\u7684\u641c\u7d22\u5206\u652f\uff0c\u4ece\u800c\u8f93\u51fa\u91cd\u590d\u5b50\u96c6\uff1b\u540c\u7406\uff0c\u7b2c\u4e8c\u8f6e\u7684\u4e24\u4e2a \\(4\\) \u4e5f\u4f1a\u4ea7\u751f\u91cd\u590d\u5b50\u96c6\u3002

\u56fe 13-13 \u00a0 \u76f8\u7b49\u5143\u7d20\u5bfc\u81f4\u7684\u91cd\u590d\u5b50\u96c6

"},{"location":"chapter_backtracking/subset_sum_problem/#1_1","title":"1. \u00a0 \u76f8\u7b49\u5143\u7d20\u526a\u679d","text":"

\u4e3a\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u9700\u8981\u9650\u5236\u76f8\u7b49\u5143\u7d20\u5728\u6bcf\u4e00\u8f6e\u4e2d\u53ea\u88ab\u9009\u62e9\u4e00\u6b21\u3002\u5b9e\u73b0\u65b9\u5f0f\u6bd4\u8f83\u5de7\u5999\uff1a\u7531\u4e8e\u6570\u7ec4\u662f\u5df2\u6392\u5e8f\u7684\uff0c\u56e0\u6b64\u76f8\u7b49\u5143\u7d20\u90fd\u662f\u76f8\u90bb\u7684\u3002\u8fd9\u610f\u5473\u7740\u5728\u67d0\u8f6e\u9009\u62e9\u4e2d\uff0c\u82e5\u5f53\u524d\u5143\u7d20\u4e0e\u5176\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u5219\u8bf4\u660e\u5b83\u5df2\u7ecf\u88ab\u9009\u62e9\u8fc7\uff0c\u56e0\u6b64\u76f4\u63a5\u8df3\u8fc7\u5f53\u524d\u5143\u7d20\u3002

\u4e0e\u6b64\u540c\u65f6\uff0c\u672c\u9898\u89c4\u5b9a\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u53ea\u80fd\u88ab\u9009\u62e9\u4e00\u6b21\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5229\u7528\u53d8\u91cf start \u6765\u6ee1\u8db3\u8be5\u7ea6\u675f\uff1a\u5f53\u505a\u51fa\u9009\u62e9 \\(x_{i}\\) \u540e\uff0c\u8bbe\u5b9a\u4e0b\u4e00\u8f6e\u4ece\u7d22\u5f15 \\(i + 1\\) \u5f00\u59cb\u5411\u540e\u904d\u5386\u3002\u8fd9\u6837\u5373\u80fd\u53bb\u9664\u91cd\u590d\u5b50\u96c6\uff0c\u4e5f\u80fd\u907f\u514d\u91cd\u590d\u9009\u62e9\u5143\u7d20\u3002

"},{"location":"chapter_backtracking/subset_sum_problem/#2_1","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"PythonC++JavaC#GoSwiftJSTSDartRustCZig subset_sum_ii.py
def backtrack(\nstate: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n\"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II\"\"\"\n# \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == 0:\nres.append(list(state))\nreturn\n# \u904d\u5386\u6240\u6709\u9009\u62e9\n# \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n# \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor i in range(start, len(choices)):\n# \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n# \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif target - choices[i] < 0:\nbreak\n# \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif i > start and choices[i] == choices[i - 1]:\ncontinue\n# \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.append(choices[i])\n# \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i + 1, res)\n# \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop()\ndef subset_sum_ii(nums: list[int], target: int) -> list[list[int]]:\n\"\"\"\u6c42\u89e3\u5b50\u96c6\u548c II\"\"\"\nstate = []  # \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort()  # \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nstart = 0  # \u904d\u5386\u8d77\u59cb\u70b9\nres = []  # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res)\nreturn res\n
subset_sum_ii.cpp
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(vector<int> &state, int target, vector<int> &choices, int start, vector<vector<int>> &res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nres.push_back(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor (int i = start; i < choices.size(); i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] < 0) {\nbreak;\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif (i > start && choices[i] == choices[i - 1]) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.push_back(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i + 1, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop_back();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nvector<vector<int>> subsetSumII(vector<int> &nums, int target) {\nvector<int> state;              // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nsort(nums.begin(), nums.end()); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0;                  // \u904d\u5386\u8d77\u59cb\u70b9\nvector<vector<int>> res;        // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_ii.java
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nres.add(new ArrayList<>(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor (int i = start; i < choices.length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] < 0) {\nbreak;\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif (i > start && choices[i] == choices[i - 1]) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i + 1, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.remove(state.size() - 1);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nList<List<Integer>> subsetSumII(int[] nums, int target) {\nList<Integer> state = new ArrayList<>(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nArrays.sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nList<List<Integer>> res = new ArrayList<>(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_ii.cs
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nres.Add(new List<int>(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor (int i = start; i < choices.Length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] < 0) {\nbreak;\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif (i > start && choices[i] == choices[i - 1]) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.Add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nBacktrack(state, target - choices[i], choices, i + 1, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.RemoveAt(state.Count - 1);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nList<List<int>> SubsetSumII(int[] nums, int target) {\nList<int> state = new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nArray.Sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nList<List<int>> res = new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nBacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_ii.go
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunc backtrackSubsetSumII(start, target int, state, choices *[]int, res *[][]int) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == 0 {\nnewState := append([]int{}, *state...)\n*res = append(*res, newState)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor i := start; i < len(*choices); i++ {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif target-(*choices)[i] < 0 {\nbreak\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif i > start && (*choices)[i] == (*choices)[i-1] {\ncontinue\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n*state = append(*state, (*choices)[i])\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrackSubsetSumII(i+1, target-(*choices)[i], state, choices, res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n*state = (*state)[:len(*state)-1]\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunc subsetSumII(nums []int, target int) [][]int {\nstate := make([]int, 0) // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nsort.Ints(nums)         // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nstart := 0              // \u904d\u5386\u8d77\u59cb\u70b9\nres := make([][]int, 0) // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrackSubsetSumII(start, target, &state, &nums, &res)\nreturn res\n}\n
subset_sum_ii.swift
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunc backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == 0 {\nres.append(state)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor i in stride(from: start, to: choices.count, by: 1) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif target - choices[i] < 0 {\nbreak\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif i > start, choices[i] == choices[i - 1] {\ncontinue\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.append(choices[i])\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state: &state, target: target - choices[i], choices: choices, start: i + 1, res: &res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.removeLast()\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunc subsetSumII(nums: [Int], target: Int) -> [[Int]] {\nvar state: [Int] = [] // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nlet nums = nums.sorted() // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nlet start = 0 // \u904d\u5386\u8d77\u59cb\u70b9\nvar res: [[Int]] = [] // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state: &state, target: target, choices: nums, start: start, res: &res)\nreturn res\n}\n
subset_sum_ii.js
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunction backtrack(state, target, choices, start, res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target === 0) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor (let i = start; i < choices.length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] < 0) {\nbreak;\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif (i > start && choices[i] === choices[i - 1]) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i + 1, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunction subsetSumII(nums, target) {\nconst state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort((a, b) => a - b); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nconst start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nconst res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_ii.ts
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunction backtrack(\nstate: number[],\ntarget: number,\nchoices: number[],\nstart: number,\nres: number[][]\n): void {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target === 0) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor (let i = start; i < choices.length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] < 0) {\nbreak;\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif (i > start && choices[i] === choices[i - 1]) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i + 1, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunction subsetSumII(nums: number[], target: number): number[][] {\nconst state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort((a, b) => a - b); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nconst start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nconst res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_ii.dart
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(\nList<int> state,\nint target,\nList<int> choices,\nint start,\nList<List<int>> res,\n) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nres.add(List.from(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor (int i = start; i < choices.length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] < 0) {\nbreak;\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif (i > start && choices[i] == choices[i - 1]) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i + 1, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.removeLast();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nList<List<int>> subsetSumII(List<int> nums, int target) {\nList<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nList<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_ii.rs
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfn backtrack(mut state: Vec<i32>, target: i32, choices: &[i32], start: usize, res: &mut Vec<Vec<i32>>) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == 0 {\nres.push(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor i in start..choices.len() {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif target - choices[i] < 0 {\nbreak;\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif i > start && choices[i] == choices[i - 1] {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state.clone(), target - choices[i], choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfn subset_sum_ii(nums: &mut [i32], target: i32) -> Vec<Vec<i32>> {\nlet state = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nlet start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nlet mut res = Vec::new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, &mut res);\nres\n}\n
subset_sum_ii.c
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(vector *state, int target, vector *choices, int start, vector *res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nvector *tmpVector = newVector();\nfor (int i = 0; i < state->size; i++) {\nvectorPushback(tmpVector, state->data[i], sizeof(int));\n}\nvectorPushback(res, tmpVector, sizeof(vector));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor (int i = start; i < choices->size; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - *(int *)(choices->data[i]) < 0) {\ncontinue;\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif (i > start && *(int *)(choices->data[i]) == *(int *)(choices->data[i - 1])) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nvectorPushback(state, choices->data[i], sizeof(int));\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - *(int *)(choices->data[i]), choices, i + 1, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nvectorPopback(state);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nvector *subsetSumII(vector *nums, int target) {\nvector *state = newVector();                        // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nqsort(nums->data, nums->size, sizeof(int *), comp); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0;                                      // \u5b50\u96c6\u548c\nvector *res = newVector();                          // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_ii.zig
[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumII}\n

\u56fe 13-14 \u5c55\u793a\u4e86\u6570\u7ec4 \\([4, 4, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \u7684\u56de\u6eaf\u8fc7\u7a0b\uff0c\u5171\u5305\u542b\u56db\u79cd\u526a\u679d\u64cd\u4f5c\u3002\u8bf7\u4f60\u5c06\u56fe\u793a\u4e0e\u4ee3\u7801\u6ce8\u91ca\u76f8\u7ed3\u5408\uff0c\u7406\u89e3\u6574\u4e2a\u641c\u7d22\u8fc7\u7a0b\uff0c\u4ee5\u53ca\u6bcf\u79cd\u526a\u679d\u64cd\u4f5c\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002

\u56fe 13-14 \u00a0 \u5b50\u96c6\u548c II \u56de\u6eaf\u8fc7\u7a0b

"},{"location":"chapter_backtracking/summary/","title":"13.5 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_backtracking/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
  • \u56de\u6eaf\u7b97\u6cd5\u672c\u8d28\u662f\u7a77\u4e3e\u6cd5\uff0c\u901a\u8fc7\u5bf9\u89e3\u7a7a\u95f4\u8fdb\u884c\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u6765\u5bfb\u627e\u7b26\u5408\u6761\u4ef6\u7684\u89e3\u3002\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u9047\u5230\u6ee1\u8db3\u6761\u4ef6\u7684\u89e3\u5219\u8bb0\u5f55\uff0c\u76f4\u81f3\u627e\u5230\u6240\u6709\u89e3\u6216\u904d\u5386\u5b8c\u6210\u540e\u7ed3\u675f\u3002
  • \u56de\u6eaf\u7b97\u6cd5\u7684\u641c\u7d22\u8fc7\u7a0b\u5305\u62ec\u5c1d\u8bd5\u4e0e\u56de\u9000\u4e24\u4e2a\u90e8\u5206\u3002\u5b83\u901a\u8fc7\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u6765\u5c1d\u8bd5\u5404\u79cd\u9009\u62e9\uff0c\u5f53\u9047\u5230\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u60c5\u51b5\u65f6\uff0c\u5219\u64a4\u9500\u4e0a\u4e00\u6b65\u7684\u9009\u62e9\uff0c\u9000\u56de\u5230\u4e4b\u524d\u7684\u72b6\u6001\uff0c\u5e76\u7ee7\u7eed\u5c1d\u8bd5\u5176\u4ed6\u9009\u62e9\u3002\u5c1d\u8bd5\u4e0e\u56de\u9000\u662f\u4e24\u4e2a\u65b9\u5411\u76f8\u53cd\u7684\u64cd\u4f5c\u3002
  • \u56de\u6eaf\u95ee\u9898\u901a\u5e38\u5305\u542b\u591a\u4e2a\u7ea6\u675f\u6761\u4ef6\uff0c\u5b83\u4eec\u53ef\u7528\u4e8e\u5b9e\u73b0\u526a\u679d\u64cd\u4f5c\u3002\u526a\u679d\u53ef\u4ee5\u63d0\u524d\u7ed3\u675f\u4e0d\u5fc5\u8981\u7684\u641c\u7d22\u5206\u652f\uff0c\u5927\u5e45\u63d0\u5347\u641c\u7d22\u6548\u7387\u3002
  • \u56de\u6eaf\u7b97\u6cd5\u4e3b\u8981\u53ef\u7528\u4e8e\u89e3\u51b3\u641c\u7d22\u95ee\u9898\u548c\u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\u3002\u7ec4\u5408\u4f18\u5316\u95ee\u9898\u867d\u7136\u53ef\u4ee5\u7528\u56de\u6eaf\u7b97\u6cd5\u89e3\u51b3\uff0c\u4f46\u5f80\u5f80\u5b58\u5728\u66f4\u9ad8\u6548\u7387\u6216\u66f4\u597d\u6548\u679c\u7684\u89e3\u6cd5\u3002
  • \u5168\u6392\u5217\u95ee\u9898\u65e8\u5728\u641c\u7d22\u7ed9\u5b9a\u96c6\u5408\u7684\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u3002\u6211\u4eec\u501f\u52a9\u4e00\u4e2a\u6570\u7ec4\u6765\u8bb0\u5f55\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u88ab\u9009\u62e9\uff0c\u526a\u679d\u6389\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\u7684\u641c\u7d22\u5206\u652f\uff0c\u786e\u4fdd\u6bcf\u4e2a\u5143\u7d20\u53ea\u88ab\u9009\u62e9\u4e00\u6b21\u3002
  • \u5728\u5168\u6392\u5217\u95ee\u9898\u4e2d\uff0c\u5982\u679c\u96c6\u5408\u4e2d\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff0c\u5219\u6700\u7ec8\u7ed3\u679c\u4f1a\u51fa\u73b0\u91cd\u590d\u6392\u5217\u3002\u6211\u4eec\u9700\u8981\u7ea6\u675f\u76f8\u7b49\u5143\u7d20\u5728\u6bcf\u8f6e\u4e2d\u53ea\u80fd\u88ab\u9009\u62e9\u4e00\u6b21\uff0c\u8fd9\u901a\u5e38\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868\u6765\u5b9e\u73b0\u3002
  • \u5b50\u96c6\u548c\u95ee\u9898\u7684\u76ee\u6807\u662f\u5728\u7ed9\u5b9a\u96c6\u5408\u4e2d\u627e\u5230\u548c\u4e3a\u76ee\u6807\u503c\u7684\u6240\u6709\u5b50\u96c6\u3002\u96c6\u5408\u4e0d\u533a\u5206\u5143\u7d20\u987a\u5e8f\uff0c\u800c\u641c\u7d22\u8fc7\u7a0b\u4f1a\u8f93\u51fa\u6240\u6709\u987a\u5e8f\u7684\u7ed3\u679c\uff0c\u4ea7\u751f\u91cd\u590d\u5b50\u96c6\u3002\u6211\u4eec\u5728\u56de\u6eaf\u524d\u5c06\u6570\u636e\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u8bbe\u7f6e\u4e00\u4e2a\u53d8\u91cf\u6765\u6307\u793a\u6bcf\u4e00\u8f6e\u7684\u904d\u5386\u8d77\u70b9\uff0c\u4ece\u800c\u5c06\u751f\u6210\u91cd\u590d\u5b50\u96c6\u7684\u641c\u7d22\u5206\u652f\u8fdb\u884c\u526a\u679d\u3002
  • \u5bf9\u4e8e\u5b50\u96c6\u548c\u95ee\u9898\uff0c\u6570\u7ec4\u4e2d\u7684\u76f8\u7b49\u5143\u7d20\u4f1a\u4ea7\u751f\u91cd\u590d\u96c6\u5408\u3002\u6211\u4eec\u5229\u7528\u6570\u7ec4\u5df2\u6392\u5e8f\u7684\u524d\u7f6e\u6761\u4ef6\uff0c\u901a\u8fc7\u5224\u65ad\u76f8\u90bb\u5143\u7d20\u662f\u5426\u76f8\u7b49\u5b9e\u73b0\u526a\u679d\uff0c\u4ece\u800c\u786e\u4fdd\u76f8\u7b49\u5143\u7d20\u5728\u6bcf\u8f6e\u4e2d\u53ea\u80fd\u88ab\u9009\u4e2d\u4e00\u6b21\u3002
  • \\(n\\) \u7687\u540e\u65e8\u5728\u5bfb\u627e\u5c06 \\(n\\) \u4e2a\u7687\u540e\u653e\u7f6e\u5230 \\(n \\times n\\) \u5c3a\u5bf8\u68cb\u76d8\u4e0a\u7684\u65b9\u6848\uff0c\u8981\u6c42\u6240\u6709\u7687\u540e\u4e24\u4e24\u4e4b\u95f4\u65e0\u6cd5\u653b\u51fb\u5bf9\u65b9\u3002\u8be5\u95ee\u9898\u7684\u7ea6\u675f\u6761\u4ef6\u6709\u884c\u7ea6\u675f\u3001\u5217\u7ea6\u675f\u3001\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\u7ea6\u675f\u3002\u4e3a\u6ee1\u8db3\u884c\u7ea6\u675f\uff0c\u6211\u4eec\u91c7\u7528\u6309\u884c\u653e\u7f6e\u7684\u7b56\u7565\uff0c\u4fdd\u8bc1\u6bcf\u4e00\u884c\u653e\u7f6e\u4e00\u4e2a\u7687\u540e\u3002
  • \u5217\u7ea6\u675f\u548c\u5bf9\u89d2\u7ebf\u7ea6\u675f\u7684\u5904\u7406\u65b9\u5f0f\u7c7b\u4f3c\u3002\u5bf9\u4e8e\u5217\u7ea6\u675f\uff0c\u6211\u4eec\u5229\u7528\u4e00\u4e2a\u6570\u7ec4\u6765\u8bb0\u5f55\u6bcf\u4e00\u5217\u662f\u5426\u6709\u7687\u540e\uff0c\u4ece\u800c\u6307\u793a\u9009\u4e2d\u7684\u683c\u5b50\u662f\u5426\u5408\u6cd5\u3002\u5bf9\u4e8e\u5bf9\u89d2\u7ebf\u7ea6\u675f\uff0c\u6211\u4eec\u501f\u52a9\u4e24\u4e2a\u6570\u7ec4\u6765\u5206\u522b\u8bb0\u5f55\u8be5\u4e3b\u3001\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u5b58\u5728\u7687\u540e\uff1b\u96be\u70b9\u5728\u4e8e\u627e\u5904\u5728\u5230\u540c\u4e00\u4e3b\uff08\u526f\uff09\u5bf9\u89d2\u7ebf\u4e0a\u683c\u5b50\u6ee1\u8db3\u7684\u884c\u5217\u7d22\u5f15\u89c4\u5f8b\u3002
"},{"location":"chapter_backtracking/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

\u600e\u4e48\u7406\u89e3\u56de\u6eaf\u548c\u9012\u5f52\u7684\u5173\u7cfb\uff1f

\u603b\u7684\u6765\u770b\uff0c\u56de\u6eaf\u662f\u4e00\u79cd\u201c\u7b97\u6cd5\u7b56\u7565\u201d\uff0c\u800c\u9012\u5f52\u66f4\u50cf\u662f\u4e00\u4e2a\u201c\u5de5\u5177\u201d\u3002

  • \u56de\u6eaf\u7b97\u6cd5\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u5b9e\u73b0\u3002\u7136\u800c\uff0c\u56de\u6eaf\u662f\u9012\u5f52\u7684\u5e94\u7528\u573a\u666f\u4e4b\u4e00\uff0c\u662f\u9012\u5f52\u5728\u641c\u7d22\u95ee\u9898\u4e2d\u7684\u5e94\u7528\u3002
  • \u9012\u5f52\u7684\u7ed3\u6784\u4f53\u73b0\u4e86\u201c\u5b50\u95ee\u9898\u5206\u89e3\u201d\u7684\u89e3\u9898\u8303\u5f0f\uff0c\u5e38\u7528\u4e8e\u89e3\u51b3\u5206\u6cbb\u3001\u56de\u6eaf\u3001\u52a8\u6001\u89c4\u5212\uff08\u8bb0\u5fc6\u5316\u9012\u5f52\uff09\u7b49\u95ee\u9898\u3002
"},{"location":"chapter_computational_complexity/","title":"\u7b2c 2 \u7ae0 \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"

Abstract

\u590d\u6742\u5ea6\u5206\u6790\u72b9\u5982\u6d69\u701a\u7684\u7b97\u6cd5\u5b87\u5b99\u4e2d\u7684\u65f6\u7a7a\u5411\u5bfc\u3002

\u5b83\u5e26\u9886\u6211\u4eec\u5728\u65f6\u95f4\u4e0e\u7a7a\u95f4\u8fd9\u4e24\u4e2a\u7ef4\u5ea6\u4e0a\u6df1\u5165\u63a2\u7d22\uff0c\u5bfb\u627e\u66f4\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u6848\u3002

"},{"location":"chapter_computational_complexity/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 2.1 \u00a0 \u7b97\u6cd5\u6548\u7387\u8bc4\u4f30
  • 2.2 \u00a0 \u8fed\u4ee3\u4e0e\u9012\u5f52
  • 2.3 \u00a0 \u65f6\u95f4\u590d\u6742\u5ea6
  • 2.4 \u00a0 \u7a7a\u95f4\u590d\u6742\u5ea6
  • 2.5 \u00a0 \u5c0f\u7ed3
"},{"location":"chapter_computational_complexity/iteration_and_recursion/","title":"2.2 \u00a0 \u8fed\u4ee3\u4e0e\u9012\u5f52","text":"

\u5728\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u4e2d\uff0c\u91cd\u590d\u6267\u884c\u67d0\u4e2a\u4efb\u52a1\u662f\u5f88\u5e38\u89c1\u7684\uff0c\u5176\u4e0e\u7b97\u6cd5\u7684\u590d\u6742\u5ea6\u5bc6\u5207\u76f8\u5173\u3002\u800c\u8981\u91cd\u590d\u6267\u884c\u67d0\u4e2a\u4efb\u52a1\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u9009\u7528\u4e24\u79cd\u57fa\u672c\u7684\u7a0b\u5e8f\u7ed3\u6784\uff1a\u8fed\u4ee3\u548c\u9012\u5f52\u3002

"},{"location":"chapter_computational_complexity/iteration_and_recursion/#221","title":"2.2.1 \u00a0 \u8fed\u4ee3","text":"

\u300c\u8fed\u4ee3 iteration\u300d\u662f\u4e00\u79cd\u91cd\u590d\u6267\u884c\u67d0\u4e2a\u4efb\u52a1\u7684\u63a7\u5236\u7ed3\u6784\u3002\u5728\u8fed\u4ee3\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u5728\u6ee1\u8db3\u4e00\u5b9a\u7684\u6761\u4ef6\u4e0b\u91cd\u590d\u6267\u884c\u67d0\u6bb5\u4ee3\u7801\uff0c\u76f4\u5230\u8fd9\u4e2a\u6761\u4ef6\u4e0d\u518d\u6ee1\u8db3\u3002

"},{"location":"chapter_computational_complexity/iteration_and_recursion/#1-for","title":"1. \u00a0 for \u5faa\u73af","text":"

for \u5faa\u73af\u662f\u6700\u5e38\u89c1\u7684\u8fed\u4ee3\u5f62\u5f0f\u4e4b\u4e00\uff0c\u9002\u5408\u9884\u5148\u77e5\u9053\u8fed\u4ee3\u6b21\u6570\u65f6\u4f7f\u7528\u3002

\u4ee5\u4e0b\u51fd\u6570\u57fa\u4e8e for \u5faa\u73af\u5b9e\u73b0\u4e86\u6c42\u548c \\(1 + 2 + \\dots + n\\) \uff0c\u6c42\u548c\u7ed3\u679c\u4f7f\u7528\u53d8\u91cf res \u8bb0\u5f55\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cPython \u4e2d range(a, b) \u5bf9\u5e94\u7684\u533a\u95f4\u662f\u201c\u5de6\u95ed\u53f3\u5f00\u201d\u7684\uff0c\u5bf9\u5e94\u7684\u904d\u5386\u8303\u56f4\u4e3a \\(a, a + 1, \\dots, b-1\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig iteration.py
def for_loop(n: int) -> int:\n\"\"\"for \u5faa\u73af\"\"\"\nres = 0\n# \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nfor i in range(1, n + 1):\nres += i\nreturn res\n
iteration.cpp
/* for \u5faa\u73af */\nint forLoop(int n) {\nint res = 0;\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nfor (int i = 1; i <= n; ++i) {\nres += i;\n}\nreturn res;\n}\n
iteration.java
/* for \u5faa\u73af */\nint forLoop(int n) {\nint res = 0;\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nfor (int i = 1; i <= n; i++) {\nres += i;\n}\nreturn res;\n}\n
iteration.cs
/* for \u5faa\u73af */\nint ForLoop(int n) {\nint res = 0;\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nfor (int i = 1; i <= n; i++) {\nres += i;\n}\nreturn res;\n}\n
iteration.go
/* for \u5faa\u73af */\nfunc forLoop(n int) int {\nres := 0\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nfor i := 1; i <= n; i++ {\nres += i\n}\nreturn res\n}\n
iteration.swift
/* for \u5faa\u73af */\nfunc forLoop(n: Int) -> Int {\nvar res = 0\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nfor i in 1 ... n {\nres += i\n}\nreturn res\n}\n
iteration.js
/* for \u5faa\u73af */\nfunction forLoop(n) {\nlet res = 0;\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nfor (let i = 1; i <= n; i++) {\nres += i;\n}\nreturn res;\n}\n
iteration.ts
/* for \u5faa\u73af */\nfunction forLoop(n: number): number {\nlet res = 0;\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nfor (let i = 1; i <= n; i++) {\nres += i;\n}\nreturn res;\n}\n
iteration.dart
/* for \u5faa\u73af */\nint forLoop(int n) {\nint res = 0;\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nfor (int i = 1; i <= n; i++) {\nres += i;\n}\nreturn res;\n}\n
iteration.rs
/* for \u5faa\u73af */\nfn for_loop(n: i32) -> i32 {\nlet mut res = 0;\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nfor i in 1..=n {\nres += i;\n}\nres\n} 
iteration.c
/* for \u5faa\u73af */\nint forLoop(int n) {\nint res = 0;\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nfor (int i = 1; i <= n; i++) {\nres += i;\n}\nreturn res;\n}\n
iteration.zig
[class]{}-[func]{forLoop}\n

\u56fe 2-1 \u5c55\u793a\u4e86\u8be5\u6c42\u548c\u51fd\u6570\u7684\u6d41\u7a0b\u6846\u56fe\u3002

\u56fe 2-1 \u00a0 \u6c42\u548c\u51fd\u6570\u7684\u6d41\u7a0b\u6846\u56fe

\u6b64\u6c42\u548c\u51fd\u6570\u7684\u64cd\u4f5c\u6570\u91cf\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u6210\u6b63\u6bd4\uff0c\u6216\u8005\u8bf4\u6210\u201c\u7ebf\u6027\u5173\u7cfb\u201d\u3002\u5b9e\u9645\u4e0a\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u63cf\u8ff0\u7684\u5c31\u662f\u8fd9\u4e2a\u201c\u7ebf\u6027\u5173\u7cfb\u201d\u3002\u76f8\u5173\u5185\u5bb9\u5c06\u4f1a\u5728\u4e0b\u4e00\u8282\u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u3002

"},{"location":"chapter_computational_complexity/iteration_and_recursion/#2-while","title":"2. \u00a0 while \u5faa\u73af","text":"

\u4e0e for \u5faa\u73af\u7c7b\u4f3c\uff0cwhile \u5faa\u73af\u4e5f\u662f\u4e00\u79cd\u5b9e\u73b0\u8fed\u4ee3\u7684\u65b9\u6cd5\u3002\u5728 while \u5faa\u73af\u4e2d\uff0c\u7a0b\u5e8f\u6bcf\u8f6e\u90fd\u4f1a\u5148\u68c0\u67e5\u6761\u4ef6\uff0c\u5982\u679c\u6761\u4ef6\u4e3a\u771f\u5219\u7ee7\u7eed\u6267\u884c\uff0c\u5426\u5219\u5c31\u7ed3\u675f\u5faa\u73af\u3002

\u4e0b\u9762\uff0c\u6211\u4eec\u7528 while \u5faa\u73af\u6765\u5b9e\u73b0\u6c42\u548c \\(1 + 2 + \\dots + n\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig iteration.py
def while_loop(n: int) -> int:\n\"\"\"while \u5faa\u73af\"\"\"\nres = 0\ni = 1  # \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n# \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nwhile i <= n:\nres += i\ni += 1  # \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\nreturn res\n
iteration.cpp
/* while \u5faa\u73af */\nint whileLoop(int n) {\nint res = 0;\nint i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nwhile (i <= n) {\nres += i;\ni++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n}\nreturn res;\n}\n
iteration.java
/* while \u5faa\u73af */\nint whileLoop(int n) {\nint res = 0;\nint i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nwhile (i <= n) {\nres += i;\ni++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n}\nreturn res;\n}\n
iteration.cs
/* while \u5faa\u73af */\nint WhileLoop(int n) {\nint res = 0;\nint i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nwhile (i <= n) {\nres += i;\ni += 1; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n}\nreturn res;\n}\n
iteration.go
/* while \u5faa\u73af */\nfunc whileLoop(n int) int {\nres := 0\n// \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\ni := 1\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nfor i <= n {\nres += i\n// \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\ni++\n}\nreturn res\n}\n
iteration.swift
/* while \u5faa\u73af */\nfunc whileLoop(n: Int) -> Int {\nvar res = 0\nvar i = 1 // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nwhile i <= n {\nres += i\ni += 1 // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n}\nreturn res\n}\n
iteration.js
/* while \u5faa\u73af */\nfunction whileLoop(n) {\nlet res = 0;\nlet i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nwhile (i <= n) {\nres += i;\ni++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n}\nreturn res;\n}\n
iteration.ts
/* while \u5faa\u73af */\nfunction whileLoop(n: number): number {\nlet res = 0;\nlet i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nwhile (i <= n) {\nres += i;\ni++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n}\nreturn res;\n}\n
iteration.dart
/* while \u5faa\u73af */\nint whileLoop(int n) {\nint res = 0;\nint i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nwhile (i <= n) {\nres += i;\ni++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n}\nreturn res;\n}\n
iteration.rs
/* while \u5faa\u73af */\nfn while_loop(n: i32) -> i32 {\nlet mut res = 0;\nlet mut i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nwhile i <= n {\nres += i;\ni += 1; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n}\nres\n}\n
iteration.c
/* while \u5faa\u73af */\nint whileLoop(int n) {\nint res = 0;\nint i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nwhile (i <= n) {\nres += i;\ni++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n}\nreturn res;\n}\n
iteration.zig
[class]{}-[func]{whileLoop}\n

\u5728 while \u5faa\u73af\u4e2d\uff0c\u7531\u4e8e\u521d\u59cb\u5316\u548c\u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\u7684\u6b65\u9aa4\u662f\u72ec\u7acb\u5728\u5faa\u73af\u7ed3\u6784\u4e4b\u5916\u7684\uff0c\u56e0\u6b64\u5b83\u6bd4 for \u5faa\u73af\u7684\u81ea\u7531\u5ea6\u66f4\u9ad8\u3002

\u4f8b\u5982\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u6761\u4ef6\u53d8\u91cf \\(i\\) \u6bcf\u8f6e\u8fdb\u884c\u4e86\u4e24\u6b21\u66f4\u65b0\uff0c\u8fd9\u79cd\u60c5\u51b5\u5c31\u4e0d\u592a\u65b9\u4fbf\u7528 for \u5faa\u73af\u5b9e\u73b0\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig iteration.py
def while_loop_ii(n: int) -> int:\n\"\"\"while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09\"\"\"\nres = 0\ni = 1  # \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n# \u5faa\u73af\u6c42\u548c 1, 4, ...\nwhile i <= n:\nres += i\n# \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\ni += 1\ni *= 2\nreturn res\n
iteration.cpp
/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\nint res = 0;\nint i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 4, ...\nwhile (i <= n) {\nres += i;\n// \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\ni++;\ni *= 2;\n}\nreturn res;\n}\n
iteration.java
/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\nint res = 0;\nint i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 4, ...\nwhile (i <= n) {\nres += i;\n// \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\ni++;\ni *= 2;\n}\nreturn res;\n}\n
iteration.cs
/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint WhileLoopII(int n) {\nint res = 0;\nint i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 2, 4, 5...\nwhile (i <= n) {\nres += i;\n// \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\ni += 1; i *= 2;\n}\nreturn res;\n}\n
iteration.go
/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunc whileLoopII(n int) int {\nres := 0\n// \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\ni := 1\n// \u5faa\u73af\u6c42\u548c 1, 4, ...\nfor i <= n {\nres += i\n// \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\ni++\ni *= 2\n}\nreturn res\n}\n
iteration.swift
/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunc whileLoopII(n: Int) -> Int {\nvar res = 0\nvar i = 1 // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 4, ...\nwhile i <= n {\nres += i\n// \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\ni += 1\ni *= 2\n}\nreturn res\n}\n
iteration.js
/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunction whileLoopII(n) {\nlet res = 0;\nlet i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 4, ...\nwhile (i <= n) {\nres += i;\n// \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\ni++;\ni *= 2;\n}\nreturn res;\n}\n
iteration.ts
/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunction whileLoopII(n: number): number {\nlet res = 0;\nlet i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 4, ...\nwhile (i <= n) {\nres += i;\n// \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\ni++;\ni *= 2;\n}\nreturn res;\n}\n
iteration.dart
/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\nint res = 0;\nint i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 4, ...\nwhile (i <= n) {\nres += i;\n// \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\ni++;\ni *= 2;\n}\nreturn res;\n}\n
iteration.rs
/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfn while_loop_ii(n: i32) -> i32 {\nlet mut res = 0;\nlet mut i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 4, ...\nwhile i <= n {\nres += i;\n// \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\ni += 1;\ni *= 2;\n}\nres\n}\n
iteration.c
/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\nint res = 0;\nint i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 4, ...\nwhile (i <= n) {\nres += i;\n// \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\ni++;\ni *= 2;\n}\nreturn res;\n}\n
iteration.zig
[class]{}-[func]{whileLoopII}\n

\u603b\u7684\u6765\u8bf4\uff0cfor \u5faa\u73af\u7684\u4ee3\u7801\u66f4\u52a0\u7d27\u51d1\uff0cwhile \u5faa\u73af\u66f4\u52a0\u7075\u6d3b\uff0c\u4e24\u8005\u90fd\u53ef\u4ee5\u5b9e\u73b0\u8fed\u4ee3\u7ed3\u6784\u3002\u9009\u62e9\u4f7f\u7528\u54ea\u4e00\u4e2a\u5e94\u8be5\u6839\u636e\u7279\u5b9a\u95ee\u9898\u7684\u9700\u6c42\u6765\u51b3\u5b9a\u3002

"},{"location":"chapter_computational_complexity/iteration_and_recursion/#3","title":"3. \u00a0 \u5d4c\u5957\u5faa\u73af","text":"

\u6211\u4eec\u53ef\u4ee5\u5728\u4e00\u4e2a\u5faa\u73af\u7ed3\u6784\u5185\u5d4c\u5957\u53e6\u4e00\u4e2a\u5faa\u73af\u7ed3\u6784\uff0c\u4ee5 for \u5faa\u73af\u4e3a\u4f8b\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig iteration.py
def nested_for_loop(n: int) -> str:\n\"\"\"\u53cc\u5c42 for \u5faa\u73af\"\"\"\nres = \"\"\n# \u5faa\u73af i = 1, 2, ..., n-1, n\nfor i in range(1, n + 1):\n# \u5faa\u73af j = 1, 2, ..., n-1, n\nfor j in range(1, n + 1):\nres += f\"({i}, {j}), \"\nreturn res\n
iteration.cpp
/* \u53cc\u5c42 for \u5faa\u73af */\nstring nestedForLoop(int n) {\nostringstream res;\n// \u5faa\u73af i = 1, 2, ..., n-1, n\nfor (int i = 1; i <= n; ++i) {\n// \u5faa\u73af j = 1, 2, ..., n-1, n\nfor (int j = 1; j <= n; ++j) {\nres << \"(\" << i << \", \" << j << \"), \";\n}\n}\nreturn res.str();\n}\n
iteration.java
/* \u53cc\u5c42 for \u5faa\u73af */\nString nestedForLoop(int n) {\nStringBuilder res = new StringBuilder();\n// \u5faa\u73af i = 1, 2, ..., n-1, n\nfor (int i = 1; i <= n; i++) {\n// \u5faa\u73af j = 1, 2, ..., n-1, n\nfor (int j = 1; j <= n; j++) {\nres.append(\"(\" + i + \", \" + j + \"), \");\n}\n}\nreturn res.toString();\n}\n
iteration.cs
/* \u53cc\u5c42 for \u5faa\u73af */\nstring NestedForLoop(int n) {\nStringBuilder res = new();\n// \u5faa\u73af i = 1, 2, ..., n-1, n\nfor (int i = 1; i <= n; i++) {\n// \u5faa\u73af j = 1, 2, ..., n-1, n\nfor (int j = 1; j <= n; j++) {\nres.Append($\"({i}, {j}), \");\n}\n}\nreturn res.ToString();\n}\n
iteration.go
/* \u53cc\u5c42 for \u5faa\u73af */\nfunc nestedForLoop(n int) string {\nres := \"\"\n// \u5faa\u73af i = 1, 2, ..., n-1, n\nfor i := 1; i <= n; i++ {\nfor j := 1; j <= n; j++ {\n// \u5faa\u73af j = 1, 2, ..., n-1, n\nres += fmt.Sprintf(\"(%d, %d), \", i, j)\n}\n}\nreturn res\n}\n
iteration.swift
/* \u53cc\u5c42 for \u5faa\u73af */\nfunc nestedForLoop(n: Int) -> String {\nvar res = \"\"\n// \u5faa\u73af i = 1, 2, ..., n-1, n\nfor i in 1 ... n {\n// \u5faa\u73af j = 1, 2, ..., n-1, n\nfor j in 1 ... n {\nres.append(\"(\\(i), \\(j)), \")\n}\n}\nreturn res\n}\n
iteration.js
/* \u53cc\u5c42 for \u5faa\u73af */\nfunction nestedForLoop(n) {\nlet res = '';\n// \u5faa\u73af i = 1, 2, ..., n-1, n\nfor (let i = 1; i <= n; i++) {\n// \u5faa\u73af j = 1, 2, ..., n-1, n\nfor (let j = 1; j <= n; j++) {\nres += `(${i}, ${j}), `;\n}\n}\nreturn res;\n}\n
iteration.ts
/* \u53cc\u5c42 for \u5faa\u73af */\nfunction nestedForLoop(n: number): string {\nlet res = '';\n// \u5faa\u73af i = 1, 2, ..., n-1, n\nfor (let i = 1; i <= n; i++) {\n// \u5faa\u73af j = 1, 2, ..., n-1, n\nfor (let j = 1; j <= n; j++) {\nres += `(${i}, ${j}), `;\n}\n}\nreturn res;\n}\n
iteration.dart
/* \u53cc\u5c42 for \u5faa\u73af */\nString nestedForLoop(int n) {\nString res = \"\";\n// \u5faa\u73af i = 1, 2, ..., n-1, n\nfor (int i = 1; i <= n; i++) {\n// \u5faa\u73af j = 1, 2, ..., n-1, n\nfor (int j = 1; j <= n; j++) {\nres += \"($i, $j), \";\n}\n}\nreturn res;\n}\n
iteration.rs
/* \u53cc\u5c42 for \u5faa\u73af */\nfn nested_for_loop(n: i32) -> String {\nlet mut res = vec![];\n// \u5faa\u73af i = 1, 2, ..., n-1, n\nfor i in 1..=n {\n// \u5faa\u73af j = 1, 2, ..., n-1, n\nfor j in 1..=n {\nres.push(format!(\"({}, {}), \", i, j));\n}\n}\nres.join(\"\")\n}\n
iteration.c
/* \u53cc\u5c42 for \u5faa\u73af */\nchar *nestedForLoop(int n) {\n// n * n \u4e3a\u5bf9\u5e94\u70b9\u6570\u91cf\uff0c\"(i, j), \" \u5bf9\u5e94\u5b57\u7b26\u4e32\u957f\u6700\u5927\u4e3a 6+10*2\uff0c\u52a0\u4e0a\u6700\u540e\u4e00\u4e2a\u7a7a\u5b57\u7b26 \\0 \u7684\u989d\u5916\u7a7a\u95f4\nint size = n * n * 26 + 1;\nchar *res = malloc(size * sizeof(char));\n// \u5faa\u73af i = 1, 2, ..., n-1, n\nfor (int i = 1; i <= n; i++) {\n// \u5faa\u73af j = 1, 2, ..., n-1, n\nfor (int j = 1; j <= n; j++) {\nchar tmp[26];\nsnprintf(tmp, sizeof(tmp), \"(%d, %d), \", i, j);\nstrncat(res, tmp, size - strlen(res) - 1);\n}\n}\nreturn res;\n}\n
iteration.zig
[class]{}-[func]{nestedForLoop}\n

\u56fe 2-2 \u7ed9\u51fa\u4e86\u8be5\u5d4c\u5957\u5faa\u73af\u7684\u6d41\u7a0b\u6846\u56fe\u3002

\u56fe 2-2 \u00a0 \u5d4c\u5957\u5faa\u73af\u7684\u6d41\u7a0b\u6846\u56fe

\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u51fd\u6570\u7684\u64cd\u4f5c\u6570\u91cf\u4e0e \\(n^2\\) \u6210\u6b63\u6bd4\uff0c\u6216\u8005\u8bf4\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u548c\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u6210\u201c\u5e73\u65b9\u5173\u7cfb\u201d\u3002

\u6211\u4eec\u53ef\u4ee5\u7ee7\u7eed\u6dfb\u52a0\u5d4c\u5957\u5faa\u73af\uff0c\u6bcf\u4e00\u6b21\u5d4c\u5957\u90fd\u662f\u4e00\u6b21\u201c\u5347\u7ef4\u201d\uff0c\u5c06\u4f1a\u4f7f\u65f6\u95f4\u590d\u6742\u5ea6\u63d0\u9ad8\u81f3\u201c\u7acb\u65b9\u5173\u7cfb\u201d\u3001\u201c\u56db\u6b21\u65b9\u5173\u7cfb\u201d\u3001\u4ee5\u6b64\u7c7b\u63a8\u3002

"},{"location":"chapter_computational_complexity/iteration_and_recursion/#222","title":"2.2.2 \u00a0 \u9012\u5f52","text":"

\u300c\u9012\u5f52 recursion\u300d\u662f\u4e00\u79cd\u7b97\u6cd5\u7b56\u7565\uff0c\u901a\u8fc7\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u6765\u89e3\u51b3\u95ee\u9898\u3002\u5b83\u4e3b\u8981\u5305\u542b\u4e24\u4e2a\u9636\u6bb5\u3002

  1. \u9012\uff1a\u7a0b\u5e8f\u4e0d\u65ad\u6df1\u5165\u5730\u8c03\u7528\u81ea\u8eab\uff0c\u901a\u5e38\u4f20\u5165\u66f4\u5c0f\u6216\u66f4\u7b80\u5316\u7684\u53c2\u6570\uff0c\u76f4\u5230\u8fbe\u5230\u201c\u7ec8\u6b62\u6761\u4ef6\u201d\u3002
  2. \u5f52\uff1a\u89e6\u53d1\u201c\u7ec8\u6b62\u6761\u4ef6\u201d\u540e\uff0c\u7a0b\u5e8f\u4ece\u6700\u6df1\u5c42\u7684\u9012\u5f52\u51fd\u6570\u5f00\u59cb\u9010\u5c42\u8fd4\u56de\uff0c\u6c47\u805a\u6bcf\u4e00\u5c42\u7684\u7ed3\u679c\u3002

\u800c\u4ece\u5b9e\u73b0\u7684\u89d2\u5ea6\u770b\uff0c\u9012\u5f52\u4ee3\u7801\u4e3b\u8981\u5305\u542b\u4e09\u4e2a\u8981\u7d20\u3002

  1. \u7ec8\u6b62\u6761\u4ef6\uff1a\u7528\u4e8e\u51b3\u5b9a\u4ec0\u4e48\u65f6\u5019\u7531\u201c\u9012\u201d\u8f6c\u201c\u5f52\u201d\u3002
  2. \u9012\u5f52\u8c03\u7528\uff1a\u5bf9\u5e94\u201c\u9012\u201d\uff0c\u51fd\u6570\u8c03\u7528\u81ea\u8eab\uff0c\u901a\u5e38\u8f93\u5165\u66f4\u5c0f\u6216\u66f4\u7b80\u5316\u7684\u53c2\u6570\u3002
  3. \u8fd4\u56de\u7ed3\u679c\uff1a\u5bf9\u5e94\u201c\u5f52\u201d\uff0c\u5c06\u5f53\u524d\u9012\u5f52\u5c42\u7ea7\u7684\u7ed3\u679c\u8fd4\u56de\u81f3\u4e0a\u4e00\u5c42\u3002

\u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u6211\u4eec\u53ea\u9700\u8c03\u7528\u51fd\u6570 recur(n) \uff0c\u5c31\u53ef\u4ee5\u5b8c\u6210 \\(1 + 2 + \\dots + n\\) \u7684\u8ba1\u7b97\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig recursion.py
def recur(n: int) -> int:\n\"\"\"\u9012\u5f52\"\"\"\n# \u7ec8\u6b62\u6761\u4ef6\nif n == 1:\nreturn 1\n# \u9012\uff1a\u9012\u5f52\u8c03\u7528\nres = recur(n - 1)\n# \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nreturn n + res\n
recursion.cpp
/* \u9012\u5f52 */\nint recur(int n) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n == 1)\nreturn 1;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nint res = recur(n - 1);\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nreturn n + res;\n}\n
recursion.java
/* \u9012\u5f52 */\nint recur(int n) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n == 1)\nreturn 1;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nint res = recur(n - 1);\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nreturn n + res;\n}\n
recursion.cs
/* \u9012\u5f52 */\nint Recur(int n) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n == 1)\nreturn 1;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nint res = Recur(n - 1);\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nreturn n + res;\n}\n
recursion.go
/* \u9012\u5f52 */\nfunc recur(n int) int {\n// \u7ec8\u6b62\u6761\u4ef6\nif n == 1 {\nreturn 1\n}\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nres := recur(n - 1)\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nreturn n + res\n}\n
recursion.swift
/* \u9012\u5f52 */\nfunc recur(n: Int) -> Int {\n// \u7ec8\u6b62\u6761\u4ef6\nif n == 1 {\nreturn 1\n}\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nlet res = recur(n: n - 1)\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nreturn n + res\n}\n
recursion.js
/* \u9012\u5f52 */\nfunction recur(n) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n === 1) return 1;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nconst res = recur(n - 1);\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nreturn n + res;\n}\n
recursion.ts
/* \u9012\u5f52 */\nfunction recur(n: number): number {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n === 1) return 1;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nconst res = recur(n - 1);\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nreturn n + res;\n}\n
recursion.dart
/* \u9012\u5f52 */\nint recur(int n) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n == 1) return 1;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nint res = recur(n - 1);\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nreturn n + res;\n}\n
recursion.rs
/* \u9012\u5f52 */\nfn recur(n: i32) -> i32 {\n// \u7ec8\u6b62\u6761\u4ef6\nif n == 1 {\nreturn 1;\n}\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nlet res = recur(n - 1);\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nn + res\n}\n
recursion.c
/* \u9012\u5f52 */\nint recur(int n) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n == 1)\nreturn 1;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nint res = recur(n - 1);\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nreturn n + res;\n}\n
recursion.zig
[class]{}-[func]{recur}\n

\u56fe 2-3 \u5c55\u793a\u4e86\u8be5\u51fd\u6570\u7684\u9012\u5f52\u8fc7\u7a0b\u3002

\u56fe 2-3 \u00a0 \u6c42\u548c\u51fd\u6570\u7684\u9012\u5f52\u8fc7\u7a0b

\u867d\u7136\u4ece\u8ba1\u7b97\u89d2\u5ea6\u770b\uff0c\u8fed\u4ee3\u4e0e\u9012\u5f52\u53ef\u4ee5\u5f97\u5230\u76f8\u540c\u7684\u7ed3\u679c\uff0c\u4f46\u5b83\u4eec\u4ee3\u8868\u4e86\u4e24\u79cd\u5b8c\u5168\u4e0d\u540c\u7684\u601d\u8003\u548c\u89e3\u51b3\u95ee\u9898\u7684\u8303\u5f0f\u3002

  • \u8fed\u4ee3\uff1a\u201c\u81ea\u4e0b\u800c\u4e0a\u201d\u5730\u89e3\u51b3\u95ee\u9898\u3002\u4ece\u6700\u57fa\u7840\u7684\u6b65\u9aa4\u5f00\u59cb\uff0c\u7136\u540e\u4e0d\u65ad\u91cd\u590d\u6216\u7d2f\u52a0\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u76f4\u5230\u4efb\u52a1\u5b8c\u6210\u3002
  • \u9012\u5f52\uff1a\u201c\u81ea\u4e0a\u800c\u4e0b\u201d\u5730\u89e3\u51b3\u95ee\u9898\u3002\u5c06\u539f\u95ee\u9898\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u8fd9\u4e9b\u5b50\u95ee\u9898\u548c\u539f\u95ee\u9898\u5177\u6709\u76f8\u540c\u7684\u5f62\u5f0f\u3002\u63a5\u4e0b\u6765\u5c06\u5b50\u95ee\u9898\u7ee7\u7eed\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u76f4\u5230\u57fa\u672c\u60c5\u51b5\u65f6\u505c\u6b62\uff08\u57fa\u672c\u60c5\u51b5\u7684\u89e3\u662f\u5df2\u77e5\u7684\uff09\u3002

\u4ee5\u4e0a\u8ff0\u7684\u6c42\u548c\u51fd\u6570\u4e3a\u4f8b\uff0c\u8bbe\u95ee\u9898 \\(f(n) = 1 + 2 + \\dots + n\\) \u3002

  • \u8fed\u4ee3\uff1a\u5728\u5faa\u73af\u4e2d\u6a21\u62df\u6c42\u548c\u8fc7\u7a0b\uff0c\u4ece \\(1\\) \u904d\u5386\u5230 \\(n\\) \uff0c\u6bcf\u8f6e\u6267\u884c\u6c42\u548c\u64cd\u4f5c\uff0c\u5373\u53ef\u6c42\u5f97 \\(f(n)\\) \u3002
  • \u9012\u5f52\uff1a\u5c06\u95ee\u9898\u5206\u89e3\u4e3a\u5b50\u95ee\u9898 \\(f(n) = n + f(n-1)\\) \uff0c\u4e0d\u65ad\uff08\u9012\u5f52\u5730\uff09\u5206\u89e3\u4e0b\u53bb\uff0c\u76f4\u81f3\u57fa\u672c\u60c5\u51b5 \\(f(1) = 1\\) \u65f6\u7ec8\u6b62\u3002
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#1","title":"1. \u00a0 \u8c03\u7528\u6808","text":"

\u9012\u5f52\u51fd\u6570\u6bcf\u6b21\u8c03\u7528\u81ea\u8eab\u65f6\uff0c\u7cfb\u7edf\u90fd\u4f1a\u4e3a\u65b0\u5f00\u542f\u7684\u51fd\u6570\u5206\u914d\u5185\u5b58\uff0c\u4ee5\u5b58\u50a8\u5c40\u90e8\u53d8\u91cf\u3001\u8c03\u7528\u5730\u5740\u548c\u5176\u4ed6\u4fe1\u606f\u7b49\u3002\u8fd9\u5c06\u5bfc\u81f4\u4e24\u65b9\u9762\u7684\u7ed3\u679c\u3002

  • \u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u6570\u636e\u90fd\u5b58\u50a8\u5728\u79f0\u4e3a\u201c\u6808\u5e27\u7a7a\u95f4\u201d\u7684\u5185\u5b58\u533a\u57df\u4e2d\uff0c\u76f4\u81f3\u51fd\u6570\u8fd4\u56de\u540e\u624d\u4f1a\u88ab\u91ca\u653e\u3002\u56e0\u6b64\uff0c\u9012\u5f52\u901a\u5e38\u6bd4\u8fed\u4ee3\u66f4\u52a0\u8017\u8d39\u5185\u5b58\u7a7a\u95f4\u3002
  • \u9012\u5f52\u8c03\u7528\u51fd\u6570\u4f1a\u4ea7\u751f\u989d\u5916\u7684\u5f00\u9500\u3002\u56e0\u6b64\u9012\u5f52\u901a\u5e38\u6bd4\u5faa\u73af\u7684\u65f6\u95f4\u6548\u7387\u66f4\u4f4e\u3002

\u5982\u56fe 2-4 \u6240\u793a\uff0c\u5728\u89e6\u53d1\u7ec8\u6b62\u6761\u4ef6\u524d\uff0c\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684\u9012\u5f52\u51fd\u6570\uff0c\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \u3002

\u56fe 2-4 \u00a0 \u9012\u5f52\u8c03\u7528\u6df1\u5ea6

\u5728\u5b9e\u9645\u4e2d\uff0c\u7f16\u7a0b\u8bed\u8a00\u5141\u8bb8\u7684\u9012\u5f52\u6df1\u5ea6\u901a\u5e38\u662f\u6709\u9650\u7684\uff0c\u8fc7\u6df1\u7684\u9012\u5f52\u53ef\u80fd\u5bfc\u81f4\u6808\u6ea2\u51fa\u62a5\u9519\u3002

"},{"location":"chapter_computational_complexity/iteration_and_recursion/#2","title":"2. \u00a0 \u5c3e\u9012\u5f52","text":"

\u6709\u8da3\u7684\u662f\uff0c\u5982\u679c\u51fd\u6570\u5728\u8fd4\u56de\u524d\u7684\u6700\u540e\u4e00\u6b65\u624d\u8fdb\u884c\u9012\u5f52\u8c03\u7528\uff0c\u5219\u8be5\u51fd\u6570\u53ef\u4ee5\u88ab\u7f16\u8bd1\u5668\u6216\u89e3\u91ca\u5668\u4f18\u5316\uff0c\u4f7f\u5176\u5728\u7a7a\u95f4\u6548\u7387\u4e0a\u4e0e\u8fed\u4ee3\u76f8\u5f53\u3002\u8fd9\u79cd\u60c5\u51b5\u88ab\u79f0\u4e3a\u300c\u5c3e\u9012\u5f52 tail recursion\u300d\u3002

  • \u666e\u901a\u9012\u5f52\uff1a\u5f53\u51fd\u6570\u8fd4\u56de\u5230\u4e0a\u4e00\u5c42\u7ea7\u7684\u51fd\u6570\u540e\uff0c\u9700\u8981\u7ee7\u7eed\u6267\u884c\u4ee3\u7801\uff0c\u56e0\u6b64\u7cfb\u7edf\u9700\u8981\u4fdd\u5b58\u4e0a\u4e00\u5c42\u8c03\u7528\u7684\u4e0a\u4e0b\u6587\u3002
  • \u5c3e\u9012\u5f52\uff1a\u9012\u5f52\u8c03\u7528\u662f\u51fd\u6570\u8fd4\u56de\u524d\u7684\u6700\u540e\u4e00\u4e2a\u64cd\u4f5c\uff0c\u8fd9\u610f\u5473\u7740\u51fd\u6570\u8fd4\u56de\u5230\u4e0a\u4e00\u5c42\u7ea7\u540e\uff0c\u65e0\u9700\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\uff0c\u56e0\u6b64\u7cfb\u7edf\u65e0\u9700\u4fdd\u5b58\u4e0a\u4e00\u5c42\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u3002

\u4ee5\u8ba1\u7b97 \\(1 + 2 + \\dots + n\\) \u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u7ed3\u679c\u53d8\u91cf res \u8bbe\u4e3a\u51fd\u6570\u53c2\u6570\uff0c\u4ece\u800c\u5b9e\u73b0\u5c3e\u9012\u5f52\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig recursion.py
def tail_recur(n, res):\n\"\"\"\u5c3e\u9012\u5f52\"\"\"\n# \u7ec8\u6b62\u6761\u4ef6\nif n == 0:\nreturn res\n# \u5c3e\u9012\u5f52\u8c03\u7528\nreturn tail_recur(n - 1, res + n)\n
recursion.cpp
/* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n == 0)\nreturn res;\n// \u5c3e\u9012\u5f52\u8c03\u7528\nreturn tailRecur(n - 1, res + n);\n}\n
recursion.java
/* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n == 0)\nreturn res;\n// \u5c3e\u9012\u5f52\u8c03\u7528\nreturn tailRecur(n - 1, res + n);\n}\n
recursion.cs
/* \u5c3e\u9012\u5f52 */\nint TailRecur(int n, int res) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n == 0)\nreturn res;\n// \u5c3e\u9012\u5f52\u8c03\u7528\nreturn TailRecur(n - 1, res + n);\n}\n
recursion.go
/* \u5c3e\u9012\u5f52 */\nfunc tailRecur(n int, res int) int {\n// \u7ec8\u6b62\u6761\u4ef6\nif n == 0 {\nreturn res\n}\n// \u5c3e\u9012\u5f52\u8c03\u7528\nreturn tailRecur(n-1, res+n)\n}\n
recursion.swift
/* \u5c3e\u9012\u5f52 */\nfunc tailRecur(n: Int, res: Int) -> Int {\n// \u7ec8\u6b62\u6761\u4ef6\nif n == 0 {\nreturn res\n}\n// \u5c3e\u9012\u5f52\u8c03\u7528\nreturn tailRecur(n: n - 1, res: res + n)\n}\n
recursion.js
/* \u5c3e\u9012\u5f52 */\nfunction tailRecur(n, res) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n === 0) return res;\n// \u5c3e\u9012\u5f52\u8c03\u7528\nreturn tailRecur(n - 1, res + n);\n}\n
recursion.ts
/* \u5c3e\u9012\u5f52 */\nfunction tailRecur(n: number, res: number): number {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n === 0) return res;\n// \u5c3e\u9012\u5f52\u8c03\u7528\nreturn tailRecur(n - 1, res + n);\n}\n
recursion.dart
/* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n == 0) return res;\n// \u5c3e\u9012\u5f52\u8c03\u7528\nreturn tailRecur(n - 1, res + n);\n}\n
recursion.rs
/* \u5c3e\u9012\u5f52 */\nfn tail_recur(n: i32, res: i32) -> i32 {\n// \u7ec8\u6b62\u6761\u4ef6\nif n == 0 {\nreturn res;\n}\n// \u5c3e\u9012\u5f52\u8c03\u7528\ntail_recur(n - 1, res + n)\n}\n
recursion.c
/* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n == 0)\nreturn res;\n// \u5c3e\u9012\u5f52\u8c03\u7528\nreturn tailRecur(n - 1, res + n);\n}\n
recursion.zig
[class]{}-[func]{tailRecur}\n

\u5c3e\u9012\u5f52\u7684\u6267\u884c\u8fc7\u7a0b\u5982\u56fe 2-5 \u6240\u793a\u3002\u5bf9\u6bd4\u666e\u901a\u9012\u5f52\u548c\u5c3e\u9012\u5f52\uff0c\u6c42\u548c\u64cd\u4f5c\u7684\u6267\u884c\u70b9\u662f\u4e0d\u540c\u7684\u3002

  • \u666e\u901a\u9012\u5f52\uff1a\u6c42\u548c\u64cd\u4f5c\u662f\u5728\u201c\u5f52\u201d\u7684\u8fc7\u7a0b\u4e2d\u6267\u884c\u7684\uff0c\u6bcf\u5c42\u8fd4\u56de\u540e\u90fd\u8981\u518d\u6267\u884c\u4e00\u6b21\u6c42\u548c\u64cd\u4f5c\u3002
  • \u5c3e\u9012\u5f52\uff1a\u6c42\u548c\u64cd\u4f5c\u662f\u5728\u201c\u9012\u201d\u7684\u8fc7\u7a0b\u4e2d\u6267\u884c\u7684\uff0c\u201c\u5f52\u201d\u7684\u8fc7\u7a0b\u53ea\u9700\u5c42\u5c42\u8fd4\u56de\u3002

\u56fe 2-5 \u00a0 \u5c3e\u9012\u5f52\u8fc7\u7a0b

Tip

\u8bf7\u6ce8\u610f\uff0c\u8bb8\u591a\u7f16\u8bd1\u5668\u6216\u89e3\u91ca\u5668\u5e76\u4e0d\u652f\u6301\u5c3e\u9012\u5f52\u4f18\u5316\u3002\u4f8b\u5982\uff0cPython \u9ed8\u8ba4\u4e0d\u652f\u6301\u5c3e\u9012\u5f52\u4f18\u5316\uff0c\u56e0\u6b64\u5373\u4f7f\u51fd\u6570\u662f\u5c3e\u9012\u5f52\u5f62\u5f0f\uff0c\u4f46\u4ecd\u7136\u53ef\u80fd\u4f1a\u9047\u5230\u6808\u6ea2\u51fa\u95ee\u9898\u3002

"},{"location":"chapter_computational_complexity/iteration_and_recursion/#3_1","title":"3. \u00a0 \u9012\u5f52\u6811","text":"

\u5f53\u5904\u7406\u4e0e\u201c\u5206\u6cbb\u201d\u76f8\u5173\u7684\u7b97\u6cd5\u95ee\u9898\u65f6\uff0c\u9012\u5f52\u5f80\u5f80\u6bd4\u8fed\u4ee3\u7684\u601d\u8def\u66f4\u52a0\u76f4\u89c2\u3001\u4ee3\u7801\u66f4\u52a0\u6613\u8bfb\u3002\u4ee5\u201c\u6590\u6ce2\u90a3\u5951\u6570\u5217\u201d\u4e3a\u4f8b\u3002

Question

\u7ed9\u5b9a\u4e00\u4e2a\u6590\u6ce2\u90a3\u5951\u6570\u5217 \\(0, 1, 1, 2, 3, 5, 8, 13, \\dots\\) \uff0c\u6c42\u8be5\u6570\u5217\u7684\u7b2c \\(n\\) \u4e2a\u6570\u5b57\u3002

\u8bbe\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u7b2c \\(n\\) \u4e2a\u6570\u5b57\u4e3a \\(f(n)\\) \uff0c\u6613\u5f97\u4e24\u4e2a\u7ed3\u8bba\u3002

  • \u6570\u5217\u7684\u524d\u4e24\u4e2a\u6570\u5b57\u4e3a \\(f(1) = 0\\) \u548c \\(f(2) = 1\\) \u3002
  • \u6570\u5217\u4e2d\u7684\u6bcf\u4e2a\u6570\u5b57\u662f\u524d\u4e24\u4e2a\u6570\u5b57\u7684\u548c\uff0c\u5373 \\(f(n) = f(n - 1) + f(n - 2)\\) \u3002

\u6309\u7167\u9012\u63a8\u5173\u7cfb\u8fdb\u884c\u9012\u5f52\u8c03\u7528\uff0c\u5c06\u524d\u4e24\u4e2a\u6570\u5b57\u4f5c\u4e3a\u7ec8\u6b62\u6761\u4ef6\uff0c\u4fbf\u53ef\u5199\u51fa\u9012\u5f52\u4ee3\u7801\u3002\u8c03\u7528 fib(n) \u5373\u53ef\u5f97\u5230\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u7b2c \\(n\\) \u4e2a\u6570\u5b57\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig recursion.py
def fib(n: int) -> int:\n\"\"\"\u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52\"\"\"\n# \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\nif n == 1 or n == 2:\nreturn n - 1\n# \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\nres = fib(n - 1) + fib(n - 2)\n# \u8fd4\u56de\u7ed3\u679c f(n)\nreturn res\n
recursion.cpp
/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n// \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\nif (n == 1 || n == 2)\nreturn n - 1;\n// \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\nint res = fib(n - 1) + fib(n - 2);\n// \u8fd4\u56de\u7ed3\u679c f(n)\nreturn res;\n}\n
recursion.java
/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n// \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\nif (n == 1 || n == 2)\nreturn n - 1;\n// \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\nint res = fib(n - 1) + fib(n - 2);\n// \u8fd4\u56de\u7ed3\u679c f(n)\nreturn res;\n}\n
recursion.cs
/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint Fib(int n) {\n// \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\nif (n == 1 || n == 2)\nreturn n - 1;\n// \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\nint res = Fib(n - 1) + Fib(n - 2);\n// \u8fd4\u56de\u7ed3\u679c f(n)\nreturn res;\n}\n
recursion.go
/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunc fib(n int) int {\n// \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\nif n == 1 || n == 2 {\nreturn n - 1\n}\n// \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\nres := fib(n-1) + fib(n-2)\n// \u8fd4\u56de\u7ed3\u679c f(n)\nreturn res\n}\n
recursion.swift
/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunc fib(n: Int) -> Int {\n// \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\nif n == 1 || n == 2 {\nreturn n - 1\n}\n// \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\nlet res = fib(n: n - 1) + fib(n: n - 2)\n// \u8fd4\u56de\u7ed3\u679c f(n)\nreturn res\n}\n
recursion.js
/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunction fib(n) {\n// \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\nif (n === 1 || n === 2) return n - 1;\n// \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\nconst res = fib(n - 1) + fib(n - 2);\n// \u8fd4\u56de\u7ed3\u679c f(n)\nreturn res;\n}\n
recursion.ts
/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunction fib(n: number): number {\n// \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\nif (n === 1 || n === 2) return n - 1;\n// \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\nconst res = fib(n - 1) + fib(n - 2);\n// \u8fd4\u56de\u7ed3\u679c f(n)\nreturn res;\n}\n
recursion.dart
/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n// \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\nif (n == 1 || n == 2) return n - 1;\n// \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\nint res = fib(n - 1) + fib(n - 2);\n// \u8fd4\u56de\u7ed3\u679c f(n)\nreturn res;\n}\n
recursion.rs
/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfn fib(n: i32) -> i32 {\n// \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\nif n == 1 || n == 2 {\nreturn n - 1;\n}\n// \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\nlet res = fib(n - 1) + fib(n - 2);\n// \u8fd4\u56de\u7ed3\u679c\nres\n}\n
recursion.c
/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n// \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\nif (n == 1 || n == 2)\nreturn n - 1;\n// \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\nint res = fib(n - 1) + fib(n - 2);\n// \u8fd4\u56de\u7ed3\u679c f(n)\nreturn res;\n}\n
recursion.zig
[class]{}-[func]{fib}\n

\u89c2\u5bdf\u4ee5\u4e0a\u4ee3\u7801\uff0c\u6211\u4eec\u5728\u51fd\u6570\u5185\u9012\u5f52\u8c03\u7528\u4e86\u4e24\u4e2a\u51fd\u6570\uff0c\u8fd9\u610f\u5473\u7740\u4ece\u4e00\u4e2a\u8c03\u7528\u4ea7\u751f\u4e86\u4e24\u4e2a\u8c03\u7528\u5206\u652f\u3002\u5982\u56fe 2-6 \u6240\u793a\uff0c\u8fd9\u6837\u4e0d\u65ad\u9012\u5f52\u8c03\u7528\u4e0b\u53bb\uff0c\u6700\u7ec8\u5c06\u4ea7\u751f\u4e00\u4e2a\u5c42\u6570\u4e3a \\(n\\) \u7684\u300c\u9012\u5f52\u6811 recursion tree\u300d\u3002

\u56fe 2-6 \u00a0 \u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u9012\u5f52\u6811

\u672c\u8d28\u4e0a\u770b\uff0c\u9012\u5f52\u4f53\u73b0\u201c\u5c06\u95ee\u9898\u5206\u89e3\u4e3a\u66f4\u5c0f\u5b50\u95ee\u9898\u201d\u7684\u601d\u7ef4\u8303\u5f0f\uff0c\u8fd9\u79cd\u5206\u6cbb\u7b56\u7565\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002

  • \u4ece\u7b97\u6cd5\u89d2\u5ea6\u770b\uff0c\u641c\u7d22\u3001\u6392\u5e8f\u3001\u56de\u6eaf\u3001\u5206\u6cbb\u3001\u52a8\u6001\u89c4\u5212\u7b49\u8bb8\u591a\u91cd\u8981\u7b97\u6cd5\u7b56\u7565\u90fd\u76f4\u63a5\u6216\u95f4\u63a5\u5730\u5e94\u7528\u8fd9\u79cd\u601d\u7ef4\u65b9\u5f0f\u3002
  • \u4ece\u6570\u636e\u7ed3\u6784\u89d2\u5ea6\u770b\uff0c\u9012\u5f52\u5929\u7136\u9002\u5408\u5904\u7406\u94fe\u8868\u3001\u6811\u548c\u56fe\u7684\u76f8\u5173\u95ee\u9898\uff0c\u56e0\u4e3a\u5b83\u4eec\u975e\u5e38\u9002\u5408\u7528\u5206\u6cbb\u601d\u60f3\u8fdb\u884c\u5206\u6790\u3002
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#223","title":"2.2.3 \u00a0 \u4e24\u8005\u5bf9\u6bd4","text":"

\u603b\u7ed3\u4ee5\u4e0a\u5185\u5bb9\uff0c\u5982\u8868 2-1 \u6240\u793a\uff0c\u8fed\u4ee3\u548c\u9012\u5f52\u5728\u5b9e\u73b0\u3001\u6027\u80fd\u548c\u9002\u7528\u6027\u4e0a\u6709\u6240\u4e0d\u540c\u3002

\u8868 2-1 \u00a0 \u8fed\u4ee3\u4e0e\u9012\u5f52\u7279\u70b9\u5bf9\u6bd4

\u8fed\u4ee3 \u9012\u5f52 \u5b9e\u73b0\u65b9\u5f0f \u5faa\u73af\u7ed3\u6784 \u51fd\u6570\u8c03\u7528\u81ea\u8eab \u65f6\u95f4\u6548\u7387 \u6548\u7387\u901a\u5e38\u8f83\u9ad8\uff0c\u65e0\u51fd\u6570\u8c03\u7528\u5f00\u9500 \u6bcf\u6b21\u51fd\u6570\u8c03\u7528\u90fd\u4f1a\u4ea7\u751f\u5f00\u9500 \u5185\u5b58\u4f7f\u7528 \u901a\u5e38\u4f7f\u7528\u56fa\u5b9a\u5927\u5c0f\u7684\u5185\u5b58\u7a7a\u95f4 \u7d2f\u79ef\u51fd\u6570\u8c03\u7528\u53ef\u80fd\u4f7f\u7528\u5927\u91cf\u7684\u6808\u5e27\u7a7a\u95f4 \u9002\u7528\u95ee\u9898 \u9002\u7528\u4e8e\u7b80\u5355\u5faa\u73af\u4efb\u52a1\uff0c\u4ee3\u7801\u76f4\u89c2\u3001\u53ef\u8bfb\u6027\u597d \u9002\u7528\u4e8e\u5b50\u95ee\u9898\u5206\u89e3\uff0c\u5982\u6811\u3001\u56fe\u3001\u5206\u6cbb\u3001\u56de\u6eaf\u7b49\uff0c\u4ee3\u7801\u7ed3\u6784\u7b80\u6d01\u3001\u6e05\u6670

Tip

\u5982\u679c\u611f\u89c9\u4ee5\u4e0b\u5185\u5bb9\u7406\u89e3\u56f0\u96be\uff0c\u53ef\u4ee5\u5728\u8bfb\u5b8c\u201c\u6808\u201d\u7ae0\u8282\u540e\u518d\u6765\u590d\u4e60\u3002

\u90a3\u4e48\uff0c\u8fed\u4ee3\u548c\u9012\u5f52\u5177\u6709\u4ec0\u4e48\u5185\u5728\u8054\u7cfb\u5462\uff1f\u4ee5\u4e0a\u8ff0\u7684\u9012\u5f52\u51fd\u6570\u4e3a\u4f8b\uff0c\u6c42\u548c\u64cd\u4f5c\u5728\u9012\u5f52\u7684\u201c\u5f52\u201d\u9636\u6bb5\u8fdb\u884c\u3002\u8fd9\u610f\u5473\u7740\u6700\u521d\u88ab\u8c03\u7528\u7684\u51fd\u6570\u5b9e\u9645\u4e0a\u662f\u6700\u540e\u5b8c\u6210\u5176\u6c42\u548c\u64cd\u4f5c\u7684\uff0c\u8fd9\u79cd\u5de5\u4f5c\u673a\u5236\u4e0e\u6808\u7684\u201c\u5148\u5165\u540e\u51fa\u201d\u539f\u5219\u662f\u5f02\u66f2\u540c\u5de5\u7684\u3002

\u4e8b\u5b9e\u4e0a\uff0c\u201c\u8c03\u7528\u6808\u201d\u548c\u201c\u6808\u5e27\u7a7a\u95f4\u201d\u8fd9\u7c7b\u9012\u5f52\u672f\u8bed\u5df2\u7ecf\u6697\u793a\u4e86\u9012\u5f52\u4e0e\u6808\u4e4b\u95f4\u7684\u5bc6\u5207\u5173\u7cfb\u3002

  1. \u9012\uff1a\u5f53\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u7cfb\u7edf\u4f1a\u5728\u201c\u8c03\u7528\u6808\u201d\u4e0a\u4e3a\u8be5\u51fd\u6570\u5206\u914d\u65b0\u7684\u6808\u5e27\uff0c\u7528\u4e8e\u5b58\u50a8\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\u3001\u53c2\u6570\u3001\u8fd4\u56de\u5730\u5740\u7b49\u6570\u636e\u3002
  2. \u5f52\uff1a\u5f53\u51fd\u6570\u5b8c\u6210\u6267\u884c\u5e76\u8fd4\u56de\u65f6\uff0c\u5bf9\u5e94\u7684\u6808\u5e27\u4f1a\u4ece\u201c\u8c03\u7528\u6808\u201d\u4e0a\u88ab\u79fb\u9664\uff0c\u6062\u590d\u4e4b\u524d\u51fd\u6570\u7684\u6267\u884c\u73af\u5883\u3002

\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u8c03\u7528\u6808\u7684\u884c\u4e3a\uff0c\u4ece\u800c\u5c06\u9012\u5f52\u8f6c\u5316\u4e3a\u8fed\u4ee3\u5f62\u5f0f\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig recursion.py
def for_loop_recur(n: int) -> int:\n\"\"\"\u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52\"\"\"\n# \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\nstack = []\nres = 0\n# \u9012\uff1a\u9012\u5f52\u8c03\u7528\nfor i in range(n, 0, -1):\n# \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\nstack.append(i)\n# \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nwhile stack:\n# \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\nres += stack.pop()\n# res = 1+2+3+...+n\nreturn res\n
recursion.cpp
/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n// \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\nstack<int> stack;\nint res = 0;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nfor (int i = n; i > 0; i--) {\n// \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\nstack.push(i);\n}\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nwhile (!stack.empty()) {\n// \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\nres += stack.top();\nstack.pop();\n}\n// res = 1+2+3+...+n\nreturn res;\n}\n
recursion.java
/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n// \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\nStack<Integer> stack = new Stack<>();\nint res = 0;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nfor (int i = n; i > 0; i--) {\n// \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\nstack.push(i);\n}\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nwhile (!stack.isEmpty()) {\n// \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\nres += stack.pop();\n}\n// res = 1+2+3+...+n\nreturn res;\n}\n
recursion.cs
/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint ForLoopRecur(int n) {\n// \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\nStack<int> stack = new();\nint res = 0;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nfor (int i = n; i > 0; i--) {\n// \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\nstack.Push(i);\n}\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nwhile (stack.Count > 0) {\n// \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\nres += stack.Pop();\n}\n// res = 1+2+3+...+n\nreturn res;\n}\n
recursion.go
/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunc forLoopRecur(n int) int {\n// \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\nstack := list.New()\nres := 0\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nfor i := n; i > 0; i-- {\n// \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\nstack.PushBack(i)\n}\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nfor stack.Len() != 0 {\n// \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\nres += stack.Back().Value.(int)\nstack.Remove(stack.Back())\n}\n// res = 1+2+3+...+n\nreturn res\n}\n
recursion.swift
/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunc forLoopRecur(n: Int) -> Int {\n// \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\nvar stack: [Int] = []\nvar res = 0\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nfor i in stride(from: n, to: 0, by: -1) {\n// \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\nstack.append(i)\n}\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nwhile !stack.isEmpty {\n// \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\nres += stack.removeLast()\n}\n// res = 1+2+3+...+n\nreturn res\n}\n
recursion.js
/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunction forLoopRecur(n) {\n// \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\nconst stack = [];\nlet res = 0;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nfor (let i = 1; i <= n; i++) {\n// \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\nstack.push(i);\n}\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nwhile (stack.length) { // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\nres += stack.pop();\n}\n// res = 1+2+3+...+n\nreturn res;\n}\n
recursion.ts
/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunction forLoopRecur(n: number): number {\n// \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808 \nconst stack: number[] = [];\nlet res: number = 0;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nfor (let i = 1; i <= n; i++) {\n// \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\nstack.push(i);\n}\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nwhile (stack.length) { // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\nres += stack.pop();\n}\n// res = 1+2+3+...+n\nreturn res;\n}\n
recursion.dart
/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n// \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\nList<int> stack = [];\nint res = 0;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nfor (int i = n; i > 0; i--) {\n// \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\nstack.add(i);\n}\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nwhile (!stack.isEmpty) {\n// \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\nres += stack.removeLast();\n}\n// res = 1+2+3+...+n\nreturn res;\n}\n
recursion.rs
/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfn for_loop_recur(n: i32) -> i32 {\n// \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\nlet mut stack = Vec::new();\nlet mut res = 0;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nfor i in (1..=n).rev() {\n// \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\nstack.push(i);\n}\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nwhile !stack.is_empty() {\n// \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\nres += stack.pop().unwrap();\n}\n// res = 1+2+3+...+n\nres\n}\n
recursion.c
[class]{}-[func]{forLoopRecur}\n
recursion.zig
[class]{}-[func]{forLoopRecur}\n

\u89c2\u5bdf\u4ee5\u4e0a\u4ee3\u7801\uff0c\u5f53\u9012\u5f52\u88ab\u8f6c\u6362\u4e3a\u8fed\u4ee3\u540e\uff0c\u4ee3\u7801\u53d8\u5f97\u66f4\u52a0\u590d\u6742\u4e86\u3002\u5c3d\u7ba1\u8fed\u4ee3\u548c\u9012\u5f52\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4e92\u76f8\u8f6c\u6362\uff0c\u4f46\u4e5f\u4e0d\u4e00\u5b9a\u503c\u5f97\u8fd9\u6837\u505a\uff0c\u6709\u4ee5\u4e0b\u4e24\u70b9\u539f\u56e0\u3002

  • \u8f6c\u5316\u540e\u7684\u4ee3\u7801\u53ef\u80fd\u66f4\u52a0\u96be\u4ee5\u7406\u89e3\uff0c\u53ef\u8bfb\u6027\u66f4\u5dee\u3002
  • \u5bf9\u4e8e\u67d0\u4e9b\u590d\u6742\u95ee\u9898\uff0c\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\u7684\u884c\u4e3a\u53ef\u80fd\u975e\u5e38\u56f0\u96be\u3002

\u603b\u4e4b\uff0c\u9009\u62e9\u8fed\u4ee3\u8fd8\u662f\u9012\u5f52\u53d6\u51b3\u4e8e\u7279\u5b9a\u95ee\u9898\u7684\u6027\u8d28\u3002\u5728\u7f16\u7a0b\u5b9e\u8df5\u4e2d\uff0c\u6743\u8861\u4e24\u8005\u7684\u4f18\u52a3\u5e76\u6839\u636e\u60c5\u5883\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002

"},{"location":"chapter_computational_complexity/performance_evaluation/","title":"2.1 \u00a0 \u7b97\u6cd5\u6548\u7387\u8bc4\u4f30","text":"

\u5728\u7b97\u6cd5\u8bbe\u8ba1\u4e2d\uff0c\u6211\u4eec\u5148\u540e\u8ffd\u6c42\u4ee5\u4e0b\u4e24\u4e2a\u5c42\u9762\u7684\u76ee\u6807\u3002

  1. \u627e\u5230\u95ee\u9898\u89e3\u6cd5\uff1a\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\uff1a\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

\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u80fd\u591f\u89e3\u51b3\u95ee\u9898\u7684\u524d\u63d0\u4e0b\uff0c\u7b97\u6cd5\u6548\u7387\u5df2\u6210\u4e3a\u8861\u91cf\u7b97\u6cd5\u4f18\u52a3\u7684\u4e3b\u8981\u8bc4\u4ef7\u6307\u6807\uff0c\u5b83\u5305\u62ec\u4ee5\u4e0b\u4e24\u4e2a\u7ef4\u5ea6\u3002

  • \u65f6\u95f4\u6548\u7387\uff1a\u7b97\u6cd5\u8fd0\u884c\u901f\u5ea6\u7684\u5feb\u6162\u3002
  • \u7a7a\u95f4\u6548\u7387\uff1a\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\u800c\u6709\u6548\u5730\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\u81f3\u5173\u91cd\u8981\uff0c\u56e0\u4e3a\u53ea\u6709\u8fd9\u6837\u6211\u4eec\u624d\u80fd\u5c06\u5404\u79cd\u7b97\u6cd5\u8fdb\u884c\u5bf9\u6bd4\uff0c\u4ece\u800c\u6307\u5bfc\u7b97\u6cd5\u8bbe\u8ba1\u4e0e\u4f18\u5316\u8fc7\u7a0b\u3002

\u6548\u7387\u8bc4\u4f30\u65b9\u6cd5\u4e3b\u8981\u5206\u4e3a\u4e24\u79cd\uff1a\u5b9e\u9645\u6d4b\u8bd5\u3001\u7406\u8bba\u4f30\u7b97\u3002

"},{"location":"chapter_computational_complexity/performance_evaluation/#211","title":"2.1.1 \u00a0 \u5b9e\u9645\u6d4b\u8bd5","text":"

\u5047\u8bbe\u6211\u4eec\u73b0\u5728\u6709\u7b97\u6cd5 A \u548c\u7b97\u6cd5 B \uff0c\u5b83\u4eec\u90fd\u80fd\u89e3\u51b3\u540c\u4e00\u95ee\u9898\uff0c\u73b0\u5728\u9700\u8981\u5bf9\u6bd4\u8fd9\u4e24\u4e2a\u7b97\u6cd5\u7684\u6548\u7387\u3002\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u627e\u4e00\u53f0\u8ba1\u7b97\u673a\uff0c\u8fd0\u884c\u8fd9\u4e24\u4e2a\u7b97\u6cd5\uff0c\u5e76\u76d1\u63a7\u8bb0\u5f55\u5b83\u4eec\u7684\u8fd0\u884c\u65f6\u95f4\u548c\u5185\u5b58\u5360\u7528\u60c5\u51b5\u3002\u8fd9\u79cd\u8bc4\u4f30\u65b9\u5f0f\u80fd\u591f\u53cd\u6620\u771f\u5b9e\u60c5\u51b5\uff0c\u4f46\u4e5f\u5b58\u5728\u8f83\u5927\u5c40\u9650\u6027\u3002

\u4e00\u65b9\u9762\uff0c\u96be\u4ee5\u6392\u9664\u6d4b\u8bd5\u73af\u5883\u7684\u5e72\u6270\u56e0\u7d20\u3002\u786c\u4ef6\u914d\u7f6e\u4f1a\u5f71\u54cd\u7b97\u6cd5\u7684\u6027\u80fd\u8868\u73b0\u3002\u6bd4\u5982\u5728\u67d0\u53f0\u8ba1\u7b97\u673a\u4e2d\uff0c\u7b97\u6cd5 A \u7684\u8fd0\u884c\u65f6\u95f4\u6bd4\u7b97\u6cd5 B \u77ed\uff1b\u4f46\u5728\u53e6\u4e00\u53f0\u914d\u7f6e\u4e0d\u540c\u7684\u8ba1\u7b97\u673a\u4e2d\uff0c\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\u7edf\u8ba1\u5e73\u5747\u6548\u7387\uff0c\u800c\u8fd9\u662f\u4e0d\u73b0\u5b9e\u7684\u3002

\u53e6\u4e00\u65b9\u9762\uff0c\u5c55\u5f00\u5b8c\u6574\u6d4b\u8bd5\u975e\u5e38\u8017\u8d39\u8d44\u6e90\u3002\u968f\u7740\u8f93\u5165\u6570\u636e\u91cf\u7684\u53d8\u5316\uff0c\u7b97\u6cd5\u4f1a\u8868\u73b0\u51fa\u4e0d\u540c\u7684\u6548\u7387\u3002\u4f8b\u5982\uff0c\u5728\u8f93\u5165\u6570\u636e\u91cf\u8f83\u5c0f\u65f6\uff0c\u7b97\u6cd5 A \u7684\u8fd0\u884c\u65f6\u95f4\u6bd4\u7b97\u6cd5 B \u66f4\u5c11\uff1b\u800c\u8f93\u5165\u6570\u636e\u91cf\u8f83\u5927\u65f6\uff0c\u6d4b\u8bd5\u7ed3\u679c\u53ef\u80fd\u6070\u6070\u76f8\u53cd\u3002\u56e0\u6b64\uff0c\u4e3a\u4e86\u5f97\u5230\u6709\u8bf4\u670d\u529b\u7684\u7ed3\u8bba\uff0c\u6211\u4eec\u9700\u8981\u6d4b\u8bd5\u5404\u79cd\u89c4\u6a21\u7684\u8f93\u5165\u6570\u636e\uff0c\u800c\u8fd9\u9700\u8981\u8017\u8d39\u5927\u91cf\u7684\u8ba1\u7b97\u8d44\u6e90\u3002

"},{"location":"chapter_computational_complexity/performance_evaluation/#212","title":"2.1.2 \u00a0 \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\u6e10\u8fd1\u590d\u6742\u5ea6\u5206\u6790 asymptotic complexity analysis\u300d\uff0c\u7b80\u79f0\u300c\u590d\u6742\u5ea6\u5206\u6790\u300d\u3002

\u590d\u6742\u5ea6\u5206\u6790\u4f53\u73b0\u7b97\u6cd5\u8fd0\u884c\u6240\u9700\u7684\u65f6\u95f4\uff08\u7a7a\u95f4\uff09\u8d44\u6e90\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u5b83\u63cf\u8ff0\u4e86\u968f\u7740\u8f93\u5165\u6570\u636e\u5927\u5c0f\u7684\u589e\u52a0\uff0c\u7b97\u6cd5\u6267\u884c\u6240\u9700\u65f6\u95f4\u548c\u7a7a\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u3002\u8fd9\u4e2a\u5b9a\u4e49\u6709\u4e9b\u62d7\u53e3\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u5206\u4e3a\u4e09\u4e2a\u91cd\u70b9\u6765\u7406\u89e3\u3002

  • \u201c\u65f6\u95f4\u548c\u7a7a\u95f4\u8d44\u6e90\u201d\u5206\u522b\u5bf9\u5e94\u300c\u65f6\u95f4\u590d\u6742\u5ea6 time complexity\u300d\u548c\u300c\u7a7a\u95f4\u590d\u6742\u5ea6 space complexity\u300d\u3002
  • \u201c\u968f\u7740\u8f93\u5165\u6570\u636e\u5927\u5c0f\u7684\u589e\u52a0\u201d\u610f\u5473\u7740\u590d\u6742\u5ea6\u53cd\u6620\u4e86\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u4e0e\u8f93\u5165\u6570\u636e\u4f53\u91cf\u4e4b\u95f4\u7684\u5173\u7cfb\u3002
  • \u201c\u65f6\u95f4\u548c\u7a7a\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u201d\u8868\u793a\u590d\u6742\u5ea6\u5206\u6790\u5173\u6ce8\u7684\u4e0d\u662f\u8fd0\u884c\u65f6\u95f4\u6216\u5360\u7528\u7a7a\u95f4\u7684\u5177\u4f53\u503c\uff0c\u800c\u662f\u65f6\u95f4\u6216\u7a7a\u95f4\u589e\u957f\u7684\u201c\u5feb\u6162\u201d\u3002

\u590d\u6742\u5ea6\u5206\u6790\u514b\u670d\u4e86\u5b9e\u9645\u6d4b\u8bd5\u65b9\u6cd5\u7684\u5f0a\u7aef\uff0c\u4f53\u73b0\u5728\u4ee5\u4e0b\u4e24\u4e2a\u65b9\u9762\u3002

  • \u5b83\u72ec\u7acb\u4e8e\u6d4b\u8bd5\u73af\u5883\uff0c\u5206\u6790\u7ed3\u679c\u9002\u7528\u4e8e\u6240\u6709\u8fd0\u884c\u5e73\u53f0\u3002
  • \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

Tip

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

\u590d\u6742\u5ea6\u5206\u6790\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u628a\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\u7684\u201c\u6807\u5c3a\u201d\uff0c\u4f7f\u6211\u4eec\u53ef\u4ee5\u8861\u91cf\u6267\u884c\u67d0\u4e2a\u7b97\u6cd5\u6240\u9700\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u8d44\u6e90\uff0c\u5bf9\u6bd4\u4e0d\u540c\u7b97\u6cd5\u4e4b\u95f4\u7684\u6548\u7387\u3002

\u590d\u6742\u5ea6\u662f\u4e2a\u6570\u5b66\u6982\u5ff5\uff0c\u5bf9\u4e8e\u521d\u5b66\u8005\u53ef\u80fd\u6bd4\u8f83\u62bd\u8c61\uff0c\u5b66\u4e60\u96be\u5ea6\u76f8\u5bf9\u8f83\u9ad8\u3002\u4ece\u8fd9\u4e2a\u89d2\u5ea6\u770b\uff0c\u590d\u6742\u5ea6\u5206\u6790\u53ef\u80fd\u4e0d\u592a\u9002\u5408\u4f5c\u4e3a\u6700\u5148\u4ecb\u7ecd\u7684\u5185\u5bb9\u3002\u7136\u800c\uff0c\u5f53\u6211\u4eec\u8ba8\u8bba\u67d0\u4e2a\u6570\u636e\u7ed3\u6784\u6216\u7b97\u6cd5\u7684\u7279\u70b9\u65f6\uff0c\u96be\u4ee5\u907f\u514d\u8981\u5206\u6790\u5176\u8fd0\u884c\u901f\u5ea6\u548c\u7a7a\u95f4\u4f7f\u7528\u60c5\u51b5\u3002

\u7efc\u4e0a\u6240\u8ff0\uff0c\u5efa\u8bae\u4f60\u5728\u6df1\u5165\u5b66\u4e60\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u4e4b\u524d\uff0c\u5148\u5bf9\u590d\u6742\u5ea6\u5206\u6790\u5efa\u7acb\u521d\u6b65\u7684\u4e86\u89e3\uff0c\u4ee5\u4fbf\u80fd\u591f\u5b8c\u6210\u7b80\u5355\u7b97\u6cd5\u7684\u590d\u6742\u5ea6\u5206\u6790\u3002

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

\u300c\u7a7a\u95f4\u590d\u6742\u5ea6 space complexity\u300d\u7528\u4e8e\u8861\u91cf\u7b97\u6cd5\u5360\u7528\u5185\u5b58\u7a7a\u95f4\u968f\u7740\u6570\u636e\u91cf\u53d8\u5927\u65f6\u7684\u589e\u957f\u8d8b\u52bf\u3002\u8fd9\u4e2a\u6982\u5ff5\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u975e\u5e38\u7c7b\u4f3c\uff0c\u53ea\u9700\u5c06\u201c\u8fd0\u884c\u65f6\u95f4\u201d\u66ff\u6362\u4e3a\u201c\u5360\u7528\u5185\u5b58\u7a7a\u95f4\u201d\u3002

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

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

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

\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u7edf\u8ba1\u8303\u56f4\u662f\u201c\u6682\u5b58\u7a7a\u95f4\u201d\u52a0\u4e0a\u201c\u8f93\u51fa\u7a7a\u95f4\u201d\u3002

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

  • \u6682\u5b58\u6570\u636e\uff1a\u7528\u4e8e\u4fdd\u5b58\u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u5404\u79cd\u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u7b49\u3002
  • \u6808\u5e27\u7a7a\u95f4\uff1a\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
  • \u6307\u4ee4\u7a7a\u95f4\uff1a\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

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

\u56fe 2-15 \u00a0 \u7b97\u6cd5\u4f7f\u7528\u7684\u76f8\u5173\u7a7a\u95f4

PythonC++JavaC#GoSwiftJSTSDartRustCZig
class Node:\n\"\"\"\u7c7b\"\"\"\ndef __init__(self, x: int):\nself.val: int = x              # \u8282\u70b9\u503c\nself.next: Node | None = None  # \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\ndef function() -> int:\n\"\"\"\u51fd\u6570\"\"\"\n# \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\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 */\nstruct Node {\nint val;\nNode *next;\nNode(int x) : val(x), next(nullptr) {}\n};\n/* \u51fd\u6570 */\nint func() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\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
/* \u7c7b */\nclass Node {\nint val;\nNode next;\nNode(int x) { val = x; }\n}\n/* \u51fd\u6570 */\nint function() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\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
/* \u7c7b */\nclass Node {\nint val;\nNode next;\nNode(int x) { val = x; }\n}\n/* \u51fd\u6570 */\nint Function() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\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(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 */\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// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\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 {\nvar val: Int\nvar next: Node?\ninit(x: Int) {\nval = x\n}\n}\n/* \u51fd\u6570 */\nfunc function() -> Int {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\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
/* \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// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\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// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\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
/* \u7c7b */\nclass Node {\nint val;\nNode next;\nNode(this.val, [this.next]);\n}\n/* \u51fd\u6570 */\nint function() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\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
use std::rc::Rc;\nuse std::cell::RefCell;\n/* \u7ed3\u6784\u4f53 */\nstruct Node {\nval: i32,\nnext: Option<Rc<RefCell<Node>>>,\n}\n/* \u521b\u5efa Node \u7ed3\u6784\u4f53 */\nimpl Node {\nfn new(val: i32) -> Self {\nSelf { val: val, next: None }\n}\n}\n/* \u51fd\u6570 */\nfn function() -> i32 {      // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\nreturn 0;\n}\nfn algorithm(n: i32) -> i32 {       // \u8f93\u5165\u6570\u636e\nconst a: i32 = 0;               // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nlet mut b = 0;                  // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nlet node = Node::new(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
/* \u51fd\u6570 */\nint func() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\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
\n
"},{"location":"chapter_computational_complexity/space_complexity/#242","title":"2.4.2 \u00a0 \u63a8\u7b97\u65b9\u6cd5","text":"

\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u63a8\u7b97\u65b9\u6cd5\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u5927\u81f4\u76f8\u540c\uff0c\u53ea\u9700\u5c06\u7edf\u8ba1\u5bf9\u8c61\u4ece\u201c\u64cd\u4f5c\u6570\u91cf\u201d\u8f6c\u4e3a\u201c\u4f7f\u7528\u7a7a\u95f4\u5927\u5c0f\u201d\u3002

\u800c\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u540c\u7684\u662f\uff0c\u6211\u4eec\u901a\u5e38\u53ea\u5173\u6ce8\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u3002\u8fd9\u662f\u56e0\u4e3a\u5185\u5b58\u7a7a\u95f4\u662f\u4e00\u9879\u786c\u6027\u8981\u6c42\uff0c\u6211\u4eec\u5fc5\u987b\u786e\u4fdd\u5728\u6240\u6709\u8f93\u5165\u6570\u636e\u4e0b\u90fd\u6709\u8db3\u591f\u7684\u5185\u5b58\u7a7a\u95f4\u9884\u7559\u3002

\u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e2d\u7684\u201c\u6700\u5dee\u201d\u6709\u4e24\u5c42\u542b\u4e49\u3002

  1. \u4ee5\u6700\u5dee\u8f93\u5165\u6570\u636e\u4e3a\u51c6\uff1a\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)\\) \u3002
  2. \u4ee5\u7b97\u6cd5\u8fd0\u884c\u4e2d\u7684\u5cf0\u503c\u5185\u5b58\u4e3a\u51c6\uff1a\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)\\) \u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig
def algorithm(n: int):\na = 0               # O(1)\nb = [0] * 10000     # O(1)\nif n > 10:\nnums = [0] * n  # O(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
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)\nint[] b = new int[10000];    // O(1)\nif (n > 10) {\nint[] nums = new int[n]; // O(n)\n}\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
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
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)\nList<int> b = List.filled(10000, 0);  // O(1)\nif (n > 10) {\nList<int> nums = List.filled(n, 0); // O(n)\n}\n}\n
fn algorithm(n: i32) {\nlet a = 0;                              // O(1)\nlet b = [0; 10000];                     // O(1)\nif n > 10 {\nlet nums = vec![0; n as usize];     // O(n)\n}\n}\n
void algorithm(int n) {\nint a = 0;               // O(1)\nint b[10000];            // O(1)\nif (n > 10)\nint nums[n] = {0};   // O(n)\n}\n
\n

\u5728\u9012\u5f52\u51fd\u6570\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u7edf\u8ba1\u6808\u5e27\u7a7a\u95f4\u3002\u4f8b\u5982\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff1a

  • \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
  • \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
PythonC++JavaC#GoSwiftJSTSDartRustCZig
def function() -> int:\n# \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0\ndef loop(n: int):\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
int func() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\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// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\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 Function() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\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) */\nint Recur(int n) {\nif (n == 1) return 1;\nreturn Recur(n - 1);\n}\n
func function() int {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\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
@discardableResult\nfunc function() -> Int {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\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
function constFunc() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\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// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\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 function() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\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
fn function() -> i32 {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nfn loop(n: i32) {\nfor i in 0..n {\nfunction();\n}\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(n: i32) {\nif n == 1 {\nreturn;\n}\nrecur(n - 1);\n}\n
int func() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\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
\n
"},{"location":"chapter_computational_complexity/space_complexity/#243","title":"2.4.3 \u00a0 \u5e38\u89c1\u7c7b\u578b","text":"

\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u56fe 2-16 \u5c55\u793a\u4e86\u5e38\u89c1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u7c7b\u578b\uff08\u4ece\u4f4e\u5230\u9ad8\u6392\u5217\uff09\u3002

\\[ \\begin{aligned} O(1) < O(\\log n) < O(n) < O(n^2) < O(2^n) \\newline \\text{\u5e38\u6570\u9636} < \\text{\u5bf9\u6570\u9636} < \\text{\u7ebf\u6027\u9636} < \\text{\u5e73\u65b9\u9636} < \\text{\u6307\u6570\u9636} \\end{aligned} \\]

\u56fe 2-16 \u00a0 \u5e38\u89c1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u7c7b\u578b

"},{"location":"chapter_computational_complexity/space_complexity/#1-o1","title":"1. \u00a0 \u5e38\u6570\u9636 \\(O(1)\\)","text":"

\u5e38\u6570\u9636\u5e38\u89c1\u4e8e\u6570\u91cf\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\u7684\u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u3002

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u5faa\u73af\u4e2d\u521d\u59cb\u5316\u53d8\u91cf\u6216\u8c03\u7528\u51fd\u6570\u800c\u5360\u7528\u7684\u5185\u5b58\uff0c\u5728\u8fdb\u5165\u4e0b\u4e00\u5faa\u73af\u540e\u5c31\u4f1a\u88ab\u91ca\u653e\uff0c\u56e0\u6b64\u4e0d\u4f1a\u7d2f\u79ef\u5360\u7528\u7a7a\u95f4\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.py
def function() -> int:\n\"\"\"\u51fd\u6570\"\"\"\n# \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0\ndef constant(n: int):\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.cpp
/* \u51fd\u6570 */\nint func() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \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.java
/* \u51fd\u6570 */\nint function() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \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.cs
/* \u51fd\u6570 */\nint Function() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \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(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.go
/* \u51fd\u6570 */\nfunc function() int {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\nreturn 0\n}\n/* \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.swift
/* \u51fd\u6570 */\n@discardableResult\nfunc function() -> Int {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0\n}\n/* \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.js
/* \u51fd\u6570 */\nfunction constFunc() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \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
/* \u51fd\u6570 */\nfunction constFunc(): number {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \u5e38\u6570\u9636 */\nfunction constant(n: number): void {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\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.dart
/* \u51fd\u6570 */\nint function() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \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);\nListNode node = ListNode(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
space_complexity.rs
/* \u51fd\u6570 */\nfn function() ->i32 {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \u5e38\u6570\u9636 */\n#[allow(unused)]\nfn constant(n: i32) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst A: i32 = 0;\nlet b = 0;\nlet nums = vec![0; 10000];\nlet node = ListNode::new(0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor i in 0..n {\nlet c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor i in 0..n {\nfunction();\n}\n}\n
space_complexity.c
/* \u51fd\u6570 */\nint func() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \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.zig
[class]{}-[func]{function}\n// \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
"},{"location":"chapter_computational_complexity/space_complexity/#2-on","title":"2. \u00a0 \u7ebf\u6027\u9636 \\(O(n)\\)","text":"

\u7ebf\u6027\u9636\u5e38\u89c1\u4e8e\u5143\u7d20\u6570\u91cf\u4e0e \\(n\\) \u6210\u6b63\u6bd4\u7684\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u7b49\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.py
def linear(n: int):\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\nhmap = dict[int, str]()\nfor i in range(n):\nhmap[i] = str(i)\n
space_complexity.cpp
/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\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.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.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.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.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.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.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
space_complexity.rs
/* \u7ebf\u6027\u9636 */\n#[allow(unused)]\nfn linear(n: i32) {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\nlet mut nums = vec![0; n as usize];\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nlet mut nodes = Vec::new();\nfor i in 0..n {\nnodes.push(ListNode::new(i))\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nlet mut map = HashMap::new();\nfor i in 0..n {\nmap.insert(i, i.to_string());\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.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

\u5982\u56fe 2-17 \u6240\u793a\uff0c\u6b64\u51fd\u6570\u7684\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u5373\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684 linear_recur() \u51fd\u6570\uff0c\u4f7f\u7528 \\(O(n)\\) \u5927\u5c0f\u7684\u6808\u5e27\u7a7a\u95f4\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.py
def linear_recur(n: int):\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.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.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.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.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.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.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.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
space_complexity.rs
/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn linear_recur(n: i32) {\nprintln!(\"\u9012\u5f52 n = {}\", n);\nif n == 1 {return};\nlinear_recur(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.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

\u56fe 2-17 \u00a0 \u9012\u5f52\u51fd\u6570\u4ea7\u751f\u7684\u7ebf\u6027\u9636\u7a7a\u95f4\u590d\u6742\u5ea6

"},{"location":"chapter_computational_complexity/space_complexity/#3-on2","title":"3. \u00a0 \u5e73\u65b9\u9636 \\(O(n^2)\\)","text":"

\u5e73\u65b9\u9636\u5e38\u89c1\u4e8e\u77e9\u9635\u548c\u56fe\uff0c\u5143\u7d20\u6570\u91cf\u4e0e \\(n\\) \u6210\u5e73\u65b9\u5173\u7cfb\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.py
def quadratic(n: int):\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.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.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.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.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.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.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.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
space_complexity.rs
/* \u5e73\u65b9\u9636 */\n#[allow(unused)]\nfn quadratic(n: i32) {\n// \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\nlet num_matrix = vec![vec![0; n as usize]; n as usize];\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nlet mut num_list = Vec::new();\nfor i in 0..n {\nlet mut tmp = Vec::new();\nfor j in 0..n {\ntmp.push(0);\n}\nnum_list.push(tmp);\n}\n}\n
space_complexity.c
/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\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.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

\u5982\u56fe 2-18 \u6240\u793a\uff0c\u8be5\u51fd\u6570\u7684\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u5728\u6bcf\u4e2a\u9012\u5f52\u51fd\u6570\u4e2d\u90fd\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u6570\u7ec4\uff0c\u957f\u5ea6\u5206\u522b\u4e3a \\(n\\)\u3001\\(n-1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \uff0c\u5e73\u5747\u957f\u5ea6\u4e3a \\(n / 2\\) \uff0c\u56e0\u6b64\u603b\u4f53\u5360\u7528 \\(O(n^2)\\) \u7a7a\u95f4\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.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.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.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.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.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.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.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.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
space_complexity.rs
/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn quadratic_recur(n: i32) -> i32 {\nif n <= 0 {return 0};\n// \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\nlet nums = vec![0; n as usize];\nprintln!(\"\u9012\u5f52 n = {} \u4e2d\u7684 nums \u957f\u5ea6 = {}\", n, nums.len());\nreturn quadratic_recur(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);\nint res = quadraticRecur(n - 1);\nfree(nums);\nreturn res;\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

\u56fe 2-18 \u00a0 \u9012\u5f52\u51fd\u6570\u4ea7\u751f\u7684\u5e73\u65b9\u9636\u7a7a\u95f4\u590d\u6742\u5ea6

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

\u6307\u6570\u9636\u5e38\u89c1\u4e8e\u4e8c\u53c9\u6811\u3002\u89c2\u5bdf\u56fe 2-19 \uff0c\u9ad8\u5ea6\u4e3a \\(n\\) \u7684\u201c\u6ee1\u4e8c\u53c9\u6811\u201d\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(2^n - 1\\) \uff0c\u5360\u7528 \\(O(2^n)\\) \u7a7a\u95f4\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.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.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.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.cs
/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode? BuildTree(int n) {\nif (n == 0) return null;\nTreeNode root = new(0) {\nleft = BuildTree(n - 1),\nright = BuildTree(n - 1)\n};\nreturn root;\n}\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.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.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.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
space_complexity.rs
/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfn build_tree(n: i32) -> Option<Rc<RefCell<TreeNode>>> {\nif n == 0 {return None};\nlet root = TreeNode::new(0);\nroot.borrow_mut().left = build_tree(n - 1);\nroot.borrow_mut().right = build_tree(n - 1);\nreturn Some(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.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

\u56fe 2-19 \u00a0 \u6ee1\u4e8c\u53c9\u6811\u4ea7\u751f\u7684\u6307\u6570\u9636\u7a7a\u95f4\u590d\u6742\u5ea6

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

\u5bf9\u6570\u9636\u5e38\u89c1\u4e8e\u5206\u6cbb\u7b97\u6cd5\u3002\u4f8b\u5982\u5f52\u5e76\u6392\u5e8f\uff0c\u8f93\u5165\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\uff0c\u6bcf\u8f6e\u9012\u5f52\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5212\u5206\u4e3a\u4e24\u534a\uff0c\u5f62\u6210\u9ad8\u5ea6\u4e3a \\(\\log n\\) \u7684\u9012\u5f52\u6811\uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u6808\u5e27\u7a7a\u95f4\u3002

\u518d\u4f8b\u5982\u5c06\u6570\u5b57\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\uff0c\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570 \\(n\\) \uff0c\u5b83\u7684\u4f4d\u6570\u4e3a \\(\\log_{10} n + 1\\) \uff0c\u5373\u5bf9\u5e94\u5b57\u7b26\u4e32\u957f\u5ea6\u4e3a \\(\\log_{10} n + 1\\) \uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log_{10} n + 1) = O(\\log n)\\) \u3002

"},{"location":"chapter_computational_complexity/space_complexity/#244","title":"2.4.4 \u00a0 \u6743\u8861\u65f6\u95f4\u4e0e\u7a7a\u95f4","text":"

\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u90fd\u80fd\u8fbe\u5230\u6700\u4f18\u3002\u7136\u800c\u5728\u5b9e\u9645\u60c5\u51b5\u4e2d\uff0c\u540c\u65f6\u4f18\u5316\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u901a\u5e38\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\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\u901a\u5e38\u662f\u66f4\u5e38\u7528\u7684\u7b56\u7565\u3002\u5f53\u7136\uff0c\u5728\u6570\u636e\u91cf\u5f88\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u63a7\u5236\u7a7a\u95f4\u590d\u6742\u5ea6\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002

"},{"location":"chapter_computational_complexity/summary/","title":"2.5 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_computational_complexity/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"

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

  • \u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u662f\u8861\u91cf\u7b97\u6cd5\u4f18\u52a3\u7684\u4e24\u4e2a\u4e3b\u8981\u8bc4\u4ef7\u6307\u6807\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\u7684\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\u5bf9\u5e94\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c\uff0c\u53cd\u6620\u5f53 \\(n\\) \u8d8b\u5411\u6b63\u65e0\u7a77\u65f6\uff0c\u64cd\u4f5c\u6570\u91cf \\(T(n)\\) \u7684\u589e\u957f\u7ea7\u522b\u3002
  • \u63a8\u7b97\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u4e3a\u4e24\u6b65\uff0c\u9996\u5148\u7edf\u8ba1\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)\\)\u3001\\(O(\\log n)\\)\u3001\\(O(n)\\)\u3001\\(O(n \\log n)\\)\u3001\\(O(n^2)\\)\u3001\\(O(2^n)\\) \u548c \\(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

  • \u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u4f5c\u7528\u7c7b\u4f3c\u4e8e\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\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)\\)\u3001\\(O(\\log n)\\)\u3001\\(O(n)\\)\u3001\\(O(n^2)\\) \u548c \\(O(2^n)\\) \u7b49\u3002
"},{"location":"chapter_computational_complexity/summary/#2-q-a","title":"2. \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\u3001Python\u3001C++\u3001Go\u3001C# \u7b49\uff09\u90fd\u4e0d\u652f\u6301\u81ea\u52a8\u4f18\u5316\u5c3e\u9012\u5f52\uff0c\u56e0\u6b64\u901a\u5e38\u8ba4\u4e3a\u7a7a\u95f4\u590d\u6742\u5ea6\u662f \\(O(n)\\) \u3002

\u51fd\u6570\u548c\u65b9\u6cd5\u8fd9\u4e24\u4e2a\u672f\u8bed\u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f

\u51fd\u6570\uff08function\uff09\u53ef\u4ee5\u88ab\u72ec\u7acb\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\u88ab\u9690\u5f0f\u4f20\u9012\u7ed9\u8c03\u7528\u5b83\u7684\u5bf9\u8c61\uff0c\u80fd\u591f\u5bf9\u7c7b\u7684\u5b9e\u4f8b\u4e2d\u5305\u542b\u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002

\u4e0b\u9762\u4ee5\u51e0\u4e2a\u5e38\u89c1\u7684\u7f16\u7a0b\u8bed\u8a00\u6765\u8bf4\u660e\u3002

  • C \u8bed\u8a00\u662f\u8fc7\u7a0b\u5f0f\u7f16\u7a0b\u8bed\u8a00\uff0c\u6ca1\u6709\u9762\u5411\u5bf9\u8c61\u7684\u6982\u5ff5\uff0c\u6240\u4ee5\u53ea\u6709\u51fd\u6570\u3002\u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u7ed3\u6784\u4f53\uff08struct\uff09\u6765\u6a21\u62df\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff0c\u4e0e\u7ed3\u6784\u4f53\u76f8\u5173\u8054\u7684\u51fd\u6570\u5c31\u76f8\u5f53\u4e8e\u5176\u4ed6\u8bed\u8a00\u4e2d\u7684\u65b9\u6cd5\u3002
  • Java \u548c C# \u662f\u9762\u5411\u5bf9\u8c61\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u4ee3\u7801\u5757\uff08\u65b9\u6cd5\uff09\u901a\u5e38\u90fd\u662f\u4f5c\u4e3a\u67d0\u4e2a\u7c7b\u7684\u4e00\u90e8\u5206\u3002\u9759\u6001\u65b9\u6cd5\u7684\u884c\u4e3a\u7c7b\u4f3c\u4e8e\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u88ab\u7ed1\u5b9a\u5728\u7c7b\u4e0a\uff0c\u4e0d\u80fd\u8bbf\u95ee\u7279\u5b9a\u7684\u5b9e\u4f8b\u53d8\u91cf\u3002
  • C++ \u548c Python \u65e2\u652f\u6301\u8fc7\u7a0b\u5f0f\u7f16\u7a0b\uff08\u51fd\u6570\uff09\uff0c\u4e5f\u652f\u6301\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08\u65b9\u6cd5\uff09\u3002

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

\u4e0d\u662f\uff0c\u8be5\u56fe\u7247\u5c55\u793a\u7684\u662f\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u5176\u53cd\u6620\u7684\u662f\u589e\u957f\u8d8b\u52bf\uff0c\u800c\u4e0d\u662f\u5360\u7528\u7a7a\u95f4\u7684\u7edd\u5bf9\u5927\u5c0f\u3002

\u5047\u8bbe\u53d6 \\(n = 8\\) \uff0c\u4f60\u53ef\u80fd\u4f1a\u53d1\u73b0\u6bcf\u6761\u66f2\u7ebf\u7684\u503c\u4e0e\u51fd\u6570\u5bf9\u5e94\u4e0d\u4e0a\u3002\u8fd9\u662f\u56e0\u4e3a\u6bcf\u6761\u66f2\u7ebf\u90fd\u5305\u542b\u4e00\u4e2a\u5e38\u6570\u9879\uff0c\u7528\u4e8e\u5c06\u53d6\u503c\u8303\u56f4\u538b\u7f29\u5230\u4e00\u4e2a\u89c6\u89c9\u8212\u9002\u7684\u8303\u56f4\u5185\u3002

\u5728\u5b9e\u9645\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u901a\u5e38\u4e0d\u77e5\u9053\u6bcf\u4e2a\u65b9\u6cd5\u7684\u201c\u5e38\u6570\u9879\u201d\u590d\u6742\u5ea6\u662f\u591a\u5c11\uff0c\u6240\u4ee5\u4e00\u822c\u65e0\u6cd5\u4ec5\u51ed\u590d\u6742\u5ea6\u6765\u9009\u62e9 \\(n = 8\\) \u4e4b\u4e0b\u7684\u6700\u4f18\u89e3\u6cd5\u3002\u4f46\u5bf9\u4e8e \\(n = 8^5\\) \u5c31\u5f88\u597d\u9009\u4e86\uff0c\u8fd9\u65f6\u589e\u957f\u8d8b\u52bf\u5df2\u7ecf\u5360\u4e3b\u5bfc\u4e86\u3002

"},{"location":"chapter_computational_complexity/time_complexity/","title":"2.3 \u00a0 \u65f6\u95f4\u590d\u6742\u5ea6","text":"

\u8fd0\u884c\u65f6\u95f4\u53ef\u4ee5\u76f4\u89c2\u4e14\u51c6\u786e\u5730\u53cd\u6620\u7b97\u6cd5\u7684\u6548\u7387\u3002\u5982\u679c\u6211\u4eec\u60f3\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 print() \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\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig
# \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\ndef algorithm(n: int):\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\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\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++\nConsole.WriteLine(0);      // 5 ns\n}\n}\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\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
// \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++\nprint(0); // 5 ns\n}\n}\n
// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfn algorithm(n: i32) {\nlet mut 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 \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\nprintln!(\"{}\", 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
\n

\u6839\u636e\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5f97\u5230\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u4e3a \\(6n + 12\\) ns \uff1a

\\[ 1 + 1 + 10 + (1 + 5) \\times n = 6n + 12 \\]

\u4f46\u5b9e\u9645\u4e0a\uff0c\u7edf\u8ba1\u7b97\u6cd5\u7684\u8fd0\u884c\u65f6\u95f4\u65e2\u4e0d\u5408\u7406\u4e5f\u4e0d\u73b0\u5b9e\u3002\u9996\u5148\uff0c\u6211\u4eec\u4e0d\u5e0c\u671b\u5c06\u9884\u4f30\u65f6\u95f4\u548c\u8fd0\u884c\u5e73\u53f0\u7ed1\u5b9a\uff0c\u56e0\u4e3a\u7b97\u6cd5\u9700\u8981\u5728\u5404\u79cd\u4e0d\u540c\u7684\u5e73\u53f0\u4e0a\u8fd0\u884c\u3002\u5176\u6b21\uff0c\u6211\u4eec\u5f88\u96be\u83b7\u77e5\u6bcf\u79cd\u64cd\u4f5c\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u8fd9\u7ed9\u9884\u4f30\u8fc7\u7a0b\u5e26\u6765\u4e86\u6781\u5927\u7684\u96be\u5ea6\u3002

"},{"location":"chapter_computational_complexity/time_complexity/#231","title":"2.3.1 \u00a0 \u7edf\u8ba1\u65f6\u95f4\u589e\u957f\u8d8b\u52bf","text":"

\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u7edf\u8ba1\u7684\u4e0d\u662f\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\uff0c\u800c\u662f\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u968f\u7740\u6570\u636e\u91cf\u53d8\u5927\u65f6\u7684\u589e\u957f\u8d8b\u52bf\u3002

\u201c\u65f6\u95f4\u589e\u957f\u8d8b\u52bf\u201d\u8fd9\u4e2a\u6982\u5ff5\u6bd4\u8f83\u62bd\u8c61\uff0c\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u4f8b\u5b50\u6765\u52a0\u4ee5\u7406\u89e3\u3002\u5047\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u7ed9\u5b9a\u4e09\u4e2a\u7b97\u6cd5\u51fd\u6570 A\u3001B \u548c C \uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig
# \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_A(n: int):\nprint(0)\n# \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\ndef algorithm_B(n: int):\nfor _ in range(n):\nprint(0)\n# \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_C(n: int):\nfor _ in range(1000000):\nprint(0)\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\ncout << 0 << endl;\n}\n// \u7b97\u6cd5 B \u7684\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 \u7684\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 \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\nSystem.out.println(0);\n}\n// \u7b97\u6cd5 B \u7684\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 \u7684\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 \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid AlgorithmA(int n) {\nConsole.WriteLine(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid AlgorithmB(int n) {\nfor (int i = 0; i < n; i++) {\nConsole.WriteLine(0);\n}\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid AlgorithmC(int n) {\nfor (int i = 0; i < 1000000; i++) {\nConsole.WriteLine(0);\n}\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithm_A(n int) {\nfmt.Println(0)\n}\n// \u7b97\u6cd5 B \u7684\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 \u7684\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 \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithmA(n: Int) {\nprint(0)\n}\n// \u7b97\u6cd5 B \u7684\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 \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithmC(n: Int) {\nfor _ in 0 ..< 1000000 {\nprint(0)\n}\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_A(n) {\nconsole.log(0);\n}\n// \u7b97\u6cd5 B \u7684\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 \u7684\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 \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_A(n: number): void {\nconsole.log(0);\n}\n// \u7b97\u6cd5 B \u7684\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 \u7684\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 \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithmA(int n) {\nprint(0);\n}\n// \u7b97\u6cd5 B \u7684\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 \u7684\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
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfn algorithm_A(n: i32) {\nprintln!(\"{}\", 0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfn algorithm_B(n: i32) {\nfor _ in 0..n {\nprintln!(\"{}\", 0);\n}\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfn algorithm_C(n: i32) {\nfor _ in 0..1000000 {\nprintln!(\"{}\", 0);\n}\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\nprintf(\"%d\", 0);\n}\n// \u7b97\u6cd5 B \u7684\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 \u7684\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
\n

\u56fe 2-7 \u5c55\u793a\u4e86\u4ee5\u4e0a\u4e09\u4e2a\u7b97\u6cd5\u51fd\u6570\u7684\u65f6\u95f4\u590d\u6742\u5ea6\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\u201c\u5e38\u6570\u9636\u201d\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\u201c\u7ebf\u6027\u9636\u201d\u3002
  • \u7b97\u6cd5 C \u4e2d\u7684\u6253\u5370\u64cd\u4f5c\u9700\u8981\u5faa\u73af \\(1000000\\) \u6b21\uff0c\u867d\u7136\u8fd0\u884c\u65f6\u95f4\u5f88\u957f\uff0c\u4f46\u5b83\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\u3002\u56e0\u6b64 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u548c A \u76f8\u540c\uff0c\u4ecd\u4e3a\u201c\u5e38\u6570\u9636\u201d\u3002

\u56fe 2-7 \u00a0 \u7b97\u6cd5 A\u3001B \u548c C \u7684\u65f6\u95f4\u589e\u957f\u8d8b\u52bf

\u76f8\u8f83\u4e8e\u76f4\u63a5\u7edf\u8ba1\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u6709\u54ea\u4e9b\u7279\u70b9\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 \u66f4\u6162\uff0c\u5728 \\(n > 1000000\\) \u65f6\u6bd4\u7b97\u6cd5 C \u66f4\u6162\u3002\u4e8b\u5b9e\u4e0a\uff0c\u53ea\u8981\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u8db3\u591f\u5927\uff0c\u590d\u6742\u5ea6\u4e3a\u201c\u5e38\u6570\u9636\u201d\u7684\u7b97\u6cd5\u4e00\u5b9a\u4f18\u4e8e\u201c\u7ebf\u6027\u9636\u201d\u7684\u7b97\u6cd5\uff0c\u8fd9\u6b63\u662f\u65f6\u95f4\u589e\u957f\u8d8b\u52bf\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\u4e00\u6765\u4f30\u7b97\u96be\u5ea6\u5c31\u5927\u5927\u964d\u4f4e\u4e86\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\u7684\u9ad8\u4f4e\u3002\u5f53\u7136\uff0c\u5c3d\u7ba1\u5b58\u5728\u4e0a\u8ff0\u95ee\u9898\uff0c\u590d\u6742\u5ea6\u5206\u6790\u4ecd\u7136\u662f\u8bc4\u5224\u7b97\u6cd5\u6548\u7387\u6700\u6709\u6548\u4e14\u5e38\u7528\u7684\u65b9\u6cd5\u3002
"},{"location":"chapter_computational_complexity/time_complexity/#232","title":"2.3.2 \u00a0 \u51fd\u6570\u6e10\u8fd1\u4e0a\u754c","text":"

\u7ed9\u5b9a\u4e00\u4e2a\u8f93\u5165\u5927\u5c0f\u4e3a \\(n\\) \u7684\u51fd\u6570\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig
def algorithm(n: int):\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
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
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\nConsole.WriteLine(0);   // +1\n}\n}\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
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
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\nprint(0); // +1\n}\n}\n
fn algorithm(n: i32) {\nlet mut a = 1;   // +1\na = a + 1;      // +1\na = a * 2;      // +1\n// \u5faa\u73af n \u6b21\nfor _ in 0..n { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\nprintln!(\"{}\", 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}  
\n

\u8bbe\u7b97\u6cd5\u7684\u64cd\u4f5c\u6570\u91cf\u662f\u4e00\u4e2a\u5173\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u7684\u51fd\u6570\uff0c\u8bb0\u4e3a \\(T(n)\\) \uff0c\u5219\u4ee5\u4e0a\u51fd\u6570\u7684\u7684\u64cd\u4f5c\u6570\u91cf\u4e3a\uff1a

\\[ T(n) = 3 + 2n \\]

\\(T(n)\\) \u662f\u4e00\u6b21\u51fd\u6570\uff0c\u8bf4\u660e\u5176\u8fd0\u884c\u65f6\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u662f\u7ebf\u6027\u7684\uff0c\u56e0\u6b64\u5b83\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f\u7ebf\u6027\u9636\u3002

\u6211\u4eec\u5c06\u7ebf\u6027\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u8bb0\u4e3a \\(O(n)\\) \uff0c\u8fd9\u4e2a\u6570\u5b66\u7b26\u53f7\u79f0\u4e3a\u300c\u5927 \\(O\\) \u8bb0\u53f7 big-\\(O\\) notation\u300d\uff0c\u8868\u793a\u51fd\u6570 \\(T(n)\\) \u7684\u300c\u6e10\u8fd1\u4e0a\u754c asymptotic upper bound\u300d\u3002

\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u672c\u8d28\u4e0a\u662f\u8ba1\u7b97\u201c\u64cd\u4f5c\u6570\u91cf\u51fd\u6570 \\(T(n)\\)\u201d\u7684\u6e10\u8fd1\u4e0a\u754c\uff0c\u5176\u5177\u6709\u660e\u786e\u7684\u6570\u5b66\u5b9a\u4e49\u3002

\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c

\u82e5\u5b58\u5728\u6b63\u5b9e\u6570 \\(c\\) \u548c\u5b9e\u6570 \\(n_0\\) \uff0c\u4f7f\u5f97\u5bf9\u4e8e\u6240\u6709\u7684 \\(n > n_0\\) \uff0c\u5747\u6709 \\(T(n) \\leq c \\cdot f(n)\\) \uff0c\u5219\u53ef\u8ba4\u4e3a \\(f(n)\\) \u7ed9\u51fa\u4e86 \\(T(n)\\) \u7684\u4e00\u4e2a\u6e10\u8fd1\u4e0a\u754c\uff0c\u8bb0\u4e3a \\(T(n) = O(f(n))\\) \u3002

\u5982\u56fe 2-8 \u6240\u793a\uff0c\u8ba1\u7b97\u6e10\u8fd1\u4e0a\u754c\u5c31\u662f\u5bfb\u627e\u4e00\u4e2a\u51fd\u6570 \\(f(n)\\) \uff0c\u4f7f\u5f97\u5f53 \\(n\\) \u8d8b\u5411\u4e8e\u65e0\u7a77\u5927\u65f6\uff0c\\(T(n)\\) \u548c \\(f(n)\\) \u5904\u4e8e\u76f8\u540c\u7684\u589e\u957f\u7ea7\u522b\uff0c\u4ec5\u76f8\u5dee\u4e00\u4e2a\u5e38\u6570\u9879 \\(c\\) \u7684\u500d\u6570\u3002

\u56fe 2-8 \u00a0 \u51fd\u6570\u7684\u6e10\u8fd1\u4e0a\u754c

"},{"location":"chapter_computational_complexity/time_complexity/#233","title":"2.3.3 \u00a0 \u63a8\u7b97\u65b9\u6cd5","text":"

\u6e10\u8fd1\u4e0a\u754c\u7684\u6570\u5b66\u5473\u513f\u6709\u70b9\u91cd\uff0c\u5982\u679c\u4f60\u611f\u89c9\u6ca1\u6709\u5b8c\u5168\u7406\u89e3\uff0c\u4e5f\u65e0\u987b\u62c5\u5fc3\u3002\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\u5c31\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":"1. \u00a0 \u7b2c\u4e00\u6b65\uff1a\u7edf\u8ba1\u64cd\u4f5c\u6570\u91cf","text":"

\u9488\u5bf9\u4ee3\u7801\uff0c\u9010\u884c\u4ece\u4e0a\u5230\u4e0b\u8ba1\u7b97\u5373\u53ef\u3002\u7136\u800c\uff0c\u7531\u4e8e\u4e0a\u8ff0 \\(c \\cdot f(n)\\) \u4e2d\u7684\u5e38\u6570\u9879 \\(c\\) \u53ef\u4ee5\u53d6\u4efb\u610f\u5927\u5c0f\uff0c\u56e0\u6b64\u64cd\u4f5c\u6570\u91cf \\(T(n)\\) \u4e2d\u7684\u5404\u79cd\u7cfb\u6570\u3001\u5e38\u6570\u9879\u90fd\u53ef\u4ee5\u88ab\u5ffd\u7565\u3002\u6839\u636e\u6b64\u539f\u5219\uff0c\u53ef\u4ee5\u603b\u7ed3\u51fa\u4ee5\u4e0b\u8ba1\u6570\u7b80\u5316\u6280\u5de7\u3002

  1. \u5ffd\u7565 \\(T(n)\\) \u4e2d\u7684\u5e38\u6570\u9879\u3002\u56e0\u4e3a\u5b83\u4eec\u90fd\u4e0e \\(n\\) \u65e0\u5173\uff0c\u6240\u4ee5\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\u7b2c 1. \u70b9\u548c\u7b2c 2. \u70b9\u7684\u6280\u5de7\u3002

\u7ed9\u5b9a\u4e00\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u4e0a\u8ff0\u6280\u5de7\u6765\u7edf\u8ba1\u64cd\u4f5c\u6570\u91cf\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig
def 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 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
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
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++) {\nConsole.WriteLine(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++) {\nConsole.WriteLine(0);\n}\n}\n}\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
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
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++) {\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
fn algorithm(n: i32) {\nlet mut 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 i in 0..(5 * n + 1) {\nprintln!(\"{}\", 0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor i in 0..(2 * n) {\nfor j in 0..(n + 1) {\nprintln!(\"{}\", 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
\n

\u4ee5\u4e0b\u516c\u5f0f\u5c55\u793a\u4e86\u4f7f\u7528\u4e0a\u8ff0\u6280\u5de7\u524d\u540e\u7684\u7edf\u8ba1\u7ed3\u679c\uff0c\u4e24\u8005\u63a8\u51fa\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n^2)\\) \u3002

\\[ \\begin{aligned} T(n) & = 2n(n + 1) + (5n + 1) + 2 & \\text{\u5b8c\u6574\u7edf\u8ba1 (-.-|||)} \\newline & = 2n^2 + 7n + 3 \\newline T(n) & = n^2 + n & \\text{\u5077\u61d2\u7edf\u8ba1 (o.O)} \\end{aligned} \\]"},{"location":"chapter_computational_complexity/time_complexity/#2","title":"2. \u00a0 \u7b2c\u4e8c\u6b65\uff1a\u5224\u65ad\u6e10\u8fd1\u4e0a\u754c","text":"

\u65f6\u95f4\u590d\u6742\u5ea6\u7531\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

\u8868 2-2 \u5c55\u793a\u4e86\u4e00\u4e9b\u4f8b\u5b50\uff0c\u5176\u4e2d\u4e00\u4e9b\u5938\u5f20\u7684\u503c\u662f\u4e3a\u4e86\u5f3a\u8c03\u201c\u7cfb\u6570\u65e0\u6cd5\u64bc\u52a8\u9636\u6570\u201d\u8fd9\u4e00\u7ed3\u8bba\u3002\u5f53 \\(n\\) \u8d8b\u4e8e\u65e0\u7a77\u5927\u65f6\uff0c\u8fd9\u4e9b\u5e38\u6570\u53d8\u5f97\u65e0\u8db3\u8f7b\u91cd\u3002

\u8868 2-2 \u00a0 \u4e0d\u540c\u64cd\u4f5c\u6570\u91cf\u5bf9\u5e94\u7684\u65f6\u95f4\u590d\u6742\u5ea6

\u64cd\u4f5c\u6570\u91cf \\(T(n)\\) \u65f6\u95f4\u590d\u6742\u5ea6 \\(O(f(n))\\) \\(100000\\) \\(O(1)\\) \\(3n + 2\\) \\(O(n)\\) \\(2n^2 + 3n + 2\\) \\(O(n^2)\\) \\(n^3 + 10000n^2\\) \\(O(n^3)\\) \\(2^n + 10000n^{10000}\\) \\(O(2^n)\\)"},{"location":"chapter_computational_complexity/time_complexity/#234","title":"2.3.4 \u00a0 \u5e38\u89c1\u7c7b\u578b","text":"

\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u5e38\u89c1\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7c7b\u578b\u5982\u56fe 2-9 \u6240\u793a\uff08\u6309\u7167\u4ece\u4f4e\u5230\u9ad8\u7684\u987a\u5e8f\u6392\u5217\uff09\u3002

\\[ \\begin{aligned} O(1) < O(\\log n) < O(n) < O(n \\log n) < O(n^2) < O(2^n) < O(n!) \\newline \\text{\u5e38\u6570\u9636} < \\text{\u5bf9\u6570\u9636} < \\text{\u7ebf\u6027\u9636} < \\text{\u7ebf\u6027\u5bf9\u6570\u9636} < \\text{\u5e73\u65b9\u9636} < \\text{\u6307\u6570\u9636} < \\text{\u9636\u4e58\u9636} \\end{aligned} \\]

\u56fe 2-9 \u00a0 \u5e38\u89c1\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7c7b\u578b

"},{"location":"chapter_computational_complexity/time_complexity/#1-o1","title":"1. \u00a0 \u5e38\u6570\u9636 \\(O(1)\\)","text":"

\u5e38\u6570\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\uff0c\u5373\u4e0d\u968f\u7740 \\(n\\) \u7684\u53d8\u5316\u800c\u53d8\u5316\u3002

\u5728\u4ee5\u4e0b\u51fd\u6570\u4e2d\uff0c\u5c3d\u7ba1\u64cd\u4f5c\u6570\u91cf size \u53ef\u80fd\u5f88\u5927\uff0c\u4f46\u7531\u4e8e\u5176\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
time_complexity.rs
/* \u5e38\u6570\u9636 */\nfn constant(n: i32) -> i32 {\n_ = n;\nlet mut count = 0;\nlet size = 100_000;\nfor _ in 0..size {\ncount += 1;\n}\ncount\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.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
"},{"location":"chapter_computational_complexity/time_complexity/#2-on","title":"2. \u00a0 \u7ebf\u6027\u9636 \\(O(n)\\)","text":"

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

PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.py
def linear(n: int) -> int:\n\"\"\"\u7ebf\u6027\u9636\"\"\"\ncount = 0\nfor _ in range(n):\ncount += 1\nreturn count\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.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.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.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.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.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.dart
/* \u7ebf\u6027\u9636 */\nint linear(int n) {\nint count = 0;\nfor (var i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n
time_complexity.rs
/* \u7ebf\u6027\u9636 */\nfn linear(n: i32) -> i32 {\nlet mut count = 0;\nfor _ in 0..n {\ncount += 1;\n}\ncount\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.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

\u904d\u5386\u6570\u7ec4\u548c\u904d\u5386\u94fe\u8868\u7b49\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u6570\u7ec4\u6216\u94fe\u8868\u7684\u957f\u5ea6\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.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.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.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.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.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.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.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.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
time_complexity.rs
/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfn array_traversal(nums: &[i32]) -> i32 {\nlet mut count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor _ in nums {\ncount += 1;\n}\ncount\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.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

\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u9700\u6839\u636e\u8f93\u5165\u6570\u636e\u7684\u7c7b\u578b\u6765\u5177\u4f53\u786e\u5b9a\u3002\u6bd4\u5982\u5728\u7b2c\u4e00\u4e2a\u793a\u4f8b\u4e2d\uff0c\u53d8\u91cf \\(n\\) \u4e3a\u8f93\u5165\u6570\u636e\u5927\u5c0f\uff1b\u5728\u7b2c\u4e8c\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6570\u7ec4\u957f\u5ea6 \\(n\\) \u4e3a\u6570\u636e\u5927\u5c0f\u3002

"},{"location":"chapter_computational_complexity/time_complexity/#3-on2","title":"3. \u00a0 \u5e73\u65b9\u9636 \\(O(n^2)\\)","text":"

\u5e73\u65b9\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u76f8\u5bf9\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u4ee5\u5e73\u65b9\u7ea7\u522b\u589e\u957f\u3002\u5e73\u65b9\u9636\u901a\u5e38\u51fa\u73b0\u5728\u5d4c\u5957\u5faa\u73af\u4e2d\uff0c\u5916\u5c42\u5faa\u73af\u548c\u5185\u5c42\u5faa\u73af\u90fd\u4e3a \\(O(n)\\) \uff0c\u56e0\u6b64\u603b\u4f53\u4e3a \\(O(n^2)\\) \uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
time_complexity.rs
/* \u5e73\u65b9\u9636 */\nfn quadratic(n: i32) -> i32 {\nlet mut 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}\ncount\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.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

\u56fe 2-10 \u5bf9\u6bd4\u4e86\u5e38\u6570\u9636\u3001\u7ebf\u6027\u9636\u548c\u5e73\u65b9\u9636\u4e09\u79cd\u65f6\u95f4\u590d\u6742\u5ea6\u3002

\u56fe 2-10 \u00a0 \u5e38\u6570\u9636\u3001\u7ebf\u6027\u9636\u548c\u5e73\u65b9\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

\u4ee5\u5192\u6ce1\u6392\u5e8f\u4e3a\u4f8b\uff0c\u5916\u5c42\u5faa\u73af\u6267\u884c \\(n - 1\\) \u6b21\uff0c\u5185\u5c42\u5faa\u73af\u6267\u884c \\(n-1\\)\u3001\\(n-2\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \u6b21\uff0c\u5e73\u5747\u4e3a \\(n / 2\\) \u6b21\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O((n - 1) n / 2) = O(n^2)\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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]\n(nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\ncount += 3;  // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\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.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.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.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
time_complexity.rs
/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfn bubble_sort(nums: &mut [i32]) -> i32 {\nlet mut count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor i in (1..nums.len()).rev() {\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \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}\ncount\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.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 = @as(i32, @intCast(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
"},{"location":"chapter_computational_complexity/time_complexity/#4-o2n","title":"4. \u00a0 \u6307\u6570\u9636 \\(O(2^n)\\)","text":"

\u751f\u7269\u5b66\u7684\u201c\u7ec6\u80de\u5206\u88c2\u201d\u662f\u6307\u6570\u9636\u589e\u957f\u7684\u5178\u578b\u4f8b\u5b50\uff1a\u521d\u59cb\u72b6\u6001\u4e3a \\(1\\) \u4e2a\u7ec6\u80de\uff0c\u5206\u88c2\u4e00\u8f6e\u540e\u53d8\u4e3a \\(2\\) \u4e2a\uff0c\u5206\u88c2\u4e24\u8f6e\u540e\u53d8\u4e3a \\(4\\) \u4e2a\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u5206\u88c2 \\(n\\) \u8f6e\u540e\u6709 \\(2^n\\) \u4e2a\u7ec6\u80de\u3002

\u56fe 2-11 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6a21\u62df\u4e86\u7ec6\u80de\u5206\u88c2\u7684\u8fc7\u7a0b\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.py
def exponential(n: int) -> int:\n\"\"\"\u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\"\"\"\ncount = 0\nbase = 1\n# \u7ec6\u80de\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.cpp
/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\nint count = 0, base = 1;\n// \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\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.java
/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\nint count = 0, base = 1;\n// \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\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.cs
/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint Exponential(int n) {\nint count = 0, bas = 1;\n// \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\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.go
/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09*/\nfunc exponential(n int) int {\ncount, base := 0, 1\n// \u7ec6\u80de\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.swift
/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunc exponential(n: Int) -> Int {\nvar count = 0\nvar base = 1\n// \u7ec6\u80de\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.js
/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction exponential(n) {\nlet count = 0,\nbase = 1;\n// \u7ec6\u80de\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// \u7ec6\u80de\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.dart
/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\nint count = 0, base = 1;\n// \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\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
time_complexity.rs
/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfn exponential(n: i32) -> i32 {\nlet mut count = 0;\nlet mut base = 1;\n// \u7ec6\u80de\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\ncount\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// \u7ec6\u80de\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.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// \u7ec6\u80de\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

\u56fe 2-11 \u00a0 \u6307\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

\u5728\u5b9e\u9645\u7b97\u6cd5\u4e2d\uff0c\u6307\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u9012\u5f52\u51fd\u6570\u4e2d\u3002\u4f8b\u5982\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u5176\u9012\u5f52\u5730\u4e00\u5206\u4e3a\u4e8c\uff0c\u7ecf\u8fc7 \\(n\\) \u6b21\u5206\u88c2\u540e\u505c\u6b62\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.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.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.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.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.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.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.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.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
time_complexity.rs
/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn exp_recur(n: i32) -> i32 {\nif n == 1 {\nreturn 1;\n}\nexp_recur(n - 1) + exp_recur(n - 1) + 1\n}\n
time_complexity.c
/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\nif (n == 1)\nreturn 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.zig
// \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn expRecur(n: i32) i32 {\nif (n == 1) return 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n

\u6307\u6570\u9636\u589e\u957f\u975e\u5e38\u8fc5\u901f\uff0c\u5728\u7a77\u4e3e\u6cd5\uff08\u66b4\u529b\u641c\u7d22\u3001\u56de\u6eaf\u7b49\uff09\u4e2d\u6bd4\u8f83\u5e38\u89c1\u3002\u5bf9\u4e8e\u6570\u636e\u89c4\u6a21\u8f83\u5927\u7684\u95ee\u9898\uff0c\u6307\u6570\u9636\u662f\u4e0d\u53ef\u63a5\u53d7\u7684\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528\u52a8\u6001\u89c4\u5212\u6216\u8d2a\u5fc3\u7b49\u7b97\u6cd5\u6765\u89e3\u51b3\u3002

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

\u4e0e\u6307\u6570\u9636\u76f8\u53cd\uff0c\u5bf9\u6570\u9636\u53cd\u6620\u4e86\u201c\u6bcf\u8f6e\u7f29\u51cf\u5230\u4e00\u534a\u201d\u7684\u60c5\u51b5\u3002\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u7531\u4e8e\u6bcf\u8f6e\u7f29\u51cf\u5230\u4e00\u534a\uff0c\u56e0\u6b64\u5faa\u73af\u6b21\u6570\u662f \\(\\log_2 n\\) \uff0c\u5373 \\(2^n\\) \u7684\u53cd\u51fd\u6570\u3002

\u56fe 2-12 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6a21\u62df\u4e86\u201c\u6bcf\u8f6e\u7f29\u51cf\u5230\u4e00\u534a\u201d\u7684\u8fc7\u7a0b\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log_2 n)\\) \uff0c\u7b80\u8bb0\u4e3a \\(O(\\log n)\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
time_complexity.rs
/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfn logarithmic(mut n: f32) -> i32 {\nlet mut count = 0;\nwhile n > 1.0 {\nn = n / 2.0;\ncount += 1;\n}\ncount\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.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

\u56fe 2-12 \u00a0 \u5bf9\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

\u4e0e\u6307\u6570\u9636\u7c7b\u4f3c\uff0c\u5bf9\u6570\u9636\u4e5f\u5e38\u51fa\u73b0\u4e8e\u9012\u5f52\u51fd\u6570\u4e2d\u3002\u4ee5\u4e0b\u4ee3\u7801\u5f62\u6210\u4e86\u4e00\u4e2a\u9ad8\u5ea6\u4e3a \\(\\log_2 n\\) \u7684\u9012\u5f52\u6811\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
time_complexity.rs
/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn log_recur(n: f32) -> i32 {\nif n <= 1.0 {\nreturn 0;\n}\nlog_recur(n / 2.0) + 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.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

\u5bf9\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u7b97\u6cd5\u4e2d\uff0c\u4f53\u73b0\u4e86\u201c\u4e00\u5206\u4e3a\u591a\u201d\u548c\u201c\u5316\u7e41\u4e3a\u7b80\u201d\u7684\u7b97\u6cd5\u601d\u60f3\u3002\u5b83\u589e\u957f\u7f13\u6162\uff0c\u662f\u4ec5\u6b21\u4e8e\u5e38\u6570\u9636\u7684\u7406\u60f3\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002

\\(O(\\log n)\\) \u7684\u5e95\u6570\u662f\u591a\u5c11\uff1f

\u51c6\u786e\u6765\u8bf4\uff0c\u201c\u4e00\u5206\u4e3a \\(m\\)\u201d\u5bf9\u5e94\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(\\log_m n)\\) \u3002\u800c\u901a\u8fc7\u5bf9\u6570\u6362\u5e95\u516c\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u5177\u6709\u4e0d\u540c\u5e95\u6570\u7684\u3001\u76f8\u7b49\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a

\\[ O(\\log_m n) = O(\\log_k n / \\log_k m) = O(\\log_k n) \\]

\u4e5f\u5c31\u662f\u8bf4\uff0c\u5e95\u6570 \\(m\\) \u53ef\u4ee5\u5728\u4e0d\u5f71\u54cd\u590d\u6742\u5ea6\u7684\u524d\u63d0\u4e0b\u8f6c\u6362\u3002\u56e0\u6b64\u6211\u4eec\u901a\u5e38\u4f1a\u7701\u7565\u5e95\u6570 \\(m\\) \uff0c\u5c06\u5bf9\u6570\u9636\u76f4\u63a5\u8bb0\u4e3a \\(O(\\log n)\\) \u3002

"},{"location":"chapter_computational_complexity/time_complexity/#6-on-log-n","title":"6. \u00a0 \u7ebf\u6027\u5bf9\u6570\u9636 \\(O(n \\log n)\\)","text":"

\u7ebf\u6027\u5bf9\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u5d4c\u5957\u5faa\u73af\u4e2d\uff0c\u4e24\u5c42\u5faa\u73af\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u522b\u4e3a \\(O(\\log n)\\) \u548c \\(O(n)\\) \u3002\u76f8\u5173\u4ee3\u7801\u5982\u4e0b\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.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.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.java
/* \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) + LinearLogRecur(n / 2);\nfor (int i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n
time_complexity.go
/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunc linearLogRecur(n float64) int {\nif n <= 1 {\nreturn 1\n}\ncount := linearLogRecur(n/2) + linearLogRecur(n/2)\nfor i := 0.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.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.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
time_complexity.rs
/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfn linear_log_recur(n: f32) -> i32 {\nif n <= 1.0 {\nreturn 1;\n}\nlet mut count = linear_log_recur(n / 2.0) + linear_log_recur(n / 2.0);\nfor _ in 0 ..n as i32 {\ncount += 1;\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.zig
// \u7ebf\u6027\u5bf9\u6570\u9636\nfn linearLogRecur(n: f32) i32 {\nif (n <= 1) return 1;\nvar count: i32 = linearLogRecur(n / 2) + linearLogRecur(n / 2);\nvar i: f32 = 0;\nwhile (i < n) : (i += 1) {\ncount += 1;\n}\nreturn count;\n}\n

\u56fe 2-13 \u5c55\u793a\u4e86\u7ebf\u6027\u5bf9\u6570\u9636\u7684\u751f\u6210\u65b9\u5f0f\u3002\u4e8c\u53c9\u6811\u7684\u6bcf\u4e00\u5c42\u7684\u64cd\u4f5c\u603b\u6570\u90fd\u4e3a \\(n\\) \uff0c\u6811\u5171\u6709 \\(\\log_2 n + 1\\) \u5c42\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002

\u56fe 2-13 \u00a0 \u7ebf\u6027\u5bf9\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

\u4e3b\u6d41\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4e3a \\(O(n \\log n)\\) \uff0c\u4f8b\u5982\u5feb\u901f\u6392\u5e8f\u3001\u5f52\u5e76\u6392\u5e8f\u3001\u5806\u6392\u5e8f\u7b49\u3002

"},{"location":"chapter_computational_complexity/time_complexity/#7-on","title":"7. \u00a0 \u9636\u4e58\u9636 \\(O(n!)\\)","text":"

\u9636\u4e58\u9636\u5bf9\u5e94\u6570\u5b66\u4e0a\u7684\u201c\u5168\u6392\u5217\u201d\u95ee\u9898\u3002\u7ed9\u5b9a \\(n\\) \u4e2a\u4e92\u4e0d\u91cd\u590d\u7684\u5143\u7d20\uff0c\u6c42\u5176\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u65b9\u6848\uff0c\u65b9\u6848\u6570\u91cf\u4e3a\uff1a

\\[ n! = n \\times (n - 1) \\times (n - 2) \\times \\dots \\times 2 \\times 1 \\]

\u9636\u4e58\u901a\u5e38\u4f7f\u7528\u9012\u5f52\u5b9e\u73b0\u3002\u5982\u56fe 2-14 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u7b2c\u4e00\u5c42\u5206\u88c2\u51fa \\(n\\) \u4e2a\uff0c\u7b2c\u4e8c\u5c42\u5206\u88c2\u51fa \\(n - 1\\) \u4e2a\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u7b2c \\(n\\) \u5c42\u65f6\u505c\u6b62\u5206\u88c2\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.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.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.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.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.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.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.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.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
time_complexity.rs
/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn factorial_recur(n: i32) -> i32 {\nif n == 0 {\nreturn 1;\n}\nlet mut count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor _ in 0..n {\ncount += factorial_recur(n - 1);\n}\ncount\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.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

\u56fe 2-14 \u00a0 \u9636\u4e58\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

\u8bf7\u6ce8\u610f\uff0c\u56e0\u4e3a\u5f53 \\(n \\geq 4\\) \u65f6\u6052\u6709 \\(n! > 2^n\\) \uff0c\u6240\u4ee5\u9636\u4e58\u9636\u6bd4\u6307\u6570\u9636\u589e\u957f\u5f97\u66f4\u5feb\uff0c\u5728 \\(n\\) \u8f83\u5927\u65f6\u4e5f\u662f\u4e0d\u53ef\u63a5\u53d7\u7684\u3002

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

\u7b97\u6cd5\u7684\u65f6\u95f4\u6548\u7387\u5f80\u5f80\u4e0d\u662f\u56fa\u5b9a\u7684\uff0c\u800c\u662f\u4e0e\u8f93\u5165\u6570\u636e\u7684\u5206\u5e03\u6709\u5173\u3002\u5047\u8bbe\u8f93\u5165\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4 nums \uff0c\u5176\u4e2d nums \u7531\u4ece \\(1\\) \u81f3 \\(n\\) \u7684\u6570\u5b57\u7ec4\u6210\uff0c\u6bcf\u4e2a\u6570\u5b57\u53ea\u51fa\u73b0\u4e00\u6b21\uff1b\u4f46\u5143\u7d20\u987a\u5e8f\u662f\u968f\u673a\u6253\u4e71\u7684\uff0c\u4efb\u52a1\u76ee\u6807\u662f\u8fd4\u56de\u5143\u7d20 \\(1\\) \u7684\u7d22\u5f15\u3002\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\u3002

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

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

PythonC++JavaC#GoSwiftJSTSDartRustCZig worst_best_time_complexity.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.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.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.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++) {\nint index = new Random().Next(i, nums.Length);\n(nums[i], nums[index]) = (nums[index], nums[i]);\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.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.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.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.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
worst_best_time_complexity.rs
/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfn random_numbers(n: i32) -> Vec<i32> {\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nlet mut nums = (1..=n).collect::<Vec<i32>>();\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nnums.shuffle(&mut thread_rng());\nnums\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfn find_one(nums: &[i32]) -> Option<usize> {\nfor i in 0..nums.len() {\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nif nums[i] == 1 {\nreturn Some(i);\n}\n}\nNone\n}\n
worst_best_time_complexity.c
/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint *randomNumbers(int n) {\n// \u5206\u914d\u5806\u533a\u5185\u5b58\uff08\u521b\u5efa\u4e00\u7ef4\u53ef\u53d8\u957f\u6570\u7ec4\uff1a\u6570\u7ec4\u4e2d\u5143\u7d20\u6570\u91cf\u4e3a n \uff0c\u5143\u7d20\u7c7b\u578b\u4e3a int \uff09\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.zig
// \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71\nfn randomNumbers(comptime n: usize) [n]i32 {\nvar nums: [n]i32 = undefined;\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (&nums, 0..) |*num, i| {\nnum.* = @as(i32, @intCast(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\nfn findOne(nums: []i32) i32 {\nfor (nums, 0..) |num, i| {\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nif (num == 1) return @intCast(i);\n}\nreturn -1;\n}\n

\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6211\u4eec\u5728\u5b9e\u9645\u4e2d\u5f88\u5c11\u4f7f\u7528\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u56e0\u4e3a\u901a\u5e38\u53ea\u6709\u5728\u5f88\u5c0f\u6982\u7387\u4e0b\u624d\u80fd\u8fbe\u5230\uff0c\u53ef\u80fd\u4f1a\u5e26\u6765\u4e00\u5b9a\u7684\u8bef\u5bfc\u6027\u3002\u800c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u66f4\u4e3a\u5b9e\u7528\uff0c\u56e0\u4e3a\u5b83\u7ed9\u51fa\u4e86\u4e00\u4e2a\u6548\u7387\u5b89\u5168\u503c\uff0c\u8ba9\u6211\u4eec\u53ef\u4ee5\u653e\u5fc3\u5730\u4f7f\u7528\u7b97\u6cd5\u3002

\u4ece\u4e0a\u8ff0\u793a\u4f8b\u53ef\u4ee5\u770b\u51fa\uff0c\u6700\u5dee\u6216\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u53ea\u51fa\u73b0\u4e8e\u201c\u7279\u6b8a\u7684\u6570\u636e\u5206\u5e03\u201d\uff0c\u8fd9\u4e9b\u60c5\u51b5\u7684\u51fa\u73b0\u6982\u7387\u53ef\u80fd\u5f88\u5c0f\uff0c\u5e76\u4e0d\u80fd\u771f\u5b9e\u5730\u53cd\u6620\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u4f53\u73b0\u7b97\u6cd5\u5728\u968f\u673a\u8f93\u5165\u6570\u636e\u4e0b\u7684\u8fd0\u884c\u6548\u7387\uff0c\u7528 \\(\\Theta\\) \u8bb0\u53f7\u6765\u8868\u793a\u3002

\u5bf9\u4e8e\u90e8\u5206\u7b97\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u5730\u63a8\u7b97\u51fa\u968f\u673a\u6570\u636e\u5206\u5e03\u4e0b\u7684\u5e73\u5747\u60c5\u51b5\u3002\u6bd4\u5982\u4e0a\u8ff0\u793a\u4f8b\uff0c\u7531\u4e8e\u8f93\u5165\u6570\u7ec4\u662f\u88ab\u6253\u4e71\u7684\uff0c\u56e0\u6b64\u5143\u7d20 \\(1\\) \u51fa\u73b0\u5728\u4efb\u610f\u7d22\u5f15\u7684\u6982\u7387\u90fd\u662f\u76f8\u7b49\u7684\uff0c\u90a3\u4e48\u7b97\u6cd5\u7684\u5e73\u5747\u5faa\u73af\u6b21\u6570\u5c31\u662f\u6570\u7ec4\u957f\u5ea6\u7684\u4e00\u534a \\(n / 2\\) \uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(\\Theta(n / 2) = \\Theta(n)\\) \u3002

\u4f46\u5bf9\u4e8e\u8f83\u4e3a\u590d\u6742\u7684\u7b97\u6cd5\uff0c\u8ba1\u7b97\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u5f80\u5f80\u662f\u6bd4\u8f83\u56f0\u96be\u7684\uff0c\u56e0\u4e3a\u5f88\u96be\u5206\u6790\u51fa\u5728\u6570\u636e\u5206\u5e03\u4e0b\u7684\u6574\u4f53\u6570\u5b66\u671f\u671b\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4f5c\u4e3a\u7b97\u6cd5\u6548\u7387\u7684\u8bc4\u5224\u6807\u51c6\u3002

\u4e3a\u4ec0\u4e48\u5f88\u5c11\u770b\u5230 \\(\\Theta\\) \u7b26\u53f7\uff1f

\u53ef\u80fd\u7531\u4e8e \\(O\\) \u7b26\u53f7\u8fc7\u4e8e\u6717\u6717\u4e0a\u53e3\uff0c\u6211\u4eec\u5e38\u5e38\u4f7f\u7528\u5b83\u6765\u8868\u793a\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u3002\u4f46\u4ece\u4e25\u683c\u610f\u4e49\u4e0a\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":"\u7b2c 3 \u7ae0 \u00a0 \u6570\u636e\u7ed3\u6784","text":"

Abstract

\u6570\u636e\u7ed3\u6784\u5982\u540c\u4e00\u526f\u7a33\u56fa\u800c\u591a\u6837\u7684\u6846\u67b6\u3002

\u5b83\u4e3a\u6570\u636e\u7684\u6709\u5e8f\u7ec4\u7ec7\u63d0\u4f9b\u4e86\u84dd\u56fe\uff0c\u4f7f\u7b97\u6cd5\u5f97\u4ee5\u5728\u6b64\u57fa\u7840\u4e0a\u751f\u52a8\u8d77\u6765\u3002

"},{"location":"chapter_data_structure/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 3.1 \u00a0 \u6570\u636e\u7ed3\u6784\u5206\u7c7b
  • 3.2 \u00a0 \u57fa\u672c\u6570\u636e\u7c7b\u578b
  • 3.3 \u00a0 \u6570\u5b57\u7f16\u7801 *
  • 3.4 \u00a0 \u5b57\u7b26\u7f16\u7801 *
  • 3.5 \u00a0 \u5c0f\u7ed3
"},{"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\uff0c\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\u7c7b\u578b\u3002

  • \u6574\u6570\u7c7b\u578b byte\u3001short\u3001int\u3001long \u3002
  • \u6d6e\u70b9\u6570\u7c7b\u578b float\u3001double \uff0c\u7528\u4e8e\u8868\u793a\u5c0f\u6570\u3002
  • \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\u3002
  • \u5e03\u5c14\u7c7b\u578b bool \uff0c\u7528\u4e8e\u8868\u793a\u201c\u662f\u201d\u4e0e\u201c\u5426\u201d\u5224\u65ad\u3002

\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4ee5\u4e8c\u8fdb\u5236\u7684\u5f62\u5f0f\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u4e2d\u3002\u4e00\u4e2a\u4e8c\u8fdb\u5236\u4f4d\u5373\u4e3a \\(1\\) \u6bd4\u7279\u3002\u5728\u7edd\u5927\u591a\u6570\u73b0\u4ee3\u7cfb\u7edf\u4e2d\uff0c\\(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\u3002\u4e0b\u9762\u4ee5 Java \u4e3a\u4f8b\u3002

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

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

\u8868 3-1 \u00a0 \u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u5360\u7528\u7a7a\u95f4\u548c\u53d6\u503c\u8303\u56f4

\u7c7b\u578b \u7b26\u53f7 \u5360\u7528\u7a7a\u95f4 \u6700\u5c0f\u503c \u6700\u5927\u503c \u9ed8\u8ba4\u503c \u6574\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}\\)

\u5bf9\u4e8e\u8868 3-1 \uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\u3002

  • C \u548c C++ \u672a\u660e\u786e\u89c4\u5b9a\u57fa\u672c\u6570\u636e\u7c7b\u578b\u5927\u5c0f\uff0c\u800c\u56e0\u5b9e\u73b0\u548c\u5e73\u53f0\u5404\u5f02\u3002\u8868 3-1 \u9075\u5faa LP64 \u6570\u636e\u6a21\u578b\uff0c\u5176\u7528\u4e8e\u5305\u62ec Linux \u548c macOS \u5728\u5185\u7684 Unix 64 \u4f4d\u64cd\u4f5c\u7cfb\u7edf\u3002
  • \u5b57\u7b26 char \u7684\u5927\u5c0f\u5728 C \u548c C++ \u4e2d\u4e3a 1 \u5b57\u8282\uff0c\u5728\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4e2d\u53d6\u51b3\u4e8e\u7279\u5b9a\u7684\u5b57\u7b26\u7f16\u7801\u65b9\u6cd5\uff0c\u8be6\u89c1\u201c\u5b57\u7b26\u7f16\u7801\u201d\u7ae0\u8282\u3002
  • \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\u8fd9\u662f\u56e0\u4e3a\u73b0\u4ee3\u8ba1\u7b97\u673a CPU \u901a\u5e38\u5c06 1 \u5b57\u8282\u4f5c\u4e3a\u6700\u5c0f\u5bfb\u5740\u5185\u5b58\u5355\u5143\u3002

\u90a3\u4e48\uff0c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e0e\u6570\u636e\u7ed3\u6784\u4e4b\u95f4\u6709\u4ec0\u4e48\u8054\u7cfb\u5462\uff1f\u6211\u4eec\u77e5\u9053\uff0c\u6570\u636e\u7ed3\u6784\u662f\u5728\u8ba1\u7b97\u673a\u4e2d\u7ec4\u7ec7\u4e0e\u5b58\u50a8\u6570\u636e\u7684\u65b9\u5f0f\u3002\u5b83\u7684\u4e3b\u8bed\u662f\u201c\u7ed3\u6784\u201d\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 \u6216\u662f\u5b57\u7b26 char \uff0c\u5219\u4e0e\u201c\u6570\u636e\u7ed3\u6784\u201d\u65e0\u5173\u3002

\u6362\u53e5\u8bdd\u8bf4\uff0c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u63d0\u4f9b\u4e86\u6570\u636e\u7684\u201c\u5185\u5bb9\u7c7b\u578b\u201d\uff0c\u800c\u6570\u636e\u7ed3\u6784\u63d0\u4f9b\u4e86\u6570\u636e\u7684\u201c\u7ec4\u7ec7\u65b9\u5f0f\u201d\u3002\u4f8b\u5982\u4ee5\u4e0b\u4ee3\u7801\uff0c\u6211\u4eec\u7528\u76f8\u540c\u7684\u6570\u636e\u7ed3\u6784\uff08\u6570\u7ec4\uff09\u6765\u5b58\u50a8\u4e0e\u8868\u793a\u4e0d\u540c\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec int\u3001float\u3001char\u3001bool \u7b49\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig
# \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nnumbers: list[int] = [0] * 5\ndecimals: list[float] = [0.0] * 5\n# Python \u7684\u5b57\u7b26\u5e94\u88ab\u770b\u4f5c\u957f\u5ea6\u4e3a\u4e00\u7684\u5b57\u7b26\u4e32\ncharacters: list[str] = ['0'] * 5\nbools: list[bool] = [False] * 5\n# Python \u7684\u5217\u8868\u53ef\u4ee5\u81ea\u7531\u5b58\u50a8\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5bf9\u8c61\u5f15\u7528\ndata = [0, 0.0, 'a', False, ListNode(0)]\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint numbers[5];\nfloat decimals[5];\nchar characters[5];\nbool bools[5];\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nboolean[] bools = new boolean[5];\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nbool[] bools = new bool[5];\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nvar numbers = [5]int{}\nvar decimals = [5]float64{}\nvar characters = [5]byte{}\nvar bools = [5]bool{}\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nlet numbers = Array(repeating: Int(), count: 5)\nlet decimals = Array(repeating: Double(), count: 5)\nlet characters = Array(repeating: Character(\"a\"), count: 5)\nlet bools = Array(repeating: Bool(), count: 5)\n
// JavaScript \u7684\u6570\u7ec4\u53ef\u4ee5\u81ea\u7531\u5b58\u50a8\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5bf9\u8c61\nconst array = [0, 0.0, 'a', false];\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nconst numbers: number[] = [];\nconst characters: string[] = [];\nconst bools: boolean[] = [];\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nList<int> numbers = List.filled(5, 0);\nList<double> decimals = List.filled(5, 0.0);\nList<String> characters = List.filled(5, 'a');\nList<bool> bools = List.filled(5, false);\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nlet numbers: Vec<i32> = vec![0; 5];\nlet decimals: Vec<float> = vec![0.0, 5];\nlet characters: Vec<char> = vec!['0'; 5];\nlet bools: Vec<bool> = vec![false; 5];\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint numbers[10];\nfloat decimals[10];\nchar characters[10];\nbool bools[10];\n
\n
"},{"location":"chapter_data_structure/character_encoding/","title":"3.4 \u00a0 \u5b57\u7b26\u7f16\u7801 *","text":"

\u5728\u8ba1\u7b97\u673a\u4e2d\uff0c\u6240\u6709\u6570\u636e\u90fd\u662f\u4ee5\u4e8c\u8fdb\u5236\u6570\u7684\u5f62\u5f0f\u5b58\u50a8\u7684\uff0c\u5b57\u7b26 char \u4e5f\u4e0d\u4f8b\u5916\u3002\u4e3a\u4e86\u8868\u793a\u5b57\u7b26\uff0c\u6211\u4eec\u9700\u8981\u5efa\u7acb\u4e00\u5957\u201c\u5b57\u7b26\u96c6\u201d\uff0c\u89c4\u5b9a\u6bcf\u4e2a\u5b57\u7b26\u548c\u4e8c\u8fdb\u5236\u6570\u4e4b\u95f4\u7684\u4e00\u4e00\u5bf9\u5e94\u5173\u7cfb\u3002\u6709\u4e86\u5b57\u7b26\u96c6\u4e4b\u540e\uff0c\u8ba1\u7b97\u673a\u5c31\u53ef\u4ee5\u901a\u8fc7\u67e5\u8868\u5b8c\u6210\u4e8c\u8fdb\u5236\u6570\u5230\u5b57\u7b26\u7684\u8f6c\u6362\u3002

"},{"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\u5982\u56fe 3-6 \u6240\u793a\uff0cASCII \u7801\u5305\u62ec\u82f1\u6587\u5b57\u6bcd\u7684\u5927\u5c0f\u5199\u3001\u6570\u5b57 0 ~ 9\u3001\u4e00\u4e9b\u6807\u70b9\u7b26\u53f7\uff0c\u4ee5\u53ca\u4e00\u4e9b\u63a7\u5236\u5b57\u7b26\uff08\u5982\u6362\u884c\u7b26\u548c\u5236\u8868\u7b26\uff09\u3002

\u56fe 3-6 \u00a0 ASCII \u7801

\u7136\u800c\uff0cASCII \u7801\u4ec5\u80fd\u591f\u8868\u793a\u82f1\u6587\u3002\u968f\u7740\u8ba1\u7b97\u673a\u7684\u5168\u7403\u5316\uff0c\u8bde\u751f\u4e86\u4e00\u79cd\u80fd\u591f\u8868\u793a\u66f4\u591a\u8bed\u8a00\u7684\u5b57\u7b26\u96c6\u300cEASCII\u300d\u3002\u5b83\u5728 ASCII \u7684 7 \u4f4d\u57fa\u7840\u4e0a\u6269\u5c55\u5230 8 \u4f4d\uff0c\u80fd\u591f\u8868\u793a 256 \u4e2a\u4e0d\u540c\u7684\u5b57\u7b26\u3002

\u5728\u4e16\u754c\u8303\u56f4\u5185\uff0c\u9646\u7eed\u51fa\u73b0\u4e86\u4e00\u6279\u9002\u7528\u4e8e\u4e0d\u540c\u5730\u533a\u7684 EASCII \u5b57\u7b26\u96c6\u3002\u8fd9\u4e9b\u5b57\u7b26\u96c6\u7684\u524d 128 \u4e2a\u5b57\u7b26\u7edf\u4e00\u4e3a ASCII \u7801\uff0c\u540e 128 \u4e2a\u5b57\u7b26\u5b9a\u4e49\u4e0d\u540c\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u8bed\u8a00\u7684\u9700\u6c42\u3002

"},{"location":"chapter_data_structure/character_encoding/#342-gbk","title":"3.4.2 \u00a0 GBK \u5b57\u7b26\u96c6","text":"

\u540e\u6765\u4eba\u4eec\u53d1\u73b0\uff0cEASCII \u7801\u4ecd\u7136\u65e0\u6cd5\u6ee1\u8db3\u8bb8\u591a\u8bed\u8a00\u7684\u5b57\u7b26\u6570\u91cf\u8981\u6c42\u3002\u6bd4\u5982\u6c49\u5b57\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\u300cGBK\u300d\u5b57\u7b26\u96c6\u662f\u5728 GB2312 \u7684\u57fa\u7840\u4e0a\u6269\u5c55\u5f97\u5230\u7684\uff0c\u5b83\u5171\u6536\u5f55\u4e86 21886 \u4e2a\u6c49\u5b57\u3002\u5728 GBK \u7684\u7f16\u7801\u65b9\u6848\u4e2d\uff0cASCII \u5b57\u7b26\u4f7f\u7528\u4e00\u4e2a\u5b57\u8282\u8868\u793a\uff0c\u6c49\u5b57\u4f7f\u7528\u4e24\u4e2a\u5b57\u8282\u8868\u793a\u3002

"},{"location":"chapter_data_structure/character_encoding/#343-unicode","title":"3.4.3 \u00a0 Unicode \u5b57\u7b26\u96c6","text":"

\u968f\u7740\u8ba1\u7b97\u673a\u7684\u84ec\u52c3\u53d1\u5c55\uff0c\u5b57\u7b26\u96c6\u4e0e\u7f16\u7801\u6807\u51c6\u767e\u82b1\u9f50\u653e\uff0c\u800c\u8fd9\u5e26\u6765\u4e86\u8bb8\u591a\u95ee\u9898\u3002\u4e00\u65b9\u9762\uff0c\u8fd9\u4e9b\u5b57\u7b26\u96c6\u4e00\u822c\u53ea\u5b9a\u4e49\u4e86\u7279\u5b9a\u8bed\u8a00\u7684\u5b57\u7b26\uff0c\u65e0\u6cd5\u5728\u591a\u8bed\u8a00\u73af\u5883\u4e0b\u6b63\u5e38\u5de5\u4f5c\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u540c\u4e00\u79cd\u8bed\u8a00\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\u7814\u7a76\u4eba\u5458\u5c31\u5728\u60f3\uff1a\u5982\u679c\u63a8\u51fa\u4e00\u4e2a\u8db3\u591f\u5b8c\u6574\u7684\u5b57\u7b26\u96c6\uff0c\u5c06\u4e16\u754c\u8303\u56f4\u5185\u7684\u6240\u6709\u8bed\u8a00\u548c\u7b26\u53f7\u90fd\u6536\u5f55\u5176\u4e2d\uff0c\u4e0d\u5c31\u53ef\u4ee5\u89e3\u51b3\u8de8\u8bed\u8a00\u73af\u5883\u548c\u4e71\u7801\u95ee\u9898\u4e86\u5417\uff1f\u5728\u8fd9\u79cd\u60f3\u6cd5\u7684\u9a71\u52a8\u4e0b\uff0c\u4e00\u4e2a\u5927\u800c\u5168\u7684\u5b57\u7b26\u96c6 Unicode \u5e94\u8fd0\u800c\u751f\u3002

\u300cUnicode\u300d\u7684\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\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a 2 \u5b57\u8282\u7684\u7f16\u7801\uff0c\u7cfb\u7edf\u5982\u4f55\u786e\u8ba4\u5b83\u662f\u4e00\u4e2a 2 \u5b57\u8282\u7684\u5b57\u7b26\u8fd8\u662f\u4e24\u4e2a 1 \u5b57\u8282\u7684\u5b57\u7b26\uff1f

\u5bf9\u4e8e\u4ee5\u4e0a\u95ee\u9898\uff0c\u4e00\u79cd\u76f4\u63a5\u7684\u89e3\u51b3\u65b9\u6848\u662f\u5c06\u6240\u6709\u5b57\u7b26\u5b58\u50a8\u4e3a\u7b49\u957f\u7684\u7f16\u7801\u3002\u5982\u56fe 3-7 \u6240\u793a\uff0c\u201cHello\u201d\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 1 \u5b57\u8282\uff0c\u201c\u7b97\u6cd5\u201d\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 2 \u5b57\u8282\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u9ad8\u4f4d\u586b 0 \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

\u56fe 3-7 \u00a0 Unicode \u7f16\u7801\u793a\u4f8b

\u7136\u800c ASCII \u7801\u5df2\u7ecf\u5411\u6211\u4eec\u8bc1\u660e\uff0c\u7f16\u7801\u82f1\u6587\u53ea\u9700\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\u4e24\u500d\uff0c\u975e\u5e38\u6d6a\u8d39\u5185\u5b58\u7a7a\u95f4\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u4e00\u79cd\u66f4\u52a0\u9ad8\u6548\u7684 Unicode \u7f16\u7801\u65b9\u6cd5\u3002

"},{"location":"chapter_data_structure/character_encoding/#344-utf-8","title":"3.4.4 \u00a0 UTF-8 \u7f16\u7801","text":"

\u76ee\u524d\uff0cUTF-8 \u5df2\u6210\u4e3a\u56fd\u9645\u4e0a\u4f7f\u7528\u6700\u5e7f\u6cdb\u7684 Unicode \u7f16\u7801\u65b9\u6cd5\u3002\u5b83\u662f\u4e00\u79cd\u53ef\u53d8\u957f\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\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\u3002

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

\u56fe 3-8 \u5c55\u793a\u4e86\u201cHello\u7b97\u6cd5\u201d\u5bf9\u5e94\u7684 UTF-8 \u7f16\u7801\u3002\u89c2\u5bdf\u53d1\u73b0\uff0c\u7531\u4e8e\u6700\u9ad8 \\(n\\) \u4f4d\u90fd\u88ab\u8bbe\u7f6e\u4e3a \\(1\\) \uff0c\u56e0\u6b64\u7cfb\u7edf\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u6700\u9ad8\u4f4d \\(1\\) \u7684\u4e2a\u6570\u6765\u89e3\u6790\u51fa\u5b57\u7b26\u7684\u957f\u5ea6\u4e3a \\(n\\) \u3002

\u4f46\u4e3a\u4ec0\u4e48\u8981\u5c06\u5176\u4f59\u6240\u6709\u5b57\u8282\u7684\u9ad8 2 \u4f4d\u90fd\u8bbe\u7f6e\u4e3a \\(10\\) \u5462\uff1f\u5b9e\u9645\u4e0a\uff0c\u8fd9\u4e2a \\(10\\) \u80fd\u591f\u8d77\u5230\u6821\u9a8c\u7b26\u7684\u4f5c\u7528\u3002\u5047\u8bbe\u7cfb\u7edf\u4ece\u4e00\u4e2a\u9519\u8bef\u7684\u5b57\u8282\u5f00\u59cb\u89e3\u6790\u6587\u672c\uff0c\u5b57\u8282\u5934\u90e8\u7684 \\(10\\) \u80fd\u591f\u5e2e\u52a9\u7cfb\u7edf\u5feb\u901f\u7684\u5224\u65ad\u51fa\u5f02\u5e38\u3002

\u4e4b\u6240\u4ee5\u5c06 \\(10\\) \u5f53\u4f5c\u6821\u9a8c\u7b26\uff0c\u662f\u56e0\u4e3a\u5728 UTF-8 \u7f16\u7801\u89c4\u5219\u4e0b\uff0c\u4e0d\u53ef\u80fd\u6709\u5b57\u7b26\u7684\u6700\u9ad8\u4e24\u4f4d\u662f \\(10\\) \u3002\u8fd9\u4e2a\u7ed3\u8bba\u53ef\u4ee5\u7528\u53cd\u8bc1\u6cd5\u6765\u8bc1\u660e\uff1a\u5047\u8bbe\u4e00\u4e2a\u5b57\u7b26\u7684\u6700\u9ad8\u4e24\u4f4d\u662f \\(10\\) \uff0c\u8bf4\u660e\u8be5\u5b57\u7b26\u7684\u957f\u5ea6\u4e3a \\(1\\) \uff0c\u5bf9\u5e94 ASCII \u7801\u3002\u800c ASCII \u7801\u7684\u6700\u9ad8\u4f4d\u5e94\u8be5\u662f \\(0\\) \uff0c\u4e0e\u5047\u8bbe\u77db\u76fe\u3002

\u56fe 3-8 \u00a0 UTF-8 \u7f16\u7801\u793a\u4f8b

\u9664\u4e86 UTF-8 \u4e4b\u5916\uff0c\u5e38\u89c1\u7684\u7f16\u7801\u65b9\u5f0f\u8fd8\u5305\u62ec\u4ee5\u4e0b\u4e24\u79cd\u3002

  • 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\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 ASCII \u5b57\u7b26\u5360\u6bd4\u8f83\u9ad8\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\u5728\u7b49\u957f\u7f16\u7801\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u770b\u4f5c\u6570\u7ec4\u6765\u5904\u7406\uff0c\u8fd9\u79cd\u505a\u6cd5\u5177\u6709\u4ee5\u4e0b\u4f18\u70b9\u3002

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

\u5b9e\u9645\u4e0a\uff0c\u7f16\u7a0b\u8bed\u8a00\u7684\u5b57\u7b26\u7f16\u7801\u65b9\u6848\u8bbe\u8ba1\u662f\u4e00\u4e2a\u5f88\u6709\u8da3\u7684\u8bdd\u9898\uff0c\u5176\u6d89\u53ca\u5230\u8bb8\u591a\u56e0\u7d20\u3002

  • 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\u4ece\u800c\u4e27\u5931\u4e86\u7b49\u957f\u7f16\u7801\u7684\u4f18\u52bf\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u5904\u7406\u4ee3\u7406\u5bf9\u9700\u8981\u589e\u52a0\u989d\u5916\u4ee3\u7801\uff0c\u8fd9\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\u4e00\u4e9b\u4e0d\u540c\u7684\u7f16\u7801\u65b9\u6848\u3002

  • Python \u4e2d\u7684 str \u4f7f\u7528 Unicode \u7f16\u7801\uff0c\u5e76\u91c7\u7528\u4e00\u79cd\u7075\u6d3b\u7684\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u5b58\u50a8\u7684\u5b57\u7b26\u957f\u5ea6\u53d6\u51b3\u4e8e\u5b57\u7b26\u4e32\u4e2d\u6700\u5927\u7684 Unicode \u7801\u70b9\u3002\u82e5\u5b57\u7b26\u4e32\u4e2d\u5168\u90e8\u662f ASCII \u5b57\u7b26\uff0c\u5219\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 1 \u4e2a\u5b57\u8282\uff1b\u5982\u679c\u6709\u5b57\u7b26\u8d85\u51fa\u4e86 ASCII \u8303\u56f4\uff0c\u4f46\u5168\u90e8\u5728\u57fa\u672c\u591a\u8bed\u8a00\u5e73\u9762\uff08BMP\uff09\u5185\uff0c\u5219\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 2 \u4e2a\u5b57\u8282\uff1b\u5982\u679c\u6709\u8d85\u51fa BMP \u7684\u5b57\u7b26\uff0c\u5219\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 4 \u4e2a\u5b57\u8282\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\u901a\u5e38\u4f1a\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a UTF-8 \u683c\u5f0f\uff0c\u4ee5\u8fbe\u5230\u6700\u4f18\u7684\u517c\u5bb9\u6027\u548c\u7a7a\u95f4\u6548\u7387\u3002

"},{"location":"chapter_data_structure/classification_of_data_structure/","title":"3.1 \u00a0 \u6570\u636e\u7ed3\u6784\u5206\u7c7b","text":"

\u5e38\u89c1\u7684\u6570\u636e\u7ed3\u6784\u5305\u62ec\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\uff0c\u5b83\u4eec\u53ef\u4ee5\u4ece\u201c\u903b\u8f91\u7ed3\u6784\u201d\u548c\u201c\u7269\u7406\u7ed3\u6784\u201d\u4e24\u4e2a\u7ef4\u5ea6\u8fdb\u884c\u5206\u7c7b\u3002

"},{"location":"chapter_data_structure/classification_of_data_structure/#311","title":"3.1.1 \u00a0 \u903b\u8f91\u7ed3\u6784\uff1a\u7ebf\u6027\u4e0e\u975e\u7ebf\u6027","text":"

\u903b\u8f91\u7ed3\u6784\u63ed\u793a\u4e86\u6570\u636e\u5143\u7d20\u4e4b\u95f4\u7684\u903b\u8f91\u5173\u7cfb\u3002\u5728\u6570\u7ec4\u548c\u94fe\u8868\u4e2d\uff0c\u6570\u636e\u6309\u7167\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

\u5982\u56fe 3-1 \u6240\u793a\uff0c\u903b\u8f91\u7ed3\u6784\u53ef\u88ab\u5206\u4e3a\u201c\u7ebf\u6027\u201d\u548c\u201c\u975e\u7ebf\u6027\u201d\u4e24\u5927\u7c7b\u3002\u7ebf\u6027\u7ed3\u6784\u6bd4\u8f83\u76f4\u89c2\uff0c\u6307\u6570\u636e\u5728\u903b\u8f91\u5173\u7cfb\u4e0a\u5448\u7ebf\u6027\u6392\u5217\uff1b\u975e\u7ebf\u6027\u7ed3\u6784\u5219\u76f8\u53cd\uff0c\u5448\u975e\u7ebf\u6027\u6392\u5217\u3002

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

\u56fe 3-1 \u00a0 \u7ebf\u6027\u4e0e\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784

\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u8fdb\u4e00\u6b65\u88ab\u5212\u5206\u4e3a\u6811\u5f62\u7ed3\u6784\u548c\u7f51\u72b6\u7ed3\u6784\u3002

  • \u7ebf\u6027\u7ed3\u6784\uff1a\u6570\u7ec4\u3001\u94fe\u8868\u3001\u961f\u5217\u3001\u6808\u3001\u54c8\u5e0c\u8868\uff0c\u5143\u7d20\u4e4b\u95f4\u662f\u4e00\u5bf9\u4e00\u7684\u987a\u5e8f\u5173\u7cfb\u3002
  • \u6811\u5f62\u7ed3\u6784\uff1a\u6811\u3001\u5806\u3001\u54c8\u5e0c\u8868\uff0c\u5143\u7d20\u4e4b\u95f4\u662f\u4e00\u5bf9\u591a\u7684\u5173\u7cfb\u3002
  • \u7f51\u72b6\u7ed3\u6784\uff1a\u56fe\uff0c\u5143\u7d20\u4e4b\u95f4\u662f\u591a\u5bf9\u591a\u7684\u5173\u7cfb\u3002
"},{"location":"chapter_data_structure/classification_of_data_structure/#312","title":"3.1.2 \u00a0 \u7269\u7406\u7ed3\u6784\uff1a\u8fde\u7eed\u4e0e\u5206\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\u786c\u76d8\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\u5185\u5b58\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\u56fe 3-2 \u5c55\u793a\u4e86\u4e00\u4e2a\u8ba1\u7b97\u673a\u5185\u5b58\u6761\uff0c\u5176\u4e2d\u6bcf\u4e2a\u9ed1\u8272\u65b9\u5757\u90fd\u5305\u542b\u4e00\u5757\u5185\u5b58\u7a7a\u95f4\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u5185\u5b58\u60f3\u8c61\u6210\u4e00\u4e2a\u5de8\u5927\u7684 Excel \u8868\u683c\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5355\u5143\u683c\u90fd\u53ef\u4ee5\u5b58\u50a8\u4e00\u5b9a\u5927\u5c0f\u7684\u6570\u636e\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\u5185\u5b58\u5730\u5740\u6765\u8bbf\u95ee\u76ee\u6807\u4f4d\u7f6e\u7684\u6570\u636e\u3002\u5982\u56fe 3-2 \u6240\u793a\uff0c\u8ba1\u7b97\u673a\u6839\u636e\u7279\u5b9a\u89c4\u5219\u4e3a\u8868\u683c\u4e2d\u7684\u6bcf\u4e2a\u5355\u5143\u683c\u5206\u914d\u7f16\u53f7\uff0c\u786e\u4fdd\u6bcf\u4e2a\u5185\u5b58\u7a7a\u95f4\u90fd\u6709\u552f\u4e00\u7684\u5185\u5b58\u5730\u5740\u3002\u6709\u4e86\u8fd9\u4e9b\u5730\u5740\uff0c\u7a0b\u5e8f\u4fbf\u53ef\u4ee5\u8bbf\u95ee\u5185\u5b58\u4e2d\u7684\u6570\u636e\u3002

\u56fe 3-2 \u00a0 \u5185\u5b58\u6761\u3001\u5185\u5b58\u7a7a\u95f4\u3001\u5185\u5b58\u5730\u5740

\u5185\u5b58\u662f\u6240\u6709\u7a0b\u5e8f\u7684\u5171\u4eab\u8d44\u6e90\uff0c\u5f53\u67d0\u5757\u5185\u5b58\u88ab\u67d0\u4e2a\u7a0b\u5e8f\u5360\u7528\u65f6\uff0c\u5219\u65e0\u6cd5\u88ab\u5176\u4ed6\u7a0b\u5e8f\u540c\u65f6\u4f7f\u7528\u4e86\u3002\u56e0\u6b64\u5728\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u8bbe\u8ba1\u4e2d\uff0c\u5185\u5b58\u8d44\u6e90\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u6bd4\u5982\uff0c\u7b97\u6cd5\u6240\u5360\u7528\u7684\u5185\u5b58\u5cf0\u503c\u4e0d\u5e94\u8d85\u8fc7\u7cfb\u7edf\u5269\u4f59\u7a7a\u95f2\u5185\u5b58\uff1b\u5982\u679c\u7f3a\u5c11\u8fde\u7eed\u5927\u5757\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u90a3\u4e48\u6240\u9009\u7528\u7684\u6570\u636e\u7ed3\u6784\u5fc5\u987b\u80fd\u591f\u5b58\u50a8\u5728\u5206\u6563\u7684\u5185\u5b58\u7a7a\u95f4\u5185\u3002

\u5982\u56fe 3-3 \u6240\u793a\uff0c\u7269\u7406\u7ed3\u6784\u53cd\u6620\u4e86\u6570\u636e\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u53ef\u5206\u4e3a\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\uff08\u6570\u7ec4\uff09\u548c\u5206\u6563\u7a7a\u95f4\u5b58\u50a8\uff08\u94fe\u8868\uff09\u3002\u7269\u7406\u7ed3\u6784\u4ece\u5e95\u5c42\u51b3\u5b9a\u4e86\u6570\u636e\u7684\u8bbf\u95ee\u3001\u66f4\u65b0\u3001\u589e\u5220\u7b49\u64cd\u4f5c\u65b9\u6cd5\uff0c\u540c\u65f6\u5728\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u65b9\u9762\u5448\u73b0\u51fa\u4e92\u8865\u7684\u7279\u70b9\u3002

\u56fe 3-3 \u00a0 \u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u4e0e\u5206\u6563\u7a7a\u95f4\u5b58\u50a8

\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6240\u6709\u6570\u636e\u7ed3\u6784\u90fd\u662f\u57fa\u4e8e\u6570\u7ec4\u3001\u94fe\u8868\u6216\u4e8c\u8005\u7684\u7ec4\u5408\u5b9e\u73b0\u7684\u3002\u4f8b\u5982\uff0c\u6808\u548c\u961f\u5217\u65e2\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u94fe\u8868\u5b9e\u73b0\uff1b\u800c\u54c8\u5e0c\u8868\u7684\u5b9e\u73b0\u53ef\u80fd\u540c\u65f6\u5305\u542b\u6570\u7ec4\u548c\u94fe\u8868\u3002

  • \u57fa\u4e8e\u6570\u7ec4\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\u3002
  • \u57fa\u4e8e\u94fe\u8868\u53ef\u5b9e\u73b0\uff1a\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\u7b49\u3002

\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u4e5f\u88ab\u79f0\u4e3a\u201c\u9759\u6001\u6570\u636e\u7ed3\u6784\u201d\uff0c\u8fd9\u610f\u5473\u7740\u6b64\u7c7b\u6570\u636e\u7ed3\u6784\u5728\u521d\u59cb\u5316\u540e\u957f\u5ea6\u4e0d\u53ef\u53d8\u3002\u76f8\u5bf9\u5e94\u5730\uff0c\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u88ab\u79f0\u4e3a\u201c\u52a8\u6001\u6570\u636e\u7ed3\u6784\u201d\uff0c\u8fd9\u7c7b\u6570\u636e\u7ed3\u6784\u5728\u521d\u59cb\u5316\u540e\uff0c\u4ecd\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u5bf9\u5176\u957f\u5ea6\u8fdb\u884c\u8c03\u6574\u3002

Tip

\u5982\u679c\u4f60\u611f\u89c9\u7269\u7406\u7ed3\u6784\u7406\u89e3\u8d77\u6765\u6709\u56f0\u96be\uff0c\u5efa\u8bae\u5148\u9605\u8bfb\u4e0b\u4e00\u7ae0\u201c\u6570\u7ec4\u4e0e\u94fe\u8868\u201d\uff0c\u7136\u540e\u518d\u56de\u987e\u672c\u8282\u5185\u5bb9\u3002

"},{"location":"chapter_data_structure/number_encoding/","title":"3.3 \u00a0 \u6570\u5b57\u7f16\u7801 *","text":"

Note

\u5728\u672c\u4e66\u4e2d\uff0c\u6807\u9898\u5e26\u6709\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\u53ef\u4ee5\u5148\u8df3\u8fc7\uff0c\u7b49\u5b66\u5b8c\u5fc5\u8bfb\u7ae0\u8282\u540e\u518d\u5355\u72ec\u653b\u514b\u3002

"},{"location":"chapter_data_structure/number_encoding/#331","title":"3.3.1 \u00a0 \u6574\u6570\u7f16\u7801","text":"

\u5728\u4e0a\u4e00\u8282\u7684\u8868\u683c\u4e2d\u6211\u4eec\u53d1\u73b0\uff0c\u6240\u6709\u6574\u6570\u7c7b\u578b\u80fd\u591f\u8868\u793a\u7684\u8d1f\u6570\u90fd\u6bd4\u6b63\u6570\u591a\u4e00\u4e2a\uff0c\u4f8b\u5982 byte \u7684\u53d6\u503c\u8303\u56f4\u662f \\([-128, 127]\\) \u3002\u8fd9\u4e2a\u73b0\u8c61\u6bd4\u8f83\u53cd\u76f4\u89c9\uff0c\u5b83\u7684\u5185\u5728\u539f\u56e0\u6d89\u53ca\u5230\u539f\u7801\u3001\u53cd\u7801\u3001\u8865\u7801\u7684\u76f8\u5173\u77e5\u8bc6\u3002

\u9996\u5148\u9700\u8981\u6307\u51fa\uff0c\u6570\u5b57\u662f\u4ee5\u201c\u8865\u7801\u201d\u7684\u5f62\u5f0f\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u4e2d\u7684\u3002\u5728\u5206\u6790\u8fd9\u6837\u505a\u7684\u539f\u56e0\u4e4b\u524d\uff0c\u6211\u4eec\u9996\u5148\u7ed9\u51fa\u4e09\u8005\u7684\u5b9a\u4e49\u3002

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

\u56fe 3-4 \u5c55\u793a\u4e86\u539f\u7801\u3001\u53cd\u7801\u548c\u8865\u7801\u4e4b\u95f4\u7684\u8f6c\u6362\u65b9\u6cd5\u3002

\u56fe 3-4 \u00a0 \u539f\u7801\u3001\u53cd\u7801\u4e0e\u8865\u7801\u4e4b\u95f4\u7684\u76f8\u4e92\u8f6c\u6362

\u300c\u539f\u7801 true form\u300d\u867d\u7136\u6700\u76f4\u89c2\uff0c\u4f46\u5b58\u5728\u4e00\u4e9b\u5c40\u9650\u6027\u3002\u4e00\u65b9\u9762\uff0c\u8d1f\u6570\u7684\u539f\u7801\u4e0d\u80fd\u76f4\u63a5\u7528\u4e8e\u8fd0\u7b97\u3002\u4f8b\u5982\u5728\u539f\u7801\u4e0b\u8ba1\u7b97 \\(1 + (-2)\\) \uff0c\u5f97\u5230\u7684\u7ed3\u679c\u662f \\(-3\\) \uff0c\u8fd9\u663e\u7136\u662f\u4e0d\u5bf9\u7684\u3002

\\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 + 1000 \\; 0010 \\newline & = 1000 \\; 0011 \\newline & \\rightarrow -3 \\end{aligned} \\]

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

\\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 \\; \\text{(\u539f\u7801)} + 1000 \\; 0010 \\; \\text{(\u539f\u7801)} \\newline & = 0000 \\; 0001 \\; \\text{(\u53cd\u7801)} + 1111 \\; 1101 \\; \\text{(\u53cd\u7801)} \\newline & = 1111 \\; 1110 \\; \\text{(\u53cd\u7801)} \\newline & = 1000 \\; 0001 \\; \\text{(\u539f\u7801)} \\newline & \\rightarrow -1 \\end{aligned} \\]

\u53e6\u4e00\u65b9\u9762\uff0c\u6570\u5b57\u96f6\u7684\u539f\u7801\u6709 \\(+0\\) \u548c \\(-0\\) \u4e24\u79cd\u8868\u793a\u65b9\u5f0f\u3002\u8fd9\u610f\u5473\u7740\u6570\u5b57\u96f6\u5bf9\u5e94\u7740\u4e24\u4e2a\u4e0d\u540c\u7684\u4e8c\u8fdb\u5236\u7f16\u7801\uff0c\u5176\u53ef\u80fd\u4f1a\u5e26\u6765\u6b67\u4e49\u3002\u6bd4\u5982\u5728\u6761\u4ef6\u5224\u65ad\u4e2d\uff0c\u5982\u679c\u6ca1\u6709\u533a\u5206\u6b63\u96f6\u548c\u8d1f\u96f6\uff0c\u5219\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5224\u65ad\u7ed3\u679c\u51fa\u9519\u3002\u800c\u5982\u679c\u6211\u4eec\u60f3\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 & \\rightarrow 0000 \\; 0000 \\newline -0 & \\rightarrow 1000 \\; 0000 \\end{aligned} \\]

\u4e0e\u539f\u7801\u4e00\u6837\uff0c\u53cd\u7801\u4e5f\u5b58\u5728\u6b63\u8d1f\u96f6\u6b67\u4e49\u95ee\u9898\uff0c\u56e0\u6b64\u8ba1\u7b97\u673a\u8fdb\u4e00\u6b65\u5f15\u5165\u4e86\u300c\u8865\u7801 2's complement code\u300d\u3002\u6211\u4eec\u5148\u6765\u89c2\u5bdf\u4e00\u4e0b\u8d1f\u96f6\u7684\u539f\u7801\u3001\u53cd\u7801\u3001\u8865\u7801\u7684\u8f6c\u6362\u8fc7\u7a0b\uff1a

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

\u5728\u8d1f\u96f6\u7684\u53cd\u7801\u57fa\u7840\u4e0a\u52a0 \\(1\\) \u4f1a\u4ea7\u751f\u8fdb\u4f4d\uff0c\u4f46 byte \u7c7b\u578b\u7684\u957f\u5ea6\u53ea\u6709 8 \u4f4d\uff0c\u56e0\u6b64\u6ea2\u51fa\u5230\u7b2c 9 \u4f4d\u7684 \\(1\\) \u4f1a\u88ab\u820d\u5f03\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8d1f\u96f6\u7684\u8865\u7801\u4e3a \\(0000 \\; 0000\\) \uff0c\u4e0e\u6b63\u96f6\u7684\u8865\u7801\u76f8\u540c\u3002\u8fd9\u610f\u5473\u7740\u5728\u8865\u7801\u8868\u793a\u4e2d\u53ea\u5b58\u5728\u4e00\u4e2a\u96f6\uff0c\u6b63\u8d1f\u96f6\u6b67\u4e49\u4ece\u800c\u5f97\u5230\u89e3\u51b3\u3002

\u8fd8\u5269\u4f59\u6700\u540e\u4e00\u4e2a\u7591\u60d1\uff1abyte \u7c7b\u578b\u7684\u53d6\u503c\u8303\u56f4\u662f \\([-128, 127]\\) \uff0c\u591a\u51fa\u6765\u7684\u4e00\u4e2a\u8d1f\u6570 \\(-128\\) \u662f\u5982\u4f55\u5f97\u5230\u7684\u5462\uff1f\u6211\u4eec\u6ce8\u610f\u5230\uff0c\u533a\u95f4 \\([-127, +127]\\) \u5185\u7684\u6240\u6709\u6574\u6570\u90fd\u6709\u5bf9\u5e94\u7684\u539f\u7801\u3001\u53cd\u7801\u548c\u8865\u7801\uff0c\u5e76\u4e14\u539f\u7801\u548c\u8865\u7801\u4e4b\u95f4\u662f\u53ef\u4ee5\u4e92\u76f8\u8f6c\u6362\u7684\u3002

\u7136\u800c\uff0c\u8865\u7801 \\(1000 \\; 0000\\) \u662f\u4e00\u4e2a\u4f8b\u5916\uff0c\u5b83\u5e76\u6ca1\u6709\u5bf9\u5e94\u7684\u539f\u7801\u3002\u6839\u636e\u8f6c\u6362\u65b9\u6cd5\uff0c\u6211\u4eec\u5f97\u5230\u8be5\u8865\u7801\u7684\u539f\u7801\u4e3a \\(0000 \\; 0000\\) \u3002\u8fd9\u663e\u7136\u662f\u77db\u76fe\u7684\uff0c\u56e0\u4e3a\u8be5\u539f\u7801\u8868\u793a\u6570\u5b57 \\(0\\) \uff0c\u5b83\u7684\u8865\u7801\u5e94\u8be5\u662f\u81ea\u8eab\u3002\u8ba1\u7b97\u673a\u89c4\u5b9a\u8fd9\u4e2a\u7279\u6b8a\u7684\u8865\u7801 \\(1000 \\; 0000\\) \u4ee3\u8868 \\(-128\\) \u3002\u5b9e\u9645\u4e0a\uff0c\\((-1) + (-127)\\) \u5728\u8865\u7801\u4e0b\u7684\u8ba1\u7b97\u7ed3\u679c\u5c31\u662f \\(-128\\) \u3002

\\[ \\begin{aligned} & (-127) + (-1) \\newline & \\rightarrow 1111 \\; 1111 \\; \\text{(\u539f\u7801)} + 1000 \\; 0001 \\; \\text{(\u539f\u7801)} \\newline & = 1000 \\; 0000 \\; \\text{(\u53cd\u7801)} + 1111 \\; 1110 \\; \\text{(\u53cd\u7801)} \\newline & = 1000 \\; 0001 \\; \\text{(\u8865\u7801)} + 1111 \\; 1111 \\; \\text{(\u8865\u7801)} \\newline & = 1000 \\; 0000 \\; \\text{(\u8865\u7801)} \\newline & \\rightarrow -128 \\end{aligned} \\]

\u4f60\u53ef\u80fd\u5df2\u7ecf\u53d1\u73b0\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\u8fd0\u7b97\u901f\u5ea6\u66f4\u5feb\u3002

\u8bf7\u6ce8\u610f\uff0c\u8fd9\u5e76\u4e0d\u610f\u5473\u7740\u8ba1\u7b97\u673a\u53ea\u80fd\u505a\u52a0\u6cd5\u3002\u901a\u8fc7\u5c06\u52a0\u6cd5\u4e0e\u4e00\u4e9b\u57fa\u672c\u903b\u8f91\u8fd0\u7b97\u7ed3\u5408\uff0c\u8ba1\u7b97\u673a\u80fd\u591f\u5b9e\u73b0\u5404\u79cd\u5176\u4ed6\u7684\u6570\u5b66\u8fd0\u7b97\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u51cf\u6cd5 \\(a - b\\) \u53ef\u4ee5\u8f6c\u6362\u4e3a\u8ba1\u7b97\u52a0\u6cd5 \\(a + (-b)\\) \uff1b\u8ba1\u7b97\u4e58\u6cd5\u548c\u9664\u6cd5\u53ef\u4ee5\u8f6c\u6362\u4e3a\u8ba1\u7b97\u591a\u6b21\u52a0\u6cd5\u6216\u51cf\u6cd5\u3002

\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u603b\u7ed3\u51fa\u8ba1\u7b97\u673a\u4f7f\u7528\u8865\u7801\u7684\u539f\u56e0\uff1a\u57fa\u4e8e\u8865\u7801\u8868\u793a\uff0c\u8ba1\u7b97\u673a\u53ef\u4ee5\u7528\u540c\u6837\u7684\u7535\u8def\u548c\u64cd\u4f5c\u6765\u5904\u7406\u6b63\u6570\u548c\u8d1f\u6570\u7684\u52a0\u6cd5\uff0c\u4e0d\u9700\u8981\u8bbe\u8ba1\u7279\u6b8a\u7684\u786c\u4ef6\u7535\u8def\u6765\u5904\u7406\u51cf\u6cd5\uff0c\u5e76\u4e14\u65e0\u987b\u7279\u522b\u5904\u7406\u6b63\u8d1f\u96f6\u7684\u6b67\u4e49\u95ee\u9898\u3002\u8fd9\u5927\u5927\u7b80\u5316\u4e86\u786c\u4ef6\u8bbe\u8ba1\uff0c\u63d0\u9ad8\u4e86\u8fd0\u7b97\u6548\u7387\u3002

\u8865\u7801\u7684\u8bbe\u8ba1\u975e\u5e38\u7cbe\u5999\uff0c\u56e0\u7bc7\u5e45\u5173\u7cfb\u6211\u4eec\u5c31\u5148\u4ecb\u7ecd\u5230\u8fd9\u91cc\uff0c\u5efa\u8bae\u6709\u5174\u8da3\u7684\u8bfb\u8005\u8fdb\u4e00\u6b65\u6df1\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\u8bb0\u4e00\u4e2a 32-bit \u957f\u5ea6\u7684\u4e8c\u8fdb\u5236\u6570\u4e3a\uff1a

\\[ b_{31} b_{30} b_{29} \\ldots b_2 b_1 b_0 \\]

\u6839\u636e IEEE 754 \u6807\u51c6\uff0c32-bit \u957f\u5ea6\u7684 float \u7531\u4ee5\u4e0b\u4e09\u4e2a\u90e8\u5206\u6784\u6210\u3002

  • \u7b26\u53f7\u4f4d \\(\\mathrm{S}\\) \uff1a\u5360 1 bit \uff0c\u5bf9\u5e94 \\(b_{31}\\) \u3002
  • \u6307\u6570\u4f4d \\(\\mathrm{E}\\) \uff1a\u5360 8 bits \uff0c\u5bf9\u5e94 \\(b_{30} b_{29} \\ldots b_{23}\\) \u3002
  • \u5206\u6570\u4f4d \\(\\mathrm{N}\\) \uff1a\u5360 23 bits \uff0c\u5bf9\u5e94 \\(b_{22} b_{21} \\ldots b_0\\) \u3002

\u4e8c\u8fdb\u5236\u6570 float \u5bf9\u5e94\u7684\u503c\u7684\u8ba1\u7b97\u65b9\u6cd5\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\uff1a

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

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

\\[ \\begin{aligned} \\mathrm{S} \\in & \\{ 0, 1\\}, \\quad \\mathrm{E} \\in \\{ 1, 2, \\dots, 254 \\} \\newline (1 + \\mathrm{N}) = & (1 + \\sum_{i=1}^{23} b_{23-i} 2^{-i}) \\subset [1, 2 - 2^{-23}] \\end{aligned} \\]

\u56fe 3-5 \u00a0 IEEE 754 \u6807\u51c6\u4e0b\u7684 float \u7684\u8ba1\u7b97\u793a\u4f8b

\u89c2\u5bdf\u56fe 3-5 \uff0c\u7ed9\u5b9a\u4e00\u4e2a\u793a\u4f8b\u6570\u636e \\(\\mathrm{S} = 0\\) \uff0c \\(\\mathrm{E} = 124\\) \uff0c\\(\\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\\) \uff0c\u5219\u6709\uff1a

\\[ \\text { val } = (-1)^0 \\times 2^{124 - 127} \\times (1 + 0.375) = 0.171875 \\]

\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u56de\u7b54\u6700\u521d\u7684\u95ee\u9898\uff1afloat \u7684\u8868\u793a\u65b9\u5f0f\u5305\u542b\u6307\u6570\u4f4d\uff0c\u5bfc\u81f4\u5176\u53d6\u503c\u8303\u56f4\u8fdc\u5927\u4e8e int \u3002\u6839\u636e\u4ee5\u4e0a\u8ba1\u7b97\uff0cfloat \u53ef\u8868\u793a\u7684\u6700\u5927\u6b63\u6570\u4e3a \\(2^{254 - 127} \\times (2 - 2^{-23}) \\approx 3.4 \\times 10^{38}\\) \uff0c\u5207\u6362\u7b26\u53f7\u4f4d\u4fbf\u53ef\u5f97\u5230\u6700\u5c0f\u8d1f\u6570\u3002

\u5c3d\u7ba1\u6d6e\u70b9\u6570 float \u6269\u5c55\u4e86\u53d6\u503c\u8303\u56f4\uff0c\u4f46\u5176\u526f\u4f5c\u7528\u662f\u727a\u7272\u4e86\u7cbe\u5ea6\u3002\u6574\u6570\u7c7b\u578b int \u5c06\u5168\u90e8 32 \u4f4d\u7528\u4e8e\u8868\u793a\u6570\u5b57\uff0c\u6570\u5b57\u662f\u5747\u5300\u5206\u5e03\u7684\uff1b\u800c\u7531\u4e8e\u6307\u6570\u4f4d\u7684\u5b58\u5728\uff0c\u6d6e\u70b9\u6570 float \u7684\u6570\u503c\u8d8a\u5927\uff0c\u76f8\u90bb\u4e24\u4e2a\u6570\u5b57\u4e4b\u95f4\u7684\u5dee\u503c\u5c31\u4f1a\u8d8b\u5411\u8d8a\u5927\u3002

\u5982\u8868 3-2 \u6240\u793a\uff0c\u6307\u6570\u4f4d \\(E = 0\\) \u548c \\(E = 255\\) \u5177\u6709\u7279\u6b8a\u542b\u4e49\uff0c\u7528\u4e8e\u8868\u793a\u96f6\u3001\u65e0\u7a77\u5927\u3001\\(\\mathrm{NaN}\\) \u7b49\u3002

\u8868 3-2 \u00a0 \u6307\u6570\u4f4d\u542b\u4e49

\u6307\u6570\u4f4d E \u5206\u6570\u4f4d \\(\\mathrm{N} = 0\\) \u5206\u6570\u4f4d \\(\\mathrm{N} \\ne 0\\) \u8ba1\u7b97\u516c\u5f0f \\(0\\) \\(\\pm 0\\) \u6b21\u6b63\u89c4\u6570 \\((-1)^{\\mathrm{S}} \\times 2^{-126} \\times (0.\\mathrm{N})\\) \\(1, 2, \\dots, 254\\) \u6b63\u89c4\u6570 \u6b63\u89c4\u6570 \\((-1)^{\\mathrm{S}} \\times 2^{(\\mathrm{E} -127)} \\times (1.\\mathrm{N})\\) \\(255\\) \\(\\pm \\infty\\) \\(\\mathrm{NaN}\\)

\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6b21\u6b63\u89c4\u6570\u663e\u8457\u63d0\u5347\u4e86\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\u3002\u6700\u5c0f\u6b63\u6b63\u89c4\u6570\u4e3a \\(2^{-126}\\) \uff0c\u6700\u5c0f\u6b63\u6b21\u6b63\u89c4\u6570\u4e3a \\(2^{-126} \\times 2^{-23}\\) \u3002

\u53cc\u7cbe\u5ea6 double \u4e5f\u91c7\u7528\u7c7b\u4f3c float \u7684\u8868\u793a\u65b9\u6cd5\uff0c\u5728\u6b64\u4e0d\u505a\u8d58\u8ff0\u3002

"},{"location":"chapter_data_structure/summary/","title":"3.5 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_data_structure/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
  • \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\u5206\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
  • \u8ba1\u7b97\u673a\u4e2d\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u5305\u62ec\u6574\u6570 byte\u3001short\u3001int\u3001long \uff0c\u6d6e\u70b9\u6570 float\u3001double \uff0c\u5b57\u7b26 char \u548c\u5e03\u5c14 boolean \u3002\u5b83\u4eec\u7684\u53d6\u503c\u8303\u56f4\u53d6\u51b3\u4e8e\u5360\u7528\u7a7a\u95f4\u5927\u5c0f\u548c\u8868\u793a\u65b9\u5f0f\u3002
  • \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 \u548c C# \u7b49\u7f16\u7a0b\u8bed\u8a00\u9ed8\u8ba4\u4f7f\u7528 UTF-16 \u7f16\u7801\u3002
"},{"location":"chapter_data_structure/summary/#2-q-a","title":"2. \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\u94fe\u5f0f\u5730\u5740\u201d\uff08\u540e\u7eed\u54c8\u5e0c\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 byte \u5417\uff1f

char \u7c7b\u578b\u7684\u957f\u5ea6\u7531\u7f16\u7a0b\u8bed\u8a00\u91c7\u7528\u7684\u7f16\u7801\u65b9\u6cd5\u51b3\u5b9a\u3002\u4f8b\u5982\uff0cJava\u3001JS\u3001TS\u3001C# \u90fd\u91c7\u7528 UTF-16 \u7f16\u7801\uff08\u4fdd\u5b58 Unicode \u7801\u70b9\uff09\uff0c\u56e0\u6b64 char \u7c7b\u578b\u7684\u957f\u5ea6\u4e3a 2 bytes \u3002

"},{"location":"chapter_divide_and_conquer/","title":"\u7b2c 12 \u7ae0 \u00a0 \u5206\u6cbb","text":"

Abstract

\u96be\u9898\u88ab\u9010\u5c42\u62c6\u89e3\uff0c\u6bcf\u4e00\u6b21\u7684\u62c6\u89e3\u90fd\u4f7f\u5b83\u53d8\u5f97\u66f4\u4e3a\u7b80\u5355\u3002

\u5206\u800c\u6cbb\u4e4b\u63ed\u793a\u4e86\u4e00\u4e2a\u91cd\u8981\u7684\u4e8b\u5b9e\uff1a\u4ece\u7b80\u5355\u505a\u8d77\uff0c\u4e00\u5207\u90fd\u4e0d\u518d\u590d\u6742\u3002

"},{"location":"chapter_divide_and_conquer/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 12.1 \u00a0 \u5206\u6cbb\u7b97\u6cd5
  • 12.2 \u00a0 \u5206\u6cbb\u641c\u7d22\u7b56\u7565
  • 12.3 \u00a0 \u6784\u5efa\u6811\u95ee\u9898
  • 12.4 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898
  • 12.5 \u00a0 \u5c0f\u7ed3
"},{"location":"chapter_divide_and_conquer/binary_search_recur/","title":"12.2 \u00a0 \u5206\u6cbb\u641c\u7d22\u7b56\u7565","text":"

\u6211\u4eec\u5df2\u7ecf\u5b66\u8fc7\uff0c\u641c\u7d22\u7b97\u6cd5\u5206\u4e3a\u4e24\u5927\u7c7b\u3002

  • \u66b4\u529b\u641c\u7d22\uff1a\u5b83\u901a\u8fc7\u904d\u5386\u6570\u636e\u7ed3\u6784\u5b9e\u73b0\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002
  • \u81ea\u9002\u5e94\u641c\u7d22\uff1a\u5b83\u5229\u7528\u7279\u6709\u7684\u6570\u636e\u7ec4\u7ec7\u5f62\u5f0f\u6216\u5148\u9a8c\u4fe1\u606f\uff0c\u53ef\u8fbe\u5230 \\(O(\\log n)\\) \u751a\u81f3 \\(O(1)\\) \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002

\u5b9e\u9645\u4e0a\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u7684\u641c\u7d22\u7b97\u6cd5\u901a\u5e38\u90fd\u662f\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u5b9e\u73b0\u7684\uff0c\u4f8b\u5982\u4e8c\u5206\u67e5\u627e\u548c\u6811\u3002

  • \u4e8c\u5206\u67e5\u627e\u7684\u6bcf\u4e00\u6b65\u90fd\u5c06\u95ee\u9898\uff08\u5728\u6570\u7ec4\u4e2d\u641c\u7d22\u76ee\u6807\u5143\u7d20\uff09\u5206\u89e3\u4e3a\u4e00\u4e2a\u5c0f\u95ee\u9898\uff08\u5728\u6570\u7ec4\u7684\u4e00\u534a\u4e2d\u641c\u7d22\u76ee\u6807\u5143\u7d20\uff09\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4e00\u76f4\u6301\u7eed\u5230\u6570\u7ec4\u4e3a\u7a7a\u6216\u627e\u5230\u76ee\u6807\u5143\u7d20\u4e3a\u6b62\u3002
  • \u6811\u662f\u5206\u6cbb\u5173\u7cfb\u7684\u4ee3\u8868\uff0c\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u3001AVL \u6811\u3001\u5806\u7b49\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u5404\u79cd\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7686\u4e3a \\(O(\\log n)\\) \u3002

\u4e8c\u5206\u67e5\u627e\u7684\u5206\u6cbb\u7b56\u7565\u5982\u4e0b\u6240\u793a\u3002

  • \u95ee\u9898\u53ef\u4ee5\u88ab\u5206\u89e3\uff1a\u4e8c\u5206\u67e5\u627e\u9012\u5f52\u5730\u5c06\u539f\u95ee\u9898\uff08\u5728\u6570\u7ec4\u4e2d\u8fdb\u884c\u67e5\u627e\uff09\u5206\u89e3\u4e3a\u5b50\u95ee\u9898\uff08\u5728\u6570\u7ec4\u7684\u4e00\u534a\u4e2d\u8fdb\u884c\u67e5\u627e\uff09\uff0c\u8fd9\u662f\u901a\u8fc7\u6bd4\u8f83\u4e2d\u95f4\u5143\u7d20\u548c\u76ee\u6807\u5143\u7d20\u6765\u5b9e\u73b0\u7684\u3002
  • \u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff1a\u5728\u4e8c\u5206\u67e5\u627e\u4e2d\uff0c\u6bcf\u8f6e\u53ea\u5904\u7406\u4e00\u4e2a\u5b50\u95ee\u9898\uff0c\u5b83\u4e0d\u53d7\u53e6\u5916\u5b50\u95ee\u9898\u7684\u5f71\u54cd\u3002
  • \u5b50\u95ee\u9898\u7684\u89e3\u65e0\u987b\u5408\u5e76\uff1a\u4e8c\u5206\u67e5\u627e\u65e8\u5728\u67e5\u627e\u4e00\u4e2a\u7279\u5b9a\u5143\u7d20\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u8fdb\u884c\u5408\u5e76\u3002\u5f53\u5b50\u95ee\u9898\u5f97\u5230\u89e3\u51b3\u65f6\uff0c\u539f\u95ee\u9898\u4e5f\u4f1a\u540c\u65f6\u5f97\u5230\u89e3\u51b3\u3002

\u5206\u6cbb\u80fd\u591f\u63d0\u5347\u641c\u7d22\u6548\u7387\uff0c\u672c\u8d28\u4e0a\u662f\u56e0\u4e3a\u66b4\u529b\u641c\u7d22\u6bcf\u8f6e\u53ea\u80fd\u6392\u9664\u4e00\u4e2a\u9009\u9879\uff0c\u800c\u5206\u6cbb\u641c\u7d22\u6bcf\u8f6e\u53ef\u4ee5\u6392\u9664\u4e00\u534a\u9009\u9879\u3002

"},{"location":"chapter_divide_and_conquer/binary_search_recur/#1","title":"1. \u00a0 \u57fa\u4e8e\u5206\u6cbb\u5b9e\u73b0\u4e8c\u5206","text":"

\u5728\u4e4b\u524d\u7684\u7ae0\u8282\u4e2d\uff0c\u4e8c\u5206\u67e5\u627e\u662f\u57fa\u4e8e\u9012\u63a8\uff08\u8fed\u4ee3\uff09\u5b9e\u73b0\u7684\u3002\u73b0\u5728\u6211\u4eec\u57fa\u4e8e\u5206\u6cbb\uff08\u9012\u5f52\uff09\u6765\u5b9e\u73b0\u5b83\u3002

Question

\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6709\u5e8f\u6570\u7ec4 nums \uff0c\u6570\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u90fd\u662f\u552f\u4e00\u7684\uff0c\u8bf7\u67e5\u627e\u5143\u7d20 target \u3002

\u4ece\u5206\u6cbb\u89d2\u5ea6\uff0c\u6211\u4eec\u5c06\u641c\u7d22\u533a\u95f4 \\([i, j]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u8bb0\u4e3a \\(f(i, j)\\) \u3002

\u4ece\u539f\u95ee\u9898 \\(f(0, n-1)\\) \u4e3a\u8d77\u59cb\u70b9\uff0c\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u4e8c\u5206\u67e5\u627e\u3002

  1. \u8ba1\u7b97\u641c\u7d22\u533a\u95f4 \\([i, j]\\) \u7684\u4e2d\u70b9 \\(m\\) \uff0c\u6839\u636e\u5b83\u6392\u9664\u4e00\u534a\u641c\u7d22\u533a\u95f4\u3002
  2. \u9012\u5f52\u6c42\u89e3\u89c4\u6a21\u51cf\u5c0f\u4e00\u534a\u7684\u5b50\u95ee\u9898\uff0c\u53ef\u80fd\u4e3a \\(f(i, m-1)\\) \u6216 \\(f(m+1, j)\\) \u3002
  3. \u5faa\u73af\u7b2c 1. \u548c 2. \u6b65\uff0c\u76f4\u81f3\u627e\u5230 target \u6216\u533a\u95f4\u4e3a\u7a7a\u65f6\u8fd4\u56de\u3002

\u56fe 12-4 \u5c55\u793a\u4e86\u5728\u6570\u7ec4\u4e2d\u4e8c\u5206\u67e5\u627e\u5143\u7d20 \\(6\\) \u7684\u5206\u6cbb\u8fc7\u7a0b\u3002

\u56fe 12-4 \u00a0 \u4e8c\u5206\u67e5\u627e\u7684\u5206\u6cbb\u8fc7\u7a0b

\u5728\u5b9e\u73b0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u9012\u5f52\u51fd\u6570 dfs() \u6765\u6c42\u89e3\u95ee\u9898 \\(f(i, j)\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_recur.py
def dfs(nums: list[int], target: int, i: int, j: int) -> int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j)\"\"\"\n# \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif i > j:\nreturn -1\n# \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nm = (i + j) // 2\nif nums[m] < target:\n# \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m + 1, j)\nelif nums[m] > target:\n# \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m - 1)\nelse:\n# \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m\ndef binary_search(nums: list[int], target: int) -> int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\"\"\"\nn = len(nums)\n# \u6c42\u89e3\u95ee\u9898 f(0, n-1)\nreturn dfs(nums, target, 0, n - 1)\n
binary_search_recur.cpp
/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(vector<int> &nums, int target, int i, int j) {\n// \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif (i > j) {\nreturn -1;\n}\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nint m = (i + j) / 2;\nif (nums[m] < target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m + 1, j);\n} else if (nums[m] > target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m - 1);\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(vector<int> &nums, int target) {\nint n = nums.size();\n// \u6c42\u89e3\u95ee\u9898 f(0, n-1)\nreturn dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.java
/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(int[] nums, int target, int i, int j) {\n// \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif (i > j) {\nreturn -1;\n}\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nint m = (i + j) / 2;\nif (nums[m] < target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m + 1, j);\n} else if (nums[m] > target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m - 1);\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(int[] nums, int target) {\nint n = nums.length;\n// \u6c42\u89e3\u95ee\u9898 f(0, n-1)\nreturn dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.cs
/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint DFS(int[] nums, int target, int i, int j) {\n// \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif (i > j) {\nreturn -1;\n}\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nint m = (i + j) / 2;\nif (nums[m] < target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn DFS(nums, target, m + 1, j);\n} else if (nums[m] > target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn DFS(nums, target, i, m - 1);\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nint BinarySearch(int[] nums, int target) {\nint n = nums.Length;\n// \u6c42\u89e3\u95ee\u9898 f(0, n-1)\nreturn DFS(nums, target, 0, n - 1);\n}\n
binary_search_recur.go
/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunc dfs(nums []int, target, i, j int) int {\n// \u5982\u679c\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u6ca1\u6709\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif i > j {\nreturn -1\n}\n//    \u8ba1\u7b97\u7d22\u5f15\u4e2d\u70b9\nm := i + ((j - i) >> 1)\n//\u5224\u65ad\u4e2d\u70b9\u4e0e\u76ee\u6807\u5143\u7d20\u5927\u5c0f\nif nums[m] < target {\n// \u5c0f\u4e8e\u5219\u9012\u5f52\u53f3\u534a\u6570\u7ec4\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m+1, j)\n} else if nums[m] > target {\n// \u5c0f\u4e8e\u5219\u9012\u5f52\u5de6\u534a\u6570\u7ec4\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m-1)\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nfunc binarySearch(nums []int, target int) int {\nn := len(nums)\nreturn dfs(nums, target, 0, n-1)\n}\n
binary_search_recur.swift
/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunc dfs(nums: [Int], target: Int, i: Int, j: Int) -> Int {\n// \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif i > j {\nreturn -1\n}\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nlet m = (i + j) / 2\nif nums[m] < target {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums: nums, target: target, i: m + 1, j: j)\n} else if nums[m] > target {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums: nums, target: target, i: i, j: m - 1)\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nfunc binarySearch(nums: [Int], target: Int) -> Int {\nlet n = nums.count\n// \u6c42\u89e3\u95ee\u9898 f(0, n-1)\nreturn dfs(nums: nums, target: target, i: 0, j: n - 1)\n}\n
binary_search_recur.js
/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunction dfs(nums, target, i, j) {\n// \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif (i > j) {\nreturn -1;\n}\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nconst m = i + ((j - i) >> 1);\nif (nums[m] < target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m + 1, j);\n} else if (nums[m] > target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m - 1);\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nfunction binarySearch(nums, target) {\nconst n = nums.length;\n// \u6c42\u89e3\u95ee\u9898 f(0, n-1)\nreturn dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.ts
/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunction dfs(nums: number[], target: number, i: number, j: number): number {\n// \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif (i > j) {\nreturn -1;\n}\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nconst m = i + ((j - i) >> 1);\nif (nums[m] < target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m + 1, j);\n} else if (nums[m] > target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m - 1);\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nfunction binarySearch(nums: number[], target: number): number {\nconst n = nums.length;\n// \u6c42\u89e3\u95ee\u9898 f(0, n-1)\nreturn dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.dart
/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(List<int> nums, int target, int i, int j) {\n// \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif (i > j) {\nreturn -1;\n}\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nint m = (i + j) ~/ 2;\nif (nums[m] < target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m + 1, j);\n} else if (nums[m] > target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m - 1);\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(List<int> nums, int target) {\nint n = nums.length;\n// \u6c42\u89e3\u95ee\u9898 f(0, n-1)\nreturn dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.rs
/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfn dfs(nums: &[i32], target: i32, i: i32, j: i32) -> i32 {\n// \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif i > j { return -1; }\nlet m: i32 = (i + j) / 2;\nif nums[m as usize] < target {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m + 1, j);\n} else if nums[m as usize] > target {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m - 1);\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nfn binary_search(nums: &[i32], target: i32) -> i32 {\nlet n = nums.len() as i32;\n// \u6c42\u89e3\u95ee\u9898 f(0, n-1)\ndfs(nums, target, 0, n - 1)\n}\n
binary_search_recur.c
/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(int nums[], int target, int i, int j) {\n// \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif (i > j) {\nreturn -1;\n}\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nint m = (i + j) / 2;\nif (nums[m] < target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m + 1, j);\n} else if (nums[m] > target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m - 1);\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(int nums[], int target, int numsSize) {\nint n = numsSize;\n// \u6c42\u89e3\u95ee\u9898 f(0, n-1)\nreturn dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.zig
[class]{}-[func]{dfs}\n[class]{}-[func]{binarySearch}\n
"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/","title":"12.3 \u00a0 \u6784\u5efa\u4e8c\u53c9\u6811\u95ee\u9898","text":"

Question

\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811\u7684\u524d\u5e8f\u904d\u5386 preorder \u548c\u4e2d\u5e8f\u904d\u5386 inorder \uff0c\u8bf7\u4ece\u4e2d\u6784\u5efa\u4e8c\u53c9\u6811\uff0c\u8fd4\u56de\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\u3002

\u56fe 12-5 \u00a0 \u6784\u5efa\u4e8c\u53c9\u6811\u7684\u793a\u4f8b\u6570\u636e

"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#1","title":"1. \u00a0 \u5224\u65ad\u662f\u5426\u4e3a\u5206\u6cbb\u95ee\u9898","text":"

\u539f\u95ee\u9898\u5b9a\u4e49\u4e3a\u4ece preorder \u548c inorder \u6784\u5efa\u4e8c\u53c9\u6811\uff0c\u5176\u662f\u4e00\u4e2a\u5178\u578b\u7684\u5206\u6cbb\u95ee\u9898\u3002

  • \u95ee\u9898\u53ef\u4ee5\u88ab\u5206\u89e3\uff1a\u4ece\u5206\u6cbb\u7684\u89d2\u5ea6\u5207\u5165\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u539f\u95ee\u9898\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\u3001\u6784\u5efa\u53f3\u5b50\u6811\uff0c\u52a0\u4e0a\u4e00\u6b65\u64cd\u4f5c\uff1a\u521d\u59cb\u5316\u6839\u8282\u70b9\u3002\u800c\u5bf9\u4e8e\u6bcf\u4e2a\u5b50\u6811\uff08\u5b50\u95ee\u9898\uff09\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u590d\u7528\u4ee5\u4e0a\u5212\u5206\u65b9\u6cd5\uff0c\u5c06\u5176\u5212\u5206\u4e3a\u66f4\u5c0f\u7684\u5b50\u6811\uff08\u5b50\u95ee\u9898\uff09\uff0c\u76f4\u81f3\u8fbe\u5230\u6700\u5c0f\u5b50\u95ee\u9898\uff08\u7a7a\u5b50\u6811\uff09\u65f6\u7ec8\u6b62\u3002
  • \u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff1a\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u662f\u76f8\u4e92\u72ec\u7acb\u7684\uff0c\u5b83\u4eec\u4e4b\u95f4\u6ca1\u6709\u4ea4\u96c6\u3002\u5728\u6784\u5efa\u5de6\u5b50\u6811\u65f6\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5173\u6ce8\u4e2d\u5e8f\u904d\u5386\u548c\u524d\u5e8f\u904d\u5386\u4e2d\u4e0e\u5de6\u5b50\u6811\u5bf9\u5e94\u7684\u90e8\u5206\u3002\u53f3\u5b50\u6811\u540c\u7406\u3002
  • \u5b50\u95ee\u9898\u7684\u89e3\u53ef\u4ee5\u5408\u5e76\uff1a\u4e00\u65e6\u5f97\u5230\u4e86\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\uff08\u5b50\u95ee\u9898\u7684\u89e3\uff09\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u5b83\u4eec\u94fe\u63a5\u5230\u6839\u8282\u70b9\u4e0a\uff0c\u5f97\u5230\u539f\u95ee\u9898\u7684\u89e3\u3002
"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#2","title":"2. \u00a0 \u5982\u4f55\u5212\u5206\u5b50\u6811","text":"

\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u8fd9\u9053\u9898\u662f\u53ef\u4ee5\u4f7f\u7528\u5206\u6cbb\u6765\u6c42\u89e3\u7684\uff0c\u4f46\u5982\u4f55\u901a\u8fc7\u524d\u5e8f\u904d\u5386 preorder \u548c\u4e2d\u5e8f\u904d\u5386 inorder \u6765\u5212\u5206\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u5462\uff1f

\u6839\u636e\u5b9a\u4e49\uff0cpreorder \u548c inorder \u90fd\u53ef\u4ee5\u88ab\u5212\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\u3002

  • \u524d\u5e8f\u904d\u5386\uff1a[ \u6839\u8282\u70b9 | \u5de6\u5b50\u6811 | \u53f3\u5b50\u6811 ] \uff0c\u4f8b\u5982\u56fe 12-5 \u7684\u6811\u5bf9\u5e94 [ 3 | 9 | 2 1 7 ] \u3002
  • \u4e2d\u5e8f\u904d\u5386\uff1a[ \u5de6\u5b50\u6811 | \u6839\u8282\u70b9 \uff5c \u53f3\u5b50\u6811 ] \uff0c\u4f8b\u5982\u56fe 12-5 \u7684\u6811\u5bf9\u5e94 [ 9 | 3 | 1 2 7 ] \u3002

\u4ee5\u4e0a\u56fe\u6570\u636e\u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u56fe 12-6 \u6240\u793a\u7684\u6b65\u9aa4\u5f97\u5230\u5212\u5206\u7ed3\u679c\u3002

  1. \u524d\u5e8f\u904d\u5386\u7684\u9996\u5143\u7d20 3 \u662f\u6839\u8282\u70b9\u7684\u503c\u3002
  2. \u67e5\u627e\u6839\u8282\u70b9 3 \u5728 inorder \u4e2d\u7684\u7d22\u5f15\uff0c\u5229\u7528\u8be5\u7d22\u5f15\u53ef\u5c06 inorder \u5212\u5206\u4e3a [ 9 | 3 \uff5c 1 2 7 ] \u3002
  3. \u6839\u636e inorder \u5212\u5206\u7ed3\u679c\uff0c\u6613\u5f97\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u7684\u8282\u70b9\u6570\u91cf\u5206\u522b\u4e3a 1 \u548c 3 \uff0c\u4ece\u800c\u53ef\u5c06 preorder \u5212\u5206\u4e3a [ 3 | 9 | 2 1 7 ] \u3002

\u56fe 12-6 \u00a0 \u5728\u524d\u5e8f\u548c\u4e2d\u5e8f\u904d\u5386\u4e2d\u5212\u5206\u5b50\u6811

"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#3","title":"3. \u00a0 \u57fa\u4e8e\u53d8\u91cf\u63cf\u8ff0\u5b50\u6811\u533a\u95f4","text":"

\u6839\u636e\u4ee5\u4e0a\u5212\u5206\u65b9\u6cd5\uff0c\u6211\u4eec\u5df2\u7ecf\u5f97\u5230\u6839\u8282\u70b9\u3001\u5de6\u5b50\u6811\u3001\u53f3\u5b50\u6811\u5728 preorder \u548c inorder \u4e2d\u7684\u7d22\u5f15\u533a\u95f4\u3002\u800c\u4e3a\u4e86\u63cf\u8ff0\u8fd9\u4e9b\u7d22\u5f15\u533a\u95f4\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u51e0\u4e2a\u6307\u9488\u53d8\u91cf\u3002

  • \u5c06\u5f53\u524d\u6811\u7684\u6839\u8282\u70b9\u5728 preorder \u4e2d\u7684\u7d22\u5f15\u8bb0\u4e3a \\(i\\) \u3002
  • \u5c06\u5f53\u524d\u6811\u7684\u6839\u8282\u70b9\u5728 inorder \u4e2d\u7684\u7d22\u5f15\u8bb0\u4e3a \\(m\\) \u3002
  • \u5c06\u5f53\u524d\u6811\u5728 inorder \u4e2d\u7684\u7d22\u5f15\u533a\u95f4\u8bb0\u4e3a \\([l, r]\\) \u3002

\u5982\u8868 12-1 \u6240\u793a\uff0c\u901a\u8fc7\u4ee5\u4e0a\u53d8\u91cf\u5373\u53ef\u8868\u793a\u6839\u8282\u70b9\u5728 preorder \u4e2d\u7684\u7d22\u5f15\uff0c\u4ee5\u53ca\u5b50\u6811\u5728 inorder \u4e2d\u7684\u7d22\u5f15\u533a\u95f4\u3002

\u8868 12-1 \u00a0 \u6839\u8282\u70b9\u548c\u5b50\u6811\u5728\u524d\u5e8f\u548c\u4e2d\u5e8f\u904d\u5386\u4e2d\u7684\u7d22\u5f15

\u6839\u8282\u70b9\u5728 preorder \u4e2d\u7684\u7d22\u5f15 \u5b50\u6811\u5728 inorder \u4e2d\u7684\u7d22\u5f15\u533a\u95f4 \u5f53\u524d\u6811 \\(i\\) \\([l, r]\\) \u5de6\u5b50\u6811 \\(i + 1\\) \\([l, m-1]\\) \u53f3\u5b50\u6811 \\(i + 1 + (m - l)\\) \\([m+1, r]\\)

\u8bf7\u6ce8\u610f\uff0c\u53f3\u5b50\u6811\u6839\u8282\u70b9\u7d22\u5f15\u4e2d\u7684 \\((m-l)\\) \u7684\u542b\u4e49\u662f\u201c\u5de6\u5b50\u6811\u7684\u8282\u70b9\u6570\u91cf\u201d\uff0c\u5efa\u8bae\u914d\u5408\u56fe 12-7 \u7406\u89e3\u3002

\u56fe 12-7 \u00a0 \u6839\u8282\u70b9\u548c\u5de6\u53f3\u5b50\u6811\u7684\u7d22\u5f15\u533a\u95f4\u8868\u793a

"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#4","title":"4. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u4e3a\u4e86\u63d0\u5347\u67e5\u8be2 \\(m\\) \u7684\u6548\u7387\uff0c\u6211\u4eec\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868 hmap \u6765\u5b58\u50a8\u6570\u7ec4 inorder \u4e2d\u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig build_tree.py
def dfs(\npreorder: list[int],\ninorder_map: dict[int, int],\ni: int,\nl: int,\nr: int,\n) -> TreeNode | None:\n\"\"\"\u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb\"\"\"\n# \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif r - l < 0:\nreturn None\n# \u521d\u59cb\u5316\u6839\u8282\u70b9\nroot = TreeNode(preorder[i])\n# \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nm = inorder_map[preorder[i]]\n# \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.left = dfs(preorder, inorder_map, i + 1, l, m - 1)\n# \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r)\n# \u8fd4\u56de\u6839\u8282\u70b9\nreturn root\ndef build_tree(preorder: list[int], inorder: list[int]) -> TreeNode | None:\n\"\"\"\u6784\u5efa\u4e8c\u53c9\u6811\"\"\"\n# \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\ninorder_map = {val: i for i, val in enumerate(inorder)}\nroot = dfs(preorder, inorder_map, 0, 0, len(inorder) - 1)\nreturn root\n
build_tree.cpp
/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode *dfs(vector<int> &preorder, unordered_map<int, int> &inorderMap, int i, int l, int r) {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif (r - l < 0)\nreturn NULL;\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nTreeNode *root = new TreeNode(preorder[i]);\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nint m = inorderMap[preorder[i]];\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot->left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot->right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n// \u8fd4\u56de\u6839\u8282\u70b9\nreturn root;\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\nunordered_map<int, int> inorderMap;\nfor (int i = 0; i < inorder.size(); i++) {\ninorderMap[inorder[i]] = i;\n}\nTreeNode *root = dfs(preorder, inorderMap, 0, 0, inorder.size() - 1);\nreturn root;\n}\n
build_tree.java
/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode dfs(int[] preorder, Map<Integer, Integer> inorderMap, int i, int l, int r) {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif (r - l < 0)\nreturn null;\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nTreeNode root = new TreeNode(preorder[i]);\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nint m = inorderMap.get(preorder[i]);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n// \u8fd4\u56de\u6839\u8282\u70b9\nreturn root;\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode buildTree(int[] preorder, int[] inorder) {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\nMap<Integer, Integer> inorderMap = new HashMap<>();\nfor (int i = 0; i < inorder.length; i++) {\ninorderMap.put(inorder[i], i);\n}\nTreeNode root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\nreturn root;\n}\n
build_tree.cs
/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode DFS(int[] preorder, Dictionary<int, int> inorderMap, int i, int l, int r) {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif (r - l < 0)\nreturn null;\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nTreeNode root = new(preorder[i]);\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nint m = inorderMap[preorder[i]];\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.left = DFS(preorder, inorderMap, i + 1, l, m - 1);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.right = DFS(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n// \u8fd4\u56de\u6839\u8282\u70b9\nreturn root;\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode BuildTree(int[] preorder, int[] inorder) {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\nDictionary<int, int> inorderMap = new();\nfor (int i = 0; i < inorder.Length; i++) {\ninorderMap.TryAdd(inorder[i], i);\n}\nTreeNode root = DFS(preorder, inorderMap, 0, 0, inorder.Length - 1);\nreturn root;\n}\n
build_tree.go
/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunc dfsBuildTree(preorder []int, inorderMap map[int]int, i, l, r int) *TreeNode {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif r-l < 0 {\nreturn nil\n}\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nroot := NewTreeNode(preorder[i])\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nm := inorderMap[preorder[i]]\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.Left = dfsBuildTree(preorder, inorderMap, i+1, l, m-1)\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.Right = dfsBuildTree(preorder, inorderMap, i+1+m-l, m+1, r)\n// \u8fd4\u56de\u6839\u8282\u70b9\nreturn root\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunc buildTree(preorder, inorder []int) *TreeNode {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\ninorderMap := make(map[int]int, len(inorder))\nfor i := 0; i < len(inorder); i++ {\ninorderMap[inorder[i]] = i\n}\nroot := dfsBuildTree(preorder, inorderMap, 0, 0, len(inorder)-1)\nreturn root\n}\n
build_tree.swift
/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunc dfs(preorder: [Int], inorderMap: [Int: Int], i: Int, l: Int, r: Int) -> TreeNode? {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif r - l < 0 {\nreturn nil\n}\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nlet root = TreeNode(x: preorder[i])\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nlet m = inorderMap[preorder[i]]!\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.left = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1, l: l, r: m - 1)\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.right = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1 + m - l, l: m + 1, r: r)\n// \u8fd4\u56de\u6839\u8282\u70b9\nreturn root\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunc buildTree(preorder: [Int], inorder: [Int]) -> TreeNode? {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\nlet inorderMap = inorder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }\nreturn dfs(preorder: preorder, inorderMap: inorderMap, i: 0, l: 0, r: inorder.count - 1)\n}\n
build_tree.js
/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunction dfs(preorder, inorderMap, i, l, r) {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif (r - l < 0) return null;\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nconst root = new TreeNode(preorder[i]);\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nconst m = inorderMap.get(preorder[i]);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n// \u8fd4\u56de\u6839\u8282\u70b9\nreturn root;\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunction buildTree(preorder, inorder) {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\nlet inorderMap = new Map();\nfor (let i = 0; i < inorder.length; i++) {\ninorderMap.set(inorder[i], i);\n}\nconst root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\nreturn root;\n}\n
build_tree.ts
/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunction dfs(\npreorder: number[],\ninorderMap: Map<number, number>,\ni: number,\nl: number,\nr: number\n): TreeNode | null {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif (r - l < 0) return null;\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nconst root: TreeNode = new TreeNode(preorder[i]);\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nconst m = inorderMap.get(preorder[i]);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n// \u8fd4\u56de\u6839\u8282\u70b9\nreturn root;\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunction buildTree(preorder: number[], inorder: number[]): TreeNode | null {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\nlet inorderMap = new Map<number, number>();\nfor (let i = 0; i < inorder.length; i++) {\ninorderMap.set(inorder[i], i);\n}\nconst root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\nreturn root;\n}\n
build_tree.dart
/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode? dfs(\nList<int> preorder,\nMap<int, int> inorderMap,\nint i,\nint l,\nint r,\n) {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif (r - l < 0) {\nreturn null;\n}\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nTreeNode? root = TreeNode(preorder[i]);\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nint m = inorderMap[preorder[i]]!;\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n// \u8fd4\u56de\u6839\u8282\u70b9\nreturn root;\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode? buildTree(List<int> preorder, List<int> inorder) {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\nMap<int, int> inorderMap = {};\nfor (int i = 0; i < inorder.length; i++) {\ninorderMap[inorder[i]] = i;\n}\nTreeNode? root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\nreturn root;\n}\n
build_tree.rs
/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfn dfs(preorder: &[i32], inorder_map: &HashMap<i32, i32>, i: i32, l: i32, r: i32) -> Option<Rc<RefCell<TreeNode>>> {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif r - l < 0 { return None; }\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nlet root = TreeNode::new(preorder[i as usize]);\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nlet m = inorder_map.get(&preorder[i as usize]).unwrap();\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.borrow_mut().left = dfs(preorder, inorder_map, i + 1, l, m - 1);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.borrow_mut().right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r);\n// \u8fd4\u56de\u6839\u8282\u70b9\nSome(root)\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfn build_tree(preorder: &[i32], inorder: &[i32]) -> Option<Rc<RefCell<TreeNode>>> {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\nlet mut inorder_map: HashMap<i32, i32> = HashMap::new();\nfor i in 0..inorder.len() {\ninorder_map.insert(inorder[i], i as i32);\n}\nlet root = dfs(preorder, &inorder_map, 0, 0, inorder.len() as i32 - 1);\nroot\n}\n
build_tree.c
[class]{}-[func]{dfs}\n[class]{}-[func]{buildTree}\n
build_tree.zig
[class]{}-[func]{dfs}\n[class]{}-[func]{buildTree}\n

\u56fe 12-8 \u5c55\u793a\u4e86\u6784\u5efa\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b\uff0c\u5404\u4e2a\u8282\u70b9\u662f\u5728\u5411\u4e0b\u201c\u9012\u201d\u7684\u8fc7\u7a0b\u4e2d\u5efa\u7acb\u7684\uff0c\u800c\u5404\u6761\u8fb9\uff08\u5373\u5f15\u7528\uff09\u662f\u5728\u5411\u4e0a\u201c\u5f52\u201d\u7684\u8fc7\u7a0b\u4e2d\u5efa\u7acb\u7684\u3002

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

\u56fe 12-8 \u00a0 \u6784\u5efa\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b

\u6bcf\u4e2a\u9012\u5f52\u51fd\u6570\u5185\u7684\u524d\u5e8f\u904d\u5386 preorder \u548c\u4e2d\u5e8f\u904d\u5386 inorder \u7684\u5212\u5206\u7ed3\u679c\u5982\u56fe 12-9 \u6240\u793a\u3002

\u56fe 12-9 \u00a0 \u6bcf\u4e2a\u9012\u5f52\u51fd\u6570\u4e2d\u7684\u5212\u5206\u7ed3\u679c

\u8bbe\u6811\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u521d\u59cb\u5316\u6bcf\u4e00\u4e2a\u8282\u70b9\uff08\u6267\u884c\u4e00\u4e2a\u9012\u5f52\u51fd\u6570 dfs() \uff09\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002\u56e0\u6b64\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

\u54c8\u5e0c\u8868\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u5373\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u94fe\u8868\u65f6\uff0c\u9012\u5f52\u6df1\u5ea6\u8fbe\u5230 \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u7684\u6808\u5e27\u7a7a\u95f4\u3002\u56e0\u6b64\u603b\u4f53\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

"},{"location":"chapter_divide_and_conquer/divide_and_conquer/","title":"12.1 \u00a0 \u5206\u6cbb\u7b97\u6cd5","text":"

\u300c\u5206\u6cbb divide and conquer\u300d\uff0c\u5168\u79f0\u5206\u800c\u6cbb\u4e4b\uff0c\u662f\u4e00\u79cd\u975e\u5e38\u91cd\u8981\u4e14\u5e38\u89c1\u7684\u7b97\u6cd5\u7b56\u7565\u3002\u5206\u6cbb\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u5b9e\u73b0\uff0c\u5305\u62ec\u201c\u5206\u201d\u548c\u201c\u6cbb\u201d\u4e24\u4e2a\u6b65\u9aa4\u3002

  1. \u5206\uff08\u5212\u5206\u9636\u6bb5\uff09\uff1a\u9012\u5f52\u5730\u5c06\u539f\u95ee\u9898\u5206\u89e3\u4e3a\u4e24\u4e2a\u6216\u591a\u4e2a\u5b50\u95ee\u9898\uff0c\u76f4\u81f3\u5230\u8fbe\u6700\u5c0f\u5b50\u95ee\u9898\u65f6\u7ec8\u6b62\u3002
  2. \u6cbb\uff08\u5408\u5e76\u9636\u6bb5\uff09\uff1a\u4ece\u5df2\u77e5\u89e3\u7684\u6700\u5c0f\u5b50\u95ee\u9898\u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5730\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u8fdb\u884c\u5408\u5e76\uff0c\u4ece\u800c\u6784\u5efa\u51fa\u539f\u95ee\u9898\u7684\u89e3\u3002

\u5982\u56fe 12-1 \u6240\u793a\uff0c\u201c\u5f52\u5e76\u6392\u5e8f\u201d\u662f\u5206\u6cbb\u7b56\u7565\u7684\u5178\u578b\u5e94\u7528\u4e4b\u4e00\u3002

  1. \u5206\uff1a\u9012\u5f52\u5730\u5c06\u539f\u6570\u7ec4\uff08\u539f\u95ee\u9898\uff09\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff08\u5b50\u95ee\u9898\uff09\uff0c\u76f4\u5230\u5b50\u6570\u7ec4\u53ea\u5269\u4e00\u4e2a\u5143\u7d20\uff08\u6700\u5c0f\u5b50\u95ee\u9898\uff09\u3002
  2. \u6cbb\uff1a\u4ece\u5e95\u81f3\u9876\u5730\u5c06\u6709\u5e8f\u7684\u5b50\u6570\u7ec4\uff08\u5b50\u95ee\u9898\u7684\u89e3\uff09\u8fdb\u884c\u5408\u5e76\uff0c\u4ece\u800c\u5f97\u5230\u6709\u5e8f\u7684\u539f\u6570\u7ec4\uff08\u539f\u95ee\u9898\u7684\u89e3\uff09\u3002

\u56fe 12-1 \u00a0 \u5f52\u5e76\u6392\u5e8f\u7684\u5206\u6cbb\u7b56\u7565

"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1211","title":"12.1.1 \u00a0 \u5982\u4f55\u5224\u65ad\u5206\u6cbb\u95ee\u9898","text":"

\u4e00\u4e2a\u95ee\u9898\u662f\u5426\u9002\u5408\u4f7f\u7528\u5206\u6cbb\u89e3\u51b3\uff0c\u901a\u5e38\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u51e0\u4e2a\u5224\u65ad\u4f9d\u636e\u3002

  1. \u95ee\u9898\u53ef\u4ee5\u88ab\u5206\u89e3\uff1a\u539f\u95ee\u9898\u53ef\u4ee5\u88ab\u5206\u89e3\u6210\u89c4\u6a21\u66f4\u5c0f\u3001\u7c7b\u4f3c\u7684\u5b50\u95ee\u9898\uff0c\u4ee5\u53ca\u80fd\u591f\u4ee5\u76f8\u540c\u65b9\u5f0f\u9012\u5f52\u5730\u8fdb\u884c\u5212\u5206\u3002
  2. \u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff1a\u5b50\u95ee\u9898\u4e4b\u95f4\u662f\u6ca1\u6709\u91cd\u53e0\u7684\uff0c\u4e92\u76f8\u6ca1\u6709\u4f9d\u8d56\uff0c\u53ef\u4ee5\u88ab\u72ec\u7acb\u89e3\u51b3\u3002
  3. \u5b50\u95ee\u9898\u7684\u89e3\u53ef\u4ee5\u88ab\u5408\u5e76\uff1a\u539f\u95ee\u9898\u7684\u89e3\u901a\u8fc7\u5408\u5e76\u5b50\u95ee\u9898\u7684\u89e3\u5f97\u6765\u3002

\u663e\u7136\uff0c\u5f52\u5e76\u6392\u5e8f\u662f\u6ee1\u8db3\u4ee5\u4e0a\u4e09\u6761\u5224\u65ad\u4f9d\u636e\u7684\u3002

  1. \u95ee\u9898\u53ef\u4ee5\u88ab\u5206\u89e3\uff1a\u9012\u5f52\u5730\u5c06\u6570\u7ec4\uff08\u539f\u95ee\u9898\uff09\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff08\u5b50\u95ee\u9898\uff09\u3002
  2. \u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff1a\u6bcf\u4e2a\u5b50\u6570\u7ec4\u90fd\u53ef\u4ee5\u72ec\u7acb\u5730\u8fdb\u884c\u6392\u5e8f\uff08\u5b50\u95ee\u9898\u53ef\u4ee5\u72ec\u7acb\u8fdb\u884c\u6c42\u89e3\uff09\u3002
  3. \u5b50\u95ee\u9898\u7684\u89e3\u53ef\u4ee5\u88ab\u5408\u5e76\uff1a\u4e24\u4e2a\u6709\u5e8f\u5b50\u6570\u7ec4\uff08\u5b50\u95ee\u9898\u7684\u89e3\uff09\u53ef\u4ee5\u88ab\u5408\u5e76\u4e3a\u4e00\u4e2a\u6709\u5e8f\u6570\u7ec4\uff08\u539f\u95ee\u9898\u7684\u89e3\uff09\u3002
"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1212","title":"12.1.2 \u00a0 \u901a\u8fc7\u5206\u6cbb\u63d0\u5347\u6548\u7387","text":"

\u5206\u6cbb\u4e0d\u4ec5\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u51b3\u7b97\u6cd5\u95ee\u9898\uff0c\u5f80\u5f80\u8fd8\u53ef\u4ee5\u5e26\u6765\u7b97\u6cd5\u6548\u7387\u7684\u63d0\u5347\u3002\u5728\u6392\u5e8f\u7b97\u6cd5\u4e2d\uff0c\u5feb\u901f\u6392\u5e8f\u3001\u5f52\u5e76\u6392\u5e8f\u3001\u5806\u6392\u5e8f\u76f8\u8f83\u4e8e\u9009\u62e9\u3001\u5192\u6ce1\u3001\u63d2\u5165\u6392\u5e8f\u66f4\u5feb\uff0c\u5c31\u662f\u56e0\u4e3a\u5b83\u4eec\u5e94\u7528\u4e86\u5206\u6cbb\u7b56\u7565\u3002

\u90a3\u4e48\uff0c\u6211\u4eec\u4e0d\u7981\u53d1\u95ee\uff1a\u4e3a\u4ec0\u4e48\u5206\u6cbb\u53ef\u4ee5\u63d0\u5347\u7b97\u6cd5\u6548\u7387\uff0c\u5176\u5e95\u5c42\u903b\u8f91\u662f\u4ec0\u4e48\uff1f\u6362\u53e5\u8bdd\u8bf4\uff0c\u5c06\u5927\u95ee\u9898\u5206\u89e3\u4e3a\u591a\u4e2a\u5b50\u95ee\u9898\u3001\u89e3\u51b3\u5b50\u95ee\u9898\u3001\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u5408\u5e76\u4e3a\u539f\u95ee\u9898\u7684\u89e3\uff0c\u8fd9\u51e0\u6b65\u7684\u6548\u7387\u4e3a\u4ec0\u4e48\u6bd4\u76f4\u63a5\u89e3\u51b3\u539f\u95ee\u9898\u7684\u6548\u7387\u66f4\u9ad8\uff1f\u8fd9\u4e2a\u95ee\u9898\u53ef\u4ee5\u4ece\u64cd\u4f5c\u6570\u91cf\u548c\u5e76\u884c\u8ba1\u7b97\u4e24\u65b9\u9762\u6765\u8ba8\u8bba\u3002

"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1","title":"1. \u00a0 \u64cd\u4f5c\u6570\u91cf\u4f18\u5316","text":"

\u4ee5\u201c\u5192\u6ce1\u6392\u5e8f\u201d\u4e3a\u4f8b\uff0c\u5176\u5904\u7406\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\u9700\u8981 \\(O(n^2)\\) \u65f6\u95f4\u3002\u5047\u8bbe\u6211\u4eec\u6309\u7167\u56fe 12-2 \u6240\u793a\u7684\u65b9\u5f0f\uff0c\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u5219\u5212\u5206\u9700\u8981 \\(O(n)\\) \u65f6\u95f4\uff0c\u6392\u5e8f\u6bcf\u4e2a\u5b50\u6570\u7ec4\u9700\u8981 \\(O((n / 2)^2)\\) \u65f6\u95f4\uff0c\u5408\u5e76\u4e24\u4e2a\u5b50\u6570\u7ec4\u9700\u8981 \\(O(n)\\) \u65f6\u95f4\uff0c\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\uff1a

\\[ O(n + (\\frac{n}{2})^2 \\times 2 + n) = O(\\frac{n^2}{2} + 2n) \\]

\u56fe 12-2 \u00a0 \u5212\u5206\u6570\u7ec4\u524d\u540e\u7684\u5192\u6ce1\u6392\u5e8f

\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8ba1\u7b97\u4ee5\u4e0b\u4e0d\u7b49\u5f0f\uff0c\u5176\u5de6\u8fb9\u548c\u53f3\u8fb9\u5206\u522b\u4e3a\u5212\u5206\u524d\u548c\u5212\u5206\u540e\u7684\u64cd\u4f5c\u603b\u6570\uff1a

\\[ \\begin{aligned} n^2 & > \\frac{n^2}{2} + 2n \\newline n^2 - \\frac{n^2}{2} - 2n & > 0 \\newline n(n - 4) & > 0 \\end{aligned} \\]

\u8fd9\u610f\u5473\u7740\u5f53 \\(n > 4\\) \u65f6\uff0c\u5212\u5206\u540e\u7684\u64cd\u4f5c\u6570\u91cf\u66f4\u5c11\uff0c\u6392\u5e8f\u6548\u7387\u5e94\u8be5\u66f4\u9ad8\u3002\u8bf7\u6ce8\u610f\uff0c\u5212\u5206\u540e\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4ecd\u7136\u662f\u5e73\u65b9\u9636 \\(O(n^2)\\) \uff0c\u53ea\u662f\u590d\u6742\u5ea6\u4e2d\u7684\u5e38\u6570\u9879\u53d8\u5c0f\u4e86\u3002

\u8fdb\u4e00\u6b65\u60f3\uff0c\u5982\u679c\u6211\u4eec\u628a\u5b50\u6570\u7ec4\u4e0d\u65ad\u5730\u518d\u4ece\u4e2d\u70b9\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u76f4\u81f3\u5b50\u6570\u7ec4\u53ea\u5269\u4e00\u4e2a\u5143\u7d20\u65f6\u505c\u6b62\u5212\u5206\u5462\uff1f\u8fd9\u79cd\u601d\u8def\u5b9e\u9645\u4e0a\u5c31\u662f\u201c\u5f52\u5e76\u6392\u5e8f\u201d\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002

\u518d\u601d\u8003\uff0c\u5982\u679c\u6211\u4eec\u591a\u8bbe\u7f6e\u51e0\u4e2a\u5212\u5206\u70b9\uff0c\u5c06\u539f\u6570\u7ec4\u5e73\u5747\u5212\u5206\u4e3a \\(k\\) \u4e2a\u5b50\u6570\u7ec4\u5462\uff1f\u8fd9\u79cd\u60c5\u51b5\u4e0e\u201c\u6876\u6392\u5e8f\u201d\u975e\u5e38\u7c7b\u4f3c\uff0c\u5b83\u975e\u5e38\u9002\u5408\u6392\u5e8f\u6d77\u91cf\u6570\u636e\uff0c\u7406\u8bba\u4e0a\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u8fbe\u5230 \\(O(n + k)\\) \u3002

"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#2","title":"2. \u00a0 \u5e76\u884c\u8ba1\u7b97\u4f18\u5316","text":"

\u6211\u4eec\u77e5\u9053\uff0c\u5206\u6cbb\u751f\u6210\u7684\u5b50\u95ee\u9898\u662f\u76f8\u4e92\u72ec\u7acb\u7684\uff0c\u56e0\u6b64\u901a\u5e38\u53ef\u4ee5\u5e76\u884c\u89e3\u51b3\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5206\u6cbb\u4e0d\u4ec5\u53ef\u4ee5\u964d\u4f4e\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u8fd8\u6709\u5229\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u7684\u5e76\u884c\u4f18\u5316\u3002

\u5e76\u884c\u4f18\u5316\u5728\u591a\u6838\u6216\u591a\u5904\u7406\u5668\u7684\u73af\u5883\u4e2d\u5c24\u5176\u6709\u6548\uff0c\u56e0\u4e3a\u7cfb\u7edf\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u591a\u4e2a\u5b50\u95ee\u9898\uff0c\u66f4\u52a0\u5145\u5206\u5730\u5229\u7528\u8ba1\u7b97\u8d44\u6e90\uff0c\u4ece\u800c\u663e\u8457\u51cf\u5c11\u603b\u4f53\u7684\u8fd0\u884c\u65f6\u95f4\u3002

\u6bd4\u5982\u5728\u56fe 12-3 \u6240\u793a\u7684\u201c\u6876\u6392\u5e8f\u201d\u4e2d\uff0c\u6211\u4eec\u5c06\u6d77\u91cf\u7684\u6570\u636e\u5e73\u5747\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\uff0c\u5219\u53ef\u6240\u6709\u6876\u7684\u6392\u5e8f\u4efb\u52a1\u5206\u6563\u5230\u5404\u4e2a\u8ba1\u7b97\u5355\u5143\uff0c\u5b8c\u6210\u540e\u518d\u8fdb\u884c\u7ed3\u679c\u5408\u5e76\u3002

\u56fe 12-3 \u00a0 \u6876\u6392\u5e8f\u7684\u5e76\u884c\u8ba1\u7b97

"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1213","title":"12.1.3 \u00a0 \u5206\u6cbb\u5e38\u89c1\u5e94\u7528","text":"

\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u53ef\u4ee5\u7528\u6765\u89e3\u51b3\u8bb8\u591a\u7ecf\u5178\u7b97\u6cd5\u95ee\u9898\u3002

  • \u5bfb\u627e\u6700\u8fd1\u70b9\u5bf9\uff1a\u8be5\u7b97\u6cd5\u9996\u5148\u5c06\u70b9\u96c6\u5206\u6210\u4e24\u90e8\u5206\uff0c\u7136\u540e\u5206\u522b\u627e\u51fa\u4e24\u90e8\u5206\u4e2d\u7684\u6700\u8fd1\u70b9\u5bf9\uff0c\u6700\u540e\u518d\u627e\u51fa\u8de8\u8d8a\u4e24\u90e8\u5206\u7684\u6700\u8fd1\u70b9\u5bf9\u3002
  • \u5927\u6574\u6570\u4e58\u6cd5\uff1a\u4f8b\u5982 Karatsuba \u7b97\u6cd5\uff0c\u5b83\u662f\u5c06\u5927\u6574\u6570\u4e58\u6cd5\u5206\u89e3\u4e3a\u51e0\u4e2a\u8f83\u5c0f\u7684\u6574\u6570\u7684\u4e58\u6cd5\u548c\u52a0\u6cd5\u3002
  • \u77e9\u9635\u4e58\u6cd5\uff1a\u4f8b\u5982 Strassen \u7b97\u6cd5\uff0c\u5b83\u662f\u5c06\u5927\u77e9\u9635\u4e58\u6cd5\u5206\u89e3\u4e3a\u591a\u4e2a\u5c0f\u77e9\u9635\u7684\u4e58\u6cd5\u548c\u52a0\u6cd5\u3002
  • \u6c49\u8bfa\u5854\u95ee\u9898\uff1a\u6c49\u8bfa\u5854\u95ee\u9898\u53ef\u4ee5\u89c6\u4e3a\u5178\u578b\u7684\u5206\u6cbb\u7b56\u7565\uff0c\u901a\u8fc7\u9012\u5f52\u89e3\u51b3\u3002
  • \u6c42\u89e3\u9006\u5e8f\u5bf9\uff1a\u5728\u4e00\u4e2a\u5e8f\u5217\u4e2d\uff0c\u5982\u679c\u524d\u9762\u7684\u6570\u5b57\u5927\u4e8e\u540e\u9762\u7684\u6570\u5b57\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u6570\u5b57\u6784\u6210\u4e00\u4e2a\u9006\u5e8f\u5bf9\u3002\u6c42\u89e3\u9006\u5e8f\u5bf9\u95ee\u9898\u53ef\u4ee5\u901a\u8fc7\u5206\u6cbb\u7684\u601d\u60f3\uff0c\u501f\u52a9\u5f52\u5e76\u6392\u5e8f\u8fdb\u884c\u6c42\u89e3\u3002

\u53e6\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u5728\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u7684\u8bbe\u8ba1\u4e2d\u5e94\u7528\u975e\u5e38\u5e7f\u6cdb\u3002

  • \u4e8c\u5206\u67e5\u627e\uff1a\u4e8c\u5206\u67e5\u627e\u662f\u5c06\u6709\u5e8f\u6570\u7ec4\u4ece\u4e2d\u70b9\u7d22\u5f15\u5206\u4e3a\u4e24\u90e8\u5206\uff0c\u7136\u540e\u6839\u636e\u76ee\u6807\u503c\u4e0e\u4e2d\u95f4\u5143\u7d20\u503c\u6bd4\u8f83\u7ed3\u679c\uff0c\u51b3\u5b9a\u6392\u9664\u54ea\u4e00\u534a\u533a\u95f4\uff0c\u7136\u540e\u5728\u5269\u4f59\u533a\u95f4\u6267\u884c\u76f8\u540c\u7684\u4e8c\u5206\u64cd\u4f5c\u3002
  • \u5f52\u5e76\u6392\u5e8f\uff1a\u6587\u7ae0\u5f00\u5934\u5df2\u4ecb\u7ecd\uff0c\u4e0d\u518d\u8d58\u8ff0\u3002
  • \u5feb\u901f\u6392\u5e8f\uff1a\u5feb\u901f\u6392\u5e8f\u662f\u9009\u53d6\u4e00\u4e2a\u57fa\u51c6\u503c\uff0c\u7136\u540e\u628a\u6570\u7ec4\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u4e00\u4e2a\u5b50\u6570\u7ec4\u7684\u5143\u7d20\u6bd4\u57fa\u51c6\u503c\u5c0f\uff0c\u53e6\u4e00\u5b50\u6570\u7ec4\u7684\u5143\u7d20\u6bd4\u57fa\u51c6\u503c\u5927\uff0c\u7136\u540e\u518d\u5bf9\u8fd9\u4e24\u90e8\u5206\u8fdb\u884c\u76f8\u540c\u7684\u5212\u5206\u64cd\u4f5c\uff0c\u76f4\u81f3\u5b50\u6570\u7ec4\u53ea\u5269\u4e0b\u4e00\u4e2a\u5143\u7d20\u3002
  • \u6876\u6392\u5e8f\uff1a\u6876\u6392\u5e8f\u7684\u57fa\u672c\u601d\u60f3\u662f\u5c06\u6570\u636e\u5206\u6563\u5230\u591a\u4e2a\u6876\uff0c\u7136\u540e\u5bf9\u6bcf\u4e2a\u6876\u5185\u7684\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\uff0c\u6700\u540e\u5c06\u5404\u4e2a\u6876\u7684\u5143\u7d20\u4f9d\u6b21\u53d6\u51fa\uff0c\u4ece\u800c\u5f97\u5230\u4e00\u4e2a\u6709\u5e8f\u6570\u7ec4\u3002
  • \u6811\uff1a\u4f8b\u5982\u4e8c\u53c9\u641c\u7d22\u6811\u3001AVL \u6811\u3001\u7ea2\u9ed1\u6811\u3001B \u6811\u3001B+ \u6811\u7b49\uff0c\u5b83\u4eec\u7684\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u7b49\u64cd\u4f5c\u90fd\u53ef\u4ee5\u89c6\u4e3a\u5206\u6cbb\u7684\u5e94\u7528\u3002
  • \u5806\uff1a\u5806\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u5176\u5404\u79cd\u64cd\u4f5c\uff0c\u5982\u63d2\u5165\u3001\u5220\u9664\u548c\u5806\u5316\uff0c\u5b9e\u9645\u4e0a\u90fd\u9690\u542b\u4e86\u5206\u6cbb\u7684\u601d\u60f3\u3002
  • \u54c8\u5e0c\u8868\uff1a\u867d\u7136\u54c8\u5e0c\u8868\u6765\u5e76\u4e0d\u76f4\u63a5\u5e94\u7528\u5206\u6cbb\uff0c\u4f46\u67d0\u4e9b\u54c8\u5e0c\u51b2\u7a81\u89e3\u51b3\u7b56\u7565\u95f4\u63a5\u5e94\u7528\u4e86\u5206\u6cbb\u7b56\u7565\uff0c\u4f8b\u5982\uff0c\u94fe\u5f0f\u5730\u5740\u4e2d\u7684\u957f\u94fe\u8868\u4f1a\u88ab\u8f6c\u5316\u4e3a\u7ea2\u9ed1\u6811\uff0c\u4ee5\u63d0\u5347\u67e5\u8be2\u6548\u7387\u3002

\u53ef\u4ee5\u770b\u51fa\uff0c\u5206\u6cbb\u662f\u4e00\u79cd\u201c\u6da6\u7269\u7ec6\u65e0\u58f0\u201d\u7684\u7b97\u6cd5\u601d\u60f3\uff0c\u9690\u542b\u5728\u5404\u79cd\u7b97\u6cd5\u4e0e\u6570\u636e\u7ed3\u6784\u4e4b\u4e2d\u3002

"},{"location":"chapter_divide_and_conquer/hanota_problem/","title":"12.4 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898","text":"

\u5728\u5f52\u5e76\u6392\u5e8f\u548c\u6784\u5efa\u4e8c\u53c9\u6811\u4e2d\uff0c\u6211\u4eec\u90fd\u662f\u5c06\u539f\u95ee\u9898\u5206\u89e3\u4e3a\u4e24\u4e2a\u89c4\u6a21\u4e3a\u539f\u95ee\u9898\u4e00\u534a\u7684\u5b50\u95ee\u9898\u3002\u7136\u800c\u5bf9\u4e8e\u6c49\u8bfa\u5854\u95ee\u9898\uff0c\u6211\u4eec\u91c7\u7528\u4e0d\u540c\u7684\u5206\u89e3\u7b56\u7565\u3002

Question

\u7ed9\u5b9a\u4e09\u6839\u67f1\u5b50\uff0c\u8bb0\u4e3a A\u3001B \u548c C \u3002\u8d77\u59cb\u72b6\u6001\u4e0b\uff0c\u67f1\u5b50 A \u4e0a\u5957\u7740 \\(n\\) \u4e2a\u5706\u76d8\uff0c\u5b83\u4eec\u4ece\u4e0a\u5230\u4e0b\u6309\u7167\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u6392\u5217\u3002\u6211\u4eec\u7684\u4efb\u52a1\u662f\u8981\u628a\u8fd9 \\(n\\) \u4e2a\u5706\u76d8\u79fb\u5230\u67f1\u5b50 C \u4e0a\uff0c\u5e76\u4fdd\u6301\u5b83\u4eec\u7684\u539f\u6709\u987a\u5e8f\u4e0d\u53d8\u3002\u5728\u79fb\u52a8\u5706\u76d8\u7684\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u9075\u5b88\u4ee5\u4e0b\u89c4\u5219\u3002

  1. \u5706\u76d8\u53ea\u80fd\u4ece\u4e00\u4e2a\u67f1\u5b50\u9876\u90e8\u62ff\u51fa\uff0c\u4ece\u53e6\u4e00\u4e2a\u67f1\u5b50\u9876\u90e8\u653e\u5165\u3002
  2. \u6bcf\u6b21\u53ea\u80fd\u79fb\u52a8\u4e00\u4e2a\u5706\u76d8\u3002
  3. \u5c0f\u5706\u76d8\u5fc5\u987b\u65f6\u523b\u4f4d\u4e8e\u5927\u5706\u76d8\u4e4b\u4e0a\u3002

\u56fe 12-10 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898\u793a\u4f8b

\u6211\u4eec\u5c06\u89c4\u6a21\u4e3a \\(i\\) \u7684\u6c49\u8bfa\u5854\u95ee\u9898\u8bb0\u505a \\(f(i)\\) \u3002\u4f8b\u5982 \\(f(3)\\) \u4ee3\u8868\u5c06 \\(3\\) \u4e2a\u5706\u76d8\u4ece A \u79fb\u52a8\u81f3 C \u7684\u6c49\u8bfa\u5854\u95ee\u9898\u3002

"},{"location":"chapter_divide_and_conquer/hanota_problem/#1","title":"1. \u00a0 \u8003\u8651\u57fa\u672c\u60c5\u51b5","text":"

\u5982\u56fe 12-11 \u6240\u793a\uff0c\u5bf9\u4e8e\u95ee\u9898 \\(f(1)\\) \uff0c\u5373\u5f53\u53ea\u6709\u4e00\u4e2a\u5706\u76d8\u65f6\uff0c\u6211\u4eec\u5c06\u5b83\u76f4\u63a5\u4ece A \u79fb\u52a8\u81f3 C \u5373\u53ef\u3002

<1><2>

\u56fe 12-11 \u00a0 \u89c4\u6a21\u4e3a 1 \u95ee\u9898\u7684\u89e3

\u5982\u56fe 12-12 \u6240\u793a\uff0c\u5bf9\u4e8e\u95ee\u9898 \\(f(2)\\) \uff0c\u5373\u5f53\u6709\u4e24\u4e2a\u5706\u76d8\u65f6\uff0c\u7531\u4e8e\u8981\u65f6\u523b\u6ee1\u8db3\u5c0f\u5706\u76d8\u5728\u5927\u5706\u76d8\u4e4b\u4e0a\uff0c\u56e0\u6b64\u9700\u8981\u501f\u52a9 B \u6765\u5b8c\u6210\u79fb\u52a8\u3002

  1. \u5148\u5c06\u4e0a\u9762\u7684\u5c0f\u5706\u76d8\u4ece A \u79fb\u81f3 B \u3002
  2. \u518d\u5c06\u5927\u5706\u76d8\u4ece A \u79fb\u81f3 C \u3002
  3. \u6700\u540e\u5c06\u5c0f\u5706\u76d8\u4ece B \u79fb\u81f3 C \u3002
<1><2><3><4>

\u56fe 12-12 \u00a0 \u89c4\u6a21\u4e3a 2 \u95ee\u9898\u7684\u89e3

\u89e3\u51b3\u95ee\u9898 \\(f(2)\\) \u7684\u8fc7\u7a0b\u53ef\u603b\u7ed3\u4e3a\uff1a\u5c06\u4e24\u4e2a\u5706\u76d8\u501f\u52a9 B \u4ece A \u79fb\u81f3 C \u3002\u5176\u4e2d\uff0cC \u79f0\u4e3a\u76ee\u6807\u67f1\u3001B \u79f0\u4e3a\u7f13\u51b2\u67f1\u3002

"},{"location":"chapter_divide_and_conquer/hanota_problem/#2","title":"2. \u00a0 \u5b50\u95ee\u9898\u5206\u89e3","text":"

\u5bf9\u4e8e\u95ee\u9898 \\(f(3)\\) \uff0c\u5373\u5f53\u6709\u4e09\u4e2a\u5706\u76d8\u65f6\uff0c\u60c5\u51b5\u53d8\u5f97\u7a0d\u5fae\u590d\u6742\u4e86\u4e00\u4e9b\u3002

\u56e0\u4e3a\u5df2\u77e5 \\(f(1)\\) \u548c \\(f(2)\\) \u7684\u89e3\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ece\u5206\u6cbb\u89d2\u5ea6\u601d\u8003\uff0c\u5c06 A \u9876\u90e8\u7684\u4e24\u4e2a\u5706\u76d8\u770b\u505a\u4e00\u4e2a\u6574\u4f53\uff0c\u6267\u884c\u56fe 12-13 \u6240\u793a\u7684\u6b65\u9aa4\u3002\u8fd9\u6837\u4e09\u4e2a\u5706\u76d8\u5c31\u88ab\u987a\u5229\u5730\u4ece A \u79fb\u52a8\u81f3 C \u4e86\u3002

  1. \u4ee4 B \u4e3a\u76ee\u6807\u67f1\u3001C \u4e3a\u7f13\u51b2\u67f1\uff0c\u5c06\u4e24\u4e2a\u5706\u76d8\u4ece A \u79fb\u52a8\u81f3 B \u3002
  2. \u5c06 A \u4e2d\u5269\u4f59\u7684\u4e00\u4e2a\u5706\u76d8\u4ece A \u76f4\u63a5\u79fb\u52a8\u81f3 C \u3002
  3. \u4ee4 C \u4e3a\u76ee\u6807\u67f1\u3001A \u4e3a\u7f13\u51b2\u67f1\uff0c\u5c06\u4e24\u4e2a\u5706\u76d8\u4ece B \u79fb\u52a8\u81f3 C \u3002
<1><2><3><4>

\u56fe 12-13 \u00a0 \u89c4\u6a21\u4e3a 3 \u95ee\u9898\u7684\u89e3

\u672c\u8d28\u4e0a\u770b\uff0c\u6211\u4eec\u5c06\u95ee\u9898 \\(f(3)\\) \u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u95ee\u9898 \\(f(2)\\) \u548c\u5b50\u95ee\u9898 \\(f(1)\\) \u3002\u6309\u987a\u5e8f\u89e3\u51b3\u8fd9\u4e09\u4e2a\u5b50\u95ee\u9898\u4e4b\u540e\uff0c\u539f\u95ee\u9898\u968f\u4e4b\u5f97\u5230\u89e3\u51b3\u3002\u8fd9\u8bf4\u660e\u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff0c\u800c\u4e14\u89e3\u662f\u53ef\u4ee5\u5408\u5e76\u7684\u3002

\u81f3\u6b64\uff0c\u6211\u4eec\u53ef\u603b\u7ed3\u51fa\u56fe 12-14 \u6240\u793a\u7684\u6c49\u8bfa\u5854\u95ee\u9898\u7684\u5206\u6cbb\u7b56\u7565\uff1a\u5c06\u539f\u95ee\u9898 \\(f(n)\\) \u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u95ee\u9898 \\(f(n-1)\\) \u548c\u4e00\u4e2a\u5b50\u95ee\u9898 \\(f(1)\\) \uff0c\u5e76\u6309\u7167\u4ee5\u4e0b\u987a\u5e8f\u89e3\u51b3\u8fd9\u4e09\u4e2a\u5b50\u95ee\u9898\u3002

  1. \u5c06 \\(n-1\\) \u4e2a\u5706\u76d8\u501f\u52a9 C \u4ece A \u79fb\u81f3 B \u3002
  2. \u5c06\u5269\u4f59 \\(1\\) \u4e2a\u5706\u76d8\u4ece A \u76f4\u63a5\u79fb\u81f3 C \u3002
  3. \u5c06 \\(n-1\\) \u4e2a\u5706\u76d8\u501f\u52a9 A \u4ece B \u79fb\u81f3 C \u3002

\u5bf9\u4e8e\u8fd9\u4e24\u4e2a\u5b50\u95ee\u9898 \\(f(n-1)\\) \uff0c\u53ef\u4ee5\u901a\u8fc7\u76f8\u540c\u7684\u65b9\u5f0f\u8fdb\u884c\u9012\u5f52\u5212\u5206\uff0c\u76f4\u81f3\u8fbe\u5230\u6700\u5c0f\u5b50\u95ee\u9898 \\(f(1)\\) \u3002\u800c \\(f(1)\\) \u7684\u89e3\u662f\u5df2\u77e5\u7684\uff0c\u53ea\u9700\u4e00\u6b21\u79fb\u52a8\u64cd\u4f5c\u5373\u53ef\u3002

\u56fe 12-14 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898\u7684\u5206\u6cbb\u7b56\u7565

"},{"location":"chapter_divide_and_conquer/hanota_problem/#3","title":"3. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u5728\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u9012\u5f52\u51fd\u6570 dfs(i, src, buf, tar) \uff0c\u5b83\u7684\u4f5c\u7528\u662f\u5c06\u67f1 src \u9876\u90e8\u7684 \\(i\\) \u4e2a\u5706\u76d8\u501f\u52a9\u7f13\u51b2\u67f1 buf \u79fb\u52a8\u81f3\u76ee\u6807\u67f1 tar \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig hanota.py
def move(src: list[int], tar: list[int]):\n\"\"\"\u79fb\u52a8\u4e00\u4e2a\u5706\u76d8\"\"\"\n# \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\npan = src.pop()\n# \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.append(pan)\ndef dfs(i: int, src: list[int], buf: list[int], tar: list[int]):\n\"\"\"\u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i)\"\"\"\n# \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif i == 1:\nmove(src, tar)\nreturn\n# \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i - 1, src, tar, buf)\n# \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src, tar)\n# \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i - 1, buf, src, tar)\ndef solve_hanota(A: list[int], B: list[int], C: list[int]):\n\"\"\"\u6c42\u89e3\u6c49\u8bfa\u5854\"\"\"\nn = len(A)\n# \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(n, A, B, C)\n
hanota.cpp
/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(vector<int> &src, vector<int> &tar) {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\nint pan = src.back();\nsrc.pop_back();\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.push_back(pan);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nvoid dfs(int i, vector<int> &src, vector<int> &buf, vector<int> &tar) {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif (i == 1) {\nmove(src, tar);\nreturn;\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i - 1, src, tar, buf);\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src, tar);\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i - 1, buf, src, tar);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nvoid solveHanota(vector<int> &A, vector<int> &B, vector<int> &C) {\nint n = A.size();\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(n, A, B, C);\n}\n
hanota.java
/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(List<Integer> src, List<Integer> tar) {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\nInteger pan = src.remove(src.size() - 1);\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.add(pan);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nvoid dfs(int i, List<Integer> src, List<Integer> buf, List<Integer> tar) {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif (i == 1) {\nmove(src, tar);\nreturn;\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i - 1, src, tar, buf);\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src, tar);\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i - 1, buf, src, tar);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nvoid solveHanota(List<Integer> A, List<Integer> B, List<Integer> C) {\nint n = A.size();\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(n, A, B, C);\n}\n
hanota.cs
/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid Move(List<int> src, List<int> tar) {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\nint pan = src[^1];\nsrc.RemoveAt(src.Count - 1);\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.Add(pan);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nvoid DFS(int i, List<int> src, List<int> buf, List<int> tar) {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif (i == 1) {\nMove(src, tar);\nreturn;\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\nDFS(i - 1, src, tar, buf);\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nMove(src, tar);\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\nDFS(i - 1, buf, src, tar);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nvoid SolveHanota(List<int> A, List<int> B, List<int> C) {\nint n = A.Count;\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\nDFS(n, A, B, C);\n}\n
hanota.go
/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunc move(src, tar *list.List) {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\npan := src.Back()\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.PushBack(pan.Value)\n// \u79fb\u9664 src \u9876\u90e8\u5706\u76d8\nsrc.Remove(pan)\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nfunc dfsHanota(i int, src, buf, tar *list.List) {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif i == 1 {\nmove(src, tar)\nreturn\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfsHanota(i-1, src, tar, buf)\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src, tar)\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfsHanota(i-1, buf, src, tar)\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nfunc solveHanota(A, B, C *list.List) {\nn := A.Len()\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfsHanota(n, A, B, C)\n}\n
hanota.swift
/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunc move(src: inout [Int], tar: inout [Int]) {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\nlet pan = src.popLast()!\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.append(pan)\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nfunc dfs(i: Int, src: inout [Int], buf: inout [Int], tar: inout [Int]) {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif i == 1 {\nmove(src: &src, tar: &tar)\nreturn\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i: i - 1, src: &src, buf: &tar, tar: &buf)\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src: &src, tar: &tar)\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i: i - 1, src: &buf, buf: &src, tar: &tar)\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nfunc solveHanota(A: inout [Int], B: inout [Int], C: inout [Int]) {\nlet n = A.count\n// \u5217\u8868\u5c3e\u90e8\u662f\u67f1\u5b50\u9876\u90e8\n// \u5c06 src \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(i: n, src: &A, buf: &B, tar: &C)\n}\n
hanota.js
/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunction move(src, tar) {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\nconst pan = src.pop();\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.push(pan);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nfunction dfs(i, src, buf, tar) {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif (i === 1) {\nmove(src, tar);\nreturn;\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i - 1, src, tar, buf);\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src, tar);\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i - 1, buf, src, tar);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nfunction solveHanota(A, B, C) {\nconst n = A.length;\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(n, A, B, C);\n}\n
hanota.ts
/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunction move(src: number[], tar: number[]): void {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\nconst pan = src.pop();\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.push(pan);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nfunction dfs(i: number, src: number[], buf: number[], tar: number[]): void {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif (i === 1) {\nmove(src, tar);\nreturn;\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i - 1, src, tar, buf);\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src, tar);\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i - 1, buf, src, tar);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nfunction solveHanota(A: number[], B: number[], C: number[]): void {\nconst n = A.length;\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(n, A, B, C);\n}\n
hanota.dart
/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(List<int> src, List<int> tar) {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\nint pan = src.removeLast();\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.add(pan);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nvoid dfs(int i, List<int> src, List<int> buf, List<int> tar) {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif (i == 1) {\nmove(src, tar);\nreturn;\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i - 1, src, tar, buf);\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src, tar);\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i - 1, buf, src, tar);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nvoid solveHanota(List<int> A, List<int> B, List<int> C) {\nint n = A.length;\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(n, A, B, C);\n}\n
hanota.rs
/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfn move_pan(src: &mut Vec<i32>, tar: &mut Vec<i32>) {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\nlet pan = src.remove(src.len() - 1);\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.push(pan);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nfn dfs(i: i32, src: &mut Vec<i32>, buf: &mut Vec<i32>, tar: &mut Vec<i32>) {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif i == 1 {\nmove_pan(src, tar);\nreturn;\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i - 1, src, tar, buf);\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove_pan(src, tar);\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i - 1, buf, src, tar);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nfn solve_hanota(A: &mut Vec<i32>, B: &mut Vec<i32>, C: &mut Vec<i32>) {\nlet n = A.len() as i32;\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(n, A, B, C);\n}\n
hanota.c
/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(vector *src, vector *tar) {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\nint *panTemp = vectorBack(src);\nint *pan = malloc(sizeof(int));\n*pan = *panTemp;\nvectorPopback(src);\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\nvectorPushback(tar, pan, sizeof(int));\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nvoid dfs(int i, vector *src, vector *buf, vector *tar) {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif (i == 1) {\nmove(src, tar);\nreturn;\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i - 1, src, tar, buf);\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src, tar);\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i - 1, buf, src, tar);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nvoid solveHanota(vector *A, vector *B, vector *C) {\nint n = A->size;\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(n, A, B, C);\n}\n
hanota.zig
[class]{}-[func]{move}\n[class]{}-[func]{dfs}\n[class]{}-[func]{solveHanota}\n

\u5982\u56fe 12-15 \u6240\u793a\uff0c\u6c49\u8bfa\u5854\u95ee\u9898\u5f62\u6210\u4e00\u4e2a\u9ad8\u5ea6\u4e3a \\(n\\) \u7684\u9012\u5f52\u6811\uff0c\u6bcf\u4e2a\u8282\u70b9\u4ee3\u8868\u4e00\u4e2a\u5b50\u95ee\u9898\u3001\u5bf9\u5e94\u4e00\u4e2a\u5f00\u542f\u7684 dfs() \u51fd\u6570\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

\u56fe 12-15 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898\u7684\u9012\u5f52\u6811

Quote

\u6c49\u8bfa\u5854\u95ee\u9898\u6e90\u81ea\u4e00\u79cd\u53e4\u8001\u7684\u4f20\u8bf4\u6545\u4e8b\u3002\u5728\u53e4\u5370\u5ea6\u7684\u4e00\u4e2a\u5bfa\u5e99\u91cc\uff0c\u50e7\u4fa3\u4eec\u6709\u4e09\u6839\u9ad8\u5927\u7684\u94bb\u77f3\u67f1\u5b50\uff0c\u4ee5\u53ca \\(64\\) \u4e2a\u5927\u5c0f\u4e0d\u4e00\u7684\u91d1\u5706\u76d8\u3002\u50e7\u4fa3\u4eec\u4e0d\u65ad\u5730\u79fb\u52a8\u539f\u76d8\uff0c\u4ed6\u4eec\u76f8\u4fe1\u5728\u6700\u540e\u4e00\u4e2a\u5706\u76d8\u88ab\u6b63\u786e\u653e\u7f6e\u7684\u90a3\u4e00\u523b\uff0c\u8fd9\u4e2a\u4e16\u754c\u5c31\u4f1a\u7ed3\u675f\u3002

\u7136\u800c\uff0c\u5373\u4f7f\u50e7\u4fa3\u4eec\u6bcf\u79d2\u949f\u79fb\u52a8\u4e00\u6b21\uff0c\u603b\u5171\u9700\u8981\u5927\u7ea6 \\(2^{64} \\approx 1.84\u00d710^{19}\\) \u79d2\uff0c\u5408\u7ea6 \\(5850\\) \u4ebf\u5e74\uff0c\u8fdc\u8fdc\u8d85\u8fc7\u4e86\u73b0\u5728\u5bf9\u5b87\u5b99\u5e74\u9f84\u7684\u4f30\u8ba1\u3002\u6240\u4ee5\uff0c\u5018\u82e5\u8fd9\u4e2a\u4f20\u8bf4\u662f\u771f\u7684\uff0c\u6211\u4eec\u5e94\u8be5\u4e0d\u9700\u8981\u62c5\u5fc3\u4e16\u754c\u672b\u65e5\u7684\u5230\u6765\u3002

"},{"location":"chapter_divide_and_conquer/summary/","title":"12.5 \u00a0 \u5c0f\u7ed3","text":"
  • \u5206\u6cbb\u7b97\u6cd5\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u7b97\u6cd5\u8bbe\u8ba1\u7b56\u7565\uff0c\u5305\u62ec\u5206\uff08\u5212\u5206\uff09\u548c\u6cbb\uff08\u5408\u5e76\uff09\u4e24\u4e2a\u9636\u6bb5\uff0c\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u5b9e\u73b0\u3002
  • \u5224\u65ad\u662f\u5426\u662f\u5206\u6cbb\u7b97\u6cd5\u95ee\u9898\u7684\u4f9d\u636e\u5305\u62ec\uff1a\u95ee\u9898\u80fd\u5426\u88ab\u5206\u89e3\u3001\u5b50\u95ee\u9898\u662f\u5426\u72ec\u7acb\u3001\u5b50\u95ee\u9898\u662f\u5426\u53ef\u4ee5\u88ab\u5408\u5e76\u3002
  • \u5f52\u5e76\u6392\u5e8f\u662f\u5206\u6cbb\u7b56\u7565\u7684\u5178\u578b\u5e94\u7528\uff0c\u5176\u9012\u5f52\u5730\u5c06\u6570\u7ec4\u5212\u5206\u4e3a\u7b49\u957f\u7684\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u76f4\u5230\u53ea\u5269\u4e00\u4e2a\u5143\u7d20\u65f6\u5f00\u59cb\u9010\u5c42\u5408\u5e76\uff0c\u4ece\u800c\u5b8c\u6210\u6392\u5e8f\u3002
  • \u5f15\u5165\u5206\u6cbb\u7b56\u7565\u5f80\u5f80\u53ef\u4ee5\u5e26\u6765\u7b97\u6cd5\u6548\u7387\u7684\u63d0\u5347\u3002\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u7b56\u7565\u51cf\u5c11\u4e86\u64cd\u4f5c\u6570\u91cf\uff1b\u53e6\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u540e\u6709\u5229\u4e8e\u7cfb\u7edf\u7684\u5e76\u884c\u4f18\u5316\u3002
  • \u5206\u6cbb\u65e2\u53ef\u4ee5\u89e3\u51b3\u8bb8\u591a\u7b97\u6cd5\u95ee\u9898\uff0c\u4e5f\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u8bbe\u8ba1\u4e2d\uff0c\u5904\u5904\u53ef\u89c1\u5176\u8eab\u5f71\u3002
  • \u76f8\u8f83\u4e8e\u66b4\u529b\u641c\u7d22\uff0c\u81ea\u9002\u5e94\u641c\u7d22\u6548\u7387\u66f4\u9ad8\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u7684\u641c\u7d22\u7b97\u6cd5\u901a\u5e38\u90fd\u662f\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u5b9e\u73b0\u7684\u3002
  • \u4e8c\u5206\u67e5\u627e\u662f\u5206\u6cbb\u7b56\u7565\u7684\u53e6\u4e00\u4e2a\u5178\u578b\u5e94\u7528\uff0c\u5b83\u4e0d\u5305\u542b\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u8fdb\u884c\u5408\u5e76\u7684\u6b65\u9aa4\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u5206\u6cbb\u5b9e\u73b0\u4e8c\u5206\u67e5\u627e\u3002
  • \u5728\u6784\u5efa\u4e8c\u53c9\u6811\u95ee\u9898\u4e2d\uff0c\u6784\u5efa\u6811\uff08\u539f\u95ee\u9898\uff09\u53ef\u4ee5\u88ab\u5212\u5206\u4e3a\u6784\u5efa\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\uff08\u5b50\u95ee\u9898\uff09\uff0c\u5176\u53ef\u4ee5\u901a\u8fc7\u5212\u5206\u524d\u5e8f\u904d\u5386\u548c\u4e2d\u5e8f\u904d\u5386\u7684\u7d22\u5f15\u533a\u95f4\u6765\u5b9e\u73b0\u3002
  • \u5728\u6c49\u8bfa\u5854\u95ee\u9898\u4e2d\uff0c\u4e00\u4e2a\u89c4\u6a21\u4e3a \\(n\\) \u7684\u95ee\u9898\u53ef\u4ee5\u88ab\u5212\u5206\u4e3a\u4e24\u4e2a\u89c4\u6a21\u4e3a \\(n-1\\) \u7684\u5b50\u95ee\u9898\u548c\u4e00\u4e2a\u89c4\u6a21\u4e3a \\(1\\) \u7684\u5b50\u95ee\u9898\u3002\u6309\u987a\u5e8f\u89e3\u51b3\u8fd9\u4e09\u4e2a\u5b50\u95ee\u9898\u540e\uff0c\u539f\u95ee\u9898\u968f\u4e4b\u5f97\u5230\u89e3\u51b3\u3002
"},{"location":"chapter_dynamic_programming/","title":"\u7b2c 14 \u7ae0 \u00a0 \u52a8\u6001\u89c4\u5212","text":"

Abstract

\u5c0f\u6eaa\u6c47\u5165\u6cb3\u6d41\uff0c\u6c5f\u6cb3\u6c47\u5165\u5927\u6d77\u3002

\u52a8\u6001\u89c4\u5212\u5c06\u5c0f\u95ee\u9898\u7684\u89e3\u6c47\u96c6\u6210\u5927\u95ee\u9898\u7684\u7b54\u6848\uff0c\u4e00\u6b65\u6b65\u5f15\u9886\u6211\u4eec\u8d70\u5411\u89e3\u51b3\u95ee\u9898\u7684\u5f7c\u5cb8\u3002

"},{"location":"chapter_dynamic_programming/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 14.1 \u00a0 \u521d\u63a2\u52a8\u6001\u89c4\u5212
  • 14.2 \u00a0 DP \u95ee\u9898\u7279\u6027
  • 14.3 \u00a0 DP \u89e3\u9898\u601d\u8def
  • 14.4 \u00a0 0-1 \u80cc\u5305\u95ee\u9898
  • 14.5 \u00a0 \u5b8c\u5168\u80cc\u5305\u95ee\u9898
  • 14.6 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898
  • 14.7 \u00a0 \u5c0f\u7ed3
"},{"location":"chapter_dynamic_programming/dp_problem_features/","title":"14.2 \u00a0 \u52a8\u6001\u89c4\u5212\u95ee\u9898\u7279\u6027","text":"

\u5728\u4e0a\u8282\u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u52a8\u6001\u89c4\u5212\u662f\u5982\u4f55\u901a\u8fc7\u5b50\u95ee\u9898\u5206\u89e3\u6765\u6c42\u89e3\u95ee\u9898\u7684\u3002\u5b9e\u9645\u4e0a\uff0c\u5b50\u95ee\u9898\u5206\u89e3\u662f\u4e00\u79cd\u901a\u7528\u7684\u7b97\u6cd5\u601d\u8def\uff0c\u5728\u5206\u6cbb\u3001\u52a8\u6001\u89c4\u5212\u3001\u56de\u6eaf\u4e2d\u7684\u4fa7\u91cd\u70b9\u4e0d\u540c\u3002

  • \u5206\u6cbb\u7b97\u6cd5\u9012\u5f52\u5730\u5c06\u539f\u95ee\u9898\u5212\u5206\u4e3a\u591a\u4e2a\u76f8\u4e92\u72ec\u7acb\u7684\u5b50\u95ee\u9898\uff0c\u76f4\u81f3\u6700\u5c0f\u5b50\u95ee\u9898\uff0c\u5e76\u5728\u56de\u6eaf\u4e2d\u5408\u5e76\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u6700\u7ec8\u5f97\u5230\u539f\u95ee\u9898\u7684\u89e3\u3002
  • \u52a8\u6001\u89c4\u5212\u4e5f\u5bf9\u95ee\u9898\u8fdb\u884c\u9012\u5f52\u5206\u89e3\uff0c\u4f46\u4e0e\u5206\u6cbb\u7b97\u6cd5\u7684\u4e3b\u8981\u533a\u522b\u662f\uff0c\u52a8\u6001\u89c4\u5212\u4e2d\u7684\u5b50\u95ee\u9898\u662f\u76f8\u4e92\u4f9d\u8d56\u7684\uff0c\u5728\u5206\u89e3\u8fc7\u7a0b\u4e2d\u4f1a\u51fa\u73b0\u8bb8\u591a\u91cd\u53e0\u5b50\u95ee\u9898\u3002
  • \u56de\u6eaf\u7b97\u6cd5\u5728\u5c1d\u8bd5\u548c\u56de\u9000\u4e2d\u7a77\u4e3e\u6240\u6709\u53ef\u80fd\u7684\u89e3\uff0c\u5e76\u901a\u8fc7\u526a\u679d\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u641c\u7d22\u5206\u652f\u3002\u539f\u95ee\u9898\u7684\u89e3\u7531\u4e00\u7cfb\u5217\u51b3\u7b56\u6b65\u9aa4\u6784\u6210\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6bcf\u4e2a\u51b3\u7b56\u6b65\u9aa4\u4e4b\u524d\u7684\u5b50\u5e8f\u5217\u770b\u4f5c\u4e3a\u4e00\u4e2a\u5b50\u95ee\u9898\u3002

\u5b9e\u9645\u4e0a\uff0c\u52a8\u6001\u89c4\u5212\u5e38\u7528\u6765\u6c42\u89e3\u6700\u4f18\u5316\u95ee\u9898\uff0c\u5b83\u4eec\u4e0d\u4ec5\u5305\u542b\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u8fd8\u5177\u6709\u53e6\u5916\u4e24\u5927\u7279\u6027\uff1a\u6700\u4f18\u5b50\u7ed3\u6784\u3001\u65e0\u540e\u6548\u6027\u3002

"},{"location":"chapter_dynamic_programming/dp_problem_features/#1421","title":"14.2.1 \u00a0 \u6700\u4f18\u5b50\u7ed3\u6784","text":"

\u6211\u4eec\u5bf9\u722c\u697c\u68af\u95ee\u9898\u7a0d\u4f5c\u6539\u52a8\uff0c\u4f7f\u4e4b\u66f4\u52a0\u9002\u5408\u5c55\u793a\u6700\u4f18\u5b50\u7ed3\u6784\u6982\u5ff5\u3002

\u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7

\u7ed9\u5b9a\u4e00\u4e2a\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\uff0c\u6bcf\u4e00\u9636\u697c\u68af\u4e0a\u90fd\u8d34\u6709\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\uff0c\u8868\u793a\u4f60\u5728\u8be5\u53f0\u9636\u6240\u9700\u8981\u4ed8\u51fa\u7684\u4ee3\u4ef7\u3002\u7ed9\u5b9a\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\u6570\u7ec4 \\(cost\\) \uff0c\u5176\u4e2d \\(cost[i]\\) \u8868\u793a\u5728\u7b2c \\(i\\) \u4e2a\u53f0\u9636\u9700\u8981\u4ed8\u51fa\u7684\u4ee3\u4ef7\uff0c\\(cost[0]\\) \u4e3a\u5730\u9762\u8d77\u59cb\u70b9\u3002\u8bf7\u8ba1\u7b97\u6700\u5c11\u9700\u8981\u4ed8\u51fa\u591a\u5c11\u4ee3\u4ef7\u624d\u80fd\u5230\u8fbe\u9876\u90e8\uff1f

\u5982\u56fe 14-6 \u6240\u793a\uff0c\u82e5\u7b2c \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u9636\u7684\u4ee3\u4ef7\u5206\u522b\u4e3a \\(1\\)\u3001\\(10\\)\u3001\\(1\\) \uff0c\u5219\u4ece\u5730\u9762\u722c\u5230\u7b2c \\(3\\) \u9636\u7684\u6700\u5c0f\u4ee3\u4ef7\u4e3a \\(2\\) \u3002

\u56fe 14-6 \u00a0 \u722c\u5230\u7b2c 3 \u9636\u7684\u6700\u5c0f\u4ee3\u4ef7

\u8bbe \\(dp[i]\\) \u4e3a\u722c\u5230\u7b2c \\(i\\) \u9636\u7d2f\u8ba1\u4ed8\u51fa\u7684\u4ee3\u4ef7\uff0c\u7531\u4e8e\u7b2c \\(i\\) \u9636\u53ea\u53ef\u80fd\u4ece \\(i - 1\\) \u9636\u6216 \\(i - 2\\) \u9636\u8d70\u6765\uff0c\u56e0\u6b64 \\(dp[i]\\) \u53ea\u53ef\u80fd\u7b49\u4e8e \\(dp[i - 1] + cost[i]\\) \u6216 \\(dp[i - 2] + cost[i]\\) \u3002\u4e3a\u4e86\u5c3d\u53ef\u80fd\u51cf\u5c11\u4ee3\u4ef7\uff0c\u6211\u4eec\u5e94\u8be5\u9009\u62e9\u4e24\u8005\u4e2d\u8f83\u5c0f\u7684\u90a3\u4e00\u4e2a\uff1a

\\[ dp[i] = \\min(dp[i-1], dp[i-2]) + cost[i] \\]

\u8fd9\u4fbf\u53ef\u4ee5\u5f15\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\u7684\u542b\u4e49\uff1a\u539f\u95ee\u9898\u7684\u6700\u4f18\u89e3\u662f\u4ece\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u6784\u5efa\u5f97\u6765\u7684\u3002

\u672c\u9898\u663e\u7136\u5177\u6709\u6700\u4f18\u5b50\u7ed3\u6784\uff1a\u6211\u4eec\u4ece\u4e24\u4e2a\u5b50\u95ee\u9898\u6700\u4f18\u89e3 \\(dp[i-1]\\) \u548c \\(dp[i-2]\\) \u4e2d\u6311\u9009\u51fa\u8f83\u4f18\u7684\u90a3\u4e00\u4e2a\uff0c\u5e76\u7528\u5b83\u6784\u5efa\u51fa\u539f\u95ee\u9898 \\(dp[i]\\) \u7684\u6700\u4f18\u89e3\u3002

\u90a3\u4e48\uff0c\u4e0a\u8282\u7684\u722c\u697c\u68af\u9898\u76ee\u6709\u6ca1\u6709\u6700\u4f18\u5b50\u7ed3\u6784\u5462\uff1f\u5b83\u7684\u76ee\u6807\u662f\u6c42\u89e3\u65b9\u6848\u6570\u91cf\uff0c\u770b\u4f3c\u662f\u4e00\u4e2a\u8ba1\u6570\u95ee\u9898\uff0c\u4f46\u5982\u679c\u6362\u4e00\u79cd\u95ee\u6cd5\uff1a\u201c\u6c42\u89e3\u6700\u5927\u65b9\u6848\u6570\u91cf\u201d\u3002\u6211\u4eec\u610f\u5916\u5730\u53d1\u73b0\uff0c\u867d\u7136\u9898\u76ee\u4fee\u6539\u524d\u540e\u662f\u7b49\u4ef7\u7684\uff0c\u4f46\u6700\u4f18\u5b50\u7ed3\u6784\u6d6e\u73b0\u51fa\u6765\u4e86\uff1a\u7b2c \\(n\\) \u9636\u6700\u5927\u65b9\u6848\u6570\u91cf\u7b49\u4e8e\u7b2c \\(n-1\\) \u9636\u548c\u7b2c \\(n-2\\) \u9636\u6700\u5927\u65b9\u6848\u6570\u91cf\u4e4b\u548c\u3002\u6240\u4ee5\u8bf4\uff0c\u6700\u4f18\u5b50\u7ed3\u6784\u7684\u89e3\u91ca\u65b9\u5f0f\u6bd4\u8f83\u7075\u6d3b\uff0c\u5728\u4e0d\u540c\u95ee\u9898\u4e2d\u4f1a\u6709\u4e0d\u540c\u7684\u542b\u4e49\u3002

\u6839\u636e\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff0c\u4ee5\u53ca\u521d\u59cb\u72b6\u6001 \\(dp[1] = cost[1]\\) \u548c \\(dp[2] = cost[2]\\) \uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f97\u5230\u52a8\u6001\u89c4\u5212\u4ee3\u7801\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig min_cost_climbing_stairs_dp.py
def min_cost_climbing_stairs_dp(cost: list[int]) -> int:\n\"\"\"\u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(cost) - 1\nif n == 1 or n == 2:\nreturn cost[n]\n# \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\ndp = [0] * (n + 1)\n# \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1], dp[2] = cost[1], cost[2]\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in range(3, n + 1):\ndp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\nreturn dp[n]\n
min_cost_climbing_stairs_dp.cpp
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(vector<int> &cost) {\nint n = cost.size() - 1;\nif (n == 1 || n == 2)\nreturn cost[n];\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvector<int> dp(n + 1);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1];\ndp[2] = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n}\nreturn dp[n];\n}\n
min_cost_climbing_stairs_dp.java
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(int[] cost) {\nint n = cost.length - 1;\nif (n == 1 || n == 2)\nreturn cost[n];\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nint[] dp = new int[n + 1];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1];\ndp[2] = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n}\nreturn dp[n];\n}\n
min_cost_climbing_stairs_dp.cs
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint MinCostClimbingStairsDP(int[] cost) {\nint n = cost.Length - 1;\nif (n == 1 || n == 2)\nreturn cost[n];\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nint[] dp = new int[n + 1];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1];\ndp[2] = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i] = Math.Min(dp[i - 1], dp[i - 2]) + cost[i];\n}\nreturn dp[n];\n}\n
min_cost_climbing_stairs_dp.go
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDP(cost []int) int {\nn := len(cost) - 1\nif n == 1 || n == 2 {\nreturn cost[n]\n}\nmin := func(a, b int) int {\nif a < b {\nreturn a\n}\nreturn b\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\ndp := make([]int, n+1)\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1]\ndp[2] = cost[2]\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i := 3; i <= n; i++ {\ndp[i] = min(dp[i-1], dp[i-2]) + cost[i]\n}\nreturn dp[n]\n}\n
min_cost_climbing_stairs_dp.swift
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDP(cost: [Int]) -> Int {\nlet n = cost.count - 1\nif n == 1 || n == 2 {\nreturn cost[n]\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvar dp = Array(repeating: 0, count: n + 1)\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1]\ndp[2] = cost[2]\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in stride(from: 3, through: n, by: 1) {\ndp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n}\nreturn dp[n]\n}\n
min_cost_climbing_stairs_dp.js
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction minCostClimbingStairsDP(cost) {\nconst n = cost.length - 1;\nif (n === 1 || n === 2) {\nreturn cost[n];\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nconst dp = new Array(n + 1);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1];\ndp[2] = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (let i = 3; i <= n; i++) {\ndp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n}\nreturn dp[n];\n}\n
min_cost_climbing_stairs_dp.ts
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction minCostClimbingStairsDP(cost: Array<number>): number {\nconst n = cost.length - 1;\nif (n === 1 || n === 2) {\nreturn cost[n];\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nconst dp = new Array(n + 1);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1];\ndp[2] = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (let i = 3; i <= n; i++) {\ndp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n}\nreturn dp[n];\n}\n
min_cost_climbing_stairs_dp.dart
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(List<int> cost) {\nint n = cost.length - 1;\nif (n == 1 || n == 2) return cost[n];\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nList<int> dp = List.filled(n + 1, 0);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1];\ndp[2] = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n}\nreturn dp[n];\n}\n
min_cost_climbing_stairs_dp.rs
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfn min_cost_climbing_stairs_dp(cost: &[i32]) -> i32 {\nlet n = cost.len() - 1;\nif n == 1 || n == 2 { return cost[n]; }\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nlet mut dp = vec![-1; n + 1];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1];\ndp[2] = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in 3..=n {\ndp[i] = cmp::min(dp[i - 1], dp[i - 2]) + cost[i];\n}\ndp[n]\n}\n
min_cost_climbing_stairs_dp.c
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(int cost[], int costSize) {\nint n = costSize - 1;\nif (n == 1 || n == 2)\nreturn cost[n];\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nint dp[n + 1];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1];\ndp[2] = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n}\nreturn dp[n];\n}\n
min_cost_climbing_stairs_dp.zig
// \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212\nfn minCostClimbingStairsDP(comptime cost: []i32) i32 {\ncomptime var n = cost.len - 1;\nif (n == 1 or n == 2) {\nreturn cost[n];\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvar dp = [_]i32{-1} ** (n + 1);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1];\ndp[2] = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (3..n + 1) |i| {\ndp[i] = @min(dp[i - 1], dp[i - 2]) + cost[i];\n}\nreturn dp[n];\n}\n

\u56fe 14-7 \u5c55\u793a\u4e86\u4ee5\u4e0a\u4ee3\u7801\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b\u3002

\u56fe 14-7 \u00a0 \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

\u672c\u9898\u4e5f\u53ef\u4ee5\u8fdb\u884c\u7a7a\u95f4\u4f18\u5316\uff0c\u5c06\u4e00\u7ef4\u538b\u7f29\u81f3\u96f6\u7ef4\uff0c\u4f7f\u5f97\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n)\\) \u964d\u4f4e\u81f3 \\(O(1)\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig min_cost_climbing_stairs_dp.py
def min_cost_climbing_stairs_dp_comp(cost: list[int]) -> int:\n\"\"\"\u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(cost) - 1\nif n == 1 or n == 2:\nreturn cost[n]\na, b = cost[1], cost[2]\nfor i in range(3, n + 1):\na, b = b, min(a, b) + cost[i]\nreturn b\n
min_cost_climbing_stairs_dp.cpp
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(vector<int> &cost) {\nint n = cost.size() - 1;\nif (n == 1 || n == 2)\nreturn cost[n];\nint a = cost[1], b = cost[2];\nfor (int i = 3; i <= n; i++) {\nint tmp = b;\nb = min(a, tmp) + cost[i];\na = tmp;\n}\nreturn b;\n}\n
min_cost_climbing_stairs_dp.java
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(int[] cost) {\nint n = cost.length - 1;\nif (n == 1 || n == 2)\nreturn cost[n];\nint a = cost[1], b = cost[2];\nfor (int i = 3; i <= n; i++) {\nint tmp = b;\nb = Math.min(a, tmp) + cost[i];\na = tmp;\n}\nreturn b;\n}\n
min_cost_climbing_stairs_dp.cs
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint MinCostClimbingStairsDPComp(int[] cost) {\nint n = cost.Length - 1;\nif (n == 1 || n == 2)\nreturn cost[n];\nint a = cost[1], b = cost[2];\nfor (int i = 3; i <= n; i++) {\nint tmp = b;\nb = Math.Min(a, tmp) + cost[i];\na = tmp;\n}\nreturn b;\n}\n
min_cost_climbing_stairs_dp.go
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDPComp(cost []int) int {\nn := len(cost) - 1\nif n == 1 || n == 2 {\nreturn cost[n]\n}\nmin := func(a, b int) int {\nif a < b {\nreturn a\n}\nreturn b\n}\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\na, b := cost[1], cost[2]\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i := 3; i <= n; i++ {\ntmp := b\nb = min(a, tmp) + cost[i]\na = tmp\n}\nreturn b\n}\n
min_cost_climbing_stairs_dp.swift
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDPComp(cost: [Int]) -> Int {\nlet n = cost.count - 1\nif n == 1 || n == 2 {\nreturn cost[n]\n}\nvar (a, b) = (cost[1], cost[2])\nfor i in stride(from: 3, through: n, by: 1) {\n(a, b) = (b, min(a, b) + cost[i])\n}\nreturn b\n}\n
min_cost_climbing_stairs_dp.js
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction minCostClimbingStairsDPComp(cost) {\nconst n = cost.length - 1;\nif (n === 1 || n === 2) {\nreturn cost[n];\n}\nlet a = cost[1],\nb = cost[2];\nfor (let i = 3; i <= n; i++) {\nconst tmp = b;\nb = Math.min(a, tmp) + cost[i];\na = tmp;\n}\nreturn b;\n}\n
min_cost_climbing_stairs_dp.ts
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction minCostClimbingStairsDPComp(cost: Array<number>): number {\nconst n = cost.length - 1;\nif (n === 1 || n === 2) {\nreturn cost[n];\n}\nlet a = cost[1],\nb = cost[2];\nfor (let i = 3; i <= n; i++) {\nconst tmp = b;\nb = Math.min(a, tmp) + cost[i];\na = tmp;\n}\nreturn b;\n}\n
min_cost_climbing_stairs_dp.dart
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(List<int> cost) {\nint n = cost.length - 1;\nif (n == 1 || n == 2) return cost[n];\nint a = cost[1], b = cost[2];\nfor (int i = 3; i <= n; i++) {\nint tmp = b;\nb = min(a, tmp) + cost[i];\na = tmp;\n}\nreturn b;\n}\n
min_cost_climbing_stairs_dp.rs
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn min_cost_climbing_stairs_dp_comp(cost: &[i32]) -> i32 {\nlet n = cost.len() - 1;\nif n == 1 || n == 2 { return cost[n] };\nlet (mut a, mut b) = (cost[1], cost[2]);\nfor i in 3..=n {\nlet tmp = b;\nb = cmp::min(a, tmp) + cost[i];\na = tmp;\n}\nb\n}\n
min_cost_climbing_stairs_dp.c
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(int cost[], int costSize) {\nint n = costSize - 1;\nif (n == 1 || n == 2)\nreturn cost[n];\nint a = cost[1], b = cost[2];\nfor (int i = 3; i <= n; i++) {\nint tmp = b;\nb = min(a, tmp) + cost[i];\na = tmp;\n}\nreturn b;\n}\n
min_cost_climbing_stairs_dp.zig
// \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn minCostClimbingStairsDPComp(cost: []i32) i32 {\nvar n = cost.len - 1;\nif (n == 1 or n == 2) {\nreturn cost[n];\n}\nvar a = cost[1];\nvar b = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (3..n + 1) |i| {\nvar tmp = b;\nb = @min(a, tmp) + cost[i];\na = tmp;\n}\nreturn b;\n}\n
"},{"location":"chapter_dynamic_programming/dp_problem_features/#1422","title":"14.2.2 \u00a0 \u65e0\u540e\u6548\u6027","text":"

\u65e0\u540e\u6548\u6027\u662f\u52a8\u6001\u89c4\u5212\u80fd\u591f\u6709\u6548\u89e3\u51b3\u95ee\u9898\u7684\u91cd\u8981\u7279\u6027\u4e4b\u4e00\uff0c\u5b9a\u4e49\u4e3a\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u786e\u5b9a\u7684\u72b6\u6001\uff0c\u5b83\u7684\u672a\u6765\u53d1\u5c55\u53ea\u4e0e\u5f53\u524d\u72b6\u6001\u6709\u5173\uff0c\u800c\u4e0e\u5f53\u524d\u72b6\u6001\u8fc7\u53bb\u6240\u7ecf\u5386\u8fc7\u7684\u6240\u6709\u72b6\u6001\u65e0\u5173\u3002

\u4ee5\u722c\u697c\u68af\u95ee\u9898\u4e3a\u4f8b\uff0c\u7ed9\u5b9a\u72b6\u6001 \\(i\\) \uff0c\u5b83\u4f1a\u53d1\u5c55\u51fa\u72b6\u6001 \\(i+1\\) \u548c\u72b6\u6001 \\(i+2\\) \uff0c\u5206\u522b\u5bf9\u5e94\u8df3 \\(1\\) \u6b65\u548c\u8df3 \\(2\\) \u6b65\u3002\u5728\u505a\u51fa\u8fd9\u4e24\u79cd\u9009\u62e9\u65f6\uff0c\u6211\u4eec\u65e0\u987b\u8003\u8651\u72b6\u6001 \\(i\\) \u4e4b\u524d\u7684\u72b6\u6001\uff0c\u5b83\u4eec\u5bf9\u72b6\u6001 \\(i\\) \u7684\u672a\u6765\u6ca1\u6709\u5f71\u54cd\u3002

\u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u5411\u722c\u697c\u68af\u95ee\u9898\u6dfb\u52a0\u4e00\u4e2a\u7ea6\u675f\uff0c\u60c5\u51b5\u5c31\u4e0d\u4e00\u6837\u4e86\u3002

\u5e26\u7ea6\u675f\u722c\u697c\u68af

\u7ed9\u5b9a\u4e00\u4e2a\u5171\u6709 \\(n\\) \u9636\u7684\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\uff0c\u4f46\u4e0d\u80fd\u8fde\u7eed\u4e24\u8f6e\u8df3 \\(1\\) \u9636\uff0c\u8bf7\u95ee\u6709\u591a\u5c11\u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\u3002

\u4f8b\u5982\u56fe 14-8 \uff0c\u722c\u4e0a\u7b2c \\(3\\) \u9636\u4ec5\u5269 \\(2\\) \u79cd\u53ef\u884c\u65b9\u6848\uff0c\u5176\u4e2d\u8fde\u7eed\u4e09\u6b21\u8df3 \\(1\\) \u9636\u7684\u65b9\u6848\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\uff0c\u56e0\u6b64\u88ab\u820d\u5f03\u3002

\u56fe 14-8 \u00a0 \u5e26\u7ea6\u675f\u722c\u5230\u7b2c 3 \u9636\u7684\u65b9\u6848\u6570\u91cf

\u5728\u8be5\u95ee\u9898\u4e2d\uff0c\u5982\u679c\u4e0a\u4e00\u8f6e\u662f\u8df3 \\(1\\) \u9636\u4e0a\u6765\u7684\uff0c\u90a3\u4e48\u4e0b\u4e00\u8f6e\u5c31\u5fc5\u987b\u8df3 \\(2\\) \u9636\u3002\u8fd9\u610f\u5473\u7740\uff0c\u4e0b\u4e00\u6b65\u9009\u62e9\u4e0d\u80fd\u7531\u5f53\u524d\u72b6\u6001\uff08\u5f53\u524d\u697c\u68af\u9636\u6570\uff09\u72ec\u7acb\u51b3\u5b9a\uff0c\u8fd8\u548c\u524d\u4e00\u4e2a\u72b6\u6001\uff08\u4e0a\u8f6e\u697c\u68af\u9636\u6570\uff09\u6709\u5173\u3002

\u4e0d\u96be\u53d1\u73b0\uff0c\u6b64\u95ee\u9898\u5df2\u4e0d\u6ee1\u8db3\u65e0\u540e\u6548\u6027\uff0c\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b \\(dp[i] = dp[i-1] + dp[i-2]\\) \u4e5f\u5931\u6548\u4e86\uff0c\u56e0\u4e3a \\(dp[i-1]\\) \u4ee3\u8868\u672c\u8f6e\u8df3 \\(1\\) \u9636\uff0c\u4f46\u5176\u4e2d\u5305\u542b\u4e86\u8bb8\u591a\u201c\u4e0a\u4e00\u8f6e\u8df3 \\(1\\) \u9636\u4e0a\u6765\u7684\u201d\u65b9\u6848\uff0c\u800c\u4e3a\u4e86\u6ee1\u8db3\u7ea6\u675f\uff0c\u6211\u4eec\u5c31\u4e0d\u80fd\u5c06 \\(dp[i-1]\\) \u76f4\u63a5\u8ba1\u5165 \\(dp[i]\\) \u4e2d\u3002

\u4e3a\u6b64\uff0c\u6211\u4eec\u9700\u8981\u6269\u5c55\u72b6\u6001\u5b9a\u4e49\uff1a\u72b6\u6001 \\([i, j]\\) \u8868\u793a\u5904\u5728\u7b2c \\(i\\) \u9636\u3001\u5e76\u4e14\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(j\\) \u9636\uff0c\u5176\u4e2d \\(j \\in \\{1, 2\\}\\) \u3002\u6b64\u72b6\u6001\u5b9a\u4e49\u6709\u6548\u5730\u533a\u5206\u4e86\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(1\\) \u9636\u8fd8\u662f \\(2\\) \u9636\uff0c\u6211\u4eec\u53ef\u4ee5\u636e\u6b64\u6765\u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u4ece\u4f55\u800c\u6765\u7684\u3002

  • \u5f53\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(1\\) \u9636\u65f6\uff0c\u4e0a\u4e0a\u4e00\u8f6e\u53ea\u80fd\u9009\u62e9\u8df3 \\(2\\) \u9636\uff0c\u5373 \\(dp[i, 1]\\) \u53ea\u80fd\u4ece \\(dp[i-1, 2]\\) \u8f6c\u79fb\u8fc7\u6765\u3002
  • \u5f53\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(2\\) \u9636\u65f6\uff0c\u4e0a\u4e0a\u4e00\u8f6e\u53ef\u9009\u62e9\u8df3 \\(1\\) \u9636\u6216\u8df3 \\(2\\) \u9636\uff0c\u5373 \\(dp[i, 2]\\) \u53ef\u4ee5\u4ece \\(dp[i-2, 1]\\) \u6216 \\(dp[i-2, 2]\\) \u8f6c\u79fb\u8fc7\u6765\u3002

\u5982\u56fe 14-9 \u6240\u793a\uff0c\u5728\u8be5\u5b9a\u4e49\u4e0b\uff0c\\(dp[i, j]\\) \u8868\u793a\u72b6\u6001 \\([i, j]\\) \u5bf9\u5e94\u7684\u65b9\u6848\u6570\u3002\u6b64\u65f6\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a

\\[ \\begin{cases} dp[i, 1] = dp[i-1, 2] \\\\ dp[i, 2] = dp[i-2, 1] + dp[i-2, 2] \\end{cases} \\]

\u56fe 14-9 \u00a0 \u8003\u8651\u7ea6\u675f\u4e0b\u7684\u9012\u63a8\u5173\u7cfb

\u6700\u7ec8\uff0c\u8fd4\u56de \\(dp[n, 1] + dp[n, 2]\\) \u5373\u53ef\uff0c\u4e24\u8005\u4e4b\u548c\u4ee3\u8868\u722c\u5230\u7b2c \\(n\\) \u9636\u7684\u65b9\u6848\u603b\u6570\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_constraint_dp.py
def climbing_stairs_constraint_dp(n: int) -> int:\n\"\"\"\u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nif n == 1 or n == 2:\nreturn 1\n# \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\ndp = [[0] * 3 for _ in range(n + 1)]\n# \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1], dp[1][2] = 1, 0\ndp[2][1], dp[2][2] = 0, 1\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in range(3, n + 1):\ndp[i][1] = dp[i - 1][2]\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2]\nreturn dp[n][1] + dp[n][2]\n
climbing_stairs_constraint_dp.cpp
/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\nif (n == 1 || n == 2) {\nreturn 1;\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvector<vector<int>> dp(n + 1, vector<int>(3, 0));\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1;\ndp[1][2] = 0;\ndp[2][1] = 0;\ndp[2][2] = 1;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i][1] = dp[i - 1][2];\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n}\nreturn dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.java
/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\nif (n == 1 || n == 2) {\nreturn 1;\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nint[][] dp = new int[n + 1][3];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1;\ndp[1][2] = 0;\ndp[2][1] = 0;\ndp[2][2] = 1;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i][1] = dp[i - 1][2];\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n}\nreturn dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.cs
/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint ClimbingStairsConstraintDP(int n) {\nif (n == 1 || n == 2) {\nreturn 1;\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nint[,] dp = new int[n + 1, 3];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1, 1] = 1;\ndp[1, 2] = 0;\ndp[2, 1] = 0;\ndp[2, 2] = 1;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i, 1] = dp[i - 1, 2];\ndp[i, 2] = dp[i - 2, 1] + dp[i - 2, 2];\n}\nreturn dp[n, 1] + dp[n, 2];\n}\n
climbing_stairs_constraint_dp.go
/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsConstraintDP(n int) int {\nif n == 1 || n == 2 {\nreturn 1\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\ndp := make([][3]int, n+1)\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1\ndp[1][2] = 0\ndp[2][1] = 0\ndp[2][2] = 1\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i := 3; i <= n; i++ {\ndp[i][1] = dp[i-1][2]\ndp[i][2] = dp[i-2][1] + dp[i-2][2]\n}\nreturn dp[n][1] + dp[n][2]\n}\n
climbing_stairs_constraint_dp.swift
/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsConstraintDP(n: Int) -> Int {\nif n == 1 || n == 2 {\nreturn 1\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvar dp = Array(repeating: Array(repeating: 0, count: 3), count: n + 1)\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1\ndp[1][2] = 0\ndp[2][1] = 0\ndp[2][2] = 1\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in stride(from: 3, through: n, by: 1) {\ndp[i][1] = dp[i - 1][2]\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n}\nreturn dp[n][1] + dp[n][2]\n}\n
climbing_stairs_constraint_dp.js
/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsConstraintDP(n) {\nif (n === 1 || n === 2) {\nreturn 1;\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nconst dp = Array.from(new Array(n + 1), () => new Array(3));\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1;\ndp[1][2] = 0;\ndp[2][1] = 0;\ndp[2][2] = 1;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (let i = 3; i <= n; i++) {\ndp[i][1] = dp[i - 1][2];\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n}\nreturn dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.ts
/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsConstraintDP(n: number): number {\nif (n === 1 || n === 2) {\nreturn 1;\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nconst dp = Array.from({ length: n + 1 }, () => new Array(3));\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1;\ndp[1][2] = 0;\ndp[2][1] = 0;\ndp[2][2] = 1;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (let i = 3; i <= n; i++) {\ndp[i][1] = dp[i - 1][2];\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n}\nreturn dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.dart
/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\nif (n == 1 || n == 2) {\nreturn 1;\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nList<List<int>> dp = List.generate(n + 1, (index) => List.filled(3, 0));\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1;\ndp[1][2] = 0;\ndp[2][1] = 0;\ndp[2][2] = 1;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i][1] = dp[i - 1][2];\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n}\nreturn dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.rs
/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfn climbing_stairs_constraint_dp(n: usize) -> i32 {\nif n == 1 || n == 2 { return 1 };\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nlet mut dp = vec![vec![-1; 3]; n + 1];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1;\ndp[1][2] = 0;\ndp[2][1] = 0;\ndp[2][2] = 1;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in 3..=n {\ndp[i][1] = dp[i - 1][2];\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n}\ndp[n][1] + dp[n][2]\n}\n
climbing_stairs_constraint_dp.c
/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\nif (n == 1 || n == 2) {\nreturn 1;\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nint dp[n + 1][3];\nmemset(dp, 0, sizeof(dp));\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1;\ndp[1][2] = 0;\ndp[2][1] = 0;\ndp[2][2] = 1;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i][1] = dp[i - 1][2];\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n}\nreturn dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.zig
// \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212\nfn climbingStairsConstraintDP(comptime n: usize) i32 {\nif (n == 1 or n == 2) {\nreturn 1;\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvar dp = [_][3]i32{ [_]i32{ -1, -1, -1 } } ** (n + 1);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1;\ndp[1][2] = 0;\ndp[2][1] = 0;\ndp[2][2] = 1;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (3..n + 1) |i| {\ndp[i][1] = dp[i - 1][2];\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n}\nreturn dp[n][1] + dp[n][2];\n}\n

\u5728\u4e0a\u9762\u7684\u6848\u4f8b\u4e2d\uff0c\u7531\u4e8e\u4ec5\u9700\u591a\u8003\u8651\u524d\u9762\u4e00\u4e2a\u72b6\u6001\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u901a\u8fc7\u6269\u5c55\u72b6\u6001\u5b9a\u4e49\uff0c\u4f7f\u5f97\u95ee\u9898\u91cd\u65b0\u6ee1\u8db3\u65e0\u540e\u6548\u6027\u3002\u7136\u800c\uff0c\u67d0\u4e9b\u95ee\u9898\u5177\u6709\u975e\u5e38\u4e25\u91cd\u7684\u201c\u6709\u540e\u6548\u6027\u201d\u3002

\u722c\u697c\u68af\u4e0e\u969c\u788d\u751f\u6210

\u7ed9\u5b9a\u4e00\u4e2a\u5171\u6709 \\(n\\) \u9636\u7684\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\u3002\u89c4\u5b9a\u5f53\u722c\u5230\u7b2c \\(i\\) \u9636\u65f6\uff0c\u7cfb\u7edf\u81ea\u52a8\u4f1a\u7ed9\u7b2c \\(2i\\) \u9636\u4e0a\u653e\u4e0a\u969c\u788d\u7269\uff0c\u4e4b\u540e\u6240\u6709\u8f6e\u90fd\u4e0d\u5141\u8bb8\u8df3\u5230\u7b2c \\(2i\\) \u9636\u4e0a\u3002\u4f8b\u5982\uff0c\u524d\u4e24\u8f6e\u5206\u522b\u8df3\u5230\u4e86\u7b2c \\(2\\)\u3001\\(3\\) \u9636\u4e0a\uff0c\u5219\u4e4b\u540e\u5c31\u4e0d\u80fd\u8df3\u5230\u7b2c \\(4\\)\u3001\\(6\\) \u9636\u4e0a\u3002\u8bf7\u95ee\u6709\u591a\u5c11\u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\u3002

\u5728\u8fd9\u4e2a\u95ee\u9898\u4e2d\uff0c\u4e0b\u6b21\u8df3\u8dc3\u4f9d\u8d56\u4e8e\u8fc7\u53bb\u6240\u6709\u7684\u72b6\u6001\uff0c\u56e0\u4e3a\u6bcf\u4e00\u6b21\u8df3\u8dc3\u90fd\u4f1a\u5728\u66f4\u9ad8\u7684\u9636\u68af\u4e0a\u8bbe\u7f6e\u969c\u788d\uff0c\u5e76\u5f71\u54cd\u672a\u6765\u7684\u8df3\u8dc3\u3002\u5bf9\u4e8e\u8fd9\u7c7b\u95ee\u9898\uff0c\u52a8\u6001\u89c4\u5212\u5f80\u5f80\u96be\u4ee5\u89e3\u51b3\u3002

\u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u590d\u6742\u7684\u7ec4\u5408\u4f18\u5316\u95ee\u9898\uff08\u4f8b\u5982\u65c5\u884c\u5546\u95ee\u9898\uff09\u90fd\u4e0d\u6ee1\u8db3\u65e0\u540e\u6548\u6027\u3002\u5bf9\u4e8e\u8fd9\u7c7b\u95ee\u9898\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u9009\u62e9\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\uff0c\u4f8b\u5982\u542f\u53d1\u5f0f\u641c\u7d22\u3001\u9057\u4f20\u7b97\u6cd5\u3001\u5f3a\u5316\u5b66\u4e60\u7b49\uff0c\u4ece\u800c\u5728\u6709\u9650\u65f6\u95f4\u5185\u5f97\u5230\u53ef\u7528\u7684\u5c40\u90e8\u6700\u4f18\u89e3\u3002

"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/","title":"14.3 \u00a0 \u52a8\u6001\u89c4\u5212\u89e3\u9898\u601d\u8def","text":"

\u4e0a\u4e24\u8282\u4ecb\u7ecd\u4e86\u52a8\u6001\u89c4\u5212\u95ee\u9898\u7684\u4e3b\u8981\u7279\u5f81\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u4e00\u8d77\u63a2\u7a76\u4e24\u4e2a\u66f4\u52a0\u5b9e\u7528\u7684\u95ee\u9898\u3002

  1. \u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u95ee\u9898\u662f\u4e0d\u662f\u52a8\u6001\u89c4\u5212\u95ee\u9898\uff1f
  2. \u6c42\u89e3\u52a8\u6001\u89c4\u5212\u95ee\u9898\u8be5\u4ece\u4f55\u5904\u5165\u624b\uff0c\u5b8c\u6574\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f
"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1431","title":"14.3.1 \u00a0 \u95ee\u9898\u5224\u65ad","text":"

\u603b\u7684\u6765\u8bf4\uff0c\u5982\u679c\u4e00\u4e2a\u95ee\u9898\u5305\u542b\u91cd\u53e0\u5b50\u95ee\u9898\u3001\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u5e76\u6ee1\u8db3\u65e0\u540e\u6548\u6027\uff0c\u90a3\u4e48\u5b83\u901a\u5e38\u5c31\u9002\u5408\u7528\u52a8\u6001\u89c4\u5212\u6c42\u89e3\u3002\u7136\u800c\uff0c\u6211\u4eec\u5f88\u96be\u4ece\u95ee\u9898\u63cf\u8ff0\u4e0a\u76f4\u63a5\u63d0\u53d6\u51fa\u8fd9\u4e9b\u7279\u6027\u3002\u56e0\u6b64\u6211\u4eec\u901a\u5e38\u4f1a\u653e\u5bbd\u6761\u4ef6\uff0c\u5148\u89c2\u5bdf\u95ee\u9898\u662f\u5426\u9002\u5408\u4f7f\u7528\u56de\u6eaf\uff08\u7a77\u4e3e\uff09\u89e3\u51b3\u3002

\u9002\u5408\u7528\u56de\u6eaf\u89e3\u51b3\u7684\u95ee\u9898\u901a\u5e38\u6ee1\u8db3\u201c\u51b3\u7b56\u6811\u6a21\u578b\u201d\uff0c\u8fd9\u79cd\u95ee\u9898\u53ef\u4ee5\u4f7f\u7528\u6811\u5f62\u7ed3\u6784\u6765\u63cf\u8ff0\uff0c\u5176\u4e2d\u6bcf\u4e00\u4e2a\u8282\u70b9\u4ee3\u8868\u4e00\u4e2a\u51b3\u7b56\uff0c\u6bcf\u4e00\u6761\u8def\u5f84\u4ee3\u8868\u4e00\u4e2a\u51b3\u7b56\u5e8f\u5217\u3002

\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c\u95ee\u9898\u5305\u542b\u660e\u786e\u7684\u51b3\u7b56\u6982\u5ff5\uff0c\u5e76\u4e14\u89e3\u662f\u901a\u8fc7\u4e00\u7cfb\u5217\u51b3\u7b56\u4ea7\u751f\u7684\uff0c\u90a3\u4e48\u5b83\u5c31\u6ee1\u8db3\u51b3\u7b56\u6811\u6a21\u578b\uff0c\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528\u56de\u6eaf\u6765\u89e3\u51b3\u3002

\u5728\u6b64\u57fa\u7840\u4e0a\uff0c\u52a8\u6001\u89c4\u5212\u95ee\u9898\u8fd8\u6709\u4e00\u4e9b\u5224\u65ad\u7684\u201c\u52a0\u5206\u9879\u201d\u3002

  • \u95ee\u9898\u5305\u542b\u6700\u5927\uff08\u5c0f\uff09\u6216\u6700\u591a\uff08\u5c11\uff09\u7b49\u6700\u4f18\u5316\u63cf\u8ff0\u3002
  • \u95ee\u9898\u7684\u72b6\u6001\u80fd\u591f\u4f7f\u7528\u4e00\u4e2a\u5217\u8868\u3001\u591a\u7ef4\u77e9\u9635\u6216\u6811\u6765\u8868\u793a\uff0c\u5e76\u4e14\u4e00\u4e2a\u72b6\u6001\u4e0e\u5176\u5468\u56f4\u7684\u72b6\u6001\u5b58\u5728\u9012\u63a8\u5173\u7cfb\u3002

\u76f8\u5e94\u5730\uff0c\u4e5f\u5b58\u5728\u4e00\u4e9b\u201c\u51cf\u5206\u9879\u201d\u3002

  • \u95ee\u9898\u7684\u76ee\u6807\u662f\u627e\u51fa\u6240\u6709\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u800c\u4e0d\u662f\u627e\u51fa\u6700\u4f18\u89e3\u3002
  • \u95ee\u9898\u63cf\u8ff0\u4e2d\u6709\u660e\u663e\u7684\u6392\u5217\u7ec4\u5408\u7684\u7279\u5f81\uff0c\u9700\u8981\u8fd4\u56de\u5177\u4f53\u7684\u591a\u4e2a\u65b9\u6848\u3002

\u5982\u679c\u4e00\u4e2a\u95ee\u9898\u6ee1\u8db3\u51b3\u7b56\u6811\u6a21\u578b\uff0c\u5e76\u5177\u6709\u8f83\u4e3a\u660e\u663e\u7684\u201c\u52a0\u5206\u9879\u201c\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5047\u8bbe\u5b83\u662f\u4e00\u4e2a\u52a8\u6001\u89c4\u5212\u95ee\u9898\uff0c\u5e76\u5728\u6c42\u89e3\u8fc7\u7a0b\u4e2d\u9a8c\u8bc1\u5b83\u3002

"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1432","title":"14.3.2 \u00a0 \u95ee\u9898\u6c42\u89e3\u6b65\u9aa4","text":"

\u52a8\u6001\u89c4\u5212\u7684\u89e3\u9898\u6d41\u7a0b\u4f1a\u56e0\u95ee\u9898\u7684\u6027\u8d28\u548c\u96be\u5ea6\u800c\u6709\u6240\u4e0d\u540c\uff0c\u4f46\u901a\u5e38\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u63cf\u8ff0\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u5efa\u7acb \\(dp\\) \u8868\uff0c\u63a8\u5bfc\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff0c\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u7b49\u3002

\u4e3a\u4e86\u66f4\u5f62\u8c61\u5730\u5c55\u793a\u89e3\u9898\u6b65\u9aa4\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u7ecf\u5178\u95ee\u9898\u201c\u6700\u5c0f\u8def\u5f84\u548c\u201d\u6765\u4e3e\u4f8b\u3002

Question

\u7ed9\u5b9a\u4e00\u4e2a \\(n \\times m\\) \u7684\u4e8c\u7ef4\u7f51\u683c grid \uff0c\u7f51\u683c\u4e2d\u7684\u6bcf\u4e2a\u5355\u5143\u683c\u5305\u542b\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\uff0c\u8868\u793a\u8be5\u5355\u5143\u683c\u7684\u4ee3\u4ef7\u3002\u673a\u5668\u4eba\u4ee5\u5de6\u4e0a\u89d2\u5355\u5143\u683c\u4e3a\u8d77\u59cb\u70b9\uff0c\u6bcf\u6b21\u53ea\u80fd\u5411\u4e0b\u6216\u8005\u5411\u53f3\u79fb\u52a8\u4e00\u6b65\uff0c\u76f4\u81f3\u5230\u8fbe\u53f3\u4e0b\u89d2\u5355\u5143\u683c\u3002\u8bf7\u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230\u53f3\u4e0b\u89d2\u7684\u6700\u5c0f\u8def\u5f84\u548c\u3002

\u56fe 14-10 \u5c55\u793a\u4e86\u4e00\u4e2a\u4f8b\u5b50\uff0c\u7ed9\u5b9a\u7f51\u683c\u7684\u6700\u5c0f\u8def\u5f84\u548c\u4e3a \\(13\\) \u3002

\u56fe 14-10 \u00a0 \u6700\u5c0f\u8def\u5f84\u548c\u793a\u4f8b\u6570\u636e

\u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868

\u672c\u9898\u7684\u6bcf\u4e00\u8f6e\u7684\u51b3\u7b56\u5c31\u662f\u4ece\u5f53\u524d\u683c\u5b50\u5411\u4e0b\u6216\u5411\u53f3\u4e00\u6b65\u3002\u8bbe\u5f53\u524d\u683c\u5b50\u7684\u884c\u5217\u7d22\u5f15\u4e3a \\([i, j]\\) \uff0c\u5219\u5411\u4e0b\u6216\u5411\u53f3\u8d70\u4e00\u6b65\u540e\uff0c\u7d22\u5f15\u53d8\u4e3a \\([i+1, j]\\) \u6216 \\([i, j+1]\\) \u3002\u56e0\u6b64\uff0c\u72b6\u6001\u5e94\u5305\u542b\u884c\u7d22\u5f15\u548c\u5217\u7d22\u5f15\u4e24\u4e2a\u53d8\u91cf\uff0c\u8bb0\u4e3a \\([i, j]\\) \u3002

\u72b6\u6001 \\([i, j]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u4e3a\uff1a\u4ece\u8d77\u59cb\u70b9 \\([0, 0]\\) \u8d70\u5230 \\([i, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\uff0c\u89e3\u8bb0\u4e3a \\(dp[i, j]\\) \u3002

\u81f3\u6b64\uff0c\u6211\u4eec\u5c31\u5f97\u5230\u4e86\u56fe 14-11 \u6240\u793a\u7684\u4e8c\u7ef4 \\(dp\\) \u77e9\u9635\uff0c\u5176\u5c3a\u5bf8\u4e0e\u8f93\u5165\u7f51\u683c \\(grid\\) \u76f8\u540c\u3002

\u56fe 14-11 \u00a0 \u72b6\u6001\u5b9a\u4e49\u4e0e dp \u8868

Note

\u52a8\u6001\u89c4\u5212\u548c\u56de\u6eaf\u8fc7\u7a0b\u53ef\u4ee5\u88ab\u63cf\u8ff0\u4e3a\u4e00\u4e2a\u51b3\u7b56\u5e8f\u5217\uff0c\u800c\u72b6\u6001\u7531\u6240\u6709\u51b3\u7b56\u53d8\u91cf\u6784\u6210\u3002\u5b83\u5e94\u5f53\u5305\u542b\u63cf\u8ff0\u89e3\u9898\u8fdb\u5ea6\u7684\u6240\u6709\u53d8\u91cf\uff0c\u5176\u5305\u542b\u4e86\u8db3\u591f\u7684\u4fe1\u606f\uff0c\u80fd\u591f\u7528\u6765\u63a8\u5bfc\u51fa\u4e0b\u4e00\u4e2a\u72b6\u6001\u3002

\u6bcf\u4e2a\u72b6\u6001\u90fd\u5bf9\u5e94\u4e00\u4e2a\u5b50\u95ee\u9898\uff0c\u6211\u4eec\u4f1a\u5b9a\u4e49\u4e00\u4e2a \\(dp\\) \u8868\u6765\u5b58\u50a8\u6240\u6709\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u72b6\u6001\u7684\u6bcf\u4e2a\u72ec\u7acb\u53d8\u91cf\u90fd\u662f \\(dp\\) \u8868\u7684\u4e00\u4e2a\u7ef4\u5ea6\u3002\u672c\u8d28\u4e0a\u770b\uff0c\\(dp\\) \u8868\u662f\u72b6\u6001\u548c\u5b50\u95ee\u9898\u7684\u89e3\u4e4b\u95f4\u7684\u6620\u5c04\u3002

\u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b

\u5bf9\u4e8e\u72b6\u6001 \\([i, j]\\) \uff0c\u5b83\u53ea\u80fd\u4ece\u4e0a\u8fb9\u683c\u5b50 \\([i-1, j]\\) \u548c\u5de6\u8fb9\u683c\u5b50 \\([i, j-1]\\) \u8f6c\u79fb\u800c\u6765\u3002\u56e0\u6b64\u6700\u4f18\u5b50\u7ed3\u6784\u4e3a\uff1a\u5230\u8fbe \\([i, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\u7531 \\([i, j-1]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\u4e0e \\([i-1, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\uff0c\u8fd9\u4e24\u8005\u8f83\u5c0f\u7684\u90a3\u4e00\u4e2a\u51b3\u5b9a\u3002

\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u53ef\u63a8\u51fa\u56fe 14-12 \u6240\u793a\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff1a

\\[ dp[i, j] = \\min(dp[i-1, j], dp[i, j-1]) + grid[i, j] \\]

\u56fe 14-12 \u00a0 \u6700\u4f18\u5b50\u7ed3\u6784\u4e0e\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b

Note

\u6839\u636e\u5b9a\u4e49\u597d\u7684 \\(dp\\) \u8868\uff0c\u601d\u8003\u539f\u95ee\u9898\u548c\u5b50\u95ee\u9898\u7684\u5173\u7cfb\uff0c\u627e\u51fa\u901a\u8fc7\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u6765\u6784\u9020\u539f\u95ee\u9898\u7684\u6700\u4f18\u89e3\u7684\u65b9\u6cd5\uff0c\u5373\u6700\u4f18\u5b50\u7ed3\u6784\u3002

\u4e00\u65e6\u6211\u4eec\u627e\u5230\u4e86\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u6784\u5efa\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u3002

\u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f

\u5728\u672c\u9898\u4e2d\uff0c\u9996\u884c\u7684\u72b6\u6001\u53ea\u80fd\u4ece\u5176\u5de6\u8fb9\u7684\u72b6\u6001\u5f97\u6765\uff0c\u9996\u5217\u7684\u72b6\u6001\u53ea\u80fd\u4ece\u5176\u4e0a\u8fb9\u7684\u72b6\u6001\u5f97\u6765\uff0c\u56e0\u6b64\u9996\u884c \\(i = 0\\) \u548c\u9996\u5217 \\(j = 0\\) \u662f\u8fb9\u754c\u6761\u4ef6\u3002

\u5982\u56fe 14-13 \u6240\u793a\uff0c\u7531\u4e8e\u6bcf\u4e2a\u683c\u5b50\u662f\u7531\u5176\u5de6\u65b9\u683c\u5b50\u548c\u4e0a\u65b9\u683c\u5b50\u8f6c\u79fb\u800c\u6765\uff0c\u56e0\u6b64\u6211\u4eec\u4f7f\u7528\u91c7\u7528\u5faa\u73af\u6765\u904d\u5386\u77e9\u9635\uff0c\u5916\u5faa\u73af\u904d\u5386\u5404\u884c\u3001\u5185\u5faa\u73af\u904d\u5386\u5404\u5217\u3002

\u56fe 14-13 \u00a0 \u8fb9\u754c\u6761\u4ef6\u4e0e\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f

Note

\u8fb9\u754c\u6761\u4ef6\u5728\u52a8\u6001\u89c4\u5212\u4e2d\u7528\u4e8e\u521d\u59cb\u5316 \\(dp\\) \u8868\uff0c\u5728\u641c\u7d22\u4e2d\u7528\u4e8e\u526a\u679d\u3002

\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f\u7684\u6838\u5fc3\u662f\u8981\u4fdd\u8bc1\u5728\u8ba1\u7b97\u5f53\u524d\u95ee\u9898\u7684\u89e3\u65f6\uff0c\u6240\u6709\u5b83\u4f9d\u8d56\u7684\u66f4\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\u90fd\u5df2\u7ecf\u88ab\u6b63\u786e\u5730\u8ba1\u7b97\u51fa\u6765\u3002

\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u6211\u4eec\u5df2\u7ecf\u53ef\u4ee5\u76f4\u63a5\u5199\u51fa\u52a8\u6001\u89c4\u5212\u4ee3\u7801\u3002\u7136\u800c\u5b50\u95ee\u9898\u5206\u89e3\u662f\u4e00\u79cd\u4ece\u9876\u81f3\u5e95\u7684\u601d\u60f3\uff0c\u56e0\u6b64\u6309\u7167\u201c\u66b4\u529b\u641c\u7d22 \\(\\rightarrow\\) \u8bb0\u5fc6\u5316\u641c\u7d22 \\(\\rightarrow\\) \u52a8\u6001\u89c4\u5212\u201d\u7684\u987a\u5e8f\u5b9e\u73b0\u66f4\u52a0\u7b26\u5408\u601d\u7ef4\u4e60\u60ef\u3002

"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1","title":"1. \u00a0 \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u641c\u7d22","text":"

\u4ece\u72b6\u6001 \\([i, j]\\) \u5f00\u59cb\u641c\u7d22\uff0c\u4e0d\u65ad\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u72b6\u6001 \\([i-1, j]\\) \u548c \\([i, j-1]\\) \uff0c\u9012\u5f52\u51fd\u6570\u5305\u62ec\u4ee5\u4e0b\u8981\u7d20\u3002

  • \u9012\u5f52\u53c2\u6570\uff1a\u72b6\u6001 \\([i, j]\\) \u3002
  • \u8fd4\u56de\u503c\uff1a\u4ece \\([0, 0]\\) \u5230 \\([i, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c \\(dp[i, j]\\) \u3002
  • \u7ec8\u6b62\u6761\u4ef6\uff1a\u5f53 \\(i = 0\\) \u4e14 \\(j = 0\\) \u65f6\uff0c\u8fd4\u56de\u4ee3\u4ef7 \\(grid[0, 0]\\) \u3002
  • \u526a\u679d\uff1a\u5f53 \\(i < 0\\) \u65f6\u6216 \\(j < 0\\) \u65f6\u7d22\u5f15\u8d8a\u754c\uff0c\u6b64\u65f6\u8fd4\u56de\u4ee3\u4ef7 \\(+\\infty\\) \uff0c\u4ee3\u8868\u4e0d\u53ef\u884c\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig min_path_sum.py
def min_path_sum_dfs(grid: list[list[int]], i: int, j: int) -> int:\n\"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22\"\"\"\n# \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif i == 0 and j == 0:\nreturn grid[0][0]\n# \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif i < 0 or j < 0:\nreturn inf\n# \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nup = min_path_sum_dfs(grid, i - 1, j)\nleft = min_path_sum_dfs(grid, i, j - 1)\n# \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn min(left, up) + grid[i][j]\n
min_path_sum.cpp
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(vector<vector<int>> &grid, int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 && j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\nreturn INT_MAX;\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint up = minPathSumDFS(grid, i - 1, j);\nint left = minPathSumDFS(grid, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\n}\n
min_path_sum.java
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(int[][] grid, int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 && j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\nreturn Integer.MAX_VALUE;\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint up = minPathSumDFS(grid, i - 1, j);\nint left = minPathSumDFS(grid, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn Math.min(left, up) + grid[i][j];\n}\n
min_path_sum.cs
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint MinPathSumDFS(int[][] grid, int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 && j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\nreturn int.MaxValue;\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint up = MinPathSumDFS(grid, i - 1, j);\nint left = MinPathSumDFS(grid, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn Math.Min(left, up) + grid[i][j];\n}\n
min_path_sum.go
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc minPathSumDFS(grid [][]int, i, j int) int {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif i == 0 && j == 0 {\nreturn grid[0][0]\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif i < 0 || j < 0 {\nreturn math.MaxInt\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nup := minPathSumDFS(grid, i-1, j)\nleft := minPathSumDFS(grid, i, j-1)\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn int(math.Min(float64(left), float64(up))) + grid[i][j]\n}\n
min_path_sum.swift
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc minPathSumDFS(grid: [[Int]], i: Int, j: Int) -> Int {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif i == 0, j == 0 {\nreturn grid[0][0]\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif i < 0 || j < 0 {\nreturn .max\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nlet up = minPathSumDFS(grid: grid, i: i - 1, j: j)\nlet left = minPathSumDFS(grid: grid, i: i, j: j - 1)\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn min(left, up) + grid[i][j]\n}\n
min_path_sum.js
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunction minPathSumDFS(grid, i, j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i === 0 && j === 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\nreturn Infinity;\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nconst up = minPathSumDFS(grid, i - 1, j);\nconst left = minPathSumDFS(grid, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn Math.min(left, up) + grid[i][j];\n}\n
min_path_sum.ts
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunction minPathSumDFS(\ngrid: Array<Array<number>>,\ni: number,\nj: number\n): number {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i === 0 && j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\nreturn Infinity;\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nconst up = minPathSumDFS(grid, i - 1, j);\nconst left = minPathSumDFS(grid, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn Math.min(left, up) + grid[i][j];\n}\n
min_path_sum.dart
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(List<List<int>> grid, int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 && j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\n// \u5728 Dart \u4e2d\uff0cint \u7c7b\u578b\u662f\u56fa\u5b9a\u8303\u56f4\u7684\u6574\u6570\uff0c\u4e0d\u5b58\u5728\u8868\u793a\u201c\u65e0\u7a77\u5927\u201d\u7684\u503c\nreturn BigInt.from(2).pow(31).toInt();\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint up = minPathSumDFS(grid, i - 1, j);\nint left = minPathSumDFS(grid, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn min(left, up) + grid[i][j];\n}\n
min_path_sum.rs
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfn min_path_sum_dfs(grid: &Vec<Vec<i32>>, i: i32, j: i32) -> i32 {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif i == 0 && j == 0 {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif i < 0 || j < 0 {\nreturn i32::MAX;\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nlet up = min_path_sum_dfs(grid, i - 1, j);\nlet left = min_path_sum_dfs(grid, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nstd::cmp::min(left, up) + grid[i as usize][j as usize]\n}\n
min_path_sum.c
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(int gridCols, int grid[][gridCols], int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 && j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\nreturn INT_MAX;\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint up = minPathSumDFS(gridCols, grid, i - 1, j);\nint left = minPathSumDFS(gridCols, grid, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\n}\n
min_path_sum.zig
// \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22\nfn minPathSumDFS(grid: anytype, i: i32, j: i32) i32 {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 and j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 or j < 0) {\nreturn std.math.maxInt(i32);\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nvar up = minPathSumDFS(grid, i - 1, j);\nvar left = minPathSumDFS(grid, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn @min(left, up) + grid[@as(usize, @intCast(i))][@as(usize, @intCast(j))];\n}\n

\u56fe 14-14 \u7ed9\u51fa\u4e86\u4ee5 \\(dp[2, 1]\\) \u4e3a\u6839\u8282\u70b9\u7684\u9012\u5f52\u6811\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e9b\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u5176\u6570\u91cf\u4f1a\u968f\u7740\u7f51\u683c grid \u7684\u5c3a\u5bf8\u53d8\u5927\u800c\u6025\u5267\u589e\u591a\u3002

\u672c\u8d28\u4e0a\u770b\uff0c\u9020\u6210\u91cd\u53e0\u5b50\u95ee\u9898\u7684\u539f\u56e0\u4e3a\uff1a\u5b58\u5728\u591a\u6761\u8def\u5f84\u53ef\u4ee5\u4ece\u5de6\u4e0a\u89d2\u5230\u8fbe\u67d0\u4e00\u5355\u5143\u683c\u3002

\u56fe 14-14 \u00a0 \u66b4\u529b\u641c\u7d22\u9012\u5f52\u6811

\u6bcf\u4e2a\u72b6\u6001\u90fd\u6709\u5411\u4e0b\u548c\u5411\u53f3\u4e24\u79cd\u9009\u62e9\uff0c\u4ece\u5de6\u4e0a\u89d2\u8d70\u5230\u53f3\u4e0b\u89d2\u603b\u5171\u9700\u8981 \\(m + n - 2\\) \u6b65\uff0c\u6240\u4ee5\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^{m + n})\\) \u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u79cd\u8ba1\u7b97\u65b9\u5f0f\u672a\u8003\u8651\u4e34\u8fd1\u7f51\u683c\u8fb9\u754c\u7684\u60c5\u51b5\uff0c\u5f53\u5230\u8fbe\u7f51\u7edc\u8fb9\u754c\u65f6\u53ea\u5269\u4e0b\u4e00\u79cd\u9009\u62e9\u3002\u56e0\u6b64\u5b9e\u9645\u7684\u8def\u5f84\u6570\u91cf\u4f1a\u5c11\u4e00\u4e9b\u3002

"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#2","title":"2. \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22","text":"

\u6211\u4eec\u5f15\u5165\u4e00\u4e2a\u548c\u7f51\u683c grid \u76f8\u540c\u5c3a\u5bf8\u7684\u8bb0\u5fc6\u5217\u8868 mem \uff0c\u7528\u4e8e\u8bb0\u5f55\u5404\u4e2a\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5e76\u5c06\u91cd\u53e0\u5b50\u95ee\u9898\u8fdb\u884c\u526a\u679d\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig min_path_sum.py
def min_path_sum_dfs_mem(\ngrid: list[list[int]], mem: list[list[int]], i: int, j: int\n) -> int:\n\"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n# \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif i == 0 and j == 0:\nreturn grid[0][0]\n# \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif i < 0 or j < 0:\nreturn inf\n# \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif mem[i][j] != -1:\nreturn mem[i][j]\n# \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nup = min_path_sum_dfs_mem(grid, mem, i - 1, j)\nleft = min_path_sum_dfs_mem(grid, mem, i, j - 1)\n# \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = min(left, up) + grid[i][j]\nreturn mem[i][j]\n
min_path_sum.cpp
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(vector<vector<int>> &grid, vector<vector<int>> &mem, int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 && j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\nreturn INT_MAX;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][j] != -1) {\nreturn mem[i][j];\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint up = minPathSumDFSMem(grid, mem, i - 1, j);\nint left = minPathSumDFSMem(grid, mem, i, j - 1);\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\nreturn mem[i][j];\n}\n
min_path_sum.java
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 && j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\nreturn Integer.MAX_VALUE;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][j] != -1) {\nreturn mem[i][j];\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint up = minPathSumDFSMem(grid, mem, i - 1, j);\nint left = minPathSumDFSMem(grid, mem, i, j - 1);\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = Math.min(left, up) + grid[i][j];\nreturn mem[i][j];\n}\n
min_path_sum.cs
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint MinPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 && j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\nreturn int.MaxValue;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][j] != -1) {\nreturn mem[i][j];\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint up = MinPathSumDFSMem(grid, mem, i - 1, j);\nint left = MinPathSumDFSMem(grid, mem, i, j - 1);\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = Math.Min(left, up) + grid[i][j];\nreturn mem[i][j];\n}\n
min_path_sum.go
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc minPathSumDFSMem(grid, mem [][]int, i, j int) int {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif i == 0 && j == 0 {\nreturn grid[0][0]\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif i < 0 || j < 0 {\nreturn math.MaxInt\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif mem[i][j] != -1 {\nreturn mem[i][j]\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nup := minPathSumDFSMem(grid, mem, i-1, j)\nleft := minPathSumDFSMem(grid, mem, i, j-1)\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = int(math.Min(float64(left), float64(up))) + grid[i][j]\nreturn mem[i][j]\n}\n
min_path_sum.swift
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc minPathSumDFSMem(grid: [[Int]], mem: inout [[Int]], i: Int, j: Int) -> Int {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif i == 0, j == 0 {\nreturn grid[0][0]\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif i < 0 || j < 0 {\nreturn .max\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif mem[i][j] != -1 {\nreturn mem[i][j]\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nlet up = minPathSumDFSMem(grid: grid, mem: &mem, i: i - 1, j: j)\nlet left = minPathSumDFSMem(grid: grid, mem: &mem, i: i, j: j - 1)\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = min(left, up) + grid[i][j]\nreturn mem[i][j]\n}\n
min_path_sum.js
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction minPathSumDFSMem(grid, mem, i, j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i === 0 && j === 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\nreturn Infinity;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][j] !== -1) {\nreturn mem[i][j];\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nconst up = minPathSumDFSMem(grid, mem, i - 1, j);\nconst left = minPathSumDFSMem(grid, mem, i, j - 1);\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = Math.min(left, up) + grid[i][j];\nreturn mem[i][j];\n}\n
min_path_sum.ts
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction minPathSumDFSMem(\ngrid: Array<Array<number>>,\nmem: Array<Array<number>>,\ni: number,\nj: number\n): number {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i === 0 && j === 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\nreturn Infinity;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][j] != -1) {\nreturn mem[i][j];\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nconst up = minPathSumDFSMem(grid, mem, i - 1, j);\nconst left = minPathSumDFSMem(grid, mem, i, j - 1);\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = Math.min(left, up) + grid[i][j];\nreturn mem[i][j];\n}\n
min_path_sum.dart
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(List<List<int>> grid, List<List<int>> mem, int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 && j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\n// \u5728 Dart \u4e2d\uff0cint \u7c7b\u578b\u662f\u56fa\u5b9a\u8303\u56f4\u7684\u6574\u6570\uff0c\u4e0d\u5b58\u5728\u8868\u793a\u201c\u65e0\u7a77\u5927\u201d\u7684\u503c\nreturn BigInt.from(2).pow(31).toInt();\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][j] != -1) {\nreturn mem[i][j];\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint up = minPathSumDFSMem(grid, mem, i - 1, j);\nint left = minPathSumDFSMem(grid, mem, i, j - 1);\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = min(left, up) + grid[i][j];\nreturn mem[i][j];\n}\n
min_path_sum.rs
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn min_path_sum_dfs_mem(grid: &Vec<Vec<i32>>, mem: &mut Vec<Vec<i32>>, i: i32, j: i32) -> i32 {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif i == 0 && j == 0 {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif i < 0 || j < 0 {\nreturn i32::MAX;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif mem[i as usize][j as usize] != -1 {\nreturn mem[i as usize][j as usize];\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nlet up = min_path_sum_dfs_mem(grid, mem, i - 1, j);\nlet left = min_path_sum_dfs_mem(grid, mem, i, j - 1);\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i as usize][j as usize] = std::cmp::min(left, up) + grid[i as usize][j as usize];\nmem[i as usize][j as usize]\n}\n
min_path_sum.c
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(int gridCols, int grid[][gridCols], int mem[][gridCols], int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 && j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\nreturn INT_MAX;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][j] != -1) {\nreturn mem[i][j];\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint up = minPathSumDFSMem(gridCols, grid, mem, i - 1, j);\nint left = minPathSumDFSMem(gridCols, grid, mem, i, j - 1);\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\nreturn mem[i][j];\n}\n
min_path_sum.zig
// \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\nfn minPathSumDFSMem(grid: anytype, mem: anytype, i: i32, j: i32) i32 {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 and j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 or j < 0) {\nreturn std.math.maxInt(i32);\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))] != -1) {\nreturn mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))];\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nvar up = minPathSumDFSMem(grid, mem, i - 1, j);\nvar left = minPathSumDFSMem(grid, mem, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[@as(usize, @intCast(i))][@as(usize, @intCast(j))] = @min(left, up) + grid[@as(usize, @intCast(i))][@as(usize, @intCast(j))];\nreturn mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))];\n}\n

\u5982\u56fe 14-15 \u6240\u793a\uff0c\u5728\u5f15\u5165\u8bb0\u5fc6\u5316\u540e\uff0c\u6240\u6709\u5b50\u95ee\u9898\u7684\u89e3\u53ea\u9700\u8ba1\u7b97\u4e00\u6b21\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u72b6\u6001\u603b\u6570\uff0c\u5373\u7f51\u683c\u5c3a\u5bf8 \\(O(nm)\\) \u3002

\u56fe 14-15 \u00a0 \u8bb0\u5fc6\u5316\u641c\u7d22\u9012\u5f52\u6811

"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#3","title":"3. \u00a0 \u65b9\u6cd5\u4e09\uff1a\u52a8\u6001\u89c4\u5212","text":"

\u57fa\u4e8e\u8fed\u4ee3\u5b9e\u73b0\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig min_path_sum.py
def min_path_sum_dp(grid: list[list[int]]) -> int:\n\"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nn, m = len(grid), len(grid[0])\n# \u521d\u59cb\u5316 dp \u8868\ndp = [[0] * m for _ in range(n)]\ndp[0][0] = grid[0][0]\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor j in range(1, m):\ndp[0][j] = dp[0][j - 1] + grid[0][j]\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor i in range(1, n):\ndp[i][0] = dp[i - 1][0] + grid[i][0]\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in range(1, n):\nfor j in range(1, m):\ndp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\nreturn dp[n - 1][m - 1]\n
min_path_sum.cpp
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(vector<vector<int>> &grid) {\nint n = grid.size(), m = grid[0].size();\n// \u521d\u59cb\u5316 dp \u8868\nvector<vector<int>> dp(n, vector<int>(m));\ndp[0][0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j < m; j++) {\ndp[0][j] = dp[0][j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor (int i = 1; i < n; i++) {\ndp[i][0] = dp[i - 1][0] + grid[i][0];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i < n; i++) {\nfor (int j = 1; j < m; j++) {\ndp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n}\n}\nreturn dp[n - 1][m - 1];\n}\n
min_path_sum.java
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(int[][] grid) {\nint n = grid.length, m = grid[0].length;\n// \u521d\u59cb\u5316 dp \u8868\nint[][] dp = new int[n][m];\ndp[0][0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j < m; j++) {\ndp[0][j] = dp[0][j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor (int i = 1; i < n; i++) {\ndp[i][0] = dp[i - 1][0] + grid[i][0];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i < n; i++) {\nfor (int j = 1; j < m; j++) {\ndp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n}\n}\nreturn dp[n - 1][m - 1];\n}\n
min_path_sum.cs
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint MinPathSumDP(int[][] grid) {\nint n = grid.Length, m = grid[0].Length;\n// \u521d\u59cb\u5316 dp \u8868\nint[,] dp = new int[n, m];\ndp[0, 0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j < m; j++) {\ndp[0, j] = dp[0, j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor (int i = 1; i < n; i++) {\ndp[i, 0] = dp[i - 1, 0] + grid[i][0];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i < n; i++) {\nfor (int j = 1; j < m; j++) {\ndp[i, j] = Math.Min(dp[i, j - 1], dp[i - 1, j]) + grid[i][j];\n}\n}\nreturn dp[n - 1, m - 1];\n}\n
min_path_sum.go
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDP(grid [][]int) int {\nn, m := len(grid), len(grid[0])\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([][]int, n)\nfor i := 0; i < n; i++ {\ndp[i] = make([]int, m)\n}\ndp[0][0] = grid[0][0]\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor j := 1; j < m; j++ {\ndp[0][j] = dp[0][j-1] + grid[0][j]\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor i := 1; i < n; i++ {\ndp[i][0] = dp[i-1][0] + grid[i][0]\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i := 1; i < n; i++ {\nfor j := 1; j < m; j++ {\ndp[i][j] = int(math.Min(float64(dp[i][j-1]), float64(dp[i-1][j]))) + grid[i][j]\n}\n}\nreturn dp[n-1][m-1]\n}\n
min_path_sum.swift
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDP(grid: [[Int]]) -> Int {\nlet n = grid.count\nlet m = grid[0].count\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: Array(repeating: 0, count: m), count: n)\ndp[0][0] = grid[0][0]\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor j in stride(from: 1, to: m, by: 1) {\ndp[0][j] = dp[0][j - 1] + grid[0][j]\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor i in stride(from: 1, to: n, by: 1) {\ndp[i][0] = dp[i - 1][0] + grid[i][0]\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in stride(from: 1, to: n, by: 1) {\nfor j in stride(from: 1, to: m, by: 1) {\ndp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n}\n}\nreturn dp[n - 1][m - 1]\n}\n
min_path_sum.js
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction minPathSumDP(grid) {\nconst n = grid.length,\nm = grid[0].length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: n }, () =>\nArray.from({ length: m }, () => 0)\n);\ndp[0][0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (let j = 1; j < m; j++) {\ndp[0][j] = dp[0][j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor (let i = 1; i < n; i++) {\ndp[i][0] = dp[i - 1][0] + grid[i][0];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (let i = 1; i < n; i++) {\nfor (let j = 1; j < m; j++) {\ndp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n}\n}\nreturn dp[n - 1][m - 1];\n}\n
min_path_sum.ts
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction minPathSumDP(grid: Array<Array<number>>): number {\nconst n = grid.length,\nm = grid[0].length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: n }, () =>\nArray.from({ length: m }, () => 0)\n);\ndp[0][0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (let j = 1; j < m; j++) {\ndp[0][j] = dp[0][j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor (let i = 1; i < n; i++) {\ndp[i][0] = dp[i - 1][0] + grid[i][0];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (let i = 1; i < n; i++) {\nfor (let j: number = 1; j < m; j++) {\ndp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n}\n}\nreturn dp[n - 1][m - 1];\n}\n
min_path_sum.dart
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(List<List<int>> grid) {\nint n = grid.length, m = grid[0].length;\n// \u521d\u59cb\u5316 dp \u8868\nList<List<int>> dp = List.generate(n, (i) => List.filled(m, 0));\ndp[0][0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j < m; j++) {\ndp[0][j] = dp[0][j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor (int i = 1; i < n; i++) {\ndp[i][0] = dp[i - 1][0] + grid[i][0];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i < n; i++) {\nfor (int j = 1; j < m; j++) {\ndp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n}\n}\nreturn dp[n - 1][m - 1];\n}\n
min_path_sum.rs
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfn min_path_sum_dp(grid: &Vec<Vec<i32>>) -> i32 {\nlet (n, m) = (grid.len(), grid[0].len());\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![vec![0; m]; n];\ndp[0][0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor j in 1..m {\ndp[0][j] = dp[0][j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor i in 1..n {\ndp[i][0] = dp[i - 1][0] + grid[i][0];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in 1..n {\nfor j in 1..m {\ndp[i][j] = std::cmp::min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n}\n}\ndp[n - 1][m - 1]\n}\n
min_path_sum.c
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(int gridCols, int grid[][gridCols], int n, int m) {\n// \u521d\u59cb\u5316 dp \u8868\nint dp[n][m];\ndp[0][0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j < m; j++) {\ndp[0][j] = dp[0][j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor (int i = 1; i < n; i++) {\ndp[i][0] = dp[i - 1][0] + grid[i][0];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i < n; i++) {\nfor (int j = 1; j < m; j++) {\ndp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n}\n}\nreturn dp[n - 1][m - 1];\n}\n
min_path_sum.zig
// \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212\nfn minPathSumDP(comptime grid: anytype) i32 {\ncomptime var n = grid.len;\ncomptime var m = grid[0].len;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_][m]i32{[_]i32{0} ** m} ** n;\ndp[0][0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (1..m) |j| {\ndp[0][j] = dp[0][j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor (1..n) |i| {\ndp[i][0] = dp[i - 1][0] + grid[i][0];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (1..n) |i| {\nfor (1..m) |j| {\ndp[i][j] = @min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n}\n}\nreturn dp[n - 1][m - 1];\n}\n

\u56fe 14-16 \u5c55\u793a\u4e86\u6700\u5c0f\u8def\u5f84\u548c\u7684\u72b6\u6001\u8f6c\u79fb\u8fc7\u7a0b\uff0c\u5176\u904d\u5386\u4e86\u6574\u4e2a\u7f51\u683c\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nm)\\) \u3002

\u6570\u7ec4 dp \u5927\u5c0f\u4e3a \\(n \\times m\\) \uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nm)\\) \u3002

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

\u56fe 14-16 \u00a0 \u6700\u5c0f\u8def\u5f84\u548c\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#4","title":"4. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

\u7531\u4e8e\u6bcf\u4e2a\u683c\u5b50\u53ea\u4e0e\u5176\u5de6\u8fb9\u548c\u4e0a\u8fb9\u7684\u683c\u5b50\u6709\u5173\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u53ea\u7528\u4e00\u4e2a\u5355\u884c\u6570\u7ec4\u6765\u5b9e\u73b0 \\(dp\\) \u8868\u3002

\u8bf7\u6ce8\u610f\uff0c\u56e0\u4e3a\u6570\u7ec4 dp \u53ea\u80fd\u8868\u793a\u4e00\u884c\u7684\u72b6\u6001\uff0c\u6240\u4ee5\u6211\u4eec\u65e0\u6cd5\u63d0\u524d\u521d\u59cb\u5316\u9996\u5217\u72b6\u6001\uff0c\u800c\u662f\u5728\u904d\u5386\u6bcf\u884c\u4e2d\u66f4\u65b0\u5b83\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig min_path_sum.py
def min_path_sum_dp_comp(grid: list[list[int]]) -> int:\n\"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\nn, m = len(grid), len(grid[0])\n# \u521d\u59cb\u5316 dp \u8868\ndp = [0] * m\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0]\nfor j in range(1, m):\ndp[j] = dp[j - 1] + grid[0][j]\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor i in range(1, n):\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0]\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor j in range(1, m):\ndp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\nreturn dp[m - 1]\n
min_path_sum.cpp
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(vector<vector<int>> &grid) {\nint n = grid.size(), m = grid[0].size();\n// \u521d\u59cb\u5316 dp \u8868\nvector<int> dp(m);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0];\nfor (int j = 1; j < m; j++) {\ndp[j] = dp[j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i < n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j < m; j++) {\ndp[j] = min(dp[j - 1], dp[j]) + grid[i][j];\n}\n}\nreturn dp[m - 1];\n}\n
min_path_sum.java
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(int[][] grid) {\nint n = grid.length, m = grid[0].length;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[m];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0];\nfor (int j = 1; j < m; j++) {\ndp[j] = dp[j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i < n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j < m; j++) {\ndp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n}\n}\nreturn dp[m - 1];\n}\n
min_path_sum.cs
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint MinPathSumDPComp(int[][] grid) {\nint n = grid.Length, m = grid[0].Length;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[m];\ndp[0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j < m; j++) {\ndp[j] = dp[j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i < n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j < m; j++) {\ndp[j] = Math.Min(dp[j - 1], dp[j]) + grid[i][j];\n}\n}\nreturn dp[m - 1];\n}\n
min_path_sum.go
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDPComp(grid [][]int) int {\nn, m := len(grid), len(grid[0])\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([]int, m)\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0]\nfor j := 1; j < m; j++ {\ndp[j] = dp[j-1] + grid[0][j]\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i := 1; i < n; i++ {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0]\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor j := 1; j < m; j++ {\ndp[j] = int(math.Min(float64(dp[j-1]), float64(dp[j]))) + grid[i][j]\n}\n}\nreturn dp[m-1]\n}\n
min_path_sum.swift
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDPComp(grid: [[Int]]) -> Int {\nlet n = grid.count\nlet m = grid[0].count\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: 0, count: m)\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0]\nfor j in stride(from: 1, to: m, by: 1) {\ndp[j] = dp[j - 1] + grid[0][j]\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor i in stride(from: 1, to: n, by: 1) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0]\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor j in stride(from: 1, to: m, by: 1) {\ndp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n}\n}\nreturn dp[m - 1]\n}\n
min_path_sum.js
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction minPathSumDPComp(grid) {\nconst n = grid.length,\nm = grid[0].length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = new Array(m);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0];\nfor (let j = 1; j < m; j++) {\ndp[j] = dp[j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (let i = 1; i < n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (let j = 1; j < m; j++) {\ndp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n}\n}\nreturn dp[m - 1];\n}\n
min_path_sum.ts
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction minPathSumDPComp(grid: Array<Array<number>>): number {\nconst n = grid.length,\nm = grid[0].length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = new Array(m);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0];\nfor (let j = 1; j < m; j++) {\ndp[j] = dp[j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (let i = 1; i < n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (let j = 1; j < m; j++) {\ndp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n}\n}\nreturn dp[m - 1];\n}\n
min_path_sum.dart
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(List<List<int>> grid) {\nint n = grid.length, m = grid[0].length;\n// \u521d\u59cb\u5316 dp \u8868\nList<int> dp = List.filled(m, 0);\ndp[0] = grid[0][0];\nfor (int j = 1; j < m; j++) {\ndp[j] = dp[j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i < n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j < m; j++) {\ndp[j] = min(dp[j - 1], dp[j]) + grid[i][j];\n}\n}\nreturn dp[m - 1];\n}\n
min_path_sum.rs
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn min_path_sum_dp_comp(grid: &Vec<Vec<i32>>) -> i32 {\nlet (n, m) = (grid.len(), grid[0].len());\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![0; m];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0];\nfor j in 1..m {\ndp[j] = dp[j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor i in 1..n {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor j in 1..m {\ndp[j] = std::cmp::min(dp[j - 1], dp[j]) + grid[i][j];\n}\n}\ndp[m - 1]\n}\n
min_path_sum.c
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(int gridCols, int grid[][gridCols], int n, int m) {\n// \u521d\u59cb\u5316 dp \u8868\nint dp[m];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0];\nfor (int j = 1; j < m; j++) {\ndp[j] = dp[j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i < n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j < m; j++) {\ndp[j] = min(dp[j - 1], dp[j]) + grid[i][j];\n}\n}\nreturn dp[m - 1];\n}\n
min_path_sum.zig
// \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn minPathSumDPComp(comptime grid: anytype) i32 {\ncomptime var n = grid.len;\ncomptime var m = grid[0].len;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_]i32{0} ** m;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0];\nfor (1..m) |j| {\ndp[j] = dp[j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (1..n) |i| {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0];\nfor (1..m) |j| {\ndp[j] = @min(dp[j - 1], dp[j]) + grid[i][j];\n}\n}\nreturn dp[m - 1];\n}\n
"},{"location":"chapter_dynamic_programming/edit_distance_problem/","title":"14.6 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898","text":"

\u7f16\u8f91\u8ddd\u79bb\uff0c\u4e5f\u88ab\u79f0\u4e3a Levenshtein \u8ddd\u79bb\uff0c\u6307\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e4b\u95f4\u4e92\u76f8\u8f6c\u6362\u7684\u6700\u5c0f\u4fee\u6539\u6b21\u6570\uff0c\u901a\u5e38\u7528\u4e8e\u5728\u4fe1\u606f\u68c0\u7d22\u548c\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4e2d\u5ea6\u91cf\u4e24\u4e2a\u5e8f\u5217\u7684\u76f8\u4f3c\u5ea6\u3002

Question

\u8f93\u5165\u4e24\u4e2a\u5b57\u7b26\u4e32 \\(s\\) \u548c \\(t\\) \uff0c\u8fd4\u56de\u5c06 \\(s\\) \u8f6c\u6362\u4e3a \\(t\\) \u6240\u9700\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u3002

\u4f60\u53ef\u4ee5\u5728\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u8fdb\u884c\u4e09\u79cd\u7f16\u8f91\u64cd\u4f5c\uff1a\u63d2\u5165\u4e00\u4e2a\u5b57\u7b26\u3001\u5220\u9664\u4e00\u4e2a\u5b57\u7b26\u3001\u66ff\u6362\u5b57\u7b26\u4e3a\u4efb\u610f\u4e00\u4e2a\u5b57\u7b26\u3002

\u5982\u56fe 14-27 \u6240\u793a\uff0c\u5c06 kitten \u8f6c\u6362\u4e3a sitting \u9700\u8981\u7f16\u8f91 3 \u6b65\uff0c\u5305\u62ec 2 \u6b21\u66ff\u6362\u64cd\u4f5c\u4e0e 1 \u6b21\u6dfb\u52a0\u64cd\u4f5c\uff1b\u5c06 hello \u8f6c\u6362\u4e3a algo \u9700\u8981 3 \u6b65\uff0c\u5305\u62ec 2 \u6b21\u66ff\u6362\u64cd\u4f5c\u548c 1 \u6b21\u5220\u9664\u64cd\u4f5c\u3002

\u56fe 14-27 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u7684\u793a\u4f8b\u6570\u636e

\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898\u53ef\u4ee5\u5f88\u81ea\u7136\u5730\u7528\u51b3\u7b56\u6811\u6a21\u578b\u6765\u89e3\u91ca\u3002\u5b57\u7b26\u4e32\u5bf9\u5e94\u6811\u8282\u70b9\uff0c\u4e00\u8f6e\u51b3\u7b56\uff08\u4e00\u6b21\u7f16\u8f91\u64cd\u4f5c\uff09\u5bf9\u5e94\u6811\u7684\u4e00\u6761\u8fb9\u3002

\u5982\u56fe 14-28 \u6240\u793a\uff0c\u5728\u4e0d\u9650\u5236\u64cd\u4f5c\u7684\u60c5\u51b5\u4e0b\uff0c\u6bcf\u4e2a\u8282\u70b9\u90fd\u53ef\u4ee5\u6d3e\u751f\u51fa\u8bb8\u591a\u6761\u8fb9\uff0c\u6bcf\u6761\u8fb9\u5bf9\u5e94\u4e00\u79cd\u64cd\u4f5c\uff0c\u8fd9\u610f\u5473\u7740\u4ece hello \u8f6c\u6362\u5230 algo \u6709\u8bb8\u591a\u79cd\u53ef\u80fd\u7684\u8def\u5f84\u3002

\u4ece\u51b3\u7b56\u6811\u7684\u89d2\u5ea6\u770b\uff0c\u672c\u9898\u7684\u76ee\u6807\u662f\u6c42\u89e3\u8282\u70b9 hello \u548c\u8282\u70b9 algo \u4e4b\u95f4\u7684\u6700\u77ed\u8def\u5f84\u3002

\u56fe 14-28 \u00a0 \u57fa\u4e8e\u51b3\u7b56\u6811\u6a21\u578b\u8868\u793a\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898

"},{"location":"chapter_dynamic_programming/edit_distance_problem/#1","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"

\u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868

\u6bcf\u4e00\u8f6e\u7684\u51b3\u7b56\u662f\u5bf9\u5b57\u7b26\u4e32 \\(s\\) \u8fdb\u884c\u4e00\u6b21\u7f16\u8f91\u64cd\u4f5c\u3002

\u6211\u4eec\u5e0c\u671b\u5728\u7f16\u8f91\u64cd\u4f5c\u7684\u8fc7\u7a0b\u4e2d\uff0c\u95ee\u9898\u7684\u89c4\u6a21\u9010\u6e10\u7f29\u5c0f\uff0c\u8fd9\u6837\u624d\u80fd\u6784\u5efa\u5b50\u95ee\u9898\u3002\u8bbe\u5b57\u7b26\u4e32 \\(s\\) \u548c \\(t\\) \u7684\u957f\u5ea6\u5206\u522b\u4e3a \\(n\\) \u548c \\(m\\) \uff0c\u6211\u4eec\u5148\u8003\u8651\u4e24\u5b57\u7b26\u4e32\u5c3e\u90e8\u7684\u5b57\u7b26 \\(s[n-1]\\) \u548c \\(t[m-1]\\) \u3002

  • \u82e5 \\(s[n-1]\\) \u548c \\(t[m-1]\\) \u76f8\u540c\uff0c\u6211\u4eec\u53ef\u4ee5\u8df3\u8fc7\u5b83\u4eec\uff0c\u76f4\u63a5\u8003\u8651 \\(s[n-2]\\) \u548c \\(t[m-2]\\) \u3002
  • \u82e5 \\(s[n-1]\\) \u548c \\(t[m-1]\\) \u4e0d\u540c\uff0c\u6211\u4eec\u9700\u8981\u5bf9 \\(s\\) \u8fdb\u884c\u4e00\u6b21\u7f16\u8f91\uff08\u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\uff09\uff0c\u4f7f\u5f97\u4e24\u5b57\u7b26\u4e32\u5c3e\u90e8\u7684\u5b57\u7b26\u76f8\u540c\uff0c\u4ece\u800c\u53ef\u4ee5\u8df3\u8fc7\u5b83\u4eec\uff0c\u8003\u8651\u89c4\u6a21\u66f4\u5c0f\u7684\u95ee\u9898\u3002

\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u5728\u5b57\u7b26\u4e32 \\(s\\) \u4e2d\u8fdb\u884c\u7684\u6bcf\u4e00\u8f6e\u51b3\u7b56\uff08\u7f16\u8f91\u64cd\u4f5c\uff09\uff0c\u90fd\u4f1a\u4f7f\u5f97 \\(s\\) \u548c \\(t\\) \u4e2d\u5269\u4f59\u7684\u5f85\u5339\u914d\u5b57\u7b26\u53d1\u751f\u53d8\u5316\u3002\u56e0\u6b64\uff0c\u72b6\u6001\u4e3a\u5f53\u524d\u5728 \\(s\\) \u548c \\(t\\) \u4e2d\u8003\u8651\u7684\u7b2c \\(i\\) \u548c \\(j\\) \u4e2a\u5b57\u7b26\uff0c\u8bb0\u4e3a \\([i, j]\\) \u3002

\u72b6\u6001 \\([i, j]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\uff1a\u5c06 \\(s\\) \u7684\u524d \\(i\\) \u4e2a\u5b57\u7b26\u66f4\u6539\u4e3a \\(t\\) \u7684\u524d \\(j\\) \u4e2a\u5b57\u7b26\u6240\u9700\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u3002

\u81f3\u6b64\uff0c\u5f97\u5230\u4e00\u4e2a\u5c3a\u5bf8\u4e3a \\((i+1) \\times (j+1)\\) \u7684\u4e8c\u7ef4 \\(dp\\) \u8868\u3002

\u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b

\u8003\u8651\u5b50\u95ee\u9898 \\(dp[i, j]\\) \uff0c\u5176\u5bf9\u5e94\u7684\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u5c3e\u90e8\u5b57\u7b26\u4e3a \\(s[i-1]\\) \u548c \\(t[j-1]\\) \uff0c\u53ef\u6839\u636e\u4e0d\u540c\u7f16\u8f91\u64cd\u4f5c\u5206\u4e3a\u56fe 14-29 \u6240\u793a\u7684\u4e09\u79cd\u60c5\u51b5\u3002

  1. \u5728 \\(s[i-1]\\) \u4e4b\u540e\u6dfb\u52a0 \\(t[j-1]\\) \uff0c\u5219\u5269\u4f59\u5b50\u95ee\u9898 \\(dp[i, j-1]\\) \u3002
  2. \u5220\u9664 \\(s[i-1]\\) \uff0c\u5219\u5269\u4f59\u5b50\u95ee\u9898 \\(dp[i-1, j]\\) \u3002
  3. \u5c06 \\(s[i-1]\\) \u66ff\u6362\u4e3a \\(t[j-1]\\) \uff0c\u5219\u5269\u4f59\u5b50\u95ee\u9898 \\(dp[i-1, j-1]\\) \u3002

\u56fe 14-29 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u7684\u72b6\u6001\u8f6c\u79fb

\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u53ef\u5f97\u6700\u4f18\u5b50\u7ed3\u6784\uff1a\\(dp[i, j]\\) \u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u7b49\u4e8e \\(dp[i, j-1]\\)\u3001\\(dp[i-1, j]\\)\u3001\\(dp[i-1, j-1]\\) \u4e09\u8005\u4e2d\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\uff0c\u518d\u52a0\u4e0a\u672c\u6b21\u7684\u7f16\u8f91\u6b65\u6570 \\(1\\) \u3002\u5bf9\u5e94\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a

\\[ dp[i, j] = \\min(dp[i, j-1], dp[i-1, j], dp[i-1, j-1]) + 1 \\]

\u8bf7\u6ce8\u610f\uff0c\u5f53 \\(s[i-1]\\) \u548c \\(t[j-1]\\) \u76f8\u540c\u65f6\uff0c\u65e0\u987b\u7f16\u8f91\u5f53\u524d\u5b57\u7b26\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a

\\[ dp[i, j] = dp[i-1, j-1] \\]

\u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f

\u5f53\u4e24\u5b57\u7b26\u4e32\u90fd\u4e3a\u7a7a\u65f6\uff0c\u7f16\u8f91\u6b65\u6570\u4e3a \\(0\\) \uff0c\u5373 \\(dp[0, 0] = 0\\) \u3002\u5f53 \\(s\\) \u4e3a\u7a7a\u4f46 \\(t\\) \u4e0d\u4e3a\u7a7a\u65f6\uff0c\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u7b49\u4e8e \\(t\\) \u7684\u957f\u5ea6\uff0c\u5373\u9996\u884c \\(dp[0, j] = j\\) \u3002\u5f53 \\(s\\) \u4e0d\u4e3a\u7a7a\u4f46 \\(t\\) \u4e3a\u7a7a\u65f6\uff0c\u7b49\u4e8e \\(s\\) \u7684\u957f\u5ea6\uff0c\u5373\u9996\u5217 \\(dp[i, 0] = i\\) \u3002

\u89c2\u5bdf\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff0c\u89e3 \\(dp[i, j]\\) \u4f9d\u8d56\u5de6\u65b9\u3001\u4e0a\u65b9\u3001\u5de6\u4e0a\u65b9\u7684\u89e3\uff0c\u56e0\u6b64\u901a\u8fc7\u4e24\u5c42\u5faa\u73af\u6b63\u5e8f\u904d\u5386\u6574\u4e2a \\(dp\\) \u8868\u5373\u53ef\u3002

"},{"location":"chapter_dynamic_programming/edit_distance_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"PythonC++JavaC#GoSwiftJSTSDartRustCZig edit_distance.py
def edit_distance_dp(s: str, t: str) -> int:\n\"\"\"\u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nn, m = len(s), len(t)\ndp = [[0] * (m + 1) for _ in range(n + 1)]\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor i in range(1, n + 1):\ndp[i][0] = i\nfor j in range(1, m + 1):\ndp[0][j] = j\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in range(1, n + 1):\nfor j in range(1, m + 1):\nif s[i - 1] == t[j - 1]:\n# \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1]\nelse:\n# \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1\nreturn dp[n][m]\n
edit_distance.cpp
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(string s, string t) {\nint n = s.length(), m = t.length();\nvector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int i = 1; i <= n; i++) {\ndp[i][0] = i;\n}\nfor (int j = 1; j <= m; j++) {\ndp[0][j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i <= n; i++) {\nfor (int j = 1; j <= m; j++) {\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1];\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n}\n}\n}\nreturn dp[n][m];\n}\n
edit_distance.java
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(String s, String t) {\nint n = s.length(), m = t.length();\nint[][] dp = new int[n + 1][m + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int i = 1; i <= n; i++) {\ndp[i][0] = i;\n}\nfor (int j = 1; j <= m; j++) {\ndp[0][j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i <= n; i++) {\nfor (int j = 1; j <= m; j++) {\nif (s.charAt(i - 1) == t.charAt(j - 1)) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1];\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n}\n}\n}\nreturn dp[n][m];\n}\n
edit_distance.cs
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint EditDistanceDP(string s, string t) {\nint n = s.Length, m = t.Length;\nint[,] dp = new int[n + 1, m + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int i = 1; i <= n; i++) {\ndp[i, 0] = i;\n}\nfor (int j = 1; j <= m; j++) {\ndp[0, j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i <= n; i++) {\nfor (int j = 1; j <= m; j++) {\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i, j] = dp[i - 1, j - 1];\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i, j] = Math.Min(Math.Min(dp[i, j - 1], dp[i - 1, j]), dp[i - 1, j - 1]) + 1;\n}\n}\n}\nreturn dp[n, m];\n}\n
edit_distance.go
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDP(s string, t string) int {\nn := len(s)\nm := len(t)\ndp := make([][]int, n+1)\nfor i := 0; i <= n; i++ {\ndp[i] = make([]int, m+1)\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor i := 1; i <= n; i++ {\ndp[i][0] = i\n}\nfor j := 1; j <= m; j++ {\ndp[0][j] = j\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i := 1; i <= n; i++ {\nfor j := 1; j <= m; j++ {\nif s[i-1] == t[j-1] {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i-1][j-1]\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = MinInt(MinInt(dp[i][j-1], dp[i-1][j]), dp[i-1][j-1]) + 1\n}\n}\n}\nreturn dp[n][m]\n}\n
edit_distance.swift
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDP(s: String, t: String) -> Int {\nlet n = s.utf8CString.count\nlet m = t.utf8CString.count\nvar dp = Array(repeating: Array(repeating: 0, count: m + 1), count: n + 1)\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor i in stride(from: 1, through: n, by: 1) {\ndp[i][0] = i\n}\nfor j in stride(from: 1, through: m, by: 1) {\ndp[0][j] = j\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in stride(from: 1, through: n, by: 1) {\nfor j in stride(from: 1, through: m, by: 1) {\nif s.utf8CString[i - 1] == t.utf8CString[j - 1] {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1]\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1\n}\n}\n}\nreturn dp[n][m]\n}\n
edit_distance.js
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction editDistanceDP(s, t) {\nconst n = s.length,\nm = t.length;\nconst dp = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0));\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (let i = 1; i <= n; i++) {\ndp[i][0] = i;\n}\nfor (let j = 1; j <= m; j++) {\ndp[0][j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (let i = 1; i <= n; i++) {\nfor (let j = 1; j <= m; j++) {\nif (s.charAt(i - 1) === t.charAt(j - 1)) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1];\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] =\nMath.min(\nMath.min(dp[i][j - 1], dp[i - 1][j]),\ndp[i - 1][j - 1]\n) + 1;\n}\n}\n}\nreturn dp[n][m];\n}\n
edit_distance.ts
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction editDistanceDP(s: string, t: string): number {\nconst n = s.length,\nm = t.length;\nconst dp = Array.from({ length: n + 1 }, () =>\nArray.from({ length: m + 1 }, () => 0)\n);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (let i = 1; i <= n; i++) {\ndp[i][0] = i;\n}\nfor (let j = 1; j <= m; j++) {\ndp[0][j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (let i = 1; i <= n; i++) {\nfor (let j = 1; j <= m; j++) {\nif (s.charAt(i - 1) === t.charAt(j - 1)) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1];\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] =\nMath.min(\nMath.min(dp[i][j - 1], dp[i - 1][j]),\ndp[i - 1][j - 1]\n) + 1;\n}\n}\n}\nreturn dp[n][m];\n}\n
edit_distance.dart
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(String s, String t) {\nint n = s.length, m = t.length;\nList<List<int>> dp = List.generate(n + 1, (_) => List.filled(m + 1, 0));\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int i = 1; i <= n; i++) {\ndp[i][0] = i;\n}\nfor (int j = 1; j <= m; j++) {\ndp[0][j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i <= n; i++) {\nfor (int j = 1; j <= m; j++) {\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1];\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n}\n}\n}\nreturn dp[n][m];\n}\n
edit_distance.rs
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfn edit_distance_dp(s: &str, t: &str) -> i32 {\nlet (n, m) = (s.len(), t.len());\nlet mut dp = vec![vec![0; m + 1]; n + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor i in 1..= n {\ndp[i][0] = i as i32;\n}\nfor j in 1..m {\ndp[0][j] = j as i32;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in 1..=n {\nfor j in 1..=m {\nif s.chars().nth(i - 1) == t.chars().nth(j - 1) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1];\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = std::cmp::min(std::cmp::min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n}\n}\n}\ndp[n][m]\n}\n
edit_distance.c
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(char *s, char *t, int n, int m) {\nint dp[n + 1][m + 1];\nmemset(dp, 0, sizeof(dp));\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int i = 1; i <= n; i++) {\ndp[i][0] = i;\n}\nfor (int j = 1; j <= m; j++) {\ndp[0][j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i <= n; i++) {\nfor (int j = 1; j <= m; j++) {\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1];\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n}\n}\n}\nreturn dp[n][m];\n}\n
edit_distance.zig
// \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212\nfn editDistanceDP(comptime s: []const u8, comptime t: []const u8) i32 {\ncomptime var n = s.len;\ncomptime var m = t.len;\nvar dp = [_][m + 1]i32{[_]i32{0} ** (m + 1)} ** (n + 1);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (1..n + 1) |i| {\ndp[i][0] = @intCast(i);\n}\nfor (1..m + 1) |j| {\ndp[0][j] = @intCast(j);\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (1..n + 1) |i| {\nfor (1..m + 1) |j| {\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1];\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = @min(@min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n}\n}\n}\nreturn dp[n][m];\n}\n

\u5982\u56fe 14-30 \u6240\u793a\uff0c\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898\u7684\u72b6\u6001\u8f6c\u79fb\u8fc7\u7a0b\u4e0e\u80cc\u5305\u95ee\u9898\u975e\u5e38\u7c7b\u4f3c\uff0c\u90fd\u53ef\u4ee5\u770b\u4f5c\u662f\u586b\u5199\u4e00\u4e2a\u4e8c\u7ef4\u7f51\u683c\u7684\u8fc7\u7a0b\u3002

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

\u56fe 14-30 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

"},{"location":"chapter_dynamic_programming/edit_distance_problem/#3","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

\u7531\u4e8e \\(dp[i,j]\\) \u662f\u7531\u4e0a\u65b9 \\(dp[i-1, j]\\)\u3001\u5de6\u65b9 \\(dp[i, j-1]\\)\u3001\u5de6\u4e0a\u65b9\u72b6\u6001 \\(dp[i-1, j-1]\\) \u8f6c\u79fb\u800c\u6765\uff0c\u800c\u6b63\u5e8f\u904d\u5386\u4f1a\u4e22\u5931\u5de6\u4e0a\u65b9 \\(dp[i-1, j-1]\\) \uff0c\u5012\u5e8f\u904d\u5386\u65e0\u6cd5\u63d0\u524d\u6784\u5efa \\(dp[i, j-1]\\) \uff0c\u56e0\u6b64\u4e24\u79cd\u904d\u5386\u987a\u5e8f\u90fd\u4e0d\u53ef\u53d6\u3002

\u4e3a\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u53d8\u91cf leftup \u6765\u6682\u5b58\u5de6\u4e0a\u65b9\u7684\u89e3 \\(dp[i-1, j-1]\\) \uff0c\u4ece\u800c\u53ea\u9700\u8003\u8651\u5de6\u65b9\u548c\u4e0a\u65b9\u7684\u89e3\u3002\u6b64\u65f6\u7684\u60c5\u51b5\u4e0e\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u76f8\u540c\uff0c\u53ef\u4f7f\u7528\u6b63\u5e8f\u904d\u5386\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig edit_distance.py
def edit_distance_dp_comp(s: str, t: str) -> int:\n\"\"\"\u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\nn, m = len(s), len(t)\ndp = [0] * (m + 1)\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor j in range(1, m + 1):\ndp[j] = j\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor i in range(1, n + 1):\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nleftup = dp[0]  # \u6682\u5b58 dp[i-1, j-1]\ndp[0] += 1\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor j in range(1, m + 1):\ntemp = dp[j]\nif s[i - 1] == t[j - 1]:\n# \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup\nelse:\n# \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = min(dp[j - 1], dp[j], leftup) + 1\nleftup = temp  # \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\nreturn dp[m]\n
edit_distance.cpp
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(string s, string t) {\nint n = s.length(), m = t.length();\nvector<int> dp(m + 1, 0);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j <= m; j++) {\ndp[j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i <= n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nint leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j <= m; j++) {\nint temp = dp[j];\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup;\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;\n}\nleftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m];\n}\n
edit_distance.java
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(String s, String t) {\nint n = s.length(), m = t.length();\nint[] dp = new int[m + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j <= m; j++) {\ndp[j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i <= n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nint leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j <= m; j++) {\nint temp = dp[j];\nif (s.charAt(i - 1) == t.charAt(j - 1)) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup;\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = Math.min(Math.min(dp[j - 1], dp[j]), leftup) + 1;\n}\nleftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m];\n}\n
edit_distance.cs
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint EditDistanceDPComp(string s, string t) {\nint n = s.Length, m = t.Length;\nint[] dp = new int[m + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j <= m; j++) {\ndp[j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i <= n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nint leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j <= m; j++) {\nint temp = dp[j];\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup;\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = Math.Min(Math.Min(dp[j - 1], dp[j]), leftup) + 1;\n}\nleftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m];\n}\n
edit_distance.go
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDPComp(s string, t string) int {\nn := len(s)\nm := len(t)\ndp := make([]int, m+1)\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor j := 1; j <= m; j++ {\ndp[j] = j\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor i := 1; i <= n; i++ {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nleftUp := dp[0] // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor j := 1; j <= m; j++ {\ntemp := dp[j]\nif s[i-1] == t[j-1] {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftUp\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = MinInt(MinInt(dp[j-1], dp[j]), leftUp) + 1\n}\nleftUp = temp // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m]\n}\n
edit_distance.swift
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDPComp(s: String, t: String) -> Int {\nlet n = s.utf8CString.count\nlet m = t.utf8CString.count\nvar dp = Array(repeating: 0, count: m + 1)\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor j in stride(from: 1, through: m, by: 1) {\ndp[j] = j\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor i in stride(from: 1, through: n, by: 1) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nvar leftup = dp[0] // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor j in stride(from: 1, through: m, by: 1) {\nlet temp = dp[j]\nif s.utf8CString[i - 1] == t.utf8CString[j - 1] {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1\n}\nleftup = temp // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m]\n}\n
edit_distance.js
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction editDistanceDPComp(s, t) {\nconst n = s.length,\nm = t.length;\nconst dp = new Array(m + 1).fill(0);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (let j = 1; j <= m; j++) {\ndp[j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (let i = 1; i <= n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nlet leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (let j = 1; j <= m; j++) {\nconst temp = dp[j];\nif (s.charAt(i - 1) === t.charAt(j - 1)) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup;\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = Math.min(Math.min(dp[j - 1], dp[j]), leftup) + 1;\n}\nleftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m];\n}\n
edit_distance.ts
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction editDistanceDPComp(s: string, t: string): number {\nconst n = s.length,\nm = t.length;\nconst dp = new Array(m + 1).fill(0);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (let j = 1; j <= m; j++) {\ndp[j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (let i = 1; i <= n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nlet leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (let j = 1; j <= m; j++) {\nconst temp = dp[j];\nif (s.charAt(i - 1) === t.charAt(j - 1)) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup;\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = Math.min(Math.min(dp[j - 1], dp[j]), leftup) + 1;\n}\nleftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m];\n}\n
edit_distance.dart
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(String s, String t) {\nint n = s.length, m = t.length;\nList<int> dp = List.filled(m + 1, 0);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j <= m; j++) {\ndp[j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i <= n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nint leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j <= m; j++) {\nint temp = dp[j];\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup;\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;\n}\nleftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m];\n}\n
edit_distance.rs
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn edit_distance_dp_comp(s: &str, t: &str) -> i32 {\nlet (n, m) = (s.len(), t.len());\nlet mut dp = vec![0; m + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor j in 1..m {\ndp[j] = j as i32;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor i in 1..=n {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nlet mut leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i as i32;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor j in 1..=m {\nlet temp = dp[j];\nif s.chars().nth(i - 1) == t.chars().nth(j - 1) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup;\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = std::cmp::min(std::cmp::min(dp[j - 1], dp[j]), leftup) + 1;\n}\nleftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\ndp[m]\n}\n
edit_distance.c
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(char *s, char *t, int n, int m) {\nint dp[m + 1];\nmemset(dp, 0, sizeof(dp));\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j <= m; j++) {\ndp[j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i <= n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nint leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j <= m; j++) {\nint temp = dp[j];\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup;\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;\n}\nleftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m];\n}\n
edit_distance.zig
// \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn editDistanceDPComp(comptime s: []const u8, comptime t: []const u8) i32 {\ncomptime var n = s.len;\ncomptime var m = t.len;\nvar dp = [_]i32{0} ** (m + 1);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (1..m + 1) |j| {\ndp[j] = @intCast(j);\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (1..n + 1) |i| {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nvar leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = @intCast(i);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (1..m + 1) |j| {\nvar temp = dp[j];\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup;\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = @min(@min(dp[j - 1], dp[j]), leftup) + 1;\n}\nleftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m];\n}\n
"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/","title":"14.1 \u00a0 \u521d\u63a2\u52a8\u6001\u89c4\u5212","text":"

\u300c\u52a8\u6001\u89c4\u5212 dynamic programming\u300d\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u7b97\u6cd5\u8303\u5f0f\uff0c\u5b83\u5c06\u4e00\u4e2a\u95ee\u9898\u5206\u89e3\u4e3a\u4e00\u7cfb\u5217\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u5e76\u901a\u8fc7\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\u6765\u907f\u514d\u91cd\u590d\u8ba1\u7b97\uff0c\u4ece\u800c\u5927\u5e45\u63d0\u5347\u65f6\u95f4\u6548\u7387\u3002

\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u4ece\u4e00\u4e2a\u7ecf\u5178\u4f8b\u9898\u5165\u624b\uff0c\u5148\u7ed9\u51fa\u5b83\u7684\u66b4\u529b\u56de\u6eaf\u89e3\u6cd5\uff0c\u89c2\u5bdf\u5176\u4e2d\u5305\u542b\u7684\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u518d\u9010\u6b65\u5bfc\u51fa\u66f4\u9ad8\u6548\u7684\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\u3002

\u722c\u697c\u68af

\u7ed9\u5b9a\u4e00\u4e2a\u5171\u6709 \\(n\\) \u9636\u7684\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\uff0c\u8bf7\u95ee\u6709\u591a\u5c11\u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\u3002

\u5982\u56fe 14-1 \u6240\u793a\uff0c\u5bf9\u4e8e\u4e00\u4e2a \\(3\\) \u9636\u697c\u68af\uff0c\u5171\u6709 \\(3\\) \u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\u3002

\u56fe 14-1 \u00a0 \u722c\u5230\u7b2c 3 \u9636\u7684\u65b9\u6848\u6570\u91cf

\u672c\u9898\u7684\u76ee\u6807\u662f\u6c42\u89e3\u65b9\u6848\u6570\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u901a\u8fc7\u56de\u6eaf\u6765\u7a77\u4e3e\u6240\u6709\u53ef\u80fd\u6027\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5c06\u722c\u697c\u68af\u60f3\u8c61\u4e3a\u4e00\u4e2a\u591a\u8f6e\u9009\u62e9\u7684\u8fc7\u7a0b\uff1a\u4ece\u5730\u9762\u51fa\u53d1\uff0c\u6bcf\u8f6e\u9009\u62e9\u4e0a \\(1\\) \u9636\u6216 \\(2\\) \u9636\uff0c\u6bcf\u5f53\u5230\u8fbe\u697c\u68af\u9876\u90e8\u65f6\u5c31\u5c06\u65b9\u6848\u6570\u91cf\u52a0 \\(1\\) \uff0c\u5f53\u8d8a\u8fc7\u697c\u68af\u9876\u90e8\u65f6\u5c31\u5c06\u5176\u526a\u679d\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_backtrack.py
def backtrack(choices: list[int], state: int, n: int, res: list[int]) -> int:\n\"\"\"\u56de\u6eaf\"\"\"\n# \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif state == n:\nres[0] += 1\n# \u904d\u5386\u6240\u6709\u9009\u62e9\nfor choice in choices:\n# \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif state + choice > n:\nbreak\n# \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res)\n# \u56de\u9000\ndef climbing_stairs_backtrack(n: int) -> int:\n\"\"\"\u722c\u697c\u68af\uff1a\u56de\u6eaf\"\"\"\nchoices = [1, 2]  # \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nstate = 0  # \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nres = [0]  # \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(choices, state, n, res)\nreturn res[0]\n
climbing_stairs_backtrack.cpp
/* \u56de\u6eaf */\nvoid backtrack(vector<int> &choices, int state, int n, vector<int> &res) {\n// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif (state == n)\nres[0]++;\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (auto &choice : choices) {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif (state + choice > n)\nbreak;\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res);\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\nvector<int> choices = {1, 2}; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nint state = 0;                // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nvector<int> res = {0};        // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(choices, state, n, res);\nreturn res[0];\n}\n
climbing_stairs_backtrack.java
/* \u56de\u6eaf */\nvoid backtrack(List<Integer> choices, int state, int n, List<Integer> res) {\n// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif (state == n)\nres.set(0, res.get(0) + 1);\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (Integer choice : choices) {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif (state + choice > n)\nbreak;\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res);\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\nList<Integer> choices = Arrays.asList(1, 2); // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nint state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nList<Integer> res = new ArrayList<>();\nres.add(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(choices, state, n, res);\nreturn res.get(0);\n}\n
climbing_stairs_backtrack.cs
/* \u56de\u6eaf */\nvoid Backtrack(List<int> choices, int state, int n, List<int> res) {\n// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif (state == n)\nres[0]++;\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nforeach (int choice in choices) {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif (state + choice > n)\nbreak;\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nBacktrack(choices, state + choice, n, res);\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint ClimbingStairsBacktrack(int n) {\nList<int> choices = new() { 1, 2 }; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nint state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nList<int> res = new() { 0 }; // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nBacktrack(choices, state, n, res);\nreturn res[0];\n}\n
climbing_stairs_backtrack.go
/* \u56de\u6eaf */\nfunc backtrack(choices []int, state, n int, res []int) {\n// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif state == n {\nres[0] = res[0] + 1\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor _, choice := range choices {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif state+choice > n {\nbreak\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state+choice, n, res)\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunc climbingStairsBacktrack(n int) int {\n// \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nchoices := []int{1, 2}\n// \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nstate := 0\nres := make([]int, 1)\n// \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nres[0] = 0\nbacktrack(choices, state, n, res)\nreturn res[0]\n}\n
climbing_stairs_backtrack.swift
/* \u56de\u6eaf */\nfunc backtrack(choices: [Int], state: Int, n: Int, res: inout [Int]) {\n// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif state == n {\nres[0] += 1\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor choice in choices {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif state + choice > n {\nbreak\n}\nbacktrack(choices: choices, state: state + choice, n: n, res: &res)\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunc climbingStairsBacktrack(n: Int) -> Int {\nlet choices = [1, 2] // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nlet state = 0 // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nvar res: [Int] = []\nres.append(0) // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(choices: choices, state: state, n: n, res: &res)\nreturn res[0]\n}\n
climbing_stairs_backtrack.js
/* \u56de\u6eaf */\nfunction backtrack(choices, state, n, res) {\n// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif (state === n) res.set(0, res.get(0) + 1);\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (const choice of choices) {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif (state + choice > n) break;\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res);\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunction climbingStairsBacktrack(n) {\nconst choices = [1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nconst state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nconst res = new Map();\nres.set(0, 0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(choices, state, n, res);\nreturn res.get(0);\n}\n
climbing_stairs_backtrack.ts
/* \u56de\u6eaf */\nfunction backtrack(\nchoices: number[],\nstate: number,\nn: number,\nres: Map<0, any>\n): void {\n// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif (state === n) res.set(0, res.get(0) + 1);\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (const choice of choices) {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif (state + choice > n) break;\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res);\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunction climbingStairsBacktrack(n: number): number {\nconst choices = [1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nconst state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nconst res = new Map();\nres.set(0, 0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(choices, state, n, res);\nreturn res.get(0);\n}\n
climbing_stairs_backtrack.dart
/* \u56de\u6eaf */\nvoid backtrack(List<int> choices, int state, int n, List<int> res) {\n// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif (state == n) {\nres[0]++;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int choice in choices) {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif (state + choice > n) break;\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res);\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\nList<int> choices = [1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nint state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nList<int> res = [];\nres.add(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(choices, state, n, res);\nreturn res[0];\n}\n
climbing_stairs_backtrack.rs
/* \u56de\u6eaf */\nfn backtrack(choices: &[i32], state: i32, n: i32, res: &mut [i32]) {\n// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif state == n { res[0] = res[0] + 1; }\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor &choice in choices {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif state + choice > n { break; }\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res);\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfn climbing_stairs_backtrack(n: usize) -> i32 {\nlet choices = vec![ 1, 2 ]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nlet state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nlet mut res = Vec::new();\nres.push(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(&choices, state, n as i32, &mut res);\nres[0]\n}\n
climbing_stairs_backtrack.c
/* \u56de\u6eaf */\nvoid backtrack(int *choices, int state, int n, int *res, int len) {\n// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif (state == n)\nres[0]++;\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i < len; i++) {\nint choice = choices[i];\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif (state + choice > n)\nbreak;\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res, len);\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\nint choices[2] = {1, 2}; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nint state = 0;           // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nint *res = (int *)malloc(sizeof(int));\n*res = 0;                // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nint len = sizeof(choices) / sizeof(int);\nbacktrack(choices, state, n, res, len);\nint result = *res;\nfree(res);\nreturn result;\n}\n
climbing_stairs_backtrack.zig
// \u56de\u6eaf\nfn backtrack(choices: []i32, state: i32, n: i32, res: std.ArrayList(i32)) void {\n// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif (state == n) {\nres.items[0] = res.items[0] + 1;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (choices) |choice| {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif (state + choice > n) {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res);\n// \u56de\u9000\n}\n}\n// \u722c\u697c\u68af\uff1a\u56de\u6eaf\nfn climbingStairsBacktrack(n: usize) !i32 {\nvar choices = [_]i32{ 1, 2 }; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nvar state: i32 = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nvar res = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer res.deinit();\ntry res.append(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(&choices, state, @intCast(n), res);\nreturn res.items[0];\n}\n
"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1411","title":"14.1.1 \u00a0 \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u641c\u7d22","text":"

\u56de\u6eaf\u7b97\u6cd5\u901a\u5e38\u5e76\u4e0d\u663e\u5f0f\u5730\u5bf9\u95ee\u9898\u8fdb\u884c\u62c6\u89e3\uff0c\u800c\u662f\u5c06\u95ee\u9898\u770b\u4f5c\u4e00\u7cfb\u5217\u51b3\u7b56\u6b65\u9aa4\uff0c\u901a\u8fc7\u8bd5\u63a2\u548c\u526a\u679d\uff0c\u641c\u7d22\u6240\u6709\u53ef\u80fd\u7684\u89e3\u3002

\u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u4ece\u95ee\u9898\u5206\u89e3\u7684\u89d2\u5ea6\u5206\u6790\u8fd9\u9053\u9898\u3002\u8bbe\u722c\u5230\u7b2c \\(i\\) \u9636\u5171\u6709 \\(dp[i]\\) \u79cd\u65b9\u6848\uff0c\u90a3\u4e48 \\(dp[i]\\) \u5c31\u662f\u539f\u95ee\u9898\uff0c\u5176\u5b50\u95ee\u9898\u5305\u62ec:

\\[ dp[i-1], dp[i-2], \\dots, dp[2], dp[1] \\]

\u7531\u4e8e\u6bcf\u8f6e\u53ea\u80fd\u4e0a \\(1\\) \u9636\u6216 \\(2\\) \u9636\uff0c\u56e0\u6b64\u5f53\u6211\u4eec\u7ad9\u5728\u7b2c \\(i\\) \u9636\u697c\u68af\u4e0a\u65f6\uff0c\u4e0a\u4e00\u8f6e\u53ea\u53ef\u80fd\u7ad9\u5728\u7b2c \\(i - 1\\) \u9636\u6216\u7b2c \\(i - 2\\) \u9636\u4e0a\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u4eec\u53ea\u80fd\u4ece\u7b2c \\(i -1\\) \u9636\u6216\u7b2c \\(i - 2\\) \u9636\u524d\u5f80\u7b2c \\(i\\) \u9636\u3002

\u7531\u6b64\u4fbf\u53ef\u5f97\u51fa\u4e00\u4e2a\u91cd\u8981\u63a8\u8bba\uff1a\u722c\u5230\u7b2c \\(i - 1\\) \u9636\u7684\u65b9\u6848\u6570\u52a0\u4e0a\u722c\u5230\u7b2c \\(i - 2\\) \u9636\u7684\u65b9\u6848\u6570\u5c31\u7b49\u4e8e\u722c\u5230\u7b2c \\(i\\) \u9636\u7684\u65b9\u6848\u6570\u3002\u516c\u5f0f\u5982\u4e0b\uff1a

\\[ dp[i] = dp[i-1] + dp[i-2] \\]

\u8fd9\u610f\u5473\u7740\u5728\u722c\u697c\u68af\u95ee\u9898\u4e2d\uff0c\u5404\u4e2a\u5b50\u95ee\u9898\u4e4b\u95f4\u5b58\u5728\u9012\u63a8\u5173\u7cfb\uff0c\u539f\u95ee\u9898\u7684\u89e3\u53ef\u4ee5\u7531\u5b50\u95ee\u9898\u7684\u89e3\u6784\u5efa\u5f97\u6765\u3002\u56fe 14-2 \u5c55\u793a\u4e86\u8be5\u9012\u63a8\u5173\u7cfb\u3002

\u56fe 14-2 \u00a0 \u65b9\u6848\u6570\u91cf\u9012\u63a8\u5173\u7cfb

\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9012\u63a8\u516c\u5f0f\u5f97\u5230\u66b4\u529b\u641c\u7d22\u89e3\u6cd5\u3002\u4ee5 \\(dp[n]\\) \u4e3a\u8d77\u59cb\u70b9\uff0c\u9012\u5f52\u5730\u5c06\u4e00\u4e2a\u8f83\u5927\u95ee\u9898\u62c6\u89e3\u4e3a\u4e24\u4e2a\u8f83\u5c0f\u95ee\u9898\u7684\u548c\uff0c\u76f4\u81f3\u5230\u8fbe\u6700\u5c0f\u5b50\u95ee\u9898 \\(dp[1]\\) \u548c \\(dp[2]\\) \u65f6\u8fd4\u56de\u3002\u5176\u4e2d\uff0c\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\u662f\u5df2\u77e5\u7684\uff0c\u5373 \\(dp[1] = 1\\)\u3001\\(dp[2] = 2\\) \uff0c\u8868\u793a\u722c\u5230\u7b2c \\(1\\)\u3001\\(2\\) \u9636\u5206\u522b\u6709 \\(1\\)\u3001\\(2\\) \u79cd\u65b9\u6848\u3002

\u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u5b83\u548c\u6807\u51c6\u56de\u6eaf\u4ee3\u7801\u90fd\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff0c\u4f46\u66f4\u52a0\u7b80\u6d01\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_dfs.py
def dfs(i: int) -> int:\n\"\"\"\u641c\u7d22\"\"\"\n# \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif i == 1 or i == 2:\nreturn i\n# dp[i] = dp[i-1] + dp[i-2]\ncount = dfs(i - 1) + dfs(i - 2)\nreturn count\ndef climbing_stairs_dfs(n: int) -> int:\n\"\"\"\u722c\u697c\u68af\uff1a\u641c\u7d22\"\"\"\nreturn dfs(n)\n
climbing_stairs_dfs.cpp
/* \u641c\u7d22 */\nint dfs(int i) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2)\nreturn i;\n// dp[i] = dp[i-1] + dp[i-2]\nint count = dfs(i - 1) + dfs(i - 2);\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\nreturn dfs(n);\n}\n
climbing_stairs_dfs.java
/* \u641c\u7d22 */\nint dfs(int i) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2)\nreturn i;\n// dp[i] = dp[i-1] + dp[i-2]\nint count = dfs(i - 1) + dfs(i - 2);\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\nreturn dfs(n);\n}\n
climbing_stairs_dfs.cs
/* \u641c\u7d22 */\nint DFS(int i) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2)\nreturn i;\n// dp[i] = dp[i-1] + dp[i-2]\nint count = DFS(i - 1) + DFS(i - 2);\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint ClimbingStairsDFS(int n) {\nreturn DFS(n);\n}\n
climbing_stairs_dfs.go
/* \u641c\u7d22 */\nfunc dfs(i int) int {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif i == 1 || i == 2 {\nreturn i\n}\n// dp[i] = dp[i-1] + dp[i-2]\ncount := dfs(i-1) + dfs(i-2)\nreturn count\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunc climbingStairsDFS(n int) int {\nreturn dfs(n)\n}\n
climbing_stairs_dfs.swift
/* \u641c\u7d22 */\nfunc dfs(i: Int) -> Int {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif i == 1 || i == 2 {\nreturn i\n}\n// dp[i] = dp[i-1] + dp[i-2]\nlet count = dfs(i: i - 1) + dfs(i: i - 2)\nreturn count\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunc climbingStairsDFS(n: Int) -> Int {\ndfs(i: n)\n}\n
climbing_stairs_dfs.js
/* \u641c\u7d22 */\nfunction dfs(i) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i === 1 || i === 2) return i;\n// dp[i] = dp[i-1] + dp[i-2]\nconst count = dfs(i - 1) + dfs(i - 2);\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunction climbingStairsDFS(n) {\nreturn dfs(n);\n}\n
climbing_stairs_dfs.ts
/* \u641c\u7d22 */\nfunction dfs(i: number): number {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i === 1 || i === 2) return i;\n// dp[i] = dp[i-1] + dp[i-2]\nconst count = dfs(i - 1) + dfs(i - 2);\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunction climbingStairsDFS(n: number): number {\nreturn dfs(n);\n}\n
climbing_stairs_dfs.dart
/* \u641c\u7d22 */\nint dfs(int i) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2) return i;\n// dp[i] = dp[i-1] + dp[i-2]\nint count = dfs(i - 1) + dfs(i - 2);\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\nreturn dfs(n);\n}\n
climbing_stairs_dfs.rs
/* \u641c\u7d22 */\nfn dfs(i: usize) -> i32 {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif i == 1 || i == 2 { return i as i32; }\n// dp[i] = dp[i-1] + dp[i-2]\nlet count = dfs(i - 1) + dfs(i - 2);\ncount\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfn climbing_stairs_dfs(n: usize) -> i32 {\ndfs(n) }\n
climbing_stairs_dfs.c
/* \u641c\u7d22 */\nint dfs(int i) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2)\nreturn i;\n// dp[i] = dp[i-1] + dp[i-2]\nint count = dfs(i - 1) + dfs(i - 2);\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\nreturn dfs(n);\n}\n
climbing_stairs_dfs.zig
// \u641c\u7d22\nfn dfs(i: usize) i32 {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 or i == 2) {\nreturn @intCast(i);\n}\n// dp[i] = dp[i-1] + dp[i-2]\nvar count = dfs(i - 1) + dfs(i - 2);\nreturn count;\n}\n// \u722c\u697c\u68af\uff1a\u641c\u7d22\nfn climbingStairsDFS(comptime n: usize) i32 {\nreturn dfs(n);\n}\n

\u56fe 14-3 \u5c55\u793a\u4e86\u66b4\u529b\u641c\u7d22\u5f62\u6210\u7684\u9012\u5f52\u6811\u3002\u5bf9\u4e8e\u95ee\u9898 \\(dp[n]\\) \uff0c\u5176\u9012\u5f52\u6811\u7684\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \u3002\u6307\u6570\u9636\u5c5e\u4e8e\u7206\u70b8\u5f0f\u589e\u957f\uff0c\u5982\u679c\u6211\u4eec\u8f93\u5165\u4e00\u4e2a\u6bd4\u8f83\u5927\u7684 \\(n\\) \uff0c\u5219\u4f1a\u9677\u5165\u6f2b\u957f\u7684\u7b49\u5f85\u4e4b\u4e2d\u3002

\u56fe 14-3 \u00a0 \u722c\u697c\u68af\u5bf9\u5e94\u9012\u5f52\u6811

\u89c2\u5bdf\u56fe 14-3 \uff0c\u6307\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f\u7531\u4e8e\u201c\u91cd\u53e0\u5b50\u95ee\u9898\u201d\u5bfc\u81f4\u7684\u3002\u4f8b\u5982 \\(dp[9]\\) \u88ab\u5206\u89e3\u4e3a \\(dp[8]\\) \u548c \\(dp[7]\\) \uff0c\\(dp[8]\\) \u88ab\u5206\u89e3\u4e3a \\(dp[7]\\) \u548c \\(dp[6]\\) \uff0c\u4e24\u8005\u90fd\u5305\u542b\u5b50\u95ee\u9898 \\(dp[7]\\) \u3002

\u4ee5\u6b64\u7c7b\u63a8\uff0c\u5b50\u95ee\u9898\u4e2d\u5305\u542b\u66f4\u5c0f\u7684\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u5b50\u5b50\u5b59\u5b59\u65e0\u7a77\u5c3d\u4e5f\u3002\u7edd\u5927\u90e8\u5206\u8ba1\u7b97\u8d44\u6e90\u90fd\u6d6a\u8d39\u5728\u8fd9\u4e9b\u91cd\u53e0\u7684\u95ee\u9898\u4e0a\u3002

"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1412","title":"14.1.2 \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22","text":"

\u4e3a\u4e86\u63d0\u5347\u7b97\u6cd5\u6548\u7387\uff0c\u6211\u4eec\u5e0c\u671b\u6240\u6709\u7684\u91cd\u53e0\u5b50\u95ee\u9898\u90fd\u53ea\u88ab\u8ba1\u7b97\u4e00\u6b21\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u6570\u7ec4 mem \u6765\u8bb0\u5f55\u6bcf\u4e2a\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5e76\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u5c06\u91cd\u53e0\u5b50\u95ee\u9898\u526a\u679d\u3002

  1. \u5f53\u9996\u6b21\u8ba1\u7b97 \\(dp[i]\\) \u65f6\uff0c\u6211\u4eec\u5c06\u5176\u8bb0\u5f55\u81f3 mem[i] \uff0c\u4ee5\u4fbf\u4e4b\u540e\u4f7f\u7528\u3002
  2. \u5f53\u518d\u6b21\u9700\u8981\u8ba1\u7b97 \\(dp[i]\\) \u65f6\uff0c\u6211\u4eec\u4fbf\u53ef\u76f4\u63a5\u4ece mem[i] \u4e2d\u83b7\u53d6\u7ed3\u679c\uff0c\u4ece\u800c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u8be5\u5b50\u95ee\u9898\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_dfs_mem.py
def dfs(i: int, mem: list[int]) -> int:\n\"\"\"\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n# \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif i == 1 or i == 2:\nreturn i\n# \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif mem[i] != -1:\nreturn mem[i]\n# dp[i] = dp[i-1] + dp[i-2]\ncount = dfs(i - 1, mem) + dfs(i - 2, mem)\n# \u8bb0\u5f55 dp[i]\nmem[i] = count\nreturn count\ndef climbing_stairs_dfs_mem(n: int) -> int:\n\"\"\"\u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n# mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nmem = [-1] * (n + 1)\nreturn dfs(n, mem)\n
climbing_stairs_dfs_mem.cpp
/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, vector<int> &mem) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2)\nreturn i;\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif (mem[i] != -1)\nreturn mem[i];\n// dp[i] = dp[i-1] + dp[i-2]\nint count = dfs(i - 1, mem) + dfs(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nvector<int> mem(n + 1, -1);\nreturn dfs(n, mem);\n}\n
climbing_stairs_dfs_mem.java
/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, int[] mem) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2)\nreturn i;\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif (mem[i] != -1)\nreturn mem[i];\n// dp[i] = dp[i-1] + dp[i-2]\nint count = dfs(i - 1, mem) + dfs(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nint[] mem = new int[n + 1];\nArrays.fill(mem, -1);\nreturn dfs(n, mem);\n}\n
climbing_stairs_dfs_mem.cs
/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint DFS(int i, int[] mem) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2)\nreturn i;\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif (mem[i] != -1)\nreturn mem[i];\n// dp[i] = dp[i-1] + dp[i-2]\nint count = DFS(i - 1, mem) + DFS(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint ClimbingStairsDFSMem(int n) {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nint[] mem = new int[n + 1];\nArray.Fill(mem, -1);\nreturn DFS(n, mem);\n}\n
climbing_stairs_dfs_mem.go
/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc dfsMem(i int, mem []int) int {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif i == 1 || i == 2 {\nreturn i\n}\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif mem[i] != -1 {\nreturn mem[i]\n}\n// dp[i] = dp[i-1] + dp[i-2]\ncount := dfsMem(i-1, mem) + dfsMem(i-2, mem)\n// \u8bb0\u5f55 dp[i]\nmem[i] = count\nreturn count\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc climbingStairsDFSMem(n int) int {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nmem := make([]int, n+1)\nfor i := range mem {\nmem[i] = -1\n}\nreturn dfsMem(n, mem)\n}\n
climbing_stairs_dfs_mem.swift
/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc dfs(i: Int, mem: inout [Int]) -> Int {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif i == 1 || i == 2 {\nreturn i\n}\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif mem[i] != -1 {\nreturn mem[i]\n}\n// dp[i] = dp[i-1] + dp[i-2]\nlet count = dfs(i: i - 1, mem: &mem) + dfs(i: i - 2, mem: &mem)\n// \u8bb0\u5f55 dp[i]\nmem[i] = count\nreturn count\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc climbingStairsDFSMem(n: Int) -> Int {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nvar mem = Array(repeating: -1, count: n + 1)\nreturn dfs(i: n, mem: &mem)\n}\n
climbing_stairs_dfs_mem.js
/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction dfs(i, mem) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i === 1 || i === 2) return i;\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif (mem[i] != -1) return mem[i];\n// dp[i] = dp[i-1] + dp[i-2]\nconst count = dfs(i - 1, mem) + dfs(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction climbingStairsDFSMem(n) {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nconst mem = new Array(n + 1).fill(-1);\nreturn dfs(n, mem);\n}\n
climbing_stairs_dfs_mem.ts
/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction dfs(i: number, mem: number[]): number {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i === 1 || i === 2) return i;\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif (mem[i] != -1) return mem[i];\n// dp[i] = dp[i-1] + dp[i-2]\nconst count = dfs(i - 1, mem) + dfs(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction climbingStairsDFSMem(n: number): number {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nconst mem = new Array(n + 1).fill(-1);\nreturn dfs(n, mem);\n}\n
climbing_stairs_dfs_mem.dart
/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, List<int> mem) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2) return i;\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif (mem[i] != -1) return mem[i];\n// dp[i] = dp[i-1] + dp[i-2]\nint count = dfs(i - 1, mem) + dfs(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nList<int> mem = List.filled(n + 1, -1);\nreturn dfs(n, mem);\n}\n
climbing_stairs_dfs_mem.rs
/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn dfs(i: usize, mem: &mut [i32]) -> i32 {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif i == 1 || i == 2 { return i as i32; }\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif mem[i] != -1 { return mem[i]; }\n// dp[i] = dp[i-1] + dp[i-2]\nlet count = dfs(i - 1, mem) + dfs(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\ncount\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn climbing_stairs_dfs_mem(n: usize) -> i32 {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nlet mut mem = vec![-1; n + 1];\ndfs(n, &mut mem)\n}\n
climbing_stairs_dfs_mem.c
/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, int *mem) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2)\nreturn i;\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif (mem[i] != -1)\nreturn mem[i];\n// dp[i] = dp[i-1] + dp[i-2]\nint count = dfs(i - 1, mem) + dfs(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nint *mem = (int *)malloc((n + 1) * sizeof(int));\nfor (int i = 0; i <= n; i++) {\nmem[i] = -1;\n}\nint result = dfs(n, mem);\nfree(mem);\nreturn result;\n}\n
climbing_stairs_dfs_mem.zig
// \u8bb0\u5fc6\u5316\u641c\u7d22\nfn dfs(i: usize, mem: []i32) i32 {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 or i == 2) {\nreturn @intCast(i);\n}\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif (mem[i] != -1) {\nreturn mem[i];\n}\n// dp[i] = dp[i-1] + dp[i-2]\nvar count = dfs(i - 1, mem) + dfs(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\nreturn count;\n}\n// \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\nfn climbingStairsDFSMem(comptime n: usize) i32 {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nvar mem = [_]i32{ -1 } ** (n + 1);\nreturn dfs(n, &mem);\n}\n

\u89c2\u5bdf\u56fe 14-4 \uff0c\u7ecf\u8fc7\u8bb0\u5fc6\u5316\u5904\u7406\u540e\uff0c\u6240\u6709\u91cd\u53e0\u5b50\u95ee\u9898\u90fd\u53ea\u9700\u88ab\u8ba1\u7b97\u4e00\u6b21\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u88ab\u4f18\u5316\u81f3 \\(O(n)\\) \uff0c\u8fd9\u662f\u4e00\u4e2a\u5de8\u5927\u7684\u98de\u8dc3\u3002

\u56fe 14-4 \u00a0 \u8bb0\u5fc6\u5316\u641c\u7d22\u5bf9\u5e94\u9012\u5f52\u6811

"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1413","title":"14.1.3 \u00a0 \u65b9\u6cd5\u4e09\uff1a\u52a8\u6001\u89c4\u5212","text":"

\u8bb0\u5fc6\u5316\u641c\u7d22\u662f\u4e00\u79cd\u201c\u4ece\u9876\u81f3\u5e95\u201d\u7684\u65b9\u6cd5\uff1a\u6211\u4eec\u4ece\u539f\u95ee\u9898\uff08\u6839\u8282\u70b9\uff09\u5f00\u59cb\uff0c\u9012\u5f52\u5730\u5c06\u8f83\u5927\u5b50\u95ee\u9898\u5206\u89e3\u4e3a\u8f83\u5c0f\u5b50\u95ee\u9898\uff0c\u76f4\u81f3\u89e3\u5df2\u77e5\u7684\u6700\u5c0f\u5b50\u95ee\u9898\uff08\u53f6\u8282\u70b9\uff09\u3002\u4e4b\u540e\uff0c\u901a\u8fc7\u56de\u6eaf\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u9010\u5c42\u6536\u96c6\uff0c\u6784\u5efa\u51fa\u539f\u95ee\u9898\u7684\u89e3\u3002

\u4e0e\u4e4b\u76f8\u53cd\uff0c\u52a8\u6001\u89c4\u5212\u662f\u4e00\u79cd\u201c\u4ece\u5e95\u81f3\u9876\u201d\u7684\u65b9\u6cd5\uff1a\u4ece\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\u5f00\u59cb\uff0c\u8fed\u4ee3\u5730\u6784\u5efa\u66f4\u5927\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u76f4\u81f3\u5f97\u5230\u539f\u95ee\u9898\u7684\u89e3\u3002

\u7531\u4e8e\u52a8\u6001\u89c4\u5212\u4e0d\u5305\u542b\u56de\u6eaf\u8fc7\u7a0b\uff0c\u56e0\u6b64\u53ea\u9700\u4f7f\u7528\u5faa\u73af\u8fed\u4ee3\u5b9e\u73b0\uff0c\u65e0\u987b\u4f7f\u7528\u9012\u5f52\u3002\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521d\u59cb\u5316\u4e00\u4e2a\u6570\u7ec4 dp \u6765\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5b83\u8d77\u5230\u4e86\u8bb0\u5fc6\u5316\u641c\u7d22\u4e2d\u6570\u7ec4 mem \u76f8\u540c\u7684\u8bb0\u5f55\u4f5c\u7528\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_dp.py
def climbing_stairs_dp(n: int) -> int:\n\"\"\"\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nif n == 1 or n == 2:\nreturn n\n# \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\ndp = [0] * (n + 1)\n# \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1], dp[2] = 1, 2\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in range(3, n + 1):\ndp[i] = dp[i - 1] + dp[i - 2]\nreturn dp[n]\n
climbing_stairs_dp.cpp
/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\nif (n == 1 || n == 2)\nreturn n;\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvector<int> dp(n + 1);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\nreturn dp[n];\n}\n
climbing_stairs_dp.java
/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\nif (n == 1 || n == 2)\nreturn n;\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nint[] dp = new int[n + 1];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\nreturn dp[n];\n}\n
climbing_stairs_dp.cs
/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint ClimbingStairsDP(int n) {\nif (n == 1 || n == 2)\nreturn n;\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nint[] dp = new int[n + 1];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\nreturn dp[n];\n}\n
climbing_stairs_dp.go
/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDP(n int) int {\nif n == 1 || n == 2 {\nreturn n\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\ndp := make([]int, n+1)\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1\ndp[2] = 2\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i := 3; i <= n; i++ {\ndp[i] = dp[i-1] + dp[i-2]\n}\nreturn dp[n]\n}\n
climbing_stairs_dp.swift
/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDP(n: Int) -> Int {\nif n == 1 || n == 2 {\nreturn n\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvar dp = Array(repeating: 0, count: n + 1)\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1\ndp[2] = 2\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in stride(from: 3, through: n, by: 1) {\ndp[i] = dp[i - 1] + dp[i - 2]\n}\nreturn dp[n]\n}\n
climbing_stairs_dp.js
/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDP(n) {\nif (n === 1 || n === 2) return n;\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nconst dp = new Array(n + 1).fill(-1);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (let i = 3; i <= n; i++) {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\nreturn dp[n];\n}\n
climbing_stairs_dp.ts
/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDP(n: number): number {\nif (n === 1 || n === 2) return n;\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nconst dp = new Array(n + 1).fill(-1);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (let i = 3; i <= n; i++) {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\nreturn dp[n];\n}\n
climbing_stairs_dp.dart
/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\nif (n == 1 || n == 2) return n;\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nList<int> dp = List.filled(n + 1, 0);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\nreturn dp[n];\n}\n
climbing_stairs_dp.rs
/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfn climbing_stairs_dp(n: usize) -> i32 {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif n == 1 || n == 2 { return n as i32; }\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nlet mut dp = vec![-1; n + 1];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in 3..=n {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\ndp[n]\n}\n
climbing_stairs_dp.c
/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\nif (n == 1 || n == 2)\nreturn n;\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nint *dp = (int *)malloc((n + 1) * sizeof(int));\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\nint result = dp[n];\nfree(dp);\nreturn result;\n}\n
climbing_stairs_dp.zig
// \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212\nfn climbingStairsDP(comptime n: usize) i32 {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (n == 1 or n == 2) {\nreturn @intCast(n);\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvar dp = [_]i32{-1} ** (n + 1);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (3..n + 1) |i| {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\nreturn dp[n];\n}\n

\u56fe 14-5 \u6a21\u62df\u4e86\u4ee5\u4e0a\u4ee3\u7801\u7684\u6267\u884c\u8fc7\u7a0b\u3002

\u56fe 14-5 \u00a0 \u722c\u697c\u68af\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

\u4e0e\u56de\u6eaf\u7b97\u6cd5\u4e00\u6837\uff0c\u52a8\u6001\u89c4\u5212\u4e5f\u4f7f\u7528\u201c\u72b6\u6001\u201d\u6982\u5ff5\u6765\u8868\u793a\u95ee\u9898\u6c42\u89e3\u7684\u67d0\u4e2a\u7279\u5b9a\u9636\u6bb5\uff0c\u6bcf\u4e2a\u72b6\u6001\u90fd\u5bf9\u5e94\u4e00\u4e2a\u5b50\u95ee\u9898\u4ee5\u53ca\u76f8\u5e94\u7684\u5c40\u90e8\u6700\u4f18\u89e3\u3002\u4f8b\u5982\uff0c\u722c\u697c\u68af\u95ee\u9898\u7684\u72b6\u6001\u5b9a\u4e49\u4e3a\u5f53\u524d\u6240\u5728\u697c\u68af\u9636\u6570 \\(i\\) \u3002

\u6839\u636e\u4ee5\u4e0a\u5185\u5bb9\uff0c\u6211\u4eec\u53ef\u4ee5\u603b\u7ed3\u51fa\u52a8\u6001\u89c4\u5212\u7684\u5e38\u7528\u672f\u8bed\u3002

  • \u5c06\u6570\u7ec4 dp \u79f0\u4e3a\u300c\\(dp\\) \u8868\u300d\uff0c\\(dp[i]\\) \u8868\u793a\u72b6\u6001 \\(i\\) \u5bf9\u5e94\u5b50\u95ee\u9898\u7684\u89e3\u3002
  • \u5c06\u6700\u5c0f\u5b50\u95ee\u9898\u5bf9\u5e94\u7684\u72b6\u6001\uff08\u5373\u7b2c \\(1\\) \u548c \\(2\\) \u9636\u697c\u68af\uff09\u79f0\u4e3a\u300c\u521d\u59cb\u72b6\u6001\u300d\u3002
  • \u5c06\u9012\u63a8\u516c\u5f0f \\(dp[i] = dp[i-1] + dp[i-2]\\) \u79f0\u4e3a\u300c\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u300d\u3002
"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1414","title":"14.1.4 \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

\u7ec6\u5fc3\u7684\u4f60\u53ef\u80fd\u53d1\u73b0\uff0c\u7531\u4e8e \\(dp[i]\\) \u53ea\u4e0e \\(dp[i-1]\\) \u548c \\(dp[i-2]\\) \u6709\u5173\uff0c\u56e0\u6b64\u6211\u4eec\u65e0\u987b\u4f7f\u7528\u4e00\u4e2a\u6570\u7ec4 dp \u6765\u5b58\u50a8\u6240\u6709\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u800c\u53ea\u9700\u4e24\u4e2a\u53d8\u91cf\u6eda\u52a8\u524d\u8fdb\u5373\u53ef\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_dp.py
def climbing_stairs_dp_comp(n: int) -> int:\n\"\"\"\u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\nif n == 1 or n == 2:\nreturn n\na, b = 1, 2\nfor _ in range(3, n + 1):\na, b = b, a + b\nreturn b\n
climbing_stairs_dp.cpp
/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\nif (n == 1 || n == 2)\nreturn n;\nint a = 1, b = 2;\nfor (int i = 3; i <= n; i++) {\nint tmp = b;\nb = a + b;\na = tmp;\n}\nreturn b;\n}\n
climbing_stairs_dp.java
/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\nif (n == 1 || n == 2)\nreturn n;\nint a = 1, b = 2;\nfor (int i = 3; i <= n; i++) {\nint tmp = b;\nb = a + b;\na = tmp;\n}\nreturn b;\n}\n
climbing_stairs_dp.cs
/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint ClimbingStairsDPComp(int n) {\nif (n == 1 || n == 2)\nreturn n;\nint a = 1, b = 2;\nfor (int i = 3; i <= n; i++) {\nint tmp = b;\nb = a + b;\na = tmp;\n}\nreturn b;\n}\n
climbing_stairs_dp.go
/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDPComp(n int) int {\nif n == 1 || n == 2 {\nreturn n\n}\na, b := 1, 2\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i := 3; i <= n; i++ {\na, b = b, a+b\n}\nreturn b\n}\n
climbing_stairs_dp.swift
/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDPComp(n: Int) -> Int {\nif n == 1 || n == 2 {\nreturn n\n}\nvar a = 1\nvar b = 2\nfor _ in stride(from: 3, through: n, by: 1) {\n(a, b) = (b, a + b)\n}\nreturn b\n}\n
climbing_stairs_dp.js
/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDPComp(n) {\nif (n === 1 || n === 2) return n;\nlet a = 1,\nb = 2;\nfor (let i = 3; i <= n; i++) {\nconst tmp = b;\nb = a + b;\na = tmp;\n}\nreturn b;\n}\n
climbing_stairs_dp.ts
/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDPComp(n: number): number {\nif (n === 1 || n === 2) return n;\nlet a = 1,\nb = 2;\nfor (let i = 3; i <= n; i++) {\nconst tmp = b;\nb = a + b;\na = tmp;\n}\nreturn b;\n}\n
climbing_stairs_dp.dart
/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\nif (n == 1 || n == 2) return n;\nint a = 1, b = 2;\nfor (int i = 3; i <= n; i++) {\nint tmp = b;\nb = a + b;\na = tmp;\n}\nreturn b;\n}\n
climbing_stairs_dp.rs
/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn climbing_stairs_dp_comp(n: usize) -> i32 {\nif n == 1 || n == 2 { return n as i32; }\nlet (mut a, mut b) = (1, 2);\nfor _ in 3..=n {\nlet tmp = b;\nb = a + b;\na = tmp;\n}\nb\n}\n
climbing_stairs_dp.c
/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\nif (n == 1 || n == 2)\nreturn n;\nint a = 1, b = 2;\nfor (int i = 3; i <= n; i++) {\nint tmp = b;\nb = a + b;\na = tmp;\n}\nreturn b;\n}\n
climbing_stairs_dp.zig
// \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn climbingStairsDPComp(comptime n: usize) i32 {\nif (n == 1 or n == 2) {\nreturn @intCast(n);\n}\nvar a: i32 = 1;\nvar b: i32 = 2;\nfor (3..n + 1) |_| {\nvar tmp = b;\nb = a + b;\na = tmp;\n}\nreturn b;\n}\n

\u89c2\u5bdf\u4ee5\u4e0a\u4ee3\u7801\uff0c\u7531\u4e8e\u7701\u53bb\u4e86\u6570\u7ec4 dp \u5360\u7528\u7684\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n)\\) \u964d\u4f4e\u81f3 \\(O(1)\\) \u3002

\u5728\u52a8\u6001\u89c4\u5212\u95ee\u9898\u4e2d\uff0c\u5f53\u524d\u72b6\u6001\u5f80\u5f80\u4ec5\u4e0e\u524d\u9762\u6709\u9650\u4e2a\u72b6\u6001\u6709\u5173\uff0c\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u53ea\u4fdd\u7559\u5fc5\u8981\u7684\u72b6\u6001\uff0c\u901a\u8fc7\u201c\u964d\u7ef4\u201d\u6765\u8282\u7701\u5185\u5b58\u7a7a\u95f4\u3002\u8fd9\u79cd\u7a7a\u95f4\u4f18\u5316\u6280\u5de7\u88ab\u79f0\u4e3a\u201c\u6eda\u52a8\u53d8\u91cf\u201d\u6216\u201c\u6eda\u52a8\u6570\u7ec4\u201d\u3002

"},{"location":"chapter_dynamic_programming/knapsack_problem/","title":"14.4 \u00a0 0-1 \u80cc\u5305\u95ee\u9898","text":"

\u80cc\u5305\u95ee\u9898\u662f\u4e00\u4e2a\u975e\u5e38\u597d\u7684\u52a8\u6001\u89c4\u5212\u5165\u95e8\u9898\u76ee\uff0c\u662f\u52a8\u6001\u89c4\u5212\u4e2d\u6700\u5e38\u89c1\u7684\u95ee\u9898\u5f62\u5f0f\u3002\u5176\u5177\u6709\u5f88\u591a\u53d8\u79cd\uff0c\u4f8b\u5982 0-1 \u80cc\u5305\u95ee\u9898\u3001\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u3001\u591a\u91cd\u80cc\u5305\u95ee\u9898\u7b49\u3002

\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5148\u6765\u6c42\u89e3\u6700\u5e38\u89c1\u7684 0-1 \u80cc\u5305\u95ee\u9898\u3002

Question

\u7ed9\u5b9a \\(n\\) \u4e2a\u7269\u54c1\uff0c\u7b2c \\(i\\) \u4e2a\u7269\u54c1\u7684\u91cd\u91cf\u4e3a \\(wgt[i-1]\\)\u3001\u4ef7\u503c\u4e3a \\(val[i-1]\\) \uff0c\u548c\u4e00\u4e2a\u5bb9\u91cf\u4e3a \\(cap\\) \u7684\u80cc\u5305\u3002\u6bcf\u4e2a\u7269\u54c1\u53ea\u80fd\u9009\u62e9\u4e00\u6b21\uff0c\u95ee\u5728\u4e0d\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\u4e0b\u80fd\u653e\u5165\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u3002

\u89c2\u5bdf\u56fe 14-17 \uff0c\u7531\u4e8e\u7269\u54c1\u7f16\u53f7 \\(i\\) \u4ece \\(1\\) \u5f00\u59cb\u8ba1\u6570\uff0c\u6570\u7ec4\u7d22\u5f15\u4ece \\(0\\) \u5f00\u59cb\u8ba1\u6570\uff0c\u56e0\u6b64\u7269\u54c1 \\(i\\) \u5bf9\u5e94\u91cd\u91cf \\(wgt[i-1]\\) \u548c\u4ef7\u503c \\(val[i-1]\\) \u3002

\u56fe 14-17 \u00a0 0-1 \u80cc\u5305\u7684\u793a\u4f8b\u6570\u636e

\u6211\u4eec\u53ef\u4ee5\u5c06 0-1 \u80cc\u5305\u95ee\u9898\u770b\u4f5c\u662f\u4e00\u4e2a\u7531 \\(n\\) \u8f6e\u51b3\u7b56\u7ec4\u6210\u7684\u8fc7\u7a0b\uff0c\u6bcf\u4e2a\u7269\u4f53\u90fd\u6709\u4e0d\u653e\u5165\u548c\u653e\u5165\u4e24\u79cd\u51b3\u7b56\uff0c\u56e0\u6b64\u8be5\u95ee\u9898\u662f\u6ee1\u8db3\u51b3\u7b56\u6811\u6a21\u578b\u7684\u3002

\u8be5\u95ee\u9898\u7684\u76ee\u6807\u662f\u6c42\u89e3\u201c\u5728\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u7684\u6700\u5927\u4ef7\u503c\u201d\uff0c\u56e0\u6b64\u8f83\u5927\u6982\u7387\u662f\u4e2a\u52a8\u6001\u89c4\u5212\u95ee\u9898\u3002

\u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868

\u5bf9\u4e8e\u6bcf\u4e2a\u7269\u54c1\u6765\u8bf4\uff0c\u4e0d\u653e\u5165\u80cc\u5305\uff0c\u80cc\u5305\u5bb9\u91cf\u4e0d\u53d8\uff1b\u653e\u5165\u80cc\u5305\uff0c\u80cc\u5305\u5bb9\u91cf\u51cf\u5c0f\u3002\u7531\u6b64\u53ef\u5f97\u72b6\u6001\u5b9a\u4e49\uff1a\u5f53\u524d\u7269\u54c1\u7f16\u53f7 \\(i\\) \u548c\u5269\u4f59\u80cc\u5305\u5bb9\u91cf \\(c\\) \uff0c\u8bb0\u4e3a \\([i, c]\\) \u3002

\u72b6\u6001 \\([i, c]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u4e3a\uff1a\u524d \\(i\\) \u4e2a\u7269\u54c1\u5728\u5269\u4f59\u5bb9\u91cf\u4e3a \\(c\\) \u7684\u80cc\u5305\u4e2d\u7684\u6700\u5927\u4ef7\u503c\uff0c\u8bb0\u4e3a \\(dp[i, c]\\) \u3002

\u5f85\u6c42\u89e3\u7684\u662f \\(dp[n, cap]\\) \uff0c\u56e0\u6b64\u9700\u8981\u4e00\u4e2a\u5c3a\u5bf8\u4e3a \\((n+1) \\times (cap+1)\\) \u7684\u4e8c\u7ef4 \\(dp\\) \u8868\u3002

\u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b

\u5f53\u6211\u4eec\u505a\u51fa\u7269\u54c1 \\(i\\) \u7684\u51b3\u7b56\u540e\uff0c\u5269\u4f59\u7684\u662f\u524d \\(i-1\\) \u4e2a\u7269\u54c1\u7684\u51b3\u7b56\uff0c\u53ef\u5206\u4e3a\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\u3002

  • \u4e0d\u653e\u5165\u7269\u54c1 \\(i\\) \uff1a\u80cc\u5305\u5bb9\u91cf\u4e0d\u53d8\uff0c\u72b6\u6001\u53d8\u5316\u4e3a \\([i-1, c]\\) \u3002
  • \u653e\u5165\u7269\u54c1 \\(i\\) \uff1a\u80cc\u5305\u5bb9\u91cf\u51cf\u5c0f \\(wgt[i-1]\\) \uff0c\u4ef7\u503c\u589e\u52a0 \\(val[i-1]\\) \uff0c\u72b6\u6001\u53d8\u5316\u4e3a \\([i-1, c-wgt[i-1]]\\) \u3002

\u4e0a\u8ff0\u5206\u6790\u5411\u6211\u4eec\u63ed\u793a\u4e86\u672c\u9898\u7684\u6700\u4f18\u5b50\u7ed3\u6784\uff1a\u6700\u5927\u4ef7\u503c \\(dp[i, c]\\) \u7b49\u4e8e\u4e0d\u653e\u5165\u7269\u54c1 \\(i\\) \u548c\u653e\u5165\u7269\u54c1 \\(i\\) \u4e24\u79cd\u65b9\u6848\u4e2d\u7684\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\u3002\u7531\u6b64\u53ef\u63a8\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff1a

\\[ dp[i, c] = \\max(dp[i-1, c], dp[i-1, c - wgt[i-1]] + val[i-1]) \\]

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u82e5\u5f53\u524d\u7269\u54c1\u91cd\u91cf \\(wgt[i - 1]\\) \u8d85\u51fa\u5269\u4f59\u80cc\u5305\u5bb9\u91cf \\(c\\) \uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\u3002

\u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f

\u5f53\u65e0\u7269\u54c1\u6216\u65e0\u5269\u4f59\u80cc\u5305\u5bb9\u91cf\u65f6\u6700\u5927\u4ef7\u503c\u4e3a \\(0\\) \uff0c\u5373\u9996\u5217 \\(dp[i, 0]\\) \u548c\u9996\u884c \\(dp[0, c]\\) \u90fd\u7b49\u4e8e \\(0\\) \u3002

\u5f53\u524d\u72b6\u6001 \\([i, c]\\) \u4ece\u4e0a\u65b9\u7684\u72b6\u6001 \\([i-1, c]\\) \u548c\u5de6\u4e0a\u65b9\u7684\u72b6\u6001 \\([i-1, c-wgt[i-1]]\\) \u8f6c\u79fb\u800c\u6765\uff0c\u56e0\u6b64\u901a\u8fc7\u4e24\u5c42\u5faa\u73af\u6b63\u5e8f\u904d\u5386\u6574\u4e2a \\(dp\\) \u8868\u5373\u53ef\u3002

\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u6211\u4eec\u63a5\u4e0b\u6765\u6309\u987a\u5e8f\u5b9e\u73b0\u66b4\u529b\u641c\u7d22\u3001\u8bb0\u5fc6\u5316\u641c\u7d22\u3001\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\u3002

"},{"location":"chapter_dynamic_programming/knapsack_problem/#1","title":"1. \u00a0 \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u641c\u7d22","text":"

\u641c\u7d22\u4ee3\u7801\u5305\u542b\u4ee5\u4e0b\u8981\u7d20\u3002

  • \u9012\u5f52\u53c2\u6570\uff1a\u72b6\u6001 \\([i, c]\\) \u3002
  • \u8fd4\u56de\u503c\uff1a\u5b50\u95ee\u9898\u7684\u89e3 \\(dp[i, c]\\) \u3002
  • \u7ec8\u6b62\u6761\u4ef6\uff1a\u5f53\u7269\u54c1\u7f16\u53f7\u8d8a\u754c \\(i = 0\\) \u6216\u80cc\u5305\u5269\u4f59\u5bb9\u91cf\u4e3a \\(0\\) \u65f6\uff0c\u7ec8\u6b62\u9012\u5f52\u5e76\u8fd4\u56de\u4ef7\u503c \\(0\\) \u3002
  • \u526a\u679d\uff1a\u82e5\u5f53\u524d\u7269\u54c1\u91cd\u91cf\u8d85\u51fa\u80cc\u5305\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig knapsack.py
def knapsack_dfs(wgt: list[int], val: list[int], i: int, c: int) -> int:\n\"\"\"0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22\"\"\"\n# \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif i == 0 or c == 0:\nreturn 0\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif wgt[i - 1] > c:\nreturn knapsack_dfs(wgt, val, i - 1, c)\n# \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nno = knapsack_dfs(wgt, val, i - 1, c)\nyes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1]\n# \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn max(no, yes)\n
knapsack.cpp
/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(vector<int> &wgt, vector<int> &val, int i, int c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFS(wgt, val, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = knapsackDFS(wgt, val, i - 1, c);\nint yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn max(no, yes);\n}\n
knapsack.java
/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(int[] wgt, int[] val, int i, int c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFS(wgt, val, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = knapsackDFS(wgt, val, i - 1, c);\nint yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn Math.max(no, yes);\n}\n
knapsack.cs
/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint KnapsackDFS(int[] weight, int[] val, int i, int c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (weight[i - 1] > c) {\nreturn KnapsackDFS(weight, val, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = KnapsackDFS(weight, val, i - 1, c);\nint yes = KnapsackDFS(weight, val, i - 1, c - weight[i - 1]) + val[i - 1];\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn Math.Max(no, yes);\n}\n
knapsack.go
/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc knapsackDFS(wgt, val []int, i, c int) int {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif i == 0 || c == 0 {\nreturn 0\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif wgt[i-1] > c {\nreturn knapsackDFS(wgt, val, i-1, c)\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nno := knapsackDFS(wgt, val, i-1, c)\nyes := knapsackDFS(wgt, val, i-1, c-wgt[i-1]) + val[i-1]\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn int(math.Max(float64(no), float64(yes)))\n}\n
knapsack.swift
/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc knapsackDFS(wgt: [Int], val: [Int], i: Int, c: Int) -> Int {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif i == 0 || c == 0 {\nreturn 0\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif wgt[i - 1] > c {\nreturn knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c)\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nlet no = knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c)\nlet yes = knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c - wgt[i - 1]) + val[i - 1]\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn max(no, yes)\n}\n
knapsack.js
/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunction knapsackDFS(wgt, val, i, c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i === 0 || c === 0) {\nreturn 0;\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFS(wgt, val, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nconst no = knapsackDFS(wgt, val, i - 1, c);\nconst yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn Math.max(no, yes);\n}\n
knapsack.ts
/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunction knapsackDFS(\nwgt: Array<number>,\nval: Array<number>,\ni: number,\nc: number\n): number {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i === 0 || c === 0) {\nreturn 0;\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFS(wgt, val, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nconst no = knapsackDFS(wgt, val, i - 1, c);\nconst yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn Math.max(no, yes);\n}\n
knapsack.dart
/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(List<int> wgt, List<int> val, int i, int c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFS(wgt, val, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = knapsackDFS(wgt, val, i - 1, c);\nint yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn max(no, yes);\n}\n
knapsack.rs
/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfn knapsack_dfs(wgt: &[i32], val: &[i32], i: usize, c: usize) -> i32 {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif i == 0 || c == 0 {\nreturn 0;\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif wgt[i - 1] > c as i32 {\nreturn knapsack_dfs(wgt, val, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nlet no = knapsack_dfs(wgt, val, i - 1, c);\nlet yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1] as usize) + val[i - 1];\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nstd::cmp::max(no, yes)\n}\n
knapsack.c
/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(int wgt[], int val[], int i, int c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFS(wgt, val, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = knapsackDFS(wgt, val, i - 1, c);\nint yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn max(no, yes);\n}\n
knapsack.zig
// 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22\nfn knapsackDFS(wgt: []i32, val: []i32, i: usize, c: usize) i32 {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 or c == 0) {\nreturn 0;\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFS(wgt, val, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nvar no = knapsackDFS(wgt, val, i - 1, c);\nvar yes = knapsackDFS(wgt, val, i - 1, c - @as(usize, @intCast(wgt[i - 1]))) + val[i - 1];\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn @max(no, yes);\n}\n

\u5982\u56fe 14-18 \u6240\u793a\uff0c\u7531\u4e8e\u6bcf\u4e2a\u7269\u54c1\u90fd\u4f1a\u4ea7\u751f\u4e0d\u9009\u548c\u9009\u4e24\u6761\u641c\u7d22\u5206\u652f\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \u3002

\u89c2\u5bdf\u9012\u5f52\u6811\uff0c\u5bb9\u6613\u53d1\u73b0\u5176\u4e2d\u5b58\u5728\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u4f8b\u5982 \\(dp[1, 10]\\) \u7b49\u3002\u800c\u5f53\u7269\u54c1\u8f83\u591a\u3001\u80cc\u5305\u5bb9\u91cf\u8f83\u5927\uff0c\u5c24\u5176\u662f\u76f8\u540c\u91cd\u91cf\u7684\u7269\u54c1\u8f83\u591a\u65f6\uff0c\u91cd\u53e0\u5b50\u95ee\u9898\u7684\u6570\u91cf\u5c06\u4f1a\u5927\u5e45\u589e\u591a\u3002

\u56fe 14-18 \u00a0 0-1 \u80cc\u5305\u7684\u66b4\u529b\u641c\u7d22\u9012\u5f52\u6811

"},{"location":"chapter_dynamic_programming/knapsack_problem/#2","title":"2. \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22","text":"

\u4e3a\u4e86\u4fdd\u8bc1\u91cd\u53e0\u5b50\u95ee\u9898\u53ea\u88ab\u8ba1\u7b97\u4e00\u6b21\uff0c\u6211\u4eec\u501f\u52a9\u8bb0\u5fc6\u5217\u8868 mem \u6765\u8bb0\u5f55\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5176\u4e2d mem[i][c] \u5bf9\u5e94 \\(dp[i, c]\\) \u3002

\u5f15\u5165\u8bb0\u5fc6\u5316\u4e4b\u540e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u5b50\u95ee\u9898\u6570\u91cf\uff0c\u4e5f\u5c31\u662f \\(O(n \\times cap)\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig knapsack.py
def knapsack_dfs_mem(\nwgt: list[int], val: list[int], mem: list[list[int]], i: int, c: int\n) -> int:\n\"\"\"0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n# \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif i == 0 or c == 0:\nreturn 0\n# \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif mem[i][c] != -1:\nreturn mem[i][c]\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif wgt[i - 1] > c:\nreturn knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n# \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nno = knapsack_dfs_mem(wgt, val, mem, i - 1, c)\nyes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1]\n# \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = max(no, yes)\nreturn mem[i][c]\n
knapsack.cpp
/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(vector<int> &wgt, vector<int> &val, vector<vector<int>> &mem, int i, int c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][c] != -1) {\nreturn mem[i][c];\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFSMem(wgt, val, mem, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = knapsackDFSMem(wgt, val, mem, i - 1, c);\nint yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = max(no, yes);\nreturn mem[i][c];\n}\n
knapsack.java
/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(int[] wgt, int[] val, int[][] mem, int i, int c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][c] != -1) {\nreturn mem[i][c];\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFSMem(wgt, val, mem, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = knapsackDFSMem(wgt, val, mem, i - 1, c);\nint yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = Math.max(no, yes);\nreturn mem[i][c];\n}\n
knapsack.cs
/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint KnapsackDFSMem(int[] weight, int[] val, int[][] mem, int i, int c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][c] != -1) {\nreturn mem[i][c];\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (weight[i - 1] > c) {\nreturn KnapsackDFSMem(weight, val, mem, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = KnapsackDFSMem(weight, val, mem, i - 1, c);\nint yes = KnapsackDFSMem(weight, val, mem, i - 1, c - weight[i - 1]) + val[i - 1];\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = Math.Max(no, yes);\nreturn mem[i][c];\n}\n
knapsack.go
/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc knapsackDFSMem(wgt, val []int, mem [][]int, i, c int) int {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif i == 0 || c == 0 {\nreturn 0\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif mem[i][c] != -1 {\nreturn mem[i][c]\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif wgt[i-1] > c {\nreturn knapsackDFSMem(wgt, val, mem, i-1, c)\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nno := knapsackDFSMem(wgt, val, mem, i-1, c)\nyes := knapsackDFSMem(wgt, val, mem, i-1, c-wgt[i-1]) + val[i-1]\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = int(math.Max(float64(no), float64(yes)))\nreturn mem[i][c]\n}\n
knapsack.swift
/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc knapsackDFSMem(wgt: [Int], val: [Int], mem: inout [[Int]], i: Int, c: Int) -> Int {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif i == 0 || c == 0 {\nreturn 0\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif mem[i][c] != -1 {\nreturn mem[i][c]\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif wgt[i - 1] > c {\nreturn knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c)\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nlet no = knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c)\nlet yes = knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c - wgt[i - 1]) + val[i - 1]\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = max(no, yes)\nreturn mem[i][c]\n}\n
knapsack.js
/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction knapsackDFSMem(wgt, val, mem, i, c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i === 0 || c === 0) {\nreturn 0;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][c] !== -1) {\nreturn mem[i][c];\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFSMem(wgt, val, mem, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nconst no = knapsackDFSMem(wgt, val, mem, i - 1, c);\nconst yes =\nknapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = Math.max(no, yes);\nreturn mem[i][c];\n}\n
knapsack.ts
/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction knapsackDFSMem(\nwgt: Array<number>,\nval: Array<number>,\nmem: Array<Array<number>>,\ni: number,\nc: number\n): number {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i === 0 || c === 0) {\nreturn 0;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][c] !== -1) {\nreturn mem[i][c];\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFSMem(wgt, val, mem, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nconst no = knapsackDFSMem(wgt, val, mem, i - 1, c);\nconst yes =\nknapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = Math.max(no, yes);\nreturn mem[i][c];\n}\n
knapsack.dart
/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(\nList<int> wgt,\nList<int> val,\nList<List<int>> mem,\nint i,\nint c,\n) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][c] != -1) {\nreturn mem[i][c];\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFSMem(wgt, val, mem, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = knapsackDFSMem(wgt, val, mem, i - 1, c);\nint yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = max(no, yes);\nreturn mem[i][c];\n}\n
knapsack.rs
/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn knapsack_dfs_mem(wgt: &[i32], val: &[i32], mem: &mut Vec<Vec<i32>>, i: usize, c: usize) -> i32 {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif i == 0 || c == 0 {\nreturn 0;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif mem[i][c] != -1 {\nreturn mem[i][c];\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif wgt[i - 1] > c as i32 {\nreturn knapsack_dfs_mem(wgt, val, mem, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nlet no = knapsack_dfs_mem(wgt, val, mem, i - 1, c);\nlet yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1] as usize) + val[i - 1];\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = std::cmp::max(no, yes);\nmem[i][c]\n}\n
knapsack.c
/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(int wgt[], int val[], int memCols, int mem[][memCols], int i, int c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][c] != -1) {\nreturn mem[i][c];\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFSMem(wgt, val, memCols, mem, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = knapsackDFSMem(wgt, val, memCols, mem, i - 1, c);\nint yes = knapsackDFSMem(wgt, val, memCols, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = max(no, yes);\nreturn mem[i][c];\n}\n
knapsack.zig
// 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\nfn knapsackDFSMem(wgt: []i32, val: []i32, mem: anytype, i: usize, c: usize) i32 {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 or c == 0) {\nreturn 0;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][c] != -1) {\nreturn mem[i][c];\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFSMem(wgt, val, mem, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nvar no = knapsackDFSMem(wgt, val, mem, i - 1, c);\nvar yes = knapsackDFSMem(wgt, val, mem, i - 1, c - @as(usize, @intCast(wgt[i - 1]))) + val[i - 1];\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = @max(no, yes);\nreturn mem[i][c];\n}\n

\u56fe 14-19 \u5c55\u793a\u4e86\u5728\u8bb0\u5fc6\u5316\u9012\u5f52\u4e2d\u88ab\u526a\u6389\u7684\u641c\u7d22\u5206\u652f\u3002

\u56fe 14-19 \u00a0 0-1 \u80cc\u5305\u7684\u8bb0\u5fc6\u5316\u641c\u7d22\u9012\u5f52\u6811

"},{"location":"chapter_dynamic_programming/knapsack_problem/#3","title":"3. \u00a0 \u65b9\u6cd5\u4e09\uff1a\u52a8\u6001\u89c4\u5212","text":"

\u52a8\u6001\u89c4\u5212\u5b9e\u8d28\u4e0a\u5c31\u662f\u5728\u72b6\u6001\u8f6c\u79fb\u4e2d\u586b\u5145 \\(dp\\) \u8868\u7684\u8fc7\u7a0b\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig knapsack.py
def knapsack_dp(wgt: list[int], val: list[int], cap: int) -> int:\n\"\"\"0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(wgt)\n# \u521d\u59cb\u5316 dp \u8868\ndp = [[0] * (cap + 1) for _ in range(n + 1)]\n# \u72b6\u6001\u8f6c\u79fb\nfor i in range(1, n + 1):\nfor c in range(1, cap + 1):\nif wgt[i - 1] > c:\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c]\nelse:\n# \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1])\nreturn dp[n][cap]\n
knapsack.cpp
/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(vector<int> &wgt, vector<int> &val, int cap) {\nint n = wgt.size();\n// \u521d\u59cb\u5316 dp \u8868\nvector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n
knapsack.java
/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(int[] wgt, int[] val, int cap) {\nint n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nint[][] dp = new int[n + 1][cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = Math.max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n
knapsack.cs
/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint KnapsackDP(int[] weight, int[] val, int cap) {\nint n = weight.Length;\n// \u521d\u59cb\u5316 dp \u8868\nint[,] dp = new int[n + 1, cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (weight[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i, c] = dp[i - 1, c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i, c] = Math.Max(dp[i - 1, c - weight[i - 1]] + val[i - 1], dp[i - 1, c]);\n}\n}\n}\nreturn dp[n, cap];\n}\n
knapsack.go
/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDP(wgt, val []int, cap int) int {\nn := len(wgt)\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([][]int, n+1)\nfor i := 0; i <= n; i++ {\ndp[i] = make([]int, cap+1)\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor i := 1; i <= n; i++ {\nfor c := 1; c <= cap; c++ {\nif wgt[i-1] > c {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i-1][c]\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = int(math.Max(float64(dp[i-1][c]), float64(dp[i-1][c-wgt[i-1]]+val[i-1])))\n}\n}\n}\nreturn dp[n][cap]\n}\n
knapsack.swift
/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDP(wgt: [Int], val: [Int], cap: Int) -> Int {\nlet n = wgt.count\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)\n// \u72b6\u6001\u8f6c\u79fb\nfor i in stride(from: 1, through: n, by: 1) {\nfor c in stride(from: 1, through: cap, by: 1) {\nif wgt[i - 1] > c {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c]\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1])\n}\n}\n}\nreturn dp[n][cap]\n}\n
knapsack.js
/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction knapsackDP(wgt, val, cap) {\nconst n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array(n + 1)\n.fill(0)\n.map(() => Array(cap + 1).fill(0));\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\nfor (let c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = Math.max(\ndp[i - 1][c],\ndp[i - 1][c - wgt[i - 1]] + val[i - 1]\n);\n}\n}\n}\nreturn dp[n][cap];\n}\n
knapsack.ts
/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction knapsackDP(\nwgt: Array<number>,\nval: Array<number>,\ncap: number\n): number {\nconst n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: n + 1 }, () =>\nArray.from({ length: cap + 1 }, () => 0)\n);\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\nfor (let c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = Math.max(\ndp[i - 1][c],\ndp[i - 1][c - wgt[i - 1]] + val[i - 1]\n);\n}\n}\n}\nreturn dp[n][cap];\n}\n
knapsack.dart
/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(List<int> wgt, List<int> val, int cap) {\nint n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nList<List<int>> dp = List.generate(n + 1, (index) => List.filled(cap + 1, 0));\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n
knapsack.rs
/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfn knapsack_dp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\nlet n = wgt.len();\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![vec![0; cap + 1]; n + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor i in 1..=n {\nfor c in 1..=cap {\nif wgt[i - 1] > c as i32 {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = std::cmp::max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1] as usize] + val[i - 1]);\n}\n}\n}\ndp[n][cap]\n}\n
knapsack.c
/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(int wgt[], int val[], int cap, int wgtSize) {\nint n = wgtSize;\n// \u521d\u59cb\u5316 dp \u8868\nint dp[n + 1][cap + 1];\nmemset(dp, 0, sizeof(dp));\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n
knapsack.zig
// 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212\nfn knapsackDP(comptime wgt: []i32, val: []i32, comptime cap: usize) i32 {\ncomptime var n = wgt.len;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_][cap + 1]i32{[_]i32{0} ** (cap + 1)} ** (n + 1);\n// \u72b6\u6001\u8f6c\u79fb\nfor (1..n + 1) |i| {\nfor (1..cap + 1) |c| {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = @max(dp[i - 1][c], dp[i - 1][c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n

\u5982\u56fe 14-20 \u6240\u793a\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u90fd\u7531\u6570\u7ec4 dp \u5927\u5c0f\u51b3\u5b9a\uff0c\u5373 \\(O(n \\times cap)\\) \u3002

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

\u56fe 14-20 \u00a0 0-1 \u80cc\u5305\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

"},{"location":"chapter_dynamic_programming/knapsack_problem/#4","title":"4. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

\u7531\u4e8e\u6bcf\u4e2a\u72b6\u6001\u90fd\u53ea\u4e0e\u5176\u4e0a\u4e00\u884c\u7684\u72b6\u6001\u6709\u5173\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e24\u4e2a\u6570\u7ec4\u6eda\u52a8\u524d\u8fdb\uff0c\u5c06\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n^2)\\) \u5c06\u4f4e\u81f3 \\(O(n)\\) \u3002

\u8fdb\u4e00\u6b65\u601d\u8003\uff0c\u6211\u4eec\u662f\u5426\u53ef\u4ee5\u4ec5\u7528\u4e00\u4e2a\u6570\u7ec4\u5b9e\u73b0\u7a7a\u95f4\u4f18\u5316\u5462\uff1f\u89c2\u5bdf\u53ef\u77e5\uff0c\u6bcf\u4e2a\u72b6\u6001\u90fd\u662f\u7531\u6b63\u4e0a\u65b9\u6216\u5de6\u4e0a\u65b9\u7684\u683c\u5b50\u8f6c\u79fb\u8fc7\u6765\u7684\u3002\u5047\u8bbe\u53ea\u6709\u4e00\u4e2a\u6570\u7ec4\uff0c\u5f53\u5f00\u59cb\u904d\u5386\u7b2c \\(i\\) \u884c\u65f6\uff0c\u8be5\u6570\u7ec4\u5b58\u50a8\u7684\u4ecd\u7136\u662f\u7b2c \\(i-1\\) \u884c\u7684\u72b6\u6001\u3002

  • \u5982\u679c\u91c7\u53d6\u6b63\u5e8f\u904d\u5386\uff0c\u90a3\u4e48\u904d\u5386\u5230 \\(dp[i, j]\\) \u65f6\uff0c\u5de6\u4e0a\u65b9 \\(dp[i-1, 1]\\) ~ \\(dp[i-1, j-1]\\) \u503c\u53ef\u80fd\u5df2\u7ecf\u88ab\u8986\u76d6\uff0c\u6b64\u65f6\u5c31\u65e0\u6cd5\u5f97\u5230\u6b63\u786e\u7684\u72b6\u6001\u8f6c\u79fb\u7ed3\u679c\u3002
  • \u5982\u679c\u91c7\u53d6\u5012\u5e8f\u904d\u5386\uff0c\u5219\u4e0d\u4f1a\u53d1\u751f\u8986\u76d6\u95ee\u9898\uff0c\u72b6\u6001\u8f6c\u79fb\u53ef\u4ee5\u6b63\u786e\u8fdb\u884c\u3002

\u56fe 14-21 \u5c55\u793a\u4e86\u5728\u5355\u4e2a\u6570\u7ec4\u4e0b\u4ece\u7b2c \\(i = 1\\) \u884c\u8f6c\u6362\u81f3\u7b2c \\(i = 2\\) \u884c\u7684\u8fc7\u7a0b\u3002\u8bf7\u601d\u8003\u6b63\u5e8f\u904d\u5386\u548c\u5012\u5e8f\u904d\u5386\u7684\u533a\u522b\u3002

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

\u56fe 14-21 \u00a0 0-1 \u80cc\u5305\u7684\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

\u5728\u4ee3\u7801\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u4ec5\u9700\u5c06\u6570\u7ec4 dp \u7684\u7b2c\u4e00\u7ef4 \\(i\\) \u76f4\u63a5\u5220\u9664\uff0c\u5e76\u4e14\u628a\u5185\u5faa\u73af\u66f4\u6539\u4e3a\u5012\u5e8f\u904d\u5386\u5373\u53ef\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig knapsack.py
def knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -> int:\n\"\"\"0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(wgt)\n# \u521d\u59cb\u5316 dp \u8868\ndp = [0] * (cap + 1)\n# \u72b6\u6001\u8f6c\u79fb\nfor i in range(1, n + 1):\n# \u5012\u5e8f\u904d\u5386\nfor c in range(cap, 0, -1):\nif wgt[i - 1] > c:\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c]\nelse:\n# \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\nreturn dp[cap]\n
knapsack.cpp
/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {\nint n = wgt.size();\n// \u521d\u59cb\u5316 dp \u8868\nvector<int> dp(cap + 1, 0);\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\n// \u5012\u5e8f\u904d\u5386\nfor (int c = cap; c >= 1; c--) {\nif (wgt[i - 1] <= c) {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
knapsack.java
/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(int[] wgt, int[] val, int cap) {\nint n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\n// \u5012\u5e8f\u904d\u5386\nfor (int c = cap; c >= 1; c--) {\nif (wgt[i - 1] <= c) {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
knapsack.cs
/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint KnapsackDPComp(int[] weight, int[] val, int cap) {\nint n = weight.Length;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\n// \u5012\u5e8f\u904d\u5386\nfor (int c = cap; c > 0; c--) {\nif (weight[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = Math.Max(dp[c], dp[c - weight[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
knapsack.go
/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDPComp(wgt, val []int, cap int) int {\nn := len(wgt)\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([]int, cap+1)\n// \u72b6\u6001\u8f6c\u79fb\nfor i := 1; i <= n; i++ {\n// \u5012\u5e8f\u904d\u5386\nfor c := cap; c >= 1; c-- {\nif wgt[i-1] <= c {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = int(math.Max(float64(dp[c]), float64(dp[c-wgt[i-1]]+val[i-1])))\n}\n}\n}\nreturn dp[cap]\n}\n
knapsack.swift
/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDPComp(wgt: [Int], val: [Int], cap: Int) -> Int {\nlet n = wgt.count\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: 0, count: cap + 1)\n// \u72b6\u6001\u8f6c\u79fb\nfor i in stride(from: 1, through: n, by: 1) {\n// \u5012\u5e8f\u904d\u5386\nfor c in stride(from: cap, through: 1, by: -1) {\nif wgt[i - 1] <= c {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n}\n}\n}\nreturn dp[cap]\n}\n
knapsack.js
/* 0-1 \u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction knapsackDPComp(wgt, val, cap) {\nconst n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array(cap + 1).fill(0);\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\n// \u5012\u5e8f\u904d\u5386\nfor (let c = cap; c >= 1; c--) {\nif (wgt[i - 1] <= c) {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
knapsack.ts
/* 0-1 \u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction knapsackDPComp(\nwgt: Array<number>,\nval: Array<number>,\ncap: number\n): number {\nconst n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array(cap + 1).fill(0);\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\n// \u5012\u5e8f\u904d\u5386\nfor (let c = cap; c >= 1; c--) {\nif (wgt[i - 1] <= c) {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
knapsack.dart
/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(List<int> wgt, List<int> val, int cap) {\nint n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nList<int> dp = List.filled(cap + 1, 0);\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\n// \u5012\u5e8f\u904d\u5386\nfor (int c = cap; c >= 1; c--) {\nif (wgt[i - 1] <= c) {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
knapsack.rs
/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn knapsack_dp_comp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\nlet n = wgt.len();\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![0; cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor i in 1..=n {\n// \u5012\u5e8f\u904d\u5386\nfor c in (1..=cap).rev() {\nif wgt[i - 1] <= c as i32 {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = std::cmp::max(dp[c], dp[c - wgt[i - 1] as usize] + val[i - 1]);\n}\n}\n}\ndp[cap]\n}\n
knapsack.c
/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(int wgt[], int val[], int cap, int wgtSize) {\nint n = wgtSize;\n// \u521d\u59cb\u5316 dp \u8868\nint dp[cap + 1];\nmemset(dp, 0, sizeof(dp));\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\n// \u5012\u5e8f\u904d\u5386\nfor (int c = cap; c >= 1; c--) {\nif (wgt[i - 1] <= c) {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
knapsack.zig
// 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn knapsackDPComp(wgt: []i32, val: []i32, comptime cap: usize) i32 {\nvar n = wgt.len;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_]i32{0} ** (cap + 1);\n// \u72b6\u6001\u8f6c\u79fb\nfor (1..n + 1) |i| {\n// \u5012\u5e8f\u904d\u5386\nvar c = cap;\nwhile (c > 0) : (c -= 1) {\nif (wgt[i - 1] < c) {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = @max(dp[c], dp[c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
"},{"location":"chapter_dynamic_programming/summary/","title":"14.7 \u00a0 \u5c0f\u7ed3","text":"
  • \u52a8\u6001\u89c4\u5212\u5bf9\u95ee\u9898\u8fdb\u884c\u5206\u89e3\uff0c\u5e76\u901a\u8fc7\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\u6765\u89c4\u907f\u91cd\u590d\u8ba1\u7b97\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684\u8ba1\u7b97\u6548\u7387\u3002
  • \u4e0d\u8003\u8651\u65f6\u95f4\u7684\u524d\u63d0\u4e0b\uff0c\u6240\u6709\u52a8\u6001\u89c4\u5212\u95ee\u9898\u90fd\u53ef\u4ee5\u7528\u56de\u6eaf\uff08\u66b4\u529b\u641c\u7d22\uff09\u8fdb\u884c\u6c42\u89e3\uff0c\u4f46\u9012\u5f52\u6811\u4e2d\u5b58\u5728\u5927\u91cf\u7684\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u6548\u7387\u6781\u4f4e\u3002\u901a\u8fc7\u5f15\u5165\u8bb0\u5fc6\u5316\u5217\u8868\uff0c\u53ef\u4ee5\u5b58\u50a8\u6240\u6709\u8ba1\u7b97\u8fc7\u7684\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u4ece\u800c\u4fdd\u8bc1\u91cd\u53e0\u5b50\u95ee\u9898\u53ea\u88ab\u8ba1\u7b97\u4e00\u6b21\u3002
  • \u8bb0\u5fc6\u5316\u9012\u5f52\u662f\u4e00\u79cd\u4ece\u9876\u81f3\u5e95\u7684\u9012\u5f52\u5f0f\u89e3\u6cd5\uff0c\u800c\u4e0e\u4e4b\u5bf9\u5e94\u7684\u52a8\u6001\u89c4\u5212\u662f\u4e00\u79cd\u4ece\u5e95\u81f3\u9876\u7684\u9012\u63a8\u5f0f\u89e3\u6cd5\uff0c\u5176\u5982\u540c\u201c\u586b\u5199\u8868\u683c\u201d\u4e00\u6837\u3002\u7531\u4e8e\u5f53\u524d\u72b6\u6001\u4ec5\u4f9d\u8d56\u4e8e\u67d0\u4e9b\u5c40\u90e8\u72b6\u6001\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u6d88\u9664 \\(dp\\) \u8868\u7684\u4e00\u4e2a\u7ef4\u5ea6\uff0c\u4ece\u800c\u964d\u4f4e\u7a7a\u95f4\u590d\u6742\u5ea6\u3002
  • \u5b50\u95ee\u9898\u5206\u89e3\u662f\u4e00\u79cd\u901a\u7528\u7684\u7b97\u6cd5\u601d\u8def\uff0c\u5728\u5206\u6cbb\u3001\u52a8\u6001\u89c4\u5212\u3001\u56de\u6eaf\u4e2d\u5177\u6709\u4e0d\u540c\u7684\u6027\u8d28\u3002
  • \u52a8\u6001\u89c4\u5212\u95ee\u9898\u7684\u4e09\u5927\u7279\u6027\uff1a\u91cd\u53e0\u5b50\u95ee\u9898\u3001\u6700\u4f18\u5b50\u7ed3\u6784\u3001\u65e0\u540e\u6548\u6027\u3002
  • \u5982\u679c\u539f\u95ee\u9898\u7684\u6700\u4f18\u89e3\u53ef\u4ee5\u4ece\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u6784\u5efa\u5f97\u6765\uff0c\u5219\u5b83\u5c31\u5177\u6709\u6700\u4f18\u5b50\u7ed3\u6784\u3002
  • \u65e0\u540e\u6548\u6027\u6307\u5bf9\u4e8e\u4e00\u4e2a\u72b6\u6001\uff0c\u5176\u672a\u6765\u53d1\u5c55\u53ea\u4e0e\u8be5\u72b6\u6001\u6709\u5173\uff0c\u4e0e\u5176\u6240\u7ecf\u5386\u7684\u8fc7\u53bb\u7684\u6240\u6709\u72b6\u6001\u65e0\u5173\u3002\u8bb8\u591a\u7ec4\u5408\u4f18\u5316\u95ee\u9898\u90fd\u4e0d\u5177\u6709\u65e0\u540e\u6548\u6027\uff0c\u65e0\u6cd5\u4f7f\u7528\u52a8\u6001\u89c4\u5212\u5feb\u901f\u6c42\u89e3\u3002

\u80cc\u5305\u95ee\u9898

  • \u80cc\u5305\u95ee\u9898\u662f\u6700\u5178\u578b\u7684\u52a8\u6001\u89c4\u5212\u9898\u76ee\uff0c\u5177\u6709 0-1 \u80cc\u5305\u3001\u5b8c\u5168\u80cc\u5305\u3001\u591a\u91cd\u80cc\u5305\u7b49\u53d8\u79cd\u95ee\u9898\u3002
  • 0-1 \u80cc\u5305\u7684\u72b6\u6001\u5b9a\u4e49\u4e3a\u524d \\(i\\) \u4e2a\u7269\u54c1\u5728\u5269\u4f59\u5bb9\u91cf\u4e3a \\(c\\) \u7684\u80cc\u5305\u4e2d\u7684\u6700\u5927\u4ef7\u503c\u3002\u6839\u636e\u4e0d\u653e\u5165\u80cc\u5305\u548c\u653e\u5165\u80cc\u5305\u4e24\u79cd\u51b3\u7b56\uff0c\u53ef\u5f97\u5230\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u5e76\u6784\u5efa\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u3002\u5728\u7a7a\u95f4\u4f18\u5316\u4e2d\uff0c\u7531\u4e8e\u6bcf\u4e2a\u72b6\u6001\u4f9d\u8d56\u6b63\u4e0a\u65b9\u548c\u5de6\u4e0a\u65b9\u7684\u72b6\u6001\uff0c\u56e0\u6b64\u9700\u8981\u5012\u5e8f\u904d\u5386\u5217\u8868\uff0c\u907f\u514d\u5de6\u4e0a\u65b9\u72b6\u6001\u88ab\u8986\u76d6\u3002
  • \u5b8c\u5168\u80cc\u5305\u7684\u6bcf\u79cd\u7269\u54c1\u7684\u9009\u53d6\u6570\u91cf\u65e0\u9650\u5236\uff0c\u56e0\u6b64\u9009\u62e9\u653e\u5165\u7269\u54c1\u7684\u72b6\u6001\u8f6c\u79fb\u4e0e 0-1 \u80cc\u5305\u4e0d\u540c\u3002\u7531\u4e8e\u72b6\u6001\u4f9d\u8d56\u4e8e\u6b63\u4e0a\u65b9\u548c\u6b63\u5de6\u65b9\u7684\u72b6\u6001\uff0c\u56e0\u6b64\u5728\u7a7a\u95f4\u4f18\u5316\u4e2d\u5e94\u5f53\u6b63\u5e8f\u904d\u5386\u3002
  • \u96f6\u94b1\u5151\u6362\u95ee\u9898\u662f\u5b8c\u5168\u80cc\u5305\u7684\u4e00\u4e2a\u53d8\u79cd\u3002\u5b83\u4ece\u6c42\u201c\u6700\u5927\u201d\u4ef7\u503c\u53d8\u4e3a\u6c42\u201c\u6700\u5c0f\u201d\u786c\u5e01\u6570\u91cf\uff0c\u56e0\u6b64\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e2d\u7684 \\(\\max()\\) \u5e94\u6539\u4e3a \\(\\min()\\) \u3002\u4ece\u6c42\u201c\u4e0d\u8d85\u8fc7\u201d\u80cc\u5305\u5bb9\u91cf\u5230\u6c42\u201c\u6070\u597d\u201d\u51d1\u51fa\u76ee\u6807\u91d1\u989d\uff0c\u56e0\u6b64\u4f7f\u7528 \\(amt + 1\\) \u6765\u8868\u793a\u201c\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u201d\u7684\u65e0\u6548\u89e3\u3002
  • \u96f6\u94b1\u5151\u6362 II \u95ee\u9898\u4ece\u6c42\u201c\u6700\u5c11\u786c\u5e01\u6570\u91cf\u201d\u6539\u4e3a\u6c42\u201c\u786c\u5e01\u7ec4\u5408\u6570\u91cf\u201d\uff0c\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u76f8\u5e94\u5730\u4ece \\(\\min()\\) \u6539\u4e3a\u6c42\u548c\u8fd0\u7b97\u7b26\u3002

\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898

  • \u7f16\u8f91\u8ddd\u79bb\uff08Levenshtein \u8ddd\u79bb\uff09\u7528\u4e8e\u8861\u91cf\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e4b\u95f4\u7684\u76f8\u4f3c\u5ea6\uff0c\u5176\u5b9a\u4e49\u4e3a\u4ece\u4e00\u4e2a\u5b57\u7b26\u4e32\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u6700\u5c0f\u7f16\u8f91\u6b65\u6570\uff0c\u7f16\u8f91\u64cd\u4f5c\u5305\u62ec\u6dfb\u52a0\u3001\u5220\u9664\u3001\u66ff\u6362\u3002
  • \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898\u7684\u72b6\u6001\u5b9a\u4e49\u4e3a\u5c06 \\(s\\) \u7684\u524d \\(i\\) \u4e2a\u5b57\u7b26\u66f4\u6539\u4e3a \\(t\\) \u7684\u524d \\(j\\) \u4e2a\u5b57\u7b26\u6240\u9700\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u3002\u5f53 \\(s[i] \\ne t[j]\\) \u65f6\uff0c\u5177\u6709\u4e09\u79cd\u51b3\u7b56\uff1a\u6dfb\u52a0\u3001\u5220\u9664\u3001\u66ff\u6362\uff0c\u5b83\u4eec\u90fd\u6709\u76f8\u5e94\u7684\u5269\u4f59\u5b50\u95ee\u9898\u3002\u636e\u6b64\u4fbf\u53ef\u4ee5\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\u4e0e\u6784\u5efa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u3002\u800c\u5f53 \\(s[i] = t[j]\\) \u65f6\uff0c\u65e0\u987b\u7f16\u8f91\u5f53\u524d\u5b57\u7b26\u3002
  • \u5728\u7f16\u8f91\u8ddd\u79bb\u4e2d\uff0c\u72b6\u6001\u4f9d\u8d56\u4e8e\u5176\u6b63\u4e0a\u65b9\u3001\u6b63\u5de6\u65b9\u3001\u5de6\u4e0a\u65b9\u7684\u72b6\u6001\uff0c\u56e0\u6b64\u7a7a\u95f4\u4f18\u5316\u540e\u6b63\u5e8f\u6216\u5012\u5e8f\u904d\u5386\u90fd\u65e0\u6cd5\u6b63\u786e\u5730\u8fdb\u884c\u72b6\u6001\u8f6c\u79fb\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5229\u7528\u4e00\u4e2a\u53d8\u91cf\u6682\u5b58\u5de6\u4e0a\u65b9\u72b6\u6001\uff0c\u4ece\u800c\u8f6c\u5316\u5230\u4e0e\u5b8c\u5168\u80cc\u5305\u7b49\u4ef7\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u5728\u7a7a\u95f4\u4f18\u5316\u540e\u8fdb\u884c\u6b63\u5e8f\u904d\u5386\u3002
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/","title":"14.5 \u00a0 \u5b8c\u5168\u80cc\u5305\u95ee\u9898","text":"

\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5148\u6c42\u89e3\u53e6\u4e00\u4e2a\u5e38\u89c1\u7684\u80cc\u5305\u95ee\u9898\uff1a\u5b8c\u5168\u80cc\u5305\uff0c\u518d\u4e86\u89e3\u5b83\u7684\u4e00\u79cd\u7279\u4f8b\uff1a\u96f6\u94b1\u5151\u6362\u3002

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1451","title":"14.5.1 \u00a0 \u5b8c\u5168\u80cc\u5305","text":"

Question

\u7ed9\u5b9a \\(n\\) \u4e2a\u7269\u54c1\uff0c\u7b2c \\(i\\) \u4e2a\u7269\u54c1\u7684\u91cd\u91cf\u4e3a \\(wgt[i-1]\\)\u3001\u4ef7\u503c\u4e3a \\(val[i-1]\\) \uff0c\u548c\u4e00\u4e2a\u5bb9\u91cf\u4e3a \\(cap\\) \u7684\u80cc\u5305\u3002\u6bcf\u4e2a\u7269\u54c1\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u5728\u4e0d\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\u4e0b\u80fd\u653e\u5165\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u3002

\u56fe 14-22 \u00a0 \u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"

\u5b8c\u5168\u80cc\u5305\u548c 0-1 \u80cc\u5305\u95ee\u9898\u975e\u5e38\u76f8\u4f3c\uff0c\u533a\u522b\u4ec5\u5728\u4e8e\u4e0d\u9650\u5236\u7269\u54c1\u7684\u9009\u62e9\u6b21\u6570\u3002

  • \u5728 0-1 \u80cc\u5305\u4e2d\uff0c\u6bcf\u4e2a\u7269\u54c1\u53ea\u6709\u4e00\u4e2a\uff0c\u56e0\u6b64\u5c06\u7269\u54c1 \\(i\\) \u653e\u5165\u80cc\u5305\u540e\uff0c\u53ea\u80fd\u4ece\u524d \\(i-1\\) \u4e2a\u7269\u54c1\u4e2d\u9009\u62e9\u3002
  • \u5728\u5b8c\u5168\u80cc\u5305\u4e2d\uff0c\u6bcf\u4e2a\u7269\u54c1\u6709\u65e0\u6570\u4e2a\uff0c\u56e0\u6b64\u5c06\u7269\u54c1 \\(i\\) \u653e\u5165\u80cc\u5305\u540e\uff0c\u4ecd\u53ef\u4ee5\u4ece\u524d \\(i\\) \u4e2a\u7269\u54c1\u4e2d\u9009\u62e9\u3002

\u5728\u5b8c\u5168\u80cc\u5305\u7684\u89c4\u5b9a\u4e0b\uff0c\u72b6\u6001 \\([i, c]\\) \u7684\u53d8\u5316\u5206\u4e3a\u4e24\u79cd\u60c5\u51b5\u3002

  • \u4e0d\u653e\u5165\u7269\u54c1 \\(i\\) \uff1a\u4e0e 0-1 \u80cc\u5305\u76f8\u540c\uff0c\u8f6c\u79fb\u81f3 \\([i-1, c]\\) \u3002
  • \u653e\u5165\u7269\u54c1 \\(i\\) \uff1a\u4e0e 0-1 \u80cc\u5305\u4e0d\u540c\uff0c\u8f6c\u79fb\u81f3 \\([i, c-wgt[i-1]]\\) \u3002

\u4ece\u800c\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u53d8\u4e3a\uff1a

\\[ dp[i, c] = \\max(dp[i-1, c], dp[i, c - wgt[i-1]] + val[i-1]) \\]"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u5bf9\u6bd4\u4e24\u9053\u9898\u76ee\u7684\u4ee3\u7801\uff0c\u72b6\u6001\u8f6c\u79fb\u4e2d\u6709\u4e00\u5904\u4ece \\(i-1\\) \u53d8\u4e3a \\(i\\) \uff0c\u5176\u4f59\u5b8c\u5168\u4e00\u81f4\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig unbounded_knapsack.py
def unbounded_knapsack_dp(wgt: list[int], val: list[int], cap: int) -> int:\n\"\"\"\u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(wgt)\n# \u521d\u59cb\u5316 dp \u8868\ndp = [[0] * (cap + 1) for _ in range(n + 1)]\n# \u72b6\u6001\u8f6c\u79fb\nfor i in range(1, n + 1):\nfor c in range(1, cap + 1):\nif wgt[i - 1] > c:\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c]\nelse:\n# \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1])\nreturn dp[n][cap]\n
unbounded_knapsack.cpp
/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(vector<int> &wgt, vector<int> &val, int cap) {\nint n = wgt.size();\n// \u521d\u59cb\u5316 dp \u8868\nvector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n
unbounded_knapsack.java
/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(int[] wgt, int[] val, int cap) {\nint n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nint[][] dp = new int[n + 1][cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = Math.max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n
unbounded_knapsack.cs
/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint UnboundedKnapsackDP(int[] wgt, int[] val, int cap) {\nint n = wgt.Length;\n// \u521d\u59cb\u5316 dp \u8868\nint[,] dp = new int[n + 1, cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i, c] = dp[i - 1, c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i, c] = Math.Max(dp[i - 1, c], dp[i, c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[n, cap];\n}\n
unbounded_knapsack.go
/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDP(wgt, val []int, cap int) int {\nn := len(wgt)\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([][]int, n+1)\nfor i := 0; i <= n; i++ {\ndp[i] = make([]int, cap+1)\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor i := 1; i <= n; i++ {\nfor c := 1; c <= cap; c++ {\nif wgt[i-1] > c {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i-1][c]\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = int(math.Max(float64(dp[i-1][c]), float64(dp[i][c-wgt[i-1]]+val[i-1])))\n}\n}\n}\nreturn dp[n][cap]\n}\n
unbounded_knapsack.swift
/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDP(wgt: [Int], val: [Int], cap: Int) -> Int {\nlet n = wgt.count\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)\n// \u72b6\u6001\u8f6c\u79fb\nfor i in stride(from: 1, through: n, by: 1) {\nfor c in stride(from: 1, through: cap, by: 1) {\nif wgt[i - 1] > c {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c]\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1])\n}\n}\n}\nreturn dp[n][cap]\n}\n
unbounded_knapsack.js
/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction unboundedKnapsackDP(wgt, val, cap) {\nconst n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: n + 1 }, () =>\nArray.from({ length: cap + 1 }, () => 0)\n);\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\nfor (let c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = Math.max(\ndp[i - 1][c],\ndp[i][c - wgt[i - 1]] + val[i - 1]\n);\n}\n}\n}\nreturn dp[n][cap];\n}\n
unbounded_knapsack.ts
/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction unboundedKnapsackDP(\nwgt: Array<number>,\nval: Array<number>,\ncap: number\n): number {\nconst n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: n + 1 }, () =>\nArray.from({ length: cap + 1 }, () => 0)\n);\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\nfor (let c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = Math.max(\ndp[i - 1][c],\ndp[i][c - wgt[i - 1]] + val[i - 1]\n);\n}\n}\n}\nreturn dp[n][cap];\n}\n
unbounded_knapsack.dart
/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(List<int> wgt, List<int> val, int cap) {\nint n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nList<List<int>> dp = List.generate(n + 1, (index) => List.filled(cap + 1, 0));\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n
unbounded_knapsack.rs
/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfn unbounded_knapsack_dp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\nlet n = wgt.len();\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![vec![0; cap + 1]; n + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor i in 1..=n {\nfor c in 1..=cap {\nif wgt[i - 1] > c as i32 {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = std::cmp::max(dp[i - 1][c], dp[i][c - wgt[i - 1] as usize] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n
unbounded_knapsack.c
/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(int wgt[], int val[], int cap, int wgtSize) {\nint n = wgtSize;\n// \u521d\u59cb\u5316 dp \u8868\nint dp[n + 1][cap + 1];\nmemset(dp, 0, sizeof(dp));\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n
unbounded_knapsack.zig
// \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212\nfn unboundedKnapsackDP(comptime wgt: []i32, val: []i32, comptime cap: usize) i32 {\ncomptime var n = wgt.len;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_][cap + 1]i32{[_]i32{0} ** (cap + 1)} ** (n + 1);\n// \u72b6\u6001\u8f6c\u79fb\nfor (1..n + 1) |i| {\nfor (1..cap + 1) |c| {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = @max(dp[i - 1][c], dp[i][c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

\u7531\u4e8e\u5f53\u524d\u72b6\u6001\u662f\u4ece\u5de6\u8fb9\u548c\u4e0a\u8fb9\u7684\u72b6\u6001\u8f6c\u79fb\u800c\u6765\uff0c\u56e0\u6b64\u7a7a\u95f4\u4f18\u5316\u540e\u5e94\u8be5\u5bf9 \\(dp\\) \u8868\u4e2d\u7684\u6bcf\u4e00\u884c\u91c7\u53d6\u6b63\u5e8f\u904d\u5386\u3002

\u8fd9\u4e2a\u904d\u5386\u987a\u5e8f\u4e0e 0-1 \u80cc\u5305\u6b63\u597d\u76f8\u53cd\u3002\u8bf7\u501f\u52a9\u56fe 14-23 \u6765\u7406\u89e3\u4e24\u8005\u7684\u533a\u522b\u3002

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

\u56fe 14-23 \u00a0 \u5b8c\u5168\u80cc\u5305\u7684\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

\u4ee3\u7801\u5b9e\u73b0\u6bd4\u8f83\u7b80\u5355\uff0c\u4ec5\u9700\u5c06\u6570\u7ec4 dp \u7684\u7b2c\u4e00\u7ef4\u5220\u9664\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig unbounded_knapsack.py
def unbounded_knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -> int:\n\"\"\"\u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(wgt)\n# \u521d\u59cb\u5316 dp \u8868\ndp = [0] * (cap + 1)\n# \u72b6\u6001\u8f6c\u79fb\nfor i in range(1, n + 1):\n# \u6b63\u5e8f\u904d\u5386\nfor c in range(1, cap + 1):\nif wgt[i - 1] > c:\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c]\nelse:\n# \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\nreturn dp[cap]\n
unbounded_knapsack.cpp
/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {\nint n = wgt.size();\n// \u521d\u59cb\u5316 dp \u8868\nvector<int> dp(cap + 1, 0);\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
unbounded_knapsack.java
/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {\nint n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
unbounded_knapsack.cs
/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint UnboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {\nint n = wgt.Length;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = Math.Max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
unbounded_knapsack.go
/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDPComp(wgt, val []int, cap int) int {\nn := len(wgt)\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([]int, cap+1)\n// \u72b6\u6001\u8f6c\u79fb\nfor i := 1; i <= n; i++ {\nfor c := 1; c <= cap; c++ {\nif wgt[i-1] > c {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c]\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = int(math.Max(float64(dp[c]), float64(dp[c-wgt[i-1]]+val[i-1])))\n}\n}\n}\nreturn dp[cap]\n}\n
unbounded_knapsack.swift
/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDPComp(wgt: [Int], val: [Int], cap: Int) -> Int {\nlet n = wgt.count\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: 0, count: cap + 1)\n// \u72b6\u6001\u8f6c\u79fb\nfor i in stride(from: 1, through: n, by: 1) {\nfor c in stride(from: 1, through: cap, by: 1) {\nif wgt[i - 1] > c {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c]\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n}\n}\n}\nreturn dp[cap]\n}\n
unbounded_knapsack.js
/* \u5b8c\u5168\u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction unboundedKnapsackDPComp(wgt, val, cap) {\nconst n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: cap + 1 }, () => 0);\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\nfor (let c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
unbounded_knapsack.ts
/* \u5b8c\u5168\u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction unboundedKnapsackDPComp(\nwgt: Array<number>,\nval: Array<number>,\ncap: number\n): number {\nconst n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: cap + 1 }, () => 0);\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\nfor (let c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
unbounded_knapsack.dart
/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(List<int> wgt, List<int> val, int cap) {\nint n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nList<int> dp = List.filled(cap + 1, 0);\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
unbounded_knapsack.rs
/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn unbounded_knapsack_dp_comp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\nlet n = wgt.len();\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![0; cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor i in 1..=n {\nfor c in 1..=cap {\nif wgt[i - 1] > c as i32 {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = std::cmp::max(dp[c], dp[c - wgt[i - 1] as usize] + val[i - 1]);\n}\n}\n}\ndp[cap]\n}\n
unbounded_knapsack.c
/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(int wgt[], int val[], int cap, int wgtSize) {\nint n = wgtSize;\n// \u521d\u59cb\u5316 dp \u8868\nint dp[cap + 1];\nmemset(dp, 0, sizeof(dp));\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
unbounded_knapsack.zig
// \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn unboundedKnapsackDPComp(comptime wgt: []i32, val: []i32, comptime cap: usize) i32 {\ncomptime var n = wgt.len;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_]i32{0} ** (cap + 1);\n// \u72b6\u6001\u8f6c\u79fb\nfor (1..n + 1) |i| {\nfor (1..cap + 1) |c| {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = @max(dp[c], dp[c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1452","title":"14.5.2 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898","text":"

\u80cc\u5305\u95ee\u9898\u662f\u4e00\u5927\u7c7b\u52a8\u6001\u89c4\u5212\u95ee\u9898\u7684\u4ee3\u8868\uff0c\u5176\u62e5\u6709\u5f88\u591a\u7684\u53d8\u79cd\uff0c\u4f8b\u5982\u96f6\u94b1\u5151\u6362\u95ee\u9898\u3002

Question

\u7ed9\u5b9a \\(n\\) \u79cd\u786c\u5e01\uff0c\u7b2c \\(i\\) \u79cd\u786c\u5e01\u7684\u9762\u503c\u4e3a \\(coins[i - 1]\\) \uff0c\u76ee\u6807\u91d1\u989d\u4e3a \\(amt\\) \uff0c\u6bcf\u79cd\u786c\u5e01\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u80fd\u591f\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u7684\u6700\u5c11\u786c\u5e01\u4e2a\u6570\u3002\u5982\u679c\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u5219\u8fd4\u56de \\(-1\\) \u3002

\u56fe 14-24 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1_1","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"

\u96f6\u94b1\u5151\u6362\u53ef\u4ee5\u770b\u4f5c\u662f\u5b8c\u5168\u80cc\u5305\u7684\u4e00\u79cd\u7279\u6b8a\u60c5\u51b5\uff0c\u4e24\u8005\u5177\u6709\u4ee5\u4e0b\u8054\u7cfb\u4e0e\u4e0d\u540c\u70b9\u3002

  • \u4e24\u9053\u9898\u53ef\u4ee5\u76f8\u4e92\u8f6c\u6362\uff0c\u201c\u7269\u54c1\u201d\u5bf9\u5e94\u4e8e\u201c\u786c\u5e01\u201d\u3001\u201c\u7269\u54c1\u91cd\u91cf\u201d\u5bf9\u5e94\u4e8e\u201c\u786c\u5e01\u9762\u503c\u201d\u3001\u201c\u80cc\u5305\u5bb9\u91cf\u201d\u5bf9\u5e94\u4e8e\u201c\u76ee\u6807\u91d1\u989d\u201d\u3002
  • \u4f18\u5316\u76ee\u6807\u76f8\u53cd\uff0c\u80cc\u5305\u95ee\u9898\u662f\u8981\u6700\u5927\u5316\u7269\u54c1\u4ef7\u503c\uff0c\u96f6\u94b1\u5151\u6362\u95ee\u9898\u662f\u8981\u6700\u5c0f\u5316\u786c\u5e01\u6570\u91cf\u3002
  • \u80cc\u5305\u95ee\u9898\u662f\u6c42\u201c\u4e0d\u8d85\u8fc7\u201d\u80cc\u5305\u5bb9\u91cf\u4e0b\u7684\u89e3\uff0c\u96f6\u94b1\u5151\u6362\u662f\u6c42\u201c\u6070\u597d\u201d\u51d1\u5230\u76ee\u6807\u91d1\u989d\u7684\u89e3\u3002

\u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868

\u72b6\u6001 \\([i, a]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u4e3a\uff1a\u524d \\(i\\) \u79cd\u786c\u5e01\u80fd\u591f\u51d1\u51fa\u91d1\u989d \\(a\\) \u7684\u6700\u5c11\u786c\u5e01\u4e2a\u6570\uff0c\u8bb0\u4e3a \\(dp[i, a]\\) \u3002

\u4e8c\u7ef4 \\(dp\\) \u8868\u7684\u5c3a\u5bf8\u4e3a \\((n+1) \\times (amt+1)\\) \u3002

\u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b

\u672c\u9898\u4e0e\u5b8c\u5168\u80cc\u5305\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u5b58\u5728\u4ee5\u4e0b\u4e24\u4e2a\u5dee\u5f02\u3002

  • \u672c\u9898\u8981\u6c42\u6700\u5c0f\u503c\uff0c\u56e0\u6b64\u9700\u5c06\u8fd0\u7b97\u7b26 \\(\\max()\\) \u66f4\u6539\u4e3a \\(\\min()\\) \u3002
  • \u4f18\u5316\u4e3b\u4f53\u662f\u786c\u5e01\u6570\u91cf\u800c\u975e\u5546\u54c1\u4ef7\u503c\uff0c\u56e0\u6b64\u5728\u9009\u4e2d\u786c\u5e01\u65f6\u6267\u884c \\(+1\\) \u5373\u53ef\u3002
\\[ dp[i, a] = \\min(dp[i-1, a], dp[i, a - coins[i-1]] + 1) \\]

\u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f

\u5f53\u76ee\u6807\u91d1\u989d\u4e3a \\(0\\) \u65f6\uff0c\u51d1\u51fa\u5b83\u7684\u6700\u5c11\u786c\u5e01\u4e2a\u6570\u4e3a \\(0\\) \uff0c\u5373\u9996\u5217\u6240\u6709 \\(dp[i, 0]\\) \u90fd\u7b49\u4e8e \\(0\\) \u3002

\u5f53\u65e0\u786c\u5e01\u65f6\uff0c\u65e0\u6cd5\u51d1\u51fa\u4efb\u610f \\(> 0\\) \u7684\u76ee\u6807\u91d1\u989d\uff0c\u5373\u662f\u65e0\u6548\u89e3\u3002\u4e3a\u4f7f\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e2d\u7684 \\(\\min()\\) \u51fd\u6570\u80fd\u591f\u8bc6\u522b\u5e76\u8fc7\u6ee4\u65e0\u6548\u89e3\uff0c\u6211\u4eec\u8003\u8651\u4f7f\u7528 \\(+ \\infty\\) \u6765\u8868\u793a\u5b83\u4eec\uff0c\u5373\u4ee4\u9996\u884c\u6240\u6709 \\(dp[0, a]\\) \u90fd\u7b49\u4e8e \\(+ \\infty\\) \u3002

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2_1","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u5e76\u672a\u63d0\u4f9b \\(+ \\infty\\) \u53d8\u91cf\uff0c\u53ea\u80fd\u4f7f\u7528\u6574\u578b int \u7684\u6700\u5927\u503c\u6765\u4ee3\u66ff\u3002\u800c\u8fd9\u53c8\u4f1a\u5bfc\u81f4\u5927\u6570\u8d8a\u754c\uff1a\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e2d\u7684 \\(+ 1\\) \u64cd\u4f5c\u53ef\u80fd\u53d1\u751f\u6ea2\u51fa\u3002

\u4e3a\u6b64\uff0c\u6211\u4eec\u91c7\u7528\u6570\u5b57 \\(amt + 1\\) \u6765\u8868\u793a\u65e0\u6548\u89e3\uff0c\u56e0\u4e3a\u51d1\u51fa \\(amt\\) \u7684\u786c\u5e01\u4e2a\u6570\u6700\u591a\u4e3a \\(amt\\) \u4e2a\u3002

\u6700\u540e\u8fd4\u56de\u524d\uff0c\u5224\u65ad \\(dp[n, amt]\\) \u662f\u5426\u7b49\u4e8e \\(amt + 1\\) \uff0c\u82e5\u662f\u5219\u8fd4\u56de \\(-1\\) \uff0c\u4ee3\u8868\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig coin_change.py
def coin_change_dp(coins: list[int], amt: int) -> int:\n\"\"\"\u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(coins)\nMAX = amt + 1\n# \u521d\u59cb\u5316 dp \u8868\ndp = [[0] * (amt + 1) for _ in range(n + 1)]\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor a in range(1, amt + 1):\ndp[0][a] = MAX\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in range(1, n + 1):\nfor a in range(1, amt + 1):\nif coins[i - 1] > a:\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a]\nelse:\n# \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\nreturn dp[n][amt] if dp[n][amt] != MAX else -1\n
coin_change.cpp
/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(vector<int> &coins, int amt) {\nint n = coins.size();\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nvector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int a = 1; a <= amt; a++) {\ndp[0][a] = MAX;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
coin_change.java
/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(int[] coins, int amt) {\nint n = coins.length;\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nint[][] dp = new int[n + 1][amt + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int a = 1; a <= amt; a++) {\ndp[0][a] = MAX;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
coin_change.cs
/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint CoinChangeDP(int[] coins, int amt) {\nint n = coins.Length;\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nint[,] dp = new int[n + 1, amt + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int a = 1; a <= amt; a++) {\ndp[0, a] = MAX;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i, a] = dp[i - 1, a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i, a] = Math.Min(dp[i - 1, a], dp[i, a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[n, amt] != MAX ? dp[n, amt] : -1;\n}\n
coin_change.go
/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDP(coins []int, amt int) int {\nn := len(coins)\nmax := amt + 1\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([][]int, n+1)\nfor i := 0; i <= n; i++ {\ndp[i] = make([]int, amt+1)\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor a := 1; a <= amt; a++ {\ndp[0][a] = max\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i := 1; i <= n; i++ {\nfor a := 1; a <= amt; a++ {\nif coins[i-1] > a {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i-1][a]\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = int(math.Min(float64(dp[i-1][a]), float64(dp[i][a-coins[i-1]]+1)))\n}\n}\n}\nif dp[n][amt] != max {\nreturn dp[n][amt]\n}\nreturn -1\n}\n
coin_change.swift
/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDP(coins: [Int], amt: Int) -> Int {\nlet n = coins.count\nlet MAX = amt + 1\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor a in stride(from: 1, through: amt, by: 1) {\ndp[0][a] = MAX\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in stride(from: 1, through: n, by: 1) {\nfor a in stride(from: 1, through: amt, by: 1) {\nif coins[i - 1] > a {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a]\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n}\n}\n}\nreturn dp[n][amt] != MAX ? dp[n][amt] : -1\n}\n
coin_change.js
/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeDP(coins, amt) {\nconst n = coins.length;\nconst MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: n + 1 }, () =>\nArray.from({ length: amt + 1 }, () => 0)\n);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (let a = 1; a <= amt; a++) {\ndp[0][a] = MAX;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (let i = 1; i <= n; i++) {\nfor (let a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[n][amt] !== MAX ? dp[n][amt] : -1;\n}\n
coin_change.ts
/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeDP(coins: Array<number>, amt: number): number {\nconst n = coins.length;\nconst MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: n + 1 }, () =>\nArray.from({ length: amt + 1 }, () => 0)\n);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (let a = 1; a <= amt; a++) {\ndp[0][a] = MAX;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (let i = 1; i <= n; i++) {\nfor (let a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[n][amt] !== MAX ? dp[n][amt] : -1;\n}\n
coin_change.dart
/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(List<int> coins, int amt) {\nint n = coins.length;\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nList<List<int>> dp = List.generate(n + 1, (index) => List.filled(amt + 1, 0));\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int a = 1; a <= amt; a++) {\ndp[0][a] = MAX;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
coin_change.rs
/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfn coin_change_dp(coins: &[i32], amt: usize) -> i32 {\nlet n = coins.len();\nlet max = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![vec![0; amt + 1]; n + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor a in 1..= amt {\ndp[0][a] = max;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in 1..=n {\nfor a in 1..=amt {\nif coins[i - 1] > a as i32 {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = std::cmp::min(dp[i - 1][a], dp[i][a - coins[i - 1] as usize] + 1);\n}\n}\n}\nif dp[n][amt] != max { return dp[n][amt] as i32; } else { -1 }\n}\n
coin_change.c
/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(int coins[], int amt, int coinsSize) {\nint n = coinsSize;\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nint dp[n + 1][amt + 1];\nmemset(dp, 0, sizeof(dp));\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int a = 1; a <= amt; a++) {\ndp[0][a] = MAX;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
coin_change.zig
// \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212\nfn coinChangeDP(comptime coins: []i32, comptime amt: usize) i32 {\ncomptime var n = coins.len;\ncomptime var max = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_][amt + 1]i32{[_]i32{0} ** (amt + 1)} ** (n + 1);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (1..amt + 1) |a| {\ndp[0][a] = max;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (1..n + 1) |i| {\nfor (1..amt + 1) |a| {\nif (coins[i - 1] > @as(i32, @intCast(a))) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = @min(dp[i - 1][a], dp[i][a - @as(usize, @intCast(coins[i - 1]))] + 1);\n}\n}\n}\nif (dp[n][amt] != max) {\nreturn @intCast(dp[n][amt]);\n} else {\nreturn -1;\n}\n}\n

\u56fe 14-25 \u5c55\u793a\u4e86\u96f6\u94b1\u5151\u6362\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b\uff0c\u548c\u5b8c\u5168\u80cc\u5305\u975e\u5e38\u76f8\u4f3c\u3002

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

\u56fe 14-25 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3_1","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

\u96f6\u94b1\u5151\u6362\u7684\u7a7a\u95f4\u4f18\u5316\u7684\u5904\u7406\u65b9\u5f0f\u548c\u5b8c\u5168\u80cc\u5305\u4e00\u81f4\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig coin_change.py
def coin_change_dp_comp(coins: list[int], amt: int) -> int:\n\"\"\"\u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(coins)\nMAX = amt + 1\n# \u521d\u59cb\u5316 dp \u8868\ndp = [MAX] * (amt + 1)\ndp[0] = 0\n# \u72b6\u6001\u8f6c\u79fb\nfor i in range(1, n + 1):\n# \u6b63\u5e8f\u904d\u5386\nfor a in range(1, amt + 1):\nif coins[i - 1] > a:\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a]\nelse:\n# \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\nreturn dp[amt] if dp[amt] != MAX else -1\n
coin_change.cpp
/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(vector<int> &coins, int amt) {\nint n = coins.size();\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nvector<int> dp(amt + 1, MAX);\ndp[0] = 0;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[amt] != MAX ? dp[amt] : -1;\n}\n
coin_change.java
/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(int[] coins, int amt) {\nint n = coins.length;\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[amt + 1];\nArrays.fill(dp, MAX);\ndp[0] = 0;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[amt] != MAX ? dp[amt] : -1;\n}\n
coin_change.cs
/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint CoinChangeDPComp(int[] coins, int amt) {\nint n = coins.Length;\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[amt + 1];\nArray.Fill(dp, MAX);\ndp[0] = 0;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = Math.Min(dp[a], dp[a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[amt] != MAX ? dp[amt] : -1;\n}\n
coin_change.go
/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDPComp(coins []int, amt int) int {\nn := len(coins)\nmax := amt + 1\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([]int, amt+1)\nfor i := 1; i <= amt; i++ {\ndp[i] = max\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor i := 1; i <= n; i++ {\n// \u5012\u5e8f\u904d\u5386\nfor a := 1; a <= amt; a++ {\nif coins[i-1] > a {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a]\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = int(math.Min(float64(dp[a]), float64(dp[a-coins[i-1]]+1)))\n}\n}\n}\nif dp[amt] != max {\nreturn dp[amt]\n}\nreturn -1\n}\n
coin_change.swift
/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDPComp(coins: [Int], amt: Int) -> Int {\nlet n = coins.count\nlet MAX = amt + 1\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: MAX, count: amt + 1)\ndp[0] = 0\n// \u72b6\u6001\u8f6c\u79fb\nfor i in stride(from: 1, through: n, by: 1) {\nfor a in stride(from: 1, through: amt, by: 1) {\nif coins[i - 1] > a {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a]\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n}\n}\n}\nreturn dp[amt] != MAX ? dp[amt] : -1\n}\n
coin_change.js
/* \u96f6\u94b1\u5151\u6362\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeDPComp(coins, amt) {\nconst n = coins.length;\nconst MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: amt + 1 }, () => MAX);\ndp[0] = 0;\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\nfor (let a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[amt] !== MAX ? dp[amt] : -1;\n}\n
coin_change.ts
/* \u96f6\u94b1\u5151\u6362\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeDPComp(coins: Array<number>, amt: number): number {\nconst n = coins.length;\nconst MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: amt + 1 }, () => MAX);\ndp[0] = 0;\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\nfor (let a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[amt] !== MAX ? dp[amt] : -1;\n}\n
coin_change.dart
/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(List<int> coins, int amt) {\nint n = coins.length;\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nList<int> dp = List.filled(amt + 1, MAX);\ndp[0] = 0;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[amt] != MAX ? dp[amt] : -1;\n}\n
coin_change.rs
/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn coin_change_dp_comp(coins: &[i32], amt: usize) -> i32 {\nlet n = coins.len();\nlet max = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![0; amt + 1];\ndp.fill(max);\ndp[0] = 0;\n// \u72b6\u6001\u8f6c\u79fb\nfor i in 1..=n {\nfor a in 1..=amt {\nif coins[i - 1] > a as i32 {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = std::cmp::min(dp[a], dp[a - coins[i - 1] as usize] + 1);\n}\n}\n}\nif dp[amt] != max { return dp[amt] as i32; } else { -1 }\n}\n
coin_change.c
/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(int coins[], int amt, int coinsSize) {\nint n = coinsSize;\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nint dp[amt + 1];\nmemset(dp, MAX, sizeof(dp));\ndp[0] = 0;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[amt] != MAX ? dp[amt] : -1;\n}\n
coin_change.zig
// \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn coinChangeDPComp(comptime coins: []i32, comptime amt: usize) i32 {\ncomptime var n = coins.len;\ncomptime var max = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_]i32{0} ** (amt + 1);\n@memset(&dp, max);\ndp[0] = 0;\n// \u72b6\u6001\u8f6c\u79fb\nfor (1..n + 1) |i| {\nfor (1..amt + 1) |a| {\nif (coins[i - 1] > @as(i32, @intCast(a))) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = @min(dp[a], dp[a - @as(usize, @intCast(coins[i - 1]))] + 1);\n}\n}\n}\nif (dp[amt] != max) {\nreturn @intCast(dp[amt]);\n} else {\nreturn -1;\n}\n}\n
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1453-ii","title":"14.5.3 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898 II","text":"

Question

\u7ed9\u5b9a \\(n\\) \u79cd\u786c\u5e01\uff0c\u7b2c \\(i\\) \u79cd\u786c\u5e01\u7684\u9762\u503c\u4e3a \\(coins[i - 1]\\) \uff0c\u76ee\u6807\u91d1\u989d\u4e3a \\(amt\\) \uff0c\u6bcf\u79cd\u786c\u5e01\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u5728\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u7684\u786c\u5e01\u7ec4\u5408\u6570\u91cf\u3002

\u56fe 14-26 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898 II \u7684\u793a\u4f8b\u6570\u636e

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1_2","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"

\u76f8\u6bd4\u4e8e\u4e0a\u4e00\u9898\uff0c\u672c\u9898\u76ee\u6807\u662f\u7ec4\u5408\u6570\u91cf\uff0c\u56e0\u6b64\u5b50\u95ee\u9898\u53d8\u4e3a\uff1a\u524d \\(i\\) \u79cd\u786c\u5e01\u80fd\u591f\u51d1\u51fa\u91d1\u989d \\(a\\) \u7684\u7ec4\u5408\u6570\u91cf\u3002\u800c \\(dp\\) \u8868\u4ecd\u7136\u662f\u5c3a\u5bf8\u4e3a \\((n+1) \\times (amt + 1)\\) \u7684\u4e8c\u7ef4\u77e9\u9635\u3002

\u5f53\u524d\u72b6\u6001\u7684\u7ec4\u5408\u6570\u91cf\u7b49\u4e8e\u4e0d\u9009\u5f53\u524d\u786c\u5e01\u4e0e\u9009\u5f53\u524d\u786c\u5e01\u8fd9\u4e24\u79cd\u51b3\u7b56\u7684\u7ec4\u5408\u6570\u91cf\u4e4b\u548c\u3002\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a

\\[ dp[i, a] = dp[i-1, a] + dp[i, a - coins[i-1]] \\]

\u5f53\u76ee\u6807\u91d1\u989d\u4e3a \\(0\\) \u65f6\uff0c\u65e0\u987b\u9009\u62e9\u4efb\u4f55\u786c\u5e01\u5373\u53ef\u51d1\u51fa\u76ee\u6807\u91d1\u989d\uff0c\u56e0\u6b64\u5e94\u5c06\u9996\u5217\u6240\u6709 \\(dp[i, 0]\\) \u90fd\u521d\u59cb\u5316\u4e3a \\(1\\) \u3002\u5f53\u65e0\u786c\u5e01\u65f6\uff0c\u65e0\u6cd5\u51d1\u51fa\u4efb\u4f55 \\(>0\\) \u7684\u76ee\u6807\u91d1\u989d\uff0c\u56e0\u6b64\u9996\u884c\u6240\u6709 \\(dp[0, a]\\) \u90fd\u7b49\u4e8e \\(0\\) \u3002

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2_2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"PythonC++JavaC#GoSwiftJSTSDartRustCZig coin_change_ii.py
def coin_change_ii_dp(coins: list[int], amt: int) -> int:\n\"\"\"\u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(coins)\n# \u521d\u59cb\u5316 dp \u8868\ndp = [[0] * (amt + 1) for _ in range(n + 1)]\n# \u521d\u59cb\u5316\u9996\u5217\nfor i in range(n + 1):\ndp[i][0] = 1\n# \u72b6\u6001\u8f6c\u79fb\nfor i in range(1, n + 1):\nfor a in range(1, amt + 1):\nif coins[i - 1] > a:\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a]\nelse:\n# \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\nreturn dp[n][amt]\n
coin_change_ii.cpp
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(vector<int> &coins, int amt) {\nint n = coins.size();\n// \u521d\u59cb\u5316 dp \u8868\nvector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));\n// \u521d\u59cb\u5316\u9996\u5217\nfor (int i = 0; i <= n; i++) {\ndp[i][0] = 1;\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n}\n}\n}\nreturn dp[n][amt];\n}\n
coin_change_ii.java
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(int[] coins, int amt) {\nint n = coins.length;\n// \u521d\u59cb\u5316 dp \u8868\nint[][] dp = new int[n + 1][amt + 1];\n// \u521d\u59cb\u5316\u9996\u5217\nfor (int i = 0; i <= n; i++) {\ndp[i][0] = 1;\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n}\n}\n}\nreturn dp[n][amt];\n}\n
coin_change_ii.cs
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint CoinChangeIIDP(int[] coins, int amt) {\nint n = coins.Length;\n// \u521d\u59cb\u5316 dp \u8868\nint[,] dp = new int[n + 1, amt + 1];\n// \u521d\u59cb\u5316\u9996\u5217\nfor (int i = 0; i <= n; i++) {\ndp[i, 0] = 1;\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i, a] = dp[i - 1, a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i, a] = dp[i - 1, a] + dp[i, a - coins[i - 1]];\n}\n}\n}\nreturn dp[n, amt];\n}\n
coin_change_ii.go
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDP(coins []int, amt int) int {\nn := len(coins)\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([][]int, n+1)\nfor i := 0; i <= n; i++ {\ndp[i] = make([]int, amt+1)\n}\n// \u521d\u59cb\u5316\u9996\u5217\nfor i := 0; i <= n; i++ {\ndp[i][0] = 1\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i := 1; i <= n; i++ {\nfor a := 1; a <= amt; a++ {\nif coins[i-1] > a {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i-1][a]\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i][a] = dp[i-1][a] + dp[i][a-coins[i-1]]\n}\n}\n}\nreturn dp[n][amt]\n}\n
coin_change_ii.swift
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDP(coins: [Int], amt: Int) -> Int {\nlet n = coins.count\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)\n// \u521d\u59cb\u5316\u9996\u5217\nfor i in stride(from: 0, through: n, by: 1) {\ndp[i][0] = 1\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor i in stride(from: 1, through: n, by: 1) {\nfor a in stride(from: 1, through: amt, by: 1) {\nif coins[i - 1] > a {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a]\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n}\n}\n}\nreturn dp[n][amt]\n}\n
coin_change_ii.js
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeIIDP(coins, amt) {\nconst n = coins.length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: n + 1 }, () =>\nArray.from({ length: amt + 1 }, () => 0)\n);\n// \u521d\u59cb\u5316\u9996\u5217\nfor (let i = 0; i <= n; i++) {\ndp[i][0] = 1;\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\nfor (let a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n}\n}\n}\nreturn dp[n][amt];\n}\n
coin_change_ii.ts
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeIIDP(coins: Array<number>, amt: number): number {\nconst n = coins.length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: n + 1 }, () =>\nArray.from({ length: amt + 1 }, () => 0)\n);\n// \u521d\u59cb\u5316\u9996\u5217\nfor (let i = 0; i <= n; i++) {\ndp[i][0] = 1;\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\nfor (let a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n}\n}\n}\nreturn dp[n][amt];\n}\n
coin_change_ii.dart
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(List<int> coins, int amt) {\nint n = coins.length;\n// \u521d\u59cb\u5316 dp \u8868\nList<List<int>> dp = List.generate(n + 1, (index) => List.filled(amt + 1, 0));\n// \u521d\u59cb\u5316\u9996\u5217\nfor (int i = 0; i <= n; i++) {\ndp[i][0] = 1;\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n}\n}\n}\nreturn dp[n][amt];\n}\n
coin_change_ii.rs
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfn coin_change_ii_dp(coins: &[i32], amt: usize) -> i32 {\nlet n = coins.len();\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![vec![0; amt + 1]; n + 1];\n// \u521d\u59cb\u5316\u9996\u5217\nfor i in 0..= n {\ndp[i][0] = 1;\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor i in 1..=n {\nfor a in 1..=amt {\nif coins[i - 1] > a as i32 {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1] as usize];\n}\n}\n}\ndp[n][amt]\n}\n
coin_change_ii.c
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(int coins[], int amt, int coinsSize) {\nint n = coinsSize;\n// \u521d\u59cb\u5316 dp \u8868\nint dp[n + 1][amt + 1];\nmemset(dp, 0, sizeof(dp));\n// \u521d\u59cb\u5316\u9996\u5217\nfor (int i = 0; i <= n; i++) {\ndp[i][0] = 1;\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n}\n}\n}\nreturn dp[n][amt];\n}\n
coin_change_ii.zig
// \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212\nfn coinChangeIIDP(comptime coins: []i32, comptime amt: usize) i32 {\ncomptime var n = coins.len;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_][amt + 1]i32{[_]i32{0} ** (amt + 1)} ** (n + 1);\n// \u521d\u59cb\u5316\u9996\u5217\nfor (0..n + 1) |i| {\ndp[i][0] = 1;\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor (1..n + 1) |i| {\nfor (1..amt + 1) |a| {\nif (coins[i - 1] > @as(i32, @intCast(a))) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = dp[i - 1][a] + dp[i][a - @as(usize, @intCast(coins[i - 1]))];\n}\n}\n}\nreturn dp[n][amt];\n}\n
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3_2","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

\u7a7a\u95f4\u4f18\u5316\u5904\u7406\u65b9\u5f0f\u76f8\u540c\uff0c\u5220\u9664\u786c\u5e01\u7ef4\u5ea6\u5373\u53ef\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig coin_change_ii.py
def coin_change_ii_dp_comp(coins: list[int], amt: int) -> int:\n\"\"\"\u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(coins)\n# \u521d\u59cb\u5316 dp \u8868\ndp = [0] * (amt + 1)\ndp[0] = 1\n# \u72b6\u6001\u8f6c\u79fb\nfor i in range(1, n + 1):\n# \u6b63\u5e8f\u904d\u5386\nfor a in range(1, amt + 1):\nif coins[i - 1] > a:\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a]\nelse:\n# \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a - coins[i - 1]]\nreturn dp[amt]\n
coin_change_ii.cpp
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(vector<int> &coins, int amt) {\nint n = coins.size();\n// \u521d\u59cb\u5316 dp \u8868\nvector<int> dp(amt + 1, 0);\ndp[0] = 1;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a - coins[i - 1]];\n}\n}\n}\nreturn dp[amt];\n}\n
coin_change_ii.java
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(int[] coins, int amt) {\nint n = coins.length;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[amt + 1];\ndp[0] = 1;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a - coins[i - 1]];\n}\n}\n}\nreturn dp[amt];\n}\n
coin_change_ii.cs
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint CoinChangeIIDPComp(int[] coins, int amt) {\nint n = coins.Length;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[amt + 1];\ndp[0] = 1;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a - coins[i - 1]];\n}\n}\n}\nreturn dp[amt];\n}\n
coin_change_ii.go
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDPComp(coins []int, amt int) int {\nn := len(coins)\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([]int, amt+1)\ndp[0] = 1\n// \u72b6\u6001\u8f6c\u79fb\nfor i := 1; i <= n; i++ {\n// \u5012\u5e8f\u904d\u5386\nfor a := 1; a <= amt; a++ {\nif coins[i-1] > a {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a]\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a-coins[i-1]]\n}\n}\n}\nreturn dp[amt]\n}\n
coin_change_ii.swift
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDPComp(coins: [Int], amt: Int) -> Int {\nlet n = coins.count\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: 0, count: amt + 1)\ndp[0] = 1\n// \u72b6\u6001\u8f6c\u79fb\nfor i in stride(from: 1, through: n, by: 1) {\nfor a in stride(from: 1, through: amt, by: 1) {\nif coins[i - 1] > a {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a]\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a - coins[i - 1]]\n}\n}\n}\nreturn dp[amt]\n}\n
coin_change_ii.js
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeIIDPComp(coins, amt) {\nconst n = coins.length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: amt + 1 }, () => 0);\ndp[0] = 1;\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\nfor (let a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a - coins[i - 1]];\n}\n}\n}\nreturn dp[amt];\n}\n
coin_change_ii.ts
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeIIDPComp(coins: Array<number>, amt: number): number {\nconst n = coins.length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: amt + 1 }, () => 0);\ndp[0] = 1;\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\nfor (let a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a - coins[i - 1]];\n}\n}\n}\nreturn dp[amt];\n}\n
coin_change_ii.dart
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(List<int> coins, int amt) {\nint n = coins.length;\n// \u521d\u59cb\u5316 dp \u8868\nList<int> dp = List.filled(amt + 1, 0);\ndp[0] = 1;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a - coins[i - 1]];\n}\n}\n}\nreturn dp[amt];\n}\n
coin_change_ii.rs
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn coin_change_ii_dp_comp(coins: &[i32], amt: usize) -> i32 {\nlet n = coins.len();\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![0; amt + 1];\ndp[0] = 1;\n// \u72b6\u6001\u8f6c\u79fb\nfor i in 1..=n {\nfor a in 1..=amt {\nif coins[i - 1] > a as i32 {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = dp[a] + dp[a - coins[i - 1] as usize];\n}\n}\n}\ndp[amt]\n}\n
coin_change_ii.c
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(int coins[], int amt, int coinsSize) {\nint n = coinsSize;\n// \u521d\u59cb\u5316 dp \u8868\nint dp[amt + 1];\nmemset(dp, 0, sizeof(dp));\ndp[0] = 1;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a - coins[i - 1]];\n}\n}\n}\nreturn dp[amt];\n}\n
coin_change_ii.zig
// \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn coinChangeIIDPComp(comptime coins: []i32, comptime amt: usize) i32 {\ncomptime var n = coins.len;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_]i32{0} ** (amt + 1);\ndp[0] = 1;\n// \u72b6\u6001\u8f6c\u79fb\nfor (1..n + 1) |i| {\nfor (1..amt + 1) |a| {\nif (coins[i - 1] > @as(i32, @intCast(a))) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = dp[a] + dp[a - @as(usize, @intCast(coins[i - 1]))];\n}\n}\n}\nreturn dp[amt];\n}\n
"},{"location":"chapter_graph/","title":"\u7b2c 9 \u7ae0 \u00a0 \u56fe","text":"

Abstract

\u5728\u751f\u547d\u65c5\u9014\u4e2d\uff0c\u6211\u4eec\u5c31\u50cf\u662f\u6bcf\u4e2a\u8282\u70b9\uff0c\u88ab\u65e0\u6570\u770b\u4e0d\u89c1\u7684\u8fb9\u76f8\u8fde\u3002

\u6bcf\u4e00\u6b21\u7684\u76f8\u8bc6\u4e0e\u76f8\u79bb\uff0c\u90fd\u5728\u8fd9\u5f20\u5de8\u5927\u7684\u7f51\u7edc\u56fe\u4e2d\u7559\u4e0b\u72ec\u7279\u7684\u5370\u8bb0\u3002

"},{"location":"chapter_graph/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 9.1 \u00a0 \u56fe
  • 9.2 \u00a0 \u56fe\u57fa\u7840\u64cd\u4f5c
  • 9.3 \u00a0 \u56fe\u7684\u904d\u5386
  • 9.4 \u00a0 \u5c0f\u7ed3
"},{"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} \\]

\u5982\u679c\u5c06\u9876\u70b9\u770b\u4f5c\u8282\u70b9\uff0c\u5c06\u8fb9\u770b\u4f5c\u8fde\u63a5\u5404\u4e2a\u8282\u70b9\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u56fe\u770b\u4f5c\u662f\u4e00\u79cd\u4ece\u94fe\u8868\u62d3\u5c55\u800c\u6765\u7684\u6570\u636e\u7ed3\u6784\u3002\u5982\u56fe 9-1 \u6240\u793a\uff0c\u76f8\u8f83\u4e8e\u7ebf\u6027\u5173\u7cfb\uff08\u94fe\u8868\uff09\u548c\u5206\u6cbb\u5173\u7cfb\uff08\u6811\uff09\uff0c\u7f51\u7edc\u5173\u7cfb\uff08\u56fe\uff09\u7684\u81ea\u7531\u5ea6\u66f4\u9ad8\uff0c\u4ece\u800c\u66f4\u4e3a\u590d\u6742\u3002

\u56fe 9-1 \u00a0 \u94fe\u8868\u3001\u6811\u3001\u56fe\u4e4b\u95f4\u7684\u5173\u7cfb

"},{"location":"chapter_graph/graph/#911","title":"9.1.1 \u00a0 \u56fe\u5e38\u89c1\u7c7b\u578b\u4e0e\u672f\u8bed","text":"

\u6839\u636e\u8fb9\u662f\u5426\u5177\u6709\u65b9\u5411\uff0c\u53ef\u5206\u4e3a\u56fe 9-2 \u6240\u793a\u7684\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\u3002
  • \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\u3002

\u56fe 9-2 \u00a0 \u6709\u5411\u56fe\u4e0e\u65e0\u5411\u56fe

\u6839\u636e\u6240\u6709\u9876\u70b9\u662f\u5426\u8fde\u901a\uff0c\u53ef\u5206\u4e3a\u56fe 9-3 \u6240\u793a\u7684\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\u3002
  • \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\u3002

\u56fe 9-3 \u00a0 \u8fde\u901a\u56fe\u4e0e\u975e\u8fde\u901a\u56fe

\u6211\u4eec\u8fd8\u53ef\u4ee5\u4e3a\u8fb9\u6dfb\u52a0\u201c\u6743\u91cd\u201d\u53d8\u91cf\uff0c\u4ece\u800c\u5f97\u5230\u56fe 9-4 \u6240\u793a\u7684\u300c\u6709\u6743\u56fe weighted graph\u300d\u3002\u4f8b\u5982\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

\u56fe 9-4 \u00a0 \u6709\u6743\u56fe\u4e0e\u65e0\u6743\u56fe

\u56fe\u6570\u636e\u7ed3\u6784\u5305\u542b\u4ee5\u4e0b\u5e38\u7528\u672f\u8bed\u3002

  • \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\u56fe 9-4 \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\u56fe 9-4 \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\uff1a\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/#912","title":"9.1.2 \u00a0 \u56fe\u7684\u8868\u793a","text":"

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

"},{"location":"chapter_graph/graph/#1","title":"1. \u00a0 \u90bb\u63a5\u77e9\u9635","text":"

\u8bbe\u56fe\u7684\u9876\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u300c\u90bb\u63a5\u77e9\u9635 adjacency matrix\u300d\u4f7f\u7528\u4e00\u4e2a \\(n \\times n\\) \u5927\u5c0f\u7684\u77e9\u9635\u6765\u8868\u793a\u56fe\uff0c\u6bcf\u4e00\u884c\uff08\u5217\uff09\u4ee3\u8868\u4e00\u4e2a\u9876\u70b9\uff0c\u77e9\u9635\u5143\u7d20\u4ee3\u8868\u8fb9\uff0c\u7528 \\(1\\) \u6216 \\(0\\) \u8868\u793a\u4e24\u4e2a\u9876\u70b9\u4e4b\u95f4\u662f\u5426\u5b58\u5728\u8fb9\u3002

\u5982\u56fe 9-5 \u6240\u793a\uff0c\u8bbe\u90bb\u63a5\u77e9\u9635\u4e3a \\(M\\)\u3001\u9876\u70b9\u5217\u8868\u4e3a \\(V\\) \uff0c\u90a3\u4e48\u77e9\u9635\u5143\u7d20 \\(M[i, j] = 1\\) \u8868\u793a\u9876\u70b9 \\(V[i]\\) \u5230\u9876\u70b9 \\(V[j]\\) \u4e4b\u95f4\u5b58\u5728\u8fb9\uff0c\u53cd\u4e4b \\(M[i, j] = 0\\) \u8868\u793a\u4e24\u9876\u70b9\u4e4b\u95f4\u65e0\u8fb9\u3002

\u56fe 9-5 \u00a0 \u56fe\u7684\u90bb\u63a5\u77e9\u9635\u8868\u793a

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

  • \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\\) \u548c \\(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":"2. \u00a0 \u90bb\u63a5\u8868","text":"

\u300c\u90bb\u63a5\u8868 adjacency list\u300d\u4f7f\u7528 \\(n\\) \u4e2a\u94fe\u8868\u6765\u8868\u793a\u56fe\uff0c\u94fe\u8868\u8282\u70b9\u8868\u793a\u9876\u70b9\u3002\u7b2c \\(i\\) \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\u56fe 9-6 \u5c55\u793a\u4e86\u4e00\u4e2a\u4f7f\u7528\u90bb\u63a5\u8868\u5b58\u50a8\u7684\u56fe\u7684\u793a\u4f8b\u3002

\u56fe 9-6 \u00a0 \u56fe\u7684\u90bb\u63a5\u8868\u8868\u793a

\u90bb\u63a5\u8868\u4ec5\u5b58\u50a8\u5b9e\u9645\u5b58\u5728\u7684\u8fb9\uff0c\u800c\u8fb9\u7684\u603b\u6570\u901a\u5e38\u8fdc\u5c0f\u4e8e \\(n^2\\) \uff0c\u56e0\u6b64\u5b83\u66f4\u52a0\u8282\u7701\u7a7a\u95f4\u3002\u7136\u800c\uff0c\u5728\u90bb\u63a5\u8868\u4e2d\u9700\u8981\u901a\u8fc7\u904d\u5386\u94fe\u8868\u6765\u67e5\u627e\u8fb9\uff0c\u56e0\u6b64\u5176\u65f6\u95f4\u6548\u7387\u4e0d\u5982\u90bb\u63a5\u77e9\u9635\u3002

\u89c2\u5bdf\u56fe 9-6 \uff0c\u90bb\u63a5\u8868\u7ed3\u6784\u4e0e\u54c8\u5e0c\u8868\u4e2d\u7684\u201c\u94fe\u5f0f\u5730\u5740\u201d\u975e\u5e38\u76f8\u4f3c\uff0c\u56e0\u6b64\u6211\u4eec\u4e5f\u53ef\u4ee5\u91c7\u7528\u7c7b\u4f3c\u65b9\u6cd5\u6765\u4f18\u5316\u6548\u7387\u3002\u6bd4\u5982\u5f53\u94fe\u8868\u8f83\u957f\u65f6\uff0c\u53ef\u4ee5\u5c06\u94fe\u8868\u8f6c\u5316\u4e3a AVL \u6811\u6216\u7ea2\u9ed1\u6811\uff0c\u4ece\u800c\u5c06\u65f6\u95f4\u6548\u7387\u4ece \\(O(n)\\) \u4f18\u5316\u81f3 \\(O(\\log n)\\) \uff1b\u8fd8\u53ef\u4ee5\u628a\u94fe\u8868\u8f6c\u6362\u4e3a\u54c8\u5e0c\u8868\uff0c\u4ece\u800c\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u964d\u4f4e\u81f3 \\(O(1)\\) \u3002

"},{"location":"chapter_graph/graph/#913","title":"9.1.3 \u00a0 \u56fe\u5e38\u89c1\u5e94\u7528","text":"

\u5982\u8868 9-1 \u6240\u793a\uff0c\u8bb8\u591a\u73b0\u5b9e\u7cfb\u7edf\u90fd\u53ef\u4ee5\u7528\u56fe\u6765\u5efa\u6a21\uff0c\u76f8\u5e94\u7684\u95ee\u9898\u4e5f\u53ef\u4ee5\u7ea6\u5316\u4e3a\u56fe\u8ba1\u7b97\u95ee\u9898\u3002

\u8868 9-1 \u00a0 \u73b0\u5b9e\u751f\u6d3b\u4e2d\u5e38\u89c1\u7684\u56fe

\u9876\u70b9 \u8fb9 \u56fe\u8ba1\u7b97\u95ee\u9898 \u793e\u4ea4\u7f51\u7edc \u7528\u6237 \u597d\u53cb\u5173\u7cfb \u6f5c\u5728\u597d\u53cb\u63a8\u8350 \u5730\u94c1\u7ebf\u8def \u7ad9\u70b9 \u7ad9\u70b9\u95f4\u7684\u8fde\u901a\u6027 \u6700\u77ed\u8def\u7ebf\u63a8\u8350 \u592a\u9633\u7cfb \u661f\u4f53 \u661f\u4f53\u95f4\u7684\u4e07\u6709\u5f15\u529b\u4f5c\u7528 \u884c\u661f\u8f68\u9053\u8ba1\u7b97"},{"location":"chapter_graph/graph_operations/","title":"9.2 \u00a0 \u56fe\u57fa\u7840\u64cd\u4f5c","text":"

\u56fe\u7684\u57fa\u7840\u64cd\u4f5c\u53ef\u5206\u4e3a\u5bf9\u201c\u8fb9\u201d\u7684\u64cd\u4f5c\u548c\u5bf9\u201c\u9876\u70b9\u201d\u7684\u64cd\u4f5c\u3002\u5728\u201c\u90bb\u63a5\u77e9\u9635\u201d\u548c\u201c\u90bb\u63a5\u8868\u201d\u4e24\u79cd\u8868\u793a\u65b9\u6cd5\u4e0b\uff0c\u5b9e\u73b0\u65b9\u5f0f\u6709\u6240\u4e0d\u540c\u3002

"},{"location":"chapter_graph/graph_operations/#921","title":"9.2.1 \u00a0 \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u7684\u5b9e\u73b0","text":"

\u7ed9\u5b9a\u4e00\u4e2a\u9876\u70b9\u6570\u91cf\u4e3a \\(n\\) \u7684\u65e0\u5411\u56fe\uff0c\u5219\u5404\u79cd\u64cd\u4f5c\u7684\u5b9e\u73b0\u65b9\u5f0f\u5982\u56fe 9-7 \u6240\u793a\u3002

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

\u56fe 9-7 \u00a0 \u90bb\u63a5\u77e9\u9635\u7684\u521d\u59cb\u5316\u3001\u589e\u5220\u8fb9\u3001\u589e\u5220\u9876\u70b9

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

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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]]):\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):\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):\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):\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):\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):\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.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(vector<int>(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.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.cs
/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nreadonly List<int> vertices;     // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\nreadonly List<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(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.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\u51fd\u6570 */\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.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.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.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
graph_adjacency_matrix.rs
/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\u578b */\npub struct GraphAdjMat {\n// \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\npub vertices: Vec<i32>,\n// \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\npub adj_mat: Vec<Vec<i32>>,\n}\nimpl GraphAdjMat {\n/* \u6784\u9020\u65b9\u6cd5 */\npub fn new(vertices: Vec<i32>, edges: Vec<[usize; 2]>) -> Self {\nlet mut graph = GraphAdjMat {\nvertices: vec![],\nadj_mat: vec![],\n};\n// \u6dfb\u52a0\u9876\u70b9\nfor val in vertices {\ngraph.add_vertex(val);\n}\n// \u6dfb\u52a0\u8fb9\n// \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfor edge in edges {\ngraph.add_edge(edge[0], edge[1])\n}\ngraph\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\npub fn size(&self) -> usize {\nself.vertices.len()\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npub fn add_vertex(&mut self, val: i32) {\nlet n = self.size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nself.vertices.push(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nself.adj_mat.push(vec![0; n]);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor row in &mut self.adj_mat {\nrow.push(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\npub fn remove_vertex(&mut self, index: usize) {\nif index >= self.size() {\npanic!(\"index error\")\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nself.vertices.remove(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nself.adj_mat.remove(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor row in &mut self.adj_mat {\nrow.remove(index);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\npub fn add_edge(&mut self, i: usize, j: usize) {\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif i >= self.size() || j >= self.size() || i == j {\npanic!(\"index error\")\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)\nself.adj_mat[i][j] = 1;\nself.adj_mat[j][i] = 1;\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\npub fn remove_edge(&mut self, i: usize, j: usize) {\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif i >= self.size() || j >= self.size() || i == j {\npanic!(\"index error\")\n}\nself.adj_mat[i][j] = 0;\nself.adj_mat[j][i] = 0;\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\npub fn print(&self) {\nprintln!(\"\u9876\u70b9\u5217\u8868 = {:?}\", self.vertices);\nprintln!(\"\u90bb\u63a5\u77e9\u9635 =\");\nprintln!(\"[\");\nfor row in &self.adj_mat {\nprintln!(\"  {:?},\", row);\n}\nprintln!(\"]\")\n}\n}\n
graph_adjacency_matrix.c
/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\u7ed3\u6784 */\nstruct graphAdjMat {\nint *vertices;         // \u9876\u70b9\u5217\u8868\nunsigned int **adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u8fb9\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\nunsigned int size;     // \u9876\u70b9\u6570\u91cf\nunsigned int capacity; // \u56fe\u5bb9\u91cf\n};\ntypedef struct graphAdjMat graphAdjMat;\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid addEdge(graphAdjMat *t, int i, int j) {\n// \u8d8a\u754c\u68c0\u67e5\nif (i < 0 || j < 0 || i >= t->size || j >= t->size || i == j) {\nprintf(\"Out of range in %s:%d\\n\", __FILE__, __LINE__);\nexit(1);\n}\n// \u6dfb\u52a0\u8fb9\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nt->adjMat[i][j] = 1;\nt->adjMat[j][i] = 1;\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid removeEdge(graphAdjMat *t, int i, int j) {\n// \u8d8a\u754c\u68c0\u67e5\nif (i < 0 || j < 0 || i >= t->size || j >= t->size || i == j) {\nprintf(\"Out of range in %s:%d\\n\", __FILE__, __LINE__);\nexit(1);\n}\n// \u5220\u9664\u8fb9\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nt->adjMat[i][j] = 0;\nt->adjMat[j][i] = 0;\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(graphAdjMat *t, int val) {\n// \u5982\u679c\u5b9e\u9645\u4f7f\u7528\u4e0d\u5927\u4e8e\u9884\u8bbe\u7a7a\u95f4\uff0c\u5219\u76f4\u63a5\u521d\u59cb\u5316\u65b0\u7a7a\u95f4\nif (t->size < t->capacity) {\nt->vertices[t->size] = val; // \u521d\u59cb\u5316\u65b0\u9876\u70b9\u503c\nfor (int i = 0; i < t->size; i++) {\nt->adjMat[i][t->size] = 0; // \u90bb\u63a5\u77e9\u65b0\u5217\u9635\u7f6e0\n}\nmemset(t->adjMat[t->size], 0, sizeof(unsigned int) * (t->size + 1)); // \u5c06\u65b0\u589e\u884c\u7f6e 0\nt->size++;\nreturn;\n}\n// \u6269\u5bb9\uff0c\u7533\u8bf7\u65b0\u7684\u9876\u70b9\u6570\u7ec4\nint *temp = (int *)malloc(sizeof(int) * (t->size * 2));\nmemcpy(temp, t->vertices, sizeof(int) * t->size);\ntemp[t->size] = val;\n// \u91ca\u653e\u539f\u6570\u7ec4\nfree(t->vertices);\nt->vertices = temp;\n// \u6269\u5bb9\uff0c\u7533\u8bf7\u65b0\u7684\u4e8c\u7ef4\u6570\u7ec4\nunsigned int **tempMat = (unsigned int **)malloc(sizeof(unsigned int *) * t->size * 2);\nunsigned int *tempMatLine = (unsigned int *)malloc(sizeof(unsigned int) * (t->size * 2) * (t->size * 2));\nmemset(tempMatLine, 0, sizeof(unsigned int) * (t->size * 2) * (t->size * 2));\nfor (int k = 0; k < t->size * 2; k++) {\ntempMat[k] = tempMatLine + k * (t->size * 2);\n}\nfor (int i = 0; i < t->size; i++) {\nmemcpy(tempMat[i], t->adjMat[i], sizeof(unsigned int) * t->size); // \u539f\u6570\u636e\u590d\u5236\u5230\u65b0\u6570\u7ec4\n}\nfor (int i = 0; i < t->size; i++) {\ntempMat[i][t->size] = 0; // \u5c06\u65b0\u589e\u5217\u7f6e 0\n}\nmemset(tempMat[t->size], 0, sizeof(unsigned int) * (t->size + 1)); // \u5c06\u65b0\u589e\u884c\u7f6e 0\n// \u91ca\u653e\u539f\u6570\u7ec4\nfree(t->adjMat[0]);\nfree(t->adjMat);\n// \u6269\u5bb9\u540e\uff0c\u6307\u5411\u65b0\u5730\u5740\nt->adjMat = tempMat; // \u6307\u5411\u65b0\u7684\u90bb\u63a5\u77e9\u9635\u5730\u5740\nt->capacity = t->size * 2;\nt->size++;\n}\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(graphAdjMat *t, unsigned int index) {\n// \u8d8a\u754c\u68c0\u67e5\nif (index < 0 || index >= t->size) {\nprintf(\"Out of range in %s:%d\\n\", __FILE__, __LINE__);\nexit(1);\n}\nfor (int i = index; i < t->size - 1; i++) {\nt->vertices[i] = t->vertices[i + 1]; // \u6e05\u9664\u5220\u9664\u7684\u9876\u70b9\uff0c\u5e76\u5c06\u5176\u540e\u6240\u6709\u9876\u70b9\u524d\u79fb\n}\nt->vertices[t->size - 1] = 0; // \u5c06\u88ab\u524d\u79fb\u7684\u6700\u540e\u4e00\u4e2a\u9876\u70b9\u7f6e 0\n// \u6e05\u9664\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7684\u5217\nfor (int i = 0; i < t->size - 1; i++) {\nif (i < index) {\nfor (int j = index; j < t->size - 1; j++) {\nt->adjMat[i][j] = t->adjMat[i][j + 1]; // \u88ab\u5220\u9664\u5217\u540e\u7684\u6240\u6709\u5217\u524d\u79fb\n}\n} else {\nmemcpy(t->adjMat[i], t->adjMat[i + 1], sizeof(unsigned int) * t->size); // \u88ab\u5220\u9664\u884c\u7684\u4e0b\u65b9\u6240\u6709\u884c\u4e0a\u79fb\nfor (int j = index; j < t->size; j++) {\nt->adjMat[i][j] = t->adjMat[i][j + 1]; // \u88ab\u5220\u9664\u5217\u540e\u7684\u6240\u6709\u5217\u524d\u79fb\n}\n}\n}\nt->size--;\n}\n/* \u6253\u5370\u9876\u70b9\u4e0e\u90bb\u63a5\u77e9\u9635 */\nvoid printGraph(graphAdjMat *t) {\nif (t->size == 0) {\nprintf(\"graph is empty\\n\");\nreturn;\n}\nprintf(\"\u9876\u70b9\u5217\u8868 = [\");\nfor (int i = 0; i < t->size; i++) {\nif (i != t->size - 1) {\nprintf(\"%d, \", t->vertices[i]);\n} else {\nprintf(\"%d\", t->vertices[i]);\n}\n}\nprintf(\"]\\n\");\nprintf(\"\u90bb\u63a5\u77e9\u9635 =\\n[\\n\");\nfor (int i = 0; i < t->size; i++) {\nprintf(\"  [\");\nfor (int j = 0; j < t->size; j++) {\nif (j != t->size - 1) {\nprintf(\"%u, \", t->adjMat[i][j]);\n} else {\nprintf(\"%u\", t->adjMat[i][j]);\n}\n}\nprintf(\"],\\n\");\n}\nprintf(\"]\\n\");\n}\n/* \u6784\u9020\u51fd\u6570 */\ngraphAdjMat *newGraphAjdMat(unsigned int numberVertices, int *vertices, unsigned int **adjMat) {\n// \u7533\u8bf7\u5185\u5b58\ngraphAdjMat *newGraph = (graphAdjMat *)malloc(sizeof(graphAdjMat));   // \u4e3a\u56fe\u5206\u914d\u5185\u5b58\nnewGraph->vertices = (int *)malloc(sizeof(int) * numberVertices * 2); // \u4e3a\u9876\u70b9\u5217\u8868\u5206\u914d\u5185\u5b58\nnewGraph->adjMat = (unsigned int **)malloc(sizeof(unsigned int *) * numberVertices * 2); // \u4e3a\u90bb\u63a5\u77e9\u9635\u5206\u914d\u4e8c\u7ef4\u5185\u5b58\nunsigned int *temp = (unsigned int *)malloc(sizeof(unsigned int) * numberVertices * 2 * numberVertices * 2); // \u4e3a\u90bb\u63a5\u77e9\u9635\u5206\u914d\u4e00\u7ef4\u5185\u5b58\nnewGraph->size = numberVertices;                // \u521d\u59cb\u5316\u9876\u70b9\u6570\u91cf\nnewGraph->capacity = numberVertices * 2;        // \u521d\u59cb\u5316\u56fe\u5bb9\u91cf\n// \u914d\u7f6e\u4e8c\u7ef4\u6570\u7ec4\nfor (int i = 0; i < numberVertices * 2; i++) {\nnewGraph->adjMat[i] = temp + i * numberVertices * 2; // \u5c06\u4e8c\u7ef4\u6307\u9488\u6307\u5411\u4e00\u7ef4\u6570\u7ec4\n}\n// \u8d4b\u503c\nmemcpy(newGraph->vertices, vertices, sizeof(int) * numberVertices);\nfor (int i = 0; i < numberVertices; i++) {\nmemcpy(newGraph->adjMat[i], adjMat[i], sizeof(unsigned int) * numberVertices); // \u5c06\u4f20\u5165\u7684\u90bb\u63a5\u77e9\u9635\u8d4b\u503c\u7ed9\u7ed3\u6784\u4f53\u5185\u90bb\u63a5\u77e9\u9635\n}\n// \u8fd4\u56de\u7ed3\u6784\u4f53\u6307\u9488\nreturn newGraph;\n}\n
graph_adjacency_matrix.zig
[class]{GraphAdjMat}-[func]{}\n
"},{"location":"chapter_graph/graph_operations/#922","title":"9.2.2 \u00a0 \u57fa\u4e8e\u90bb\u63a5\u8868\u7684\u5b9e\u73b0","text":"

\u8bbe\u65e0\u5411\u56fe\u7684\u9876\u70b9\u603b\u6570\u4e3a \\(n\\)\u3001\u8fb9\u603b\u6570\u4e3a \\(m\\) \uff0c\u5219\u53ef\u6839\u636e\u56fe 9-8 \u6240\u793a\u7684\u65b9\u6cd5\u5b9e\u73b0\u5404\u79cd\u64cd\u4f5c\u3002

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

\u56fe 9-8 \u00a0 \u90bb\u63a5\u8868\u7684\u521d\u59cb\u5316\u3001\u589e\u5220\u8fb9\u3001\u589e\u5220\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\u800c\u8fd9\u6837\u505a\u662f\u6709\u539f\u56e0\u7684\u3002

  1. \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
  2. \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
  3. \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\u987b\u6539\u52a8\u5176\u4f59\u9876\u70b9\u4e86\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig 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]]):\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, list[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):\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):\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):\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):\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):\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.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.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.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();\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.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\u51fd\u6570 */\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\ng.adjList[vet1] = DeleteSliceElms(g.adjList[vet1], vet2)\ng.adjList[vet2] = DeleteSliceElms(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 v, list := range g.adjList {\ng.adjList[v] = DeleteSliceElms(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.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.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 (const 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 (const 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.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
graph_adjacency_list.rs
/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\u578b */\npub struct GraphAdjList {\n// \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\npub adj_list: HashMap<Vertex, Vec<Vertex>>,\n}\nimpl GraphAdjList {\n/* \u6784\u9020\u65b9\u6cd5 */\npub fn new(edges: Vec<[Vertex; 2]>) -> Self {\nlet mut graph = GraphAdjList {\nadj_list: HashMap::new(),\n};\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor edge in edges {\ngraph.add_vertex(edge[0]);\ngraph.add_vertex(edge[1]);\ngraph.add_edge(edge[0], edge[1]);\n}\ngraph\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n#[allow(unused)]\npub fn size(&self) -> usize {\nself.adj_list.len()\n}\n/* \u6dfb\u52a0\u8fb9 */\npub fn add_edge(&mut self, vet1: Vertex, vet2: Vertex) {\nif !self.adj_list.contains_key(&vet1) || !self.adj_list.contains_key(&vet2) || vet1 == vet2\n{\npanic!(\"value error\");\n}\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nself.adj_list.get_mut(&vet1).unwrap().push(vet2);\nself.adj_list.get_mut(&vet2).unwrap().push(vet1);\n}\n/* \u5220\u9664\u8fb9 */\n#[allow(unused)]\npub fn remove_edge(&mut self, vet1: Vertex, vet2: Vertex) {\nif !self.adj_list.contains_key(&vet1) || !self.adj_list.contains_key(&vet2) || vet1 == vet2\n{\npanic!(\"value error\");\n}\n// \u5220\u9664\u8fb9 vet1 - vet2\nself.adj_list\n.get_mut(&vet1)\n.unwrap()\n.retain(|&vet| vet != vet2);\nself.adj_list\n.get_mut(&vet2)\n.unwrap()\n.retain(|&vet| vet != vet1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npub fn add_vertex(&mut self, vet: Vertex) {\nif self.adj_list.contains_key(&vet) {\nreturn;\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nself.adj_list.insert(vet, vec![]);\n}\n/* \u5220\u9664\u9876\u70b9 */\n#[allow(unused)]\npub fn remove_vertex(&mut self, vet: Vertex) {\nif !self.adj_list.contains_key(&vet) {\npanic!(\"value error\");\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nself.adj_list.remove(&vet);\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor list in self.adj_list.values_mut() {\nlist.retain(|&v| v != vet);\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\npub fn print(&self) {\nprintln!(\"\u90bb\u63a5\u8868 =\");\nfor (vertex, list) in &self.adj_list {\nlet list = list.iter().map(|vertex| vertex.val).collect::<Vec<i32>>();\nprintln!(\"{}: {:?},\", vertex.val, list);\n}\n}\n}\n
graph_adjacency_list.c
/* \u57fa\u4e8e\u90bb\u63a5\u94fe\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\u7ed3\u6784 */\nstruct graphAdjList {\nVertex **verticesList; // \u90bb\u63a5\u8868\nunsigned int size;     // \u9876\u70b9\u6570\u91cf\nunsigned int capacity; // \u9876\u70b9\u5bb9\u91cf\n};\ntypedef struct graphAdjList graphAdjList;\n/* \u6dfb\u52a0\u8fb9 */\nvoid addEdge(graphAdjList *t, int i, int j) {\n// \u8d8a\u754c\u68c0\u67e5\nif (i < 0 || j < 0 || i == j || i >= t->size || j >= t->size) {\nprintf(\"Out of range in %s:%d\\n\", __FILE__, __LINE__);\nreturn;\n}\n// \u67e5\u627e\u6b32\u6dfb\u52a0\u8fb9\u7684\u9876\u70b9 vet1 - vet2\nVertex *vet1 = t->verticesList[i];\nVertex *vet2 = t->verticesList[j];\n// \u8fde\u63a5\u9876\u70b9 vet1 - vet2\npushBack(vet1->linked, vet2);\npushBack(vet2->linked, vet1);\n}\n/* \u5220\u9664\u8fb9 */\nvoid removeEdge(graphAdjList *t, int i, int j) {\n// \u8d8a\u754c\u68c0\u67e5\nif (i < 0 || j < 0 || i == j || i >= t->size || j >= t->size) {\nprintf(\"Out of range in %s:%d\\n\", __FILE__, __LINE__);\nreturn;\n}\n// \u67e5\u627e\u6b32\u5220\u9664\u8fb9\u7684\u9876\u70b9 vet1 - vet2\nVertex *vet1 = t->verticesList[i];\nVertex *vet2 = t->verticesList[j];\n// \u79fb\u9664\u5f85\u5220\u9664\u8fb9 vet1 - vet2\nremoveLink(vet1->linked, vet2);\nremoveLink(vet2->linked, vet1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(graphAdjList *t, int val) {\n// \u82e5\u5927\u5c0f\u8d85\u8fc7\u5bb9\u91cf\uff0c\u5219\u6269\u5bb9\nif (t->size >= t->capacity) {\nVertex **tempList = (Vertex **)malloc(sizeof(Vertex *) * 2 * t->capacity);\nmemcpy(tempList, t->verticesList, sizeof(Vertex *) * t->size);\nfree(t->verticesList);         // \u91ca\u653e\u539f\u90bb\u63a5\u8868\u5185\u5b58\nt->verticesList = tempList;    // \u6307\u5411\u65b0\u90bb\u63a5\u8868\nt->capacity = t->capacity * 2; // \u5bb9\u91cf\u6269\u5927\u81f32\u500d\n}\n// \u7533\u8bf7\u65b0\u9876\u70b9\u5185\u5b58\u5e76\u5c06\u65b0\u9876\u70b9\u5730\u5740\u5b58\u5165\u9876\u70b9\u5217\u8868\nVertex *newV = newVertex(val);    // \u5efa\u7acb\u65b0\u9876\u70b9\nnewV->pos = t->size;              // \u4e3a\u65b0\u9876\u70b9\u6807\u8bb0\u4e0b\u6807\nnewV->linked = newLinklist(newV); // \u4e3a\u65b0\u9876\u70b9\u5efa\u7acb\u94fe\u8868\nt->verticesList[t->size] = newV;  // \u5c06\u65b0\u9876\u70b9\u52a0\u5165\u90bb\u63a5\u8868\nt->size++;\n}\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(graphAdjList *t, unsigned int index) {\n// \u8d8a\u754c\u68c0\u67e5\nif (index < 0 || index >= t->size) {\nprintf(\"Out of range in %s:%d\\n\", __FILE__, __LINE__);\nexit(1);\n}\nVertex *vet = t->verticesList[index]; // \u67e5\u627e\u5f85\u5220\u8282\u70b9\nif (vet == 0) {                       // \u82e5\u4e0d\u5b58\u5728\u8be5\u8282\u70b9\uff0c\u5219\u8fd4\u56de\nprintf(\"index is:%d\\n\", index);\nprintf(\"Out of range in %s:%d\\n\", __FILE__, __LINE__);\nreturn;\n}\n// \u904d\u5386\u5f85\u5220\u9664\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5c06\u6240\u6709\u4e0e\u5f85\u5220\u9664\u7ed3\u70b9\u6709\u5173\u7684\u8fb9\u5220\u9664\nNode *temp = vet->linked->head->next;\nwhile (temp != 0) {\nremoveLink(temp->val->linked, vet); // \u5220\u9664\u4e0e\u8be5\u9876\u70b9\u6709\u5173\u7684\u8fb9\ntemp = temp->next;\n}\n// \u5c06\u9876\u70b9\u524d\u79fb\nfor (int i = index; i < t->size - 1; i++) {\nt->verticesList[i] = t->verticesList[i + 1]; // \u9876\u70b9\u524d\u79fb\nt->verticesList[i]->pos--;                   // \u6240\u6709\u524d\u79fb\u7684\u9876\u70b9\u7d22\u5f15\u503c\u51cf1\n}\nt->verticesList[t->size - 1] = 0; // \u5c06\u88ab\u5220\u9664\u9876\u70b9\u7684\u4f4d\u7f6e\u7f6e 0\nt->size--;\n// \u91ca\u653e\u5185\u5b58\nfreeVertex(vet);\n}\n/* \u6253\u5370\u9876\u70b9\u4e0e\u90bb\u63a5\u77e9\u9635 */\nvoid printGraph(graphAdjList *t) {\nprintf(\"\u90bb\u63a5\u8868  =\\n\");\nfor (int i = 0; i < t->size; i++) {\nNode *n = t->verticesList[i]->linked->head->next;\nprintf(\"%d: [\", t->verticesList[i]->val);\nwhile (n != 0) {\nif (n->next != 0) {\nprintf(\"%d, \", n->val->val);\n} else {\nprintf(\"%d\", n->val->val);\n}\nn = n->next;\n}\nprintf(\"]\\n\");\n}\n}\n/* \u6784\u9020\u51fd\u6570 */\ngraphAdjList *newGraphAdjList(unsigned int verticesCapacity) {\n// \u7533\u8bf7\u5185\u5b58\ngraphAdjList *newGraph = (graphAdjList *)malloc(sizeof(graphAdjList));\n// \u5efa\u7acb\u9876\u70b9\u8868\u5e76\u5206\u914d\u5185\u5b58\nnewGraph->verticesList = (Vertex **)malloc(sizeof(Vertex *) * verticesCapacity); // \u4e3a\u9876\u70b9\u5217\u8868\u5206\u914d\u5185\u5b58\nmemset(newGraph->verticesList, 0, sizeof(Vertex *) * verticesCapacity);          // \u9876\u70b9\u5217\u8868\u7f6e 0\nnewGraph->size = 0;                                                              // \u521d\u59cb\u5316\u9876\u70b9\u6570\u91cf\nnewGraph->capacity = verticesCapacity;                                           // \u521d\u59cb\u5316\u9876\u70b9\u5bb9\u91cf\n// \u8fd4\u56de\u56fe\u6307\u9488\nreturn newGraph;\n}\n
graph_adjacency_list.zig
[class]{GraphAdjList}-[func]{}\n
"},{"location":"chapter_graph/graph_operations/#923","title":"9.2.3 \u00a0 \u6548\u7387\u5bf9\u6bd4","text":"

\u8bbe\u56fe\u4e2d\u5171\u6709 \\(n\\) \u4e2a\u9876\u70b9\u548c \\(m\\) \u6761\u8fb9\uff0c\u8868 9-2 \u5bf9\u6bd4\u4e86\u90bb\u63a5\u77e9\u9635\u548c\u90bb\u63a5\u8868\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u6548\u7387\u3002

\u8868 9-2 \u00a0 \u90bb\u63a5\u77e9\u9635\u4e0e\u90bb\u63a5\u8868\u5bf9\u6bd4

\u90bb\u63a5\u77e9\u9635 \u90bb\u63a5\u8868\uff08\u94fe\u8868\uff09 \u90bb\u63a5\u8868\uff08\u54c8\u5e0c\u8868\uff09 \u5224\u65ad\u662f\u5426\u90bb\u63a5 \\(O(1)\\) \\(O(m)\\) \\(O(1)\\) \u6dfb\u52a0\u8fb9 \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) \u5220\u9664\u8fb9 \\(O(1)\\) \\(O(m)\\) \\(O(1)\\) \u6dfb\u52a0\u9876\u70b9 \\(O(n)\\) \\(O(1)\\) \\(O(1)\\) \u5220\u9664\u9876\u70b9 \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n)\\) \u5185\u5b58\u7a7a\u95f4\u5360\u7528 \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n + m)\\)

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

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

\u56fe\u548c\u6811\u90fd\u9700\u8981\u5e94\u7528\u641c\u7d22\u7b97\u6cd5\u6765\u5b9e\u73b0\u904d\u5386\u64cd\u4f5c\u3002\u56fe\u7684\u904d\u5386\u65b9\u5f0f\u53ef\u5206\u4e3a\u4e24\u79cd\uff1a\u300c\u5e7f\u5ea6\u4f18\u5148\u904d\u5386 breadth-first traversal\u300d\u548c\u300c\u6df1\u5ea6\u4f18\u5148\u904d\u5386 depth-first traversal\u300d\u3002\u5b83\u4eec\u4e5f\u5e38\u88ab\u79f0\u4e3a\u300c\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22 breadth-first search\u300d\u548c\u300c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22 depth-first search\u300d\uff0c\u7b80\u79f0 BFS \u548c DFS \u3002

"},{"location":"chapter_graph/graph_traversal/#931","title":"9.3.1 \u00a0 \u5e7f\u5ea6\u4f18\u5148\u904d\u5386","text":"

\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u662f\u4e00\u79cd\u7531\u8fd1\u53ca\u8fdc\u7684\u904d\u5386\u65b9\u5f0f\uff0c\u4ece\u67d0\u4e2a\u8282\u70b9\u51fa\u53d1\uff0c\u59cb\u7ec8\u4f18\u5148\u8bbf\u95ee\u8ddd\u79bb\u6700\u8fd1\u7684\u9876\u70b9\uff0c\u5e76\u4e00\u5c42\u5c42\u5411\u5916\u6269\u5f20\u3002\u5982\u56fe 9-9 \u6240\u793a\uff0c\u4ece\u5de6\u4e0a\u89d2\u9876\u70b9\u51fa\u53d1\uff0c\u5148\u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff0c\u7136\u540e\u904d\u5386\u4e0b\u4e00\u4e2a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u6240\u6709\u9876\u70b9\u8bbf\u95ee\u5b8c\u6bd5\u3002

\u56fe 9-9 \u00a0 \u56fe\u7684\u5e7f\u5ea6\u4f18\u5148\u904d\u5386

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

BFS \u901a\u5e38\u501f\u52a9\u961f\u5217\u6765\u5b9e\u73b0\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\u3002
  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\u3002
  3. \u5faa\u73af\u6b65\u9aa4 2. \uff0c\u76f4\u5230\u6240\u6709\u9876\u70b9\u88ab\u8bbf\u95ee\u5b8c\u6210\u540e\u7ed3\u675f\u3002

\u4e3a\u4e86\u9632\u6b62\u91cd\u590d\u904d\u5386\u9876\u70b9\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868 visited \u6765\u8bb0\u5f55\u54ea\u4e9b\u8282\u70b9\u5df2\u88ab\u8bbf\u95ee\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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<>();\nvisited.add(startVet);\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nQueue<Vertex> que = new LinkedList<>();\nque.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.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();\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nHashSet<Vertex> visited = new() { startVet };\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nQueue<Vertex> que = new();\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.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.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.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.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
graph_bfs.rs
/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfn graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nlet mut res = vec![];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nlet mut visited = HashSet::new();\nvisited.insert(start_vet);\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nlet mut que = VecDeque::new();\nque.push_back(start_vet);\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile !que.is_empty() {\nlet vet = que.pop_front().unwrap(); // \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\nif let Some(adj_vets) = graph.adj_list.get(&vet) {\nfor &adj_vet in adj_vets {\nif visited.contains(&adj_vet) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nque.push_back(adj_vet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.insert(adj_vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nres\n}\n
graph_bfs.c
/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\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\nVertex **graphBFS(graphAdjList *t, Vertex *startVet) {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nVertex **res = (Vertex **)malloc(sizeof(Vertex *) * t->size);\nmemset(res, 0, sizeof(Vertex *) * t->size);\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nqueue *que = newQueue(t->size);\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nhashTable *visited = newHash(t->size);\nint resIndex = 0;\nqueuePush(que, startVet);         // \u5c06\u7b2c\u4e00\u4e2a\u5143\u7d20\u5165\u961f\nhashMark(visited, startVet->pos); // \u6807\u8bb0\u7b2c\u4e00\u4e2a\u5165\u961f\u7684\u9876\u70b9\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile (que->head < que->tail) {\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u8fb9\u94fe\u8868\uff0c\u5c06\u6240\u6709\u4e0e\u8be5\u9876\u70b9\u6709\u8fde\u63a5\u7684\uff0c\u5e76\u4e14\u672a\u88ab\u6807\u8bb0\u7684\u9876\u70b9\u5165\u961f\nNode *n = queueTop(que)->linked->head->next;\nwhile (n != 0) {\n// \u67e5\u8be2\u54c8\u5e0c\u8868\uff0c\u82e5\u8be5\u7d22\u5f15\u7684\u9876\u70b9\u5df2\u5165\u961f\uff0c\u5219\u8df3\u8fc7\uff0c\u5426\u5219\u5165\u961f\u5e76\u6807\u8bb0\nif (hashQuery(visited, n->val->pos) == 1) {\nn = n->next;\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nqueuePush(que, n->val);         // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nhashMark(visited, n->val->pos); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n// \u961f\u9996\u5143\u7d20\u5b58\u5165\u6570\u7ec4\nres[resIndex] = queueTop(que); // \u961f\u9996\u9876\u70b9\u52a0\u5165\u9876\u70b9\u904d\u5386\u5e8f\u5217\nresIndex++;\nqueuePop(que); // \u961f\u9996\u5143\u7d20\u51fa\u961f\n}\n// \u91ca\u653e\u5185\u5b58\nfreeQueue(que);\nfreeHash(visited);\nresIndex = 0;\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res;\n}\n
graph_bfs.zig
[class]{}-[func]{graphBFS}\n

\u4ee3\u7801\u76f8\u5bf9\u62bd\u8c61\uff0c\u5efa\u8bae\u5bf9\u7167\u56fe 9-10 \u6765\u52a0\u6df1\u7406\u89e3\u3002

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

\u56fe 9-10 \u00a0 \u56fe\u7684\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u6b65\u9aa4

\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u7684\u5e8f\u5217\u662f\u5426\u552f\u4e00\uff1f

\u4e0d\u552f\u4e00\u3002\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u53ea\u8981\u6c42\u6309\u201c\u7531\u8fd1\u53ca\u8fdc\u201d\u7684\u987a\u5e8f\u904d\u5386\uff0c\u800c\u591a\u4e2a\u76f8\u540c\u8ddd\u79bb\u7684\u9876\u70b9\u7684\u904d\u5386\u987a\u5e8f\u662f\u5141\u8bb8\u88ab\u4efb\u610f\u6253\u4e71\u7684\u3002\u4ee5\u56fe 9-10 \u4e3a\u4f8b\uff0c\u9876\u70b9 \\(1\\)\u3001\\(3\\) \u7684\u8bbf\u95ee\u987a\u5e8f\u53ef\u4ee5\u4ea4\u6362\u3001\u9876\u70b9 \\(2\\)\u3001\\(4\\)\u3001\\(6\\) \u7684\u8bbf\u95ee\u987a\u5e8f\u4e5f\u53ef\u4ee5\u4efb\u610f\u4ea4\u6362\u3002

"},{"location":"chapter_graph/graph_traversal/#2","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"

\u65f6\u95f4\u590d\u6742\u5ea6\uff1a \u6240\u6709\u9876\u70b9\u90fd\u4f1a\u5165\u961f\u5e76\u51fa\u961f\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(|V|)\\) \u65f6\u95f4\uff1b\u5728\u904d\u5386\u90bb\u63a5\u9876\u70b9\u7684\u8fc7\u7a0b\u4e2d\uff0c\u7531\u4e8e\u662f\u65e0\u5411\u56fe\uff0c\u56e0\u6b64\u6240\u6709\u8fb9\u90fd\u4f1a\u88ab\u8bbf\u95ee \\(2\\) \u6b21\uff0c\u4f7f\u7528 \\(O(2|E|)\\) \u65f6\u95f4\uff1b\u603b\u4f53\u4f7f\u7528 \\(O(|V| + |E|)\\) \u65f6\u95f4\u3002

\u7a7a\u95f4\u590d\u6742\u5ea6\uff1a \u5217\u8868 res \uff0c\u54c8\u5e0c\u8868 visited \uff0c\u961f\u5217 que \u4e2d\u7684\u9876\u70b9\u6570\u91cf\u6700\u591a\u4e3a \\(|V|\\) \uff0c\u4f7f\u7528 \\(O(|V|)\\) \u7a7a\u95f4\u3002

"},{"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\u5982\u56fe 9-11 \u6240\u793a\uff0c\u4ece\u5de6\u4e0a\u89d2\u9876\u70b9\u51fa\u53d1\uff0c\u8bbf\u95ee\u5f53\u524d\u9876\u70b9\u7684\u67d0\u4e2a\u90bb\u63a5\u9876\u70b9\uff0c\u76f4\u5230\u8d70\u5230\u5c3d\u5934\u65f6\u8fd4\u56de\uff0c\u518d\u7ee7\u7eed\u8d70\u5230\u5c3d\u5934\u5e76\u8fd4\u56de\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u6240\u6709\u9876\u70b9\u904d\u5386\u5b8c\u6210\u3002

\u56fe 9-11 \u00a0 \u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u904d\u5386

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

\u8fd9\u79cd\u201c\u8d70\u5230\u5c3d\u5934\u518d\u8fd4\u56de\u201d\u7684\u7b97\u6cd5\u8303\u5f0f\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u6765\u5b9e\u73b0\u3002\u4e0e\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u7c7b\u4f3c\uff0c\u5728\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u4e2d\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

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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# \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]()\ndfs(graph, visited, res, start_vet)\nreturn res\n
graph_dfs.cpp
/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nvoid dfs(GraphAdjList &graph, unordered_set<Vertex *> &visited, vector<Vertex *> &res, Vertex *vet) {\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.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.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();\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nHashSet<Vertex> visited = new();\nDFS(graph, visited, res, startVet);\nreturn res;\n}\n
graph_dfs.go
/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nfunc dfs(g *graphAdjList, visited map[Vertex]struct{}, res *[]Vertex, vet Vertex) {\n// append \u64cd\u4f5c\u4f1a\u8fd4\u56de\u65b0\u7684\u7684\u5f15\u7528\uff0c\u5fc5\u987b\u8ba9\u539f\u5f15\u7528\u91cd\u65b0\u8d4b\u503c\u4e3a\u65b0slice\u7684\u5f15\u7528\n*res = append(*res, vet)\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.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.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.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
graph_dfs.rs
/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nfn dfs(graph: &GraphAdjList, visited: &mut HashSet<Vertex>, res: &mut Vec<Vertex>, vet: Vertex) {\nres.push(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\nif let Some(adj_vets) = graph.adj_list.get(&vet) {\nfor &adj_vet in adj_vets {\nif visited.contains(&adj_vet) {\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, adj_vet);\n}\n}\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfn graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nlet mut res = vec![];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nlet mut visited = HashSet::new();\ndfs(&graph, &mut visited, &mut res, start_vet);\nres\n}\n
graph_dfs.c
/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nint resIndex = 0;\nvoid dfs(graphAdjList *graph, hashTable *visited, Vertex *vet, Vertex **res) {\nif (hashQuery(visited, vet->pos) == 1) {\nreturn; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nhashMark(visited, vet->pos); // \u6807\u8bb0\u9876\u70b9\u5e76\u5c06\u9876\u70b9\u5b58\u5165\u6570\u7ec4\nres[resIndex] = vet;         // \u5c06\u9876\u70b9\u5b58\u5165\u6570\u7ec4\nresIndex++;\n// \u904d\u5386\u8be5\u9876\u70b9\u94fe\u8868\nNode *n = vet->linked->head->next;\nwhile (n != 0) {\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, n->val, res);\nn = n->next;\n}\nreturn;\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\nVertex **graphDFS(graphAdjList *graph, Vertex *startVet) {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nVertex **res = (Vertex **)malloc(sizeof(Vertex *) * graph->size);\nmemset(res, 0, sizeof(Vertex *) * graph->size);\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nhashTable *visited = newHash(graph->size);\ndfs(graph, visited, startVet, res);\n// \u91ca\u653e\u54c8\u5e0c\u8868\u5185\u5b58\u5e76\u5c06\u6570\u7ec4\u7d22\u5f15\u5f52\u96f6\nfreeHash(visited);\nresIndex = 0;\n// \u8fd4\u56de\u904d\u5386\u6570\u7ec4\nreturn res;\n}\n
graph_dfs.zig
[class]{}-[func]{dfs}\n[class]{}-[func]{graphDFS}\n

\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u7b97\u6cd5\u6d41\u7a0b\u5982\u56fe 9-12 \u6240\u793a\u3002

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

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

\u56fe 9-12 \u00a0 \u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u6b65\u9aa4

\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u5e8f\u5217\u662f\u5426\u552f\u4e00\uff1f

\u4e0e\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u7c7b\u4f3c\uff0c\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u5e8f\u5217\u7684\u987a\u5e8f\u4e5f\u4e0d\u662f\u552f\u4e00\u7684\u3002\u7ed9\u5b9a\u67d0\u9876\u70b9\uff0c\u5148\u5f80\u54ea\u4e2a\u65b9\u5411\u63a2\u7d22\u90fd\u53ef\u4ee5\uff0c\u5373\u90bb\u63a5\u9876\u70b9\u7684\u987a\u5e8f\u53ef\u4ee5\u4efb\u610f\u6253\u4e71\uff0c\u90fd\u662f\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u3002

\u4ee5\u6811\u7684\u904d\u5386\u4e3a\u4f8b\uff0c\u201c\u6839 \\(\\rightarrow\\) \u5de6 \\(\\rightarrow\\) \u53f3\u201d\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/#2_1","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"

\u65f6\u95f4\u590d\u6742\u5ea6\uff1a \u6240\u6709\u9876\u70b9\u90fd\u4f1a\u88ab\u8bbf\u95ee \\(1\\) \u6b21\uff0c\u4f7f\u7528 \\(O(|V|)\\) \u65f6\u95f4\uff1b\u6240\u6709\u8fb9\u90fd\u4f1a\u88ab\u8bbf\u95ee \\(2\\) \u6b21\uff0c\u4f7f\u7528 \\(O(2|E|)\\) \u65f6\u95f4\uff1b\u603b\u4f53\u4f7f\u7528 \\(O(|V| + |E|)\\) \u65f6\u95f4\u3002

\u7a7a\u95f4\u590d\u6742\u5ea6\uff1a \u5217\u8868 res \uff0c\u54c8\u5e0c\u8868 visited \u9876\u70b9\u6570\u91cf\u6700\u591a\u4e3a \\(|V|\\) \uff0c\u9012\u5f52\u6df1\u5ea6\u6700\u5927\u4e3a \\(|V|\\) \uff0c\u56e0\u6b64\u4f7f\u7528 \\(O(|V|)\\) \u7a7a\u95f4\u3002

"},{"location":"chapter_graph/summary/","title":"9.4 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_graph/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","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/#2-q-a","title":"2. \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_greedy/","title":"\u7b2c 15 \u7ae0 \u00a0 \u8d2a\u5fc3","text":"

Abstract

\u5411\u65e5\u8475\u671d\u7740\u592a\u9633\u8f6c\u52a8\uff0c\u65f6\u523b\u90fd\u5728\u8ffd\u6c42\u81ea\u8eab\u6210\u957f\u7684\u6700\u5927\u53ef\u80fd\u3002

\u8d2a\u5fc3\u7b56\u7565\u5728\u4e00\u8f6e\u8f6e\u7684\u7b80\u5355\u9009\u62e9\u4e2d\uff0c\u9010\u6b65\u5bfc\u5411\u6700\u4f73\u7684\u7b54\u6848\u3002

"},{"location":"chapter_greedy/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 15.1 \u00a0 \u8d2a\u5fc3\u7b97\u6cd5
  • 15.2 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898
  • 15.3 \u00a0 \u6700\u5927\u5bb9\u91cf\u95ee\u9898
  • 15.4 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u79ef\u95ee\u9898
  • 15.5 \u00a0 \u5c0f\u7ed3
"},{"location":"chapter_greedy/fractional_knapsack_problem/","title":"15.2 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898","text":"

Question

\u7ed9\u5b9a \\(n\\) \u4e2a\u7269\u54c1\uff0c\u7b2c \\(i\\) \u4e2a\u7269\u54c1\u7684\u91cd\u91cf\u4e3a \\(wgt[i-1]\\)\u3001\u4ef7\u503c\u4e3a \\(val[i-1]\\) \uff0c\u548c\u4e00\u4e2a\u5bb9\u91cf\u4e3a \\(cap\\) \u7684\u80cc\u5305\u3002\u6bcf\u4e2a\u7269\u54c1\u53ea\u80fd\u9009\u62e9\u4e00\u6b21\uff0c\u4f46\u53ef\u4ee5\u9009\u62e9\u7269\u54c1\u7684\u4e00\u90e8\u5206\uff0c\u4ef7\u503c\u6839\u636e\u9009\u62e9\u7684\u91cd\u91cf\u6bd4\u4f8b\u8ba1\u7b97\uff0c\u95ee\u5728\u4e0d\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\u4e0b\u80cc\u5305\u4e2d\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u3002

\u56fe 15-3 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e

\u5206\u6570\u80cc\u5305\u548c 0-1 \u80cc\u5305\u6574\u4f53\u4e0a\u975e\u5e38\u76f8\u4f3c\uff0c\u72b6\u6001\u5305\u542b\u5f53\u524d\u7269\u54c1 \\(i\\) \u548c\u5bb9\u91cf \\(c\\) \uff0c\u76ee\u6807\u662f\u6c42\u4e0d\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\u4e0b\u7684\u6700\u5927\u4ef7\u503c\u3002

\u4e0d\u540c\u70b9\u5728\u4e8e\uff0c\u672c\u9898\u5141\u8bb8\u53ea\u9009\u62e9\u7269\u54c1\u7684\u4e00\u90e8\u5206\u3002\u5982\u56fe 15-4 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u7269\u54c1\u4efb\u610f\u5730\u8fdb\u884c\u5207\u5206\uff0c\u5e76\u6309\u7167\u91cd\u91cf\u6bd4\u4f8b\u6765\u8ba1\u7b97\u7269\u54c1\u4ef7\u503c\u3002

  1. \u5bf9\u4e8e\u7269\u54c1 \\(i\\) \uff0c\u5b83\u5728\u5355\u4f4d\u91cd\u91cf\u4e0b\u7684\u4ef7\u503c\u4e3a \\(val[i-1] / wgt[i-1]\\) \uff0c\u7b80\u79f0\u4e3a\u5355\u4f4d\u4ef7\u503c\u3002
  2. \u5047\u8bbe\u653e\u5165\u4e00\u90e8\u5206\u7269\u54c1 \\(i\\) \uff0c\u91cd\u91cf\u4e3a \\(w\\) \uff0c\u5219\u80cc\u5305\u589e\u52a0\u7684\u4ef7\u503c\u4e3a \\(w \\times val[i-1] / wgt[i-1]\\) \u3002

\u56fe 15-4 \u00a0 \u7269\u54c1\u5728\u5355\u4f4d\u91cd\u91cf\u4e0b\u7684\u4ef7\u503c

"},{"location":"chapter_greedy/fractional_knapsack_problem/#1","title":"1. \u00a0 \u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a","text":"

\u6700\u5927\u5316\u80cc\u5305\u5185\u7269\u54c1\u603b\u4ef7\u503c\uff0c\u672c\u8d28\u4e0a\u662f\u8981\u6700\u5927\u5316\u5355\u4f4d\u91cd\u91cf\u4e0b\u7684\u7269\u54c1\u4ef7\u503c\u3002\u7531\u6b64\u4fbf\u53ef\u63a8\u51fa\u56fe 15-5 \u6240\u793a\u7684\u8d2a\u5fc3\u7b56\u7565\u3002

  1. \u5c06\u7269\u54c1\u6309\u7167\u5355\u4f4d\u4ef7\u503c\u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\u3002
  2. \u904d\u5386\u6240\u6709\u7269\u54c1\uff0c\u6bcf\u8f6e\u8d2a\u5fc3\u5730\u9009\u62e9\u5355\u4f4d\u4ef7\u503c\u6700\u9ad8\u7684\u7269\u54c1\u3002
  3. \u82e5\u5269\u4f59\u80cc\u5305\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u4f7f\u7528\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u586b\u6ee1\u80cc\u5305\u5373\u53ef\u3002

\u56fe 15-5 \u00a0 \u5206\u6570\u80cc\u5305\u7684\u8d2a\u5fc3\u7b56\u7565

"},{"location":"chapter_greedy/fractional_knapsack_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u6211\u4eec\u5efa\u7acb\u4e86\u4e00\u4e2a\u7269\u54c1\u7c7b Item \uff0c\u4ee5\u4fbf\u5c06\u7269\u54c1\u6309\u7167\u5355\u4f4d\u4ef7\u503c\u8fdb\u884c\u6392\u5e8f\u3002\u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u5f53\u80cc\u5305\u5df2\u6ee1\u65f6\u8df3\u51fa\u5e76\u8fd4\u56de\u89e3\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig fractional_knapsack.py
class Item:\n\"\"\"\u7269\u54c1\"\"\"\ndef __init__(self, w: int, v: int):\nself.w = w  # \u7269\u54c1\u91cd\u91cf\nself.v = v  # \u7269\u54c1\u4ef7\u503c\ndef fractional_knapsack(wgt: list[int], val: list[int], cap: int) -> int:\n\"\"\"\u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3\"\"\"\n# \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nitems = [Item(w, v) for w, v in zip(wgt, val)]\n# \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nitems.sort(key=lambda item: item.v / item.w, reverse=True)\n# \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\nres = 0\nfor item in items:\nif item.w <= cap:\n# \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += item.v\ncap -= item.w\nelse:\n# \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += (item.v / item.w) * cap\n# \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak\nreturn res\n
fractional_knapsack.cpp
/* \u7269\u54c1 */\nclass Item {\npublic:\nint w; // \u7269\u54c1\u91cd\u91cf\nint v; // \u7269\u54c1\u4ef7\u503c\nItem(int w, int v) : w(w), v(v) {\n}\n};\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble fractionalKnapsack(vector<int> &wgt, vector<int> &val, int cap) {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nvector<Item> items;\nfor (int i = 0; i < wgt.size(); i++) {\nitems.push_back(Item(wgt[i], val[i]));\n}\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nsort(items.begin(), items.end(), [](Item &a, Item &b) { return (double)a.v / a.w > (double)b.v / b.w; });\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\ndouble res = 0;\nfor (auto &item : items) {\nif (item.w <= cap) {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += item.v;\ncap -= item.w;\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += (double)item.v / item.w * cap;\n// \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak;\n}\n}\nreturn res;\n}\n
fractional_knapsack.java
/* \u7269\u54c1 */\nclass Item {\nint w; // \u7269\u54c1\u91cd\u91cf\nint v; // \u7269\u54c1\u4ef7\u503c\npublic Item(int w, int v) {\nthis.w = w;\nthis.v = v;\n}\n}\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble fractionalKnapsack(int[] wgt, int[] val, int cap) {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nItem[] items = new Item[wgt.length];\nfor (int i = 0; i < wgt.length; i++) {\nitems[i] = new Item(wgt[i], val[i]);\n}\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nArrays.sort(items, Comparator.comparingDouble(item -> -((double) item.v / item.w)));\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\ndouble res = 0;\nfor (Item item : items) {\nif (item.w <= cap) {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += item.v;\ncap -= item.w;\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += (double) item.v / item.w * cap;\n// \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak;\n}\n}\nreturn res;\n}\n
fractional_knapsack.cs
/* \u7269\u54c1 */\nclass Item {\npublic int w; // \u7269\u54c1\u91cd\u91cf\npublic int v; // \u7269\u54c1\u4ef7\u503c\npublic Item(int w, int v) {\nthis.w = w;\nthis.v = v;\n}\n}\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble FractionalKnapsack(int[] wgt, int[] val, int cap) {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nItem[] items = new Item[wgt.Length];\nfor (int i = 0; i < wgt.Length; i++) {\nitems[i] = new Item(wgt[i], val[i]);\n}\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nArray.Sort(items, (x, y) => (y.v / y.w).CompareTo(x.v / x.w));\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\ndouble res = 0;\nforeach (Item item in items) {\nif (item.w <= cap) {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += item.v;\ncap -= item.w;\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += (double)item.v / item.w * cap;\n// \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak;\n}\n}\nreturn res;\n}\n
fractional_knapsack.go
/* \u7269\u54c1 */\ntype Item struct {\nw int // \u7269\u54c1\u91cd\u91cf\nv int // \u7269\u54c1\u4ef7\u503c\n}\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfunc fractionalKnapsack(wgt []int, val []int, cap int) float64 {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nitems := make([]Item, len(wgt))\nfor i := 0; i < len(wgt); i++ {\nitems[i] = Item{wgt[i], val[i]}\n}\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nsort.Slice(items, func(i, j int) bool {\nreturn float64(items[i].v)/float64(items[i].w) > float64(items[j].v)/float64(items[j].w)\n})\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\nres := 0.0\nfor _, item := range items {\nif item.w <= cap {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += float64(item.v)\ncap -= item.w\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += float64(item.v) / float64(item.w) * float64(cap)\n// \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak\n}\n}\nreturn res\n}\n
fractional_knapsack.swift
/* \u7269\u54c1 */\nclass Item {\nvar w: Int // \u7269\u54c1\u91cd\u91cf\nvar v: Int // \u7269\u54c1\u4ef7\u503c\ninit(w: Int, v: Int) {\nself.w = w\nself.v = v\n}\n}\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfunc fractionalKnapsack(wgt: [Int], val: [Int], cap: Int) -> Double {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nvar items = zip(wgt, val).map { Item(w: $0, v: $1) }\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nitems.sort(by: { -(Double($0.v) / Double($0.w)) < -(Double($1.v) / Double($1.w)) })\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\nvar res = 0.0\nvar cap = cap\nfor item in items {\nif item.w <= cap {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += Double(item.v)\ncap -= item.w\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += Double(item.v) / Double(item.w) * Double(cap)\n// \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak\n}\n}\nreturn res\n}\n
fractional_knapsack.js
/* \u7269\u54c1 */\nclass Item {\nconstructor(w, v) {\nthis.w = w; // \u7269\u54c1\u91cd\u91cf\nthis.v = v; // \u7269\u54c1\u4ef7\u503c\n}\n}\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfunction fractionalKnapsack(wgt, val, cap) {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nconst items = wgt.map((w, i) => new Item(w, val[i]));\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nitems.sort((a, b) => b.v / b.w - a.v / a.w);\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\nlet res = 0;\nfor (const item of items) {\nif (item.w <= cap) {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += item.v;\ncap -= item.w;\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += (item.v / item.w) * cap;\n// \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak;\n}\n}\nreturn res;\n}\n
fractional_knapsack.ts
/* \u7269\u54c1 */\nclass Item {\nw: number; // \u7269\u54c1\u91cd\u91cf\nv: number; // \u7269\u54c1\u4ef7\u503c\nconstructor(w: number, v: number) {\nthis.w = w;\nthis.v = v;\n}\n}\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfunction fractionalKnapsack(wgt: number[], val: number[], cap: number): number {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nconst items: Item[] = wgt.map((w, i) => new Item(w, val[i]));\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nitems.sort((a, b) => b.v / b.w - a.v / a.w);\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\nlet res = 0;\nfor (const item of items) {\nif (item.w <= cap) {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += item.v;\ncap -= item.w;\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += (item.v / item.w) * cap;\n// \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak;\n}\n}\nreturn res;\n}\n
fractional_knapsack.dart
/* \u7269\u54c1 */\nclass Item {\nint w; // \u7269\u54c1\u91cd\u91cf\nint v; // \u7269\u54c1\u4ef7\u503c\nItem(this.w, this.v);\n}\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble fractionalKnapsack(List<int> wgt, List<int> val, int cap) {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nList<Item> items = List.generate(wgt.length, (i) => Item(wgt[i], val[i]));\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nitems.sort((a, b) => (b.v / b.w).compareTo(a.v / a.w));\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\ndouble res = 0;\nfor (Item item in items) {\nif (item.w <= cap) {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += item.v;\ncap -= item.w;\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += item.v / item.w * cap;\n// \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak;\n}\n}\nreturn res;\n}\n
fractional_knapsack.rs
/* \u7269\u54c1 */\nstruct Item {\nw: i32, // \u7269\u54c1\u91cd\u91cf\nv: i32, // \u7269\u54c1\u4ef7\u503c\n}\nimpl Item {\nfn new(w: i32, v: i32) -> Self {\nSelf { w, v }\n}\n}\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfn fractional_knapsack(wgt: &[i32], val: &[i32], mut cap: i32) -> f64 {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nlet mut items = wgt\n.iter()\n.zip(val.iter())\n.map(|(&w, &v)| Item::new(w, v))\n.collect::<Vec<Item>>();\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nitems.sort_by(|a, b| {\n(b.v as f64 / b.w as f64)\n.partial_cmp(&(a.v as f64 / a.w as f64))\n.unwrap()\n});\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\nlet mut res = 0.0;\nfor item in &items {\nif item.w <= cap {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += item.v as f64;\ncap -= item.w;\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += item.v as f64 / item.w as f64 * cap as f64;\n// \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak;\n}\n}\nres\n}\n
fractional_knapsack.c
/* \u7269\u54c1 */\nstruct Item {\nint w; // \u7269\u54c1\u91cd\u91cf\nint v; // \u7269\u54c1\u4ef7\u503c\n};\ntypedef struct Item Item;\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfloat fractionalKnapsack(int wgt[], int val[], int itemCount, int cap) {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nItem *items = malloc(sizeof(Item) * itemCount);\nfor (int i = 0; i < itemCount; i++) {\nitems[i] = (Item){.w = wgt[i], .v = val[i]};\n}\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nqsort(items, (size_t)itemCount, sizeof(Item), sortByValueDensity);\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\nfloat res = 0.0;\nfor (int i = 0; i < itemCount; i++) {\nif (items[i].w <= cap) {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += items[i].v;\ncap -= items[i].w;\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += (float)cap / items[i].w * items[i].v;\ncap = 0;\nbreak;\n}\n}\nfree(items);\nreturn res;\n}\n
fractional_knapsack.zig
[class]{Item}-[func]{}\n[class]{}-[func]{fractionalKnapsack}\n

\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u904d\u5386\u6574\u4e2a\u7269\u54c1\u5217\u8868\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u7269\u54c1\u6570\u91cf\u3002

\u7531\u4e8e\u521d\u59cb\u5316\u4e86\u4e00\u4e2a Item \u5bf9\u8c61\u5217\u8868\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

"},{"location":"chapter_greedy/fractional_knapsack_problem/#3","title":"3. \u00a0 \u6b63\u786e\u6027\u8bc1\u660e","text":"

\u91c7\u7528\u53cd\u8bc1\u6cd5\u3002\u5047\u8bbe\u7269\u54c1 \\(x\\) \u662f\u5355\u4f4d\u4ef7\u503c\u6700\u9ad8\u7684\u7269\u54c1\uff0c\u4f7f\u7528\u67d0\u7b97\u6cd5\u6c42\u5f97\u6700\u5927\u4ef7\u503c\u4e3a res \uff0c\u4f46\u8be5\u89e3\u4e2d\u4e0d\u5305\u542b\u7269\u54c1 \\(x\\) \u3002

\u73b0\u5728\u4ece\u80cc\u5305\u4e2d\u62ff\u51fa\u5355\u4f4d\u91cd\u91cf\u7684\u4efb\u610f\u7269\u54c1\uff0c\u5e76\u66ff\u6362\u4e3a\u5355\u4f4d\u91cd\u91cf\u7684\u7269\u54c1 \\(x\\) \u3002\u7531\u4e8e\u7269\u54c1 \\(x\\) \u7684\u5355\u4f4d\u4ef7\u503c\u6700\u9ad8\uff0c\u56e0\u6b64\u66ff\u6362\u540e\u7684\u603b\u4ef7\u503c\u4e00\u5b9a\u5927\u4e8e res \u3002\u8fd9\u4e0e res \u662f\u6700\u4f18\u89e3\u77db\u76fe\uff0c\u8bf4\u660e\u6700\u4f18\u89e3\u4e2d\u5fc5\u987b\u5305\u542b\u7269\u54c1 \\(x\\) \u3002

\u5bf9\u4e8e\u8be5\u89e3\u4e2d\u7684\u5176\u4ed6\u7269\u54c1\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u6784\u5efa\u51fa\u4e0a\u8ff0\u77db\u76fe\u3002\u603b\u800c\u8a00\u4e4b\uff0c\u5355\u4f4d\u4ef7\u503c\u66f4\u5927\u7684\u7269\u54c1\u603b\u662f\u66f4\u4f18\u9009\u62e9\uff0c\u8fd9\u8bf4\u660e\u8d2a\u5fc3\u7b56\u7565\u662f\u6709\u6548\u7684\u3002

\u5982\u56fe 15-6 \u6240\u793a\uff0c\u5982\u679c\u5c06\u7269\u54c1\u91cd\u91cf\u548c\u7269\u54c1\u5355\u4f4d\u4ef7\u503c\u5206\u522b\u770b\u4f5c\u4e00\u4e2a 2D \u56fe\u8868\u7684\u6a2a\u8f74\u548c\u7eb5\u8f74\uff0c\u5219\u5206\u6570\u80cc\u5305\u95ee\u9898\u53ef\u88ab\u8f6c\u5316\u4e3a\u201c\u6c42\u5728\u6709\u9650\u6a2a\u8f74\u533a\u95f4\u4e0b\u7684\u6700\u5927\u56f4\u6210\u9762\u79ef\u201d\u3002\u8fd9\u4e2a\u7c7b\u6bd4\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4ece\u51e0\u4f55\u89d2\u5ea6\u7406\u89e3\u8d2a\u5fc3\u7b56\u7565\u7684\u6709\u6548\u6027\u3002

\u56fe 15-6 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898\u7684\u51e0\u4f55\u8868\u793a

"},{"location":"chapter_greedy/greedy_algorithm/","title":"15.1 \u00a0 \u8d2a\u5fc3\u7b97\u6cd5","text":"

\u300c\u8d2a\u5fc3\u7b97\u6cd5 greedy algorithm\u300d\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u89e3\u51b3\u4f18\u5316\u95ee\u9898\u7684\u7b97\u6cd5\uff0c\u5176\u57fa\u672c\u601d\u60f3\u662f\u5728\u95ee\u9898\u7684\u6bcf\u4e2a\u51b3\u7b56\u9636\u6bb5\uff0c\u90fd\u9009\u62e9\u5f53\u524d\u770b\u8d77\u6765\u6700\u4f18\u7684\u9009\u62e9\uff0c\u5373\u8d2a\u5fc3\u5730\u505a\u51fa\u5c40\u90e8\u6700\u4f18\u7684\u51b3\u7b56\uff0c\u4ee5\u671f\u671b\u83b7\u5f97\u5168\u5c40\u6700\u4f18\u89e3\u3002\u8d2a\u5fc3\u7b97\u6cd5\u7b80\u6d01\u4e14\u9ad8\u6548\uff0c\u5728\u8bb8\u591a\u5b9e\u9645\u95ee\u9898\u4e2d\u90fd\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002

\u8d2a\u5fc3\u7b97\u6cd5\u548c\u52a8\u6001\u89c4\u5212\u90fd\u5e38\u7528\u4e8e\u89e3\u51b3\u4f18\u5316\u95ee\u9898\u3002\u5b83\u4eec\u4e4b\u95f4\u5b58\u5728\u4e00\u4e9b\u76f8\u4f3c\u4e4b\u5904\uff0c\u6bd4\u5982\u90fd\u4f9d\u8d56\u6700\u4f18\u5b50\u7ed3\u6784\u6027\u8d28\uff0c\u4f46\u5de5\u4f5c\u539f\u7406\u662f\u4e0d\u540c\u7684\u3002

  • \u52a8\u6001\u89c4\u5212\u4f1a\u6839\u636e\u4e4b\u524d\u9636\u6bb5\u7684\u6240\u6709\u51b3\u7b56\u6765\u8003\u8651\u5f53\u524d\u51b3\u7b56\uff0c\u5e76\u4f7f\u7528\u8fc7\u53bb\u5b50\u95ee\u9898\u7684\u89e3\u6765\u6784\u5efa\u5f53\u524d\u5b50\u95ee\u9898\u7684\u89e3\u3002
  • \u8d2a\u5fc3\u7b97\u6cd5\u4e0d\u4f1a\u91cd\u65b0\u8003\u8651\u8fc7\u53bb\u7684\u51b3\u7b56\uff0c\u800c\u662f\u4e00\u8def\u5411\u524d\u5730\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u4e0d\u65ad\u7f29\u5c0f\u95ee\u9898\u8303\u56f4\uff0c\u76f4\u81f3\u95ee\u9898\u88ab\u89e3\u51b3\u3002

\u6211\u4eec\u5148\u901a\u8fc7\u4f8b\u9898\u201c\u96f6\u94b1\u5151\u6362\u201d\u4e86\u89e3\u8d2a\u5fc3\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u3002\u8fd9\u9053\u9898\u5df2\u7ecf\u5728\u52a8\u6001\u89c4\u5212\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u8fc7\uff0c\u76f8\u4fe1\u4f60\u5bf9\u5b83\u5e76\u4e0d\u964c\u751f\u3002

Question

\u7ed9\u5b9a \\(n\\) \u79cd\u786c\u5e01\uff0c\u7b2c \\(i\\) \u79cd\u786c\u5e01\u7684\u9762\u503c\u4e3a \\(coins[i - 1]\\) \uff0c\u76ee\u6807\u91d1\u989d\u4e3a \\(amt\\) \uff0c\u6bcf\u79cd\u786c\u5e01\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u80fd\u591f\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u7684\u6700\u5c11\u786c\u5e01\u4e2a\u6570\u3002\u5982\u679c\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u5219\u8fd4\u56de \\(-1\\) \u3002

\u672c\u9898\u7684\u8d2a\u5fc3\u7b56\u7565\u5982\u56fe 15-1 \u6240\u793a\u3002\u7ed9\u5b9a\u76ee\u6807\u91d1\u989d\uff0c\u6211\u4eec\u8d2a\u5fc3\u5730\u9009\u62e9\u4e0d\u5927\u4e8e\u4e14\u6700\u63a5\u8fd1\u5b83\u7684\u786c\u5e01\uff0c\u4e0d\u65ad\u5faa\u73af\u8be5\u6b65\u9aa4\uff0c\u76f4\u81f3\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u4e3a\u6b62\u3002

\u56fe 15-1 \u00a0 \u96f6\u94b1\u5151\u6362\u7684\u8d2a\u5fc3\u7b56\u7565

\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002\u4f60\u53ef\u80fd\u4f1a\u4e0d\u7531\u5730\u53d1\u51fa\u611f\u53f9\uff1aSo Clean \uff01\u8d2a\u5fc3\u7b97\u6cd5\u4ec5\u7528\u5341\u884c\u4ee3\u7801\u5c31\u89e3\u51b3\u4e86\u96f6\u94b1\u5151\u6362\u95ee\u9898\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig coin_change_greedy.py
def coin_change_greedy(coins: list[int], amt: int) -> int:\n\"\"\"\u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3\"\"\"\n# \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\ni = len(coins) - 1\ncount = 0\n# \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile amt > 0:\n# \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile i > 0 and coins[i] > amt:\ni -= 1\n# \u9009\u62e9 coins[i]\namt -= coins[i]\ncount += 1\n# \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nreturn count if amt == 0 else -1\n
coin_change_greedy.cpp
/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(vector<int> &coins, int amt) {\n// \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\nint i = coins.size() - 1;\nint count = 0;\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile (amt > 0) {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile (i > 0 && coins[i] > amt) {\ni--;\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i];\ncount++;\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nreturn amt == 0 ? count : -1;\n}\n
coin_change_greedy.java
/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(int[] coins, int amt) {\n// \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\nint i = coins.length - 1;\nint count = 0;\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile (amt > 0) {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile (i > 0 && coins[i] > amt) {\ni--;\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i];\ncount++;\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nreturn amt == 0 ? count : -1;\n}\n
coin_change_greedy.cs
/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint CoinChangeGreedy(int[] coins, int amt) {\n// \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\nint i = coins.Length - 1;\nint count = 0;\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile (amt > 0) {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile (i > 0 && coins[i] > amt) {\ni--;\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i];\ncount++;\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nreturn amt == 0 ? count : -1;\n}\n
coin_change_greedy.go
/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfunc coinChangeGreedy(coins []int, amt int) int {\n// \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\ni := len(coins) - 1\ncount := 0\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nfor amt > 0 {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nfor i > 0 && coins[i] > amt {\ni--\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i]\ncount++\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nif amt != 0 {\nreturn -1\n}\nreturn count\n}\n
coin_change_greedy.swift
/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfunc coinChangeGreedy(coins: [Int], amt: Int) -> Int {\n// \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\nvar i = coins.count - 1\nvar count = 0\nvar amt = amt\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile amt > 0 {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile i > 0 && coins[i] > amt {\ni -= 1\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i]\ncount += 1\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nreturn amt == 0 ? count : -1\n}\n
coin_change_greedy.js
/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfunction coinChangeGreedy(coins, amt) {\n// \u5047\u8bbe coins \u6570\u7ec4\u6709\u5e8f\nlet i = coins.length - 1;\nlet count = 0;\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile (amt > 0) {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile (i > 0 && coins[i] > amt) {\ni--;\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i];\ncount++;\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nreturn amt === 0 ? count : -1;\n}\n
coin_change_greedy.ts
/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfunction coinChangeGreedy(coins: number[], amt: number): number {\n// \u5047\u8bbe coins \u6570\u7ec4\u6709\u5e8f\nlet i = coins.length - 1;\nlet count = 0;\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile (amt > 0) {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile (i > 0 && coins[i] > amt) {\ni--;\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i];\ncount++;\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nreturn amt === 0 ? count : -1;\n}\n
coin_change_greedy.dart
/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(List<int> coins, int amt) {\n// \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\nint i = coins.length - 1;\nint count = 0;\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile (amt > 0) {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile (i > 0 && coins[i] > amt) {\ni--;\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i];\ncount++;\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nreturn amt == 0 ? count : -1;\n}\n
coin_change_greedy.rs
/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfn coin_change_greedy(coins: &[i32], mut amt: i32) -> i32 {\n// \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\nlet mut i = coins.len() - 1;\nlet mut count = 0;\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile amt > 0 {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile i > 0 && coins[i] > amt {\ni -= 1;\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i];\ncount += 1;\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nif amt == 0 {\ncount\n} else {\n-1\n}\n}\n
coin_change_greedy.c
/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(int* coins, int size, int amt) {\n// \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\nint i = size - 1;\nint count = 0;\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile (amt > 0) {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile (i > 0 && coins[i] > amt) {\ni--;\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i];\ncount++;\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nreturn amt == 0 ? count : -1;\n}\n
coin_change_greedy.zig
[class]{}-[func]{coinChangeGreedy}\n
"},{"location":"chapter_greedy/greedy_algorithm/#1511","title":"15.1.1 \u00a0 \u8d2a\u5fc3\u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"

\u8d2a\u5fc3\u7b97\u6cd5\u4e0d\u4ec5\u64cd\u4f5c\u76f4\u63a5\u3001\u5b9e\u73b0\u7b80\u5355\uff0c\u800c\u4e14\u901a\u5e38\u6548\u7387\u4e5f\u5f88\u9ad8\u3002\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u8bb0\u786c\u5e01\u6700\u5c0f\u9762\u503c\u4e3a \\(\\min(coins)\\) \uff0c\u5219\u8d2a\u5fc3\u9009\u62e9\u6700\u591a\u5faa\u73af \\(amt / \\min(coins)\\) \u6b21\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(amt / \\min(coins))\\) \u3002\u8fd9\u6bd4\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n \\times amt)\\) \u63d0\u5347\u4e86\u4e00\u4e2a\u6570\u91cf\u7ea7\u3002

\u7136\u800c\uff0c\u5bf9\u4e8e\u67d0\u4e9b\u786c\u5e01\u9762\u503c\u7ec4\u5408\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u5e76\u4e0d\u80fd\u627e\u5230\u6700\u4f18\u89e3\u3002\u56fe 15-2 \u7ed9\u51fa\u4e86\u4e24\u4e2a\u793a\u4f8b\u3002

  • \u6b63\u4f8b \\(coins = [1, 5, 10, 20, 50, 100]\\)\uff1a\u5728\u8be5\u786c\u5e01\u7ec4\u5408\u4e0b\uff0c\u7ed9\u5b9a\u4efb\u610f \\(amt\\) \uff0c\u8d2a\u5fc3\u7b97\u6cd5\u90fd\u53ef\u4ee5\u627e\u51fa\u6700\u4f18\u89e3\u3002
  • \u53cd\u4f8b \\(coins = [1, 20, 50]\\)\uff1a\u5047\u8bbe \\(amt = 60\\) \uff0c\u8d2a\u5fc3\u7b97\u6cd5\u53ea\u80fd\u627e\u5230 \\(50 + 1 \\times 10\\) \u7684\u5151\u6362\u7ec4\u5408\uff0c\u5171\u8ba1 \\(11\\) \u679a\u786c\u5e01\uff0c\u4f46\u52a8\u6001\u89c4\u5212\u53ef\u4ee5\u627e\u5230\u6700\u4f18\u89e3 \\(20 + 20 + 20\\) \uff0c\u4ec5\u9700 \\(3\\) \u679a\u786c\u5e01\u3002
  • \u53cd\u4f8b \\(coins = [1, 49, 50]\\)\uff1a\u5047\u8bbe \\(amt = 98\\) \uff0c\u8d2a\u5fc3\u7b97\u6cd5\u53ea\u80fd\u627e\u5230 \\(50 + 1 \\times 48\\) \u7684\u5151\u6362\u7ec4\u5408\uff0c\u5171\u8ba1 \\(49\\) \u679a\u786c\u5e01\uff0c\u4f46\u52a8\u6001\u89c4\u5212\u53ef\u4ee5\u627e\u5230\u6700\u4f18\u89e3 \\(49 + 49\\) \uff0c\u4ec5\u9700 \\(2\\) \u679a\u786c\u5e01\u3002

\u56fe 15-2 \u00a0 \u8d2a\u5fc3\u65e0\u6cd5\u627e\u51fa\u6700\u4f18\u89e3\u7684\u793a\u4f8b

\u4e5f\u5c31\u662f\u8bf4\uff0c\u5bf9\u4e8e\u96f6\u94b1\u5151\u6362\u95ee\u9898\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u65e0\u6cd5\u4fdd\u8bc1\u627e\u5230\u5168\u5c40\u6700\u4f18\u89e3\uff0c\u5e76\u4e14\u6709\u53ef\u80fd\u627e\u5230\u975e\u5e38\u5dee\u7684\u89e3\u3002\u5b83\u66f4\u9002\u5408\u7528\u52a8\u6001\u89c4\u5212\u89e3\u51b3\u3002

\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u9002\u7528\u4e8e\u4ee5\u4e0b\u4e24\u7c7b\u95ee\u9898\u3002

  1. \u53ef\u4ee5\u4fdd\u8bc1\u627e\u5230\u6700\u4f18\u89e3\uff1a\u8d2a\u5fc3\u7b97\u6cd5\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5f80\u5f80\u662f\u6700\u4f18\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u5f80\u5f80\u6bd4\u56de\u6eaf\u3001\u52a8\u6001\u89c4\u5212\u66f4\u9ad8\u6548\u3002
  2. \u53ef\u4ee5\u627e\u5230\u8fd1\u4f3c\u6700\u4f18\u89e3\uff1a\u8d2a\u5fc3\u7b97\u6cd5\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4e5f\u662f\u53ef\u7528\u7684\u3002\u5bf9\u4e8e\u5f88\u591a\u590d\u6742\u95ee\u9898\u6765\u8bf4\uff0c\u5bfb\u627e\u5168\u5c40\u6700\u4f18\u89e3\u662f\u975e\u5e38\u56f0\u96be\u7684\uff0c\u80fd\u4ee5\u8f83\u9ad8\u6548\u7387\u627e\u5230\u6b21\u4f18\u89e3\u4e5f\u662f\u975e\u5e38\u4e0d\u9519\u7684\u3002
"},{"location":"chapter_greedy/greedy_algorithm/#1512","title":"15.1.2 \u00a0 \u8d2a\u5fc3\u7b97\u6cd5\u7279\u6027","text":"

\u90a3\u4e48\u95ee\u9898\u6765\u4e86\uff0c\u4ec0\u4e48\u6837\u7684\u95ee\u9898\u9002\u5408\u7528\u8d2a\u5fc3\u7b97\u6cd5\u6c42\u89e3\u5462\uff1f\u6216\u8005\u8bf4\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4fdd\u8bc1\u627e\u5230\u6700\u4f18\u89e3\uff1f

\u76f8\u8f83\u4e8e\u52a8\u6001\u89c4\u5212\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u7684\u4f7f\u7528\u6761\u4ef6\u66f4\u52a0\u82db\u523b\uff0c\u5176\u4e3b\u8981\u5173\u6ce8\u95ee\u9898\u7684\u4e24\u4e2a\u6027\u8d28\u3002

  • \u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\uff1a\u53ea\u6709\u5f53\u5c40\u90e8\u6700\u4f18\u9009\u62e9\u59cb\u7ec8\u53ef\u4ee5\u5bfc\u81f4\u5168\u5c40\u6700\u4f18\u89e3\u65f6\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u624d\u80fd\u4fdd\u8bc1\u5f97\u5230\u6700\u4f18\u89e3\u3002
  • \u6700\u4f18\u5b50\u7ed3\u6784\uff1a\u539f\u95ee\u9898\u7684\u6700\u4f18\u89e3\u5305\u542b\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u3002

\u6700\u4f18\u5b50\u7ed3\u6784\u5df2\u7ecf\u5728\u52a8\u6001\u89c4\u5212\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u8fc7\uff0c\u4e0d\u518d\u8d58\u8ff0\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u4e00\u4e9b\u95ee\u9898\u7684\u6700\u4f18\u5b50\u7ed3\u6784\u5e76\u4e0d\u660e\u663e\uff0c\u4f46\u4ecd\u7136\u53ef\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u89e3\u51b3\u3002

\u6211\u4eec\u4e3b\u8981\u63a2\u7a76\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u7684\u5224\u65ad\u65b9\u6cd5\u3002\u867d\u7136\u5b83\u7684\u63cf\u8ff0\u770b\u4e0a\u53bb\u6bd4\u8f83\u7b80\u5355\uff0c\u4f46\u5b9e\u9645\u4e0a\u5bf9\u4e8e\u8bb8\u591a\u95ee\u9898\uff0c\u8bc1\u660e\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u4e0d\u662f\u4e00\u4ef6\u6613\u4e8b\u3002

\u4f8b\u5982\u96f6\u94b1\u5151\u6362\u95ee\u9898\uff0c\u6211\u4eec\u867d\u7136\u80fd\u591f\u5bb9\u6613\u5730\u4e3e\u51fa\u53cd\u4f8b\uff0c\u5bf9\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u8fdb\u884c\u8bc1\u4f2a\uff0c\u4f46\u8bc1\u5b9e\u7684\u96be\u5ea6\u8f83\u5927\u3002\u5982\u679c\u95ee\uff1a\u6ee1\u8db3\u4ec0\u4e48\u6761\u4ef6\u7684\u786c\u5e01\u7ec4\u5408\u53ef\u4ee5\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u6c42\u89e3\uff1f\u6211\u4eec\u5f80\u5f80\u53ea\u80fd\u51ed\u501f\u76f4\u89c9\u6216\u4e3e\u4f8b\u5b50\u6765\u7ed9\u51fa\u4e00\u4e2a\u6a21\u68f1\u4e24\u53ef\u7684\u7b54\u6848\uff0c\u800c\u96be\u4ee5\u7ed9\u51fa\u4e25\u8c28\u7684\u6570\u5b66\u8bc1\u660e\u3002

Quote

\u6709\u4e00\u7bc7\u8bba\u6587\u7ed9\u51fa\u4e86\u4e00\u4e2a \\(O(n^3)\\) \u65f6\u95f4\u590d\u6742\u5ea6\u7684\u7b97\u6cd5\uff0c\u7528\u4e8e\u5224\u65ad\u4e00\u4e2a\u786c\u5e01\u7ec4\u5408\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u627e\u51fa\u4efb\u4f55\u91d1\u989d\u7684\u6700\u4f18\u89e3\u3002

Pearson, David. A polynomial-time algorithm for the change-making problem. Operations Research Letters 33.3 (2005): 231-234.

"},{"location":"chapter_greedy/greedy_algorithm/#1513","title":"15.1.3 \u00a0 \u8d2a\u5fc3\u89e3\u9898\u6b65\u9aa4","text":"

\u8d2a\u5fc3\u95ee\u9898\u7684\u89e3\u51b3\u6d41\u7a0b\u5927\u4f53\u53ef\u5206\u4e3a\u4ee5\u4e0b\u4e09\u6b65\u3002

  1. \u95ee\u9898\u5206\u6790\uff1a\u68b3\u7406\u4e0e\u7406\u89e3\u95ee\u9898\u7279\u6027\uff0c\u5305\u62ec\u72b6\u6001\u5b9a\u4e49\u3001\u4f18\u5316\u76ee\u6807\u548c\u7ea6\u675f\u6761\u4ef6\u7b49\u3002\u8fd9\u4e00\u6b65\u5728\u56de\u6eaf\u548c\u52a8\u6001\u89c4\u5212\u4e2d\u90fd\u6709\u6d89\u53ca\u3002
  2. \u786e\u5b9a\u8d2a\u5fc3\u7b56\u7565\uff1a\u786e\u5b9a\u5982\u4f55\u5728\u6bcf\u4e00\u6b65\u4e2d\u505a\u51fa\u8d2a\u5fc3\u9009\u62e9\u3002\u8fd9\u4e2a\u7b56\u7565\u80fd\u591f\u5728\u6bcf\u4e00\u6b65\u51cf\u5c0f\u95ee\u9898\u7684\u89c4\u6a21\uff0c\u5e76\u6700\u7ec8\u80fd\u89e3\u51b3\u6574\u4e2a\u95ee\u9898\u3002
  3. \u6b63\u786e\u6027\u8bc1\u660e\uff1a\u901a\u5e38\u9700\u8981\u8bc1\u660e\u95ee\u9898\u5177\u6709\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u548c\u6700\u4f18\u5b50\u7ed3\u6784\u3002\u8fd9\u4e2a\u6b65\u9aa4\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u5230\u6570\u5b66\u8bc1\u660e\uff0c\u4f8b\u5982\u5f52\u7eb3\u6cd5\u6216\u53cd\u8bc1\u6cd5\u7b49\u3002

\u786e\u5b9a\u8d2a\u5fc3\u7b56\u7565\u662f\u6c42\u89e3\u95ee\u9898\u7684\u6838\u5fc3\u6b65\u9aa4\uff0c\u4f46\u5b9e\u65bd\u8d77\u6765\u53ef\u80fd\u5e76\u4e0d\u5bb9\u6613\uff0c\u4e3b\u8981\u5305\u542b\u4ee5\u4e0b\u539f\u56e0\u3002

  • \u4e0d\u540c\u95ee\u9898\u7684\u8d2a\u5fc3\u7b56\u7565\u7684\u5dee\u5f02\u8f83\u5927\u3002\u5bf9\u4e8e\u8bb8\u591a\u95ee\u9898\u6765\u8bf4\uff0c\u8d2a\u5fc3\u7b56\u7565\u90fd\u6bd4\u8f83\u6d45\u663e\uff0c\u6211\u4eec\u901a\u8fc7\u4e00\u4e9b\u5927\u6982\u7684\u601d\u8003\u4e0e\u5c1d\u8bd5\u5c31\u80fd\u5f97\u51fa\u3002\u800c\u5bf9\u4e8e\u4e00\u4e9b\u590d\u6742\u95ee\u9898\uff0c\u8d2a\u5fc3\u7b56\u7565\u53ef\u80fd\u975e\u5e38\u9690\u853d\uff0c\u8fd9\u79cd\u60c5\u51b5\u5c31\u975e\u5e38\u8003\u9a8c\u4e2a\u4eba\u7684\u89e3\u9898\u7ecf\u9a8c\u4e0e\u7b97\u6cd5\u80fd\u529b\u4e86\u3002
  • \u67d0\u4e9b\u8d2a\u5fc3\u7b56\u7565\u5177\u6709\u8f83\u5f3a\u7684\u8ff7\u60d1\u6027\u3002\u5f53\u6211\u4eec\u6ee1\u6000\u4fe1\u5fc3\u8bbe\u8ba1\u597d\u8d2a\u5fc3\u7b56\u7565\uff0c\u5199\u51fa\u89e3\u9898\u4ee3\u7801\u5e76\u63d0\u4ea4\u8fd0\u884c\uff0c\u5f88\u53ef\u80fd\u53d1\u73b0\u90e8\u5206\u6d4b\u8bd5\u6837\u4f8b\u65e0\u6cd5\u901a\u8fc7\u3002\u8fd9\u662f\u56e0\u4e3a\u8bbe\u8ba1\u7684\u8d2a\u5fc3\u7b56\u7565\u53ea\u662f\u201c\u90e8\u5206\u6b63\u786e\u201d\u7684\uff0c\u4e0a\u6587\u4ecb\u7ecd\u7684\u96f6\u94b1\u5151\u6362\u5c31\u662f\u4e2a\u5178\u578b\u6848\u4f8b\u3002

\u4e3a\u4e86\u4fdd\u8bc1\u6b63\u786e\u6027\uff0c\u6211\u4eec\u5e94\u8be5\u5bf9\u8d2a\u5fc3\u7b56\u7565\u8fdb\u884c\u4e25\u8c28\u7684\u6570\u5b66\u8bc1\u660e\uff0c\u901a\u5e38\u9700\u8981\u7528\u5230\u53cd\u8bc1\u6cd5\u6216\u6570\u5b66\u5f52\u7eb3\u6cd5\u3002

\u7136\u800c\uff0c\u6b63\u786e\u6027\u8bc1\u660e\u4e5f\u5f88\u53ef\u80fd\u4e0d\u662f\u4e00\u4ef6\u6613\u4e8b\u3002\u5982\u82e5\u6ca1\u6709\u5934\u7eea\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u9009\u62e9\u9762\u5411\u6d4b\u8bd5\u7528\u4f8b\u8fdb\u884c Debug \uff0c\u4e00\u6b65\u6b65\u4fee\u6539\u4e0e\u9a8c\u8bc1\u8d2a\u5fc3\u7b56\u7565\u3002

"},{"location":"chapter_greedy/greedy_algorithm/#1514","title":"15.1.4 \u00a0 \u8d2a\u5fc3\u5178\u578b\u4f8b\u9898","text":"

\u8d2a\u5fc3\u7b97\u6cd5\u5e38\u5e38\u5e94\u7528\u5728\u6ee1\u8db3\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u548c\u6700\u4f18\u5b50\u7ed3\u6784\u7684\u4f18\u5316\u95ee\u9898\u4e2d\uff0c\u4ee5\u4e0b\u5217\u4e3e\u4e86\u4e00\u4e9b\u5178\u578b\u7684\u8d2a\u5fc3\u7b97\u6cd5\u95ee\u9898\u3002

  • \u786c\u5e01\u627e\u96f6\u95ee\u9898\uff1a\u5728\u67d0\u4e9b\u786c\u5e01\u7ec4\u5408\u4e0b\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u603b\u662f\u53ef\u4ee5\u5f97\u5230\u6700\u4f18\u89e3\u3002
  • \u533a\u95f4\u8c03\u5ea6\u95ee\u9898\uff1a\u5047\u8bbe\u4f60\u6709\u4e00\u4e9b\u4efb\u52a1\uff0c\u6bcf\u4e2a\u4efb\u52a1\u5728\u4e00\u6bb5\u65f6\u95f4\u5185\u8fdb\u884c\uff0c\u4f60\u7684\u76ee\u6807\u662f\u5b8c\u6210\u5c3d\u53ef\u80fd\u591a\u7684\u4efb\u52a1\u3002\u5982\u679c\u6bcf\u6b21\u90fd\u9009\u62e9\u7ed3\u675f\u65f6\u95f4\u6700\u65e9\u7684\u4efb\u52a1\uff0c\u90a3\u4e48\u8d2a\u5fc3\u7b97\u6cd5\u5c31\u53ef\u4ee5\u5f97\u5230\u6700\u4f18\u89e3\u3002
  • \u5206\u6570\u80cc\u5305\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u7ec4\u7269\u54c1\u548c\u4e00\u4e2a\u8f7d\u91cd\u91cf\uff0c\u4f60\u7684\u76ee\u6807\u662f\u9009\u62e9\u4e00\u7ec4\u7269\u54c1\uff0c\u4f7f\u5f97\u603b\u91cd\u91cf\u4e0d\u8d85\u8fc7\u8f7d\u91cd\u91cf\uff0c\u4e14\u603b\u4ef7\u503c\u6700\u5927\u3002\u5982\u679c\u6bcf\u6b21\u90fd\u9009\u62e9\u6027\u4ef7\u6bd4\u6700\u9ad8\uff08\u4ef7\u503c / \u91cd\u91cf\uff09\u7684\u7269\u54c1\uff0c\u90a3\u4e48\u8d2a\u5fc3\u7b97\u6cd5\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\u53ef\u4ee5\u5f97\u5230\u6700\u4f18\u89e3\u3002
  • \u80a1\u7968\u4e70\u5356\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u7ec4\u80a1\u7968\u7684\u5386\u53f2\u4ef7\u683c\uff0c\u4f60\u53ef\u4ee5\u8fdb\u884c\u591a\u6b21\u4e70\u5356\uff0c\u4f46\u5982\u679c\u4f60\u5df2\u7ecf\u6301\u6709\u80a1\u7968\uff0c\u90a3\u4e48\u5728\u5356\u51fa\u4e4b\u524d\u4e0d\u80fd\u518d\u4e70\uff0c\u76ee\u6807\u662f\u83b7\u53d6\u6700\u5927\u5229\u6da6\u3002
  • \u970d\u592b\u66fc\u7f16\u7801\uff1a\u970d\u592b\u66fc\u7f16\u7801\u662f\u4e00\u79cd\u7528\u4e8e\u65e0\u635f\u6570\u636e\u538b\u7f29\u7684\u8d2a\u5fc3\u7b97\u6cd5\u3002\u901a\u8fc7\u6784\u5efa\u970d\u592b\u66fc\u6811\uff0c\u6bcf\u6b21\u9009\u62e9\u51fa\u73b0\u9891\u7387\u6700\u5c0f\u7684\u4e24\u4e2a\u8282\u70b9\u5408\u5e76\uff0c\u6700\u540e\u5f97\u5230\u7684\u970d\u592b\u66fc\u6811\u7684\u5e26\u6743\u8def\u5f84\u957f\u5ea6\uff08\u5373\u7f16\u7801\u957f\u5ea6\uff09\u6700\u5c0f\u3002
  • Dijkstra \u7b97\u6cd5\uff1a\u5b83\u662f\u4e00\u79cd\u89e3\u51b3\u7ed9\u5b9a\u6e90\u9876\u70b9\u5230\u5176\u4f59\u5404\u9876\u70b9\u7684\u6700\u77ed\u8def\u5f84\u95ee\u9898\u7684\u8d2a\u5fc3\u7b97\u6cd5\u3002
"},{"location":"chapter_greedy/max_capacity_problem/","title":"15.3 \u00a0 \u6700\u5927\u5bb9\u91cf\u95ee\u9898","text":"

Question

\u8f93\u5165\u4e00\u4e2a\u6570\u7ec4 \\(ht\\) \uff0c\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u4ee3\u8868\u4e00\u4e2a\u5782\u76f4\u9694\u677f\u7684\u9ad8\u5ea6\u3002\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u4e24\u4e2a\u9694\u677f\uff0c\u4ee5\u53ca\u5b83\u4eec\u4e4b\u95f4\u7684\u7a7a\u95f4\u53ef\u4ee5\u7ec4\u6210\u4e00\u4e2a\u5bb9\u5668\u3002

\u5bb9\u5668\u7684\u5bb9\u91cf\u7b49\u4e8e\u9ad8\u5ea6\u548c\u5bbd\u5ea6\u7684\u4e58\u79ef\uff08\u5373\u9762\u79ef\uff09\uff0c\u5176\u4e2d\u9ad8\u5ea6\u7531\u8f83\u77ed\u7684\u9694\u677f\u51b3\u5b9a\uff0c\u5bbd\u5ea6\u662f\u4e24\u4e2a\u9694\u677f\u7684\u6570\u7ec4\u7d22\u5f15\u4e4b\u5dee\u3002

\u8bf7\u5728\u6570\u7ec4\u4e2d\u9009\u62e9\u4e24\u4e2a\u9694\u677f\uff0c\u4f7f\u5f97\u7ec4\u6210\u7684\u5bb9\u5668\u7684\u5bb9\u91cf\u6700\u5927\uff0c\u8fd4\u56de\u6700\u5927\u5bb9\u91cf\u3002

\u56fe 15-7 \u00a0 \u6700\u5927\u5bb9\u91cf\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e

\u5bb9\u5668\u7531\u4efb\u610f\u4e24\u4e2a\u9694\u677f\u56f4\u6210\uff0c\u56e0\u6b64\u672c\u9898\u7684\u72b6\u6001\u4e3a\u4e24\u4e2a\u9694\u677f\u7684\u7d22\u5f15\uff0c\u8bb0\u4e3a \\([i, j]\\) \u3002

\u6839\u636e\u9898\u610f\uff0c\u5bb9\u91cf\u7b49\u4e8e\u9ad8\u5ea6\u4e58\u4ee5\u5bbd\u5ea6\uff0c\u5176\u4e2d\u9ad8\u5ea6\u7531\u77ed\u677f\u51b3\u5b9a\uff0c\u5bbd\u5ea6\u662f\u4e24\u9694\u677f\u7684\u7d22\u5f15\u4e4b\u5dee\u3002\u8bbe\u5bb9\u91cf\u4e3a \\(cap[i, j]\\) \uff0c\u5219\u53ef\u5f97\u8ba1\u7b97\u516c\u5f0f\uff1a

\\[ cap[i, j] = \\min(ht[i], ht[j]) \\times (j - i) \\]

\u8bbe\u6570\u7ec4\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u4e24\u4e2a\u9694\u677f\u7684\u7ec4\u5408\u6570\u91cf\uff08\u5373\u72b6\u6001\u603b\u6570\uff09\u4e3a \\(C_n^2 = \\frac{n(n - 1)}{2}\\) \u4e2a\u3002\u6700\u76f4\u63a5\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u7a77\u4e3e\u6240\u6709\u72b6\u6001\uff0c\u4ece\u800c\u6c42\u5f97\u6700\u5927\u5bb9\u91cf\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002

"},{"location":"chapter_greedy/max_capacity_problem/#1","title":"1. \u00a0 \u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a","text":"

\u8fd9\u9053\u9898\u8fd8\u6709\u66f4\u9ad8\u6548\u7387\u7684\u89e3\u6cd5\u3002\u5982\u56fe 15-8 \u6240\u793a\uff0c\u73b0\u9009\u53d6\u4e00\u4e2a\u72b6\u6001 \\([i, j]\\) \uff0c\u5176\u6ee1\u8db3\u7d22\u5f15 \\(i < j\\) \u4e14\u9ad8\u5ea6 \\(ht[i] < ht[j]\\) \uff0c\u5373 \\(i\\) \u4e3a\u77ed\u677f\u3001\\(j\\) \u4e3a\u957f\u677f\u3002

\u56fe 15-8 \u00a0 \u521d\u59cb\u72b6\u6001

\u5982\u56fe 15-9 \u6240\u793a\uff0c\u82e5\u6b64\u65f6\u5c06\u957f\u677f \\(j\\) \u5411\u77ed\u677f \\(i\\) \u9760\u8fd1\uff0c\u5219\u5bb9\u91cf\u4e00\u5b9a\u53d8\u5c0f\u3002

\u8fd9\u662f\u56e0\u4e3a\u5728\u79fb\u52a8\u957f\u677f \\(j\\) \u540e\uff0c\u5bbd\u5ea6 \\(j-i\\) \u80af\u5b9a\u53d8\u5c0f\uff1b\u800c\u9ad8\u5ea6\u7531\u77ed\u677f\u51b3\u5b9a\uff0c\u56e0\u6b64\u9ad8\u5ea6\u53ea\u53ef\u80fd\u4e0d\u53d8\uff08 \\(i\\) \u4ecd\u4e3a\u77ed\u677f\uff09\u6216\u53d8\u5c0f\uff08\u79fb\u52a8\u540e\u7684 \\(j\\) \u6210\u4e3a\u77ed\u677f\uff09\u3002

\u56fe 15-9 \u00a0 \u5411\u5185\u79fb\u52a8\u957f\u677f\u540e\u7684\u72b6\u6001

\u53cd\u5411\u601d\u8003\uff0c\u6211\u4eec\u53ea\u6709\u5411\u5185\u6536\u7f29\u77ed\u677f \\(i\\) \uff0c\u624d\u6709\u53ef\u80fd\u4f7f\u5bb9\u91cf\u53d8\u5927\u3002\u56e0\u4e3a\u867d\u7136\u5bbd\u5ea6\u4e00\u5b9a\u53d8\u5c0f\uff0c\u4f46\u9ad8\u5ea6\u53ef\u80fd\u4f1a\u53d8\u5927\uff08\u79fb\u52a8\u540e\u7684\u77ed\u677f \\(i\\) \u53ef\u80fd\u4f1a\u53d8\u957f\uff09\u3002\u4f8b\u5982\u5728\u56fe 15-10 \u4e2d\uff0c\u79fb\u52a8\u77ed\u677f\u540e\u9762\u79ef\u53d8\u5927\u3002

\u56fe 15-10 \u00a0 \u5411\u5185\u79fb\u52a8\u77ed\u677f\u540e\u7684\u72b6\u6001

\u7531\u6b64\u4fbf\u53ef\u63a8\u51fa\u672c\u9898\u7684\u8d2a\u5fc3\u7b56\u7565\uff1a\u521d\u59cb\u5316\u4e24\u6307\u9488\u5206\u88c2\u5bb9\u5668\u4e24\u7aef\uff0c\u6bcf\u8f6e\u5411\u5185\u6536\u7f29\u77ed\u677f\u5bf9\u5e94\u7684\u6307\u9488\uff0c\u76f4\u81f3\u4e24\u6307\u9488\u76f8\u9047\u3002

\u56fe 15-11 \u5c55\u793a\u4e86\u8d2a\u5fc3\u7b56\u7565\u7684\u6267\u884c\u8fc7\u7a0b\u3002

  1. \u521d\u59cb\u72b6\u6001\u4e0b\uff0c\u6307\u9488 \\(i\\) \u548c \\(j\\) \u5206\u5217\u4e0e\u6570\u7ec4\u4e24\u7aef\u3002
  2. \u8ba1\u7b97\u5f53\u524d\u72b6\u6001\u7684\u5bb9\u91cf \\(cap[i, j]\\) \uff0c\u5e76\u66f4\u65b0\u6700\u5927\u5bb9\u91cf\u3002
  3. \u6bd4\u8f83\u677f \\(i\\) \u548c \u677f \\(j\\) \u7684\u9ad8\u5ea6\uff0c\u5e76\u5c06\u77ed\u677f\u5411\u5185\u79fb\u52a8\u4e00\u683c\u3002
  4. \u5faa\u73af\u6267\u884c\u7b2c 2. \u548c 3. \u6b65\uff0c\u76f4\u81f3 \\(i\\) \u548c \\(j\\) \u76f8\u9047\u65f6\u7ed3\u675f\u3002
<1><2><3><4><5><6><7><8><9>

\u56fe 15-11 \u00a0 \u6700\u5927\u5bb9\u91cf\u95ee\u9898\u7684\u8d2a\u5fc3\u8fc7\u7a0b

"},{"location":"chapter_greedy/max_capacity_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u4ee3\u7801\u5faa\u73af\u6700\u591a \\(n\\) \u8f6e\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

\u53d8\u91cf \\(i\\)\u3001\\(j\\)\u3001\\(res\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u989d\u5916\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig max_capacity.py
def max_capacity(ht: list[int]) -> int:\n\"\"\"\u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3\"\"\"\n# \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\ni, j = 0, len(ht) - 1\n# \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nres = 0\n# \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile i < j:\n# \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\ncap = min(ht[i], ht[j]) * (j - i)\nres = max(res, cap)\n# \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif ht[i] < ht[j]:\ni += 1\nelse:\nj -= 1\nreturn res\n
max_capacity.cpp
/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(vector<int> &ht) {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\nint i = 0, j = ht.size() - 1;\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nint res = 0;\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile (i < j) {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\nint cap = min(ht[i], ht[j]) * (j - i);\nres = max(res, cap);\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif (ht[i] < ht[j]) {\ni++;\n} else {\nj--;\n}\n}\nreturn res;\n}\n
max_capacity.java
/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(int[] ht) {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\nint i = 0, j = ht.length - 1;\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nint res = 0;\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile (i < j) {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\nint cap = Math.min(ht[i], ht[j]) * (j - i);\nres = Math.max(res, cap);\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif (ht[i] < ht[j]) {\ni++;\n} else {\nj--;\n}\n}\nreturn res;\n}\n
max_capacity.cs
/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint MaxCapacity(int[] ht) {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\nint i = 0, j = ht.Length - 1;\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nint res = 0;\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile (i < j) {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\nint cap = Math.Min(ht[i], ht[j]) * (j - i);\nres = Math.Max(res, cap);\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif (ht[i] < ht[j]) {\ni++;\n} else {\nj--;\n}\n}\nreturn res;\n}\n
max_capacity.go
/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfunc maxCapacity(ht []int) int {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\ni, j := 0, len(ht)-1\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nres := 0\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nfor i < j {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\ncapacity := int(math.Min(float64(ht[i]), float64(ht[j]))) * (j - i)\nres = int(math.Max(float64(res), float64(capacity)))\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif ht[i] < ht[j] {\ni++\n} else {\nj--\n}\n}\nreturn res\n}\n
max_capacity.swift
/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfunc maxCapacity(ht: [Int]) -> Int {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\nvar i = 0, j = ht.count - 1\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nvar res = 0\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile i < j {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\nlet cap = min(ht[i], ht[j]) * (j - i)\nres = max(res, cap)\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif ht[i] < ht[j] {\ni += 1\n} else {\nj -= 1\n}\n}\nreturn res\n}\n
max_capacity.js
/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfunction maxCapacity(ht) {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\nlet i = 0,\nj = ht.length - 1;\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nlet res = 0;\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile (i < j) {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\nconst cap = Math.min(ht[i], ht[j]) * (j - i);\nres = Math.max(res, cap);\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif (ht[i] < ht[j]) {\ni += 1;\n} else {\nj -= 1;\n}\n}\nreturn res;\n}\n
max_capacity.ts
/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfunction maxCapacity(ht: number[]): number {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\nlet i = 0,\nj = ht.length - 1;\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nlet res = 0;\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile (i < j) {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\nconst cap: number = Math.min(ht[i], ht[j]) * (j - i);\nres = Math.max(res, cap);\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif (ht[i] < ht[j]) {\ni += 1;\n} else {\nj -= 1;\n}\n}\nreturn res;\n}\n
max_capacity.dart
/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(List<int> ht) {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\nint i = 0, j = ht.length - 1;\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nint res = 0;\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile (i < j) {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\nint cap = min(ht[i], ht[j]) * (j - i);\nres = max(res, cap);\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif (ht[i] < ht[j]) {\ni++;\n} else {\nj--;\n}\n}\nreturn res;\n}\n
max_capacity.rs
/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfn max_capacity(ht: &[i32]) -> i32 {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\nlet mut i = 0;\nlet mut j = ht.len() - 1;\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nlet mut res = 0;\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile i < j {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\nlet cap = std::cmp::min(ht[i], ht[j]) * (j - i) as i32;\nres = std::cmp::max(res, cap);\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif ht[i] < ht[j] {\ni += 1;\n} else {\nj -= 1;\n}\n}\nres\n}\n
max_capacity.c
/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(int ht[], int htLength) {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\nint i = 0;\nint j = htLength - 1;\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nint res = 0;\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile (i < j) {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\nint capacity = MIN(ht[i], ht[j]) * (j - i);\nres = MAX(res, capacity);\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif (ht[i] < ht[j]) {\ni++;\n} else {\nj--;\n}\n}\nreturn res;\n}\n
max_capacity.zig
[class]{}-[func]{maxCapacity}\n
"},{"location":"chapter_greedy/max_capacity_problem/#3","title":"3. \u00a0 \u6b63\u786e\u6027\u8bc1\u660e","text":"

\u4e4b\u6240\u4ee5\u8d2a\u5fc3\u6bd4\u7a77\u4e3e\u66f4\u5feb\uff0c\u662f\u56e0\u4e3a\u6bcf\u8f6e\u7684\u8d2a\u5fc3\u9009\u62e9\u90fd\u4f1a\u201c\u8df3\u8fc7\u201d\u4e00\u4e9b\u72b6\u6001\u3002

\u6bd4\u5982\u5728\u72b6\u6001 \\(cap[i, j]\\) \u4e0b\uff0c\\(i\\) \u4e3a\u77ed\u677f\u3001\\(j\\) \u4e3a\u957f\u677f\u3002\u82e5\u8d2a\u5fc3\u5730\u5c06\u77ed\u677f \\(i\\) \u5411\u5185\u79fb\u52a8\u4e00\u683c\uff0c\u4f1a\u5bfc\u81f4\u56fe 15-12 \u6240\u793a\u7684\u72b6\u6001\u88ab\u201c\u8df3\u8fc7\u201d\u3002\u8fd9\u610f\u5473\u7740\u4e4b\u540e\u65e0\u6cd5\u9a8c\u8bc1\u8fd9\u4e9b\u72b6\u6001\u7684\u5bb9\u91cf\u5927\u5c0f\u3002

\\[ cap[i, i+1], cap[i, i+2], \\dots, cap[i, j-2], cap[i, j-1] \\]

\u56fe 15-12 \u00a0 \u79fb\u52a8\u77ed\u677f\u5bfc\u81f4\u88ab\u8df3\u8fc7\u7684\u72b6\u6001

\u89c2\u5bdf\u53d1\u73b0\uff0c\u8fd9\u4e9b\u88ab\u8df3\u8fc7\u7684\u72b6\u6001\u5b9e\u9645\u4e0a\u5c31\u662f\u5c06\u957f\u677f \\(j\\) \u5411\u5185\u79fb\u52a8\u7684\u6240\u6709\u72b6\u6001\u3002\u800c\u5728\u7b2c\u4e8c\u6b65\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u8bc1\u660e\u5185\u79fb\u957f\u677f\u4e00\u5b9a\u4f1a\u5bfc\u81f4\u5bb9\u91cf\u53d8\u5c0f\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u88ab\u8df3\u8fc7\u7684\u72b6\u6001\u90fd\u4e0d\u53ef\u80fd\u662f\u6700\u4f18\u89e3\uff0c\u8df3\u8fc7\u5b83\u4eec\u4e0d\u4f1a\u5bfc\u81f4\u9519\u8fc7\u6700\u4f18\u89e3\u3002

\u4ee5\u4e0a\u7684\u5206\u6790\u8bf4\u660e\uff0c\u79fb\u52a8\u77ed\u677f\u7684\u64cd\u4f5c\u662f\u201c\u5b89\u5168\u201d\u7684\uff0c\u8d2a\u5fc3\u7b56\u7565\u662f\u6709\u6548\u7684\u3002

"},{"location":"chapter_greedy/max_product_cutting_problem/","title":"15.4 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u79ef\u95ee\u9898","text":"

Question

\u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570 \\(n\\) \uff0c\u5c06\u5176\u5207\u5206\u4e3a\u81f3\u5c11\u4e24\u4e2a\u6b63\u6574\u6570\u7684\u548c\uff0c\u6c42\u5207\u5206\u540e\u6240\u6709\u6574\u6570\u7684\u4e58\u79ef\u6700\u5927\u662f\u591a\u5c11\u3002

\u56fe 15-13 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u79ef\u7684\u95ee\u9898\u5b9a\u4e49

\u5047\u8bbe\u6211\u4eec\u5c06 \\(n\\) \u5207\u5206\u4e3a \\(m\\) \u4e2a\u6574\u6570\u56e0\u5b50\uff0c\u5176\u4e2d\u7b2c \\(i\\) \u4e2a\u56e0\u5b50\u8bb0\u4e3a \\(n_i\\) \uff0c\u5373

\\[ n = \\sum_{i=1}^{m}n_i \\]

\u672c\u9898\u76ee\u6807\u662f\u6c42\u5f97\u6240\u6709\u6574\u6570\u56e0\u5b50\u7684\u6700\u5927\u4e58\u79ef\uff0c\u5373

\\[ \\max(\\prod_{i=1}^{m}n_i) \\]

\u6211\u4eec\u9700\u8981\u601d\u8003\u7684\u662f\uff1a\u5207\u5206\u6570\u91cf \\(m\\) \u5e94\u8be5\u591a\u5927\uff0c\u6bcf\u4e2a \\(n_i\\) \u5e94\u8be5\u662f\u591a\u5c11\uff1f

"},{"location":"chapter_greedy/max_product_cutting_problem/#1","title":"1. \u00a0 \u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a","text":"

\u6839\u636e\u7ecf\u9a8c\uff0c\u4e24\u4e2a\u6574\u6570\u7684\u4e58\u79ef\u5f80\u5f80\u6bd4\u5b83\u4eec\u7684\u52a0\u548c\u66f4\u5927\u3002\u5047\u8bbe\u4ece \\(n\\) \u4e2d\u5206\u51fa\u4e00\u4e2a\u56e0\u5b50 \\(2\\) \uff0c\u5219\u5b83\u4eec\u7684\u4e58\u79ef\u4e3a \\(2(n-2)\\) \u3002\u6211\u4eec\u5c06\u8be5\u4e58\u79ef\u4e0e \\(n\\) \u4f5c\u6bd4\u8f83\uff1a

\\[ \\begin{aligned} 2(n-2) & \\geq n \\newline 2n - n - 4 & \\geq 0 \\newline n & \\geq 4 \\end{aligned} \\]

\u5982\u56fe 15-14 \u6240\u793a\uff0c\u5f53 \\(n \\geq 4\\) \u65f6\uff0c\u5207\u5206\u51fa\u4e00\u4e2a \\(2\\) \u540e\u4e58\u79ef\u4f1a\u53d8\u5927\uff0c\u8fd9\u8bf4\u660e\u5927\u4e8e\u7b49\u4e8e \\(4\\) \u7684\u6574\u6570\u90fd\u5e94\u8be5\u88ab\u5207\u5206\u3002

\u8d2a\u5fc3\u7b56\u7565\u4e00\uff1a\u5982\u679c\u5207\u5206\u65b9\u6848\u4e2d\u5305\u542b \\(\\geq 4\\) \u7684\u56e0\u5b50\uff0c\u90a3\u4e48\u5b83\u5c31\u5e94\u8be5\u88ab\u7ee7\u7eed\u5207\u5206\u3002\u6700\u7ec8\u7684\u5207\u5206\u65b9\u6848\u53ea\u5e94\u51fa\u73b0 \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u8fd9\u4e09\u79cd\u56e0\u5b50\u3002

\u56fe 15-14 \u00a0 \u5207\u5206\u5bfc\u81f4\u4e58\u79ef\u53d8\u5927

\u63a5\u4e0b\u6765\u601d\u8003\u54ea\u4e2a\u56e0\u5b50\u662f\u6700\u4f18\u7684\u3002\u5728 \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u8fd9\u4e09\u4e2a\u56e0\u5b50\u4e2d\uff0c\u663e\u7136 \\(1\\) \u662f\u6700\u5dee\u7684\uff0c\u56e0\u4e3a \\(1 \\times (n-1) < n\\) \u6052\u6210\u7acb\uff0c\u5373\u5207\u5206\u51fa \\(1\\) \u53cd\u800c\u4f1a\u5bfc\u81f4\u4e58\u79ef\u51cf\u5c0f\u3002

\u5982\u56fe 15-15 \u6240\u793a\uff0c\u5f53 \\(n = 6\\) \u65f6\uff0c\u6709 \\(3 \\times 3 > 2 \\times 2 \\times 2\\) \u3002\u8fd9\u610f\u5473\u7740\u5207\u5206\u51fa \\(3\\) \u6bd4\u5207\u5206\u51fa \\(2\\) \u66f4\u4f18\u3002

\u8d2a\u5fc3\u7b56\u7565\u4e8c\uff1a\u5728\u5207\u5206\u65b9\u6848\u4e2d\uff0c\u6700\u591a\u53ea\u5e94\u5b58\u5728\u4e24\u4e2a \\(2\\) \u3002\u56e0\u4e3a\u4e09\u4e2a \\(2\\) \u603b\u662f\u53ef\u4ee5\u88ab\u66ff\u6362\u4e3a\u4e24\u4e2a \\(3\\) \uff0c\u4ece\u800c\u83b7\u5f97\u66f4\u5927\u4e58\u79ef\u3002

\u56fe 15-15 \u00a0 \u6700\u4f18\u5207\u5206\u56e0\u5b50

\u603b\u7ed3\u4ee5\u4e0a\uff0c\u53ef\u63a8\u51fa\u4ee5\u4e0b\u8d2a\u5fc3\u7b56\u7565\u3002

  1. \u8f93\u5165\u6574\u6570 \\(n\\) \uff0c\u4ece\u5176\u4e0d\u65ad\u5730\u5207\u5206\u51fa\u56e0\u5b50 \\(3\\) \uff0c\u76f4\u81f3\u4f59\u6570\u4e3a \\(0\\)\u3001\\(1\\)\u3001\\(2\\) \u3002
  2. \u5f53\u4f59\u6570\u4e3a \\(0\\) \u65f6\uff0c\u4ee3\u8868 \\(n\\) \u662f \\(3\\) \u7684\u500d\u6570\uff0c\u56e0\u6b64\u4e0d\u505a\u4efb\u4f55\u5904\u7406\u3002
  3. \u5f53\u4f59\u6570\u4e3a \\(2\\) \u65f6\uff0c\u4e0d\u7ee7\u7eed\u5212\u5206\uff0c\u4fdd\u7559\u4e4b\u3002
  4. \u5f53\u4f59\u6570\u4e3a \\(1\\) \u65f6\uff0c\u7531\u4e8e \\(2 \\times 2 > 1 \\times 3\\) \uff0c\u56e0\u6b64\u5e94\u5c06\u6700\u540e\u4e00\u4e2a \\(3\\) \u66ff\u6362\u4e3a \\(2\\) \u3002
"},{"location":"chapter_greedy/max_product_cutting_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u5982\u56fe 15-16 \u6240\u793a\uff0c\u6211\u4eec\u65e0\u987b\u901a\u8fc7\u5faa\u73af\u6765\u5207\u5206\u6574\u6570\uff0c\u800c\u53ef\u4ee5\u5229\u7528\u5411\u4e0b\u6574\u9664\u8fd0\u7b97\u5f97\u5230 \\(3\\) \u7684\u4e2a\u6570 \\(a\\) \uff0c\u7528\u53d6\u6a21\u8fd0\u7b97\u5f97\u5230\u4f59\u6570 \\(b\\) \uff0c\u6b64\u65f6\u6709\uff1a

\\[ n = 3 a + b \\]

\u8bf7\u6ce8\u610f\uff0c\u5bf9\u4e8e \\(n \\leq 3\\) \u7684\u8fb9\u754c\u60c5\u51b5\uff0c\u5fc5\u987b\u62c6\u5206\u51fa\u4e00\u4e2a \\(1\\) \uff0c\u4e58\u79ef\u4e3a \\(1 \\times (n - 1)\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig max_product_cutting.py
def max_product_cutting(n: int) -> int:\n\"\"\"\u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3\"\"\"\n# \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif n <= 3:\nreturn 1 * (n - 1)\n# \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\na, b = n // 3, n % 3\nif b == 1:\n# \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn int(math.pow(3, a - 1)) * 2 * 2\nif b == 2:\n# \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn int(math.pow(3, a)) * 2\n# \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn int(math.pow(3, a))\n
max_product_cutting.cpp
/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n// \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif (n <= 3) {\nreturn 1 * (n - 1);\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\nint a = n / 3;\nint b = n % 3;\nif (b == 1) {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn (int)pow(3, a - 1) * 2 * 2;\n}\nif (b == 2) {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn (int)pow(3, a) * 2;\n}\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn (int)pow(3, a);\n}\n
max_product_cutting.java
/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n// \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif (n <= 3) {\nreturn 1 * (n - 1);\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\nint a = n / 3;\nint b = n % 3;\nif (b == 1) {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn (int) Math.pow(3, a - 1) * 2 * 2;\n}\nif (b == 2) {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn (int) Math.pow(3, a) * 2;\n}\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn (int) Math.pow(3, a);\n}\n
max_product_cutting.cs
/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint MaxProductCutting(int n) {\n// \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif (n <= 3) {\nreturn 1 * (n - 1);\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\nint a = n / 3;\nint b = n % 3;\nif (b == 1) {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn (int)Math.Pow(3, a - 1) * 2 * 2;\n}\nif (b == 2) {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn (int)Math.Pow(3, a) * 2;\n}\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn (int)Math.Pow(3, a);\n}\n
max_product_cutting.go
/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfunc maxProductCutting(n int) int {\n// \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif n <= 3 {\nreturn 1 * (n - 1)\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\na := n / 3\nb := n % 3\nif b == 1 {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn int(math.Pow(3, float64(a-1))) * 2 * 2\n}\nif b == 2 {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn int(math.Pow(3, float64(a))) * 2\n}\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn int(math.Pow(3, float64(a)))\n}\n
max_product_cutting.swift
/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfunc maxProductCutting(n: Int) -> Int {\n// \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif n <= 3 {\nreturn 1 * (n - 1)\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\nlet a = n / 3\nlet b = n % 3\nif b == 1 {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn pow(3, a - 1) * 2 * 2\n}\nif b == 2 {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn pow(3, a) * 2\n}\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn pow(3, a)\n}\n
max_product_cutting.js
/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfunction maxProductCutting(n) {\n// \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif (n <= 3) {\nreturn 1 * (n - 1);\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\nlet a = Math.floor(n / 3);\nlet b = n % 3;\nif (b === 1) {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn Math.pow(3, a - 1) * 2 * 2;\n}\nif (b === 2) {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn Math.pow(3, a) * 2;\n}\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn Math.pow(3, a);\n}\n
max_product_cutting.ts
/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfunction maxProductCutting(n: number): number {\n// \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif (n <= 3) {\nreturn 1 * (n - 1);\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\nlet a: number = Math.floor(n / 3);\nlet b: number = n % 3;\nif (b === 1) {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn Math.pow(3, a - 1) * 2 * 2;\n}\nif (b === 2) {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn Math.pow(3, a) * 2;\n}\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn Math.pow(3, a);\n}\n
max_product_cutting.dart
/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n// \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif (n <= 3) {\nreturn 1 * (n - 1);\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\nint a = n ~/ 3;\nint b = n % 3;\nif (b == 1) {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn (pow(3, a - 1) * 2 * 2).toInt();\n}\nif (b == 2) {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn (pow(3, a) * 2).toInt();\n}\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn pow(3, a).toInt();\n}\n
max_product_cutting.rs
/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfn max_product_cutting(n: i32) -> i32 {\n// \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif n <= 3 {\nreturn 1 * (n - 1);\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\nlet a = n / 3;\nlet b = n % 3;\nif b == 1 {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n3_i32.pow(a as u32 - 1) * 2 * 2\n} else if b == 2 {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n3_i32.pow(a as u32) * 2\n} else {\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n3_i32.pow(a as u32)\n}\n}\n
max_product_cutting.c
/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n// \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif (n <= 3) {\nreturn 1 * (n - 1);\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\nint a = n / 3;\nint b = n % 3;\nif (b == 1) {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn pow(3, a - 1) * 2 * 2;\n}\nif (b == 2) {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn pow(3, a) * 2;\n}\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn pow(3, a);\n}\n
max_product_cutting.zig
[class]{}-[func]{maxProductCutting}\n

\u56fe 15-16 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u79ef\u7684\u8ba1\u7b97\u65b9\u6cd5

\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u7f16\u7a0b\u8bed\u8a00\u7684\u5e42\u8fd0\u7b97\u7684\u5b9e\u73b0\u65b9\u6cd5\u3002\u4ee5 Python \u4e3a\u4f8b\uff0c\u5e38\u7528\u7684\u5e42\u8ba1\u7b97\u51fd\u6570\u6709\u4e09\u79cd\u3002

  • \u8fd0\u7b97\u7b26 ** \u548c\u51fd\u6570 pow() \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(\\log\u2061 a)\\) \u3002
  • \u51fd\u6570 math.pow() \u5185\u90e8\u8c03\u7528 C \u8bed\u8a00\u5e93\u7684 pow() \u51fd\u6570\uff0c\u5176\u6267\u884c\u6d6e\u70b9\u53d6\u5e42\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002

\u53d8\u91cf \\(a\\) \u548c \\(b\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002

"},{"location":"chapter_greedy/max_product_cutting_problem/#3","title":"3. \u00a0 \u6b63\u786e\u6027\u8bc1\u660e","text":"

\u4f7f\u7528\u53cd\u8bc1\u6cd5\uff0c\u53ea\u5206\u6790 \\(n \\geq 3\\) \u7684\u60c5\u51b5\u3002

  1. \u6240\u6709\u56e0\u5b50 \\(\\leq 3\\) \uff1a\u5047\u8bbe\u6700\u4f18\u5207\u5206\u65b9\u6848\u4e2d\u5b58\u5728 \\(\\geq 4\\) \u7684\u56e0\u5b50 \\(x\\) \uff0c\u90a3\u4e48\u4e00\u5b9a\u53ef\u4ee5\u5c06\u5176\u7ee7\u7eed\u5212\u5206\u4e3a \\(2(x-2)\\) \uff0c\u4ece\u800c\u83b7\u5f97\u66f4\u5927\u7684\u4e58\u79ef\u3002\u8fd9\u4e0e\u5047\u8bbe\u77db\u76fe\u3002
  2. \u5207\u5206\u65b9\u6848\u4e0d\u5305\u542b \\(1\\) \uff1a\u5047\u8bbe\u6700\u4f18\u5207\u5206\u65b9\u6848\u4e2d\u5b58\u5728\u4e00\u4e2a\u56e0\u5b50 \\(1\\) \uff0c\u90a3\u4e48\u5b83\u4e00\u5b9a\u53ef\u4ee5\u5408\u5e76\u5165\u53e6\u5916\u4e00\u4e2a\u56e0\u5b50\u4e2d\uff0c\u4ee5\u83b7\u53d6\u66f4\u5927\u4e58\u79ef\u3002\u8fd9\u4e0e\u5047\u8bbe\u77db\u76fe\u3002
  3. \u5207\u5206\u65b9\u6848\u6700\u591a\u5305\u542b\u4e24\u4e2a \\(2\\) \uff1a\u5047\u8bbe\u6700\u4f18\u5207\u5206\u65b9\u6848\u4e2d\u5305\u542b\u4e09\u4e2a \\(2\\) \uff0c\u90a3\u4e48\u4e00\u5b9a\u53ef\u4ee5\u66ff\u6362\u4e3a\u4e24\u4e2a \\(3\\) \uff0c\u4e58\u79ef\u66f4\u5927\u3002\u8fd9\u4e0e\u5047\u8bbe\u77db\u76fe\u3002
"},{"location":"chapter_greedy/summary/","title":"15.5 \u00a0 \u5c0f\u7ed3","text":"
  • \u8d2a\u5fc3\u7b97\u6cd5\u901a\u5e38\u7528\u4e8e\u89e3\u51b3\u6700\u4f18\u5316\u95ee\u9898\uff0c\u5176\u539f\u7406\u662f\u5728\u6bcf\u4e2a\u51b3\u7b56\u9636\u6bb5\u90fd\u505a\u51fa\u5c40\u90e8\u6700\u4f18\u7684\u51b3\u7b56\uff0c\u4ee5\u671f\u671b\u83b7\u5f97\u5168\u5c40\u6700\u4f18\u89e3\u3002
  • \u8d2a\u5fc3\u7b97\u6cd5\u4f1a\u8fed\u4ee3\u5730\u505a\u51fa\u4e00\u4e2a\u53c8\u4e00\u4e2a\u7684\u8d2a\u5fc3\u9009\u62e9\uff0c\u6bcf\u8f6e\u90fd\u5c06\u95ee\u9898\u8f6c\u5316\u6210\u4e00\u4e2a\u89c4\u6a21\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u76f4\u5230\u95ee\u9898\u88ab\u89e3\u51b3\u3002
  • \u8d2a\u5fc3\u7b97\u6cd5\u4e0d\u4ec5\u5b9e\u73b0\u7b80\u5355\uff0c\u8fd8\u5177\u6709\u5f88\u9ad8\u7684\u89e3\u9898\u6548\u7387\u3002\u76f8\u6bd4\u4e8e\u52a8\u6001\u89c4\u5212\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u66f4\u4f4e\u3002
  • \u5728\u96f6\u94b1\u5151\u6362\u95ee\u9898\u4e2d\uff0c\u5bf9\u4e8e\u67d0\u4e9b\u786c\u5e01\u7ec4\u5408\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u53ef\u4ee5\u4fdd\u8bc1\u627e\u5230\u6700\u4f18\u89e3\uff1b\u5bf9\u4e8e\u53e6\u5916\u4e00\u4e9b\u786c\u5e01\u7ec4\u5408\u5219\u4e0d\u7136\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u53ef\u80fd\u627e\u5230\u5f88\u5dee\u7684\u89e3\u3002
  • \u9002\u5408\u7528\u8d2a\u5fc3\u7b97\u6cd5\u6c42\u89e3\u7684\u95ee\u9898\u5177\u6709\u4e24\u5927\u6027\u8d28\uff1a\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u548c\u6700\u4f18\u5b50\u7ed3\u6784\u3002\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u4ee3\u8868\u8d2a\u5fc3\u7b56\u7565\u7684\u6709\u6548\u6027\u3002
  • \u5bf9\u4e8e\u67d0\u4e9b\u590d\u6742\u95ee\u9898\uff0c\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u7684\u8bc1\u660e\u5e76\u4e0d\u7b80\u5355\u3002\u76f8\u5bf9\u6765\u8bf4\uff0c\u8bc1\u4f2a\u66f4\u52a0\u5bb9\u6613\uff0c\u4f8b\u5982\u96f6\u94b1\u5151\u6362\u95ee\u9898\u3002
  • \u6c42\u89e3\u8d2a\u5fc3\u95ee\u9898\u4e3b\u8981\u5206\u4e3a\u4e09\u6b65\uff1a\u95ee\u9898\u5206\u6790\u3001\u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a\u3001\u6b63\u786e\u6027\u8bc1\u660e\u3002\u5176\u4e2d\uff0c\u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a\u662f\u6838\u5fc3\u6b65\u9aa4\uff0c\u6b63\u786e\u6027\u8bc1\u660e\u5f80\u5f80\u662f\u96be\u70b9\u3002
  • \u5206\u6570\u80cc\u5305\u95ee\u9898\u5728 0-1 \u80cc\u5305\u7684\u57fa\u7840\u4e0a\uff0c\u5141\u8bb8\u9009\u62e9\u7269\u54c1\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u53ef\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u6c42\u89e3\u3002\u8d2a\u5fc3\u7b56\u7565\u7684\u6b63\u786e\u6027\u53ef\u4ee5\u4f7f\u7528\u53cd\u8bc1\u6cd5\u6765\u8bc1\u660e\u3002
  • \u6700\u5927\u5bb9\u91cf\u95ee\u9898\u53ef\u4f7f\u7528\u7a77\u4e3e\u6cd5\u6c42\u89e3\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002\u901a\u8fc7\u8bbe\u8ba1\u8d2a\u5fc3\u7b56\u7565\uff0c\u6bcf\u8f6e\u5411\u5185\u79fb\u52a8\u77ed\u677f\uff0c\u53ef\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(n)\\) \u3002
  • \u5728\u6700\u5927\u5207\u5206\u4e58\u79ef\u95ee\u9898\u4e2d\uff0c\u6211\u4eec\u5148\u540e\u63a8\u7406\u51fa\u4e24\u4e2a\u8d2a\u5fc3\u7b56\u7565\uff1a\\(\\geq 4\\) \u7684\u6574\u6570\u90fd\u5e94\u8be5\u7ee7\u7eed\u5207\u5206\u3001\u6700\u4f18\u5207\u5206\u56e0\u5b50\u4e3a \\(3\\) \u3002\u4ee3\u7801\u4e2d\u5305\u542b\u5e42\u8fd0\u7b97\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u5e42\u8fd0\u7b97\u5b9e\u73b0\u65b9\u6cd5\uff0c\u901a\u5e38\u4e3a \\(O(1)\\) \u6216 \\(O(\\log n)\\) \u3002
"},{"location":"chapter_hashing/","title":"\u7b2c 6 \u7ae0 \u00a0 \u54c8\u5e0c\u8868","text":"

Abstract

\u5728\u8ba1\u7b97\u673a\u4e16\u754c\u4e2d\uff0c\u54c8\u5e0c\u8868\u5982\u540c\u4e00\u4f4d\u667a\u80fd\u7684\u56fe\u4e66\u7ba1\u7406\u5458\u3002

\u4ed6\u77e5\u9053\u5982\u4f55\u8ba1\u7b97\u7d22\u4e66\u53f7\uff0c\u4ece\u800c\u53ef\u4ee5\u5feb\u901f\u627e\u5230\u76ee\u6807\u4e66\u7c4d\u3002

"},{"location":"chapter_hashing/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 6.1 \u00a0 \u54c8\u5e0c\u8868
  • 6.2 \u00a0 \u54c8\u5e0c\u51b2\u7a81
  • 6.3 \u00a0 \u54c8\u5e0c\u7b97\u6cd5
  • 6.4 \u00a0 \u5c0f\u7ed3
"},{"location":"chapter_hashing/hash_algorithm/","title":"6.3 \u00a0 \u54c8\u5e0c\u7b97\u6cd5","text":"

\u5728\u4e0a\u4e24\u8282\u4e2d\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u54c8\u5e0c\u8868\u7684\u5de5\u4f5c\u539f\u7406\u548c\u54c8\u5e0c\u51b2\u7a81\u7684\u5904\u7406\u65b9\u6cd5\u3002\u7136\u800c\u65e0\u8bba\u662f\u5f00\u653e\u5bfb\u5740\u8fd8\u662f\u94fe\u5730\u5740\u6cd5\uff0c\u5b83\u4eec\u53ea\u80fd\u4fdd\u8bc1\u54c8\u5e0c\u8868\u53ef\u4ee5\u5728\u53d1\u751f\u51b2\u7a81\u65f6\u6b63\u5e38\u5de5\u4f5c\uff0c\u4f46\u65e0\u6cd5\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\u7684\u53d1\u751f\u3002

\u5982\u679c\u54c8\u5e0c\u51b2\u7a81\u8fc7\u4e8e\u9891\u7e41\uff0c\u54c8\u5e0c\u8868\u7684\u6027\u80fd\u5219\u4f1a\u6025\u5267\u52a3\u5316\u3002\u5982\u56fe 6-8 \u6240\u793a\uff0c\u5bf9\u4e8e\u94fe\u5730\u5740\u54c8\u5e0c\u8868\uff0c\u7406\u60f3\u60c5\u51b5\u4e0b\u952e\u503c\u5bf9\u5e73\u5747\u5206\u5e03\u5728\u5404\u4e2a\u6876\u4e2d\uff0c\u8fbe\u5230\u6700\u4f73\u67e5\u8be2\u6548\u7387\uff1b\u6700\u5dee\u60c5\u51b5\u4e0b\u6240\u6709\u952e\u503c\u5bf9\u90fd\u88ab\u5b58\u50a8\u5230\u540c\u4e00\u4e2a\u6876\u4e2d\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u9000\u5316\u81f3 \\(O(n)\\) \u3002

\u56fe 6-8 \u00a0 \u54c8\u5e0c\u51b2\u7a81\u7684\u6700\u4f73\u4e0e\u6700\u5dee\u60c5\u51b5

\u952e\u503c\u5bf9\u7684\u5206\u5e03\u60c5\u51b5\u7531\u54c8\u5e0c\u51fd\u6570\u51b3\u5b9a\u3002\u56de\u5fc6\u54c8\u5e0c\u51fd\u6570\u7684\u8ba1\u7b97\u6b65\u9aa4\uff0c\u5148\u8ba1\u7b97\u54c8\u5e0c\u503c\uff0c\u518d\u5bf9\u6570\u7ec4\u957f\u5ea6\u53d6\u6a21\uff1a

index = hash(key) % capacity\n

\u89c2\u5bdf\u4ee5\u4e0a\u516c\u5f0f\uff0c\u5f53\u54c8\u5e0c\u8868\u5bb9\u91cf capacity \u56fa\u5b9a\u65f6\uff0c\u54c8\u5e0c\u7b97\u6cd5 hash() \u51b3\u5b9a\u4e86\u8f93\u51fa\u503c\uff0c\u8fdb\u800c\u51b3\u5b9a\u4e86\u952e\u503c\u5bf9\u5728\u54c8\u5e0c\u8868\u4e2d\u7684\u5206\u5e03\u60c5\u51b5\u3002

\u8fd9\u610f\u5473\u7740\uff0c\u4e3a\u4e86\u51cf\u5c0f\u54c8\u5e0c\u51b2\u7a81\u7684\u53d1\u751f\u6982\u7387\uff0c\u6211\u4eec\u5e94\u5f53\u5c06\u6ce8\u610f\u529b\u96c6\u4e2d\u5728\u54c8\u5e0c\u7b97\u6cd5 hash() \u7684\u8bbe\u8ba1\u4e0a\u3002

"},{"location":"chapter_hashing/hash_algorithm/#631","title":"6.3.1 \u00a0 \u54c8\u5e0c\u7b97\u6cd5\u7684\u76ee\u6807","text":"

\u4e3a\u4e86\u5b9e\u73b0\u201c\u65e2\u5feb\u53c8\u7a33\u201d\u7684\u54c8\u5e0c\u8868\u6570\u636e\u7ed3\u6784\uff0c\u54c8\u5e0c\u7b97\u6cd5\u5e94\u5305\u542b\u4ee5\u4e0b\u7279\u70b9\u3002

  • \u786e\u5b9a\u6027\uff1a\u5bf9\u4e8e\u76f8\u540c\u7684\u8f93\u5165\uff0c\u54c8\u5e0c\u7b97\u6cd5\u5e94\u59cb\u7ec8\u4ea7\u751f\u76f8\u540c\u7684\u8f93\u51fa\u3002\u8fd9\u6837\u624d\u80fd\u786e\u4fdd\u54c8\u5e0c\u8868\u662f\u53ef\u9760\u7684\u3002
  • \u6548\u7387\u9ad8\uff1a\u8ba1\u7b97\u54c8\u5e0c\u503c\u7684\u8fc7\u7a0b\u5e94\u8be5\u8db3\u591f\u5feb\u3002\u8ba1\u7b97\u5f00\u9500\u8d8a\u5c0f\uff0c\u54c8\u5e0c\u8868\u7684\u5b9e\u7528\u6027\u8d8a\u9ad8\u3002
  • \u5747\u5300\u5206\u5e03\uff1a\u54c8\u5e0c\u7b97\u6cd5\u5e94\u4f7f\u5f97\u952e\u503c\u5bf9\u5e73\u5747\u5206\u5e03\u5728\u54c8\u5e0c\u8868\u4e2d\u3002\u5206\u5e03\u8d8a\u5e73\u5747\uff0c\u54c8\u5e0c\u51b2\u7a81\u7684\u6982\u7387\u5c31\u8d8a\u4f4e\u3002

\u5b9e\u9645\u4e0a\uff0c\u54c8\u5e0c\u7b97\u6cd5\u9664\u4e86\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u54c8\u5e0c\u8868\uff0c\u8fd8\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5176\u4ed6\u9886\u57df\u4e2d\u3002

  • \u5bc6\u7801\u5b58\u50a8\uff1a\u4e3a\u4e86\u4fdd\u62a4\u7528\u6237\u5bc6\u7801\u7684\u5b89\u5168\uff0c\u7cfb\u7edf\u901a\u5e38\u4e0d\u4f1a\u76f4\u63a5\u5b58\u50a8\u7528\u6237\u7684\u660e\u6587\u5bc6\u7801\uff0c\u800c\u662f\u5b58\u50a8\u5bc6\u7801\u7684\u54c8\u5e0c\u503c\u3002\u5f53\u7528\u6237\u8f93\u5165\u5bc6\u7801\u65f6\uff0c\u7cfb\u7edf\u4f1a\u5bf9\u8f93\u5165\u7684\u5bc6\u7801\u8ba1\u7b97\u54c8\u5e0c\u503c\uff0c\u7136\u540e\u4e0e\u5b58\u50a8\u7684\u54c8\u5e0c\u503c\u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679c\u4e24\u8005\u5339\u914d\uff0c\u90a3\u4e48\u5bc6\u7801\u5c31\u88ab\u89c6\u4e3a\u6b63\u786e\u3002
  • \u6570\u636e\u5b8c\u6574\u6027\u68c0\u67e5\uff1a\u6570\u636e\u53d1\u9001\u65b9\u53ef\u4ee5\u8ba1\u7b97\u6570\u636e\u7684\u54c8\u5e0c\u503c\u5e76\u5c06\u5176\u4e00\u540c\u53d1\u9001\uff1b\u63a5\u6536\u65b9\u53ef\u4ee5\u91cd\u65b0\u8ba1\u7b97\u63a5\u6536\u5230\u7684\u6570\u636e\u7684\u54c8\u5e0c\u503c\uff0c\u5e76\u4e0e\u63a5\u6536\u5230\u7684\u54c8\u5e0c\u503c\u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679c\u4e24\u8005\u5339\u914d\uff0c\u90a3\u4e48\u6570\u636e\u5c31\u88ab\u89c6\u4e3a\u5b8c\u6574\u7684\u3002

\u5bf9\u4e8e\u5bc6\u7801\u5b66\u7684\u76f8\u5173\u5e94\u7528\uff0c\u4e3a\u4e86\u9632\u6b62\u4ece\u54c8\u5e0c\u503c\u63a8\u5bfc\u51fa\u539f\u59cb\u5bc6\u7801\u7b49\u9006\u5411\u5de5\u7a0b\uff0c\u54c8\u5e0c\u7b97\u6cd5\u9700\u8981\u5177\u5907\u66f4\u9ad8\u7b49\u7ea7\u7684\u5b89\u5168\u7279\u6027\u3002

  • \u5355\u5411\u6027\uff1a\u65e0\u6cd5\u901a\u8fc7\u54c8\u5e0c\u503c\u53cd\u63a8\u51fa\u5173\u4e8e\u8f93\u5165\u6570\u636e\u7684\u4efb\u4f55\u4fe1\u606f\u3002
  • \u6297\u78b0\u649e\u6027\uff1a\u5e94\u5f53\u6781\u5176\u56f0\u96be\u627e\u5230\u4e24\u4e2a\u4e0d\u540c\u7684\u8f93\u5165\uff0c\u4f7f\u5f97\u5b83\u4eec\u7684\u54c8\u5e0c\u503c\u76f8\u540c\u3002
  • \u96ea\u5d29\u6548\u5e94\uff1a\u8f93\u5165\u7684\u5fae\u5c0f\u53d8\u5316\u5e94\u5f53\u5bfc\u81f4\u8f93\u51fa\u7684\u663e\u8457\u4e14\u4e0d\u53ef\u9884\u6d4b\u7684\u53d8\u5316\u3002

\u8bf7\u6ce8\u610f\uff0c\u201c\u5747\u5300\u5206\u5e03\u201d\u4e0e\u201c\u6297\u78b0\u649e\u6027\u201d\u662f\u4e24\u4e2a\u72ec\u7acb\u7684\u6982\u5ff5\uff0c\u6ee1\u8db3\u5747\u5300\u5206\u5e03\u4e0d\u4e00\u5b9a\u6ee1\u8db3\u6297\u78b0\u649e\u6027\u3002\u4f8b\u5982\uff0c\u5728\u968f\u673a\u8f93\u5165 key \u4e0b\uff0c\u54c8\u5e0c\u51fd\u6570 key % 100 \u53ef\u4ee5\u4ea7\u751f\u5747\u5300\u5206\u5e03\u7684\u8f93\u51fa\u3002\u7136\u800c\u8be5\u54c8\u5e0c\u7b97\u6cd5\u8fc7\u4e8e\u7b80\u5355\uff0c\u6240\u6709\u540e\u4e24\u4f4d\u76f8\u7b49\u7684 key \u7684\u8f93\u51fa\u90fd\u76f8\u540c\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u4ece\u54c8\u5e0c\u503c\u53cd\u63a8\u51fa\u53ef\u7528\u7684 key \uff0c\u4ece\u800c\u7834\u89e3\u5bc6\u7801\u3002

"},{"location":"chapter_hashing/hash_algorithm/#632","title":"6.3.2 \u00a0 \u54c8\u5e0c\u7b97\u6cd5\u7684\u8bbe\u8ba1","text":"

\u54c8\u5e0c\u7b97\u6cd5\u7684\u8bbe\u8ba1\u662f\u4e00\u4e2a\u9700\u8981\u8003\u8651\u8bb8\u591a\u56e0\u7d20\u7684\u590d\u6742\u95ee\u9898\u3002\u7136\u800c\u5bf9\u4e8e\u67d0\u4e9b\u8981\u6c42\u4e0d\u9ad8\u7684\u573a\u666f\uff0c\u6211\u4eec\u4e5f\u80fd\u8bbe\u8ba1\u4e00\u4e9b\u7b80\u5355\u7684\u54c8\u5e0c\u7b97\u6cd5\u3002

  • \u52a0\u6cd5\u54c8\u5e0c\uff1a\u5bf9\u8f93\u5165\u7684\u6bcf\u4e2a\u5b57\u7b26\u7684 ASCII \u7801\u8fdb\u884c\u76f8\u52a0\uff0c\u5c06\u5f97\u5230\u7684\u603b\u548c\u4f5c\u4e3a\u54c8\u5e0c\u503c\u3002
  • \u4e58\u6cd5\u54c8\u5e0c\uff1a\u5229\u7528\u4e86\u4e58\u6cd5\u7684\u4e0d\u76f8\u5173\u6027\uff0c\u6bcf\u8f6e\u4e58\u4ee5\u4e00\u4e2a\u5e38\u6570\uff0c\u5c06\u5404\u4e2a\u5b57\u7b26\u7684 ASCII \u7801\u7d2f\u79ef\u5230\u54c8\u5e0c\u503c\u4e2d\u3002
  • \u5f02\u6216\u54c8\u5e0c\uff1a\u5c06\u8f93\u5165\u6570\u636e\u7684\u6bcf\u4e2a\u5143\u7d20\u901a\u8fc7\u5f02\u6216\u64cd\u4f5c\u7d2f\u79ef\u5230\u4e00\u4e2a\u54c8\u5e0c\u503c\u4e2d\u3002
  • \u65cb\u8f6c\u54c8\u5e0c\uff1a\u5c06\u6bcf\u4e2a\u5b57\u7b26\u7684 ASCII \u7801\u7d2f\u79ef\u5230\u4e00\u4e2a\u54c8\u5e0c\u503c\u4e2d\uff0c\u6bcf\u6b21\u7d2f\u79ef\u4e4b\u524d\u90fd\u4f1a\u5bf9\u54c8\u5e0c\u503c\u8fdb\u884c\u65cb\u8f6c\u64cd\u4f5c\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig simple_hash.py
def add_hash(key: str) -> int:\n\"\"\"\u52a0\u6cd5\u54c8\u5e0c\"\"\"\nhash = 0\nmodulus = 1000000007\nfor c in key:\nhash += ord(c)\nreturn hash % modulus\ndef mul_hash(key: str) -> int:\n\"\"\"\u4e58\u6cd5\u54c8\u5e0c\"\"\"\nhash = 0\nmodulus = 1000000007\nfor c in key:\nhash = 31 * hash + ord(c)\nreturn hash % modulus\ndef xor_hash(key: str) -> int:\n\"\"\"\u5f02\u6216\u54c8\u5e0c\"\"\"\nhash = 0\nmodulus = 1000000007\nfor c in key:\nhash ^= ord(c)\nreturn hash % modulus\ndef rot_hash(key: str) -> int:\n\"\"\"\u65cb\u8f6c\u54c8\u5e0c\"\"\"\nhash = 0\nmodulus = 1000000007\nfor c in key:\nhash = (hash << 4) ^ (hash >> 28) ^ ord(c)\nreturn hash % modulus\n
simple_hash.cpp
/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(string key) {\nlong long hash = 0;\nconst int MODULUS = 1000000007;\nfor (unsigned char c : key) {\nhash = (hash + (int)c) % MODULUS;\n}\nreturn (int)hash;\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(string key) {\nlong long hash = 0;\nconst int MODULUS = 1000000007;\nfor (unsigned char c : key) {\nhash = (31 * hash + (int)c) % MODULUS;\n}\nreturn (int)hash;\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(string key) {\nint hash = 0;\nconst int MODULUS = 1000000007;\nfor (unsigned char c : key) {\nhash ^= (int)c;\n}\nreturn hash & MODULUS;\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(string key) {\nlong long hash = 0;\nconst int MODULUS = 1000000007;\nfor (unsigned char c : key) {\nhash = ((hash << 4) ^ (hash >> 28) ^ (int)c) % MODULUS;\n}\nreturn (int)hash;\n}\n
simple_hash.java
/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(String key) {\nlong hash = 0;\nfinal int MODULUS = 1000000007;\nfor (char c : key.toCharArray()) {\nhash = (hash + (int) c) % MODULUS;\n}\nreturn (int) hash;\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(String key) {\nlong hash = 0;\nfinal int MODULUS = 1000000007;\nfor (char c : key.toCharArray()) {\nhash = (31 * hash + (int) c) % MODULUS;\n}\nreturn (int) hash;\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(String key) {\nint hash = 0;\nfinal int MODULUS = 1000000007;\nfor (char c : key.toCharArray()) {\nhash ^= (int) c;\n}\nreturn hash & MODULUS;\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(String key) {\nlong hash = 0;\nfinal int MODULUS = 1000000007;\nfor (char c : key.toCharArray()) {\nhash = ((hash << 4) ^ (hash >> 28) ^ (int) c) % MODULUS;\n}\nreturn (int) hash;\n}\n
simple_hash.cs
/* \u52a0\u6cd5\u54c8\u5e0c */\nint AddHash(string key) {\nlong hash = 0;\nconst int MODULUS = 1000000007;\nforeach (char c in key) {\nhash = (hash + c) % MODULUS;\n}\nreturn (int)hash;\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint MulHash(string key) {\nlong hash = 0;\nconst int MODULUS = 1000000007;\nforeach (char c in key) {\nhash = (31 * hash + c) % MODULUS;\n}\nreturn (int)hash;\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nint XorHash(string key) {\nint hash = 0;\nconst int MODULUS = 1000000007;\nforeach (char c in key) {\nhash ^= c;\n}\nreturn hash & MODULUS;\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint RotHash(string key) {\nlong hash = 0;\nconst int MODULUS = 1000000007;\nforeach (char c in key) {\nhash = ((hash << 4) ^ (hash >> 28) ^ c) % MODULUS;\n}\nreturn (int)hash;\n}\n
simple_hash.go
/* \u52a0\u6cd5\u54c8\u5e0c */\nfunc addHash(key string) int {\nvar hash int64\nvar modulus int64\nmodulus = 1000000007\nfor _, b := range []byte(key) {\nhash = (hash + int64(b)) % modulus\n}\nreturn int(hash)\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunc mulHash(key string) int {\nvar hash int64\nvar modulus int64\nmodulus = 1000000007\nfor _, b := range []byte(key) {\nhash = (31*hash + int64(b)) % modulus\n}\nreturn int(hash)\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nfunc xorHash(key string) int {\nhash := 0\nmodulus := 1000000007\nfor _, b := range []byte(key) {\nfmt.Println(int(b))\nhash ^= int(b)\nhash = (31*hash + int(b)) % modulus\n}\nreturn hash & modulus\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunc rotHash(key string) int {\nvar hash int64\nvar modulus int64\nmodulus = 1000000007\nfor _, b := range []byte(key) {\nhash = ((hash << 4) ^ (hash >> 28) ^ int64(b)) % modulus\n}\nreturn int(hash)\n}\n
simple_hash.swift
/* \u52a0\u6cd5\u54c8\u5e0c */\nfunc addHash(key: String) -> Int {\nvar hash = 0\nlet MODULUS = 1_000_000_007\nfor c in key {\nfor scalar in c.unicodeScalars {\nhash = (hash + Int(scalar.value)) % MODULUS\n}\n}\nreturn hash\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunc mulHash(key: String) -> Int {\nvar hash = 0\nlet MODULUS = 1_000_000_007\nfor c in key {\nfor scalar in c.unicodeScalars {\nhash = (31 * hash + Int(scalar.value)) % MODULUS\n}\n}\nreturn hash\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nfunc xorHash(key: String) -> Int {\nvar hash = 0\nlet MODULUS = 1_000_000_007\nfor c in key {\nfor scalar in c.unicodeScalars {\nhash ^= Int(scalar.value)\n}\n}\nreturn hash & MODULUS\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunc rotHash(key: String) -> Int {\nvar hash = 0\nlet MODULUS = 1_000_000_007\nfor c in key {\nfor scalar in c.unicodeScalars {\nhash = ((hash << 4) ^ (hash >> 28) ^ Int(scalar.value)) % MODULUS\n}\n}\nreturn hash\n}\n
simple_hash.js
/* \u52a0\u6cd5\u54c8\u5e0c */\nfunction addHash(key) {\nlet hash = 0;\nconst MODULUS = 1000000007;\nfor (const c of key) {\nhash = (hash + c.charCodeAt(0)) % MODULUS;\n}\nreturn hash;\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunction mulHash(key) {\nlet hash = 0;\nconst MODULUS = 1000000007;\nfor (const c of key) {\nhash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n}\nreturn hash;\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nfunction xorHash(key) {\nlet hash = 0;\nconst MODULUS = 1000000007;\nfor (const c of key) {\nhash ^= c.charCodeAt(0);\n}\nreturn hash & MODULUS;\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunction rotHash(key) {\nlet hash = 0;\nconst MODULUS = 1000000007;\nfor (const c of key) {\nhash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;\n}\nreturn hash;\n}\n
simple_hash.ts
/* \u52a0\u6cd5\u54c8\u5e0c */\nfunction addHash(key: string): number {\nlet hash = 0;\nconst MODULUS = 1000000007;\nfor (const c of key) {\nhash = (hash + c.charCodeAt(0)) % MODULUS;\n}\nreturn hash;\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunction mulHash(key: string): number {\nlet hash = 0;\nconst MODULUS = 1000000007;\nfor (const c of key) {\nhash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n}\nreturn hash;\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nfunction xorHash(key: string): number {\nlet hash = 0;\nconst MODULUS = 1000000007;\nfor (const c of key) {\nhash ^= c.charCodeAt(0);\n}\nreturn hash & MODULUS;\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunction rotHash(key: string): number {\nlet hash = 0;\nconst MODULUS = 1000000007;\nfor (const c of key) {\nhash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;\n}\nreturn hash;\n}\n
simple_hash.dart
/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(String key) {\nint hash = 0;\nfinal int MODULUS = 1000000007;\nfor (int i = 0; i < key.length; i++) {\nhash = (hash + key.codeUnitAt(i)) % MODULUS;\n}\nreturn hash;\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(String key) {\nint hash = 0;\nfinal int MODULUS = 1000000007;\nfor (int i = 0; i < key.length; i++) {\nhash = (31 * hash + key.codeUnitAt(i)) % MODULUS;\n}\nreturn hash;\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(String key) {\nint hash = 0;\nfinal int MODULUS = 1000000007;\nfor (int i = 0; i < key.length; i++) {\nhash ^= key.codeUnitAt(i);\n}\nreturn hash & MODULUS;\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(String key) {\nint hash = 0;\nfinal int MODULUS = 1000000007;\nfor (int i = 0; i < key.length; i++) {\nhash = ((hash << 4) ^ (hash >> 28) ^ key.codeUnitAt(i)) % MODULUS;\n}\nreturn hash;\n}\n
simple_hash.rs
/* \u52a0\u6cd5\u54c8\u5e0c */\nfn add_hash(key: &str) -> i32 {\nlet mut hash = 0_i64;\nconst MODULUS: i64 = 1000000007;\nfor c in key.chars() {\nhash = (hash + c as i64) % MODULUS;\n}\nhash as i32\n} /* \u4e58\u6cd5\u54c8\u5e0c */\nfn mul_hash(key: &str) -> i32 {\nlet mut hash = 0_i64;\nconst MODULUS: i64 = 1000000007;\nfor c in key.chars() {\nhash = (31 * hash + c as i64) % MODULUS;\n}\nhash as i32\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nfn xor_hash(key: &str) -> i32 {\nlet mut hash = 0_i64;\nconst MODULUS: i64 = 1000000007;\nfor c in key.chars() {\nhash ^= c as i64;\n}\n(hash & MODULUS) as i32\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfn rot_hash(key: &str) -> i32 {\nlet mut hash = 0_i64;\nconst MODULUS: i64 = 1000000007;\nfor c in key.chars() {\nhash = ((hash << 4) ^ (hash >> 28) ^ c as i64) % MODULUS;\n}\nhash as i32\n}\n
simple_hash.c
/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(char *key) {\nlong long hash = 0;\nconst int MODULUS = 1000000007;\nfor (int i = 0; i < strlen(key); i++) {\nhash = (hash + (unsigned char)key[i]) % MODULUS;\n}\nreturn (int)hash;\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(char *key) {\nlong long hash = 0;\nconst int MODULUS = 1000000007;\nfor (int i = 0; i < strlen(key); i++) {\nhash = (31 * hash + (unsigned char)key[i]) % MODULUS;\n}\nreturn (int)hash;\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(char *key) {\nint hash = 0;\nconst int MODULUS = 1000000007;\nfor (int i = 0; i < strlen(key); i++) {\nhash ^= (unsigned char)key[i];\n}\nreturn hash & MODULUS;\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(char *key) {\nlong long hash = 0;\nconst int MODULUS = 1000000007;\nfor (int i = 0; i < strlen(key); i++) {\nhash = ((hash << 4) ^ (hash >> 28) ^ (unsigned char)key[i]) % MODULUS;\n}\nreturn (int)hash;\n}\n
simple_hash.zig
[class]{}-[func]{addHash}\n[class]{}-[func]{mulHash}\n[class]{}-[func]{xorHash}\n[class]{}-[func]{rotHash}\n

\u89c2\u5bdf\u53d1\u73b0\uff0c\u6bcf\u79cd\u54c8\u5e0c\u7b97\u6cd5\u7684\u6700\u540e\u4e00\u6b65\u90fd\u662f\u5bf9\u5927\u8d28\u6570 \\(1000000007\\) \u53d6\u6a21\uff0c\u4ee5\u786e\u4fdd\u54c8\u5e0c\u503c\u5728\u5408\u9002\u7684\u8303\u56f4\u5185\u3002\u503c\u5f97\u601d\u8003\u7684\u662f\uff0c\u4e3a\u4ec0\u4e48\u8981\u5f3a\u8c03\u5bf9\u8d28\u6570\u53d6\u6a21\uff0c\u6216\u8005\u8bf4\u5bf9\u5408\u6570\u53d6\u6a21\u7684\u5f0a\u7aef\u662f\u4ec0\u4e48\uff1f\u8fd9\u662f\u4e00\u4e2a\u6709\u8da3\u7684\u95ee\u9898\u3002

\u5148\u629b\u51fa\u7ed3\u8bba\uff1a\u5f53\u6211\u4eec\u4f7f\u7528\u5927\u8d28\u6570\u4f5c\u4e3a\u6a21\u6570\u65f6\uff0c\u53ef\u4ee5\u6700\u5927\u5316\u5730\u4fdd\u8bc1\u54c8\u5e0c\u503c\u7684\u5747\u5300\u5206\u5e03\u3002\u56e0\u4e3a\u8d28\u6570\u4e0d\u4f1a\u4e0e\u5176\u4ed6\u6570\u5b57\u5b58\u5728\u516c\u7ea6\u6570\uff0c\u53ef\u4ee5\u51cf\u5c11\u56e0\u53d6\u6a21\u64cd\u4f5c\u800c\u4ea7\u751f\u7684\u5468\u671f\u6027\u6a21\u5f0f\uff0c\u4ece\u800c\u907f\u514d\u54c8\u5e0c\u51b2\u7a81\u3002

\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u6211\u4eec\u9009\u62e9\u5408\u6570 \\(9\\) \u4f5c\u4e3a\u6a21\u6570\uff0c\u5b83\u53ef\u4ee5\u88ab \\(3\\) \u6574\u9664\u3002\u90a3\u4e48\u6240\u6709\u53ef\u4ee5\u88ab \\(3\\) \u6574\u9664\u7684 key \u90fd\u4f1a\u88ab\u6620\u5c04\u5230 \\(0\\)\u3001\\(3\\)\u3001\\(6\\) \u8fd9\u4e09\u4e2a\u54c8\u5e0c\u503c\u3002

\\[ \\begin{aligned} \\text{modulus} & = 9 \\newline \\text{key} & = \\{ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, \\dots \\} \\newline \\text{hash} & = \\{ 0, 3, 6, 0, 3, 6, 0, 3, 6, 0, 3, 6,\\dots \\} \\end{aligned} \\]

\u5982\u679c\u8f93\u5165 key \u6070\u597d\u6ee1\u8db3\u8fd9\u79cd\u7b49\u5dee\u6570\u5217\u7684\u6570\u636e\u5206\u5e03\uff0c\u90a3\u4e48\u54c8\u5e0c\u503c\u5c31\u4f1a\u51fa\u73b0\u805a\u5806\uff0c\u4ece\u800c\u52a0\u91cd\u54c8\u5e0c\u51b2\u7a81\u3002\u73b0\u5728\uff0c\u5047\u8bbe\u5c06 modulus \u66ff\u6362\u4e3a\u8d28\u6570 \\(13\\) \uff0c\u7531\u4e8e key \u548c modulus \u4e4b\u95f4\u4e0d\u5b58\u5728\u516c\u7ea6\u6570\uff0c\u8f93\u51fa\u7684\u54c8\u5e0c\u503c\u7684\u5747\u5300\u6027\u4f1a\u660e\u663e\u63d0\u5347\u3002

\\[ \\begin{aligned} \\text{modulus} & = 13 \\newline \\text{key} & = \\{ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, \\dots \\} \\newline \\text{hash} & = \\{ 0, 3, 6, 9, 12, 2, 5, 8, 11, 1, 4, 7, \\dots \\} \\end{aligned} \\]

\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u5982\u679c\u80fd\u591f\u4fdd\u8bc1 key \u662f\u968f\u673a\u5747\u5300\u5206\u5e03\u7684\uff0c\u90a3\u4e48\u9009\u62e9\u8d28\u6570\u6216\u8005\u5408\u6570\u4f5c\u4e3a\u6a21\u6570\u90fd\u662f\u53ef\u4ee5\u7684\uff0c\u5b83\u4eec\u90fd\u80fd\u8f93\u51fa\u5747\u5300\u5206\u5e03\u7684\u54c8\u5e0c\u503c\u3002\u800c\u5f53 key \u7684\u5206\u5e03\u5b58\u5728\u67d0\u79cd\u5468\u671f\u6027\u65f6\uff0c\u5bf9\u5408\u6570\u53d6\u6a21\u66f4\u5bb9\u6613\u51fa\u73b0\u805a\u96c6\u73b0\u8c61\u3002

\u603b\u800c\u8a00\u4e4b\uff0c\u6211\u4eec\u901a\u5e38\u9009\u53d6\u8d28\u6570\u4f5c\u4e3a\u6a21\u6570\uff0c\u5e76\u4e14\u8fd9\u4e2a\u8d28\u6570\u6700\u597d\u8db3\u591f\u5927\uff0c\u4ee5\u5c3d\u53ef\u80fd\u6d88\u9664\u5468\u671f\u6027\u6a21\u5f0f\uff0c\u63d0\u5347\u54c8\u5e0c\u7b97\u6cd5\u7684\u7a33\u5065\u6027\u3002

"},{"location":"chapter_hashing/hash_algorithm/#633","title":"6.3.3 \u00a0 \u5e38\u89c1\u54c8\u5e0c\u7b97\u6cd5","text":"

\u4e0d\u96be\u53d1\u73b0\uff0c\u4ee5\u4e0a\u4ecb\u7ecd\u7684\u7b80\u5355\u54c8\u5e0c\u7b97\u6cd5\u90fd\u6bd4\u8f83\u201c\u8106\u5f31\u201d\uff0c\u8fdc\u8fdc\u6ca1\u6709\u8fbe\u5230\u54c8\u5e0c\u7b97\u6cd5\u7684\u8bbe\u8ba1\u76ee\u6807\u3002\u4f8b\u5982\uff0c\u7531\u4e8e\u52a0\u6cd5\u548c\u5f02\u6216\u6ee1\u8db3\u4ea4\u6362\u5f8b\uff0c\u56e0\u6b64\u52a0\u6cd5\u54c8\u5e0c\u548c\u5f02\u6216\u54c8\u5e0c\u65e0\u6cd5\u533a\u5206\u5185\u5bb9\u76f8\u540c\u4f46\u987a\u5e8f\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\uff0c\u8fd9\u53ef\u80fd\u4f1a\u52a0\u5267\u54c8\u5e0c\u51b2\u7a81\uff0c\u5e76\u5f15\u8d77\u4e00\u4e9b\u5b89\u5168\u95ee\u9898\u3002

\u5728\u5b9e\u9645\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u7528\u4e00\u4e9b\u6807\u51c6\u54c8\u5e0c\u7b97\u6cd5\uff0c\u4f8b\u5982 MD5\u3001SHA-1\u3001SHA-2\u3001SHA3 \u7b49\u3002\u5b83\u4eec\u53ef\u4ee5\u5c06\u4efb\u610f\u957f\u5ea6\u7684\u8f93\u5165\u6570\u636e\u6620\u5c04\u5230\u6052\u5b9a\u957f\u5ea6\u7684\u54c8\u5e0c\u503c\u3002

\u8fd1\u4e00\u4e2a\u4e16\u7eaa\u4ee5\u6765\uff0c\u54c8\u5e0c\u7b97\u6cd5\u5904\u5728\u4e0d\u65ad\u5347\u7ea7\u4e0e\u4f18\u5316\u7684\u8fc7\u7a0b\u4e2d\u3002\u4e00\u90e8\u5206\u7814\u7a76\u4eba\u5458\u52aa\u529b\u63d0\u5347\u54c8\u5e0c\u7b97\u6cd5\u7684\u6027\u80fd\uff0c\u53e6\u4e00\u90e8\u5206\u7814\u7a76\u4eba\u5458\u548c\u9ed1\u5ba2\u5219\u81f4\u529b\u4e8e\u5bfb\u627e\u54c8\u5e0c\u7b97\u6cd5\u7684\u5b89\u5168\u6027\u95ee\u9898\u3002\u8868 6-2 \u5c55\u793a\u4e86\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u5e38\u89c1\u7684\u54c8\u5e0c\u7b97\u6cd5\u3002

  • MD5 \u548c SHA-1 \u5df2\u591a\u6b21\u88ab\u6210\u529f\u653b\u51fb\uff0c\u56e0\u6b64\u5b83\u4eec\u88ab\u5404\u7c7b\u5b89\u5168\u5e94\u7528\u5f03\u7528\u3002
  • SHA-2 \u7cfb\u5217\u4e2d\u7684 SHA-256 \u662f\u6700\u5b89\u5168\u7684\u54c8\u5e0c\u7b97\u6cd5\u4e4b\u4e00\uff0c\u4ecd\u672a\u51fa\u73b0\u6210\u529f\u7684\u653b\u51fb\u6848\u4f8b\uff0c\u56e0\u6b64\u5e38\u88ab\u7528\u5728\u5404\u7c7b\u5b89\u5168\u5e94\u7528\u4e0e\u534f\u8bae\u4e2d\u3002
  • SHA-3 \u76f8\u8f83 SHA-2 \u7684\u5b9e\u73b0\u5f00\u9500\u66f4\u4f4e\u3001\u8ba1\u7b97\u6548\u7387\u66f4\u9ad8\uff0c\u4f46\u76ee\u524d\u4f7f\u7528\u8986\u76d6\u5ea6\u4e0d\u5982 SHA-2 \u7cfb\u5217\u3002

\u8868 6-2 \u00a0 \u5e38\u89c1\u7684\u54c8\u5e0c\u7b97\u6cd5

MD5 SHA-1 SHA-2 SHA-3 \u63a8\u51fa\u65f6\u95f4 1992 1995 2002 2008 \u8f93\u51fa\u957f\u5ea6 128 bits 160 bits 256 / 512 bits 224/256/384/512 bits \u54c8\u5e0c\u51b2\u7a81 \u8f83\u591a \u8f83\u591a \u5f88\u5c11 \u5f88\u5c11 \u5b89\u5168\u7b49\u7ea7 \u4f4e\uff0c\u5df2\u88ab\u6210\u529f\u653b\u51fb \u4f4e\uff0c\u5df2\u88ab\u6210\u529f\u653b\u51fb \u9ad8 \u9ad8 \u5e94\u7528 \u5df2\u88ab\u5f03\u7528\uff0c\u4ecd\u7528\u4e8e\u6570\u636e\u5b8c\u6574\u6027\u68c0\u67e5 \u5df2\u88ab\u5f03\u7528 \u52a0\u5bc6\u8d27\u5e01\u4ea4\u6613\u9a8c\u8bc1\u3001\u6570\u5b57\u7b7e\u540d\u7b49 \u53ef\u7528\u4e8e\u66ff\u4ee3 SHA-2"},{"location":"chapter_hashing/hash_algorithm/#634","title":"6.3.4 \u00a0 \u6570\u636e\u7ed3\u6784\u7684\u54c8\u5e0c\u503c","text":"

\u6211\u4eec\u77e5\u9053\uff0c\u54c8\u5e0c\u8868\u7684 key \u53ef\u4ee5\u662f\u6574\u6570\u3001\u5c0f\u6570\u6216\u5b57\u7b26\u4e32\u7b49\u6570\u636e\u7c7b\u578b\u3002\u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u4f1a\u4e3a\u8fd9\u4e9b\u6570\u636e\u7c7b\u578b\u63d0\u4f9b\u5185\u7f6e\u7684\u54c8\u5e0c\u7b97\u6cd5\uff0c\u7528\u4e8e\u8ba1\u7b97\u54c8\u5e0c\u8868\u4e2d\u7684\u6876\u7d22\u5f15\u3002\u4ee5 Python \u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528 hash() \u51fd\u6570\u6765\u8ba1\u7b97\u5404\u79cd\u6570\u636e\u7c7b\u578b\u7684\u54c8\u5e0c\u503c\u3002

  • \u6574\u6570\u548c\u5e03\u5c14\u91cf\u7684\u54c8\u5e0c\u503c\u5c31\u662f\u5176\u672c\u8eab\u3002
  • \u6d6e\u70b9\u6570\u548c\u5b57\u7b26\u4e32\u7684\u54c8\u5e0c\u503c\u8ba1\u7b97\u8f83\u4e3a\u590d\u6742\uff0c\u6709\u5174\u8da3\u7684\u540c\u5b66\u8bf7\u81ea\u884c\u5b66\u4e60\u3002
  • \u5143\u7ec4\u7684\u54c8\u5e0c\u503c\u662f\u5bf9\u5176\u4e2d\u6bcf\u4e00\u4e2a\u5143\u7d20\u8fdb\u884c\u54c8\u5e0c\uff0c\u7136\u540e\u5c06\u8fd9\u4e9b\u54c8\u5e0c\u503c\u7ec4\u5408\u8d77\u6765\uff0c\u5f97\u5230\u5355\u4e00\u7684\u54c8\u5e0c\u503c\u3002
  • \u5bf9\u8c61\u7684\u54c8\u5e0c\u503c\u57fa\u4e8e\u5176\u5185\u5b58\u5730\u5740\u751f\u6210\u3002\u901a\u8fc7\u91cd\u5199\u5bf9\u8c61\u7684\u54c8\u5e0c\u65b9\u6cd5\uff0c\u53ef\u5b9e\u73b0\u57fa\u4e8e\u5185\u5bb9\u751f\u6210\u54c8\u5e0c\u503c\u3002

Tip

\u8bf7\u6ce8\u610f\uff0c\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u7684\u5185\u7f6e\u54c8\u5e0c\u503c\u8ba1\u7b97\u51fd\u6570\u7684\u5b9a\u4e49\u548c\u65b9\u6cd5\u4e0d\u540c\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig built_in_hash.py
num = 3\nhash_num = hash(num)\n# \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3\nbol = True\nhash_bol = hash(bol)\n# \u5e03\u5c14\u91cf True \u7684\u54c8\u5e0c\u503c\u4e3a 1\ndec = 3.14159\nhash_dec = hash(dec)\n# \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 326484311674566659\nstr = \"Hello \u7b97\u6cd5\"\nhash_str = hash(str)\n# \u5b57\u7b26\u4e32 Hello \u7b97\u6cd5 \u7684\u54c8\u5e0c\u503c\u4e3a 4617003410720528961\ntup = (12836, \"\u5c0f\u54c8\")\nhash_tup = hash(tup)\n# \u5143\u7ec4 (12836, '\u5c0f\u54c8') \u7684\u54c8\u5e0c\u503c\u4e3a 1029005403108185979\nobj = ListNode(0)\nhash_obj = hash(obj)\n# \u8282\u70b9\u5bf9\u8c61 <ListNode object at 0x1058fd810> \u7684\u54c8\u5e0c\u503c\u4e3a 274267521\n
built_in_hash.cpp
int num = 3;\nsize_t hashNum = hash<int>()(num);\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3\nbool bol = true;\nsize_t hashBol = hash<bool>()(bol);\n// \u5e03\u5c14\u91cf 1 \u7684\u54c8\u5e0c\u503c\u4e3a 1\ndouble dec = 3.14159;\nsize_t hashDec = hash<double>()(dec);\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 4614256650576692846\nstring str = \"Hello \u7b97\u6cd5\";\nsize_t hashStr = hash<string>()(str);\n// \u5b57\u7b26\u4e32 Hello \u7b97\u6cd5 \u7684\u54c8\u5e0c\u503c\u4e3a 15466937326284535026\n// \u5728 C++ \u4e2d\uff0c\u5185\u7f6e std:hash() \u4ec5\u63d0\u4f9b\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u54c8\u5e0c\u503c\u8ba1\u7b97\n// \u6570\u7ec4\u3001\u5bf9\u8c61\u7684\u54c8\u5e0c\u503c\u8ba1\u7b97\u9700\u8981\u81ea\u884c\u5b9e\u73b0\n
built_in_hash.java
int num = 3;\nint hashNum = Integer.hashCode(num);\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3\nboolean bol = true;\nint hashBol = Boolean.hashCode(bol);\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a 1231\ndouble dec = 3.14159;\nint hashDec = Double.hashCode(dec);\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a -1340954729\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode();\n// \u5b57\u7b26\u4e32 Hello \u7b97\u6cd5 \u7684\u54c8\u5e0c\u503c\u4e3a -727081396\nObject[] arr = { 12836, \"\u5c0f\u54c8\" };\nint hashTup = Arrays.hashCode(arr);\n// \u6570\u7ec4 [12836, \u5c0f\u54c8] \u7684\u54c8\u5e0c\u503c\u4e3a 1151158\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode();\n// \u8282\u70b9\u5bf9\u8c61 utils.ListNode@7dc5e7b4 \u7684\u54c8\u5e0c\u503c\u4e3a 2110121908\n
built_in_hash.cs
int num = 3;\nint hashNum = num.GetHashCode();\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3;\nbool bol = true;\nint hashBol = bol.GetHashCode();\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a 1;\ndouble dec = 3.14159;\nint hashDec = dec.GetHashCode();\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a -1340954729;\nstring str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.GetHashCode();\n// \u5b57\u7b26\u4e32 Hello \u7b97\u6cd5 \u7684\u54c8\u5e0c\u503c\u4e3a -586107568;\nobject[] arr = { 12836, \"\u5c0f\u54c8\" };\nint hashTup = arr.GetHashCode();\n// \u6570\u7ec4 [12836, \u5c0f\u54c8] \u7684\u54c8\u5e0c\u503c\u4e3a 42931033;\nListNode obj = new(0);\nint hashObj = obj.GetHashCode();\n// \u8282\u70b9\u5bf9\u8c61 0 \u7684\u54c8\u5e0c\u503c\u4e3a 39053774;\n
built_in_hash.go
\n
built_in_hash.swift
let num = 3\nlet hashNum = num.hashValue\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 9047044699613009734\nlet bol = true\nlet hashBol = bol.hashValue\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a -4431640247352757451\nlet dec = 3.14159\nlet hashDec = dec.hashValue\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a -2465384235396674631\nlet str = \"Hello \u7b97\u6cd5\"\nlet hashStr = str.hashValue\n// \u5b57\u7b26\u4e32 Hello \u7b97\u6cd5 \u7684\u54c8\u5e0c\u503c\u4e3a -7850626797806988787\nlet arr = [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")]\nlet hashTup = arr.hashValue\n// \u6570\u7ec4 [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")] \u7684\u54c8\u5e0c\u503c\u4e3a -2308633508154532996\nlet obj = ListNode(x: 0)\nlet hashObj = obj.hashValue\n// \u8282\u70b9\u5bf9\u8c61 utils.ListNode \u7684\u54c8\u5e0c\u503c\u4e3a -2434780518035996159\n
built_in_hash.js
// JavaScript \u672a\u63d0\u4f9b\u5185\u7f6e hash code \u51fd\u6570\n
built_in_hash.ts
// TypeScript \u672a\u63d0\u4f9b\u5185\u7f6e hash code \u51fd\u6570\n
built_in_hash.dart
int num = 3;\nint hashNum = num.hashCode;\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 34803\nbool bol = true;\nint hashBol = bol.hashCode;\n// \u5e03\u5c14\u503c true \u7684\u54c8\u5e0c\u503c\u4e3a 1231\ndouble dec = 3.14159;\nint hashDec = dec.hashCode;\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 2570631074981783\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode;\n// \u5b57\u7b26\u4e32 Hello \u7b97\u6cd5 \u7684\u54c8\u5e0c\u503c\u4e3a 468167534\nList arr = [12836, \"\u5c0f\u54c8\"];\nint hashArr = arr.hashCode;\n// \u6570\u7ec4 [12836, \u5c0f\u54c8] \u7684\u54c8\u5e0c\u503c\u4e3a 976512528\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode;\n// \u8282\u70b9\u5bf9\u8c61 Instance of 'ListNode' \u7684\u54c8\u5e0c\u503c\u4e3a 1033450432\n
built_in_hash.rs
use std::collections::hash_map::DefaultHasher;\nuse std::hash::{Hash, Hasher};\nlet num = 3;\nlet mut num_hasher = DefaultHasher::new();\nnum.hash(&mut num_hasher);\nlet hash_num = num_hasher.finish();\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 568126464209439262\nlet bol = true;\nlet mut bol_hasher = DefaultHasher::new();\nbol.hash(&mut bol_hasher);\nlet hash_bol = bol_hasher.finish();\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a 4952851536318644461\nlet dec: f32 = 3.14159;\nlet mut dec_hasher = DefaultHasher::new();\ndec.to_bits().hash(&mut dec_hasher);\nlet hash_dec = dec_hasher.finish();\nprintln!(\"\u5c0f\u6570 {} \u7684\u54c8\u5e0c\u503c\u4e3a {}\", dec, hash_dec);\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 2566941990314602357\nlet str = \"Hello \u7b97\u6cd5\";\nlet mut str_hasher = DefaultHasher::new();\nstr.hash(&mut str_hasher);\nlet hash_str = str_hasher.finish();\n// \u5b57\u7b26\u4e32 Hello \u7b97\u6cd5 \u7684\u54c8\u5e0c\u503c\u4e3a 16092673739211250988\nlet arr = (&12836, &\"\u5c0f\u54c8\");\nlet mut tup_hasher = DefaultHasher::new();\narr.hash(&mut tup_hasher);\nlet hash_tup = tup_hasher.finish();\n// \u5143\u7ec4 (12836, \"\u5c0f\u54c8\") \u7684\u54c8\u5e0c\u503c\u4e3a 1885128010422702749\nlet node = ListNode::new(42);\nlet mut hasher = DefaultHasher::new();\nnode.borrow().val.hash(&mut hasher);\nlet hash = hasher.finish();\n// \u8282\u70b9\u5bf9\u8c61 RefCell { value: ListNode { val: 42, next: None } } \u7684\u54c8\u5e0c\u503c\u4e3a15387811073369036852\n
built_in_hash.c
// C \u672a\u63d0\u4f9b\u5185\u7f6e hash code \u51fd\u6570\n
built_in_hash.zig
\n

\u5728\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u53ea\u6709\u4e0d\u53ef\u53d8\u5bf9\u8c61\u624d\u53ef\u4f5c\u4e3a\u54c8\u5e0c\u8868\u7684 key \u3002\u5047\u5982\u6211\u4eec\u5c06\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\u4f5c\u4e3a key \uff0c\u5f53\u5217\u8868\u7684\u5185\u5bb9\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u5b83\u7684\u54c8\u5e0c\u503c\u4e5f\u968f\u4e4b\u6539\u53d8\uff0c\u6211\u4eec\u5c31\u65e0\u6cd5\u5728\u54c8\u5e0c\u8868\u4e2d\u67e5\u8be2\u5230\u539f\u5148\u7684 value \u4e86\u3002

\u867d\u7136\u81ea\u5b9a\u4e49\u5bf9\u8c61\uff08\u6bd4\u5982\u94fe\u8868\u8282\u70b9\uff09\u7684\u6210\u5458\u53d8\u91cf\u662f\u53ef\u53d8\u7684\uff0c\u4f46\u5b83\u662f\u53ef\u54c8\u5e0c\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u5bf9\u8c61\u7684\u54c8\u5e0c\u503c\u901a\u5e38\u662f\u57fa\u4e8e\u5185\u5b58\u5730\u5740\u751f\u6210\u7684\uff0c\u5373\u4f7f\u5bf9\u8c61\u7684\u5185\u5bb9\u53d1\u751f\u4e86\u53d8\u5316\uff0c\u4f46\u5b83\u7684\u5185\u5b58\u5730\u5740\u4e0d\u53d8\uff0c\u54c8\u5e0c\u503c\u4ecd\u7136\u662f\u4e0d\u53d8\u7684\u3002

\u7ec6\u5fc3\u7684\u4f60\u53ef\u80fd\u53d1\u73b0\u5728\u4e0d\u540c\u63a7\u5236\u53f0\u4e2d\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u8f93\u51fa\u7684\u54c8\u5e0c\u503c\u662f\u4e0d\u540c\u7684\u3002\u8fd9\u662f\u56e0\u4e3a Python \u89e3\u91ca\u5668\u5728\u6bcf\u6b21\u542f\u52a8\u65f6\uff0c\u90fd\u4f1a\u4e3a\u5b57\u7b26\u4e32\u54c8\u5e0c\u51fd\u6570\u52a0\u5165\u4e00\u4e2a\u968f\u673a\u7684\u76d0\uff08Salt\uff09\u503c\u3002\u8fd9\u79cd\u505a\u6cd5\u53ef\u4ee5\u6709\u6548\u9632\u6b62 HashDoS \u653b\u51fb\uff0c\u63d0\u5347\u54c8\u5e0c\u7b97\u6cd5\u7684\u5b89\u5168\u6027\u3002

"},{"location":"chapter_hashing/hash_collision/","title":"6.2 \u00a0 \u54c8\u5e0c\u51b2\u7a81","text":"

\u4e0a\u8282\u63d0\u5230\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u54c8\u5e0c\u51fd\u6570\u7684\u8f93\u5165\u7a7a\u95f4\u8fdc\u5927\u4e8e\u8f93\u51fa\u7a7a\u95f4\uff0c\u56e0\u6b64\u7406\u8bba\u4e0a\u54c8\u5e0c\u51b2\u7a81\u662f\u4e0d\u53ef\u907f\u514d\u7684\u3002\u6bd4\u5982\uff0c\u8f93\u5165\u7a7a\u95f4\u4e3a\u5168\u4f53\u6574\u6570\uff0c\u8f93\u51fa\u7a7a\u95f4\u4e3a\u6570\u7ec4\u5bb9\u91cf\u5927\u5c0f\uff0c\u5219\u5fc5\u7136\u6709\u591a\u4e2a\u6574\u6570\u6620\u5c04\u81f3\u540c\u4e00\u6876\u7d22\u5f15\u3002

\u54c8\u5e0c\u51b2\u7a81\u4f1a\u5bfc\u81f4\u67e5\u8be2\u7ed3\u679c\u9519\u8bef\uff0c\u4e25\u91cd\u5f71\u54cd\u54c8\u5e0c\u8868\u7684\u53ef\u7528\u6027\u3002\u4e3a\u89e3\u51b3\u8be5\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u6bcf\u5f53\u9047\u5230\u54c8\u5e0c\u51b2\u7a81\u65f6\u5c31\u8fdb\u884c\u54c8\u5e0c\u8868\u6269\u5bb9\uff0c\u76f4\u81f3\u51b2\u7a81\u6d88\u5931\u4e3a\u6b62\u3002\u6b64\u65b9\u6cd5\u7b80\u5355\u7c97\u66b4\u4e14\u6709\u6548\uff0c\u4f46\u6548\u7387\u592a\u4f4e\uff0c\u56e0\u4e3a\u54c8\u5e0c\u8868\u6269\u5bb9\u9700\u8981\u8fdb\u884c\u5927\u91cf\u7684\u6570\u636e\u642c\u8fd0\u4e0e\u54c8\u5e0c\u503c\u8ba1\u7b97\u3002\u4e3a\u4e86\u63d0\u5347\u6548\u7387\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u7b56\u7565\u3002

  1. \u6539\u826f\u54c8\u5e0c\u8868\u6570\u636e\u7ed3\u6784\uff0c\u4f7f\u5f97\u54c8\u5e0c\u8868\u53ef\u4ee5\u5728\u5b58\u5728\u54c8\u5e0c\u51b2\u7a81\u65f6\u6b63\u5e38\u5de5\u4f5c\u3002
  2. \u4ec5\u5728\u5fc5\u8981\u65f6\uff0c\u5373\u5f53\u54c8\u5e0c\u51b2\u7a81\u6bd4\u8f83\u4e25\u91cd\u65f6\uff0c\u624d\u6267\u884c\u6269\u5bb9\u64cd\u4f5c\u3002

\u54c8\u5e0c\u8868\u7684\u7ed3\u6784\u6539\u826f\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u201c\u94fe\u5f0f\u5730\u5740\u201d\u548c\u201c\u5f00\u653e\u5bfb\u5740\u201d\u3002

"},{"location":"chapter_hashing/hash_collision/#621","title":"6.2.1 \u00a0 \u94fe\u5f0f\u5730\u5740","text":"

\u5728\u539f\u59cb\u54c8\u5e0c\u8868\u4e2d\uff0c\u6bcf\u4e2a\u6876\u4ec5\u80fd\u5b58\u50a8\u4e00\u4e2a\u952e\u503c\u5bf9\u3002\u300c\u94fe\u5f0f\u5730\u5740 separate chaining\u300d\u5c06\u5355\u4e2a\u5143\u7d20\u8f6c\u6362\u4e3a\u94fe\u8868\uff0c\u5c06\u952e\u503c\u5bf9\u4f5c\u4e3a\u94fe\u8868\u8282\u70b9\uff0c\u5c06\u6240\u6709\u53d1\u751f\u51b2\u7a81\u7684\u952e\u503c\u5bf9\u90fd\u5b58\u50a8\u5728\u540c\u4e00\u94fe\u8868\u4e2d\u3002\u56fe 6-5 \u5c55\u793a\u4e86\u4e00\u4e2a\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\u7684\u4f8b\u5b50\u3002

\u56fe 6-5 \u00a0 \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868

\u57fa\u4e8e\u94fe\u5f0f\u5730\u5740\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868\u7684\u64cd\u4f5c\u65b9\u6cd5\u53d1\u751f\u4e86\u4ee5\u4e0b\u53d8\u5316\u3002

  • \u67e5\u8be2\u5143\u7d20\uff1a\u8f93\u5165 key \uff0c\u7ecf\u8fc7\u54c8\u5e0c\u51fd\u6570\u5f97\u5230\u6876\u7d22\u5f15\uff0c\u5373\u53ef\u8bbf\u95ee\u94fe\u8868\u5934\u8282\u70b9\uff0c\u7136\u540e\u904d\u5386\u94fe\u8868\u5e76\u5bf9\u6bd4 key \u4ee5\u67e5\u627e\u76ee\u6807\u952e\u503c\u5bf9\u3002
  • \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

\u94fe\u5f0f\u5730\u5740\u5b58\u5728\u4ee5\u4e0b\u5c40\u9650\u6027\u3002

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

\u4ee5\u4e0b\u4ee3\u7801\u7ed9\u51fa\u4e86\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\u7684\u7b80\u5355\u5b9e\u73b0\uff0c\u9700\u8981\u6ce8\u610f\u4e24\u70b9\u3002

  • \u4f7f\u7528\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\u4ee3\u66ff\u94fe\u8868\uff0c\u4ece\u800c\u7b80\u5316\u4ee3\u7801\u3002\u5728\u8fd9\u79cd\u8bbe\u5b9a\u4e0b\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\u5b9e\u73b0\u5305\u542b\u54c8\u5e0c\u8868\u6269\u5bb9\u65b9\u6cd5\u3002\u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7 \\(\\frac{2}{3}\\) \u65f6\uff0c\u6211\u4eec\u5c06\u54c8\u5e0c\u8868\u6269\u5bb9\u81f3 \\(2\\) \u500d\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig hash_map_chaining.py
class 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.0 / 3.0  # \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 | None:\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\nbreak\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.cpp
/* \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.0), extendRatio(2) {\nbuckets.resize(capacity);\n}\n/* \u6790\u6784\u65b9\u6cd5 */\n~HashMapChaining() {\nfor (auto &bucket : buckets) {\nfor (Pair *pair : bucket) {\n// \u91ca\u653e\u5185\u5b58\ndelete pair;\n}\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 / (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\u7a7a\u5b57\u7b26\u4e32\nreturn \"\";\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// \u91ca\u653e\u5185\u5b58\ndelete pair;\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.java
/* \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.0 / 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);\nsize--;\nbreak;\n}\n}\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.cs
/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\nint size; // \u952e\u503c\u5bf9\u6570\u91cf\nint capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\nreadonly double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nreadonly int 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.0 / 3.0;\nextendRatio = 2;\nbuckets = new List<List<Pair>>(capacity);\nfor (int i = 0; i < capacity; i++) {\nbuckets.Add(new List<Pair>());\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nprivate int HashFunc(int key) {\nreturn key % capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nprivate double LoadFactor() {\nreturn (double)size / capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npublic string? Get(int key) {\nint index = HashFunc(key);\n// \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nforeach (Pair pair in buckets[index]) {\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 */\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// \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nforeach (Pair pair in 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].Add(new Pair(key, val));\nsize++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\npublic void Remove(int key) {\nint index = HashFunc(key);\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nforeach (Pair pair in buckets[index].ToList()) {\nif (pair.key == key) {\nbuckets[index].Remove(pair);\nsize--;\nbreak;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nprivate void 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 List<List<Pair>>(capacity);\nfor (int i = 0; i < capacity; i++) {\nbuckets.Add(new List<Pair>());\n}\nsize = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nforeach (List<Pair> bucket in bucketsTmp) {\nforeach (Pair pair in bucket) {\nPut(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\npublic void Print() {\nforeach (List<Pair> bucket in buckets) {\nList<string> res = new();\nforeach (Pair pair in bucket) {\nres.Add(pair.key + \" -> \" + pair.val);\n}\nforeach (string kv in res) {\nConsole.WriteLine(kv);\n}\n}\n}\n}\n
hash_map_chaining.go
/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\ntype hashMapChaining struct {\nsize        int      // \u952e\u503c\u5bf9\u6570\u91cf\ncapacity    int      // \u54c8\u5e0c\u8868\u5bb9\u91cf\nloadThres   float64  // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nextendRatio int      // \u6269\u5bb9\u500d\u6570\nbuckets     [][]pair // \u6876\u6570\u7ec4\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newHashMapChaining() *hashMapChaining {\nbuckets := make([][]pair, 4)\nfor i := 0; i < 4; i++ {\nbuckets[i] = make([]pair, 0)\n}\nreturn &hashMapChaining{\nsize:        0,\ncapacity:    4,\nloadThres:   2.0 / 3.0,\nextendRatio: 2,\nbuckets:     buckets,\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (m *hashMapChaining) hashFunc(key int) int {\nreturn key % m.capacity\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfunc (m *hashMapChaining) loadFactor() float64 {\nreturn float64(m.size / m.capacity)\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (m *hashMapChaining) get(key int) string {\nidx := m.hashFunc(key)\nbucket := m.buckets[idx]\n// \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nfor _, p := range bucket {\nif p.key == key {\nreturn p.val\n}\n}\n// \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\nreturn \"\"\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (m *hashMapChaining) put(key int, val string) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif m.loadFactor() > m.loadThres {\nm.extend()\n}\nidx := m.hashFunc(key)\n// \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nfor _, p := range m.buckets[idx] {\nif p.key == key {\np.val = val\nreturn\n}\n}\n// \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\np := pair{\nkey: key,\nval: val,\n}\nm.buckets[idx] = append(m.buckets[idx], p)\nm.size += 1\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (m *hashMapChaining) remove(key int) {\nidx := m.hashFunc(key)\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor i, p := range m.buckets[idx] {\nif p.key == key {\n// \u5207\u7247\u5220\u9664\nm.buckets[idx] = append(m.buckets[idx][:i], m.buckets[idx][i+1:]...)\nm.size -= 1\nbreak\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfunc (m *hashMapChaining) extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\ntmpBuckets := make([][]pair, len(m.buckets))\nfor i := 0; i < len(m.buckets); i++ {\ntmpBuckets[i] = make([]pair, len(m.buckets[i]))\ncopy(tmpBuckets[i], m.buckets[i])\n}\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nm.capacity *= m.extendRatio\nm.buckets = make([][]pair, m.capacity)\nfor i := 0; i < m.capacity; i++ {\nm.buckets[i] = make([]pair, 0)\n}\nm.size = 0\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor _, bucket := range tmpBuckets {\nfor _, p := range bucket {\nm.put(p.key, p.val)\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (m *hashMapChaining) print() {\nvar builder strings.Builder\nfor _, bucket := range m.buckets {\nbuilder.WriteString(\"[\")\nfor _, p := range bucket {\nbuilder.WriteString(strconv.Itoa(p.key) + \" -> \" + p.val + \" \")\n}\nbuilder.WriteString(\"]\")\nfmt.Println(builder.String())\nbuilder.Reset()\n}\n}\n
hash_map_chaining.swift
/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\nvar size: Int // \u952e\u503c\u5bf9\u6570\u91cf\nvar capacity: Int // \u54c8\u5e0c\u8868\u5bb9\u91cf\nvar loadThres: Double // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nvar extendRatio: Int // \u6269\u5bb9\u500d\u6570\nvar buckets: [[Pair]] // \u6876\u6570\u7ec4\n/* \u6784\u9020\u65b9\u6cd5 */\ninit() {\nsize = 0\ncapacity = 4\nloadThres = 2.0 / 3.0\nextendRatio = 2\nbuckets = Array(repeating: [], count: capacity)\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc hashFunc(key: Int) -> Int {\nkey % capacity\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfunc loadFactor() -> Double {\nDouble(size / capacity)\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc get(key: Int) -> String? {\nlet index = hashFunc(key: key)\nlet bucket = 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}\n}\n// \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de nil\nreturn nil\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc put(key: Int, val: String) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif loadFactor() > loadThres {\nextend()\n}\nlet index = hashFunc(key: key)\nlet bucket = 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}\n}\n// \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\nlet pair = Pair(key: key, val: val)\nbuckets[index].append(pair)\nsize += 1\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfunc remove(key: Int) {\nlet index = hashFunc(key: key)\nlet bucket = buckets[index]\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor (pairIndex, pair) in bucket.enumerated() {\nif pair.key == key {\nbuckets[index].remove(at: pairIndex)\n}\n}\nsize -= 1\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfunc extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nlet bucketsTmp = buckets\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\ncapacity *= extendRatio\nbuckets = Array(repeating: [], count: capacity)\nsize = 0\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor bucket in bucketsTmp {\nfor pair in bucket {\nput(key: pair.key, val: pair.val)\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc print() {\nfor bucket in buckets {\nlet res = bucket.map { \"\\($0.key) -> \\($0.val)\" }\nSwift.print(res)\n}\n}\n}\n
hash_map_chaining.js
/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n#size; // \u952e\u503c\u5bf9\u6570\u91cf\n#capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n#loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n#extendRatio; // \u6269\u5bb9\u500d\u6570\n#buckets; // \u6876\u6570\u7ec4\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor() {\nthis.#size = 0;\nthis.#capacity = 4;\nthis.#loadThres = 2.0 / 3.0;\nthis.#extendRatio = 2;\nthis.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\n#hashFunc(key) {\nreturn key % this.#capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\n#loadFactor() {\nreturn this.#size / this.#capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nget(key) {\nconst index = this.#hashFunc(key);\nconst bucket = this.#buckets[index];\n// \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nfor (const pair of 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 */\nput(key, val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (this.#loadFactor() > this.#loadThres) {\nthis.#extend();\n}\nconst index = this.#hashFunc(key);\nconst bucket = this.#buckets[index];\n// \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nfor (const pair of 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\nconst pair = new Pair(key, val);\nbucket.push(pair);\nthis.#size++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nremove(key) {\nconst index = this.#hashFunc(key);\nlet bucket = this.#buckets[index];\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor (let i = 0; i < bucket.length; i++) {\nif (bucket[i].key === key) {\nbucket.splice(i, 1);\nthis.#size--;\nbreak;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n#extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nconst bucketsTmp = this.#buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nthis.#capacity *= this.#extendRatio;\nthis.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\nthis.#size = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (const bucket of bucketsTmp) {\nfor (const pair of bucket) {\nthis.put(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nprint() {\nfor (const bucket of this.#buckets) {\nlet res = [];\nfor (const pair of bucket) {\nres.push(pair.key + ' -> ' + pair.val);\n}\nconsole.log(res);\n}\n}\n}\n
hash_map_chaining.ts
/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n#size: number; // \u952e\u503c\u5bf9\u6570\u91cf\n#capacity: number; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n#loadThres: number; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n#extendRatio: number; // \u6269\u5bb9\u500d\u6570\n#buckets: Pair[][]; // \u6876\u6570\u7ec4\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor() {\nthis.#size = 0;\nthis.#capacity = 4;\nthis.#loadThres = 2.0 / 3.0;\nthis.#extendRatio = 2;\nthis.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\n#hashFunc(key: number): number {\nreturn key % this.#capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\n#loadFactor(): number {\nreturn this.#size / this.#capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nget(key: number): string | null {\nconst index = this.#hashFunc(key);\nconst bucket = this.#buckets[index];\n// \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nfor (const pair of 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 */\nput(key: number, val: string): void {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (this.#loadFactor() > this.#loadThres) {\nthis.#extend();\n}\nconst index = this.#hashFunc(key);\nconst bucket = this.#buckets[index];\n// \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nfor (const pair of 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\nconst pair = new Pair(key, val);\nbucket.push(pair);\nthis.#size++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nremove(key: number): void {\nconst index = this.#hashFunc(key);\nlet bucket = this.#buckets[index];\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor (let i = 0; i < bucket.length; i++) {\nif (bucket[i].key === key) {\nbucket.splice(i, 1);\nthis.#size--;\nbreak;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n#extend(): void {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nconst bucketsTmp = this.#buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nthis.#capacity *= this.#extendRatio;\nthis.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\nthis.#size = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (const bucket of bucketsTmp) {\nfor (const pair of bucket) {\nthis.put(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nprint(): void {\nfor (const bucket of this.#buckets) {\nlet res = [];\nfor (const pair of bucket) {\nres.push(pair.key + ' -> ' + pair.val);\n}\nconsole.log(res);\n}\n}\n}\n
hash_map_chaining.dart
/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\nlate int size; // \u952e\u503c\u5bf9\u6570\u91cf\nlate int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\nlate double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nlate int extendRatio; // \u6269\u5bb9\u500d\u6570\nlate List<List<Pair>> buckets; // \u6876\u6570\u7ec4\n/* \u6784\u9020\u65b9\u6cd5 */\nHashMapChaining() {\nsize = 0;\ncapacity = 4;\nloadThres = 2.0 / 3.0;\nextendRatio = 2;\nbuckets = List.generate(capacity, (_) => []);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(int key) {\nreturn key % capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor() {\nreturn size / capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nString? get(int key) {\nint index = hashFunc(key);\nList<Pair> bucket = buckets[index];\n// \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nfor (Pair pair in 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[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 in 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 = 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[index];\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor (Pair pair in bucket) {\nif (pair.key == key) {\nbucket.remove(pair);\nsize--;\nbreak;\n}\n}\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 = List.generate(capacity, (_) => []);\nsize = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (List<Pair> bucket in bucketsTmp) {\nfor (Pair pair in bucket) {\nput(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid printHashMap() {\nfor (List<Pair> bucket in buckets) {\nList<String> res = [];\nfor (Pair pair in bucket) {\nres.add(\"${pair.key} -> ${pair.val}\");\n}\nprint(res);\n}\n}\n}\n
hash_map_chaining.rs
/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nstruct HashMapChaining {\nsize: i32,\ncapacity: i32,\nload_thres: f32,\nextend_ratio: i32,\nbuckets: Vec<Vec<Pair>>,\n}\nimpl HashMapChaining {\n/* \u6784\u9020\u65b9\u6cd5 */\nfn new() -> Self {\nSelf {\nsize: 0,\ncapacity: 4,\nload_thres: 2.0 / 3.0,\nextend_ratio: 2,\nbuckets: vec![vec![]; 4],\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nfn hash_func(&self, key: i32) -> usize {\nkey as usize % self.capacity as usize\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfn load_factor(&self) -> f32 {\nself.size as f32 / self.capacity as f32\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfn remove(&mut self, key: i32) -> Option<String> {\nlet index = self.hash_func(key);\nlet bucket = &mut self.buckets[index];\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor i in 0..bucket.len() {\nif bucket[i].key == key {\nlet pair = bucket.remove(i);\nself.size -= 1;\nreturn Some(pair.val);\n}\n}\n// \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de None\nNone\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfn extend(&mut self) {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nlet buckets_tmp = std::mem::replace(&mut self.buckets, vec![]);\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nself.capacity *= self.extend_ratio;\nself.buckets = vec![Vec::new(); self.capacity as usize];\nself.size = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor bucket in buckets_tmp {\nfor pair in bucket {\nself.put(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfn print(&self) {\nfor bucket in &self.buckets {\nlet mut res = Vec::new();\nfor pair in bucket {\nres.push(format!(\"{} -> {}\", pair.key, pair.val));\n}\nprintln!(\"{:?}\", res);\n}\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfn put(&mut self, key: i32, val: String) {\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();\n}\nlet index = self.hash_func(key);\nlet bucket = &mut 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.clone();\nreturn;\n}\n}\nlet bucket = &mut self.buckets[index];\n// \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\nlet pair = Pair {\nkey,\nval: val.clone(),\n};\nbucket.push(pair);\nself.size += 1;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfn get(&self, key: i32) -> Option<&str> {\nlet index = self.hash_func(key);\nlet bucket = &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 Some(&pair.val);\n}\n}\n// \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de None\nNone\n}\n}\n
hash_map_chaining.c
/* \u94fe\u8868\u8282\u70b9 */\nstruct node {\nPair *pair;\nstruct Node *next;\n};\ntypedef struct node Node;\n/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nstruct 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\nNode **buckets;   // \u6876\u6570\u7ec4\n};\ntypedef struct hashMapChaining hashMapChaining;\n/* \u6784\u9020\u65b9\u6cd5 */\nhashMapChaining *initHashMapChaining() {\nhashMapChaining *hashMap = (hashMapChaining *)malloc(sizeof(hashMapChaining));\nhashMap->size = 0;\nhashMap->capacity = 4;\nhashMap->loadThres = 2.0 / 3.0;\nhashMap->extendRatio = 2;\nhashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));\nfor (int i = 0; i < hashMap->capacity; i++) {\nhashMap->buckets[i] = NULL;\n}\nreturn hashMap;\n}\n/* \u6790\u6784\u65b9\u6cd5 */\nvoid freeHashMapChaining(hashMapChaining *hashMap) {\nfor (int i = 0; i < hashMap->capacity; i++) {\nNode *cur = hashMap->buckets[i];\nwhile (cur) {\nNode *temp = cur;\ncur = cur->next;\nfree(temp->pair);\nfree(temp);\n}\n}\nfree(hashMap->buckets);\nfree(hashMap);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(hashMapChaining *hashMap, int key) {\nreturn key % hashMap->capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor(hashMapChaining *hashMap) {\nreturn (double)hashMap->size / (double)hashMap->capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nchar *get(hashMapChaining *hashMap, int key) {\nint index = hashFunc(hashMap, key);\n// \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nNode *cur = hashMap->buckets[index];\nwhile (cur) {\nif (cur->pair->key == key) {\nreturn cur->pair->val;\n}\ncur = cur->next;\n}\nreturn \"\"; // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(hashMapChaining *hashMap, int key, const char *val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (loadFactor(hashMap) > hashMap->loadThres) {\nextend(hashMap);\n}\nint index = hashFunc(hashMap, key);\n// \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nNode *cur = hashMap->buckets[index];\nwhile (cur) {\nif (cur->pair->key == key) {\nstrcpy(cur->pair->val, val); // \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nreturn;\n}\ncur = cur->next;\n}\n// \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\nPair *newPair = (Pair *)malloc(sizeof(Pair));\nnewPair->key = key;\nstrcpy(newPair->val, val);\nNode *newNode = (Node *)malloc(sizeof(Node));\nnewNode->pair = newPair;\nnewNode->next = hashMap->buckets[index];\nhashMap->buckets[index] = newNode;\nhashMap->size++;\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend(hashMapChaining *hashMap) {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nint oldCapacity = hashMap->capacity;\nNode **oldBuckets = hashMap->buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nhashMap->capacity *= hashMap->extendRatio;\nhashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));\nfor (int i = 0; i < hashMap->capacity; i++) {\nhashMap->buckets[i] = NULL;\n}\nhashMap->size = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (int i = 0; i < oldCapacity; i++) {\nNode *cur = oldBuckets[i];\nwhile (cur) {\nput(hashMap, cur->pair->key, cur->pair->val);\nNode *temp = cur;\ncur = cur->next;\n// \u91ca\u653e\u5185\u5b58\nfree(temp->pair);\nfree(temp);\n}\n}\nfree(oldBuckets);\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid removeKey(hashMapChaining *hashMap, int key) {\nint index = hashFunc(hashMap, key);\nNode *cur = hashMap->buckets[index];\nNode *pre = NULL;\nwhile (cur) {\nif (cur->pair->key == key) {\n// \u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nif (pre) {\npre->next = cur->next;\n} else {\nhashMap->buckets[index] = cur->next;\n}\n// \u91ca\u653e\u5185\u5b58\nfree(cur->pair);\nfree(cur);\nhashMap->size--;\nreturn;\n}\npre = cur;\ncur = cur->next;\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print(hashMapChaining *hashMap) {\nfor (int i = 0; i < hashMap->capacity; i++) {\nNode *cur = hashMap->buckets[i];\nprintf(\"[\");\nwhile (cur) {\nprintf(\"%d -> %s, \", cur->pair->key, cur->pair->val);\ncur = cur->next;\n}\nprintf(\"]\\n\");\n}\n}\n
hash_map_chaining.zig
[class]{HashMapChaining}-[func]{}\n

\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u94fe\u8868\u5f88\u957f\u65f6\uff0c\u67e5\u8be2\u6548\u7387 \\(O(n)\\) \u5f88\u5dee\u3002\u6b64\u65f6\u53ef\u4ee5\u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u201cAVL \u6811\u201d\u6216\u201c\u7ea2\u9ed1\u6811\u201d\uff0c\u4ece\u800c\u5c06\u67e5\u8be2\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(\\log n)\\) \u3002

"},{"location":"chapter_hashing/hash_collision/#622","title":"6.2.2 \u00a0 \u5f00\u653e\u5bfb\u5740","text":"

\u300c\u5f00\u653e\u5bfb\u5740 open addressing\u300d\u4e0d\u5f15\u5165\u989d\u5916\u7684\u6570\u636e\u7ed3\u6784\uff0c\u800c\u662f\u901a\u8fc7\u201c\u591a\u6b21\u63a2\u6d4b\u201d\u6765\u5904\u7406\u54c8\u5e0c\u51b2\u7a81\uff0c\u63a2\u6d4b\u65b9\u5f0f\u4e3b\u8981\u5305\u62ec\u7ebf\u6027\u63a2\u6d4b\u3001\u5e73\u65b9\u63a2\u6d4b\u3001\u591a\u6b21\u54c8\u5e0c\u7b49\u3002

\u4e0b\u9762\u5c06\u4e3b\u8981\u4ee5\u7ebf\u6027\u63a2\u6d4b\u4e3a\u4f8b\uff0c\u4ecb\u7ecd\u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868\u7684\u5de5\u4f5c\u673a\u5236\u4e0e\u4ee3\u7801\u5b9e\u73b0\u3002

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

\u7ebf\u6027\u63a2\u6d4b\u91c7\u7528\u56fa\u5b9a\u6b65\u957f\u7684\u7ebf\u6027\u641c\u7d22\u6765\u8fdb\u884c\u63a2\u6d4b\uff0c\u5176\u64cd\u4f5c\u65b9\u6cd5\u4e0e\u666e\u901a\u54c8\u5e0c\u8868\u6709\u6240\u4e0d\u540c\u3002

  • \u63d2\u5165\u5143\u7d20\uff1a\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u8ba1\u7b97\u6876\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\u6876\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\u5982\u679c\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u76ee\u6807\u5143\u7d20\u4e0d\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u8fd4\u56de \\(\\text{None}\\) \u3002

\u56fe 6-6 \u5c55\u793a\u4e86\u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\uff09\u54c8\u5e0c\u8868\u7684\u952e\u503c\u5bf9\u5206\u5e03\u3002\u6839\u636e\u6b64\u54c8\u5e0c\u51fd\u6570\uff0c\u6700\u540e\u4e24\u4f4d\u76f8\u540c\u7684 key \u90fd\u4f1a\u88ab\u6620\u5c04\u5230\u76f8\u540c\u7684\u6876\u3002\u800c\u901a\u8fc7\u7ebf\u6027\u63a2\u6d4b\uff0c\u5b83\u4eec\u88ab\u4f9d\u6b21\u5b58\u50a8\u5728\u8be5\u6876\u4ee5\u53ca\u4e4b\u4e0b\u7684\u6876\u4e2d\u3002

\u56fe 6-6 \u00a0 \u5f00\u653e\u5bfb\u5740\u548c\u7ebf\u6027\u63a2\u6d4b

\u7136\u800c\uff0c\u7ebf\u6027\u63a2\u6d4b\u5bb9\u6613\u4ea7\u751f\u201c\u805a\u96c6\u73b0\u8c61\u201d\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u6570\u7ec4\u4e2d\u8fde\u7eed\u88ab\u5360\u7528\u7684\u4f4d\u7f6e\u8d8a\u957f\uff0c\u8fd9\u4e9b\u8fde\u7eed\u4f4d\u7f6e\u53d1\u751f\u54c8\u5e0c\u51b2\u7a81\u7684\u53ef\u80fd\u6027\u8d8a\u5927\uff0c\u4ece\u800c\u8fdb\u4e00\u6b65\u4fc3\u4f7f\u8be5\u4f4d\u7f6e\u7684\u805a\u5806\u751f\u957f\uff0c\u5f62\u6210\u6076\u6027\u5faa\u73af\uff0c\u6700\u7ec8\u5bfc\u81f4\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u6548\u7387\u52a3\u5316\u3002

\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6211\u4eec\u4e0d\u80fd\u5728\u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868\u4e2d\u76f4\u63a5\u5220\u9664\u5143\u7d20\u3002\u8fd9\u662f\u56e0\u4e3a\u5220\u9664\u5143\u7d20\u4f1a\u5728\u6570\u7ec4\u5185\u4ea7\u751f\u4e00\u4e2a\u7a7a\u6876 \\(\\text{None}\\) \uff0c\u800c\u5f53\u67e5\u8be2\u5143\u7d20\u65f6\uff0c\u7ebf\u6027\u63a2\u6d4b\u5230\u8be5\u7a7a\u6876\u5c31\u4f1a\u8fd4\u56de\uff0c\u56e0\u6b64\u5728\u8be5\u7a7a\u6876\u4e4b\u4e0b\u7684\u5143\u7d20\u90fd\u65e0\u6cd5\u518d\u88ab\u8bbf\u95ee\u5230\uff0c\u7a0b\u5e8f\u53ef\u80fd\u8bef\u5224\u8fd9\u4e9b\u5143\u7d20\u4e0d\u5b58\u5728\u3002

\u56fe 6-7 \u00a0 \u5728\u5f00\u653e\u5bfb\u5740\u4e2d\u5220\u9664\u5143\u7d20\u5bfc\u81f4\u7684\u67e5\u8be2\u95ee\u9898

\u4e3a\u4e86\u89e3\u51b3\u8be5\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u7528\u300c\u61d2\u5220\u9664 lazy deletion\u300d\u673a\u5236\uff1a\u5b83\u4e0d\u76f4\u63a5\u4ece\u54c8\u5e0c\u8868\u4e2d\u79fb\u9664\u5143\u7d20\uff0c\u800c\u662f\u5229\u7528\u4e00\u4e2a\u5e38\u91cf TOMBSTONE \u6765\u6807\u8bb0\u8fd9\u4e2a\u6876\u3002\u5728\u8be5\u673a\u5236\u4e0b\uff0c\\(\\text{None}\\) \u548c TOMBSTONE \u90fd\u4ee3\u8868\u7a7a\u6876\uff0c\u90fd\u53ef\u4ee5\u653e\u7f6e\u952e\u503c\u5bf9\u3002\u4f46\u4e0d\u540c\u7684\u662f\uff0c\u7ebf\u6027\u63a2\u6d4b\u5230 TOMBSTONE \u65f6\u5e94\u8be5\u7ee7\u7eed\u904d\u5386\uff0c\u56e0\u4e3a\u5176\u4e4b\u4e0b\u53ef\u80fd\u8fd8\u5b58\u5728\u952e\u503c\u5bf9\u3002

\u7136\u800c\uff0c\u61d2\u5220\u9664\u53ef\u80fd\u4f1a\u52a0\u901f\u54c8\u5e0c\u8868\u7684\u6027\u80fd\u9000\u5316\u3002\u8fd9\u662f\u56e0\u4e3a\u6bcf\u6b21\u5220\u9664\u64cd\u4f5c\u90fd\u4f1a\u4ea7\u751f\u4e00\u4e2a\u5220\u9664\u6807\u8bb0\uff0c\u968f\u7740 TOMBSTONE \u7684\u589e\u52a0\uff0c\u641c\u7d22\u65f6\u95f4\u4e5f\u4f1a\u589e\u52a0\uff0c\u56e0\u4e3a\u7ebf\u6027\u63a2\u6d4b\u53ef\u80fd\u9700\u8981\u8df3\u8fc7\u591a\u4e2a TOMBSTONE \u624d\u80fd\u627e\u5230\u76ee\u6807\u5143\u7d20\u3002

\u4e3a\u6b64\uff0c\u8003\u8651\u5728\u7ebf\u6027\u63a2\u6d4b\u4e2d\u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a TOMBSTONE \u7684\u7d22\u5f15\uff0c\u5e76\u5c06\u641c\u7d22\u5230\u7684\u76ee\u6807\u5143\u7d20\u4e0e\u8be5 TOMBSTONE \u4ea4\u6362\u4f4d\u7f6e\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\u5f53\u6bcf\u6b21\u67e5\u8be2\u6216\u6dfb\u52a0\u5143\u7d20\u65f6\uff0c\u5143\u7d20\u4f1a\u88ab\u79fb\u52a8\u81f3\u8ddd\u79bb\u7406\u60f3\u4f4d\u7f6e\uff08\u63a2\u6d4b\u8d77\u59cb\u70b9\uff09\u66f4\u8fd1\u7684\u6876\uff0c\u4ece\u800c\u4f18\u5316\u67e5\u8be2\u6548\u7387\u3002

\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u4e2a\u5305\u542b\u61d2\u5220\u9664\u7684\u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\uff09\u54c8\u5e0c\u8868\u3002\u4e3a\u4e86\u66f4\u52a0\u5145\u5206\u5730\u4f7f\u7528\u54c8\u5e0c\u8868\u7684\u7a7a\u95f4\uff0c\u6211\u4eec\u5c06\u54c8\u5e0c\u8868\u770b\u4f5c\u662f\u4e00\u4e2a\u201c\u73af\u5f62\u6570\u7ec4\u201d\uff0c\u5f53\u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u56de\u5230\u5934\u90e8\u7ee7\u7eed\u904d\u5386\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig hash_map_open_addressing.py
class 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.0 / 3.0  # \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.TOMBSTONE = 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 find_bucket(self, key: int) -> int:\n\"\"\"\u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\"\"\"\nindex = self.hash_func(key)\nfirst_tombstone = -1\n# \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\nwhile self.buckets[index] is not None:\n# \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\nif self.buckets[index].key == key:\n# \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\nif first_tombstone != -1:\nself.buckets[first_tombstone] = self.buckets[index]\nself.buckets[index] = self.TOMBSTONE\nreturn first_tombstone  # \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\nreturn index  # \u8fd4\u56de\u6876\u7d22\u5f15\n# \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\nif first_tombstone == -1 and self.buckets[index] is self.TOMBSTONE:\nfirst_tombstone = index\n# \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nindex = (index + 1) % self.capacity\n# \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\nreturn index if first_tombstone == -1 else first_tombstone\ndef get(self, key: int) -> str:\n\"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\n# \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nindex = self.find_bucket(key)\n# \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif self.buckets[index] not in [None, self.TOMBSTONE]:\nreturn self.buckets[index].val\n# \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\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()\n# \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nindex = self.find_bucket(key)\n# \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\nif self.buckets[index] not in [None, self.TOMBSTONE]:\nself.buckets[index].val = val\nreturn\n# \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\nself.buckets[index] = Pair(key, val)\nself.size += 1\ndef remove(self, key: int):\n\"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\n# \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nindex = self.find_bucket(key)\n# \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\nif self.buckets[index] not in [None, self.TOMBSTONE]:\nself.buckets[index] = self.TOMBSTONE\nself.size -= 1\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.TOMBSTONE]:\nself.put(pair.key, pair.val)\ndef print(self):\n\"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\nfor pair in self.buckets:\nif pair is None:\nprint(\"None\")\nelif pair is self.TOMBSTONE:\nprint(\"TOMBSTONE\")\nelse:\nprint(pair.key, \"->\", pair.val)\n
hash_map_open_addressing.cpp
/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\nprivate:\nint size;                             // \u952e\u503c\u5bf9\u6570\u91cf\nint capacity = 4;                     // \u54c8\u5e0c\u8868\u5bb9\u91cf\nconst double loadThres = 2.0 / 3.0;     // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nconst int extendRatio = 2;            // \u6269\u5bb9\u500d\u6570\nvector<Pair *> buckets;               // \u6876\u6570\u7ec4\nPair *TOMBSTONE = new Pair(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nHashMapOpenAddressing() : size(0), buckets(capacity, nullptr) {\n}\n/* \u6790\u6784\u65b9\u6cd5 */\n~HashMapOpenAddressing() {\nfor (Pair *pair : buckets) {\nif (pair != nullptr && pair != TOMBSTONE) {\ndelete pair;\n}\n}\ndelete TOMBSTONE;\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/* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\nint findBucket(int key) {\nint index = hashFunc(key);\nint firstTombstone = -1;\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\nwhile (buckets[index] != nullptr) {\n// \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\nif (buckets[index]->key == key) {\n// \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\nif (firstTombstone != -1) {\nbuckets[firstTombstone] = buckets[index];\nbuckets[index] = TOMBSTONE;\nreturn firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n}\nreturn index; // \u8fd4\u56de\u6876\u7d22\u5f15\n}\n// \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\nif (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\nfirstTombstone = index;\n}\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nindex = (index + 1) % capacity;\n}\n// \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\nreturn firstTombstone == -1 ? index : firstTombstone;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nstring get(int key) {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = findBucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\nreturn buckets[index]->val;\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\nreturn \"\";\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}\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = findBucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\nif (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\nbuckets[index]->val = val;\nreturn;\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\nbuckets[index] = new Pair(key, val);\nsize++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid remove(int key) {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = findBucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\nif (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\ndelete buckets[index];\nbuckets[index] = TOMBSTONE;\nsize--;\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 != TOMBSTONE) {\nput(pair->key, pair->val);\ndelete pair;\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print() {\nfor (Pair *pair : buckets) {\nif (pair == nullptr) {\ncout << \"nullptr\" << endl;\n} else if (pair == TOMBSTONE) {\ncout << \"TOMBSTONE\" << endl;\n} else {\ncout << pair->key << \" -> \" << pair->val << endl;\n}\n}\n}\n};\n
hash_map_open_addressing.java
/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\nprivate int size; // \u952e\u503c\u5bf9\u6570\u91cf\nprivate int capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\nprivate final double loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nprivate final int extendRatio = 2; // \u6269\u5bb9\u500d\u6570\nprivate Pair[] buckets; // \u6876\u6570\u7ec4\nprivate final Pair TOMBSTONE = new Pair(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n/* \u6784\u9020\u65b9\u6cd5 */\npublic HashMapOpenAddressing() {\nsize = 0;\nbuckets = new Pair[capacity];\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nprivate int hashFunc(int key) {\nreturn key % capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nprivate double loadFactor() {\nreturn (double) size / capacity;\n}\n/* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\nprivate int findBucket(int key) {\nint index = hashFunc(key);\nint firstTombstone = -1;\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\nwhile (buckets[index] != null) {\n// \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\nif (buckets[index].key == key) {\n// \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\nif (firstTombstone != -1) {\nbuckets[firstTombstone] = buckets[index];\nbuckets[index] = TOMBSTONE;\nreturn firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n}\nreturn index; // \u8fd4\u56de\u6876\u7d22\u5f15\n}\n// \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\nif (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\nfirstTombstone = index;\n}\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nindex = (index + 1) % capacity;\n}\n// \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\nreturn firstTombstone == -1 ? index : firstTombstone;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npublic String get(int key) {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = findBucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif (buckets[index] != null && buckets[index] != TOMBSTONE) {\nreturn buckets[index].val;\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\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}\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = findBucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\nif (buckets[index] != null && buckets[index] != TOMBSTONE) {\nbuckets[index].val = val;\nreturn;\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\nbuckets[index] = new Pair(key, val);\nsize++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\npublic void remove(int key) {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = findBucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\nif (buckets[index] != null && buckets[index] != TOMBSTONE) {\nbuckets[index] = TOMBSTONE;\nsize--;\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nprivate 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 != TOMBSTONE) {\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(\"null\");\n} else if (pair == TOMBSTONE) {\nSystem.out.println(\"TOMBSTONE\");\n} else {\nSystem.out.println(pair.key + \" -> \" + pair.val);\n}\n}\n}\n}\n
hash_map_open_addressing.cs
/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\nprivate int size; // \u952e\u503c\u5bf9\u6570\u91cf\nprivate int capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\nprivate readonly double loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nprivate readonly int extendRatio = 2; // \u6269\u5bb9\u500d\u6570\nprivate Pair[] buckets; // \u6876\u6570\u7ec4\nprivate readonly Pair TOMBSTONE = new(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n/* \u6784\u9020\u65b9\u6cd5 */\npublic HashMapOpenAddressing() {\nsize = 0;\nbuckets = new Pair[capacity];\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nprivate int HashFunc(int key) {\nreturn key % capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nprivate double LoadFactor() {\nreturn (double)size / capacity;\n}\n/* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\nprivate int FindBucket(int key) {\nint index = HashFunc(key);\nint firstTombstone = -1;\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\nwhile (buckets[index] != null) {\n// \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\nif (buckets[index].key == key) {\n// \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\nif (firstTombstone != -1) {\nbuckets[firstTombstone] = buckets[index];\nbuckets[index] = TOMBSTONE;\nreturn firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n}\nreturn index; // \u8fd4\u56de\u6876\u7d22\u5f15\n}\n// \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\nif (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\nfirstTombstone = index;\n}\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nindex = (index + 1) % capacity;\n}\n// \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\nreturn firstTombstone == -1 ? index : firstTombstone;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npublic string? Get(int key) {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = FindBucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif (buckets[index] != null && buckets[index] != TOMBSTONE) {\nreturn buckets[index].val;\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\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}\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = FindBucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\nif (buckets[index] != null && buckets[index] != TOMBSTONE) {\nbuckets[index].val = val;\nreturn;\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\nbuckets[index] = new Pair(key, val);\nsize++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\npublic void Remove(int key) {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = FindBucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\nif (buckets[index] != null && buckets[index] != TOMBSTONE) {\nbuckets[index] = TOMBSTONE;\nsize--;\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nprivate 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\nforeach (Pair pair in bucketsTmp) {\nif (pair != null && pair != TOMBSTONE) {\nPut(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\npublic void Print() {\nforeach (Pair pair in buckets) {\nif (pair == null) {\nConsole.WriteLine(\"null\");\n} else if (pair == TOMBSTONE) {\nConsole.WriteLine(\"TOMBSTONE\");\n} else {\nConsole.WriteLine(pair.key + \" -> \" + pair.val);\n}\n}\n}\n}\n
hash_map_open_addressing.go
/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\ntype hashMapOpenAddressing struct {\nsize        int     // \u952e\u503c\u5bf9\u6570\u91cf\ncapacity    int     // \u54c8\u5e0c\u8868\u5bb9\u91cf\nloadThres   float64 // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nextendRatio int     // \u6269\u5bb9\u500d\u6570\nbuckets     []pair  // \u6876\u6570\u7ec4\nremoved     pair    // \u5220\u9664\u6807\u8bb0\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newHashMapOpenAddressing() *hashMapOpenAddressing {\nbuckets := make([]pair, 4)\nreturn &hashMapOpenAddressing{\nsize:        0,\ncapacity:    4,\nloadThres:   2.0 / 3.0,\nextendRatio: 2,\nbuckets:     buckets,\nremoved: pair{\nkey: -1,\nval: \"-1\",\n},\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (m *hashMapOpenAddressing) hashFunc(key int) int {\nreturn key % m.capacity\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfunc (m *hashMapOpenAddressing) loadFactor() float64 {\nreturn float64(m.size) / float64(m.capacity)\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (m *hashMapOpenAddressing) get(key int) string {\nidx := m.hashFunc(key)\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor i := 0; i < m.capacity; i++ {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nj := (idx + 1) % m.capacity\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u8fd4\u56de null\nif m.buckets[j] == (pair{}) {\nreturn \"\"\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif m.buckets[j].key == key && m.buckets[j] != m.removed {\nreturn m.buckets[j].val\n}\n}\n// \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\nreturn \"\"\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (m *hashMapOpenAddressing) put(key int, val string) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif m.loadFactor() > m.loadThres {\nm.extend()\n}\nidx := m.hashFunc(key)\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor i := 0; i < m.capacity; i++ {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nj := (idx + i) % m.capacity\n// \u82e5\u9047\u5230\u7a7a\u6876\u3001\u6216\u5e26\u6709\u5220\u9664\u6807\u8bb0\u7684\u6876\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u653e\u5165\u8be5\u6876\nif m.buckets[j] == (pair{}) || m.buckets[j] == m.removed {\nm.buckets[j] = pair{\nkey: key,\nval: val,\n}\nm.size += 1\nreturn\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val\nif m.buckets[j].key == key {\nm.buckets[j].val = val\n}\n}\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (m *hashMapOpenAddressing) remove(key int) {\nidx := m.hashFunc(key)\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor i := 0; i < m.capacity; i++ {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nj := (idx + 1) % m.capacity\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif m.buckets[j] == (pair{}) {\nreturn\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u6807\u8bb0\u5220\u9664\u5e76\u8fd4\u56de\nif m.buckets[j].key == key {\nm.buckets[j] = m.removed\nm.size -= 1\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfunc (m *hashMapOpenAddressing) extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\ntmpBuckets := make([]pair, len(m.buckets))\ncopy(tmpBuckets, m.buckets)\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nm.capacity *= m.extendRatio\nm.buckets = make([]pair, m.capacity)\nm.size = 0\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor _, p := range tmpBuckets {\nif p != (pair{}) && p != m.removed {\nm.put(p.key, p.val)\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (m *hashMapOpenAddressing) print() {\nfor _, p := range m.buckets {\nif p != (pair{}) {\nfmt.Println(strconv.Itoa(p.key) + \" -> \" + p.val)\n} else {\nfmt.Println(\"nil\")\n}\n}\n}\n
hash_map_open_addressing.swift
/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\nvar size: Int // \u952e\u503c\u5bf9\u6570\u91cf\nvar capacity: Int // \u54c8\u5e0c\u8868\u5bb9\u91cf\nvar loadThres: Double // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nvar extendRatio: Int // \u6269\u5bb9\u500d\u6570\nvar buckets: [Pair?] // \u6876\u6570\u7ec4\nvar TOMBSTONE: Pair // \u5220\u9664\u6807\u8bb0\n/* \u6784\u9020\u65b9\u6cd5 */\ninit() {\nsize = 0\ncapacity = 4\nloadThres = 2.0 / 3.0\nextendRatio = 2\nbuckets = Array(repeating: nil, count: capacity)\nTOMBSTONE = Pair(key: -1, val: \"-1\")\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc hashFunc(key: Int) -> Int {\nkey % capacity\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfunc loadFactor() -> Double {\nDouble(size / capacity)\n}\n/* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\nfunc findBucket(key: Int) -> Int {\nvar index = hashFunc(key: key)\nvar firstTombstone = -1\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\nwhile buckets[index] != nil {\n// \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\nif buckets[index]!.key == key {\n// \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\nif firstTombstone != -1 {\nbuckets[firstTombstone] = buckets[index]\nbuckets[index] = TOMBSTONE\nreturn firstTombstone // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n}\nreturn index // \u8fd4\u56de\u6876\u7d22\u5f15\n}\n// \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\nif firstTombstone == -1 && buckets[index] == TOMBSTONE {\nfirstTombstone = index\n}\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nindex = (index + 1) % capacity\n}\n// \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\nreturn firstTombstone == -1 ? index : firstTombstone\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc get(key: Int) -> String? {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nlet index = findBucket(key: key)\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif buckets[index] != nil, buckets[index] != TOMBSTONE {\nreturn buckets[index]!.val\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\nreturn nil\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc put(key: Int, val: String) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif loadFactor() > loadThres {\nextend()\n}\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nlet index = findBucket(key: key)\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\nif buckets[index] != nil, buckets[index] != TOMBSTONE {\nbuckets[index]!.val = val\nreturn\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\nbuckets[index] = Pair(key: key, val: val)\nsize += 1\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfunc remove(key: Int) {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nlet index = findBucket(key: key)\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\nif buckets[index] != nil, buckets[index] != TOMBSTONE {\nbuckets[index] = TOMBSTONE\nsize -= 1\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfunc extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nlet bucketsTmp = buckets\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\ncapacity *= extendRatio\nbuckets = Array(repeating: nil, count: capacity)\nsize = 0\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor pair in bucketsTmp {\nif let pair, pair != TOMBSTONE {\nput(key: pair.key, val: pair.val)\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc print() {\nfor pair in buckets {\nif pair == nil {\nSwift.print(\"null\")\n} else if pair == TOMBSTONE {\nSwift.print(\"TOMBSTONE\")\n} else {\nSwift.print(\"\\(pair!.key) -> \\(pair!.val)\")\n}\n}\n}\n}\n
hash_map_open_addressing.js
/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n#size; // \u952e\u503c\u5bf9\u6570\u91cf\n#capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n#loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n#extendRatio; // \u6269\u5bb9\u500d\u6570\n#buckets; // \u6876\u6570\u7ec4\n#removed; // \u5220\u9664\u6807\u8bb0\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor() {\nthis.#size = 0;\nthis.#capacity = 4;\nthis.#loadThres = 2.0 / 3.0;\nthis.#extendRatio = 2;\nthis.#buckets = new Array(this.#capacity).fill(null);\nthis.#removed = new Pair(-1, '-1');\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\n#hashFunc(key) {\nreturn key % this.#capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\n#loadFactor() {\nreturn this.#size / this.#capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nget(key) {\nconst index = this.#hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (let i = 0; i < this.#capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nconst j = (index + i) % this.#capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u8fd4\u56de null\nif (this.#buckets[j] === null) return null;\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif (\nthis.#buckets[j].key === key &&\nthis.#buckets[j].key !== this.#removed.key\n)\nreturn this.#buckets[j].val;\n}\nreturn null;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nput(key, val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (this.#loadFactor() > this.#loadThres) {\nthis.#extend();\n}\nconst index = this.#hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (let i = 0; i < this.#capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nlet j = (index + i) % this.#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 (\nthis.#buckets[j] === null ||\nthis.#buckets[j].key === this.#removed.key\n) {\nthis.#buckets[j] = new Pair(key, val);\nthis.#size += 1;\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val\nif (this.#buckets[j].key === key) {\nthis.#buckets[j].val = val;\nreturn;\n}\n}\n}\n/* \u5220\u9664\u64cd\u4f5c */\nremove(key) {\nconst index = this.#hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (let i = 0; i < this.#capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nconst j = (index + i) % this.#capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (this.#buckets[j] === null) {\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u6807\u8bb0\u5220\u9664\u5e76\u8fd4\u56de\nif (this.#buckets[j].key === key) {\nthis.#buckets[j] = this.#removed;\nthis.#size -= 1;\nreturn;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n#extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nconst bucketsTmp = this.#buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nthis.#capacity *= this.#extendRatio;\nthis.#buckets = new Array(this.#capacity).fill(null);\nthis.#size = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (const pair of bucketsTmp) {\nif (pair !== null && pair.key !== this.#removed.key) {\nthis.put(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nprint() {\nfor (const pair of this.#buckets) {\nif (pair !== null) {\nconsole.log(pair.key + ' -> ' + pair.val);\n} else {\nconsole.log('null');\n}\n}\n}\n}\n
hash_map_open_addressing.ts
/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n#size: number; // \u952e\u503c\u5bf9\u6570\u91cf\n#capacity: number; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n#loadThres: number; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n#extendRatio: number; // \u6269\u5bb9\u500d\u6570\n#buckets: Pair[]; // \u6876\u6570\u7ec4\n#removed: Pair; // \u5220\u9664\u6807\u8bb0\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor() {\nthis.#size = 0;\nthis.#capacity = 4;\nthis.#loadThres = 2.0 / 3.0;\nthis.#extendRatio = 2;\nthis.#buckets = new Array(this.#capacity).fill(null);\nthis.#removed = new Pair(-1, '-1');\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\n#hashFunc(key: number): number {\nreturn key % this.#capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\n#loadFactor(): number {\nreturn this.#size / this.#capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nget(key: number): string | null {\nconst index = this.#hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (let i = 0; i < this.#capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nconst j = (index + i) % this.#capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u8fd4\u56de null\nif (this.#buckets[j] === null) return null;\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif (\nthis.#buckets[j].key === key &&\nthis.#buckets[j].key !== this.#removed.key\n)\nreturn this.#buckets[j].val;\n}\nreturn null;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nput(key: number, val: string): void {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (this.#loadFactor() > this.#loadThres) {\nthis.#extend();\n}\nconst index = this.#hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (let i = 0; i < this.#capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nlet j = (index + i) % this.#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 (\nthis.#buckets[j] === null ||\nthis.#buckets[j].key === this.#removed.key\n) {\nthis.#buckets[j] = new Pair(key, val);\nthis.#size += 1;\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val\nif (this.#buckets[j].key === key) {\nthis.#buckets[j].val = val;\nreturn;\n}\n}\n}\n/* \u5220\u9664\u64cd\u4f5c */\nremove(key: number): void {\nconst index = this.#hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (let i = 0; i < this.#capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nconst j = (index + i) % this.#capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (this.#buckets[j] === null) {\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u6807\u8bb0\u5220\u9664\u5e76\u8fd4\u56de\nif (this.#buckets[j].key === key) {\nthis.#buckets[j] = this.#removed;\nthis.#size -= 1;\nreturn;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n#extend(): void {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nconst bucketsTmp = this.#buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nthis.#capacity *= this.#extendRatio;\nthis.#buckets = new Array(this.#capacity).fill(null);\nthis.#size = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (const pair of bucketsTmp) {\nif (pair !== null && pair.key !== this.#removed.key) {\nthis.put(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nprint(): void {\nfor (const pair of this.#buckets) {\nif (pair !== null) {\nconsole.log(pair.key + ' -> ' + pair.val);\n} else {\nconsole.log('null');\n}\n}\n}\n}\n
hash_map_open_addressing.dart
/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\nlate int _size; // \u952e\u503c\u5bf9\u6570\u91cf\nint _capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\ndouble _loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nint _extendRatio = 2; // \u6269\u5bb9\u500d\u6570\nlate List<Pair?> _buckets; // \u6876\u6570\u7ec4\nPair _TOMBSTONE = Pair(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n/* \u6784\u9020\u65b9\u6cd5 */\nHashMapOpenAddressing() {\n_size = 0;\n_buckets = List.generate(_capacity, (index) => null);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(int key) {\nreturn key % _capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor() {\nreturn _size / _capacity;\n}\n/* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\nint findBucket(int key) {\nint index = hashFunc(key);\nint firstTombstone = -1;\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\nwhile (_buckets[index] != null) {\n// \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\nif (_buckets[index]!.key == key) {\n// \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\nif (firstTombstone != -1) {\n_buckets[firstTombstone] = _buckets[index];\n_buckets[index] = _TOMBSTONE;\nreturn firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n}\nreturn index; // \u8fd4\u56de\u6876\u7d22\u5f15\n}\n// \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\nif (firstTombstone == -1 && _buckets[index] == _TOMBSTONE) {\nfirstTombstone = index;\n}\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nindex = (index + 1) % _capacity;\n}\n// \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\nreturn firstTombstone == -1 ? index : firstTombstone;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nString? get(int key) {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = findBucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\nreturn _buckets[index]!.val;\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\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}\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = findBucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\nif (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n_buckets[index]!.val = val;\nreturn;\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n_buckets[index] = new Pair(key, val);\n_size++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid remove(int key) {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = findBucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\nif (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n_buckets[index] = _TOMBSTONE;\n_size--;\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nList<Pair?> bucketsTmp = _buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n_capacity *= _extendRatio;\n_buckets = List.generate(_capacity, (index) => null);\n_size = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (Pair? pair in bucketsTmp) {\nif (pair != null && pair != _TOMBSTONE) {\nput(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid printHashMap() {\nfor (Pair? pair in _buckets) {\nif (pair == null) {\nprint(\"null\");\n} else if (pair == _TOMBSTONE) {\nprint(\"TOMBSTONE\");\n} else {\nprint(\"${pair.key} -> ${pair.val}\");\n}\n}\n}\n}\n
hash_map_open_addressing.rs
/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nstruct HashMapOpenAddressing {\nsize: usize,                    // \u952e\u503c\u5bf9\u6570\u91cf\ncapacity: usize,                // \u54c8\u5e0c\u8868\u5bb9\u91cf\nload_thres: f64,                // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nextend_ratio: usize,            // \u6269\u5bb9\u500d\u6570\nbuckets: Vec<Option<Pair>>,     // \u6876\u6570\u7ec4\nTOMBSTONE: Option<Pair>,        // \u5220\u9664\u6807\u8bb0\n}\nimpl HashMapOpenAddressing {\n/* \u6784\u9020\u65b9\u6cd5 */\nfn new() -> Self {\nSelf {\nsize: 0,\ncapacity: 4,\nload_thres: 2.0 / 3.0,\nextend_ratio: 2,\nbuckets: vec![None; 4],\nTOMBSTONE: Some(Pair {key: -1, val: \"-1\".to_string()}),\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nfn hash_func(&self, key: i32) -> usize {\n(key % self.capacity as i32) as usize\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfn load_factor(&self) -> f64 {\nself.size as f64 / self.capacity as f64\n}\n/* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\nfn find_bucket(&mut self, key: i32) -> usize {\nlet mut index = self.hash_func(key);\nlet mut first_tombstone = -1;\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\nwhile self.buckets[index].is_some() {\n// \u82e5\u9047\u5230 key\uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nif self.buckets[index].as_ref().unwrap().key == key {\n// \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u5efa\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\nif first_tombstone != -1 {\nself.buckets[first_tombstone as usize] = self.buckets[index].take();\nself.buckets[index] = self.TOMBSTONE.clone();\nreturn first_tombstone as usize;    // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n}\nreturn index;   // \u8fd4\u56de\u6876\u7d22\u5f15\n}\n// \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\nif first_tombstone == -1 && self.buckets[index] == self.TOMBSTONE {\nfirst_tombstone = index as i32;\n}\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nindex = (index + 1) % self.capacity;\n}\n// \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\nif first_tombstone == -1 { index } else { first_tombstone as usize }\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfn get(&mut self, key: i32) -> Option<&str> {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nlet index = self.find_bucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\nreturn self.buckets[index].as_ref().map(|pair| &pair.val as &str);\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\nNone\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfn put(&mut self, key: i32, val: String) {\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();\n}\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nlet index = self.find_bucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\nif self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\nself.buckets[index].as_mut().unwrap().val = val;\nreturn;\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\nself.buckets[index] = Some(Pair { key, val });\nself.size += 1;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfn remove(&mut self, key: i32) {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nlet index = self.find_bucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\nif self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\nself.buckets[index] = self.TOMBSTONE.clone();\nself.size -= 1;\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfn extend(&mut self) {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nlet buckets_tmp = self.buckets.clone();\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nself.capacity *= self.extend_ratio;\nself.buckets = vec![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.is_none() || pair == self.TOMBSTONE {\ncontinue;\n}\nlet pair = pair.unwrap();\nself.put(pair.key, pair.val);\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfn print(&self) {\nfor pair in &self.buckets {\nif pair.is_none() {\nprintln!(\"null\");\n} else if pair == &self.TOMBSTONE {\nprintln!(\"TOMBSTONE\");\n} else {\nlet pair = pair.as_ref().unwrap();\nprintln!(\"{} -> {}\", pair.key, pair.val);\n}\n}\n}\n}\n
hash_map_open_addressing.c
/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nstruct hashMapOpenAddressing {\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\nPair **buckets;   // \u6876\u6570\u7ec4\nPair *TOMBSTONE;  // \u5220\u9664\u6807\u8bb0\n};\ntypedef struct hashMapOpenAddressing hashMapOpenAddressing;\n/* \u6784\u9020\u65b9\u6cd5 */\nhashMapOpenAddressing *newHashMapOpenAddressing() {\nhashMapOpenAddressing *hashMap = (hashMapOpenAddressing *)malloc(sizeof(hashMapOpenAddressing));\nhashMap->size = 0;\nhashMap->capacity = 4;\nhashMap->loadThres = 2.0 / 3.0;\nhashMap->extendRatio = 2;\nhashMap->buckets = (Pair **)malloc(sizeof(Pair *) * hashMap->capacity);\nhashMap->TOMBSTONE = (Pair *)malloc(sizeof(Pair));\nhashMap->TOMBSTONE->key = -1;\nhashMap->TOMBSTONE->val = \"-1\";\nreturn hashMap;\n}\n/* \u6790\u6784\u65b9\u6cd5 */\nvoid delHashMapOpenAddressing(hashMapOpenAddressing *hashMap) {\nfor (int i = 0; i < hashMap->capacity; i++) {\nPair *pair = hashMap->buckets[i];\nif (pair != NULL && pair != hashMap->TOMBSTONE) {\nfree(pair->val);\nfree(pair);\n}\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(hashMapOpenAddressing *hashMap, int key) {\nreturn key % hashMap->capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor(hashMapOpenAddressing *hashMap) {\nreturn (double)hashMap->size / (double)hashMap->capacity;\n}\n/* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\nint findBucket(hashMapOpenAddressing *hashMap, int key) {\nint index = hashFunc(hashMap, key);\nint firstTombstone = -1;\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\nwhile (hashMap->buckets[index] != NULL) {\n// \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\nif (hashMap->buckets[index]->key == key) {\n// \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\nif (firstTombstone != -1) {\nhashMap->buckets[firstTombstone] = hashMap->buckets[index];\nhashMap->buckets[index] = hashMap->TOMBSTONE;\nreturn firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n}\nreturn index; // \u8fd4\u56de\u6876\u7d22\u5f15\n}\n// \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\nif (firstTombstone == -1 && hashMap->buckets[index] == hashMap->TOMBSTONE) {\nfirstTombstone = index;\n}\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nindex = (index + 1) % hashMap->capacity;\n}\n// \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\nreturn firstTombstone == -1 ? index : firstTombstone;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nchar *get(hashMapOpenAddressing *hashMap, int key) {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = findBucket(hashMap, key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\nreturn hashMap->buckets[index]->val;\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\nreturn \"\";\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(hashMapOpenAddressing *hashMap, int key, char *val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (loadFactor(hashMap) > hashMap->loadThres) {\nextend(hashMap);\n}\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = findBucket(hashMap, key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\nif (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\nfree(hashMap->buckets[index]->val);\nhashMap->buckets[index]->val = (char *)malloc(sizeof(strlen(val + 1)));\nstrcpy(hashMap->buckets[index]->val, val);\nhashMap->buckets[index]->val[strlen(val)] = '\\0';\nreturn;\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\nPair *pair = (Pair *)malloc(sizeof(Pair));\npair->key = key;\npair->val = (char *)malloc(sizeof(strlen(val + 1)));\nstrcpy(pair->val, val);\npair->val[strlen(val)] = '\\0';\nhashMap->buckets[index] = pair;\nhashMap->size++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid removeItem(hashMapOpenAddressing *hashMap, int key) {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = findBucket(hashMap, key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\nif (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\nPair *pair = hashMap->buckets[index];\nfree(pair->val);\nfree(pair);\nhashMap->buckets[index] = hashMap->TOMBSTONE;\nhashMap->size--;\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend(hashMapOpenAddressing *hashMap) {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nPair **bucketsTmp = hashMap->buckets;\nint oldCapacity = hashMap->capacity;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nhashMap->capacity *= hashMap->extendRatio;\nhashMap->buckets = (Pair **)malloc(sizeof(Pair *) * hashMap->capacity);\nhashMap->size = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (int i = 0; i < oldCapacity; i++) {\nPair *pair = bucketsTmp[i];\nif (pair != NULL && pair != hashMap->TOMBSTONE) {\nput(hashMap, pair->key, pair->val);\nfree(pair->val);\nfree(pair);\n}\n}\nfree(bucketsTmp);\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print(hashMapOpenAddressing *hashMap) {\nfor (int i = 0; i < hashMap->capacity; i++) {\nPair *pair = hashMap->buckets[i];\nif (pair == NULL) {\nprintf(\"NULL\\n\");\n} else if (pair == hashMap->TOMBSTONE) {\nprintf(\"TOMBSTONE\\n\");\n} else {\nprintf(\"%d -> %s\\n\", pair->key, pair->val);\n}\n}\n}\n
hash_map_open_addressing.zig
[class]{HashMapOpenAddressing}-[func]{}\n
"},{"location":"chapter_hashing/hash_collision/#2","title":"2. \u00a0 \u5e73\u65b9\u63a2\u6d4b","text":"

\u5e73\u65b9\u63a2\u6d4b\u4e0e\u7ebf\u6027\u63a2\u6d4b\u7c7b\u4f3c\uff0c\u90fd\u662f\u5f00\u653e\u5bfb\u5740\u7684\u5e38\u89c1\u7b56\u7565\u4e4b\u4e00\u3002\u5f53\u53d1\u751f\u51b2\u7a81\u65f6\uff0c\u5e73\u65b9\u63a2\u6d4b\u4e0d\u662f\u7b80\u5355\u5730\u8df3\u8fc7\u4e00\u4e2a\u56fa\u5b9a\u7684\u6b65\u6570\uff0c\u800c\u662f\u8df3\u8fc7\u201c\u63a2\u6d4b\u6b21\u6570\u7684\u5e73\u65b9\u201d\u7684\u6b65\u6570\uff0c\u5373 \\(1, 4, 9, \\dots\\) \u6b65\u3002

\u5e73\u65b9\u63a2\u6d4b\u901a\u4e3b\u8981\u5177\u6709\u4ee5\u4e0b\u4f18\u52bf\u3002

  • \u5e73\u65b9\u63a2\u6d4b\u901a\u8fc7\u8df3\u8fc7\u5e73\u65b9\u7684\u8ddd\u79bb\uff0c\u8bd5\u56fe\u7f13\u89e3\u7ebf\u6027\u63a2\u6d4b\u7684\u805a\u96c6\u6548\u5e94\u3002
  • \u5e73\u65b9\u63a2\u6d4b\u4f1a\u8df3\u8fc7\u66f4\u5927\u7684\u8ddd\u79bb\u6765\u5bfb\u627e\u7a7a\u4f4d\u7f6e\uff0c\u6709\u52a9\u4e8e\u6570\u636e\u5206\u5e03\u5f97\u66f4\u52a0\u5747\u5300\u3002

\u7136\u800c\uff0c\u5e73\u65b9\u63a2\u6d4b\u4e5f\u5e76\u4e0d\u662f\u5b8c\u7f8e\u7684\u3002

  • \u4ecd\u7136\u5b58\u5728\u805a\u96c6\u73b0\u8c61\uff0c\u5373\u67d0\u4e9b\u4f4d\u7f6e\u6bd4\u5176\u4ed6\u4f4d\u7f6e\u66f4\u5bb9\u6613\u88ab\u5360\u7528\u3002
  • \u7531\u4e8e\u5e73\u65b9\u7684\u589e\u957f\uff0c\u5e73\u65b9\u63a2\u6d4b\u53ef\u80fd\u4e0d\u4f1a\u63a2\u6d4b\u6574\u4e2a\u54c8\u5e0c\u8868\uff0c\u8fd9\u610f\u5473\u7740\u5373\u4f7f\u54c8\u5e0c\u8868\u4e2d\u6709\u7a7a\u6876\uff0c\u5e73\u65b9\u63a2\u6d4b\u4e5f\u53ef\u80fd\u65e0\u6cd5\u8bbf\u95ee\u5230\u5b83\u3002
"},{"location":"chapter_hashing/hash_collision/#3","title":"3. \u00a0 \u591a\u6b21\u54c8\u5e0c","text":"

\u591a\u6b21\u54c8\u5e0c\u4f7f\u7528\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570 \\(f_1(x)\\)\u3001\\(f_2(x)\\)\u3001\\(f_3(x)\\)\u3001\\(\\dots\\) \u8fdb\u884c\u63a2\u6d4b\u3002

  • \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\u6876\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\u5f53\u9047\u5230\u7a7a\u6876\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

Tip

\u8bf7\u6ce8\u610f\uff0c\u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\u3001\u5e73\u65b9\u63a2\u6d4b\u548c\u591a\u6b21\u54c8\u5e0c\uff09\u54c8\u5e0c\u8868\u90fd\u5b58\u5728\u201c\u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5143\u7d20\u201d\u7684\u95ee\u9898\u3002

"},{"location":"chapter_hashing/hash_collision/#623","title":"6.2.3 \u00a0 \u7f16\u7a0b\u8bed\u8a00\u7684\u9009\u62e9","text":"

\u5404\u4e2a\u7f16\u7a0b\u8bed\u8a00\u91c7\u53d6\u4e86\u4e0d\u540c\u7684\u54c8\u5e0c\u8868\u5b9e\u73b0\u7b56\u7565\uff0c\u4ee5\u4e0b\u4e3e\u51e0\u4e2a\u4f8b\u5b50\u3002

  • 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\u3002\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\uff0c\u53c8\u79f0\u300c\u6563\u5217\u8868\u300d\uff0c\u5176\u901a\u8fc7\u5efa\u7acb\u952e key \u4e0e\u503c value \u4e4b\u95f4\u7684\u6620\u5c04\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684\u5143\u7d20\u67e5\u8be2\u3002\u5177\u4f53\u800c\u8a00\uff0c\u6211\u4eec\u5411\u54c8\u5e0c\u8868\u8f93\u5165\u4e00\u4e2a\u952e key \uff0c\u5219\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u83b7\u53d6\u5bf9\u5e94\u7684\u503c value \u3002

\u5982\u56fe 6-1 \u6240\u793a\uff0c\u7ed9\u5b9a \\(n\\) \u4e2a\u5b66\u751f\uff0c\u6bcf\u4e2a\u5b66\u751f\u90fd\u6709\u201c\u59d3\u540d\u201d\u548c\u201c\u5b66\u53f7\u201d\u4e24\u9879\u6570\u636e\u3002\u5047\u5982\u6211\u4eec\u5e0c\u671b\u5b9e\u73b0\u201c\u8f93\u5165\u4e00\u4e2a\u5b66\u53f7\uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u59d3\u540d\u201d\u7684\u67e5\u8be2\u529f\u80fd\uff0c\u5219\u53ef\u4ee5\u91c7\u7528\u56fe 6-1 \u6240\u793a\u7684\u54c8\u5e0c\u8868\u6765\u5b9e\u73b0\u3002

\u56fe 6-1 \u00a0 \u54c8\u5e0c\u8868\u7684\u62bd\u8c61\u8868\u793a

\u9664\u54c8\u5e0c\u8868\u5916\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u67e5\u8be2\u529f\u80fd\uff0c\u5b83\u4eec\u7684\u6548\u7387\u5bf9\u6bd4\u5982\u8868 6-1 \u6240\u793a\u3002

  • \u6dfb\u52a0\u5143\u7d20\uff1a\u4ec5\u9700\u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u6570\u7ec4\uff08\u94fe\u8868\uff09\u7684\u5c3e\u90e8\u5373\u53ef\uff0c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002
  • \u67e5\u8be2\u5143\u7d20\uff1a\u7531\u4e8e\u6570\u7ec4\uff08\u94fe\u8868\uff09\u662f\u4e71\u5e8f\u7684\uff0c\u56e0\u6b64\u9700\u8981\u904d\u5386\u5176\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002
  • \u5220\u9664\u5143\u7d20\uff1a\u9700\u8981\u5148\u67e5\u8be2\u5230\u5143\u7d20\uff0c\u518d\u4ece\u6570\u7ec4\uff08\u94fe\u8868\uff09\u4e2d\u5220\u9664\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002

\u8868 6-1 \u00a0 \u5143\u7d20\u67e5\u8be2\u6548\u7387\u5bf9\u6bd4

\u6570\u7ec4 \u94fe\u8868 \u54c8\u5e0c\u8868 \u67e5\u627e\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(1)\\) \u6dfb\u52a0\u5143\u7d20 \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(1)\\)

\u89c2\u5bdf\u53d1\u73b0\uff0c\u5728\u54c8\u5e0c\u8868\u4e2d\u8fdb\u884c\u589e\u5220\u67e5\u6539\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662f \\(O(1)\\) \uff0c\u975e\u5e38\u9ad8\u6548\u3002

"},{"location":"chapter_hashing/hash_map/#611","title":"6.1.1 \u00a0 \u54c8\u5e0c\u8868\u5e38\u7528\u64cd\u4f5c","text":"

\u54c8\u5e0c\u8868\u7684\u5e38\u89c1\u64cd\u4f5c\u5305\u62ec\uff1a\u521d\u59cb\u5316\u3001\u67e5\u8be2\u64cd\u4f5c\u3001\u6dfb\u52a0\u952e\u503c\u5bf9\u548c\u5220\u9664\u952e\u503c\u5bf9\u7b49\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig hash_map.py
# \u521d\u59cb\u5316\u54c8\u5e0c\u8868\nhmap: dict = {}\n# \u6dfb\u52a0\u64cd\u4f5c\n# \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nhmap[12836] = \"\u5c0f\u54c8\"\nhmap[15937] = \"\u5c0f\u5570\"\nhmap[16750] = \"\u5c0f\u7b97\"\nhmap[13276] = \"\u5c0f\u6cd5\"\nhmap[10583] = \"\u5c0f\u9e2d\"\n# \u67e5\u8be2\u64cd\u4f5c\n# \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nname: str = hmap[15937]\n# \u5220\u9664\u64cd\u4f5c\n# \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nhmap.pop(10583)\n
hash_map.cpp
/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nunordered_map<int, string> map;\n/* \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.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.cs
/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nDictionary<int, string> map = new() {\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\n{ 12836, \"\u5c0f\u54c8\" },\n{ 15937, \"\u5c0f\u5570\" },\n{ 16750, \"\u5c0f\u7b97\" },\n{ 13276, \"\u5c0f\u6cd5\" },\n{ 10583, \"\u5c0f\u9e2d\" }\n};\n/* \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_test.go
/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nhmap := make(map[int]string)\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nhmap[12836] = \"\u5c0f\u54c8\"\nhmap[15937] = \"\u5c0f\u5570\"\nhmap[16750] = \"\u5c0f\u7b97\"\nhmap[13276] = \"\u5c0f\u6cd5\"\nhmap[10583] = \"\u5c0f\u9e2d\"\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nname := hmap[15937]\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\ndelete(hmap, 10583)\n
hash_map.swift
/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nvar map: [Int: String] = [:]\n/* \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.js
/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nconst map = new Map();\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.set(12836, '\u5c0f\u54c8');\nmap.set(15937, '\u5c0f\u5570');\nmap.set(16750, '\u5c0f\u7b97');\nmap.set(13276, '\u5c0f\u6cd5');\nmap.set(10583, '\u5c0f\u9e2d');\n/* \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.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
hash_map.rs
use std::collections::HashMap;\n/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nlet mut map: HashMap<i32, String> = HashMap::new();\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.insert(12836, \"\u5c0f\u54c8\".to_string());\nmap.insert(15937, \"\u5c0f\u5570\".to_string());\nmap.insert(16750, \"\u5c0f\u7b97\".to_string());\nmap.insert(13279, \"\u5c0f\u6cd5\".to_string());\nmap.insert(10583, \"\u5c0f\u9e2d\".to_string());\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet _name: Option<&String> = map.get(&15937);\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nlet _removed_value: Option<String> = map.remove(&10583);\n
hash_map.c
// C \u672a\u63d0\u4f9b\u5185\u7f6e\u54c8\u5e0c\u8868\n
hash_map.zig
\n

\u54c8\u5e0c\u8868\u6709\u4e09\u79cd\u5e38\u7528\u904d\u5386\u65b9\u5f0f\uff1a\u904d\u5386\u952e\u503c\u5bf9\u3001\u904d\u5386\u952e\u548c\u904d\u5386\u503c\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig hash_map.py
# \u904d\u5386\u54c8\u5e0c\u8868\n# \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor key, value in hmap.items():\nprint(key, \"->\", value)\n# \u5355\u72ec\u904d\u5386\u952e key\nfor key in hmap.keys():\nprint(key)\n# \u5355\u72ec\u904d\u5386\u503c value\nfor value in hmap.values():\nprint(value)\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 kv: map) {\ncout << kv.first << endl;\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor (auto kv: map) {\ncout << kv.second << endl;\n}\n
hash_map.java
/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor (Map.Entry <Integer, String> kv: map.entrySet()) {\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.cs
/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nforeach (var kv in map) {\nConsole.WriteLine(kv.Key + \" -> \" + kv.Value);\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nforeach (int key in map.Keys) {\nConsole.WriteLine(key);\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nforeach (string val in map.Values) {\nConsole.WriteLine(val);\n}\n
hash_map_test.go
/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor key, value := range hmap {\nfmt.Println(key, \"->\", value)\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nfor key := range hmap {\nfmt.Println(key)\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor _, value := range hmap {\nfmt.Println(value)\n}\n
hash_map.swift
/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nfor (key, value) in map {\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.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.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
hash_map.rs
/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nfor (key, value) in &map {\nprintln!(\"{key} -> {value}\");\n}\n// \u5355\u72ec\u904d\u5386\u952e Key\nfor key in map.keys() {\nprintln!(\"{key}\"); }\n// \u5355\u72ec\u904d\u5386\u503c Value\nfor value in map.values() {\nprintln!(\"{value}\");\n}\n
hash_map.c
// C \u672a\u63d0\u4f9b\u5185\u7f6e\u54c8\u5e0c\u8868\n
hash_map.zig
\n
"},{"location":"chapter_hashing/hash_map/#612","title":"6.1.2 \u00a0 \u54c8\u5e0c\u8868\u7b80\u5355\u5b9e\u73b0","text":"

\u6211\u4eec\u5148\u8003\u8651\u6700\u7b80\u5355\u7684\u60c5\u51b5\uff0c\u4ec5\u7528\u4e00\u4e2a\u6570\u7ec4\u6765\u5b9e\u73b0\u54c8\u5e0c\u8868\u3002\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u6211\u4eec\u5c06\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u7a7a\u4f4d\u79f0\u4e3a\u300c\u6876 bucket\u300d\uff0c\u6bcf\u4e2a\u6876\u53ef\u5b58\u50a8\u4e00\u4e2a\u952e\u503c\u5bf9\u3002\u56e0\u6b64\uff0c\u67e5\u8be2\u64cd\u4f5c\u5c31\u662f\u627e\u5230 key \u5bf9\u5e94\u7684\u6876\uff0c\u5e76\u5728\u6876\u4e2d\u83b7\u53d6 value \u3002

\u90a3\u4e48\uff0c\u5982\u4f55\u57fa\u4e8e key \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\u5728\u6570\u7ec4\u4e2d\u7684\u5b58\u50a8\u4f4d\u7f6e\u3002

\u8f93\u5165\u4e00\u4e2a key \uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u8ba1\u7b97\u8fc7\u7a0b\u5206\u4e3a\u4ee5\u4e0b\u4e24\u6b65\u3002

  1. \u901a\u8fc7\u67d0\u79cd\u54c8\u5e0c\u7b97\u6cd5 hash() \u8ba1\u7b97\u5f97\u5230\u54c8\u5e0c\u503c\u3002
  2. \u5c06\u54c8\u5e0c\u503c\u5bf9\u6876\u6570\u91cf\uff08\u6570\u7ec4\u957f\u5ea6\uff09capacity \u53d6\u6a21\uff0c\u4ece\u800c\u83b7\u53d6\u8be5 key \u5bf9\u5e94\u7684\u6570\u7ec4\u7d22\u5f15 index \u3002
index = hash(key) % capacity\n

\u968f\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5229\u7528 index \u5728\u54c8\u5e0c\u8868\u4e2d\u8bbf\u95ee\u5bf9\u5e94\u7684\u6876\uff0c\u4ece\u800c\u83b7\u53d6 value \u3002

\u8bbe\u6570\u7ec4\u957f\u5ea6 capacity = 100\u3001\u54c8\u5e0c\u7b97\u6cd5 hash(key) = key \uff0c\u6613\u5f97\u54c8\u5e0c\u51fd\u6570\u4e3a key % 100 \u3002\u56fe 6-2 \u4ee5 key \u5b66\u53f7\u548c value \u59d3\u540d\u4e3a\u4f8b\uff0c\u5c55\u793a\u4e86\u54c8\u5e0c\u51fd\u6570\u7684\u5de5\u4f5c\u539f\u7406\u3002

\u56fe 6-2 \u00a0 \u54c8\u5e0c\u51fd\u6570\u5de5\u4f5c\u539f\u7406

\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u5355\u54c8\u5e0c\u8868\u3002\u5176\u4e2d\uff0c\u6211\u4eec\u5c06 key \u548c value \u5c01\u88c5\u6210\u4e00\u4e2a\u7c7b Pair \uff0c\u4ee5\u8868\u793a\u952e\u503c\u5bf9\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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 \"\";\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.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.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 readonly 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(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.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.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.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) {\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) {\nconsole.info(`${pair.key} -> ${pair.val}`);\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
array_hash_map.rs
/* \u952e\u503c\u5bf9 */\n#[derive(Debug, Clone, PartialEq)]\npub struct Pair {\npub key: i32,\npub val: String,\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\npub struct ArrayHashMap {\nbuckets: Vec<Option<Pair>>\n}\nimpl ArrayHashMap {\npub fn new() -> ArrayHashMap {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nSelf { buckets: vec![None; 100] }\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nfn hash_func(&self, key: i32) -> usize {\nkey as usize % 100\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npub fn get(&self, key: i32) -> Option<&String> {\nlet index = self.hash_func(key);\nself.buckets[index].as_ref().map(|pair| &pair.val)\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\npub fn put(&mut self, key: i32, val: &str) {\nlet index = self.hash_func(key);\nself.buckets[index] = Some(Pair {\nkey,\nval: val.to_string(),\n});\n}\n/* \u5220\u9664\u64cd\u4f5c */\npub fn remove(&mut self, key: i32) {\nlet index = self.hash_func(key);\n// \u7f6e\u4e3a None \uff0c\u4ee3\u8868\u5220\u9664\nself.buckets[index] = None;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\npub fn entry_set(&self) -> Vec<&Pair> {\nself.buckets.iter().filter_map(|pair| pair.as_ref()).collect()\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\npub fn key_set(&self) -> Vec<&i32> {\nself.buckets.iter().filter_map(|pair| pair.as_ref().map(|pair| &pair.key)).collect()\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\npub fn value_set(&self) -> Vec<&String> {\nself.buckets.iter().filter_map(|pair| pair.as_ref().map(|pair| &pair.val)).collect()\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\npub fn print(&self) {\nfor pair in self.entry_set() {\nprintln!(\"{} -> {}\", 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/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nstruct arrayHashMap {\npair *buckets[HASH_MAP_DEFAULT_SIZE];\n};\ntypedef struct arrayHashMap arrayHashMap;\n/* \u54c8\u5e0c\u8868\u521d\u59cb\u5316\u51fd\u6570 */\narrayHashMap *newArrayHashMap() {\narrayHashMap *map = malloc(sizeof(arrayHashMap));\nreturn map;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(arrayHashMap *d, const int key, const char *val) {\npair *pair = malloc(sizeof(pair));\npair->key = key;\npair->val = malloc(strlen(val) + 1);\nstrcpy(pair->val, val);\nint index = hashFunc(key);\nd->buckets[index] = pair;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid removeItem(arrayHashMap *d, const int key) {\nint index = hashFunc(key);\nfree(d->buckets[index]->val);\nfree(d->buckets[index]);\nd->buckets[index] = NULL;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\nvoid pairSet(arrayHashMap *d, mapSet *set) {\npair *entries;\nint i = 0, index = 0;\nint total = 0;\n/* \u7edf\u8ba1\u6709\u6548\u952e\u503c\u5bf9\u6570\u91cf */\nfor (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {\nif (d->buckets[i] != NULL) {\ntotal++;\n}\n}\nentries = malloc(sizeof(pair) * total);\nfor (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {\nif (d->buckets[i] != NULL) {\nentries[index].key = d->buckets[i]->key;\nentries[index].val = malloc(strlen(d->buckets[i]->val + 1));\nstrcpy(entries[index].val, d->buckets[i]->val);\nindex++;\n}\n}\nset->set = entries;\nset->len = total;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\nvoid keySet(arrayHashMap *d, mapSet *set) {\nint *keys;\nint i = 0, index = 0;\nint total = 0;\n/* \u7edf\u8ba1\u6709\u6548\u952e\u503c\u5bf9\u6570\u91cf */\nfor (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {\nif (d->buckets[i] != NULL) {\ntotal++;\n}\n}\nkeys = malloc(total * sizeof(int));\nfor (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {\nif (d->buckets[i] != NULL) {\nkeys[index] = d->buckets[i]->key;\nindex++;\n}\n}\nset->set = keys;\nset->len = total;\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\nvoid valueSet(arrayHashMap *d, mapSet *set) {\nchar **vals;\nint i = 0, index = 0;\nint total = 0;\n/* \u7edf\u8ba1\u6709\u6548\u952e\u503c\u5bf9\u6570\u91cf */\nfor (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {\nif (d->buckets[i] != NULL) {\ntotal++;\n}\n}\nvals = malloc(total * sizeof(char *));\nfor (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {\nif (d->buckets[i] != NULL) {\nvals[index] = d->buckets[i]->val;\nindex++;\n}\n}\nset->set = vals;\nset->len = total;\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print(arrayHashMap *d) {\nint i;\nmapSet set;\npairSet(d, &set);\npair *entries = (pair *)set.set;\nfor (i = 0; i < set.len; i++) {\nprintf(\"%d -> %s\\n\", entries[i].key, entries[i].val);\n}\nfree(set.set);\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 {\nbucket: ?std.ArrayList(?T) = null,\nmem_allocator: std.mem.Allocator = undefined,\nconst Self = @This();\n// \u6784\u9020\u51fd\u6570\npub fn init(self: *Self, allocator: std.mem.Allocator) !void {\nself.mem_allocator = allocator;\n// \u521d\u59cb\u5316\u4e00\u4e2a\u957f\u5ea6\u4e3a 100 \u7684\u6876\uff08\u6570\u7ec4\uff09\nself.bucket = std.ArrayList(?T).init(self.mem_allocator);\nvar i: i32 = 0;\nwhile (i < 100) : (i += 1) {\ntry self.bucket.?.append(null);\n}\n}\n// \u6790\u6784\u51fd\u6570\npub fn deinit(self: *Self) void {\nif (self.bucket != null) self.bucket.?.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.bucket.?.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.bucket.?.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.bucket.?.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.bucket.?.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.bucket.?.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.bucket.?.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
"},{"location":"chapter_hashing/hash_map/#613","title":"6.1.3 \u00a0 \u54c8\u5e0c\u51b2\u7a81\u4e0e\u6269\u5bb9","text":"

\u672c\u8d28\u4e0a\u770b\uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5c06\u6240\u6709 key \u6784\u6210\u7684\u8f93\u5165\u7a7a\u95f4\u6620\u5c04\u5230\u6570\u7ec4\u6240\u6709\u7d22\u5f15\u6784\u6210\u7684\u8f93\u51fa\u7a7a\u95f4\uff0c\u800c\u8f93\u5165\u7a7a\u95f4\u5f80\u5f80\u8fdc\u5927\u4e8e\u8f93\u51fa\u7a7a\u95f4\u3002\u56e0\u6b64\uff0c\u7406\u8bba\u4e0a\u4e00\u5b9a\u5b58\u5728\u201c\u591a\u4e2a\u8f93\u5165\u5bf9\u5e94\u76f8\u540c\u8f93\u51fa\u201d\u7684\u60c5\u51b5\u3002

\u5bf9\u4e8e\u4e0a\u8ff0\u793a\u4f8b\u4e2d\u7684\u54c8\u5e0c\u51fd\u6570\uff0c\u5f53\u8f93\u5165\u7684 key \u540e\u4e24\u4f4d\u76f8\u540c\u65f6\uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u8f93\u51fa\u7ed3\u679c\u4e5f\u76f8\u540c\u3002\u4f8b\u5982\uff0c\u67e5\u8be2\u5b66\u53f7\u4e3a 12836 \u548c 20336 \u7684\u4e24\u4e2a\u5b66\u751f\u65f6\uff0c\u6211\u4eec\u5f97\u5230\uff1a

12836 % 100 = 36\n20336 % 100 = 36\n

\u5982\u56fe 6-3 \u6240\u793a\uff0c\u4e24\u4e2a\u5b66\u53f7\u6307\u5411\u4e86\u540c\u4e00\u4e2a\u59d3\u540d\uff0c\u8fd9\u663e\u7136\u662f\u4e0d\u5bf9\u7684\u3002\u6211\u4eec\u5c06\u8fd9\u79cd\u591a\u4e2a\u8f93\u5165\u5bf9\u5e94\u540c\u4e00\u8f93\u51fa\u7684\u60c5\u51b5\u79f0\u4e3a\u300c\u54c8\u5e0c\u51b2\u7a81 hash collision\u300d\u3002

\u56fe 6-3 \u00a0 \u54c8\u5e0c\u51b2\u7a81\u793a\u4f8b

\u5bb9\u6613\u60f3\u5230\uff0c\u54c8\u5e0c\u8868\u5bb9\u91cf \\(n\\) \u8d8a\u5927\uff0c\u591a\u4e2a key \u88ab\u5206\u914d\u5230\u540c\u4e00\u4e2a\u6876\u4e2d\u7684\u6982\u7387\u5c31\u8d8a\u4f4e\uff0c\u51b2\u7a81\u5c31\u8d8a\u5c11\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6269\u5bb9\u54c8\u5e0c\u8868\u6765\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\u3002

\u5982\u56fe 6-4 \u6240\u793a\uff0c\u6269\u5bb9\u524d\u952e\u503c\u5bf9 (136, A) \u548c (236, D) \u53d1\u751f\u51b2\u7a81\uff0c\u6269\u5bb9\u540e\u51b2\u7a81\u6d88\u5931\u3002

\u56fe 6-4 \u00a0 \u54c8\u5e0c\u8868\u6269\u5bb9

\u7c7b\u4f3c\u4e8e\u6570\u7ec4\u6269\u5bb9\uff0c\u54c8\u5e0c\u8868\u6269\u5bb9\u9700\u5c06\u6240\u6709\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u8fc1\u79fb\u81f3\u65b0\u54c8\u5e0c\u8868\uff0c\u975e\u5e38\u8017\u65f6\u3002\u5e76\u4e14\u7531\u4e8e\u54c8\u5e0c\u8868\u5bb9\u91cf capacity \u6539\u53d8\uff0c\u6211\u4eec\u9700\u8981\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u6765\u91cd\u65b0\u8ba1\u7b97\u6240\u6709\u952e\u503c\u5bf9\u7684\u5b58\u50a8\u4f4d\u7f6e\uff0c\u8fd9\u8fdb\u4e00\u6b65\u63d0\u9ad8\u4e86\u6269\u5bb9\u8fc7\u7a0b\u7684\u8ba1\u7b97\u5f00\u9500\u3002\u4e3a\u6b64\uff0c\u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u4f1a\u9884\u7559\u8db3\u591f\u5927\u7684\u54c8\u5e0c\u8868\u5bb9\u91cf\uff0c\u9632\u6b62\u9891\u7e41\u6269\u5bb9\u3002

\u300c\u8d1f\u8f7d\u56e0\u5b50 load factor\u300d\u662f\u54c8\u5e0c\u8868\u7684\u4e00\u4e2a\u91cd\u8981\u6982\u5ff5\uff0c\u5176\u5b9a\u4e49\u4e3a\u54c8\u5e0c\u8868\u7684\u5143\u7d20\u6570\u91cf\u9664\u4ee5\u6876\u6570\u91cf\uff0c\u7528\u4e8e\u8861\u91cf\u54c8\u5e0c\u51b2\u7a81\u7684\u4e25\u91cd\u7a0b\u5ea6\uff0c\u4e5f\u5e38\u88ab\u4f5c\u4e3a\u54c8\u5e0c\u8868\u6269\u5bb9\u7684\u89e6\u53d1\u6761\u4ef6\u3002\u4f8b\u5982\u5728 Java \u4e2d\uff0c\u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7 \\(0.75\\) \u65f6\uff0c\u7cfb\u7edf\u4f1a\u5c06\u54c8\u5e0c\u8868\u5bb9\u91cf\u6269\u5c55\u4e3a\u539f\u5148\u7684 \\(2\\) \u500d\u3002

"},{"location":"chapter_hashing/summary/","title":"6.4 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_hashing/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
  • \u8f93\u5165 key \uff0c\u54c8\u5e0c\u8868\u80fd\u591f\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u67e5\u8be2\u5230 value \uff0c\u6548\u7387\u975e\u5e38\u9ad8\u3002
  • \u5e38\u89c1\u7684\u54c8\u5e0c\u8868\u64cd\u4f5c\u5305\u62ec\u67e5\u8be2\u3001\u6dfb\u52a0\u952e\u503c\u5bf9\u3001\u5220\u9664\u952e\u503c\u5bf9\u548c\u904d\u5386\u54c8\u5e0c\u8868\u7b49\u3002
  • \u54c8\u5e0c\u51fd\u6570\u5c06 key \u6620\u5c04\u4e3a\u6570\u7ec4\u7d22\u5f15\uff0c\u4ece\u800c\u8bbf\u95ee\u5bf9\u5e94\u6876\u5e76\u83b7\u53d6 value \u3002
  • \u4e24\u4e2a\u4e0d\u540c\u7684 key \u53ef\u80fd\u5728\u7ecf\u8fc7\u54c8\u5e0c\u51fd\u6570\u540e\u5f97\u5230\u76f8\u540c\u7684\u6570\u7ec4\u7d22\u5f15\uff0c\u5bfc\u81f4\u67e5\u8be2\u7ed3\u679c\u51fa\u9519\uff0c\u8fd9\u79cd\u73b0\u8c61\u88ab\u79f0\u4e3a\u54c8\u5e0c\u51b2\u7a81\u3002
  • \u54c8\u5e0c\u8868\u5bb9\u91cf\u8d8a\u5927\uff0c\u54c8\u5e0c\u51b2\u7a81\u7684\u6982\u7387\u5c31\u8d8a\u4f4e\u3002\u56e0\u6b64\u53ef\u4ee5\u901a\u8fc7\u6269\u5bb9\u54c8\u5e0c\u8868\u6765\u7f13\u89e3\u54c8\u5e0c\u51b2\u7a81\u3002\u4e0e\u6570\u7ec4\u6269\u5bb9\u7c7b\u4f3c\uff0c\u54c8\u5e0c\u8868\u6269\u5bb9\u64cd\u4f5c\u7684\u5f00\u9500\u5f88\u5927\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
  • \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\u3002\u7136\u800c\uff0c\u94fe\u8868\u8fc7\u957f\u4f1a\u964d\u4f4e\u67e5\u8be2\u6548\u7387\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811\u6765\u63d0\u9ad8\u6548\u7387\u3002
  • \u5f00\u653e\u5bfb\u5740\u901a\u8fc7\u591a\u6b21\u63a2\u6d4b\u6765\u5904\u7406\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\uff0c\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\u8f83\u7ebf\u6027\u63a2\u6d4b\u66f4\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\u3002\u4f8b\u5982\uff0cJava \u7684 HashMap \u4f7f\u7528\u94fe\u5f0f\u5730\u5740\uff0c\u800c Python \u7684 Dict \u91c7\u7528\u5f00\u653e\u5bfb\u5740\u3002
  • \u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u6211\u4eec\u5e0c\u671b\u54c8\u5e0c\u7b97\u6cd5\u5177\u6709\u786e\u5b9a\u6027\u3001\u9ad8\u6548\u7387\u548c\u5747\u5300\u5206\u5e03\u7684\u7279\u70b9\u3002\u5728\u5bc6\u7801\u5b66\u4e2d\uff0c\u54c8\u5e0c\u7b97\u6cd5\u8fd8\u5e94\u8be5\u5177\u5907\u6297\u78b0\u649e\u6027\u548c\u96ea\u5d29\u6548\u5e94\u3002
  • \u54c8\u5e0c\u7b97\u6cd5\u901a\u5e38\u91c7\u7528\u5927\u8d28\u6570\u4f5c\u4e3a\u6a21\u6570\uff0c\u4ee5\u6700\u5927\u5316\u5730\u4fdd\u8bc1\u54c8\u5e0c\u503c\u7684\u5747\u5300\u5206\u5e03\uff0c\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\u3002
  • \u5e38\u89c1\u7684\u54c8\u5e0c\u7b97\u6cd5\u5305\u62ec MD5\u3001SHA-1\u3001SHA-2 \u548c SHA3 \u7b49\u3002MD5 \u5e38\u7528\u4e8e\u6821\u9a8c\u6587\u4ef6\u5b8c\u6574\u6027\uff0cSHA-2 \u5e38\u7528\u4e8e\u5b89\u5168\u5e94\u7528\u4e0e\u534f\u8bae\u3002
  • \u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u4f1a\u4e3a\u6570\u636e\u7c7b\u578b\u63d0\u4f9b\u5185\u7f6e\u54c8\u5e0c\u7b97\u6cd5\uff0c\u7528\u4e8e\u8ba1\u7b97\u54c8\u5e0c\u8868\u4e2d\u7684\u6876\u7d22\u5f15\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u53ea\u6709\u4e0d\u53ef\u53d8\u5bf9\u8c61\u662f\u53ef\u54c8\u5e0c\u7684\u3002
"},{"location":"chapter_hashing/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\u4ec0\u4e48\u4e0d\u662f \\(O(n)\\) \uff1f

\u5f53\u54c8\u5e0c\u51b2\u7a81\u6bd4\u8f83\u4e25\u91cd\u65f6\uff0c\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f1a\u9000\u5316\u81f3 \\(O(n)\\) \u3002\u5f53\u54c8\u5e0c\u51fd\u6570\u8bbe\u8ba1\u7684\u6bd4\u8f83\u597d\u3001\u5bb9\u91cf\u8bbe\u7f6e\u6bd4\u8f83\u5408\u7406\u3001\u51b2\u7a81\u6bd4\u8f83\u5e73\u5747\u65f6\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u3002\u6211\u4eec\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u5185\u7f6e\u7684\u54c8\u5e0c\u8868\u65f6\uff0c\u901a\u5e38\u8ba4\u4e3a\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u3002

\u4e3a\u4ec0\u4e48\u4e0d\u4f7f\u7528\u54c8\u5e0c\u51fd\u6570 \\(f(x) = x\\) \u5462\uff1f\u8fd9\u6837\u5c31\u4e0d\u4f1a\u6709\u51b2\u7a81\u4e86

\u5728 \\(f(x) = x\\) \u54c8\u5e0c\u51fd\u6570\u4e0b\uff0c\u6bcf\u4e2a\u5143\u7d20\u5bf9\u5e94\u552f\u4e00\u7684\u6876\u7d22\u5f15\uff0c\u8fd9\u4e0e\u6570\u7ec4\u7b49\u4ef7\u3002\u7136\u800c\uff0c\u8f93\u5165\u7a7a\u95f4\u901a\u5e38\u8fdc\u5927\u4e8e\u8f93\u51fa\u7a7a\u95f4\uff08\u6570\u7ec4\u957f\u5ea6\uff09\uff0c\u56e0\u6b64\u54c8\u5e0c\u51fd\u6570\u7684\u6700\u540e\u4e00\u6b65\u5f80\u5f80\u662f\u5bf9\u6570\u7ec4\u957f\u5ea6\u53d6\u6a21\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u54c8\u5e0c\u8868\u7684\u76ee\u6807\u662f\u5c06\u4e00\u4e2a\u8f83\u5927\u7684\u72b6\u6001\u7a7a\u95f4\u6620\u5c04\u5230\u4e00\u4e2a\u8f83\u5c0f\u7684\u7a7a\u95f4\uff0c\u5e76\u63d0\u4f9b \\(O(1)\\) \u7684\u67e5\u8be2\u6548\u7387\u3002

\u54c8\u5e0c\u8868\u5e95\u5c42\u5b9e\u73b0\u662f\u6570\u7ec4\u3001\u94fe\u8868\u3001\u4e8c\u53c9\u6811\uff0c\u4f46\u4e3a\u4ec0\u4e48\u6548\u7387\u53ef\u4ee5\u6bd4\u4ed6\u4eec\u66f4\u9ad8\u5462\uff1f

\u9996\u5148\uff0c\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u6548\u7387\u53d8\u9ad8\uff0c\u4f46\u7a7a\u95f4\u6548\u7387\u53d8\u4f4e\u4e86\u3002\u54c8\u5e0c\u8868\u6709\u76f8\u5f53\u4e00\u90e8\u5206\u7684\u5185\u5b58\u662f\u672a\u4f7f\u7528\u7684\uff0c

\u5176\u6b21\uff0c\u53ea\u662f\u5728\u7279\u5b9a\u4f7f\u7528\u573a\u666f\u4e0b\u65f6\u95f4\u6548\u7387\u53d8\u9ad8\u4e86\u3002\u5982\u679c\u4e00\u4e2a\u529f\u80fd\u80fd\u591f\u5728\u76f8\u540c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e0b\u4f7f\u7528\u6570\u7ec4\u6216\u94fe\u8868\u5b9e\u73b0\uff0c\u90a3\u4e48\u901a\u5e38\u6bd4\u54c8\u5e0c\u8868\u66f4\u5feb\u3002\u8fd9\u662f\u56e0\u4e3a\u54c8\u5e0c\u51fd\u6570\u8ba1\u7b97\u9700\u8981\u5f00\u9500\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u7684\u5e38\u6570\u9879\u66f4\u5927\u3002

\u6700\u540e\uff0c\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u80fd\u53d1\u751f\u52a3\u5316\u3002\u4f8b\u5982\u5728\u94fe\u5f0f\u5730\u5740\u4e2d\uff0c\u6211\u4eec\u91c7\u53d6\u5728\u94fe\u8868\u6216\u7ea2\u9ed1\u6811\u4e2d\u6267\u884c\u67e5\u627e\u64cd\u4f5c\uff0c\u4ecd\u7136\u6709\u9000\u5316\u81f3 \\(O(n)\\) \u65f6\u95f4\u7684\u98ce\u9669\u3002

\u591a\u6b21\u54c8\u5e0c\u6709\u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5143\u7d20\u7684\u7f3a\u9677\u5417\uff1f\u5bf9\u4e8e\u6807\u8bb0\u5df2\u5220\u9664\u7684\u7a7a\u95f4\uff0c\u8fd9\u4e2a\u7a7a\u95f4\u8fd8\u80fd\u518d\u6b21\u4f7f\u7528\u5417\uff1f

\u591a\u6b21\u54c8\u5e0c\u662f\u5f00\u653e\u5bfb\u5740\u7684\u4e00\u79cd\uff0c\u5f00\u653e\u5bfb\u5740\u6cd5\u90fd\u6709\u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5143\u7d20\u7684\u7f3a\u9677\uff0c\u9700\u8981\u901a\u8fc7\u6807\u8bb0\u5220\u9664\u3002\u88ab\u6807\u8bb0\u4e3a\u5df2\u5220\u9664\u7684\u7a7a\u95f4\u662f\u53ef\u4ee5\u518d\u6b21\u88ab\u4f7f\u7528\u7684\u3002\u5f53\u5c06\u65b0\u5143\u7d20\u63d2\u5165\u54c8\u5e0c\u8868\uff0c\u5e76\u4e14\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u627e\u5230\u4e86\u88ab\u6807\u8bb0\u4e3a\u5df2\u5220\u9664\u7684\u4f4d\u7f6e\u65f6\uff0c\u8be5\u4f4d\u7f6e\u53ef\u4ee5\u88ab\u65b0\u7684\u5143\u7d20\u4f7f\u7528\u3002\u8fd9\u6837\u505a\u65e2\u80fd\u4fdd\u6301\u54c8\u5e0c\u8868\u7684\u63a2\u6d4b\u5e8f\u5217\u4e0d\u53d8\uff0c\u53c8\u80fd\u4fdd\u8bc1\u54c8\u5e0c\u8868\u7684\u7a7a\u95f4\u4f7f\u7528\u7387\u3002

\u4e3a\u4ec0\u4e48\u5728\u7ebf\u6027\u63a2\u6d4b\u4e2d\uff0c\u67e5\u627e\u5143\u7d20\u7684\u65f6\u5019\u4f1a\u51fa\u73b0\u54c8\u5e0c\u51b2\u7a81\u5462\uff1f

\u67e5\u627e\u7684\u65f6\u5019\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u627e\u5230\u5bf9\u5e94\u7684\u6876\u548c\u952e\u503c\u5bf9\uff0c\u53d1\u73b0 key \u4e0d\u5339\u914d\uff0c\u8fd9\u5c31\u4ee3\u8868\u6709\u54c8\u5e0c\u51b2\u7a81\u3002\u56e0\u6b64\uff0c\u7ebf\u6027\u63a2\u6d4b\u6cd5\u4f1a\u6839\u636e\u9884\u5148\u8bbe\u5b9a\u7684\u6b65\u957f\u4f9d\u6b21\u5411\u4e0b\u67e5\u627e\uff0c\u76f4\u81f3\u627e\u5230\u6b63\u786e\u7684\u952e\u503c\u5bf9\u6216\u65e0\u6cd5\u627e\u5230\u8df3\u51fa\u4e3a\u6b62\u3002

\u4e3a\u4ec0\u4e48\u54c8\u5e0c\u8868\u6269\u5bb9\u80fd\u591f\u7f13\u89e3\u54c8\u5e0c\u51b2\u7a81\uff1f

\u54c8\u5e0c\u51fd\u6570\u7684\u6700\u540e\u4e00\u6b65\u5f80\u5f80\u662f\u5bf9\u6570\u7ec4\u957f\u5ea6 \\(n\\) \u53d6\u4f59\uff0c\u8ba9\u8f93\u51fa\u503c\u843d\u5165\u5728\u6570\u7ec4\u7d22\u5f15\u8303\u56f4\uff1b\u5728\u6269\u5bb9\u540e\uff0c\u6570\u7ec4\u957f\u5ea6 \\(n\\) \u53d1\u751f\u53d8\u5316\uff0c\u800c key \u5bf9\u5e94\u7684\u7d22\u5f15\u4e5f\u53ef\u80fd\u53d1\u751f\u53d8\u5316\u3002\u539f\u5148\u843d\u5728\u540c\u4e00\u4e2a\u6876\u7684\u591a\u4e2a key \uff0c\u5728\u6269\u5bb9\u540e\u53ef\u80fd\u4f1a\u88ab\u5206\u914d\u5230\u591a\u4e2a\u6876\u4e2d\uff0c\u4ece\u800c\u5b9e\u73b0\u54c8\u5e0c\u51b2\u7a81\u7684\u7f13\u89e3\u3002

"},{"location":"chapter_heap/","title":"\u7b2c 8 \u7ae0 \u00a0 \u5806","text":"

Abstract

\u5806\u5c31\u50cf\u662f\u5c71\u5ddd\u7684\u5cf0\u5ce6\uff0c\u5b83\u4eec\u5c42\u53e0\u8d77\u4f0f\u3001\u5f62\u6001\u5404\u5f02\u3002

\u6bcf\u4e00\u5ea7\u5c71\u5cf0\u90fd\u6709\u5176\u9ad8\u4f4e\u4e4b\u5206\uff0c\u800c\u6700\u9ad8\u7684\u5c71\u5cf0\u603b\u662f\u6700\u5148\u6620\u5165\u773c\u5e18\u3002

"},{"location":"chapter_heap/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 8.1 \u00a0 \u5806
  • 8.2 \u00a0 \u5efa\u5806\u64cd\u4f5c
  • 8.3 \u00a0 Top-K \u95ee\u9898
  • 8.4 \u00a0 \u5c0f\u7ed3
"},{"location":"chapter_heap/build_heap/","title":"8.2 \u00a0 \u5efa\u5806\u64cd\u4f5c","text":"

\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u4f7f\u7528\u4e00\u4e2a\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\u6765\u6784\u5efa\u4e00\u4e2a\u5806\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a\u201c\u5efa\u5806\u64cd\u4f5c\u201d\u3002

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

\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u7a7a\u5806\uff0c\u7136\u540e\u904d\u5386\u5217\u8868\uff0c\u4f9d\u6b21\u5bf9\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u201c\u5165\u5806\u64cd\u4f5c\u201d\uff0c\u5373\u5148\u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u5806\u7684\u5c3e\u90e8\uff0c\u518d\u5bf9\u8be5\u5143\u7d20\u6267\u884c\u201c\u4ece\u5e95\u81f3\u9876\u201d\u5806\u5316\u3002

\u6bcf\u5f53\u4e00\u4e2a\u5143\u7d20\u5165\u5806\uff0c\u5806\u7684\u957f\u5ea6\u5c31\u52a0\u4e00\u3002\u7531\u4e8e\u8282\u70b9\u662f\u4ece\u9876\u5230\u5e95\u4f9d\u6b21\u88ab\u6dfb\u52a0\u8fdb\u4e8c\u53c9\u6811\u7684\uff0c\u56e0\u6b64\u5806\u662f\u201c\u81ea\u4e0a\u800c\u4e0b\u201d\u5730\u6784\u5efa\u7684\u3002

\u8bbe\u5143\u7d20\u6570\u91cf\u4e3a \\(n\\) \uff0c\u6bcf\u4e2a\u5143\u7d20\u7684\u5165\u5806\u64cd\u4f5c\u4f7f\u7528 \\(O(\\log{n})\\) \u65f6\u95f4\uff0c\u56e0\u6b64\u8be5\u5efa\u5806\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002

"},{"location":"chapter_heap/build_heap/#822","title":"8.2.2 \u00a0 \u901a\u8fc7\u904d\u5386\u5806\u5316\u5b9e\u73b0","text":"

\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u4e00\u79cd\u66f4\u4e3a\u9ad8\u6548\u7684\u5efa\u5806\u65b9\u6cd5\uff0c\u5171\u5206\u4e3a\u4e24\u6b65\u3002

  1. \u5c06\u5217\u8868\u6240\u6709\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u5230\u5806\u4e2d\uff0c\u6b64\u65f6\u5806\u7684\u6027\u8d28\u5c1a\u672a\u5f97\u5230\u6ee1\u8db3\u3002
  2. \u5012\u5e8f\u904d\u5386\u5806\uff08\u5373\u5c42\u5e8f\u904d\u5386\u7684\u5012\u5e8f\uff09\uff0c\u4f9d\u6b21\u5bf9\u6bcf\u4e2a\u975e\u53f6\u8282\u70b9\u6267\u884c\u201c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u201d\u3002

\u6bcf\u5f53\u5806\u5316\u4e00\u4e2a\u8282\u70b9\u540e\uff0c\u4ee5\u8be5\u8282\u70b9\u4e3a\u6839\u8282\u70b9\u7684\u5b50\u6811\u5c31\u5f62\u6210\u4e00\u4e2a\u5408\u6cd5\u7684\u5b50\u5806\u3002\u800c\u7531\u4e8e\u662f\u5012\u5e8f\u904d\u5386\uff0c\u56e0\u6b64\u5806\u662f\u201c\u81ea\u4e0b\u800c\u4e0a\u201d\u5730\u88ab\u6784\u5efa\u7684\u3002

\u4e4b\u6240\u4ee5\u9009\u62e9\u5012\u5e8f\u904d\u5386\uff0c\u662f\u56e0\u4e3a\u8fd9\u6837\u80fd\u591f\u4fdd\u8bc1\u5f53\u524d\u8282\u70b9\u4e4b\u4e0b\u7684\u5b50\u6811\u5df2\u7ecf\u662f\u5408\u6cd5\u7684\u5b50\u5806\uff0c\u8fd9\u6837\u5806\u5316\u5f53\u524d\u8282\u70b9\u624d\u662f\u6709\u6548\u7684\u3002

\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u53f6\u8282\u70b9\u6ca1\u6709\u5b50\u8282\u70b9\uff0c\u5929\u7136\u5c31\u662f\u5408\u6cd5\u7684\u5b50\u5806\uff0c\u56e0\u6b64\u65e0\u9700\u5806\u5316\u3002\u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u6700\u540e\u4e00\u4e2a\u975e\u53f6\u8282\u70b9\u662f\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u7684\u7236\u8282\u70b9\uff0c\u6211\u4eec\u4ece\u5b83\u5f00\u59cb\u5012\u5e8f\u904d\u5386\u5e76\u6267\u884c\u5806\u5316\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig my_heap.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.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.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.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.go
/* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u5207\u7247\u5efa\u5806 */\nfunc newMaxHeap(nums []any) *maxHeap {\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\nh := &maxHeap{data: nums}\nfor i := h.parent(len(h.data) - 1); i >= 0; i-- {\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nh.siftDown(i)\n}\nreturn h\n}\n
my_heap.swift
/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\ninit(nums: [Int]) {\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\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.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.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--) {\nsiftDown(i);\n}\n}\n
my_heap.rs
/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nfn new(nums: Vec<i32>) -> Self {\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\nlet mut heap = MaxHeap { max_heap: nums };\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor i in (0..=Self::parent(heap.size() - 1)).rev() {\nheap.sift_down(i);\n}\nheap\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 = parent(h, 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.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
"},{"location":"chapter_heap/build_heap/#823","title":"8.2.3 \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"

\u4e0b\u9762\uff0c\u6211\u4eec\u6765\u5c1d\u8bd5\u63a8\u7b97\u7b2c\u4e8c\u79cd\u5efa\u5806\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002

  • \u5047\u8bbe\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u5219\u53f6\u8282\u70b9\u6570\u91cf\u4e3a \\((n + 1) / 2\\) \uff0c\u5176\u4e2d \\(/\\) \u4e3a\u5411\u4e0b\u6574\u9664\u3002\u56e0\u6b64\u9700\u8981\u5806\u5316\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\((n - 1) / 2\\) \u3002
  • \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 \\(\\log n\\) \u3002

\u5c06\u4e0a\u8ff0\u4e24\u8005\u76f8\u4e58\uff0c\u53ef\u5f97\u5230\u5efa\u5806\u8fc7\u7a0b\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002\u4f46\u8fd9\u4e2a\u4f30\u7b97\u7ed3\u679c\u5e76\u4e0d\u51c6\u786e\uff0c\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u8003\u8651\u5230\u4e8c\u53c9\u6811\u5e95\u5c42\u8282\u70b9\u6570\u91cf\u8fdc\u591a\u4e8e\u9876\u5c42\u8282\u70b9\u7684\u6027\u8d28\u3002

\u63a5\u4e0b\u6765\u6211\u4eec\u6765\u8fdb\u884c\u66f4\u4e3a\u51c6\u786e\u7684\u8ba1\u7b97\u3002\u4e3a\u4e86\u51cf\u5c0f\u8ba1\u7b97\u96be\u5ea6\uff0c\u5047\u8bbe\u7ed9\u5b9a\u4e00\u4e2a\u8282\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u9ad8\u5ea6\u4e3a \\(h\\) \u7684\u201c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u201d\uff0c\u8be5\u5047\u8bbe\u4e0d\u4f1a\u5f71\u54cd\u8ba1\u7b97\u7ed3\u679c\u7684\u6b63\u786e\u6027\u3002

\u56fe 8-5 \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u5404\u5c42\u8282\u70b9\u6570\u91cf

\u5982\u56fe 8-5 \u6240\u793a\uff0c\u8282\u70b9\u201c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u201d\u7684\u6700\u5927\u8fed\u4ee3\u6b21\u6570\u7b49\u4e8e\u8be5\u8282\u70b9\u5230\u53f6\u8282\u70b9\u7684\u8ddd\u79bb\uff0c\u800c\u8be5\u8ddd\u79bb\u6b63\u662f\u201c\u8282\u70b9\u9ad8\u5ea6\u201d\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\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) + \\dots + 2^{(h-1)}\\times1 \\]

\u5316\u7b80\u4e0a\u5f0f\u9700\u8981\u501f\u52a9\u4e2d\u5b66\u7684\u6570\u5217\u77e5\u8bc6\uff0c\u5148\u5bf9 \\(T(h)\\) \u4e58\u4ee5 \\(2\\) \uff0c\u5f97\u5230\uff1a

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

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

\\[ 2T(h) - T(h) = T(h) = -2^0h + 2^1 + 2^2 + \\dots + 2^{h-1} + 2^h \\]

\u89c2\u5bdf\u4e0a\u5f0f\uff0c\u53d1\u73b0 \\(T(h)\\) \u662f\u4e00\u4e2a\u7b49\u6bd4\u6570\u5217\uff0c\u53ef\u76f4\u63a5\u4f7f\u7528\u6c42\u548c\u516c\u5f0f\uff0c\u5f97\u5230\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\uff1a

\\[ \\begin{aligned} T(h) & = 2 \\frac{1 - 2^h}{1 - 2} - h \\newline & = 2^{h+1} - h - 2 \\newline & = O(2^h) \\end{aligned} \\]

\u8fdb\u4e00\u6b65\u5730\uff0c\u9ad8\u5ea6\u4e3a \\(h\\) \u7684\u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(n = 2^{h+1} - 1\\) \uff0c\u6613\u5f97\u590d\u6742\u5ea6\u4e3a \\(O(2^h) = O(n)\\) \u3002\u4ee5\u4e0a\u63a8\u7b97\u8868\u660e\uff0c\u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u975e\u5e38\u9ad8\u6548\u3002

"},{"location":"chapter_heap/heap/","title":"8.1 \u00a0 \u5806","text":"

\u300c\u5806 heap\u300d\u662f\u4e00\u79cd\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u4e3b\u8981\u53ef\u5206\u4e3a\u56fe 8-1 \u6240\u793a\u7684\u4e24\u79cd\u7c7b\u578b\u3002

  • \u300c\u5927\u9876\u5806 max heap\u300d\uff1a\u4efb\u610f\u8282\u70b9\u7684\u503c \\(\\geq\\) \u5176\u5b50\u8282\u70b9\u7684\u503c\u3002
  • \u300c\u5c0f\u9876\u5806 min heap\u300d\uff1a\u4efb\u610f\u8282\u70b9\u7684\u503c \\(\\leq\\) \u5176\u5b50\u8282\u70b9\u7684\u503c\u3002

\u56fe 8-1 \u00a0 \u5c0f\u9876\u5806\u4e0e\u5927\u9876\u5806

\u5806\u4f5c\u4e3a\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u4e00\u4e2a\u7279\u4f8b\uff0c\u5177\u6709\u4ee5\u4e0b\u7279\u6027\u3002

  • \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\u201c\u5806\u9876\u201d\uff0c\u5c06\u5e95\u5c42\u6700\u9760\u53f3\u7684\u8282\u70b9\u79f0\u4e3a\u201c\u5806\u5e95\u201d\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\u201c\u4f18\u5148\u961f\u5217\u201d\u548c\u201c\u5806\u201d\u770b\u4f5c\u7b49\u4ef7\u7684\u6570\u636e\u7ed3\u6784\u3002\u56e0\u6b64\uff0c\u672c\u4e66\u5bf9\u4e24\u8005\u4e0d\u505a\u7279\u522b\u533a\u5206\uff0c\u7edf\u4e00\u4f7f\u7528\u201c\u5806\u201c\u6765\u547d\u540d\u3002

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

\u8868 8-1 \u00a0 \u5806\u7684\u64cd\u4f5c\u6548\u7387

\u65b9\u6cd5\u540d \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 push() \u5143\u7d20\u5165\u5806 \\(O(\\log n)\\) pop() \u5806\u9876\u5143\u7d20\u51fa\u5806 \\(O(\\log n)\\) peek() \u8bbf\u95ee\u5806\u9876\u5143\u7d20\uff08\u5927 / \u5c0f\u9876\u5806\u5206\u522b\u4e3a\u6700\u5927 / \u5c0f\u503c\uff09 \\(O(1)\\) size() \u83b7\u53d6\u5806\u7684\u5143\u7d20\u6570\u91cf \\(O(1)\\) isEmpty() \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a \\(O(1)\\)

\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b\u7684\u5806\u7c7b\uff08\u6216\u4f18\u5148\u961f\u5217\u7c7b\uff09\u3002

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

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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 = maxHeap.poll(); // 5\npeek = maxHeap.poll(); // 4\npeek = maxHeap.poll(); // 3\npeek = maxHeap.poll(); // 2\npeek = maxHeap.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.cs
/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nPriorityQueue<int, int> minHeap = new();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\uff08\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u4fee\u6539 Comparator \u5373\u53ef\uff09\nPriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => y - x));\n/* \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.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.swift
// Swift \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.js
// JavaScript \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.ts
// TypeScript \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.dart
// Dart \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.rs
use std::collections::BinaryHeap;\nuse std::cmp::Reverse;\n/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nlet mut min_heap = BinaryHeap::<Reverse<i32>>::new();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\nlet mut max_heap = BinaryHeap::new();\n/* \u5143\u7d20\u5165\u5806 */\nmax_heap.push(1);\nmax_heap.push(3);\nmax_heap.push(2);\nmax_heap.push(5);\nmax_heap.push(4);\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nlet peek = max_heap.peek().unwrap();  // 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\nlet peek = max_heap.pop().unwrap();   // 5\nlet peek = max_heap.pop().unwrap();   // 4\nlet peek = max_heap.pop().unwrap();   // 3\nlet peek = max_heap.pop().unwrap();   // 2\nlet peek = max_heap.pop().unwrap();   // 1\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nlet size = max_heap.len();\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = max_heap.is_empty();\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nlet min_heap = BinaryHeap::from(vec![Reverse(1), Reverse(3), Reverse(2), Reverse(5), Reverse(4)]);\n
heap.c
// C \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.zig
\n
"},{"location":"chapter_heap/heap/#812","title":"8.1.2 \u00a0 \u5806\u7684\u5b9e\u73b0","text":"

\u4e0b\u6587\u5b9e\u73b0\u7684\u662f\u5927\u9876\u5806\u3002\u82e5\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u5c0f\u9876\u5806\uff0c\u53ea\u9700\u5c06\u6240\u6709\u5927\u5c0f\u903b\u8f91\u5224\u65ad\u53d6\u9006\uff08\u4f8b\u5982\uff0c\u5c06 \\(\\geq\\) \u66ff\u6362\u4e3a \\(\\leq\\) \uff09\u3002\u611f\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u5b9e\u73b0\u3002

"},{"location":"chapter_heap/heap/#1","title":"1. \u00a0 \u5806\u7684\u5b58\u50a8\u4e0e\u8868\u793a","text":"

\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

\u5982\u56fe 8-2 \u6240\u793a\uff0c\u7ed9\u5b9a\u7d22\u5f15 \\(i\\) \uff0c\u5176\u5de6\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 1\\) \uff0c\u53f3\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 2\\) \uff0c\u7236\u8282\u70b9\u7d22\u5f15\u4e3a \\((i - 1) / 2\\)\uff08\u5411\u4e0b\u53d6\u6574\uff09\u3002\u5f53\u7d22\u5f15\u8d8a\u754c\u65f6\uff0c\u8868\u793a\u7a7a\u8282\u70b9\u6216\u8282\u70b9\u4e0d\u5b58\u5728\u3002

\u56fe 8-2 \u00a0 \u5806\u7684\u8868\u793a\u4e0e\u5b58\u50a8

\u6211\u4eec\u53ef\u4ee5\u5c06\u7d22\u5f15\u6620\u5c04\u516c\u5f0f\u5c01\u88c5\u6210\u51fd\u6570\uff0c\u65b9\u4fbf\u540e\u7eed\u4f7f\u7528\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
my_heap.rs
/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nfn left(i: usize) -> usize {\n2 * i + 1\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nfn right(i: usize) -> usize {\n2 * i + 2\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nfn parent(i: usize) -> usize {\n(i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.c
/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint left(maxHeap *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.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
"},{"location":"chapter_heap/heap/#2","title":"2. \u00a0 \u8bbf\u95ee\u5806\u9876\u5143\u7d20","text":"

\u5806\u9876\u5143\u7d20\u5373\u4e3a\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\uff0c\u4e5f\u5c31\u662f\u5217\u8868\u7684\u9996\u4e2a\u5143\u7d20\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig my_heap.py
def peek(self) -> int:\n\"\"\"\u8bbf\u95ee\u5806\u9876\u5143\u7d20\"\"\"\nreturn self.max_heap[0]\n
my_heap.cpp
/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\nreturn maxHeap[0];\n}\n
my_heap.java
/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\nreturn maxHeap.get(0);\n}\n
my_heap.cs
/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint Peek() {\nreturn maxHeap[0];\n}\n
my_heap.go
/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunc (h *maxHeap) peek() any {\nreturn h.data[0]\n}\n
my_heap.swift
/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunc peek() -> Int {\nmaxHeap[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.dart
/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\nreturn _maxHeap[0];\n}\n
my_heap.rs
/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfn peek(&self) -> Option<i32> {\nself.max_heap.first().copied()\n}\n
my_heap.c
/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek(maxHeap *h) {\nreturn h->data[0];\n}\n
my_heap.zig
// \u8bbf\u95ee\u5806\u9876\u5143\u7d20\nfn peek(self: *Self) T {\nreturn self.max_heap.?.items[0];\n}  
"},{"location":"chapter_heap/heap/#3","title":"3. \u00a0 \u5143\u7d20\u5165\u5806","text":"

\u7ed9\u5b9a\u5143\u7d20 val \uff0c\u6211\u4eec\u9996\u5148\u5c06\u5176\u6dfb\u52a0\u5230\u5806\u5e95\u3002\u6dfb\u52a0\u4e4b\u540e\uff0c\u7531\u4e8e val \u53ef\u80fd\u5927\u4e8e\u5806\u4e2d\u5176\u4ed6\u5143\u7d20\uff0c\u5806\u7684\u6210\u7acb\u6761\u4ef6\u53ef\u80fd\u5df2\u88ab\u7834\u574f\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\u5982\u56fe 8-3 \u6240\u793a\uff0c\u6211\u4eec\u6bd4\u8f83\u63d2\u5165\u8282\u70b9\u4e0e\u5176\u7236\u8282\u70b9\u7684\u503c\uff0c\u5982\u679c\u63d2\u5165\u8282\u70b9\u66f4\u5927\uff0c\u5219\u5c06\u5b83\u4eec\u4ea4\u6362\u3002\u7136\u540e\u7ee7\u7eed\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u4ece\u5e95\u81f3\u9876\u4fee\u590d\u5806\u4e2d\u7684\u5404\u4e2a\u8282\u70b9\uff0c\u76f4\u81f3\u8d8a\u8fc7\u6839\u8282\u70b9\u6216\u9047\u5230\u65e0\u987b\u4ea4\u6362\u7684\u8282\u70b9\u65f6\u7ed3\u675f\u3002

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

\u56fe 8-3 \u00a0 \u5143\u7d20\u5165\u5806\u6b65\u9aa4

\u8bbe\u8282\u70b9\u603b\u6570\u4e3a \\(n\\) \uff0c\u5219\u6811\u7684\u9ad8\u5ea6\u4e3a \\(O(\\log n)\\) \u3002\u7531\u6b64\u53ef\u77e5\uff0c\u5806\u5316\u64cd\u4f5c\u7684\u5faa\u73af\u8f6e\u6570\u6700\u591a\u4e3a \\(O(\\log n)\\) \uff0c\u5143\u7d20\u5165\u5806\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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\u987b\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.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\u987b\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.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\u987b\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.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\u987b\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.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\u987b\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.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\u987b\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.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\u987b\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\u987b\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.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\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\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p < 0 || _maxHeap[i] <= _maxHeap[p]) {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\n_swap(i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n
my_heap.rs
/* \u5143\u7d20\u5165\u5806 */\nfn push(&mut self, val: i32) {\n// \u6dfb\u52a0\u8282\u70b9\nself.max_heap.push(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nself.sift_up(self.size() - 1);\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfn sift_up(&mut self, mut i: usize) {\nloop {\n// \u8282\u70b9 i \u5df2\u7ecf\u662f\u5806\u9876\u8282\u70b9\u4e86\uff0c\u7ed3\u675f\u5806\u5316\nif i == 0 {\nbreak;\n}\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nlet p = Self::parent(i);\n// \u5f53\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif self.max_heap[i] <= self.max_heap[p] {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nself.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\u987b\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.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\u987b\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
"},{"location":"chapter_heap/heap/#4","title":"4. \u00a0 \u5806\u9876\u5143\u7d20\u51fa\u5806","text":"

\u5806\u9876\u5143\u7d20\u662f\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\uff0c\u5373\u5217\u8868\u9996\u5143\u7d20\u3002\u5982\u679c\u6211\u4eec\u76f4\u63a5\u4ece\u5217\u8868\u4e2d\u5220\u9664\u9996\u5143\u7d20\uff0c\u90a3\u4e48\u4e8c\u53c9\u6811\u4e2d\u6240\u6709\u8282\u70b9\u7684\u7d22\u5f15\u90fd\u4f1a\u53d1\u751f\u53d8\u5316\uff0c\u8fd9\u5c06\u4f7f\u5f97\u540e\u7eed\u4f7f\u7528\u5806\u5316\u4fee\u590d\u53d8\u5f97\u56f0\u96be\u3002\u4e3a\u4e86\u5c3d\u91cf\u51cf\u5c11\u5143\u7d20\u7d22\u5f15\u7684\u53d8\u52a8\uff0c\u6211\u4eec\u91c7\u7528\u4ee5\u4e0b\u64cd\u4f5c\u6b65\u9aa4\u3002

  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\u3002
  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\u3002
  3. \u4ece\u6839\u8282\u70b9\u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u6267\u884c\u5806\u5316\u3002

\u5982\u56fe 8-4 \u6240\u793a\uff0c\u201c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u201d\u7684\u64cd\u4f5c\u65b9\u5411\u4e0e\u201c\u4ece\u5e95\u81f3\u9876\u5806\u5316\u201d\u76f8\u53cd\uff0c\u6211\u4eec\u5c06\u6839\u8282\u70b9\u7684\u503c\u4e0e\u5176\u4e24\u4e2a\u5b50\u8282\u70b9\u7684\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u5c06\u6700\u5927\u7684\u5b50\u8282\u70b9\u4e0e\u6839\u8282\u70b9\u4ea4\u6362\u3002\u7136\u540e\u5faa\u73af\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u76f4\u5230\u8d8a\u8fc7\u53f6\u8282\u70b9\u6216\u9047\u5230\u65e0\u987b\u4ea4\u6362\u7684\u8282\u70b9\u65f6\u7ed3\u675f\u3002

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

\u56fe 8-4 \u00a0 \u5806\u9876\u5143\u7d20\u51fa\u5806\u6b65\u9aa4

\u4e0e\u5143\u7d20\u5165\u5806\u64cd\u4f5c\u76f8\u4f3c\uff0c\u5806\u9876\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u4e3a \\(O(\\log n)\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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\u987b\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.cpp
/* \u5143\u7d20\u51fa\u5806 */\nvoid pop() {\n// \u5224\u7a7a\u5904\u7406\nif (isEmpty()) {\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;\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\u987b\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.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\u987b\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.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.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\u987b\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.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\u987b\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.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\u987b\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\u987b\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.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\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);\nint r = _right(i);\nint ma = i;\nif (l < size() && _maxHeap[l] > _maxHeap[ma]) ma = l;\nif (r < size() && _maxHeap[r] > _maxHeap[ma]) ma = r;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\n_swap(i, ma);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n
my_heap.rs
/* \u5143\u7d20\u51fa\u5806 */\nfn pop(&mut self) -> i32 {\n// \u5224\u7a7a\u5904\u7406\nif self.is_empty() {\npanic!(\"index out of bounds\");\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\nself.swap(0, self.size() - 1);\n// \u5220\u9664\u8282\u70b9\nlet val = self.max_heap.remove(self.size() - 1);\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nself.sift_down(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nval\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfn sift_down(&mut self, mut i: usize) {\nloop {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nlet (l, r, mut ma) = (Self::left(i), Self::right(i), i);\nif l < self.size() && self.max_heap[l] > self.max_heap[ma] {\nma = l;\n}\nif r < self.size() && self.max_heap[r] > self.max_heap[ma] {\nma = r;\n}\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif ma == i {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nself.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\u987b\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.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\u987b\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
"},{"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":""},{"location":"chapter_heap/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","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
  • Top-K \u662f\u4e00\u4e2a\u7ecf\u5178\u7b97\u6cd5\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u5806\u6570\u636e\u7ed3\u6784\u9ad8\u6548\u89e3\u51b3\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log k)\\) \u3002
"},{"location":"chapter_heap/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

\u6570\u636e\u7ed3\u6784\u7684\u201c\u5806\u201d\u4e0e\u5185\u5b58\u7ba1\u7406\u7684\u201c\u5806\u201d\u662f\u540c\u4e00\u4e2a\u6982\u5ff5\u5417\uff1f

\u4e24\u8005\u4e0d\u662f\u540c\u4e00\u4e2a\u6982\u5ff5\uff0c\u53ea\u662f\u78b0\u5de7\u90fd\u53eb\u5806\u3002\u8ba1\u7b97\u673a\u7cfb\u7edf\u5185\u5b58\u4e2d\u7684\u5806\u662f\u52a8\u6001\u5185\u5b58\u5206\u914d\u7684\u4e00\u90e8\u5206\uff0c\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5b58\u50a8\u6570\u636e\u3002\u7a0b\u5e8f\u53ef\u4ee5\u8bf7\u6c42\u4e00\u5b9a\u91cf\u7684\u5806\u5185\u5b58\uff0c\u7528\u4e8e\u5b58\u50a8\u5982\u5bf9\u8c61\u548c\u6570\u7ec4\u7b49\u590d\u6742\u7ed3\u6784\u3002\u5f53\u8fd9\u4e9b\u6570\u636e\u4e0d\u518d\u9700\u8981\u65f6\uff0c\u7a0b\u5e8f\u9700\u8981\u91ca\u653e\u8fd9\u4e9b\u5185\u5b58\uff0c\u4ee5\u9632\u6b62\u5185\u5b58\u6cc4\u9732\u3002\u76f8\u8f83\u4e8e\u6808\u5185\u5b58\uff0c\u5806\u5185\u5b58\u7684\u7ba1\u7406\u548c\u4f7f\u7528\u9700\u8981\u66f4\u8c28\u614e\uff0c\u4e0d\u6070\u5f53\u7684\u4f7f\u7528\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u9732\u548c\u91ce\u6307\u9488\u7b49\u95ee\u9898\u3002

"},{"location":"chapter_heap/top_k/","title":"8.3 \u00a0 Top-K \u95ee\u9898","text":"

Question

\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u65e0\u5e8f\u6570\u7ec4 nums \uff0c\u8bf7\u8fd4\u56de\u6570\u7ec4\u4e2d\u524d \\(k\\) \u5927\u7684\u5143\u7d20\u3002

\u5bf9\u4e8e\u8be5\u95ee\u9898\uff0c\u6211\u4eec\u5148\u4ecb\u7ecd\u4e24\u79cd\u601d\u8def\u6bd4\u8f83\u76f4\u63a5\u7684\u89e3\u6cd5\uff0c\u518d\u4ecb\u7ecd\u6548\u7387\u66f4\u9ad8\u7684\u5806\u89e3\u6cd5\u3002

"},{"location":"chapter_heap/top_k/#831","title":"8.3.1 \u00a0 \u65b9\u6cd5\u4e00\uff1a\u904d\u5386\u9009\u62e9","text":"

\u6211\u4eec\u53ef\u4ee5\u8fdb\u884c\u56fe 8-6 \u6240\u793a\u7684 \\(k\\) \u8f6e\u904d\u5386\uff0c\u5206\u522b\u5728\u6bcf\u8f6e\u4e2d\u63d0\u53d6\u7b2c \\(1\\)\u3001\\(2\\)\u3001\\(\\dots\\)\u3001\\(k\\) \u5927\u7684\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nk)\\) \u3002

\u6b64\u65b9\u6cd5\u53ea\u9002\u7528\u4e8e \\(k \\ll n\\) \u7684\u60c5\u51b5\uff0c\u56e0\u4e3a\u5f53 \\(k\\) \u4e0e \\(n\\) \u6bd4\u8f83\u63a5\u8fd1\u65f6\uff0c\u5176\u65f6\u95f4\u590d\u6742\u5ea6\u8d8b\u5411\u4e8e \\(O(n^2)\\) \uff0c\u975e\u5e38\u8017\u65f6\u3002

\u56fe 8-6 \u00a0 \u904d\u5386\u5bfb\u627e\u6700\u5927\u7684 k \u4e2a\u5143\u7d20

Tip

\u5f53 \\(k = n\\) \u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u5b8c\u6574\u7684\u6709\u5e8f\u5e8f\u5217\uff0c\u6b64\u65f6\u7b49\u4ef7\u4e8e\u201c\u9009\u62e9\u6392\u5e8f\u201d\u7b97\u6cd5\u3002

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

\u5982\u56fe 8-7 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u5bf9\u6570\u7ec4 nums \u8fdb\u884c\u6392\u5e8f\uff0c\u518d\u8fd4\u56de\u6700\u53f3\u8fb9\u7684 \\(k\\) \u4e2a\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002

\u663e\u7136\uff0c\u8be5\u65b9\u6cd5\u201c\u8d85\u989d\u201d\u5b8c\u6210\u4efb\u52a1\u4e86\uff0c\u56e0\u4e3a\u6211\u4eec\u53ea\u9700\u8981\u627e\u51fa\u6700\u5927\u7684 \\(k\\) \u4e2a\u5143\u7d20\u5373\u53ef\uff0c\u800c\u4e0d\u9700\u8981\u6392\u5e8f\u5176\u4ed6\u5143\u7d20\u3002

\u56fe 8-7 \u00a0 \u6392\u5e8f\u5bfb\u627e\u6700\u5927\u7684 k \u4e2a\u5143\u7d20

"},{"location":"chapter_heap/top_k/#833","title":"8.3.3 \u00a0 \u65b9\u6cd5\u4e09\uff1a\u5806","text":"

\u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e\u5806\u66f4\u52a0\u9ad8\u6548\u5730\u89e3\u51b3 Top-K \u95ee\u9898\uff0c\u6d41\u7a0b\u5982\u56fe 8-8 \u6240\u793a\u3002

  1. \u521d\u59cb\u5316\u4e00\u4e2a\u5c0f\u9876\u5806\uff0c\u5176\u5806\u9876\u5143\u7d20\u6700\u5c0f\u3002
  2. \u5148\u5c06\u6570\u7ec4\u7684\u524d \\(k\\) \u4e2a\u5143\u7d20\u4f9d\u6b21\u5165\u5806\u3002
  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\u3002
  4. \u904d\u5386\u5b8c\u6210\u540e\uff0c\u5806\u4e2d\u4fdd\u5b58\u7684\u5c31\u662f\u6700\u5927\u7684 \\(k\\) \u4e2a\u5143\u7d20\u3002
<1><2><3><4><5><6><7><8><9>

\u56fe 8-8 \u00a0 \u57fa\u4e8e\u5806\u5bfb\u627e\u6700\u5927\u7684 k \u4e2a\u5143\u7d20

\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

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.offer(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.offer(nums[i]);\n}\n}\nreturn heap;\n}\n
top_k.cs
/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nPriorityQueue<int, int> TopKHeap(int[] nums, int k) {\nPriorityQueue<int, int> heap = new();\n// \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nfor (int i = 0; i < k; i++) {\nheap.Enqueue(nums[i], nums[i]);\n}\n// \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\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.Dequeue();\nheap.Enqueue(nums[i], nums[i]);\n}\n}\nreturn heap;\n}\n
top_k.go
/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunc topKHeap(nums []int, k int) *minHeap {\nh := &minHeap{}\nheap.Init(h)\n// \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nfor i := 0; i < k; i++ {\nheap.Push(h, nums[i])\n}\n// \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\nfor i := k; i < len(nums); i++ {\n// \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\nif nums[i] > h.Top().(int) {\nheap.Pop(h)\nheap.Push(h, nums[i])\n}\n}\nreturn h\n}\n
top_k.swift
/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunc topKHeap(nums: [Int], k: Int) -> [Int] {\n// \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nvar heap = Array(nums.prefix(k))\n// \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\nfor i in stride(from: k, to: nums.count, by: 1) {\n// \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\nif nums[i] > heap.first! {\nheap.removeFirst()\nheap.insert(nums[i], at: 0)\n}\n}\nreturn heap\n}\n
top_k.js
/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunction topKHeap(nums, k) {\n// \u4f7f\u7528\u5927\u9876\u5806 MaxHeap \uff0c\u5bf9\u6570\u7ec4 nums \u53d6\u76f8\u53cd\u6570\nconst invertedNums = nums.map((num) => -num);\n// \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nconst heap = new MaxHeap(invertedNums.slice(0, k));\n// \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\nfor (let i = k; i < invertedNums.length; i++) {\n// \u82e5\u5f53\u524d\u5143\u7d20\u5c0f\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\nif (invertedNums[i] < heap.peek()) {\nheap.pop();\nheap.push(invertedNums[i]);\n}\n}\n// \u53d6\u51fa\u5806\u4e2d\u5143\u7d20\nconst maxHeap = heap.getMaxHeap();\n// \u5bf9\u5806\u4e2d\u5143\u7d20\u53d6\u76f8\u53cd\u6570\nconst invertedMaxHeap = maxHeap.map((num) => -num);\nreturn invertedMaxHeap;\n}\n
top_k.ts
/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunction topKHeap(nums: number[], k: number): number[] {\n// \u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u7528\u5927\u9876\u5806\u6765\u6a21\u62df\u5c0f\u9876\u5806\nconst invertedNums = nums.map((num) => -num);\n// \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nconst heap = new MaxHeap(invertedNums.slice(0, k));\n// \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\nfor (let i = k; i < invertedNums.length; i++) {\n// \u82e5\u5f53\u524d\u5143\u7d20\u5c0f\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\nif (invertedNums[i] < heap.peek()) {\nheap.pop();\nheap.push(invertedNums[i]);\n}\n}\n// \u53d6\u51fa\u5806\u4e2d\u5143\u7d20\nconst maxHeap = heap.getMaxHeap();\n// \u5bf9\u5806\u4e2d\u5143\u7d20\u53d6\u76f8\u53cd\u6570\nconst invertedMaxHeap = maxHeap.map((num) => -num);\nreturn invertedMaxHeap;\n}\n
top_k.dart
/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nMinHeap topKHeap(List<int> nums, int k) {\n// \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nMinHeap heap = MinHeap(nums.sublist(0, k));\n// \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\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.pop();\nheap.push(nums[i]);\n}\n}\nreturn heap;\n}\n
top_k.rs
/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfn top_k_heap(nums: Vec<i32>, k: usize) -> BinaryHeap<Reverse<i32>> {\n// Rust \u7684 BinaryHeap \u662f\u5927\u9876\u5806\uff0c\u4f7f\u7528 Reverse \u5c06\u5143\u7d20\u5927\u5c0f\u53cd\u8f6c\nlet mut heap = BinaryHeap::<Reverse<i32>>::new();\n// \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nfor &num in nums.iter().take(k) {\nheap.push(Reverse(num));\n}\n// \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\nfor &num in nums.iter().skip(k) {\n// \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\nif num > heap.peek().unwrap().0 {\nheap.pop();\nheap.push(Reverse(num));\n}\n}\nheap\n}\n
top_k.c
[class]{}-[func]{topKHeap}\n
top_k.zig
[class]{}-[func]{topKHeap}\n
"},{"location":"chapter_introduction/","title":"\u7b2c 1 \u7ae0 \u00a0 \u521d\u8bc6\u7b97\u6cd5","text":"

Abstract

\u4e00\u4f4d\u5c11\u5973\u7fe9\u7fe9\u8d77\u821e\uff0c\u4e0e\u6570\u636e\u4ea4\u7ec7\u5728\u4e00\u8d77\uff0c\u88d9\u6446\u4e0a\u98d8\u626c\u7740\u7b97\u6cd5\u7684\u65cb\u5f8b\u3002

\u5979\u9080\u8bf7\u4f60\u5171\u821e\uff0c\u8bf7\u7d27\u8ddf\u5979\u7684\u6b65\u4f10\uff0c\u8e0f\u5165\u5145\u6ee1\u903b\u8f91\u4e0e\u7f8e\u611f\u7684\u7b97\u6cd5\u4e16\u754c\u3002

"},{"location":"chapter_introduction/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 1.1 \u00a0 \u7b97\u6cd5\u65e0\u5904\u4e0d\u5728
  • 1.2 \u00a0 \u7b97\u6cd5\u662f\u4ec0\u4e48
  • 1.3 \u00a0 \u5c0f\u7ed3
"},{"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\u4f60\u5df2\u7ecf\u5728\u4e0d\u77e5\u4e0d\u89c9\u4e2d\u5b66\u4f1a\u4e86\u8bb8\u591a\u7b97\u6cd5\uff0c\u5e76\u4e60\u60ef\u5c06\u5b83\u4eec\u5e94\u7528\u5230\u65e5\u5e38\u751f\u6d3b\u4e2d\u4e86\u3002\u4e0b\u9762\uff0c\u6211\u5c06\u4e3e\u51e0\u4e2a\u5177\u4f53\u4f8b\u5b50\u6765\u8bc1\u5b9e\u8fd9\u4e00\u70b9\u3002

\u4f8b\u4e00\uff1a\u67e5\u9605\u5b57\u5178\u3002\u5728\u5b57\u5178\u91cc\uff0c\u6bcf\u4e2a\u6c49\u5b57\u90fd\u5bf9\u5e94\u4e00\u4e2a\u62fc\u97f3\uff0c\u800c\u5b57\u5178\u662f\u6309\u7167\u62fc\u97f3\u5b57\u6bcd\u987a\u5e8f\u6392\u5217\u7684\u3002\u5047\u8bbe\u6211\u4eec\u9700\u8981\u67e5\u627e\u4e00\u4e2a\u62fc\u97f3\u9996\u5b57\u6bcd\u4e3a \\(r\\) \u7684\u5b57\uff0c\u901a\u5e38\u4f1a\u6309\u7167\u56fe 1-1 \u6240\u793a\u7684\u65b9\u5f0f\u5b9e\u73b0\u3002

  1. \u7ffb\u5f00\u5b57\u5178\u7ea6\u4e00\u534a\u7684\u9875\u6570\uff0c\u67e5\u770b\u8be5\u9875\u7684\u9996\u5b57\u6bcd\u662f\u4ec0\u4e48\uff0c\u5047\u8bbe\u9996\u5b57\u6bcd\u4e3a \\(m\\) \u3002
  2. \u7531\u4e8e\u5728\u62fc\u97f3\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\u3002
  3. \u4e0d\u65ad\u91cd\u590d\u6b65\u9aa4 1. \u548c \u6b65\u9aa4 2. \uff0c\u76f4\u81f3\u627e\u5230\u62fc\u97f3\u9996\u5b57\u6bcd\u4e3a \\(r\\) \u7684\u9875\u7801\u4e3a\u6b62\u3002
<1><2><3><4><5>

\u56fe 1-1 \u00a0 \u67e5\u5b57\u5178\u6b65\u9aa4

\u67e5\u9605\u5b57\u5178\u8fd9\u4e2a\u5c0f\u5b66\u751f\u5fc5\u5907\u6280\u80fd\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u8457\u540d\u7684\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u3002\u4ece\u6570\u636e\u7ed3\u6784\u7684\u89d2\u5ea6\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u5b57\u5178\u89c6\u4e3a\u4e00\u4e2a\u5df2\u6392\u5e8f\u7684\u201c\u6570\u7ec4\u201d\uff1b\u4ece\u7b97\u6cd5\u7684\u89d2\u5ea6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e0a\u8ff0\u67e5\u5b57\u5178\u7684\u4e00\u7cfb\u5217\u64cd\u4f5c\u770b\u4f5c\u662f\u201c\u4e8c\u5206\u67e5\u627e\u201d\u3002

\u4f8b\u4e8c\uff1a\u6574\u7406\u6251\u514b\u3002\u6211\u4eec\u5728\u6253\u724c\u65f6\uff0c\u6bcf\u5c40\u90fd\u9700\u8981\u6574\u7406\u6251\u514b\u724c\uff0c\u4f7f\u5176\u4ece\u5c0f\u5230\u5927\u6392\u5217\uff0c\u5b9e\u73b0\u6d41\u7a0b\u5982\u56fe 1-2 \u6240\u793a\u3002

  1. \u5c06\u6251\u514b\u724c\u5212\u5206\u4e3a\u201c\u6709\u5e8f\u201d\u548c\u201c\u65e0\u5e8f\u201d\u4e24\u90e8\u5206\uff0c\u5e76\u5047\u8bbe\u521d\u59cb\u72b6\u6001\u4e0b\u6700\u5de6 1 \u5f20\u6251\u514b\u724c\u5df2\u7ecf\u6709\u5e8f\u3002
  2. \u5728\u65e0\u5e8f\u90e8\u5206\u62bd\u51fa\u4e00\u5f20\u6251\u514b\u724c\uff0c\u63d2\u5165\u81f3\u6709\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\uff1b\u5b8c\u6210\u540e\u6700\u5de6 2 \u5f20\u6251\u514b\u5df2\u7ecf\u6709\u5e8f\u3002
  3. \u4e0d\u65ad\u5faa\u73af\u6b65\u9aa4 2. \uff0c\u6bcf\u4e00\u8f6e\u5c06\u4e00\u5f20\u6251\u514b\u724c\u4ece\u65e0\u5e8f\u90e8\u5206\u63d2\u5165\u81f3\u6709\u5e8f\u90e8\u5206\uff0c\u76f4\u81f3\u6240\u6709\u6251\u514b\u724c\u90fd\u6709\u5e8f\u3002

\u56fe 1-2 \u00a0 \u6251\u514b\u6392\u5e8f\u6b65\u9aa4

\u4e0a\u8ff0\u6574\u7406\u6251\u514b\u724c\u7684\u65b9\u6cd5\u672c\u8d28\u4e0a\u662f\u201c\u63d2\u5165\u6392\u5e8f\u201d\u7b97\u6cd5\uff0c\u5b83\u5728\u5904\u7406\u5c0f\u578b\u6570\u636e\u96c6\u65f6\u975e\u5e38\u9ad8\u6548\u3002\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u7684\u6392\u5e8f\u5e93\u51fd\u6570\u4e2d\u90fd\u5b58\u5728\u63d2\u5165\u6392\u5e8f\u7684\u8eab\u5f71\u3002

\u4f8b\u4e09\uff1a\u8d27\u5e01\u627e\u96f6\u3002\u5047\u8bbe\u6211\u4eec\u5728\u8d85\u5e02\u8d2d\u4e70\u4e86 \\(69\\) \u5143\u7684\u5546\u54c1\uff0c\u7ed9\u4e86\u6536\u94f6\u5458 \\(100\\) \u5143\uff0c\u5219\u6536\u94f6\u5458\u9700\u8981\u627e\u6211\u4eec \\(31\\) \u5143\u3002\u4ed6\u4f1a\u5f88\u81ea\u7136\u5730\u5b8c\u6210\u5982\u56fe 1-3 \u6240\u793a\u7684\u601d\u8003\u3002

  1. \u53ef\u9009\u9879\u662f\u6bd4 \\(31\\) \u5143\u9762\u503c\u66f4\u5c0f\u7684\u8d27\u5e01\uff0c\u5305\u62ec \\(1\\) \u5143\u3001\\(5\\) \u5143\u3001\\(10\\) \u5143\u3001\\(20\\) \u5143\u3002
  2. \u4ece\u53ef\u9009\u9879\u4e2d\u62ff\u51fa\u6700\u5927\u7684 \\(20\\) \u5143\uff0c\u5269\u4f59 \\(31 - 20 = 11\\) \u5143\u3002
  3. \u4ece\u5269\u4f59\u53ef\u9009\u9879\u4e2d\u62ff\u51fa\u6700\u5927\u7684 \\(10\\) \u5143\uff0c\u5269\u4f59 \\(11 - 10 = 1\\) \u5143\u3002
  4. \u4ece\u5269\u4f59\u53ef\u9009\u9879\u4e2d\u62ff\u51fa\u6700\u5927\u7684 \\(1\\) \u5143\uff0c\u5269\u4f59 \\(1 - 1 = 0\\) \u5143\u3002
  5. \u5b8c\u6210\u627e\u96f6\uff0c\u65b9\u6848\u4e3a \\(20 + 10 + 1 = 31\\) \u5143\u3002

\u56fe 1-3 \u00a0 \u8d27\u5e01\u627e\u96f6\u8fc7\u7a0b

\u5728\u4ee5\u4e0a\u6b65\u9aa4\u4e2d\uff0c\u6211\u4eec\u6bcf\u4e00\u6b65\u90fd\u91c7\u53d6\u5f53\u524d\u770b\u6765\u6700\u597d\u7684\u9009\u62e9\uff08\u5c3d\u53ef\u80fd\u7528\u5927\u9762\u989d\u7684\u8d27\u5e01\uff09\uff0c\u6700\u7ec8\u5f97\u5230\u4e86\u53ef\u884c\u7684\u627e\u96f6\u65b9\u6848\u3002\u4ece\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u89d2\u5ea6\u770b\uff0c\u8fd9\u79cd\u65b9\u6cd5\u672c\u8d28\u4e0a\u662f\u201c\u8d2a\u5fc3\u201d\u7b97\u6cd5\u3002

\u5c0f\u5230\u70f9\u996a\u4e00\u9053\u83dc\uff0c\u5927\u5230\u661f\u9645\u822a\u884c\uff0c\u51e0\u4e4e\u6240\u6709\u95ee\u9898\u7684\u89e3\u51b3\u90fd\u79bb\u4e0d\u5f00\u7b97\u6cd5\u3002\u8ba1\u7b97\u673a\u7684\u51fa\u73b0\u4f7f\u6211\u4eec\u80fd\u591f\u901a\u8fc7\u7f16\u7a0b\u5c06\u6570\u636e\u7ed3\u6784\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\uff0c\u540c\u65f6\u7f16\u5199\u4ee3\u7801\u8c03\u7528 CPU \u548c GPU \u6267\u884c\u7b97\u6cd5\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u80fd\u628a\u751f\u6d3b\u4e2d\u7684\u95ee\u9898\u8f6c\u79fb\u5230\u8ba1\u7b97\u673a\u4e0a\uff0c\u4ee5\u66f4\u9ad8\u6548\u7684\u65b9\u5f0f\u89e3\u51b3\u5404\u79cd\u590d\u6742\u95ee\u9898\u3002

Tip

\u5982\u679c\u4f60\u5bf9\u6570\u636e\u7ed3\u6784\u3001\u7b97\u6cd5\u3001\u6570\u7ec4\u548c\u4e8c\u5206\u67e5\u627e\u7b49\u6982\u5ff5\u4ecd\u611f\u5230\u4e00\u77e5\u534a\u89e3\uff0c\u8bf7\u7ee7\u7eed\u5f80\u4e0b\u9605\u8bfb\uff0c\u8fd9\u672c\u4e66\u5c06\u5f15\u5bfc\u4f60\u8fc8\u5165\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u77e5\u8bc6\u6bbf\u5802\u3002

"},{"location":"chapter_introduction/summary/","title":"1.3 \u00a0 \u5c0f\u7ed3","text":"
  • \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\u4f1a\u4e86\u8bb8\u591a\u7b97\u6cd5\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\u7b97\u6cd5\u4f53\u73b0\u4e86\u5206\u800c\u6cbb\u4e4b\u7684\u91cd\u8981\u7b97\u6cd5\u601d\u60f3\u3002
  • \u6574\u7406\u6251\u514b\u7684\u8fc7\u7a0b\u4e0e\u63d2\u5165\u6392\u5e8f\u7b97\u6cd5\u975e\u5e38\u7c7b\u4f3c\u3002\u63d2\u5165\u6392\u5e8f\u7b97\u6cd5\u9002\u5408\u6392\u5e8f\u5c0f\u578b\u6570\u636e\u96c6\u3002
  • \u8d27\u5e01\u627e\u96f6\u7684\u6b65\u9aa4\u672c\u8d28\u4e0a\u662f\u8d2a\u5fc3\u7b97\u6cd5\uff0c\u6bcf\u4e00\u6b65\u90fd\u91c7\u53d6\u5f53\u524d\u770b\u6765\u7684\u6700\u597d\u7684\u9009\u62e9\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
  • \u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7c7b\u6bd4\u4e3a\u62fc\u88c5\u79ef\u6728\uff0c\u79ef\u6728\u4ee3\u8868\u6570\u636e\uff0c\u79ef\u6728\u7684\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\uff0c\u5b83\u5177\u6709\u4ee5\u4e0b\u7279\u6027\u3002

  • \u95ee\u9898\u662f\u660e\u786e\u7684\uff0c\u5305\u542b\u6e05\u6670\u7684\u8f93\u5165\u548c\u8f93\u51fa\u5b9a\u4e49\u3002
  • \u5177\u6709\u53ef\u884c\u6027\uff0c\u80fd\u591f\u5728\u6709\u9650\u6b65\u9aa4\u3001\u65f6\u95f4\u548c\u5185\u5b58\u7a7a\u95f4\u4e0b\u5b8c\u6210\u3002
  • \u5404\u6b65\u9aa4\u90fd\u6709\u786e\u5b9a\u7684\u542b\u4e49\uff0c\u76f8\u540c\u7684\u8f93\u5165\u548c\u8fd0\u884c\u6761\u4ef6\u4e0b\uff0c\u8f93\u51fa\u59cb\u7ec8\u76f8\u540c\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\uff0c\u5177\u6709\u4ee5\u4e0b\u8bbe\u8ba1\u76ee\u6807\u3002

  • \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\u4fbf\u4f7f\u5f97\u7b97\u6cd5\u9ad8\u6548\u8fd0\u884c\u3002

\u6570\u636e\u7ed3\u6784\u8bbe\u8ba1\u662f\u4e00\u4e2a\u5145\u6ee1\u6743\u8861\u7684\u8fc7\u7a0b\u3002\u5982\u679c\u60f3\u8981\u5728\u67d0\u65b9\u9762\u53d6\u5f97\u63d0\u5347\uff0c\u5f80\u5f80\u9700\u8981\u5728\u53e6\u4e00\u65b9\u9762\u4f5c\u51fa\u59a5\u534f\u3002\u4e0b\u9762\u4e3e\u4e24\u4e2a\u4f8b\u5b50\u3002

  • \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\u3002
  • \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":"

\u5982\u56fe 1-4 \u6240\u793a\uff0c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u9ad8\u5ea6\u76f8\u5173\u3001\u7d27\u5bc6\u7ed3\u5408\uff0c\u5177\u4f53\u8868\u73b0\u4ee5\u4e0b\u4e09\u4e2a\u65b9\u9762\u3002

  • \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\u4f5c\u7528\u7684\u821e\u53f0\u3002\u6570\u636e\u7ed3\u6784\u672c\u8eab\u4ec5\u5b58\u50a8\u6570\u636e\u4fe1\u606f\uff0c\u7ed3\u5408\u7b97\u6cd5\u624d\u80fd\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\u3002
  • \u7b97\u6cd5\u901a\u5e38\u53ef\u4ee5\u57fa\u4e8e\u4e0d\u540c\u7684\u6570\u636e\u7ed3\u6784\u8fdb\u884c\u5b9e\u73b0\uff0c\u4f46\u6267\u884c\u6548\u7387\u53ef\u80fd\u76f8\u5dee\u5f88\u5927\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u662f\u5173\u952e\u3002

\u56fe 1-4 \u00a0 \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u5173\u7cfb

\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u72b9\u5982\u56fe 1-5 \u6240\u793a\u7684\u62fc\u88c5\u79ef\u6728\u3002\u4e00\u5957\u79ef\u6728\uff0c\u9664\u4e86\u5305\u542b\u8bb8\u591a\u96f6\u4ef6\u4e4b\u5916\uff0c\u8fd8\u9644\u6709\u8be6\u7ec6\u7684\u7ec4\u88c5\u8bf4\u660e\u4e66\u3002\u6211\u4eec\u6309\u7167\u8bf4\u660e\u4e66\u4e00\u6b65\u6b65\u64cd\u4f5c\uff0c\u5c31\u80fd\u7ec4\u88c5\u51fa\u7cbe\u7f8e\u7684\u79ef\u6728\u6a21\u578b\u3002

\u56fe 1-5 \u00a0 \u62fc\u88c5\u79ef\u6728

\u4e24\u8005\u7684\u8be6\u7ec6\u5bf9\u5e94\u5173\u7cfb\u5982\u8868 1-1 \u6240\u793a\u3002

\u8868 1-1 \u00a0 \u5c06\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7c7b\u6bd4\u4e3a\u79ef\u6728

\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5 \u62fc\u88c5\u79ef\u6728 \u8f93\u5165\u6570\u636e \u672a\u62fc\u88c5\u7684\u79ef\u6728 \u6570\u636e\u7ed3\u6784 \u79ef\u6728\u7ec4\u7ec7\u5f62\u5f0f\uff0c\u5305\u62ec\u5f62\u72b6\u3001\u5927\u5c0f\u3001\u8fde\u63a5\u65b9\u5f0f\u7b49 \u7b97\u6cd5 \u628a\u79ef\u6728\u62fc\u6210\u76ee\u6807\u5f62\u6001\u7684\u4e00\u7cfb\u5217\u64cd\u4f5c\u6b65\u9aa4 \u8f93\u51fa\u6570\u636e \u79ef\u6728\u6a21\u578b

\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u662f\u72ec\u7acb\u4e8e\u7f16\u7a0b\u8bed\u8a00\u7684\u3002\u6b63\u56e0\u5982\u6b64\uff0c\u672c\u4e66\u5f97\u4ee5\u63d0\u4f9b\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\u7684\u5b9e\u73b0\u3002

\u7ea6\u5b9a\u4fd7\u6210\u7684\u7b80\u79f0

\u5728\u5b9e\u9645\u8ba8\u8bba\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u201c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u201d\u7b80\u79f0\u4e3a\u201c\u7b97\u6cd5\u201d\u3002\u6bd4\u5982\u4f17\u6240\u5468\u77e5\u7684 LeetCode \u7b97\u6cd5\u9898\u76ee\uff0c\u5b9e\u9645\u4e0a\u540c\u65f6\u8003\u5bdf\u4e86\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u4e24\u65b9\u9762\u7684\u77e5\u8bc6\u3002

"},{"location":"chapter_preface/","title":"\u7b2c 0 \u7ae0 \u00a0 \u524d\u8a00","text":"

Abstract

\u7b97\u6cd5\u72b9\u5982\u7f8e\u5999\u7684\u4ea4\u54cd\u4e50\uff0c\u6bcf\u4e00\u884c\u4ee3\u7801\u90fd\u50cf\u97f5\u5f8b\u822c\u6d41\u6dcc\u3002

\u613f\u8fd9\u672c\u4e66\u5728\u4f60\u7684\u8111\u6d77\u4e2d\u8f7b\u8f7b\u54cd\u8d77\uff0c\u7559\u4e0b\u72ec\u7279\u800c\u6df1\u523b\u7684\u65cb\u5f8b\u3002

"},{"location":"chapter_preface/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 0.1 \u00a0 \u5173\u4e8e\u672c\u4e66
  • 0.2 \u00a0 \u5982\u4f55\u4f7f\u7528\u672c\u4e66
  • 0.3 \u00a0 \u5c0f\u7ed3
"},{"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\u3002
  • \u7b97\u6cd5\u6e90\u4ee3\u7801\u7686\u53ef\u4e00\u952e\u8fd0\u884c\uff0c\u652f\u6301 Java\u3001C++\u3001Python\u3001Go\u3001JS\u3001TS\u3001C#\u3001Swift\u3001Rust\u3001Dart\u3001Zig \u7b49\u8bed\u8a00\u3002
  • \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\u3002
"},{"location":"chapter_preface/about_the_book/#011","title":"0.1.1 \u00a0 \u8bfb\u8005\u5bf9\u8c61","text":"

\u82e5\u60a8\u662f\u7b97\u6cd5\u521d\u5b66\u8005\uff0c\u4ece\u672a\u63a5\u89e6\u8fc7\u7b97\u6cd5\uff0c\u6216\u8005\u5df2\u7ecf\u6709\u4e00\u4e9b\u5237\u9898\u7ecf\u9a8c\uff0c\u5bf9\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u6709\u6a21\u7cca\u7684\u8ba4\u8bc6\uff0c\u5728\u4f1a\u4e0e\u4e0d\u4f1a\u4e4b\u95f4\u53cd\u590d\u6a2a\u8df3\uff0c\u90a3\u4e48\u8fd9\u672c\u4e66\u6b63\u662f\u4e3a\u60a8\u91cf\u8eab\u5b9a\u5236\uff01

\u5982\u679c\u60a8\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\u7b97\u6cd5\u5927\u795e\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\u5982\u56fe 0-1 \u6240\u793a\u3002

  • \u590d\u6742\u5ea6\u5206\u6790\uff1a\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u7684\u8bc4\u4ef7\u7ef4\u5ea6\u4e0e\u65b9\u6cd5\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u3001\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u63a8\u7b97\u65b9\u6cd5\u3001\u5e38\u89c1\u7c7b\u578b\u3001\u793a\u4f8b\u7b49\u3002
  • \u6570\u636e\u7ed3\u6784\uff1a\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u6570\u636e\u7ed3\u6784\u7684\u5206\u7c7b\u65b9\u6cd5\u3002\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\u7b49\u6570\u636e\u7ed3\u6784\u7684\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\u641c\u7d22\u3001\u6392\u5e8f\u3001\u5206\u6cbb\u3001\u56de\u6eaf\u3001\u52a8\u6001\u89c4\u5212\u3001\u8d2a\u5fc3\u7b49\u7b97\u6cd5\u7684\u5b9a\u4e49\u3001\u4f18\u7f3a\u70b9\u3001\u6548\u7387\u3001\u5e94\u7528\u573a\u666f\u3001\u89e3\u9898\u6b65\u9aa4\u3001\u793a\u4f8b\u9898\u76ee\u7b49\u3002

\u56fe 0-1 \u00a0 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\u4e00\u6b21\u7545\u8c08\u4e2d\u60a8\u9f13\u52b1\u6211\u201c\u5feb\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\u4ed6\u5f00\u53d1\u7684\u5f00\u6e90\u6587\u6863\u4e3b\u9898 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\u4ef6\u5bcc\u6709\u8da3\u5473\u7684\u4e8b\u3002

"},{"location":"chapter_preface/suggestions/","title":"0.2 \u00a0 \u5982\u4f55\u4f7f\u7528\u672c\u4e66","text":"

Tip

\u4e3a\u4e86\u83b7\u5f97\u6700\u4f73\u7684\u9605\u8bfb\u4f53\u9a8c\uff0c\u5efa\u8bae\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
  • \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
  • \u91cd\u8981\u4e13\u6709\u540d\u8bcd\u53ca\u5176\u82f1\u6587\u7ffb\u8bd1\u4f1a\u7528 \u300c \u300d \u62ec\u53f7\u6807\u6ce8\uff0c\u4f8b\u5982 \u300c\u6570\u7ec4 array\u300d \u3002\u5efa\u8bae\u8bb0\u4f4f\u5b83\u4eec\uff0c\u4ee5\u4fbf\u9605\u8bfb\u6587\u732e\u3002
  • \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
  • \u5f53\u6d89\u53ca\u5230\u7f16\u7a0b\u8bed\u8a00\u4e4b\u95f4\u4e0d\u4e00\u81f4\u7684\u540d\u8bcd\u65f6\uff0c\u672c\u4e66\u5747\u4ee5 Python \u4e3a\u51c6\uff0c\u4f8b\u5982\u4f7f\u7528 \\(\\text{None}\\) \u6765\u8868\u793a\u201c\u7a7a\u201d\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
PythonC++JavaC#GoSwiftJSTSDartRustCZig
\"\"\"\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/**\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
"},{"location":"chapter_preface/suggestions/#022","title":"0.2.2 \u00a0 \u5728\u52a8\u753b\u56fe\u89e3\u4e2d\u9ad8\u6548\u5b66\u4e60","text":"

\u76f8\u8f83\u4e8e\u6587\u5b57\uff0c\u89c6\u9891\u548c\u56fe\u7247\u5177\u6709\u66f4\u9ad8\u7684\u4fe1\u606f\u5bc6\u5ea6\u548c\u7ed3\u6784\u5316\u7a0b\u5ea6\uff0c\u66f4\u6613\u4e8e\u7406\u89e3\u3002\u5728\u672c\u4e66\u4e2d\uff0c\u91cd\u70b9\u548c\u96be\u70b9\u77e5\u8bc6\u5c06\u4e3b\u8981\u901a\u8fc7\u52a8\u753b\u548c\u56fe\u89e3\u5f62\u5f0f\u5c55\u793a\uff0c\u800c\u6587\u5b57\u5219\u4f5c\u4e3a\u52a8\u753b\u548c\u56fe\u7247\u7684\u89e3\u91ca\u4e0e\u8865\u5145\u3002

\u5982\u679c\u4f60\u5728\u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u53d1\u73b0\u67d0\u6bb5\u5185\u5bb9\u63d0\u4f9b\u4e86\u56fe 0-2 \u6240\u793a\u7684\u52a8\u753b\u6216\u56fe\u89e3\uff0c\u8bf7\u4ee5\u56fe\u4e3a\u4e3b\u3001\u4ee5\u6587\u5b57\u4e3a\u8f85\uff0c\u7efc\u5408\u4e24\u8005\u6765\u7406\u89e3\u5185\u5bb9\u3002

\u56fe 0-2 \u00a0 \u52a8\u753b\u56fe\u89e3\u793a\u4f8b

"},{"location":"chapter_preface/suggestions/#023","title":"0.2.3 \u00a0 \u5728\u4ee3\u7801\u5b9e\u8df5\u4e2d\u52a0\u6df1\u7406\u89e3","text":"

\u672c\u4e66\u7684\u914d\u5957\u4ee3\u7801\u88ab\u6258\u7ba1\u5728 GitHub \u4ed3\u5e93\u3002\u5982\u56fe 0-3 \u6240\u793a\uff0c\u6e90\u4ee3\u7801\u9644\u6709\u6d4b\u8bd5\u6837\u4f8b\uff0c\u53ef\u4e00\u952e\u8fd0\u884c\u3002

\u5982\u679c\u65f6\u95f4\u5141\u8bb8\uff0c\u5efa\u8bae\u4f60\u53c2\u7167\u4ee3\u7801\u81ea\u884c\u6572\u4e00\u904d\u3002\u5982\u679c\u5b66\u4e60\u65f6\u95f4\u6709\u9650\uff0c\u8bf7\u81f3\u5c11\u901a\u8bfb\u5e76\u8fd0\u884c\u6240\u6709\u4ee3\u7801\u3002

\u4e0e\u9605\u8bfb\u4ee3\u7801\u76f8\u6bd4\uff0c\u7f16\u5199\u4ee3\u7801\u7684\u8fc7\u7a0b\u5f80\u5f80\u80fd\u5e26\u6765\u66f4\u591a\u6536\u83b7\u3002\u52a8\u624b\u5b66\uff0c\u624d\u662f\u771f\u7684\u5b66\u3002

\u56fe 0-3 \u00a0 \u8fd0\u884c\u4ee3\u7801\u793a\u4f8b

\u8fd0\u884c\u4ee3\u7801\u7684\u524d\u7f6e\u5de5\u4f5c\u4e3b\u8981\u5206\u4e3a\u4e09\u6b65\u3002

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

\u7b2c\u4e8c\u6b65\uff1a\u514b\u9686\u6216\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\u5728\u56fe 0-4 \u6240\u793a\u7684\u4f4d\u7f6e\uff0c\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

\u56fe 0-4 \u00a0 \u514b\u9686\u4ed3\u5e93\u4e0e\u4e0b\u8f7d\u4ee3\u7801

\u7b2c\u4e09\u6b65\uff1a\u8fd0\u884c\u6e90\u4ee3\u7801\u3002\u5982\u56fe 0-5 \u6240\u793a\uff0c\u5bf9\u4e8e\u9876\u90e8\u6807\u6709\u6587\u4ef6\u540d\u79f0\u7684\u4ee3\u7801\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4ed3\u5e93\u7684 codes \u6587\u4ef6\u5939\u5185\u627e\u5230\u5bf9\u5e94\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u3002\u6e90\u4ee3\u7801\u6587\u4ef6\u53ef\u4e00\u952e\u8fd0\u884c\uff0c\u5c06\u5e2e\u52a9\u4f60\u8282\u7701\u4e0d\u5fc5\u8981\u7684\u8c03\u8bd5\u65f6\u95f4\uff0c\u8ba9\u4f60\u80fd\u591f\u4e13\u6ce8\u4e8e\u5b66\u4e60\u5185\u5bb9\u3002

\u56fe 0-5 \u00a0 \u4ee3\u7801\u5757\u4e0e\u5bf9\u5e94\u7684\u6e90\u4ee3\u7801\u6587\u4ef6

"},{"location":"chapter_preface/suggestions/#024","title":"0.2.4 \u00a0 \u5728\u63d0\u95ee\u8ba8\u8bba\u4e2d\u5171\u540c\u6210\u957f","text":"

\u5728\u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u8bf7\u4e0d\u8981\u8f7b\u6613\u8df3\u8fc7\u90a3\u4e9b\u6ca1\u5b66\u660e\u767d\u7684\u77e5\u8bc6\u70b9\u3002\u6b22\u8fce\u5728\u8bc4\u8bba\u533a\u63d0\u51fa\u4f60\u7684\u95ee\u9898\uff0c\u6211\u548c\u5c0f\u4f19\u4f34\u4eec\u5c06\u7aed\u8bda\u4e3a\u4f60\u89e3\u7b54\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u53ef\u5728\u4e24\u5929\u5185\u56de\u590d\u3002

\u5982\u56fe 0-6 \u6240\u793a\uff0c\u6bcf\u7bc7\u6587\u7ae0\u7684\u5e95\u90e8\u90fd\u914d\u6709\u8bc4\u8bba\u533a\u3002\u5e0c\u671b\u4f60\u80fd\u591a\u5173\u6ce8\u8bc4\u8bba\u533a\u7684\u5185\u5bb9\u3002\u4e00\u65b9\u9762\uff0c\u4f60\u53ef\u4ee5\u4e86\u89e3\u5927\u5bb6\u9047\u5230\u7684\u95ee\u9898\uff0c\u4ece\u800c\u67e5\u6f0f\u8865\u7f3a\uff0c\u6fc0\u53d1\u66f4\u6df1\u5165\u7684\u601d\u8003\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u671f\u5f85\u4f60\u80fd\u6177\u6168\u5730\u56de\u7b54\u5176\u4ed6\u5c0f\u4f19\u4f34\u7684\u95ee\u9898\uff0c\u5206\u4eab\u60a8\u7684\u89c1\u89e3\uff0c\u5e2e\u52a9\u4ed6\u4eba\u8fdb\u6b65\u3002

\u56fe 0-6 \u00a0 \u8bc4\u8bba\u533a\u793a\u4f8b

"},{"location":"chapter_preface/suggestions/#025","title":"0.2.5 \u00a0 \u7b97\u6cd5\u5b66\u4e60\u8def\u7ebf","text":"

\u4ece\u603b\u4f53\u4e0a\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b66\u4e60\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u8fc7\u7a0b\u5212\u5206\u4e3a\u4e09\u4e2a\u9636\u6bb5\u3002

  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

\u5982\u56fe 0-7 \u6240\u793a\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

\u56fe 0-7 \u00a0 \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":"\u7b2c 10 \u7ae0 \u00a0 \u641c\u7d22","text":"

Abstract

\u641c\u7d22\u662f\u4e00\u573a\u672a\u77e5\u7684\u5192\u9669\uff0c\u6211\u4eec\u6216\u8bb8\u9700\u8981\u8d70\u904d\u795e\u79d8\u7a7a\u95f4\u7684\u6bcf\u4e2a\u89d2\u843d\uff0c\u53c8\u6216\u8bb8\u53ef\u4ee5\u5feb\u901f\u9501\u5b9a\u76ee\u6807\u3002

\u5728\u8fd9\u573a\u5bfb\u89c5\u4e4b\u65c5\u4e2d\uff0c\u6bcf\u4e00\u6b21\u63a2\u7d22\u90fd\u53ef\u80fd\u5f97\u5230\u4e00\u4e2a\u672a\u66fe\u6599\u60f3\u7684\u7b54\u6848\u3002

"},{"location":"chapter_searching/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 10.1 \u00a0 \u4e8c\u5206\u67e5\u627e
  • 10.2 \u00a0 \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9
  • 10.3 \u00a0 \u4e8c\u5206\u67e5\u627e\u8fb9\u754c
  • 10.4 \u00a0 \u54c8\u5e0c\u4f18\u5316\u7b56\u7565
  • 10.5 \u00a0 \u91cd\u8bc6\u641c\u7d22\u7b97\u6cd5
  • 10.6 \u00a0 \u5c0f\u7ed3
"},{"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\u7b56\u7565\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

\u56fe 10-1 \u00a0 \u4e8c\u5206\u67e5\u627e\u793a\u4f8b\u6570\u636e

\u5982\u56fe 10-2 \u6240\u793a\uff0c\u6211\u4eec\u5148\u521d\u59cb\u5316\u6307\u9488 \\(i = 0\\) \u548c \\(j = n - 1\\) \uff0c\u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u548c\u5c3e\u5143\u7d20\uff0c\u4ee3\u8868\u641c\u7d22\u533a\u95f4 \\([0, n - 1]\\) \u3002\u8bf7\u6ce8\u610f\uff0c\u4e2d\u62ec\u53f7\u8868\u793a\u95ed\u533a\u95f4\uff0c\u5176\u5305\u542b\u8fb9\u754c\u503c\u672c\u8eab\u3002

\u63a5\u4e0b\u6765\uff0c\u5faa\u73af\u6267\u884c\u4ee5\u4e0b\u4e24\u6b65\u3002

  1. \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 \\(m = \\lfloor {(i + j) / 2} \\rfloor\\) \uff0c\u5176\u4e2d \\(\\lfloor \\: \\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\u4ee5\u4e0b\u4e09\u79cd\u60c5\u51b5\u3002
    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\\) \u3002
    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\\) \u3002
    3. \u5f53 nums[m] = target \u65f6\uff0c\u8bf4\u660e\u627e\u5230 target \uff0c\u56e0\u6b64\u8fd4\u56de\u7d22\u5f15 \\(m\\) \u3002

\u82e5\u6570\u7ec4\u4e0d\u5305\u542b\u76ee\u6807\u5143\u7d20\uff0c\u641c\u7d22\u533a\u95f4\u6700\u7ec8\u4f1a\u7f29\u5c0f\u4e3a\u7a7a\u3002\u6b64\u65f6\u8fd4\u56de \\(-1\\) \u3002

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

\u56fe 10-2 \u00a0 \u4e8c\u5206\u67e5\u627e\u6d41\u7a0b

\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e \\(i\\) \u548c \\(j\\) \u90fd\u662f int \u7c7b\u578b\uff0c\u56e0\u6b64 \\(i + j\\) \u53ef\u80fd\u4f1a\u8d85\u51fa int \u7c7b\u578b\u7684\u53d6\u503c\u8303\u56f4\u3002\u4e3a\u4e86\u907f\u514d\u5927\u6570\u8d8a\u754c\uff0c\u6211\u4eec\u901a\u5e38\u91c7\u7528\u516c\u5f0f \\(m = \\lfloor {i + (j - i) / 2} \\rfloor\\) \u6765\u8ba1\u7b97\u4e2d\u70b9\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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\u987b\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.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.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.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.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.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.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.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
binary_search.rs
/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfn binary_search(nums: &[i32], target: i32) -> i32 {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\nlet mut i = 0;\nlet mut j = nums.len() as i32 - 1;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\nwhile i <= j {\nlet m = i + (j - i) / 2;      // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m as usize] < target {         // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\n} else if nums[m as usize] > 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// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -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.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(m);\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n

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

\u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(1)\\) \uff1a\u6307\u9488 \\(i\\) \u548c \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7a7a\u95f4\u3002

"},{"location":"chapter_searching/binary_search/#1011","title":"10.1.1 \u00a0 \u533a\u95f4\u8868\u793a\u65b9\u6cd5","text":"

\u9664\u4e86\u4e0a\u8ff0\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

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
binary_search.rs
/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nfn binary_search_lcro(nums: &[i32], target: i32) -> i32 {\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 mut i = 0;\nlet mut j = nums.len() as i32;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nwhile i < j {\nlet m = i + (j - i) / 2;      // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m as usize] < target {         // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\n} else if nums[m as usize] > target {  // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m - 1;\n} else {                      // \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.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.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(m);\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n

\u5982\u56fe 10-3 \u6240\u793a\uff0c\u5728\u4e24\u79cd\u533a\u95f4\u8868\u793a\u4e0b\uff0c\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u7684\u521d\u59cb\u5316\u3001\u5faa\u73af\u6761\u4ef6\u548c\u7f29\u5c0f\u533a\u95f4\u64cd\u4f5c\u7686\u6709\u6240\u4e0d\u540c\u3002

\u7531\u4e8e\u201c\u53cc\u95ed\u533a\u95f4\u201d\u8868\u793a\u4e2d\u7684\u5de6\u53f3\u8fb9\u754c\u90fd\u88ab\u5b9a\u4e49\u4e3a\u95ed\u533a\u95f4\uff0c\u56e0\u6b64\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\uff0c\u56e0\u6b64\u4e00\u822c\u5efa\u8bae\u91c7\u7528\u201c\u53cc\u95ed\u533a\u95f4\u201d\u7684\u5199\u6cd5\u3002

\u56fe 10-3 \u00a0 \u4e24\u79cd\u533a\u95f4\u5b9a\u4e49

"},{"location":"chapter_searching/binary_search/#1012","title":"10.1.2 \u00a0 \u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"

\u4e8c\u5206\u67e5\u627e\u5728\u65f6\u95f4\u548c\u7a7a\u95f4\u65b9\u9762\u90fd\u6709\u8f83\u597d\u7684\u6027\u80fd\u3002

  • \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\u987b\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\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002

  • \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.3 \u00a0 \u4e8c\u5206\u67e5\u627e\u8fb9\u754c","text":""},{"location":"chapter_searching/binary_search_edge/#1031","title":"10.3.1 \u00a0 \u67e5\u627e\u5de6\u8fb9\u754c","text":"

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\u8fd4\u56de\u6570\u7ec4\u4e2d\u6700\u5de6\u4e00\u4e2a\u5143\u7d20 target \u7684\u7d22\u5f15\u3002\u82e5\u6570\u7ec4\u4e2d\u4e0d\u5305\u542b\u8be5\u5143\u7d20\uff0c\u5219\u8fd4\u56de \\(-1\\) \u3002

\u56de\u5fc6\u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\u7684\u65b9\u6cd5\uff0c\u641c\u7d22\u5b8c\u6210\u540e \\(i\\) \u6307\u5411\u6700\u5de6\u4e00\u4e2a target \uff0c\u56e0\u6b64\u67e5\u627e\u63d2\u5165\u70b9\u672c\u8d28\u4e0a\u662f\u5728\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target \u7684\u7d22\u5f15\u3002

\u8003\u8651\u901a\u8fc7\u67e5\u627e\u63d2\u5165\u70b9\u7684\u51fd\u6570\u5b9e\u73b0\u67e5\u627e\u5de6\u8fb9\u754c\u3002\u8bf7\u6ce8\u610f\uff0c\u6570\u7ec4\u4e2d\u53ef\u80fd\u4e0d\u5305\u542b target \uff0c\u8fd9\u79cd\u60c5\u51b5\u53ef\u80fd\u5bfc\u81f4\u4ee5\u4e0b\u4e24\u79cd\u7ed3\u679c\u3002

  • \u63d2\u5165\u70b9\u7684\u7d22\u5f15 \\(i\\) \u8d8a\u754c\u3002
  • \u5143\u7d20 nums[i] \u4e0e target \u4e0d\u76f8\u7b49\u3002

\u5f53\u9047\u5230\u4ee5\u4e0a\u4e24\u79cd\u60c5\u51b5\u65f6\uff0c\u76f4\u63a5\u8fd4\u56de \\(-1\\) \u5373\u53ef\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_edge.py
def binary_search_left_edge(nums: list[int], target: int) -> int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target\"\"\"\n# \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\ni = binary_search_insertion(nums, target)\n# \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif i == len(nums) or nums[i] != target:\nreturn -1\n# \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i\n
binary_search_edge.cpp
/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(vector<int> &nums, int target) {\n// \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\nint i = binarySearchInsertion(nums, target);\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (i == nums.size() || nums[i] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i;\n}\n
binary_search_edge.java
/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(int[] nums, int target) {\n// \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\nint i = binary_search_insertion.binarySearchInsertion(nums, target);\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (i == nums.length || nums[i] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i;\n}\n
binary_search_edge.cs
/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint BinarySearchLeftEdge(int[] nums, int target) {\n// \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\nint i = binary_search_insertion.BinarySearchInsertion(nums, target);\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (i == nums.Length || nums[i] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i;\n}\n
binary_search_edge.go
/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfunc binarySearchLeftEdge(nums []int, target int) int {\n// \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\ni := binarySearchInsertion(nums, target)\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif i == len(nums) || nums[i] != target {\nreturn -1\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i\n}\n
binary_search_edge.swift
/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfunc binarySearchLeftEdge(nums: [Int], target: Int) -> Int {\n// \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\nlet i = binarySearchInsertion(nums: nums, target: target)\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif i == nums.count || nums[i] != target {\nreturn -1\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i\n}\n
binary_search_edge.js
/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfunction binarySearchLeftEdge(nums, target) {\n// \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\nconst i = binarySearchInsertion(nums, target);\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (i === nums.length || nums[i] !== target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i;\n}\n
binary_search_edge.ts
/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfunction binarySearchLeftEdge(nums: Array<number>, target: number): number {\n// \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\nconst i = binarySearchInsertion(nums, target);\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (i === nums.length || nums[i] !== target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i;\n}\n
binary_search_edge.dart
/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(List<int> nums, int target) {\n// \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\nint i = binarySearchInsertion(nums, target);\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (i == nums.length || nums[i] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i;\n}\n
binary_search_edge.rs
/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfn binary_search_left_edge(nums: &[i32], target: i32) -> i32 {\n// \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\nlet i = binary_search_insertion(nums, target);\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif i == nums.len() as i32 || nums[i as usize] != target {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\ni\n}\n
binary_search_edge.c
/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(int *nums, int numSize, int target) {\n// \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\nint i = binarySearchInsertion(nums, numSize, target);\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (i == numSize || nums[i] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i;\n}\n
binary_search_edge.zig
[class]{}-[func]{binarySearchLeftEdge}\n
"},{"location":"chapter_searching/binary_search_edge/#1032","title":"10.3.2 \u00a0 \u67e5\u627e\u53f3\u8fb9\u754c","text":"

\u90a3\u4e48\u5982\u4f55\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target \u5462\uff1f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\u662f\u4fee\u6539\u4ee3\u7801\uff0c\u66ff\u6362\u5728 nums[m] == target \u60c5\u51b5\u4e0b\u7684\u6307\u9488\u6536\u7f29\u64cd\u4f5c\u3002\u4ee3\u7801\u5728\u6b64\u7701\u7565\uff0c\u6709\u5174\u8da3\u7684\u540c\u5b66\u53ef\u4ee5\u81ea\u884c\u5b9e\u73b0\u3002

\u4e0b\u9762\u6211\u4eec\u4ecb\u7ecd\u4e24\u79cd\u66f4\u52a0\u53d6\u5de7\u7684\u65b9\u6cd5\u3002

"},{"location":"chapter_searching/binary_search_edge/#1","title":"1. \u00a0 \u590d\u7528\u67e5\u627e\u5de6\u8fb9\u754c","text":"

\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u67e5\u627e\u6700\u5de6\u5143\u7d20\u7684\u51fd\u6570\u6765\u67e5\u627e\u6700\u53f3\u5143\u7d20\uff0c\u5177\u4f53\u65b9\u6cd5\u4e3a\uff1a\u5c06\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\u3002

\u5982\u56fe 10-7 \u6240\u793a\uff0c\u67e5\u627e\u5b8c\u6210\u540e\uff0c\u6307\u9488 \\(i\\) \u6307\u5411\u6700\u5de6\u4e00\u4e2a target + 1\uff08\u5982\u679c\u5b58\u5728\uff09\uff0c\u800c \\(j\\) \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0c\u56e0\u6b64\u8fd4\u56de \\(j\\) \u5373\u53ef\u3002

\u56fe 10-7 \u00a0 \u5c06\u67e5\u627e\u53f3\u8fb9\u754c\u8f6c\u5316\u4e3a\u67e5\u627e\u5de6\u8fb9\u754c

\u8bf7\u6ce8\u610f\uff0c\u8fd4\u56de\u7684\u63d2\u5165\u70b9\u662f \\(i\\) \uff0c\u56e0\u6b64\u9700\u8981\u5c06\u5176\u51cf \\(1\\) \uff0c\u4ece\u800c\u83b7\u5f97 \\(j\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_edge.py
def binary_search_right_edge(nums: list[int], target: int) -> int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target\"\"\"\n# \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\ni = binary_search_insertion(nums, target + 1)\n# j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nj = i - 1\n# \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif j == -1 or nums[j] != target:\nreturn -1\n# \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j\n
binary_search_edge.cpp
/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(vector<int> &nums, int target) {\n// \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\nint i = binarySearchInsertion(nums, target + 1);\n// j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nint j = i - 1;\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (j == -1 || nums[j] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j;\n}\n
binary_search_edge.java
/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(int[] nums, int target) {\n// \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\nint i = binary_search_insertion.binarySearchInsertion(nums, target + 1);\n// j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nint j = i - 1;\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (j == -1 || nums[j] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j;\n}\n
binary_search_edge.cs
/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint BinarySearchRightEdge(int[] nums, int target) {\n// \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\nint i = binary_search_insertion.BinarySearchInsertion(nums, target + 1);\n// j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nint j = i - 1;\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (j == -1 || nums[j] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j;\n}\n
binary_search_edge.go
/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfunc binarySearchRightEdge(nums []int, target int) int {\n// \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\ni := binarySearchInsertion(nums, target+1)\n// j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nj := i - 1\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif j == -1 || nums[j] != target {\nreturn -1\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j\n}\n
binary_search_edge.swift
/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfunc binarySearchRightEdge(nums: [Int], target: Int) -> Int {\n// \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\nlet i = binarySearchInsertion(nums: nums, target: target + 1)\n// j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nlet j = i - 1\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif j == -1 || nums[j] != target {\nreturn -1\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j\n}\n
binary_search_edge.js
/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfunction binarySearchRightEdge(nums, target) {\n// \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\nconst i = binarySearchInsertion(nums, target + 1);\n// j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nconst j = i - 1;\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (j === -1 || nums[j] !== target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j;\n}\n
binary_search_edge.ts
/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfunction binarySearchRightEdge(nums: Array<number>, target: number): number {\n// \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\nconst i = binarySearchInsertion(nums, target + 1);\n// j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nconst j = i - 1;\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (j === -1 || nums[j] !== target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j;\n}\n
binary_search_edge.dart
/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(List<int> nums, int target) {\n// \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\nint i = binarySearchInsertion(nums, target + 1);\n// j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nint j = i - 1;\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (j == -1 || nums[j] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j;\n}\n
binary_search_edge.rs
/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfn binary_search_right_edge(nums: &[i32], target: i32) -> i32 {\n// \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\nlet i = binary_search_insertion(nums, target + 1);\n// j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nlet j = i - 1;\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif j == -1 || nums[j as usize] != target {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nj\n}\n
binary_search_edge.c
/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(int *nums, int numSize, int target) {\n// \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\nint i = binarySearchInsertion(nums, numSize, target + 1);\n// j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nint j = i - 1;\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (j == -1 || nums[j] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j;\n}\n
binary_search_edge.zig
[class]{}-[func]{binarySearchRightEdge}\n
"},{"location":"chapter_searching/binary_search_edge/#2","title":"2. \u00a0 \u8f6c\u5316\u4e3a\u67e5\u627e\u5143\u7d20","text":"

\u6211\u4eec\u77e5\u9053\uff0c\u5f53\u6570\u7ec4\u4e0d\u5305\u542b target \u65f6\uff0c\u6700\u7ec8 \\(i\\) \u548c \\(j\\) \u4f1a\u5206\u522b\u6307\u5411\u9996\u4e2a\u5927\u4e8e\u3001\u5c0f\u4e8e target \u7684\u5143\u7d20\u3002

\u56e0\u6b64\uff0c\u5982\u56fe 10-8 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u6784\u9020\u4e00\u4e2a\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728\u7684\u5143\u7d20\uff0c\u7528\u4e8e\u67e5\u627e\u5de6\u53f3\u8fb9\u754c\u3002

  • \u67e5\u627e\u6700\u5de6\u4e00\u4e2a target \uff1a\u53ef\u4ee5\u8f6c\u5316\u4e3a\u67e5\u627e target - 0.5 \uff0c\u5e76\u8fd4\u56de\u6307\u9488 \\(i\\) \u3002
  • \u67e5\u627e\u6700\u53f3\u4e00\u4e2a target \uff1a\u53ef\u4ee5\u8f6c\u5316\u4e3a\u67e5\u627e target + 0.5 \uff0c\u5e76\u8fd4\u56de\u6307\u9488 \\(j\\) \u3002

\u56fe 10-8 \u00a0 \u5c06\u67e5\u627e\u8fb9\u754c\u8f6c\u5316\u4e3a\u67e5\u627e\u5143\u7d20

\u4ee3\u7801\u5728\u6b64\u7701\u7565\uff0c\u503c\u5f97\u6ce8\u610f\u4ee5\u4e0b\u4e24\u70b9\u3002

  • \u7ed9\u5b9a\u6570\u7ec4\u4e0d\u5305\u542b\u5c0f\u6570\uff0c\u8fd9\u610f\u5473\u7740\u6211\u4eec\u65e0\u987b\u5173\u5fc3\u5982\u4f55\u5904\u7406\u76f8\u7b49\u7684\u60c5\u51b5\u3002
  • \u56e0\u4e3a\u8be5\u65b9\u6cd5\u5f15\u5165\u4e86\u5c0f\u6570\uff0c\u6240\u4ee5\u9700\u8981\u5c06\u51fd\u6570\u4e2d\u7684\u53d8\u91cf target \u6539\u4e3a\u6d6e\u70b9\u6570\u7c7b\u578b\u3002
"},{"location":"chapter_searching/binary_search_insertion/","title":"10.2 \u00a0 \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9","text":"

\u4e8c\u5206\u67e5\u627e\u4e0d\u4ec5\u53ef\u7528\u4e8e\u641c\u7d22\u76ee\u6807\u5143\u7d20\uff0c\u8fd8\u5177\u6709\u8bb8\u591a\u53d8\u79cd\u95ee\u9898\uff0c\u6bd4\u5982\u641c\u7d22\u76ee\u6807\u5143\u7d20\u7684\u63d2\u5165\u4f4d\u7f6e\u3002

"},{"location":"chapter_searching/binary_search_insertion/#1021","title":"10.2.1 \u00a0 \u65e0\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"

Question

\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6709\u5e8f\u6570\u7ec4 nums \u548c\u4e00\u4e2a\u5143\u7d20 target \uff0c\u6570\u7ec4\u4e0d\u5b58\u5728\u91cd\u590d\u5143\u7d20\u3002\u73b0\u5c06 target \u63d2\u5165\u5230\u6570\u7ec4 nums \u4e2d\uff0c\u5e76\u4fdd\u6301\u5176\u6709\u5e8f\u6027\u3002\u82e5\u6570\u7ec4\u4e2d\u5df2\u5b58\u5728\u5143\u7d20 target \uff0c\u5219\u63d2\u5165\u5230\u5176\u5de6\u65b9\u3002\u8bf7\u8fd4\u56de\u63d2\u5165\u540e target \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\u3002

\u56fe 10-4 \u00a0 \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\u793a\u4f8b\u6570\u636e

\u5982\u679c\u60f3\u8981\u590d\u7528\u4e0a\u8282\u7684\u4e8c\u5206\u67e5\u627e\u4ee3\u7801\uff0c\u5219\u9700\u8981\u56de\u7b54\u4ee5\u4e0b\u4e24\u4e2a\u95ee\u9898\u3002

\u95ee\u9898\u4e00\uff1a\u5f53\u6570\u7ec4\u4e2d\u5305\u542b target \u65f6\uff0c\u63d2\u5165\u70b9\u7684\u7d22\u5f15\u662f\u5426\u662f\u8be5\u5143\u7d20\u7684\u7d22\u5f15\uff1f

\u9898\u76ee\u8981\u6c42\u5c06 target \u63d2\u5165\u5230\u76f8\u7b49\u5143\u7d20\u7684\u5de6\u8fb9\uff0c\u8fd9\u610f\u5473\u7740\u65b0\u63d2\u5165\u7684 target \u66ff\u6362\u4e86\u539f\u6765 target \u7684\u4f4d\u7f6e\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u6570\u7ec4\u5305\u542b target \u65f6\uff0c\u63d2\u5165\u70b9\u7684\u7d22\u5f15\u5c31\u662f\u8be5 target \u7684\u7d22\u5f15\u3002

\u95ee\u9898\u4e8c\uff1a\u5f53\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728 target \u65f6\uff0c\u63d2\u5165\u70b9\u662f\u54ea\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\uff1f

\u8fdb\u4e00\u6b65\u601d\u8003\u4e8c\u5206\u67e5\u627e\u8fc7\u7a0b\uff1a\u5f53 nums[m] < target \u65f6 \\(i\\) \u79fb\u52a8\uff0c\u8fd9\u610f\u5473\u7740\u6307\u9488 \\(i\\) \u5728\u5411\u5927\u4e8e\u7b49\u4e8e target \u7684\u5143\u7d20\u9760\u8fd1\u3002\u540c\u7406\uff0c\u6307\u9488 \\(j\\) \u59cb\u7ec8\u5728\u5411\u5c0f\u4e8e\u7b49\u4e8e target \u7684\u5143\u7d20\u9760\u8fd1\u3002

\u56e0\u6b64\u4e8c\u5206\u7ed3\u675f\u65f6\u4e00\u5b9a\u6709\uff1a\\(i\\) \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\uff0c\\(j\\) \u6307\u5411\u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u3002\u6613\u5f97\u5f53\u6570\u7ec4\u4e0d\u5305\u542b target \u65f6\uff0c\u63d2\u5165\u7d22\u5f15\u4e3a \\(i\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_insertion.py
def binary_search_insertion_simple(nums: list[int], target: int) -> int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09\"\"\"\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:\nreturn m  # \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n# \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i\n
binary_search_insertion.cpp
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(vector<int> &nums, int target) {\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\n} else if (nums[m] > target) {\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nreturn m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n}\n}\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.java
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(int[] nums, int target) {\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\n} else if (nums[m] > target) {\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nreturn m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n}\n}\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.cs
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint BinarySearchInsertionSimple(int[] nums, int target) {\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\n} else if (nums[m] > target) {\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nreturn m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n}\n}\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.go
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertionSimple(nums []int, target int) int {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\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// \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\nreturn m\n}\n}\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i\n}\n
binary_search_insertion.swift
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertionSimple(nums: [Int], target: Int) -> Int {\nvar i = 0, j = nums.count - 1 // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-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 {\nreturn m // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n}\n}\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i\n}\n
binary_search_insertion.js
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfunction binarySearchInsertionSimple(nums, target) {\nlet i = 0,\nj = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nconst m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\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 {\nreturn m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n}\n}\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.ts
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfunction binarySearchInsertionSimple(\nnums: Array<number>,\ntarget: number\n): number {\nlet i = 0,\nj = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nconst m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\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 {\nreturn m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n}\n}\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.dart
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(List<int> nums, int target) {\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\n} else if (nums[m] > target) {\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nreturn m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n}\n}\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.rs
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\npub fn binary_search_insertion(nums: &[i32], target: i32) -> i32 {\nlet (mut i, mut j) = (0, nums.len() as i32 - 1);    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile i <= j {\nlet m = i + (j - i) / 2;    // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m as usize] < target {\ni = m + 1;  // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if nums[m as usize] > 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}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\ni\n}\n
binary_search_insertion.c
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(int *nums, int numSize, int target) {\nint i = 0, j = numSize - 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\n} else if (nums[m] > target) {\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nreturn m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n}\n}\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.zig
[class]{}-[func]{binarySearchInsertionSimple}\n
"},{"location":"chapter_searching/binary_search_insertion/#1022","title":"10.2.2 \u00a0 \u5b58\u5728\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"

Question

\u5728\u4e0a\u4e00\u9898\u7684\u57fa\u7840\u4e0a\uff0c\u89c4\u5b9a\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u5176\u4f59\u4e0d\u53d8\u3002

\u5047\u8bbe\u6570\u7ec4\u4e2d\u5b58\u5728\u591a\u4e2a target \uff0c\u5219\u666e\u901a\u4e8c\u5206\u67e5\u627e\u53ea\u80fd\u8fd4\u56de\u5176\u4e2d\u4e00\u4e2a target \u7684\u7d22\u5f15\uff0c\u800c\u65e0\u6cd5\u786e\u5b9a\u8be5\u5143\u7d20\u7684\u5de6\u8fb9\u548c\u53f3\u8fb9\u8fd8\u6709\u591a\u5c11 target\u3002

\u9898\u76ee\u8981\u6c42\u5c06\u76ee\u6807\u5143\u7d20\u63d2\u5165\u5230\u6700\u5de6\u8fb9\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5de6\u4e00\u4e2a target \u7684\u7d22\u5f15\u3002\u521d\u6b65\u8003\u8651\u901a\u8fc7\u56fe 10-5 \u6240\u793a\u7684\u6b65\u9aa4\u5b9e\u73b0\u3002

  1. \u6267\u884c\u4e8c\u5206\u67e5\u627e\uff0c\u5f97\u5230\u4efb\u610f\u4e00\u4e2a target \u7684\u7d22\u5f15\uff0c\u8bb0\u4e3a \\(k\\) \u3002
  2. \u4ece\u7d22\u5f15 \\(k\\) \u5f00\u59cb\uff0c\u5411\u5de6\u8fdb\u884c\u7ebf\u6027\u904d\u5386\uff0c\u5f53\u627e\u5230\u6700\u5de6\u8fb9\u7684 target \u65f6\u8fd4\u56de\u3002

\u56fe 10-5 \u00a0 \u7ebf\u6027\u67e5\u627e\u91cd\u590d\u5143\u7d20\u7684\u63d2\u5165\u70b9

\u6b64\u65b9\u6cd5\u867d\u7136\u53ef\u7528\uff0c\u4f46\u5176\u5305\u542b\u7ebf\u6027\u67e5\u627e\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002\u5f53\u6570\u7ec4\u4e2d\u5b58\u5728\u5f88\u591a\u91cd\u590d\u7684 target \u65f6\uff0c\u8be5\u65b9\u6cd5\u6548\u7387\u5f88\u4f4e\u3002

\u73b0\u8003\u8651\u62d3\u5c55\u4e8c\u5206\u67e5\u627e\u4ee3\u7801\u3002\u5982\u56fe 10-6 \u6240\u793a\uff0c\u6574\u4f53\u6d41\u7a0b\u4fdd\u6301\u4e0d\u53d8\uff0c\u6bcf\u8f6e\u5148\u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 \\(m\\) \uff0c\u518d\u5224\u65ad target \u548c nums[m] \u5927\u5c0f\u5173\u7cfb\uff0c\u5206\u4e3a\u4ee5\u4e0b\u51e0\u79cd\u60c5\u51b5\u3002

  • \u5f53 nums[m] < target \u6216 nums[m] > target \u65f6\uff0c\u8bf4\u660e\u8fd8\u6ca1\u6709\u627e\u5230 target \uff0c\u56e0\u6b64\u91c7\u7528\u666e\u901a\u4e8c\u5206\u67e5\u627e\u7684\u7f29\u5c0f\u533a\u95f4\u64cd\u4f5c\uff0c\u4ece\u800c\u4f7f\u6307\u9488 \\(i\\) \u548c \\(j\\) \u5411 target \u9760\u8fd1\u3002
  • \u5f53 nums[m] == target \u65f6\uff0c\u8bf4\u660e\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 \\([i, m - 1]\\) \u4e2d\uff0c\u56e0\u6b64\u91c7\u7528 \\(j = m - 1\\) \u6765\u7f29\u5c0f\u533a\u95f4\uff0c\u4ece\u800c\u4f7f\u6307\u9488 \\(j\\) \u5411\u5c0f\u4e8e target \u7684\u5143\u7d20\u9760\u8fd1\u3002

\u5faa\u73af\u5b8c\u6210\u540e\uff0c\\(i\\) \u6307\u5411\u6700\u5de6\u8fb9\u7684 target \uff0c\\(j\\) \u6307\u5411\u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\uff0c\u56e0\u6b64\u7d22\u5f15 \\(i\\) \u5c31\u662f\u63d2\u5165\u70b9\u3002

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

\u56fe 10-6 \u00a0 \u4e8c\u5206\u67e5\u627e\u91cd\u590d\u5143\u7d20\u7684\u63d2\u5165\u70b9\u7684\u6b65\u9aa4

\u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u5224\u65ad\u5206\u652f nums[m] > target \u548c nums[m] == target \u7684\u64cd\u4f5c\u76f8\u540c\uff0c\u56e0\u6b64\u4e24\u8005\u53ef\u4ee5\u5408\u5e76\u3002

\u5373\u4fbf\u5982\u6b64\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u5c06\u5224\u65ad\u6761\u4ef6\u4fdd\u6301\u5c55\u5f00\uff0c\u56e0\u4e3a\u5176\u903b\u8f91\u66f4\u52a0\u6e05\u6670\u3001\u53ef\u8bfb\u6027\u66f4\u597d\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_insertion.py
def binary_search_insertion(nums: list[int], target: int) -> int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09\"\"\"\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\n# \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i\n
binary_search_insertion.cpp
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(vector<int> &nums, int target) {\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\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}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.java
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(int[] nums, int target) {\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\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}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.cs
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint BinarySearchInsertion(int[] nums, int target) {\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\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}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.go
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertion(nums []int, target int) int {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\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}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i\n}\n
binary_search_insertion.swift
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertion(nums: [Int], target: Int) -> Int {\nvar i = 0, j = nums.count - 1 // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-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 {\nj = m - 1 // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n}\n}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i\n}\n
binary_search_insertion.js
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfunction binarySearchInsertion(nums, target) {\nlet i = 0,\nj = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nconst m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\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}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.ts
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfunction binarySearchInsertion(nums: Array<number>, target: number): number {\nlet i = 0,\nj = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nconst m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\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}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.dart
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(List<int> nums, int target) {\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\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}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.rs
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\npub fn binary_search_insertion(nums: &[i32], target: i32) -> i32 {\nlet (mut i, mut j) = (0, nums.len() as i32 - 1);    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile i <= j {\nlet m = i + (j - i) / 2;    // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m as usize] < target {\ni = m + 1;  // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if nums[m as usize] > 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}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\ni\n}\n
binary_search_insertion.c
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(int *nums, int numSize, int target) {\nint i = 0, j = numSize - 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\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}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.zig
[class]{}-[func]{binarySearchInsertion}\n

Tip

\u672c\u8282\u7684\u4ee3\u7801\u90fd\u662f\u201c\u53cc\u95ed\u533a\u95f4\u201d\u5199\u6cd5\u3002\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u5b9e\u73b0\u201c\u5de6\u95ed\u53f3\u5f00\u201d\u5199\u6cd5\u3002

\u603b\u7684\u6765\u770b\uff0c\u4e8c\u5206\u67e5\u627e\u65e0\u975e\u5c31\u662f\u7ed9\u6307\u9488 \\(i\\) \u548c \\(j\\) \u5206\u522b\u8bbe\u5b9a\u641c\u7d22\u76ee\u6807\uff0c\u76ee\u6807\u53ef\u80fd\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u5143\u7d20\uff08\u4f8b\u5982 target \uff09\uff0c\u4e5f\u53ef\u80fd\u662f\u4e00\u4e2a\u5143\u7d20\u8303\u56f4\uff08\u4f8b\u5982\u5c0f\u4e8e target \u7684\u5143\u7d20\uff09\u3002

\u5728\u4e0d\u65ad\u7684\u5faa\u73af\u4e8c\u5206\u4e2d\uff0c\u6307\u9488 \\(i\\) \u548c \\(j\\) \u90fd\u9010\u6e10\u903c\u8fd1\u9884\u5148\u8bbe\u5b9a\u7684\u76ee\u6807\u3002\u6700\u7ec8\uff0c\u5b83\u4eec\u6216\u662f\u6210\u529f\u627e\u5230\u7b54\u6848\uff0c\u6216\u662f\u8d8a\u8fc7\u8fb9\u754c\u540e\u505c\u6b62\u3002

"},{"location":"chapter_searching/replace_linear_by_hashing/","title":"10.4 \u00a0 \u54c8\u5e0c\u4f18\u5316\u7b56\u7565","text":"

\u5728\u7b97\u6cd5\u9898\u4e2d\uff0c\u6211\u4eec\u5e38\u901a\u8fc7\u5c06\u7ebf\u6027\u67e5\u627e\u66ff\u6362\u4e3a\u54c8\u5e0c\u67e5\u627e\u6765\u964d\u4f4e\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002\u6211\u4eec\u501f\u52a9\u4e00\u4e2a\u7b97\u6cd5\u9898\u6765\u52a0\u6df1\u7406\u89e3\u3002

Question

\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums \u548c\u4e00\u4e2a\u76ee\u6807\u5143\u7d20 target \uff0c\u8bf7\u5728\u6570\u7ec4\u4e2d\u641c\u7d22\u201c\u548c\u201d\u4e3a target \u7684\u4e24\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u6570\u7ec4\u7d22\u5f15\u3002\u8fd4\u56de\u4efb\u610f\u4e00\u4e2a\u89e3\u5373\u53ef\u3002

"},{"location":"chapter_searching/replace_linear_by_hashing/#1041","title":"10.4.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\u5982\u56fe 10-9 \u6240\u793a\uff0c\u6211\u4eec\u5f00\u542f\u4e00\u4e2a\u4e24\u5c42\u5faa\u73af\uff0c\u5728\u6bcf\u8f6e\u4e2d\u5224\u65ad\u4e24\u4e2a\u6574\u6570\u7684\u548c\u662f\u5426\u4e3a target \uff0c\u82e5\u662f\u5219\u8fd4\u56de\u5b83\u4eec\u7684\u7d22\u5f15\u3002

\u56fe 10-9 \u00a0 \u7ebf\u6027\u67e5\u627e\u6c42\u89e3\u4e24\u6570\u4e4b\u548c

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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 Array.Empty<int>();\n}\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.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.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.dart
/* \u65b9\u6cd5\u4e00\uff1a \u66b4\u529b\u679a\u4e3e */\nList<int> twoSumBruteForce(List<int> nums, int target) {\nint size = nums.length;\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\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
two_sum.rs
/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\npub fn two_sum_brute_force(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {\nlet size = nums.len();\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor i in 0..size - 1 {\nfor j in i + 1..size {\nif nums[i] + nums[j] == target {\nreturn Some(vec![i as i32, j as i32]);\n}\n}\n}\nNone\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.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(i), @intCast(j)};\n}\n}\n}\nreturn null;\n}\n

\u6b64\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff0c\u5728\u5927\u6570\u636e\u91cf\u4e0b\u975e\u5e38\u8017\u65f6\u3002

"},{"location":"chapter_searching/replace_linear_by_hashing/#1042","title":"10.4.2 \u00a0 \u54c8\u5e0c\u67e5\u627e\uff1a\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4","text":"

\u8003\u8651\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868\uff0c\u952e\u503c\u5bf9\u5206\u522b\u4e3a\u6570\u7ec4\u5143\u7d20\u548c\u5143\u7d20\u7d22\u5f15\u3002\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u6bcf\u8f6e\u6267\u884c\u56fe 10-10 \u6240\u793a\u7684\u6b65\u9aa4\u3002

  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\u3002
  2. \u5c06\u952e\u503c\u5bf9 nums[i] \u548c\u7d22\u5f15 i \u6dfb\u52a0\u8fdb\u54c8\u5e0c\u8868\u3002
<1><2><3>

\u56fe 10-10 \u00a0 \u8f85\u52a9\u54c8\u5e0c\u8868\u6c42\u89e3\u4e24\u6570\u4e4b\u548c

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

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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 Array.Empty<int>();\n}\n
two_sum.go
/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunc twoSumHashTable(nums []int, target int) []int {\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 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.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.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[target - nums[i]] !== undefined) {\nreturn [m[target - nums[i]], i];\n} else {\nm[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(target - nums[i]);\nif (index !== undefined) {\nreturn [index, i];\n} else {\nm.set(nums[i], i);\n}\n}\nreturn [];\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;\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nMap<int, int> dic = HashMap();\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\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
two_sum.rs
/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\npub fn two_sum_hash_table(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nlet mut dic = HashMap::new();\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor (i, num) in nums.iter().enumerate() {\nmatch dic.get(&(target - num)) {\nSome(v) => return Some(vec![*v as i32, i as i32]),\nNone => dic.insert(num, i as i32)\n};\n}\nNone\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.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(i)};\n}\ntry dic.put(nums[i], @intCast(i));\n}\nreturn null;\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.5 \u00a0 \u91cd\u8bc6\u641c\u7d22\u7b97\u6cd5","text":"

\u300c\u641c\u7d22\u7b97\u6cd5 searching algorithm\u300d\u7528\u4e8e\u5728\u6570\u636e\u7ed3\u6784\uff08\u4f8b\u5982\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6811\u6216\u56fe\uff09\u4e2d\u641c\u7d22\u4e00\u4e2a\u6216\u4e00\u7ec4\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002

\u641c\u7d22\u7b97\u6cd5\u53ef\u6839\u636e\u5b9e\u73b0\u601d\u8def\u5206\u4e3a\u4ee5\u4e0b\u4e24\u7c7b\u3002

  • \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/#1051","title":"10.5.1 \u00a0 \u66b4\u529b\u641c\u7d22","text":"

\u66b4\u529b\u641c\u7d22\u901a\u8fc7\u904d\u5386\u6570\u636e\u7ed3\u6784\u7684\u6bcf\u4e2a\u5143\u7d20\u6765\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\u3002

  • \u201c\u7ebf\u6027\u641c\u7d22\u201d\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
  • \u201c\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u201d\u548c\u201c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u201d\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\u987b\u5bf9\u6570\u636e\u505a\u9884\u5904\u7406\u548c\u501f\u52a9\u989d\u5916\u7684\u6570\u636e\u7ed3\u6784\u3002

\u7136\u800c\uff0c\u6b64\u7c7b\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u5143\u7d20\u6570\u91cf\uff0c\u56e0\u6b64\u5728\u6570\u636e\u91cf\u8f83\u5927\u7684\u60c5\u51b5\u4e0b\u6027\u80fd\u8f83\u5dee\u3002

"},{"location":"chapter_searching/searching_algorithm_revisited/#1052","title":"10.5.2 \u00a0 \u81ea\u9002\u5e94\u641c\u7d22","text":"

\u81ea\u9002\u5e94\u641c\u7d22\u5229\u7528\u6570\u636e\u7684\u7279\u6709\u5c5e\u6027\uff08\u4f8b\u5982\u6709\u5e8f\u6027\uff09\u6765\u4f18\u5316\u641c\u7d22\u8fc7\u7a0b\uff0c\u4ece\u800c\u66f4\u9ad8\u6548\u5730\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\u3002

  • \u201c\u4e8c\u5206\u67e5\u627e\u201d\u5229\u7528\u6570\u636e\u7684\u6709\u5e8f\u6027\u5b9e\u73b0\u9ad8\u6548\u67e5\u627e\uff0c\u4ec5\u9002\u7528\u4e8e\u6570\u7ec4\u3002
  • \u201c\u54c8\u5e0c\u67e5\u627e\u201d\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
  • \u201c\u6811\u67e5\u627e\u201d\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/#1053","title":"10.5.3 \u00a0 \u641c\u7d22\u65b9\u6cd5\u9009\u53d6","text":"

\u7ed9\u5b9a\u5927\u5c0f\u4e3a \\(n\\) \u7684\u4e00\u7ec4\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7ebf\u6027\u641c\u7d22\u3001\u4e8c\u5206\u67e5\u627e\u3001\u6811\u67e5\u627e\u3001\u54c8\u5e0c\u67e5\u627e\u7b49\u591a\u79cd\u65b9\u6cd5\u5728\u8be5\u6570\u636e\u4e2d\u641c\u7d22\u76ee\u6807\u5143\u7d20\u3002\u5404\u4e2a\u65b9\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u5982\u56fe 10-11 \u6240\u793a\u3002

\u56fe 10-11 \u00a0 \u591a\u79cd\u641c\u7d22\u7b56\u7565

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

\u8868 10-1 \u00a0 \u67e5\u627e\u7b97\u6cd5\u6548\u7387\u5bf9\u6bd4

\u7ebf\u6027\u641c\u7d22 \u4e8c\u5206\u67e5\u627e \u6811\u67e5\u627e \u54c8\u5e0c\u67e5\u627e \u67e5\u627e\u5143\u7d20 \\(O(n)\\) \\(O(\\log n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u63d2\u5165\u5143\u7d20 \\(O(1)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u989d\u5916\u7a7a\u95f4 \\(O(1)\\) \\(O(1)\\) \\(O(n)\\) \\(O(n)\\) \u6570\u636e\u9884\u5904\u7406 / \u6392\u5e8f \\(O(n \\log n)\\) \u5efa\u6811 \\(O(n \\log n)\\) \u5efa\u54c8\u5e0c\u8868 \\(O(n)\\) \u6570\u636e\u662f\u5426\u6709\u5e8f \u65e0\u5e8f \u6709\u5e8f \u6709\u5e8f \u65e0\u5e8f

\u641c\u7d22\u7b97\u6cd5\u7684\u9009\u62e9\u8fd8\u53d6\u51b3\u4e8e\u6570\u636e\u4f53\u91cf\u3001\u641c\u7d22\u6027\u80fd\u8981\u6c42\u3001\u6570\u636e\u67e5\u8be2\u4e0e\u66f4\u65b0\u9891\u7387\u7b49\u3002

\u7ebf\u6027\u641c\u7d22

  • \u901a\u7528\u6027\u8f83\u597d\uff0c\u65e0\u987b\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\u5206\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.6 \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\u987b\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\u987b\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":"\u7b2c 11 \u7ae0 \u00a0 \u6392\u5e8f","text":"

Abstract

\u6392\u5e8f\u72b9\u5982\u4e00\u628a\u5c06\u6df7\u4e71\u53d8\u4e3a\u79e9\u5e8f\u7684\u9b54\u6cd5\u94a5\u5319\uff0c\u4f7f\u6211\u4eec\u80fd\u4ee5\u66f4\u9ad8\u6548\u7684\u65b9\u5f0f\u7406\u89e3\u4e0e\u5904\u7406\u6570\u636e\u3002

\u65e0\u8bba\u662f\u7b80\u5355\u7684\u5347\u5e8f\uff0c\u8fd8\u662f\u590d\u6742\u7684\u5206\u7c7b\u6392\u5217\uff0c\u6392\u5e8f\u90fd\u5411\u6211\u4eec\u5c55\u793a\u4e86\u6570\u636e\u7684\u548c\u8c10\u7f8e\u611f\u3002

"},{"location":"chapter_sorting/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 11.1 \u00a0 \u6392\u5e8f\u7b97\u6cd5
  • 11.2 \u00a0 \u9009\u62e9\u6392\u5e8f
  • 11.3 \u00a0 \u5192\u6ce1\u6392\u5e8f
  • 11.4 \u00a0 \u63d2\u5165\u6392\u5e8f
  • 11.5 \u00a0 \u5feb\u901f\u6392\u5e8f
  • 11.6 \u00a0 \u5f52\u5e76\u6392\u5e8f
  • 11.7 \u00a0 \u5806\u6392\u5e8f
  • 11.8 \u00a0 \u6876\u6392\u5e8f
  • 11.9 \u00a0 \u8ba1\u6570\u6392\u5e8f
  • 11.10 \u00a0 \u57fa\u6570\u6392\u5e8f
  • 11.11 \u00a0 \u5c0f\u7ed3
"},{"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

\u5982\u56fe 11-4 \u6240\u793a\uff0c\u5192\u6ce1\u8fc7\u7a0b\u53ef\u4ee5\u5229\u7528\u5143\u7d20\u4ea4\u6362\u64cd\u4f5c\u6765\u6a21\u62df\uff1a\u4ece\u6570\u7ec4\u6700\u5de6\u7aef\u5f00\u59cb\u5411\u53f3\u904d\u5386\uff0c\u4f9d\u6b21\u6bd4\u8f83\u76f8\u90bb\u5143\u7d20\u5927\u5c0f\uff0c\u5982\u679c\u201c\u5de6\u5143\u7d20 > \u53f3\u5143\u7d20\u201d\u5c31\u4ea4\u6362\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>

\u56fe 11-4 \u00a0 \u5229\u7528\u5143\u7d20\u4ea4\u6362\u64cd\u4f5c\u6a21\u62df\u5192\u6ce1

"},{"location":"chapter_sorting/bubble_sort/#1131","title":"11.3.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"

\u8bbe\u6570\u7ec4\u7684\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u5192\u6ce1\u6392\u5e8f\u7684\u6b65\u9aa4\u5982\u56fe 11-5 \u6240\u793a\u3002

  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\u987b\u6392\u5e8f\uff0c\u56e0\u6b64\u6570\u7ec4\u6392\u5e8f\u5b8c\u6210\u3002

\u56fe 11-5 \u00a0 \u5192\u6ce1\u6392\u5e8f\u6d41\u7a0b

PythonC++JavaC#GoSwiftJSTSDartRustCZig bubble_sort.py
def bubble_sort(nums: list[int]):\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.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.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.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]\n(nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n}\n}\n}\n}\n
bubble_sort.go
/* \u5192\u6ce1\u6392\u5e8f */\nfunc bubbleSort(nums []int) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\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.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.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.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
bubble_sort.rs
/* \u5192\u6ce1\u6392\u5e8f */\nfn bubble_sort(nums: &mut [i32]) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor i in (1..nums.len()).rev() {\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \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;\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.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
"},{"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

PythonC++JavaC#GoSwiftJSTSDartRustCZig bubble_sort.py
def bubble_sort_with_flag(nums: list[int]):\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.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.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.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]\n(nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\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.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.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.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.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
bubble_sort.rs
/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09 */\nfn bubble_sort_with_flag(nums: &mut [i32]) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor i in (1..nums.len()).rev() {\nlet mut flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \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;\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.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
"},{"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\\)\u3001\\(n - 2\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \uff0c\u603b\u548c\u4e3a \\((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\\) \u548c \\(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\u9636\u3002

\u300c\u6876\u6392\u5e8f bucket sort\u300d\u662f\u5206\u6cbb\u7b56\u7565\u7684\u4e00\u4e2a\u5178\u578b\u5e94\u7528\u3002\u5b83\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e9b\u5177\u6709\u5927\u5c0f\u987a\u5e8f\u7684\u6876\uff0c\u6bcf\u4e2a\u6876\u5bf9\u5e94\u4e00\u4e2a\u6570\u636e\u8303\u56f4\uff0c\u5c06\u6570\u636e\u5e73\u5747\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\uff1b\u7136\u540e\uff0c\u5728\u6bcf\u4e2a\u6876\u5185\u90e8\u5206\u522b\u6267\u884c\u6392\u5e8f\uff1b\u6700\u7ec8\u6309\u7167\u6876\u7684\u987a\u5e8f\u5c06\u6240\u6709\u6570\u636e\u5408\u5e76\u3002

"},{"location":"chapter_sorting/bucket_sort/#1181","title":"11.8.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"

\u8003\u8651\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\uff0c\u5143\u7d20\u662f\u8303\u56f4 \\([0, 1)\\) \u7684\u6d6e\u70b9\u6570\u3002\u6876\u6392\u5e8f\u7684\u6d41\u7a0b\u5982\u56fe 11-13 \u6240\u793a\u3002

  1. \u521d\u59cb\u5316 \\(k\\) \u4e2a\u6876\uff0c\u5c06 \\(n\\) \u4e2a\u5143\u7d20\u5206\u914d\u5230 \\(k\\) \u4e2a\u6876\u4e2d\u3002
  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\u3002
  3. \u6309\u7167\u6876\u7684\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\uff0c\u5408\u5e76\u7ed3\u679c\u3002

\u56fe 11-13 \u00a0 \u6876\u6392\u5e8f\u7b97\u6cd5\u6d41\u7a0b

PythonC++JavaC#GoSwiftJSTSDartRustCZig bucket_sort.py
def bucket_sort(nums: list[float]):\n\"\"\"\u6876\u6392\u5e8f\"\"\"\n# \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\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\u5e8f\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.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.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.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();\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.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.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 * Double(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.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.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
bucket_sort.rs
/* \u6876\u6392\u5e8f */\nfn bucket_sort(nums: &mut [f64]) {\n// \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\nlet k = nums.len() / 2;\nlet mut buckets = vec![vec![]; k];\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor &mut num in &mut *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 = (num * k as f64) as usize;\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\nbuckets[i].push(num);\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor bucket in &mut buckets {\n// \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\nbucket.sort_by(|a, b| a.partial_cmp(b).unwrap());\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nlet mut i = 0;\nfor bucket in &mut buckets {\nfor &mut num in bucket {\nnums[i] = num;\ni += 1;\n}\n}\n}\n
bucket_sort.c
/* \u6876\u6392\u5e8f */\nvoid bucketSort(float nums[], int size) {\n// \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\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.zig
[class]{}-[func]{bucketSort}\n
"},{"location":"chapter_sorting/bucket_sort/#1182","title":"11.8.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"

\u6876\u6392\u5e8f\u9002\u7528\u4e8e\u5904\u7406\u4f53\u91cf\u5f88\u5927\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u8f93\u5165\u6570\u636e\u5305\u542b 100 \u4e07\u4e2a\u5143\u7d20\uff0c\u7531\u4e8e\u7a7a\u95f4\u9650\u5236\uff0c\u7cfb\u7edf\u5185\u5b58\u65e0\u6cd5\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u6570\u636e\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u5206\u6210 1000 \u4e2a\u6876\uff0c\u7136\u540e\u5206\u522b\u5bf9\u6bcf\u4e2a\u6876\u8fdb\u884c\u6392\u5e8f\uff0c\u6700\u540e\u5c06\u7ed3\u679c\u5408\u5e76\u3002

  • \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\u6240\u6709\u6876\u548c\u5143\u7d20\uff0c\u82b1\u8d39 \\(O(n + 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

\u5982\u56fe 11-14 \u6240\u793a\uff0c\u8fd9\u79cd\u65b9\u6cd5\u672c\u8d28\u4e0a\u662f\u521b\u5efa\u4e00\u4e2a\u9012\u5f52\u6811\uff0c\u76ee\u6807\u662f\u8ba9\u53f6\u8282\u70b9\u7684\u503c\u5c3d\u53ef\u80fd\u5e73\u5747\u3002\u5f53\u7136\uff0c\u4e0d\u4e00\u5b9a\u8981\u6bcf\u8f6e\u5c06\u6570\u636e\u5212\u5206\u4e3a 3 \u4e2a\u6876\uff0c\u5177\u4f53\u5212\u5206\u65b9\u5f0f\u53ef\u6839\u636e\u6570\u636e\u7279\u70b9\u7075\u6d3b\u9009\u62e9\u3002

\u56fe 11-14 \u00a0 \u9012\u5f52\u5212\u5206\u6876

\u5982\u679c\u6211\u4eec\u63d0\u524d\u77e5\u9053\u5546\u54c1\u4ef7\u683c\u7684\u6982\u7387\u5206\u5e03\uff0c\u5219\u53ef\u4ee5\u6839\u636e\u6570\u636e\u6982\u7387\u5206\u5e03\u8bbe\u7f6e\u6bcf\u4e2a\u6876\u7684\u4ef7\u683c\u5206\u754c\u7ebf\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6570\u636e\u5206\u5e03\u5e76\u4e0d\u4e00\u5b9a\u9700\u8981\u7279\u610f\u7edf\u8ba1\uff0c\u4e5f\u53ef\u4ee5\u6839\u636e\u6570\u636e\u7279\u70b9\u91c7\u7528\u67d0\u79cd\u6982\u7387\u6a21\u578b\u8fdb\u884c\u8fd1\u4f3c\u3002

\u5982\u56fe 11-15 \u6240\u793a\uff0c\u6211\u4eec\u5047\u8bbe\u5546\u54c1\u4ef7\u683c\u670d\u4ece\u6b63\u6001\u5206\u5e03\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5408\u7406\u5730\u8bbe\u5b9a\u4ef7\u683c\u533a\u95f4\uff0c\u4ece\u800c\u5c06\u5546\u54c1\u5e73\u5747\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\u3002

\u56fe 11-15 \u00a0 \u6839\u636e\u6982\u7387\u5206\u5e03\u5212\u5206\u6876

"},{"location":"chapter_sorting/counting_sort/","title":"11.9 \u00a0 \u8ba1\u6570\u6392\u5e8f","text":"

\u300c\u8ba1\u6570\u6392\u5e8f counting sort\u300d\u901a\u8fc7\u7edf\u8ba1\u5143\u7d20\u6570\u91cf\u6765\u5b9e\u73b0\u6392\u5e8f\uff0c\u901a\u5e38\u5e94\u7528\u4e8e\u6574\u6570\u6570\u7ec4\u3002

"},{"location":"chapter_sorting/counting_sort/#1191","title":"11.9.1 \u00a0 \u7b80\u5355\u5b9e\u73b0","text":"

\u5148\u6765\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u3002\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4 nums \uff0c\u5176\u4e2d\u7684\u5143\u7d20\u90fd\u662f\u201c\u975e\u8d1f\u6574\u6570\u201d\uff0c\u8ba1\u6570\u6392\u5e8f\u7684\u6574\u4f53\u6d41\u7a0b\u5982\u56fe 11-16 \u6240\u793a\u3002

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

\u56fe 11-16 \u00a0 \u8ba1\u6570\u6392\u5e8f\u6d41\u7a0b

PythonC++JavaC#GoSwiftJSTSDartRustCZig counting_sort.py
def counting_sort_naive(nums: list[int]):\n\"\"\"\u8ba1\u6570\u6392\u5e8f\"\"\"\n# \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\n# 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\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.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.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.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.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.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.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.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
counting_sort.rs
/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfn counting_sort_naive(nums: &mut [i32]) {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nlet m = *nums.into_iter().max().unwrap();\n// 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n// counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\nlet mut counter = vec![0; m as usize + 1];\nfor &num in &*nums {\ncounter[num as usize] += 1;\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nlet mut i = 0;\nfor num in 0..m + 1 {\nfor _ in 0..counter[num as usize] {\nnums[i] = num;\ni += 1;\n}\n}\n}\n
counting_sort.c
/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid countingSortNaive(int nums[], int size) {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\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.zig
[class]{}-[func]{countingSortNaive}\n

\u8ba1\u6570\u6392\u5e8f\u4e0e\u6876\u6392\u5e8f\u7684\u8054\u7cfb

\u4ece\u6876\u6392\u5e8f\u7684\u89d2\u5ea6\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8ba1\u6570\u6392\u5e8f\u4e2d\u7684\u8ba1\u6570\u6570\u7ec4 counter \u7684\u6bcf\u4e2a\u7d22\u5f15\u89c6\u4e3a\u4e00\u4e2a\u6876\uff0c\u5c06\u7edf\u8ba1\u6570\u91cf\u7684\u8fc7\u7a0b\u770b\u4f5c\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\u5047\u8bbe\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\u201c\u524d\u7f00\u548c\u201d\u3002\u987e\u540d\u601d\u4e49\uff0c\u7d22\u5f15 i \u5904\u7684\u524d\u7f00\u548c prefix[i] \u7b49\u4e8e\u6570\u7ec4\u524d i \u4e2a\u5143\u7d20\u4e4b\u548c\uff1a

\\[ \\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\u4ee5\u4e0b\u4e24\u6b65\u3002

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

\u904d\u5386\u5b8c\u6210\u540e\uff0c\u6570\u7ec4 res \u4e2d\u5c31\u662f\u6392\u5e8f\u597d\u7684\u7ed3\u679c\uff0c\u6700\u540e\u4f7f\u7528 res \u8986\u76d6\u539f\u6570\u7ec4 nums \u5373\u53ef\u3002\u56fe 11-17 \u5c55\u793a\u4e86\u5b8c\u6574\u7684\u8ba1\u6570\u6392\u5e8f\u6d41\u7a0b\u3002

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

\u56fe 11-17 \u00a0 \u8ba1\u6570\u6392\u5e8f\u6b65\u9aa4

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

PythonC++JavaC#GoSwiftJSTSDartRustCZig counting_sort.py
def counting_sort(nums: list[int]):\n\"\"\"\u8ba1\u6570\u6392\u5e8f\"\"\"\n# \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\n# 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\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.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.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.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.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.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.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.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
counting_sort.rs
/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfn counting_sort(nums: &mut [i32]) {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nlet m = *nums.into_iter().max().unwrap();\n// 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n// counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\nlet mut counter = vec![0; m as usize + 1];\nfor &num in &*nums {\ncounter[num as usize] += 1;\n}\n// 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n// \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\nfor i in 0..m as usize {\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\nlet n = nums.len();\nlet mut res = vec![0; n];\nfor i in (0..n).rev() {\nlet num = nums[i];\nres[counter[num as usize] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[num as usize] -= 1; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n}\n// \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\nfor i in 0..n {\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.zig
[class]{}-[func]{countingSort}\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\u201c\u5806\u201c\u7ae0\u8282\u3002

\u300c\u5806\u6392\u5e8f heap sort\u300d\u662f\u4e00\u79cd\u57fa\u4e8e\u5806\u6570\u636e\u7ed3\u6784\u5b9e\u73b0\u7684\u9ad8\u6548\u6392\u5e8f\u7b97\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5df2\u7ecf\u5b66\u8fc7\u7684\u201c\u5efa\u5806\u64cd\u4f5c\u201d\u548c\u201c\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\u201d\u5b9e\u73b0\u5806\u6392\u5e8f\u3002

  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\u56fe 11-12 \u6240\u793a\u3002

  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

Tip

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

\u56fe 11-12 \u00a0 \u5806\u6392\u5e8f\u6b65\u9aa4

\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 sift_down() \u51fd\u6570\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e\u5806\u7684\u957f\u5ea6\u4f1a\u968f\u7740\u63d0\u53d6\u6700\u5927\u5143\u7d20\u800c\u51cf\u5c0f\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u7ed9 sift_down() \u51fd\u6570\u6dfb\u52a0\u4e00\u4e2a\u957f\u5ea6\u53c2\u6570 \\(n\\) \uff0c\u7528\u4e8e\u6307\u5b9a\u5806\u7684\u5f53\u524d\u6709\u6548\u957f\u5ea6\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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\u987b\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.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\u987b\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.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\u987b\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.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\u987b\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.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\u987b\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.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\u987b\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.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\u987b\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\u987b\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.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\u987b\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
heap_sort.rs
/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfn sift_down(nums: &mut [i32], n: usize, mut i: usize) {\nloop {\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 mut 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\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif ma == i {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nlet 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 */\nfn heap_sort(nums: &mut [i32]) {\n// \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor i in (0..=nums.len() / 2 - 1).rev() {\nsift_down(nums, nums.len(), i);\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor i in (1..=nums.len() - 1).rev() {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nlet 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\nsift_down(nums, i, 0);\n}\n}\n
heap_sort.c
/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int nums[], int n, int i) {\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\u987b\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.zig
[class]{}-[func]{siftDown}\n[class]{}-[func]{heapSort}\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

\u56fe 11-6 \u5c55\u793a\u4e86\u6570\u7ec4\u63d2\u5165\u5143\u7d20\u7684\u64cd\u4f5c\u6d41\u7a0b\u3002\u8bbe\u57fa\u51c6\u5143\u7d20\u4e3a base \uff0c\u6211\u4eec\u9700\u8981\u5c06\u4ece\u76ee\u6807\u7d22\u5f15\u5230 base \u4e4b\u95f4\u7684\u6240\u6709\u5143\u7d20\u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\uff0c\u7136\u540e\u518d\u5c06 base \u8d4b\u503c\u7ed9\u76ee\u6807\u7d22\u5f15\u3002

\u56fe 11-6 \u00a0 \u5355\u6b21\u63d2\u5165\u64cd\u4f5c

"},{"location":"chapter_sorting/insertion_sort/#1141","title":"11.4.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"

\u63d2\u5165\u6392\u5e8f\u7684\u6574\u4f53\u6d41\u7a0b\u5982\u56fe 11-7 \u6240\u793a\u3002

  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

\u56fe 11-7 \u00a0 \u63d2\u5165\u6392\u5e8f\u6d41\u7a0b

PythonC++JavaC#GoSwiftJSTSDartRustCZig insertion_sort.py
def insertion_sort(nums: list[int]):\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.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.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.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.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.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.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.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
insertion_sort.rs
/* \u63d2\u5165\u6392\u5e8f */\nfn insertion_sort(nums: &mut [i32]) {\n// \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\nfor i in 1..nums.len() {\nlet (base, mut j) = (nums[i],  (i - 1) as i32);\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile j >= 0 && nums[j as usize] > base {\nnums[(j + 1) as usize] = nums[j as usize]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj -= 1;\n}\nnums[(j + 1) as usize] = base;  // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n}\n}\n
insertion_sort.c
/* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(int nums[], int size) {\n// \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\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.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
"},{"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\\)\u3001\\(n-2\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \u6b21\uff0c\u6c42\u548c\u5f97\u5230 \\((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\\) \u548c \\(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\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\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002

  • \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\u662f\u4e00\u79cd\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5305\u542b\u56fe 11-10 \u6240\u793a\u7684\u201c\u5212\u5206\u201d\u548c\u201c\u5408\u5e76\u201d\u9636\u6bb5\u3002

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

\u56fe 11-10 \u00a0 \u5f52\u5e76\u6392\u5e8f\u7684\u5212\u5206\u4e0e\u5408\u5e76\u9636\u6bb5

"},{"location":"chapter_sorting/merge_sort/#1161","title":"11.6.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"

\u5982\u56fe 11-11 \u6240\u793a\uff0c\u201c\u5212\u5206\u9636\u6bb5\u201d\u4ece\u9876\u81f3\u5e95\u9012\u5f52\u5730\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5207\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\u3002

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

\u201c\u5408\u5e76\u9636\u6bb5\u201d\u4ece\u5e95\u81f3\u9876\u5730\u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u5408\u5e76\u4e3a\u4e00\u4e2a\u6709\u5e8f\u6570\u7ec4\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4ece\u957f\u5ea6\u4e3a 1 \u7684\u5b50\u6570\u7ec4\u5f00\u59cb\u5408\u5e76\uff0c\u5408\u5e76\u9636\u6bb5\u4e2d\u7684\u6bcf\u4e2a\u5b50\u6570\u7ec4\u90fd\u662f\u6709\u5e8f\u7684\u3002

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

\u56fe 11-11 \u00a0 \u5f52\u5e76\u6392\u5e8f\u6b65\u9aa4

\u89c2\u5bdf\u53d1\u73b0\uff0c\u5f52\u5e76\u6392\u5e8f\u4e0e\u4e8c\u53c9\u6811\u540e\u5e8f\u904d\u5386\u7684\u9012\u5f52\u987a\u5e8f\u662f\u4e00\u81f4\u7684\u3002

  • \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
PythonC++JavaC#GoSwiftJSTSDartRustCZig merge_sort.py
def merge(nums: list[int], left: int, mid: int, right: int):\n\"\"\"\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\"\"\"\n# \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid]\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):\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.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.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.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.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.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.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.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
merge_sort.rs
/* \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: &mut [i32], left: usize, mid: usize, right: usize) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nlet tmp: Vec<i32> = nums[left..right + 1].to_vec();\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet (left_start, left_end) = (left - left, mid - left);\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet (right_start, right_end) = (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\nlet (mut l_corrent, mut r_corrent) = (left_start, 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 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 l_corrent > left_end {\nnums[k] = tmp[r_corrent];\nr_corrent += 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 r_corrent > right_end || tmp[l_corrent] <= tmp[r_corrent] {\nnums[k] = tmp[l_corrent];\nl_corrent += 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[r_corrent];\nr_corrent += 1;\n}\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nfn merge_sort(left: usize, right: usize, nums: &mut [i32]) {\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 = (left + right) / 2;     // \u8ba1\u7b97\u4e2d\u70b9\nmerge_sort(left, mid, nums);      // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmerge_sort(mid + 1, right, nums);  // \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.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

\u5b9e\u73b0\u5408\u5e76\u51fd\u6570 merge() \u5b58\u5728\u4ee5\u4e0b\u96be\u70b9\u3002

  • \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":"

\u5bf9\u4e8e\u94fe\u8868\uff0c\u5f52\u5e76\u6392\u5e8f\u76f8\u8f83\u4e8e\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\u5177\u6709\u663e\u8457\u4f18\u52bf\uff0c\u53ef\u4ee5\u5c06\u94fe\u8868\u6392\u5e8f\u4efb\u52a1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(1)\\) \u3002

  • \u5212\u5206\u9636\u6bb5\uff1a\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u201c\u8fed\u4ee3\u201d\u66ff\u4ee3\u201c\u9012\u5f52\u201d\u6765\u5b9e\u73b0\u94fe\u8868\u5212\u5206\u5de5\u4f5c\uff0c\u4ece\u800c\u7701\u53bb\u9012\u5f52\u4f7f\u7528\u7684\u6808\u5e27\u7a7a\u95f4\u3002
  • \u5408\u5e76\u9636\u6bb5\uff1a\u5728\u94fe\u8868\u4e2d\uff0c\u8282\u70b9\u589e\u5220\u64cd\u4f5c\u4ec5\u9700\u6539\u53d8\u5f15\u7528\uff08\u6307\u9488\uff09\u5373\u53ef\u5b9e\u73b0\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\u987b\u521b\u5efa\u989d\u5916\u94fe\u8868\u3002

\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\u7b56\u7565\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u8fd0\u884c\u9ad8\u6548\uff0c\u5e94\u7528\u5e7f\u6cdb\u3002

\u5feb\u901f\u6392\u5e8f\u7684\u6838\u5fc3\u64cd\u4f5c\u662f\u201c\u54e8\u5175\u5212\u5206\u201d\uff0c\u5176\u76ee\u6807\u662f\uff1a\u9009\u62e9\u6570\u7ec4\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\u4f5c\u4e3a\u201c\u57fa\u51c6\u6570\u201d\uff0c\u5c06\u6240\u6709\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\u79fb\u5230\u5176\u5de6\u4fa7\uff0c\u800c\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\u79fb\u5230\u5176\u53f3\u4fa7\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u54e8\u5175\u5212\u5206\u7684\u6d41\u7a0b\u5982\u56fe 11-8 \u6240\u793a\u3002

  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\u3002
  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\u3002
  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\u3002
<1><2><3><4><5><6><7><8><9>

\u56fe 11-8 \u00a0 \u54e8\u5175\u5212\u5206\u6b65\u9aa4

\u54e8\u5175\u5212\u5206\u5b8c\u6210\u540e\uff0c\u539f\u6570\u7ec4\u88ab\u5212\u5206\u6210\u4e09\u90e8\u5206\uff1a\u5de6\u5b50\u6570\u7ec4\u3001\u57fa\u51c6\u6570\u3001\u53f3\u5b50\u6570\u7ec4\uff0c\u4e14\u6ee1\u8db3\u201c\u5de6\u5b50\u6570\u7ec4\u4efb\u610f\u5143\u7d20 \\(\\leq\\) \u57fa\u51c6\u6570 \\(\\leq\\) \u53f3\u5b50\u6570\u7ec4\u4efb\u610f\u5143\u7d20\u201d\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u63a5\u4e0b\u6765\u53ea\u9700\u5bf9\u8fd9\u4e24\u4e2a\u5b50\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002

\u5feb\u901f\u6392\u5e8f\u7684\u5206\u6cbb\u7b56\u7565

\u54e8\u5175\u5212\u5206\u7684\u5b9e\u8d28\u662f\u5c06\u4e00\u4e2a\u8f83\u957f\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\u7b80\u5316\u4e3a\u4e24\u4e2a\u8f83\u77ed\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig quick_sort.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.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.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.cs
/* \u5143\u7d20\u4ea4\u6362 */\nvoid Swap(int[] nums, int i, int j) {\n(nums[j], nums[i]) = (nums[i], nums[j]);\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.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.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.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.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
quick_sort.rs
/* \u54e8\u5175\u5212\u5206 */\nfn partition(nums: &mut [i32], left: usize, right: usize) -> usize {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nlet (mut i, mut j) = (left, 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}\nnums.swap(i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nnums.swap(i, left);  // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\ni                    // \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.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
"},{"location":"chapter_sorting/quick_sort/#1151","title":"11.5.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"

\u5feb\u901f\u6392\u5e8f\u7684\u6574\u4f53\u6d41\u7a0b\u5982\u56fe 11-9 \u6240\u793a\u3002

  1. \u9996\u5148\uff0c\u5bf9\u539f\u6570\u7ec4\u6267\u884c\u4e00\u6b21\u201c\u54e8\u5175\u5212\u5206\u201d\uff0c\u5f97\u5230\u672a\u6392\u5e8f\u7684\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u3002
  2. \u7136\u540e\uff0c\u5bf9\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u5206\u522b\u9012\u5f52\u6267\u884c\u201c\u54e8\u5175\u5212\u5206\u201d\u3002
  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\u3002

\u56fe 11-9 \u00a0 \u5feb\u901f\u6392\u5e8f\u6d41\u7a0b

PythonC++JavaC#GoSwiftJSTSDartRustCZig quick_sort.py
def quick_sort(self, nums: list[int], left: int, right: int):\n\"\"\"\u5feb\u901f\u6392\u5e8f\"\"\"\n# \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\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.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.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.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.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.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.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.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
quick_sort.rs
/* \u5feb\u901f\u6392\u5e8f */\npub fn quick_sort(left: i32, right: i32, nums: &mut [i32]) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nif left >= right {\nreturn;\n}\n// \u54e8\u5175\u5212\u5206\nlet pivot = Self::partition(nums, left as usize, right as usize) as i32;\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nSelf::quick_sort(left, pivot - 1, nums);\nSelf::quick_sort(pivot + 1, right, nums);\n}\n
quick_sort.c
/* \u5feb\u901f\u6392\u5e8f\u7c7b */\n// \u5feb\u901f\u6392\u5e8f\u7c7b-\u54e8\u5175\u5212\u5206\nint partition(int nums[], int left, int right) {\n// \u4ee5 nums[left] \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.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
"},{"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\u201c\u5f52\u5e76\u6392\u5e8f\u201d\u548c\u201c\u5806\u6392\u5e8f\u201d\u76f8\u540c\uff0c\u4f46\u901a\u5e38\u5feb\u901f\u6392\u5e8f\u7684\u6548\u7387\u66f4\u9ad8\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002

  • \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\u201c\u5806\u6392\u5e8f\u201d\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\u201c\u63d2\u5165\u6392\u5e8f\u201d\u6bd4\u201c\u5192\u6ce1\u6392\u5e8f\u201d\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\u201c\u5192\u6ce1\u6392\u5e8f\u201d\u3002

\u4e3a\u4e86\u5c3d\u91cf\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f18\u5316\u54e8\u5175\u5212\u5206\u4e2d\u7684\u57fa\u51c6\u6570\u7684\u9009\u53d6\u7b56\u7565\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u968f\u673a\u9009\u53d6\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6\u6570\u3002\u7136\u800c\uff0c\u5982\u679c\u8fd0\u6c14\u4e0d\u4f73\uff0c\u6bcf\u6b21\u90fd\u9009\u5230\u4e0d\u7406\u60f3\u7684\u57fa\u51c6\u6570\uff0c\u6548\u7387\u4ecd\u7136\u4e0d\u5c3d\u5982\u4eba\u610f\u3002

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u751f\u6210\u7684\u662f\u201c\u4f2a\u968f\u673a\u6570\u201d\u3002\u5982\u679c\u6211\u4eec\u9488\u5bf9\u4f2a\u968f\u673a\u6570\u5e8f\u5217\u6784\u5efa\u4e00\u4e2a\u7279\u5b9a\u7684\u6d4b\u8bd5\u6837\u4f8b\uff0c\u90a3\u4e48\u5feb\u901f\u6392\u5e8f\u7684\u6548\u7387\u4ecd\u7136\u53ef\u80fd\u52a3\u5316\u3002

\u4e3a\u4e86\u8fdb\u4e00\u6b65\u6539\u8fdb\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6570\u7ec4\u4e2d\u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\uff08\u901a\u5e38\u4e3a\u6570\u7ec4\u7684\u9996\u3001\u5c3e\u3001\u4e2d\u70b9\u5143\u7d20\uff09\uff0c\u5e76\u5c06\u8fd9\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\u4f5c\u4e3a\u57fa\u51c6\u6570\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u57fa\u51c6\u6570\u201c\u65e2\u4e0d\u592a\u5c0f\u4e5f\u4e0d\u592a\u5927\u201d\u7684\u6982\u7387\u5c06\u5927\u5e45\u63d0\u5347\u3002\u5f53\u7136\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u9009\u53d6\u66f4\u591a\u5019\u9009\u5143\u7d20\uff0c\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u7b97\u6cd5\u7684\u7a33\u5065\u6027\u3002\u91c7\u7528\u8fd9\u79cd\u65b9\u6cd5\u540e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u52a3\u5316\u81f3 \\(O(n^2)\\) \u7684\u6982\u7387\u5927\u5927\u964d\u4f4e\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
quick_sort.rs
/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nfn median_three(nums: &mut [i32], left: usize, mid: usize, right: usize) -> usize {\n// \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n// \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\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} right\n}\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nfn partition(nums: &mut [i32], left: usize, right: usize) -> usize {\n// \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nlet med = Self::median_three(nums, left, (left + right) / 2, right);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nnums.swap(left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nlet (mut i, mut j) = (left, 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}\nnums.swap(i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nnums.swap(i, left);  // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\ni                    // \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.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
"},{"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 \\(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

PythonC++JavaC#GoSwiftJSTSDartRustCZig quick_sort.py
def quick_sort(self, nums: list[int], left: int, right: int):\n\"\"\"\u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09\"\"\"\n# \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\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.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.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.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.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.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.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.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
quick_sort.rs
/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\npub fn quick_sort(mut left: i32, mut right: i32, nums: &mut [i32]) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\nwhile left < right {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nlet pivot = Self::partition(nums, left as usize, right as usize) as i32;\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif  pivot - left < right - pivot {\nSelf::quick_sort(left, pivot - 1, nums);  // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1;  // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nSelf::quick_sort(pivot + 1, right, nums); // \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.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
"},{"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\u6d41\u7a0b\u5982\u56fe 11-18 \u6240\u793a\u3002

  1. \u521d\u59cb\u5316\u4f4d\u6570 \\(k = 1\\) \u3002
  2. \u5bf9\u5b66\u53f7\u7684\u7b2c \\(k\\) \u4f4d\u6267\u884c\u201c\u8ba1\u6570\u6392\u5e8f\u201d\u3002\u5b8c\u6210\u540e\uff0c\u6570\u636e\u4f1a\u6839\u636e\u7b2c \\(k\\) \u4f4d\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\u3002
  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\u3002

\u56fe 11-18 \u00a0 \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 \\: 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

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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):\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]):\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.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.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.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.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.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.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.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;\nfor (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
radix_sort.rs
/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfn digit(num: i32, exp: i32) -> usize {\n// \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\nreturn ((num / exp) % 10) as usize;\n}\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfn counting_sort_digit(nums: &mut [i32], exp: i32) {\n// \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\nlet mut counter = [0; 10];\nlet n = nums.len();\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor i in 0..n {\nlet d = 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}\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\nlet mut res = vec![0; n];\nfor i in (0..n).rev() {\nlet d = digit(nums[i], 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 0..n {\nnums[i] = res[i];\n}\n}\n/* \u57fa\u6570\u6392\u5e8f */\nfn radix_sort(nums: &mut [i32]) {\n// \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\nlet m = *nums.into_iter().max().unwrap();\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nlet mut exp = 1;\nwhile exp <= m {\ncounting_sort_digit(nums, exp);\nexp *= 10;\n}\n}\n
radix_sort.c
/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int num, int exp) {\n// \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\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.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);\n@memset(counter, 0);\nvar n = nums.len;\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (nums) |num| {\nvar d: u32 = @bitCast(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: u32 = @bitCast(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} 

\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\u56fe 11-2 \u6240\u793a\u3002

  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\u987b\u6392\u5e8f\uff0c\u56e0\u6b64\u6570\u7ec4\u6392\u5e8f\u5b8c\u6210\u3002
<1><2><3><4><5><6><7><8><9><10><11>

\u56fe 11-2 \u00a0 \u9009\u62e9\u6392\u5e8f\u6b65\u9aa4

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

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
selection_sort.rs
/* \u9009\u62e9\u6392\u5e8f */\nfn selection_sort(nums: &mut [i32]) {\nlet n = nums.len();\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor i in 0..n-1 {\n// \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\nlet mut k = i;\nfor j in i+1..n {\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.swap(i, k);\n}\n}\n
selection_sort.c
/* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(int nums[], int n) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\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.zig
[class]{}-[func]{selectionSort}\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\\)\u3001\\(n - 1\\)\u3001\\(\\dots\\)\u3001\\(3\\)\u3001\\(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\\) \u548c \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002
  • \u975e\u7a33\u5b9a\u6392\u5e8f\uff1a\u5982\u56fe 11-3 \u6240\u793a\uff0c\u5143\u7d20 nums[i] \u6709\u53ef\u80fd\u88ab\u4ea4\u6362\u81f3\u4e0e\u5176\u76f8\u7b49\u7684\u5143\u7d20\u7684\u53f3\u8fb9\uff0c\u5bfc\u81f4\u4e24\u8005\u76f8\u5bf9\u987a\u5e8f\u53d1\u751f\u6539\u53d8\u3002

\u56fe 11-3 \u00a0 \u9009\u62e9\u6392\u5e8f\u975e\u7a33\u5b9a\u793a\u4f8b

"},{"location":"chapter_sorting/sorting_algorithm/","title":"11.1 \u00a0 \u6392\u5e8f\u7b97\u6cd5","text":"

\u300c\u6392\u5e8f\u7b97\u6cd5 sorting algorithm\u300d\u7528\u4e8e\u5bf9\u4e00\u7ec4\u6570\u636e\u6309\u7167\u7279\u5b9a\u987a\u5e8f\u8fdb\u884c\u6392\u5217\u3002\u6392\u5e8f\u7b97\u6cd5\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u56e0\u4e3a\u6709\u5e8f\u6570\u636e\u901a\u5e38\u80fd\u591f\u88ab\u66f4\u6709\u6548\u5730\u67e5\u627e\u3001\u5206\u6790\u548c\u5904\u7406\u3002

\u5982\u56fe 11-1 \u6240\u793a\uff0c\u6392\u5e8f\u7b97\u6cd5\u4e2d\u7684\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u662f\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\u7b49\u3002\u6392\u5e8f\u7684\u5224\u65ad\u89c4\u5219\u53ef\u6839\u636e\u9700\u6c42\u8bbe\u5b9a\uff0c\u5982\u6570\u5b57\u5927\u5c0f\u3001\u5b57\u7b26 ASCII \u7801\u987a\u5e8f\u6216\u81ea\u5b9a\u4e49\u89c4\u5219\u3002

\u56fe 11-1 \u00a0 \u6570\u636e\u7c7b\u578b\u548c\u5224\u65ad\u89c4\u5219\u793a\u4f8b

"},{"location":"chapter_sorting/sorting_algorithm/#1111","title":"11.1.1 \u00a0 \u8bc4\u4ef7\u7ef4\u5ea6","text":"

\u8fd0\u884c\u6548\u7387\uff1a\u6211\u4eec\u671f\u671b\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5c3d\u91cf\u4f4e\uff0c\u4e14\u603b\u4f53\u64cd\u4f5c\u6570\u91cf\u8f83\u5c11\uff08\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\u987b\u501f\u52a9\u989d\u5916\u7684\u8f85\u52a9\u6570\u7ec4\uff0c\u4ece\u800c\u8282\u7701\u5185\u5b58\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u539f\u5730\u6392\u5e8f\u7684\u6570\u636e\u642c\u8fd0\u64cd\u4f5c\u8f83\u5c11\uff0c\u8fd0\u884c\u901f\u5ea6\u4e5f\u66f4\u5feb\u3002

\u7a33\u5b9a\u6027\uff1a\u300c\u7a33\u5b9a\u6392\u5e8f\u300d\u5728\u5b8c\u6210\u6392\u5e8f\u540e\uff0c\u76f8\u7b49\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u76f8\u5bf9\u987a\u5e8f\u4e0d\u53d1\u751f\u6539\u53d8\u3002

\u7a33\u5b9a\u6392\u5e8f\u662f\u591a\u7ea7\u6392\u5e8f\u573a\u666f\u7684\u5fc5\u8981\u6761\u4ef6\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5b58\u50a8\u5b66\u751f\u4fe1\u606f\u7684\u8868\u683c\uff0c\u7b2c 1 \u5217\u548c\u7b2c 2 \u5217\u5206\u522b\u662f\u59d3\u540d\u548c\u5e74\u9f84\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u300c\u975e\u7a33\u5b9a\u6392\u5e8f\u300d\u53ef\u80fd\u5bfc\u81f4\u8f93\u5165\u6570\u636e\u7684\u6709\u5e8f\u6027\u4e27\u5931\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\\(<\\)\u3001\\(=\\)\u3001\\(>\\)\uff09\u6765\u5224\u65ad\u5143\u7d20\u7684\u76f8\u5bf9\u987a\u5e8f\uff0c\u4ece\u800c\u6392\u5e8f\u6574\u4e2a\u6570\u7ec4\uff0c\u7406\u8bba\u6700\u4f18\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002\u800c\u300c\u975e\u6bd4\u8f83\u6392\u5e8f\u300d\u4e0d\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe \\(O(n)\\) \uff0c\u4f46\u5176\u901a\u7528\u6027\u76f8\u5bf9\u8f83\u5dee\u3002

"},{"location":"chapter_sorting/sorting_algorithm/#1112","title":"11.1.2 \u00a0 \u7406\u60f3\u6392\u5e8f\u7b97\u6cd5","text":"

\u8fd0\u884c\u5feb\u3001\u539f\u5730\u3001\u7a33\u5b9a\u3001\u6b63\u5411\u81ea\u9002\u5e94\u3001\u901a\u7528\u6027\u597d\u3002\u663e\u7136\uff0c\u8fc4\u4eca\u4e3a\u6b62\u5c1a\u672a\u53d1\u73b0\u517c\u5177\u4ee5\u4e0a\u6240\u6709\u7279\u6027\u7684\u6392\u5e8f\u7b97\u6cd5\u3002\u56e0\u6b64\uff0c\u5728\u9009\u62e9\u6392\u5e8f\u7b97\u6cd5\u65f6\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u6570\u636e\u7279\u70b9\u548c\u95ee\u9898\u9700\u6c42\u6765\u51b3\u5b9a\u3002

\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u5171\u540c\u5b66\u4e60\u5404\u79cd\u6392\u5e8f\u7b97\u6cd5\uff0c\u5e76\u57fa\u4e8e\u4e0a\u8ff0\u8bc4\u4ef7\u7ef4\u5ea6\u5bf9\u5404\u4e2a\u6392\u5e8f\u7b97\u6cd5\u7684\u4f18\u7f3a\u70b9\u8fdb\u884c\u5206\u6790\u3002

"},{"location":"chapter_sorting/summary/","title":"11.11 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_sorting/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
  • \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
  • \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
  • \u56fe 11-19 \u5bf9\u6bd4\u4e86\u4e3b\u6d41\u6392\u5e8f\u7b97\u6cd5\u7684\u6548\u7387\u3001\u7a33\u5b9a\u6027\u3001\u5c31\u5730\u6027\u548c\u81ea\u9002\u5e94\u6027\u7b49\u3002

\u56fe 11-19 \u00a0 \u6392\u5e8f\u7b97\u6cd5\u5bf9\u6bd4

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

\u6392\u5e8f\u7b97\u6cd5\u7a33\u5b9a\u6027\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u662f\u5fc5\u987b\u7684\uff1f

\u5728\u73b0\u5b9e\u4e2d\uff0c\u6211\u4eec\u6709\u53ef\u80fd\u662f\u5728\u5bf9\u8c61\u7684\u67d0\u4e2a\u5c5e\u6027\u4e0a\u8fdb\u884c\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u5b66\u751f\u6709\u59d3\u540d\u548c\u8eab\u9ad8\u4e24\u4e2a\u5c5e\u6027\uff0c\u6211\u4eec\u5e0c\u671b\u5b9e\u73b0\u4e00\u4e2a\u591a\u7ea7\u6392\u5e8f/

\u5148\u6309\u7167\u59d3\u540d\u8fdb\u884c\u6392\u5e8f\uff0c\u5f97\u5230 (A, 180) (B, 185) (C, 170) (D, 170) \uff1b\u63a5\u4e0b\u6765\u5bf9\u8eab\u9ad8\u8fdb\u884c\u6392\u5e8f\u3002\u7531\u4e8e\u6392\u5e8f\u7b97\u6cd5\u4e0d\u7a33\u5b9a\uff0c\u6211\u4eec\u53ef\u80fd\u5f97\u5230 (D, 170) (C, 170) (A, 180) (B, 185) \u3002

\u53ef\u4ee5\u53d1\u73b0\uff0c\u5b66\u751f D \u548c C \u7684\u4f4d\u7f6e\u53d1\u751f\u4e86\u4ea4\u6362\uff0c\u59d3\u540d\u7684\u6709\u5e8f\u6027\u88ab\u7834\u574f\u4e86\uff0c\u800c\u8fd9\u662f\u6211\u4eec\u4e0d\u5e0c\u671b\u770b\u5230\u7684\u3002

\u54e8\u5175\u5212\u5206\u4e2d\u201c\u4ece\u53f3\u5f80\u5de6\u67e5\u627e\u201d\u4e0e\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\u7684\u987a\u5e8f\u53ef\u4ee5\u4ea4\u6362\u5417\uff1f

\u4e0d\u884c\uff0c\u5f53\u6211\u4eec\u4ee5\u6700\u5de6\u7aef\u5143\u7d20\u4e3a\u57fa\u51c6\u6570\u65f6\uff0c\u5fc5\u987b\u5148\u201c\u4ece\u53f3\u5f80\u5de6\u67e5\u627e\u201d\u518d\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\u3002\u8fd9\u4e2a\u7ed3\u8bba\u6709\u4e9b\u53cd\u76f4\u89c9\uff0c\u6211\u4eec\u6765\u5256\u6790\u4e00\u4e0b\u539f\u56e0\u3002

\u54e8\u5175\u5212\u5206 partition() \u7684\u6700\u540e\u4e00\u6b65\u662f\u4ea4\u6362 nums[left] \u548c nums[i] \u3002\u5b8c\u6210\u4ea4\u6362\u540e\uff0c\u57fa\u51c6\u6570\u5de6\u8fb9\u7684\u5143\u7d20\u90fd <= \u57fa\u51c6\u6570\uff0c\u8fd9\u5c31\u8981\u6c42\u6700\u540e\u4e00\u6b65\u4ea4\u6362\u524d nums[left] >= nums[i] \u5fc5\u987b\u6210\u7acb\u3002\u5047\u8bbe\u6211\u4eec\u5148\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\uff0c\u90a3\u4e48\u5982\u679c\u627e\u4e0d\u5230\u6bd4\u57fa\u51c6\u6570\u66f4\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

\u5173\u4e8e\u5c3e\u9012\u5f52\u4f18\u5316\uff0c\u4e3a\u4ec0\u4e48\u9009\u77ed\u7684\u6570\u7ec4\u80fd\u4fdd\u8bc1\u9012\u5f52\u6df1\u5ea6\u4e0d\u8d85\u8fc7 \\(\\log n\\) \uff1f

\u9012\u5f52\u6df1\u5ea6\u5c31\u662f\u5f53\u524d\u672a\u8fd4\u56de\u7684\u9012\u5f52\u65b9\u6cd5\u7684\u6570\u91cf\u3002\u6bcf\u8f6e\u54e8\u5175\u5212\u5206\u6211\u4eec\u5c06\u539f\u6570\u7ec4\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\u3002\u5728\u5c3e\u9012\u5f52\u4f18\u5316\u540e\uff0c\u5411\u4e0b\u9012\u5f52\u7684\u5b50\u6570\u7ec4\u957f\u5ea6\u6700\u5927\u4e3a\u539f\u6570\u7ec4\u7684\u4e00\u534a\u957f\u5ea6\u3002\u5047\u8bbe\u6700\u5dee\u60c5\u51b5\uff0c\u4e00\u76f4\u4e3a\u4e00\u534a\u957f\u5ea6\uff0c\u90a3\u4e48\u6700\u7ec8\u7684\u9012\u5f52\u6df1\u5ea6\u5c31\u662f \\(\\log n\\) \u3002

\u56de\u987e\u539f\u59cb\u7684\u5feb\u901f\u6392\u5e8f\uff0c\u6211\u4eec\u6709\u53ef\u80fd\u4f1a\u8fde\u7eed\u5730\u9012\u5f52\u957f\u5ea6\u8f83\u5927\u7684\u6570\u7ec4\uff0c\u6700\u5dee\u60c5\u51b5\u4e0b\u4e3a \\(n\\)\u3001\\(n - 1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \uff0c\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \u3002\u5c3e\u9012\u5f52\u4f18\u5316\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u7684\u51fa\u73b0\u3002

\u5f53\u6570\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u90fd\u76f8\u7b49\u65f6\uff0c\u5feb\u901f\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(n^2)\\) \u5417\uff1f\u8be5\u5982\u4f55\u5904\u7406\u8fd9\u79cd\u9000\u5316\u60c5\u51b5\uff1f

\u662f\u7684\u3002\u8fd9\u79cd\u60c5\u51b5\u53ef\u4ee5\u8003\u8651\u901a\u8fc7\u54e8\u5175\u5212\u5206\u5c06\u6570\u7ec4\u5212\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\uff1a\u5c0f\u4e8e\u3001\u7b49\u4e8e\u3001\u5927\u4e8e\u57fa\u51c6\u6570\u3002\u4ec5\u5411\u4e0b\u9012\u5f52\u5c0f\u4e8e\u548c\u5927\u4e8e\u7684\u4e24\u90e8\u5206\u3002\u5728\u8be5\u65b9\u6cd5\u4e0b\uff0c\u8f93\u5165\u5143\u7d20\u5168\u90e8\u76f8\u7b49\u7684\u6570\u7ec4\uff0c\u4ec5\u4e00\u8f6e\u54e8\u5175\u5212\u5206\u5373\u53ef\u5b8c\u6210\u6392\u5e8f\u3002

\u6876\u6392\u5e8f\u7684\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\u4ec0\u4e48\u662f \\(O(n^2)\\) \uff1f

\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u5143\u7d20\u88ab\u5206\u81f3\u540c\u4e00\u4e2a\u6876\u4e2d\u3002\u5982\u679c\u6211\u4eec\u91c7\u7528\u4e00\u4e2a \\(O(n^2)\\) \u7b97\u6cd5\u6765\u6392\u5e8f\u8fd9\u4e9b\u5143\u7d20\uff0c\u5219\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002

"},{"location":"chapter_stack_and_queue/","title":"\u7b2c 5 \u7ae0 \u00a0 \u6808\u4e0e\u961f\u5217","text":"

Abstract

\u6808\u5982\u540c\u53e0\u732b\u732b\uff0c\u800c\u961f\u5217\u5c31\u50cf\u732b\u732b\u6392\u961f\u3002

\u4e24\u8005\u5206\u522b\u4ee3\u8868\u7740\u5148\u5165\u540e\u51fa\u548c\u5148\u5165\u5148\u51fa\u7684\u903b\u8f91\u5173\u7cfb\u3002

"},{"location":"chapter_stack_and_queue/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 5.1 \u00a0 \u6808
  • 5.2 \u00a0 \u961f\u5217
  • 5.3 \u00a0 \u53cc\u5411\u961f\u5217
  • 5.4 \u00a0 \u5c0f\u7ed3
"},{"location":"chapter_stack_and_queue/deque/","title":"5.3 \u00a0 \u53cc\u5411\u961f\u5217","text":"

\u5728\u961f\u5217\u4e2d\uff0c\u6211\u4eec\u4ec5\u80fd\u5728\u5934\u90e8\u5220\u9664\u6216\u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\u3002\u5982\u56fe 5-7 \u6240\u793a\uff0c\u300c\u53cc\u5411\u961f\u5217 double-ended queue\u300d\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\uff0c\u5141\u8bb8\u5728\u5934\u90e8\u548c\u5c3e\u90e8\u6267\u884c\u5143\u7d20\u7684\u6dfb\u52a0\u6216\u5220\u9664\u64cd\u4f5c\u3002

\u56fe 5-7 \u00a0 \u53cc\u5411\u961f\u5217\u7684\u64cd\u4f5c

"},{"location":"chapter_stack_and_queue/deque/#531","title":"5.3.1 \u00a0 \u53cc\u5411\u961f\u5217\u5e38\u7528\u64cd\u4f5c","text":"

\u53cc\u5411\u961f\u5217\u7684\u5e38\u7528\u64cd\u4f5c\u5982\u8868 5-3 \u6240\u793a\uff0c\u5177\u4f53\u7684\u65b9\u6cd5\u540d\u79f0\u9700\u8981\u6839\u636e\u6240\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\u6765\u786e\u5b9a\u3002

\u8868 5-3 \u00a0 \u53cc\u5411\u961f\u5217\u64cd\u4f5c\u6548\u7387

\u65b9\u6cd5\u540d \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 pushFirst() \u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u961f\u9996 \\(O(1)\\) pushLast() \u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u961f\u5c3e \\(O(1)\\) popFirst() \u5220\u9664\u961f\u9996\u5143\u7d20 \\(O(1)\\) popLast() \u5220\u9664\u961f\u5c3e\u5143\u7d20 \\(O(1)\\) peekFirst() \u8bbf\u95ee\u961f\u9996\u5143\u7d20 \\(O(1)\\) peekLast() \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 \\(O(1)\\)

\u540c\u6837\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u4e2d\u5df2\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\u7c7b\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.cs
/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 C# \u4e2d\uff0c\u5c06\u94fe\u8868 LinkedList \u770b\u4f5c\u53cc\u5411\u961f\u5217\u6765\u4f7f\u7528\nLinkedList<int> deque = new();\n/* \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_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.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.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.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
deque.rs
/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\nlet mut deque: VecDeque<u32> = VecDeque::new();\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 */\nif let Some(front) = deque.front() { // \u961f\u9996\u5143\u7d20\n}\nif let Some(rear) = deque.back() {   // \u961f\u5c3e\u5143\u7d20\n}\n/* \u5143\u7d20\u51fa\u961f */\nif let Some(pop_front) = deque.pop_front() { // \u961f\u9996\u5143\u7d20\u51fa\u961f\n}\nif let Some(pop_rear) = deque.pop_back() {   // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = deque.len();\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = deque.is_empty();\n
deque.c
// C \u672a\u63d0\u4f9b\u5185\u7f6e\u53cc\u5411\u961f\u5217\n
deque.zig
\n
"},{"location":"chapter_stack_and_queue/deque/#532","title":"5.3.2 \u00a0 \u53cc\u5411\u961f\u5217\u5b9e\u73b0 *","text":"

\u53cc\u5411\u961f\u5217\u7684\u5b9e\u73b0\u4e0e\u961f\u5217\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u9009\u62e9\u94fe\u8868\u6216\u6570\u7ec4\u4f5c\u4e3a\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002

"},{"location":"chapter_stack_and_queue/deque/#1","title":"1. \u00a0 \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u7684\u5b9e\u73b0","text":"

\u56de\u987e\u4e0a\u4e00\u8282\u5185\u5bb9\uff0c\u6211\u4eec\u4f7f\u7528\u666e\u901a\u5355\u5411\u94fe\u8868\u6765\u5b9e\u73b0\u961f\u5217\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u5220\u9664\u5934\u8282\u70b9\uff08\u5bf9\u5e94\u51fa\u961f\u64cd\u4f5c\uff09\u548c\u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0\u65b0\u8282\u70b9\uff08\u5bf9\u5e94\u5165\u961f\u64cd\u4f5c\uff09\u3002

\u5bf9\u4e8e\u53cc\u5411\u961f\u5217\u800c\u8a00\uff0c\u5934\u90e8\u548c\u5c3e\u90e8\u90fd\u53ef\u4ee5\u6267\u884c\u5165\u961f\u548c\u51fa\u961f\u64cd\u4f5c\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u53cc\u5411\u961f\u5217\u9700\u8981\u5b9e\u73b0\u53e6\u4e00\u4e2a\u5bf9\u79f0\u65b9\u5411\u7684\u64cd\u4f5c\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u91c7\u7528\u201c\u53cc\u5411\u94fe\u8868\u201d\u4f5c\u4e3a\u53cc\u5411\u961f\u5217\u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002

\u5982\u56fe 5-8 \u6240\u793a\uff0c\u6211\u4eec\u5c06\u53cc\u5411\u94fe\u8868\u7684\u5934\u8282\u70b9\u548c\u5c3e\u8282\u70b9\u89c6\u4e3a\u53cc\u5411\u961f\u5217\u7684\u961f\u9996\u548c\u961f\u5c3e\uff0c\u540c\u65f6\u5b9e\u73b0\u5728\u4e24\u7aef\u6dfb\u52a0\u548c\u5220\u9664\u8282\u70b9\u7684\u529f\u80fd\u3002

LinkedListDequepushLast()pushFirst()popLast()popFirst()

\u56fe 5-8 \u00a0 \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u53cc\u5411\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c

\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig linkedlist_deque.py
class ListNode:\n\"\"\"\u53cc\u5411\u94fe\u8868\u8282\u70b9\"\"\"\ndef __init__(self, val: int):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.val: int = val\nself.next: ListNode | None = None  # \u540e\u7ee7\u8282\u70b9\u5f15\u7528\nself.prev: ListNode | None = None  # \u524d\u9a71\u8282\u70b9\u5f15\u7528\nclass LinkedListDeque:\n\"\"\"\u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\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  # \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):\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):\n\"\"\"\u961f\u9996\u5165\u961f\"\"\"\nself.push(num, True)\ndef push_last(self, num: int):\n\"\"\"\u961f\u5c3e\u5165\u961f\"\"\"\nself.push(num, False)\ndef pop(self, is_front: bool) -> int:\n\"\"\"\u51fa\u961f\u64cd\u4f5c\"\"\"\nif self.is_empty():\nraise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\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\"\"\"\nif self.is_empty():\nraise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\nreturn self._front.val\ndef peek_last(self) -> int:\n\"\"\"\u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\"\"\"\nif self.is_empty():\nraise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\nreturn 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.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) {\nif (isEmpty())\nthrow out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\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() {\nif (isEmpty())\nthrow out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\nreturn front->val;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast() {\nif (isEmpty())\nthrow out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\nreturn 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.java
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\nint val; // \u8282\u70b9\u503c\nListNode next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\nListNode prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\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 int pop(boolean isFront) {\nif (isEmpty())\nthrow new IndexOutOfBoundsException();\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 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() {\nif (isEmpty())\nthrow new IndexOutOfBoundsException();\nreturn front.val;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npublic int peekLast() {\nif (isEmpty())\nthrow new IndexOutOfBoundsException();\nreturn 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.cs
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\npublic int val;       // \u8282\u70b9\u503c\npublic ListNode? next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\npublic ListNode? prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\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(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) {\nif (IsEmpty())\nthrow new Exception();\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() {\nif (IsEmpty())\nthrow new Exception();\nreturn front?.val;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npublic int? PeekLast() {\nif (IsEmpty())\nthrow new Exception();\nreturn 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.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.swift
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\nvar val: Int // \u8282\u70b9\u503c\nvar next: ListNode? // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\nweak var prev: ListNode? // \u524d\u9a71\u8282\u70b9\u5f15\u7528\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.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.dart
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\nint val; // \u8282\u70b9\u503c\nListNode? next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\nListNode? prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\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 \u548c _rear \u90fd\u6307\u5411 node\n_front = _rear = node;\n} else if (isFront) {\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n_front!.prev = node;\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
linkedlist_deque.rs
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\npub struct ListNode<T> {\npub val: T,                                 // \u8282\u70b9\u503c\npub next: Option<Rc<RefCell<ListNode<T>>>>, // \u540e\u7ee7\u8282\u70b9\u6307\u9488\npub prev: Option<Rc<RefCell<ListNode<T>>>>, // \u524d\u9a71\u8282\u70b9\u6307\u9488\n}\nimpl<T> ListNode<T> {\npub fn new(val: T) -> Rc<RefCell<ListNode<T>>> {\nRc::new(RefCell::new(ListNode {\nval,\nnext: None,\nprev: None,\n}))\n}\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\n#[allow(dead_code)]\npub struct LinkedListDeque<T> {\nfront: Option<Rc<RefCell<ListNode<T>>>>,    // \u5934\u8282\u70b9 front\nrear: Option<Rc<RefCell<ListNode<T>>>>,     // \u5c3e\u8282\u70b9 rear \nque_size: usize,                            // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n}\nimpl<T: Copy> LinkedListDeque<T> {\npub fn new() -> Self {\nSelf {\nfront: None,\nrear: None,\nque_size: 0, }\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\npub fn size(&self) -> usize {\nreturn self.que_size;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npub fn is_empty(&self) -> bool {\nreturn self.size() == 0;\n}\n/* \u5165\u961f\u64cd\u4f5c */\npub fn push(&mut self, num: T, is_front: bool) {\nlet node = ListNode::new(num);\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\nif is_front {\nmatch self.front.take() {\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nNone => {\nself.rear = Some(node.clone());\nself.front = Some(node);\n}\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nSome(old_front) => {\nold_front.borrow_mut().prev = Some(node.clone());\nnode.borrow_mut().next = Some(old_front);\nself.front = Some(node); // \u66f4\u65b0\u5934\u8282\u70b9\n}\n}\n} // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\nelse {\nmatch self.rear.take() {\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nNone => {\nself.front = Some(node.clone());\nself.rear = Some(node);\n}\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\nSome(old_rear) => {\nold_rear.borrow_mut().next = Some(node.clone());\nnode.borrow_mut().prev = Some(old_rear);\nself.rear = Some(node); // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\n}\n}\nself.que_size += 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n/* \u961f\u9996\u5165\u961f */\npub fn push_first(&mut self, num: T) {\nself.push(num, true);\n}\n/* \u961f\u5c3e\u5165\u961f */\npub fn push_last(&mut self, num: T) {\nself.push(num, false);\n}\n/* \u51fa\u961f\u64cd\u4f5c */\npub fn pop(&mut self, is_front: bool) -> Option<T> {\n// \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de None\nif self.is_empty() { return None };\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif is_front {\nself.front.take().map(|old_front| {\nmatch old_front.borrow_mut().next.take() {\nSome(new_front) => {\nnew_front.borrow_mut().prev.take();\nself.front = Some(new_front);   // \u66f4\u65b0\u5934\u8282\u70b9\n}\nNone => {\nself.rear.take();\n}\n}\nself.que_size -= 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nRc::try_unwrap(old_front).ok().unwrap().into_inner().val\n})\n} // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\nelse {\nself.rear.take().map(|old_rear| {\nmatch old_rear.borrow_mut().prev.take() {\nSome(new_rear) => {\nnew_rear.borrow_mut().next.take();\nself.rear = Some(new_rear);     // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nNone => {\nself.front.take();\n}\n}\nself.que_size -= 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nRc::try_unwrap(old_rear).ok().unwrap().into_inner().val\n})\n}\n}\n/* \u961f\u9996\u51fa\u961f */\npub fn pop_first(&mut self) -> Option<T> {\nreturn self.pop(true);\n}\n/* \u961f\u5c3e\u51fa\u961f */\npub fn pop_last(&mut self) -> Option<T> {\nreturn self.pop(false);\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npub fn peek_first(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\nself.front.as_ref()\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npub fn peek_last(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\nself.rear.as_ref()\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\npub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\nif let Some(node) = head {\nlet mut nums = self.to_array(node.borrow().next.as_ref());\nnums.insert(0, node.borrow().val);\nreturn nums;\n}\nreturn Vec::new();\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// \u961f\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.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\u6307\u9488\nprev: ?*Self = null,    // \u524d\u9a71\u8282\u70b9\u6307\u9488\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\u51fd\u6570\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\u51fd\u6570\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());\n@memset(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
"},{"location":"chapter_stack_and_queue/deque/#2","title":"2. \u00a0 \u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"

\u5982\u56fe 5-9 \u6240\u793a\uff0c\u4e0e\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u961f\u5217\u7c7b\u4f3c\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u73af\u5f62\u6570\u7ec4\u6765\u5b9e\u73b0\u53cc\u5411\u961f\u5217\u3002

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

\u56fe 5-9 \u00a0 \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u53cc\u5411\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c

\u5728\u961f\u5217\u7684\u5b9e\u73b0\u57fa\u7840\u4e0a\uff0c\u4ec5\u9700\u589e\u52a0\u201c\u961f\u9996\u5165\u961f\u201d\u548c\u201c\u961f\u5c3e\u51fa\u961f\u201d\u7684\u65b9\u6cd5\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig array_deque.py
class ArrayDeque:\n\"\"\"\u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\"\"\"\ndef __init__(self, capacity: int):\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):\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):\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.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.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.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.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.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.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.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
array_deque.rs
/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nstruct ArrayDeque {\nnums: Vec<i32>,     // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nfront: usize,       // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nque_size: usize,    // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n}\nimpl ArrayDeque {\n/* \u6784\u9020\u65b9\u6cd5 */\npub fn new(capacity: usize) -> Self {\nSelf {\nnums: vec![0; capacity],\nfront: 0,\nque_size: 0,\n}\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\npub fn capacity(&self) -> usize {\nself.nums.len()\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\npub fn size(&self) -> usize {\nself.que_size\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npub fn is_empty(&self) -> bool {\nself.que_size == 0\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nfn index(&self, i: i32) -> usize {\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\nreturn ((i + self.capacity() as i32) % self.capacity() as i32) as usize;\n}\n/* \u961f\u9996\u5165\u961f */\npub fn push_first(&mut self, num: i32) {\nif self.que_size == self.capacity() {\nprintln!(\"\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\nself.front = self.index(self.front as i32 - 1);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nself.nums[self.front] = num;\nself.que_size += 1;\n}\n/* \u961f\u5c3e\u5165\u961f */\npub fn push_last(&mut self, num: i32) {\nif self.que_size == self.capacity() {\nprintln!(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\nreturn\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nlet rear = self.index(self.front as i32 + self.que_size as i32);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nself.nums[rear] = num;\nself.que_size += 1;\n}\n/* \u961f\u9996\u51fa\u961f */\nfn pop_first(&mut self) -> i32 {\nlet num = self.peek_first();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nself.front = self.index(self.front as i32 + 1);\nself.que_size -= 1;\nnum\n}\n/* \u961f\u5c3e\u51fa\u961f */\nfn pop_last(&mut self) -> i32 {\nlet num = self.peek_last();\nself.que_size -= 1;\nnum\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfn peek_first(&self) -> i32 {\nif self.is_empty() { panic!(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\") };\nself.nums[self.front]\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfn peek_last(&self) -> i32 {\nif self.is_empty() { panic!(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\") };\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nlet last = self.index(self.front as i32 + self.que_size as i32 - 1);\nself.nums[last]\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nfn to_array(&self) -> Vec<i32> {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nlet mut res = vec![0; self.que_size];\nlet mut j = self.front;\nfor i in 0..self.que_size {\nres[i] = self.nums[self.index(j as i32)];\nj += 1;\n}\nres\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.zig
[class]{ArrayDeque}-[func]{}\n
"},{"location":"chapter_stack_and_queue/deque/#533","title":"5.3.3 \u00a0 \u53cc\u5411\u961f\u5217\u5e94\u7528","text":"

\u53cc\u5411\u961f\u5217\u517c\u5177\u6808\u4e0e\u961f\u5217\u7684\u903b\u8f91\uff0c\u56e0\u6b64\u5b83\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e24\u8005\u7684\u6240\u6709\u5e94\u7528\u573a\u666f\uff0c\u540c\u65f6\u63d0\u4f9b\u66f4\u9ad8\u7684\u81ea\u7531\u5ea6\u3002

\u6211\u4eec\u77e5\u9053\uff0c\u8f6f\u4ef6\u7684\u201c\u64a4\u9500\u201d\u529f\u80fd\u901a\u5e38\u4f7f\u7528\u6808\u6765\u5b9e\u73b0\uff1a\u7cfb\u7edf\u5c06\u6bcf\u6b21\u66f4\u6539\u64cd\u4f5c push \u5230\u6808\u4e2d\uff0c\u7136\u540e\u901a\u8fc7 pop \u5b9e\u73b0\u64a4\u9500\u3002\u7136\u800c\uff0c\u8003\u8651\u5230\u7cfb\u7edf\u8d44\u6e90\u7684\u9650\u5236\uff0c\u8f6f\u4ef6\u901a\u5e38\u4f1a\u9650\u5236\u64a4\u9500\u7684\u6b65\u6570\uff08\u4f8b\u5982\u4ec5\u5141\u8bb8\u4fdd\u5b58 \\(50\\) \u6b65\uff09\u3002\u5f53\u6808\u7684\u957f\u5ea6\u8d85\u8fc7 \\(50\\) \u65f6\uff0c\u8f6f\u4ef6\u9700\u8981\u5728\u6808\u5e95\uff08\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\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

\u5982\u56fe 5-4 \u6240\u793a\uff0c\u6211\u4eec\u5c06\u961f\u5217\u7684\u5934\u90e8\u79f0\u4e3a\u201c\u961f\u9996\u201d\uff0c\u5c3e\u90e8\u79f0\u4e3a\u201c\u961f\u5c3e\u201d\uff0c\u5c06\u628a\u5143\u7d20\u52a0\u5165\u961f\u5c3e\u7684\u64cd\u4f5c\u79f0\u4e3a\u201c\u5165\u961f\u201d\uff0c\u5220\u9664\u961f\u9996\u5143\u7d20\u7684\u64cd\u4f5c\u79f0\u4e3a\u201c\u51fa\u961f\u201d\u3002

\u56fe 5-4 \u00a0 \u961f\u5217\u7684\u5148\u5165\u5148\u51fa\u89c4\u5219

"},{"location":"chapter_stack_and_queue/queue/#521","title":"5.2.1 \u00a0 \u961f\u5217\u5e38\u7528\u64cd\u4f5c","text":"

\u961f\u5217\u7684\u5e38\u89c1\u64cd\u4f5c\u5982\u8868 5-2 \u6240\u793a\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u7684\u65b9\u6cd5\u540d\u79f0\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002\u6211\u4eec\u5728\u6b64\u91c7\u7528\u4e0e\u6808\u76f8\u540c\u7684\u65b9\u6cd5\u547d\u540d\u3002

\u8868 5-2 \u00a0 \u961f\u5217\u64cd\u4f5c\u6548\u7387

\u65b9\u6cd5\u540d \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 push() \u5143\u7d20\u5165\u961f\uff0c\u5373\u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u961f\u5c3e \\(O(1)\\) pop() \u961f\u9996\u5143\u7d20\u51fa\u961f \\(O(1)\\) peek() \u8bbf\u95ee\u961f\u9996\u5143\u7d20 \\(O(1)\\)

\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u4e2d\u73b0\u6210\u7684\u961f\u5217\u7c7b\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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_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.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.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.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
queue.rs
/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 Rust \u4e2d\u4f7f\u7528\u53cc\u5411\u961f\u5217\u4f5c\u4e3a\u666e\u901a\u961f\u5217\u6765\u4f7f\u7528\nlet mut deque: VecDeque<u32> = VecDeque::new();\n/* \u5143\u7d20\u5165\u961f */\ndeque.push_back(1);\ndeque.push_back(3);\ndeque.push_back(2);\ndeque.push_back(5);\ndeque.push_back(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nif let Some(front) = deque.front() {\n}\n/* \u5143\u7d20\u51fa\u961f */\nif let Some(pop) = deque.pop_front() {\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = deque.len();\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = deque.is_empty();\n
queue.c
// C \u672a\u63d0\u4f9b\u5185\u7f6e\u961f\u5217\n
queue.zig
\n
"},{"location":"chapter_stack_and_queue/queue/#522","title":"5.2.2 \u00a0 \u961f\u5217\u5b9e\u73b0","text":"

\u4e3a\u4e86\u5b9e\u73b0\u961f\u5217\uff0c\u6211\u4eec\u9700\u8981\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u5728\u4e00\u7aef\u6dfb\u52a0\u5143\u7d20\uff0c\u5e76\u5728\u53e6\u4e00\u7aef\u5220\u9664\u5143\u7d20\u3002\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":"1. \u00a0 \u57fa\u4e8e\u94fe\u8868\u7684\u5b9e\u73b0","text":"

\u5982\u56fe 5-5 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u94fe\u8868\u7684\u201c\u5934\u8282\u70b9\u201d\u548c\u201c\u5c3e\u8282\u70b9\u201d\u5206\u522b\u89c6\u4e3a\u201c\u961f\u9996\u201d\u548c\u201c\u961f\u5c3e\u201d\uff0c\u89c4\u5b9a\u961f\u5c3e\u4ec5\u53ef\u6dfb\u52a0\u8282\u70b9\uff0c\u961f\u9996\u4ec5\u53ef\u5220\u9664\u8282\u70b9\u3002

LinkedListQueuepush()pop()

\u56fe 5-5 \u00a0 \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c

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

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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):\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.is_empty():\nraise IndexError(\"\u961f\u5217\u4e3a\u7a7a\")\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.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 isEmpty() {\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.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 (isEmpty())\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.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(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 (IsEmpty())\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.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.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.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.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
linkedlist_queue.rs
/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\n#[allow(dead_code)]\npub struct LinkedListQueue<T> {\nfront: Option<Rc<RefCell<ListNode<T>>>>,    // \u5934\u8282\u70b9 front\nrear: Option<Rc<RefCell<ListNode<T>>>>,     // \u5c3e\u8282\u70b9 rear \nque_size: usize,                            // \u961f\u5217\u7684\u957f\u5ea6\n}\nimpl<T: Copy> LinkedListQueue<T> {\npub fn new() -> Self {\nSelf {\nfront: None,\nrear: None,\nque_size: 0, }\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\npub fn size(&self) -> usize {\nreturn self.que_size;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npub fn is_empty(&self) -> bool {\nreturn self.size() == 0;\n}\n/* \u5165\u961f */\npub fn push(&mut self, num: T) {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nlet new_rear = ListNode::new(num);\nmatch self.rear.take() {\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\nSome(old_rear) => {\nold_rear.borrow_mut().next = Some(new_rear.clone());\nself.rear = Some(new_rear);\n}\n// \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\nNone => {\nself.front = Some(new_rear.clone());\nself.rear = Some(new_rear);\n}\n}\nself.que_size += 1;\n}\n/* \u51fa\u961f */\npub fn pop(&mut self) -> Option<T> {\nself.front.take().map(|old_front| {\nmatch old_front.borrow_mut().next.take() {\nSome(new_front) => {\nself.front = Some(new_front);\n}\nNone => {\nself.rear.take();\n}\n}\nself.que_size -= 1;\nRc::try_unwrap(old_front).ok().unwrap().into_inner().val\n})\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\nself.front.as_ref()\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\nif let Some(node) = head {\nlet mut nums = self.to_array(node.borrow().next.as_ref());\nnums.insert(0, node.borrow().val);\nreturn nums;\n}\nreturn Vec::new();\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; i++) {\narr[i] = node->val;\nnode = node->next;\n}\nprintArray(arr, queue->queSize);\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\u51fd\u6570\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\u51fd\u6570\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());\n@memset(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
"},{"location":"chapter_stack_and_queue/queue/#2","title":"2. \u00a0 \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 size \u7528\u4e8e\u8bb0\u5f55\u961f\u5217\u957f\u5ea6\u3002\u5b9a\u4e49 rear = front + size \uff0c\u8fd9\u4e2a\u516c\u5f0f\u8ba1\u7b97\u51fa\u7684 rear \u6307\u5411\u961f\u5c3e\u5143\u7d20\u4e4b\u540e\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\u3002

\u57fa\u4e8e\u6b64\u8bbe\u8ba1\uff0c\u6570\u7ec4\u4e2d\u5305\u542b\u5143\u7d20\u7684\u6709\u6548\u533a\u95f4\u4e3a [front, rear - 1]\uff0c\u5404\u79cd\u64cd\u4f5c\u7684\u5b9e\u73b0\u65b9\u6cd5\u5982\u56fe 5-6 \u6240\u793a\u3002

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

\u53ef\u4ee5\u770b\u5230\uff0c\u5165\u961f\u548c\u51fa\u961f\u64cd\u4f5c\u90fd\u53ea\u9700\u8fdb\u884c\u4e00\u6b21\u64cd\u4f5c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(1)\\) \u3002

ArrayQueuepush()pop()

\u56fe 5-6 \u00a0 \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c

\u4f60\u53ef\u80fd\u4f1a\u53d1\u73b0\u4e00\u4e2a\u95ee\u9898\uff1a\u5728\u4e0d\u65ad\u8fdb\u884c\u5165\u961f\u548c\u51fa\u961f\u7684\u8fc7\u7a0b\u4e2d\uff0cfront \u548c rear \u90fd\u5728\u5411\u53f3\u79fb\u52a8\uff0c\u5f53\u5b83\u4eec\u5230\u8fbe\u6570\u7ec4\u5c3e\u90e8\u65f6\u5c31\u65e0\u6cd5\u7ee7\u7eed\u79fb\u52a8\u4e86\u3002\u4e3a\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u89c6\u4e3a\u9996\u5c3e\u76f8\u63a5\u7684\u201c\u73af\u5f62\u6570\u7ec4\u201d\u3002

\u5bf9\u4e8e\u73af\u5f62\u6570\u7ec4\uff0c\u6211\u4eec\u9700\u8981\u8ba9 front \u6216 rear \u5728\u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u76f4\u63a5\u56de\u5230\u6570\u7ec4\u5934\u90e8\u7ee7\u7eed\u904d\u5386\u3002\u8fd9\u79cd\u5468\u671f\u6027\u89c4\u5f8b\u53ef\u4ee5\u901a\u8fc7\u201c\u53d6\u4f59\u64cd\u4f5c\u201d\u6765\u5b9e\u73b0\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig array_queue.py
class ArrayQueue:\n\"\"\"\u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217\"\"\"\ndef __init__(self, size: int):\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):\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\u90e8\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.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 isEmpty() {\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 (isEmpty())\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.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.cs
/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\nprivate readonly 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.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.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.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 */\nisEmpty() {\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.isEmpty()) 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 */\nisEmpty(): 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.isEmpty()) 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.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
array_queue.rs
/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nstruct ArrayQueue {\nnums: Vec<i32>,     // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nfront: i32,         // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nque_size: i32,      // \u961f\u5217\u957f\u5ea6\nque_capacity: i32,  // \u961f\u5217\u5bb9\u91cf\n}\nimpl ArrayQueue {\n/* \u6784\u9020\u65b9\u6cd5 */\nfn new(capacity: i32) -> ArrayQueue {\nArrayQueue {\nnums: vec![0; capacity as usize],\nfront: 0,\nque_size: 0,\nque_capacity: capacity,\n}\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nfn capacity(&self) -> i32 {\nself.que_capacity\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfn size(&self) -> i32 {\nself.que_size\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfn is_empty(&self) -> bool {\nself.que_size == 0\n}\n/* \u5165\u961f */\nfn push(&mut self, num: i32) {\nif self.que_size == self.capacity() {\nprintln!(\"\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 = (self.front + self.que_size) % self.que_capacity;\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nself.nums[rear as usize] = num;\nself.que_size += 1;\n}\n/* \u51fa\u961f */\nfn pop(&mut self) -> i32 {\nlet 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.que_capacity;\nself.que_size -= 1;\nnum\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfn peek(&self) -> i32 {\nif self.is_empty() {\npanic!(\"index out of bounds\");\n}\nself.nums[self.front as usize]\n}\n/* \u8fd4\u56de\u6570\u7ec4 */\nfn to_vector(&self) -> Vec<i32> {\nlet cap = self.que_capacity;\nlet mut j = self.front;\nlet mut arr = vec![0; self.que_size as usize];\nfor i in 0..self.que_size {\narr[i as usize] = self.nums[(j % cap) as usize];\nj += 1;\n}\narr\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.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\nqueSize: 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\u51fd\u6570\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);\n@memset(self.nums, @as(T, 0));\n}\n// \u6790\u6784\u51fd\u6570\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.queSize;\n}\n// \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\npub fn isEmpty(self: *Self) bool {\nreturn self.queSize == 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.queSize) % self.capacity();\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nself.nums[rear] = num;\nself.queSize += 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.queSize -= 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());\n@memset(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

\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\u7684\u903b\u8f91\u7684\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u3002

\u6211\u4eec\u53ef\u4ee5\u5c06\u6808\u7c7b\u6bd4\u4e3a\u684c\u9762\u4e0a\u7684\u4e00\u645e\u76d8\u5b50\uff0c\u5982\u679c\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

\u5982\u56fe 5-1 \u6240\u793a\uff0c\u6211\u4eec\u628a\u5806\u53e0\u5143\u7d20\u7684\u9876\u90e8\u79f0\u4e3a\u201c\u6808\u9876\u201d\uff0c\u5e95\u90e8\u79f0\u4e3a\u201c\u6808\u5e95\u201d\u3002\u5c06\u628a\u5143\u7d20\u6dfb\u52a0\u5230\u6808\u9876\u7684\u64cd\u4f5c\u53eb\u505a\u201c\u5165\u6808\u201d\uff0c\u5220\u9664\u6808\u9876\u5143\u7d20\u7684\u64cd\u4f5c\u53eb\u505a\u201c\u51fa\u6808\u201d\u3002

\u56fe 5-1 \u00a0 \u6808\u7684\u5148\u5165\u540e\u51fa\u89c4\u5219

"},{"location":"chapter_stack_and_queue/stack/#511","title":"5.1.1 \u00a0 \u6808\u5e38\u7528\u64cd\u4f5c","text":"

\u6808\u7684\u5e38\u7528\u64cd\u4f5c\u5982\u8868 5-1 \u6240\u793a\uff0c\u5177\u4f53\u7684\u65b9\u6cd5\u540d\u9700\u8981\u6839\u636e\u6240\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\u6765\u786e\u5b9a\u3002\u5728\u6b64\uff0c\u6211\u4eec\u4ee5\u5e38\u89c1\u7684 push()\u3001pop()\u3001peek() \u547d\u540d\u4e3a\u4f8b\u3002

\u8868 5-1 \u00a0 \u6808\u7684\u64cd\u4f5c\u6548\u7387

\u65b9\u6cd5 \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 push() \u5143\u7d20\u5165\u6808\uff08\u6dfb\u52a0\u81f3\u6808\u9876\uff09 \\(O(1)\\) pop() \u6808\u9876\u5143\u7d20\u51fa\u6808 \\(O(1)\\) peek() \u8bbf\u95ee\u6808\u9876\u5143\u7d20 \\(O(1)\\)

\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u5185\u7f6e\u7684\u6808\u7c7b\u3002\u7136\u800c\uff0c\u67d0\u4e9b\u8bed\u8a00\u53ef\u80fd\u6ca1\u6709\u4e13\u95e8\u63d0\u4f9b\u6808\u7c7b\uff0c\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u5c06\u8be5\u8bed\u8a00\u7684\u201c\u6570\u7ec4\u201d\u6216\u201c\u94fe\u8868\u201d\u89c6\u4f5c\u6808\u6765\u4f7f\u7528\uff0c\u5e76\u5728\u7a0b\u5e8f\u903b\u8f91\u4e0a\u5ffd\u7565\u4e0e\u6808\u65e0\u5173\u7684\u64cd\u4f5c\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig stack.py
# \u521d\u59cb\u5316\u6808\n# Python \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a List \u5f53\u4f5c\u6808\u6765\u4f7f\u7528 \nstack: list[int] = []\n# \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.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.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.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_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.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.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.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
stack.rs
/* \u521d\u59cb\u5316\u6808 */\n// \u628a Vec \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nlet mut stack: Vec<i32> = Vec::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 */\nlet top = stack[stack.len() - 1];\n/* \u5143\u7d20\u51fa\u6808 */\nlet pop = stack.pop().unwrap();\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nlet size = stack.len();\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = stack.is_empty();\n
stack.c
// C \u672a\u63d0\u4f9b\u5185\u7f6e\u6808\n
stack.zig
\n
"},{"location":"chapter_stack_and_queue/stack/#512","title":"5.1.2 \u00a0 \u6808\u7684\u5b9e\u73b0","text":"

\u4e3a\u4e86\u6df1\u5165\u4e86\u89e3\u6808\u7684\u8fd0\u884c\u673a\u5236\uff0c\u6211\u4eec\u6765\u5c1d\u8bd5\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e2a\u6808\u7c7b\u3002

\u6808\u9075\u5faa\u5148\u5165\u540e\u51fa\u7684\u539f\u5219\uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u80fd\u5728\u6808\u9876\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\u3002\u7136\u800c\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u90fd\u53ef\u4ee5\u5728\u4efb\u610f\u4f4d\u7f6e\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\uff0c\u56e0\u6b64\u6808\u53ef\u4ee5\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":"1. \u00a0 \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

\u5982\u56fe 5-2 \u6240\u793a\uff0c\u5bf9\u4e8e\u5165\u6808\u64cd\u4f5c\uff0c\u6211\u4eec\u53ea\u9700\u5c06\u5143\u7d20\u63d2\u5165\u94fe\u8868\u5934\u90e8\uff0c\u8fd9\u79cd\u8282\u70b9\u63d2\u5165\u65b9\u6cd5\u88ab\u79f0\u4e3a\u201c\u5934\u63d2\u6cd5\u201d\u3002\u800c\u5bf9\u4e8e\u51fa\u6808\u64cd\u4f5c\uff0c\u53ea\u9700\u5c06\u5934\u8282\u70b9\u4ece\u94fe\u8868\u4e2d\u5220\u9664\u5373\u53ef\u3002

LinkedListStackpush()pop()

\u56fe 5-2 \u00a0 \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u6808\u7684\u5165\u6808\u51fa\u6808\u64cd\u4f5c

\u4ee5\u4e0b\u662f\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u6808\u7684\u793a\u4f8b\u4ee3\u7801\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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):\n\"\"\"\u5165\u6808\"\"\"\nnode = ListNode(val)\nnode.next = self._peek\nself._peek = node\nself._size += 1\ndef pop(self) -> int:\n\"\"\"\u51fa\u6808\"\"\"\nnum = self.peek()\nself._peek = self._peek.next\nself._size -= 1\nreturn num\ndef peek(self) -> int:\n\"\"\"\u8bbf\u95ee\u6808\u9876\u5143\u7d20\"\"\"\nif self.is_empty():\nraise IndexError(\"\u6808\u4e3a\u7a7a\")\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.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 isEmpty() {\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 (isEmpty())\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.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 (isEmpty())\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.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(num) {\nnext = stackPeek\n};\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 (IsEmpty())\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.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.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.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.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
linkedlist_stack.rs
/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\n#[allow(dead_code)]\npub struct LinkedListStack<T> {\nstack_peek: Option<Rc<RefCell<ListNode<T>>>>,   // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nstk_size: usize,                                // \u6808\u7684\u957f\u5ea6\n}\nimpl<T: Copy> LinkedListStack<T> {\npub fn new() -> Self {\nSelf {\nstack_peek: None,\nstk_size: 0,\n}\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\npub fn size(&self) -> usize {\nreturn self.stk_size;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\npub fn is_empty(&self) -> bool {\nreturn self.size() == 0;\n}\n/* \u5165\u6808 */\npub fn push(&mut self, num: T) {\nlet node = ListNode::new(num);\nnode.borrow_mut().next = self.stack_peek.take();\nself.stack_peek = Some(node);\nself.stk_size += 1;\n}\n/* \u51fa\u6808 */\npub fn pop(&mut self) -> Option<T> {\nself.stack_peek.take().map(|old_head| {\nmatch old_head.borrow_mut().next.take() {\nSome(new_head) => {\nself.stack_peek = Some(new_head);\n}\nNone => {\nself.stack_peek = None;\n}\n}\nself.stk_size -= 1;\nRc::try_unwrap(old_head).ok().unwrap().into_inner().val\n})\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\nself.stack_peek.as_ref()\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\nif let Some(node) = head {\nlet mut nums = self.to_array(node.borrow().next.as_ref());\nnums.push(node.borrow().val);\nreturn nums;\n}\nreturn Vec::new();\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.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\u51fd\u6570\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\u51fd\u6570\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());\n@memset(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
"},{"location":"chapter_stack_and_queue/stack/#2","title":"2. \u00a0 \u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"

\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u6808\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u7684\u5c3e\u90e8\u4f5c\u4e3a\u6808\u9876\u3002\u5982\u56fe 5-3 \u6240\u793a\uff0c\u5165\u6808\u4e0e\u51fa\u6808\u64cd\u4f5c\u5206\u522b\u5bf9\u5e94\u5728\u6570\u7ec4\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\u4e0e\u5220\u9664\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(1)\\) \u3002

ArrayStackpush()pop()

\u56fe 5-3 \u00a0 \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u6808\u7684\u5165\u6808\u51fa\u6808\u64cd\u4f5c

\u7531\u4e8e\u5165\u6808\u7684\u5143\u7d20\u53ef\u80fd\u4f1a\u6e90\u6e90\u4e0d\u65ad\u5730\u589e\u52a0\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u52a8\u6001\u6570\u7ec4\uff0c\u8fd9\u6837\u5c31\u65e0\u987b\u81ea\u884c\u5904\u7406\u6570\u7ec4\u6269\u5bb9\u95ee\u9898\u3002\u4ee5\u4e0b\u4e3a\u793a\u4f8b\u4ee3\u7801\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig array_stack.py
class ArrayStack:\n\"\"\"\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\"\"\"\ndef __init__(self):\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):\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.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 isEmpty() {\nreturn stack.size() == 0;\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 (isEmpty())\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.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.cs
/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\nprivate readonly 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.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.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.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 */\nisEmpty() {\nreturn this.#stack.length === 0;\n}\n/* \u5165\u6808 */\npush(num) {\nthis.#stack.push(num);\n}\n/* \u51fa\u6808 */\npop() {\nif (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\nreturn this.#stack.pop();\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\ntop() {\nif (this.isEmpty()) 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 */\nisEmpty(): 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.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\nreturn this.stack.pop();\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\ntop(): number | undefined {\nif (this.isEmpty()) 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.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
array_stack.rs
/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nstruct ArrayStack<T> {\nstack: Vec<T>,\n}\nimpl<T> ArrayStack<T> {\n/* \u521d\u59cb\u5316\u6808 */\nfn new() -> ArrayStack<T> {\nArrayStack::<T> { stack: Vec::<T>::new() }\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nfn size(&self) -> usize {\nself.stack.len()\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nfn is_empty(&self) -> bool {\nself.size() == 0\n}\n/* \u5165\u6808 */\nfn push(&mut self, num: T) {\nself.stack.push(num);\n}\n/* \u51fa\u6808 */\nfn pop(&mut self) -> Option<T> {\nmatch self.stack.pop() {\nSome(num) => Some(num),\nNone => None,\n}\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nfn peek(&self) -> Option<&T> {\nif self.is_empty() { panic!(\"\u6808\u4e3a\u7a7a\") };\nself.stack.last()\n}\n/* \u8fd4\u56de &Vec */\nfn to_array(&self) -> &Vec<T> {\n&self.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.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
"},{"location":"chapter_stack_and_queue/stack/#513","title":"5.1.3 \u00a0 \u4e24\u79cd\u5b9e\u73b0\u5bf9\u6bd4","text":"

\u652f\u6301\u64cd\u4f5c

\u4e24\u79cd\u5b9e\u73b0\u90fd\u652f\u6301\u6808\u5b9a\u4e49\u4e2d\u7684\u5404\u9879\u64cd\u4f5c\u3002\u6570\u7ec4\u5b9e\u73b0\u989d\u5916\u652f\u6301\u968f\u673a\u8bbf\u95ee\uff0c\u4f46\u8fd9\u5df2\u8d85\u51fa\u4e86\u6808\u7684\u5b9a\u4e49\u8303\u7574\uff0c\u56e0\u6b64\u4e00\u822c\u4e0d\u4f1a\u7528\u5230\u3002

\u65f6\u95f4\u6548\u7387

\u5728\u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0\u4e2d\uff0c\u5165\u6808\u548c\u51fa\u6808\u64cd\u4f5c\u90fd\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\u65f6\uff0c\u4f8b\u5982 int \u6216 double \uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\u3002

  • \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\u3002
  • \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\u53ef\u4ee5\u63d0\u4f9b\u66f4\u52a0\u7a33\u5b9a\u7684\u6548\u7387\u8868\u73b0\u3002

\u7a7a\u95f4\u6548\u7387

\u5728\u521d\u59cb\u5316\u5217\u8868\u65f6\uff0c\u7cfb\u7edf\u4f1a\u4e3a\u5217\u8868\u5206\u914d\u201c\u521d\u59cb\u5bb9\u91cf\u201d\uff0c\u8be5\u5bb9\u91cf\u53ef\u80fd\u8d85\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\u540e\u9000\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":""},{"location":"chapter_stack_and_queue/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","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\u52a3\u5316\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/#2-q-a","title":"2. \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

\u64a4\u9500\uff08undo\uff09\u548c\u53cd\u64a4\u9500\uff08redo\uff09\u5177\u4f53\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff1f

\u4f7f\u7528\u4e24\u4e2a\u5806\u6808\uff0c\u6808 A \u7528\u4e8e\u64a4\u9500\uff0c\u6808 B \u7528\u4e8e\u53cd\u64a4\u9500\u3002

  1. \u6bcf\u5f53\u7528\u6237\u6267\u884c\u4e00\u4e2a\u64cd\u4f5c\uff0c\u5c06\u8fd9\u4e2a\u64cd\u4f5c\u538b\u5165\u6808 A \uff0c\u5e76\u6e05\u7a7a\u6808 B \u3002
  2. \u5f53\u7528\u6237\u6267\u884c\u201c\u64a4\u9500\u201d\u65f6\uff0c\u4ece\u6808 A \u4e2d\u5f39\u51fa\u6700\u8fd1\u7684\u64cd\u4f5c\uff0c\u5e76\u5c06\u5176\u538b\u5165\u6808 B \u3002
  3. \u5f53\u7528\u6237\u6267\u884c\u201c\u53cd\u64a4\u9500\u201d\u65f6\uff0c\u4ece\u6808 B \u4e2d\u5f39\u51fa\u6700\u8fd1\u7684\u64cd\u4f5c\uff0c\u5e76\u5c06\u5176\u538b\u5165\u6808 A \u3002
"},{"location":"chapter_tree/","title":"\u7b2c 7 \u7ae0 \u00a0 \u6811","text":"

Abstract

\u53c2\u5929\u5927\u6811\u5145\u6ee1\u751f\u547d\u529b\uff0c\u5176\u6839\u6df1\u53f6\u8302\uff0c\u5206\u679d\u6276\u758f\u3002

\u5b83\u4e3a\u6211\u4eec\u5c55\u73b0\u4e86\u6570\u636e\u5206\u6cbb\u7684\u751f\u52a8\u5f62\u6001\u3002

"},{"location":"chapter_tree/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 7.1 \u00a0 \u4e8c\u53c9\u6811
  • 7.2 \u00a0 \u4e8c\u53c9\u6811\u904d\u5386
  • 7.3 \u00a0 \u4e8c\u53c9\u6811\u6570\u7ec4\u8868\u793a
  • 7.4 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811
  • 7.5 \u00a0 AVL \u6811 *
  • 7.6 \u00a0 \u5c0f\u7ed3
"},{"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\u6211\u4eec\u80fd\u5426\u7528\u6570\u7ec4\u6765\u8868\u793a\u4e8c\u53c9\u6811\u5462\uff1f\u7b54\u6848\u662f\u80af\u5b9a\u7684\u3002

"},{"location":"chapter_tree/array_representation_of_tree/#731","title":"7.3.1 \u00a0 \u8868\u793a\u5b8c\u7f8e\u4e8c\u53c9\u6811","text":"

\u5148\u5206\u6790\u4e00\u4e2a\u7b80\u5355\u6848\u4f8b\u3002\u7ed9\u5b9a\u4e00\u4e2a\u5b8c\u7f8e\u4e8c\u53c9\u6811\uff0c\u6211\u4eec\u5c06\u6240\u6709\u8282\u70b9\u6309\u7167\u5c42\u5e8f\u904d\u5386\u7684\u987a\u5e8f\u5b58\u50a8\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\uff0c\u5219\u6bcf\u4e2a\u8282\u70b9\u90fd\u5bf9\u5e94\u552f\u4e00\u7684\u6570\u7ec4\u7d22\u5f15\u3002

\u6839\u636e\u5c42\u5e8f\u904d\u5386\u7684\u7279\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u63a8\u5bfc\u51fa\u7236\u8282\u70b9\u7d22\u5f15\u4e0e\u5b50\u8282\u70b9\u7d22\u5f15\u4e4b\u95f4\u7684\u201c\u6620\u5c04\u516c\u5f0f\u201d\uff1a\u82e5\u8282\u70b9\u7684\u7d22\u5f15\u4e3a \\(i\\) \uff0c\u5219\u8be5\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 1\\) \uff0c\u53f3\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 2\\) \u3002\u56fe 7-12 \u5c55\u793a\u4e86\u5404\u4e2a\u8282\u70b9\u7d22\u5f15\u4e4b\u95f4\u7684\u6620\u5c04\u5173\u7cfb\u3002

\u56fe 7-12 \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a

\u6620\u5c04\u516c\u5f0f\u7684\u89d2\u8272\u76f8\u5f53\u4e8e\u94fe\u8868\u4e2d\u7684\u6307\u9488\u3002\u7ed9\u5b9a\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u8282\u70b9\uff0c\u6211\u4eec\u90fd\u53ef\u4ee5\u901a\u8fc7\u6620\u5c04\u516c\u5f0f\u6765\u8bbf\u95ee\u5b83\u7684\u5de6\uff08\u53f3\uff09\u5b50\u8282\u70b9\u3002

"},{"location":"chapter_tree/array_representation_of_tree/#732","title":"7.3.2 \u00a0 \u8868\u793a\u4efb\u610f\u4e8c\u53c9\u6811","text":"

\u5b8c\u7f8e\u4e8c\u53c9\u6811\u662f\u4e00\u4e2a\u7279\u4f8b\uff0c\u5728\u4e8c\u53c9\u6811\u7684\u4e2d\u95f4\u5c42\u901a\u5e38\u5b58\u5728\u8bb8\u591a \\(\\text{None}\\) \u3002\u7531\u4e8e\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5e76\u4e0d\u5305\u542b\u8fd9\u4e9b \\(\\text{None}\\) \uff0c\u56e0\u6b64\u6211\u4eec\u65e0\u6cd5\u4ec5\u51ed\u8be5\u5e8f\u5217\u6765\u63a8\u6d4b \\(\\text{None}\\) \u7684\u6570\u91cf\u548c\u5206\u5e03\u4f4d\u7f6e\u3002\u8fd9\u610f\u5473\u7740\u5b58\u5728\u591a\u79cd\u4e8c\u53c9\u6811\u7ed3\u6784\u90fd\u7b26\u5408\u8be5\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u3002

\u5982\u56fe 7-13 \u6240\u793a\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u975e\u5b8c\u7f8e\u4e8c\u53c9\u6811\uff0c\u4e0a\u8ff0\u7684\u6570\u7ec4\u8868\u793a\u65b9\u6cd5\u5df2\u7ecf\u5931\u6548\u3002

\u56fe 7-13 \u00a0 \u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5bf9\u5e94\u591a\u79cd\u4e8c\u53c9\u6811\u53ef\u80fd\u6027

\u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u5728\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u4e2d\u663e\u5f0f\u5730\u5199\u51fa\u6240\u6709 \\(\\text{None}\\) \u3002\u5982\u56fe 7-14 \u6240\u793a\uff0c\u8fd9\u6837\u5904\u7406\u540e\uff0c\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5c31\u53ef\u4ee5\u552f\u4e00\u8868\u793a\u4e8c\u53c9\u6811\u4e86\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig
# \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a\n# \u4f7f\u7528 None \u6765\u8868\u793a\u7a7a\u4f4d\ntree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int \u6700\u5927\u503c INT_MAX \u6807\u8bb0\u7a7a\u4f4d\nvector<int> tree = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int \u7684\u5305\u88c5\u7c7b Integer \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u6765\u6807\u8bb0\u7a7a\u4f4d\nInteger[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int? \u53ef\u7a7a\u7c7b\u578b \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u6765\u6807\u8bb0\u7a7a\u4f4d\nint?[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 any \u7c7b\u578b\u7684\u5207\u7247, \u5c31\u53ef\u4ee5\u4f7f\u7528 nil \u6765\u6807\u8bb0\u7a7a\u4f4d\ntree := []any{1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15}\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 Int? \u53ef\u7a7a\u7c7b\u578b \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 nil \u6765\u6807\u8bb0\u7a7a\u4f4d\nlet tree: [Int?] = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 null \u6765\u8868\u793a\u7a7a\u4f4d\nlet tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 null \u6765\u8868\u793a\u7a7a\u4f4d\nlet tree: (number | null)[] = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int? \u53ef\u7a7a\u7c7b\u578b \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u6765\u6807\u8bb0\u7a7a\u4f4d\nList<int?> tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 None \u6765\u6807\u8bb0\u7a7a\u4f4d\nlet tree = [Some(1), Some(2), Some(3), Some(4), None, Some(6), Some(7), Some(8), Some(9), None, None, Some(12), None, None, Some(15)];\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int \u6700\u5927\u503c\u6807\u8bb0\u7a7a\u4f4d\uff0c\u56e0\u6b64\u8981\u6c42\u8282\u70b9\u503c\u4e0d\u80fd\u4e3a INT_MAX\nint tree[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
\n

\u56fe 7-14 \u00a0 \u4efb\u610f\u7c7b\u578b\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a

\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u5b8c\u5168\u4e8c\u53c9\u6811\u975e\u5e38\u9002\u5408\u4f7f\u7528\u6570\u7ec4\u6765\u8868\u793a\u3002\u56de\u987e\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u5b9a\u4e49\uff0c\\(\\text{None}\\) \u53ea\u51fa\u73b0\u5728\u6700\u5e95\u5c42\u4e14\u9760\u53f3\u7684\u4f4d\u7f6e\uff0c\u56e0\u6b64\u6240\u6709 \\(\\text{None}\\) \u4e00\u5b9a\u51fa\u73b0\u5728\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u7684\u672b\u5c3e\u3002

\u8fd9\u610f\u5473\u7740\u4f7f\u7528\u6570\u7ec4\u8868\u793a\u5b8c\u5168\u4e8c\u53c9\u6811\u65f6\uff0c\u53ef\u4ee5\u7701\u7565\u5b58\u50a8\u6240\u6709 \\(\\text{None}\\) \uff0c\u975e\u5e38\u65b9\u4fbf\u3002\u56fe 7-15 \u7ed9\u51fa\u4e86\u4e00\u4e2a\u4f8b\u5b50\u3002

\u56fe 7-15 \u00a0 \u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a

\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u4e2a\u57fa\u4e8e\u6570\u7ec4\u8868\u793a\u7684\u4e8c\u53c9\u6811\uff0c\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\u64cd\u4f5c\u3002

  • \u7ed9\u5b9a\u67d0\u8282\u70b9\uff0c\u83b7\u53d6\u5b83\u7684\u503c\u3001\u5de6\uff08\u53f3\uff09\u5b50\u8282\u70b9\u3001\u7236\u8282\u70b9\u3002
  • \u83b7\u53d6\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u3001\u540e\u5e8f\u904d\u5386\u3001\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig array_binary_tree.py
class ArrayBinaryTree:\n\"\"\"\u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b\"\"\"\ndef __init__(self, arr: list[int | None]):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself._tree = list(arr)\ndef size(self):\n\"\"\"\u8282\u70b9\u6570\u91cf\"\"\"\nreturn len(self._tree)\ndef val(self, i: int) -> int:\n\"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c\"\"\"\n# \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de None \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif i < 0 or i >= self.size():\nreturn None\nreturn self._tree[i]\ndef left(self, i: int) -> int | None:\n\"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\nreturn 2 * i + 1\ndef right(self, i: int) -> int | None:\n\"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\nreturn 2 * i + 2\ndef parent(self, i: int) -> int | None:\n\"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\nreturn (i - 1) // 2\ndef level_order(self) -> list[int]:\n\"\"\"\u5c42\u5e8f\u904d\u5386\"\"\"\nself.res = []\n# \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor i in range(self.size()):\nif self.val(i) is not None:\nself.res.append(self.val(i))\nreturn self.res\ndef dfs(self, i: int, order: str):\n\"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386\"\"\"\nif self.val(i) is None:\nreturn\n# \u524d\u5e8f\u904d\u5386\nif order == \"pre\":\nself.res.append(self.val(i))\nself.dfs(self.left(i), order)\n# \u4e2d\u5e8f\u904d\u5386\nif order == \"in\":\nself.res.append(self.val(i))\nself.dfs(self.right(i), order)\n# \u540e\u5e8f\u904d\u5386\nif order == \"post\":\nself.res.append(self.val(i))\ndef pre_order(self) -> list[int]:\n\"\"\"\u524d\u5e8f\u904d\u5386\"\"\"\nself.res = []\nself.dfs(0, order=\"pre\")\nreturn self.res\ndef in_order(self) -> list[int]:\n\"\"\"\u4e2d\u5e8f\u904d\u5386\"\"\"\nself.res = []\nself.dfs(0, order=\"in\")\nreturn self.res\ndef post_order(self) -> list[int]:\n\"\"\"\u540e\u5e8f\u904d\u5386\"\"\"\nself.res = []\nself.dfs(0, order=\"post\")\nreturn self.res\n
array_binary_tree.cpp
/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nArrayBinaryTree(vector<int> arr) {\ntree = arr;\n}\n/* \u8282\u70b9\u6570\u91cf */\nint size() {\nreturn tree.size();\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nint val(int i) {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de INT_MAX \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif (i < 0 || i >= size())\nreturn INT_MAX;\nreturn tree[i];\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint right(int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint parent(int i) {\nreturn (i - 1) / 2;\n}\n/* \u5c42\u5e8f\u904d\u5386 */\nvector<int> levelOrder() {\nvector<int> res;\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i < size(); i++) {\nif (val(i) != INT_MAX)\nres.push_back(val(i));\n}\nreturn res;\n}\n/* \u524d\u5e8f\u904d\u5386 */\nvector<int> preOrder() {\nvector<int> res;\ndfs(0, \"pre\", res);\nreturn res;\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nvector<int> inOrder() {\nvector<int> res;\ndfs(0, \"in\", res);\nreturn res;\n}\n/* \u540e\u5e8f\u904d\u5386 */\nvector<int> postOrder() {\nvector<int> res;\ndfs(0, \"post\", res);\nreturn res;\n}\nprivate:\nvector<int> tree;\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nvoid dfs(int i, string order, vector<int> &res) {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nif (val(i) == INT_MAX)\nreturn;\n// \u524d\u5e8f\u904d\u5386\nif (order == \"pre\")\nres.push_back(val(i));\ndfs(left(i), order, res);\n// \u4e2d\u5e8f\u904d\u5386\nif (order == \"in\")\nres.push_back(val(i));\ndfs(right(i), order, res);\n// \u540e\u5e8f\u904d\u5386\nif (order == \"post\")\nres.push_back(val(i));\n}\n};\n
array_binary_tree.java
/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\nprivate List<Integer> tree;\n/* \u6784\u9020\u65b9\u6cd5 */\npublic ArrayBinaryTree(List<Integer> arr) {\ntree = new ArrayList<>(arr);\n}\n/* \u8282\u70b9\u6570\u91cf */\npublic int size() {\nreturn tree.size();\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\npublic Integer val(int i) {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif (i < 0 || i >= size())\nreturn null;\nreturn tree.get(i);\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\npublic Integer left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\npublic Integer right(int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\npublic Integer parent(int i) {\nreturn (i - 1) / 2;\n}\n/* \u5c42\u5e8f\u904d\u5386 */\npublic List<Integer> levelOrder() {\nList<Integer> res = new ArrayList<>();\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i < size(); i++) {\nif (val(i) != null)\nres.add(val(i));\n}\nreturn res;\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nprivate void dfs(Integer i, String order, List<Integer> res) {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nif (val(i) == null)\nreturn;\n// \u524d\u5e8f\u904d\u5386\nif (order == \"pre\")\nres.add(val(i));\ndfs(left(i), order, res);\n// \u4e2d\u5e8f\u904d\u5386\nif (order == \"in\")\nres.add(val(i));\ndfs(right(i), order, res);\n// \u540e\u5e8f\u904d\u5386\nif (order == \"post\")\nres.add(val(i));\n}\n/* \u524d\u5e8f\u904d\u5386 */\npublic List<Integer> preOrder() {\nList<Integer> res = new ArrayList<>();\ndfs(0, \"pre\", res);\nreturn res;\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\npublic List<Integer> inOrder() {\nList<Integer> res = new ArrayList<>();\ndfs(0, \"in\", res);\nreturn res;\n}\n/* \u540e\u5e8f\u904d\u5386 */\npublic List<Integer> postOrder() {\nList<Integer> res = new ArrayList<>();\ndfs(0, \"post\", res);\nreturn res;\n}\n}\n
array_binary_tree.cs
/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\nprivate readonly List<int?> tree;\n/* \u6784\u9020\u65b9\u6cd5 */\npublic ArrayBinaryTree(List<int?> arr) {\ntree = new List<int?>(arr);\n}\n/* \u8282\u70b9\u6570\u91cf */\npublic int Size() {\nreturn tree.Count;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\npublic int? Val(int i) {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif (i < 0 || i >= Size())\nreturn null;\nreturn tree[i];\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\npublic int Left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\npublic int Right(int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\npublic int Parent(int i) {\nreturn (i - 1) / 2;\n}\n/* \u5c42\u5e8f\u904d\u5386 */\npublic List<int> LevelOrder() {\nList<int> res = new();\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i < Size(); i++) {\nif (Val(i).HasValue)\nres.Add(Val(i).Value);\n}\nreturn res;\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nprivate void DFS(int i, string order, List<int> res) {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nif (!Val(i).HasValue)\nreturn;\n// \u524d\u5e8f\u904d\u5386\nif (order == \"pre\")\nres.Add(Val(i).Value);\nDFS(Left(i), order, res);\n// \u4e2d\u5e8f\u904d\u5386\nif (order == \"in\")\nres.Add(Val(i).Value);\nDFS(Right(i), order, res);\n// \u540e\u5e8f\u904d\u5386\nif (order == \"post\")\nres.Add(Val(i).Value);\n}\n/* \u524d\u5e8f\u904d\u5386 */\npublic List<int> PreOrder() {\nList<int> res = new();\nDFS(0, \"pre\", res);\nreturn res;\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\npublic List<int> InOrder() {\nList<int> res = new();\nDFS(0, \"in\", res);\nreturn res;\n}\n/* \u540e\u5e8f\u904d\u5386 */\npublic List<int> PostOrder() {\nList<int> res = new();\nDFS(0, \"post\", res);\nreturn res;\n}\n}\n
array_binary_tree.go
/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\ntype arrayBinaryTree struct {\ntree []any\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newArrayBinaryTree(arr []any) *arrayBinaryTree {\nreturn &arrayBinaryTree{\ntree: arr,\n}\n}\n/* \u8282\u70b9\u6570\u91cf */\nfunc (abt *arrayBinaryTree) size() int {\nreturn len(abt.tree)\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nfunc (abt *arrayBinaryTree) val(i int) any {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif i < 0 || i >= abt.size() {\nreturn nil\n}\nreturn abt.tree[i]\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) left(i int) int {\nreturn 2*i + 1\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) right(i int) int {\nreturn 2*i + 2\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) parent(i int) int {\nreturn (i - 1) / 2\n}\n/* \u5c42\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) levelOrder() []any {\nvar res []any\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor i := 0; i < abt.size(); i++ {\nif abt.val(i) != nil {\nres = append(res, abt.val(i))\n}\n}\nreturn res\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nfunc (abt *arrayBinaryTree) dfs(i int, order string, res *[]any) {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nif abt.val(i) == nil {\nreturn\n}\n// \u524d\u5e8f\u904d\u5386\nif order == \"pre\" {\n*res = append(*res, abt.val(i))\n}\nabt.dfs(abt.left(i), order, res)\n// \u4e2d\u5e8f\u904d\u5386\nif order == \"in\" {\n*res = append(*res, abt.val(i))\n}\nabt.dfs(abt.right(i), order, res)\n// \u540e\u5e8f\u904d\u5386\nif order == \"post\" {\n*res = append(*res, abt.val(i))\n}\n}\n/* \u524d\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) preOrder() []any {\nvar res []any\nabt.dfs(0, \"pre\", &res)\nreturn res\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) inOrder() []any {\nvar res []any\nabt.dfs(0, \"in\", &res)\nreturn res\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) postOrder() []any {\nvar res []any\nabt.dfs(0, \"post\", &res)\nreturn res\n}\n
array_binary_tree.swift
/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\nprivate var tree: [Int?]\n/* \u6784\u9020\u65b9\u6cd5 */\ninit(arr: [Int?]) {\ntree = arr\n}\n/* \u8282\u70b9\u6570\u91cf */\nfunc size() -> Int {\ntree.count\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nfunc val(i: Int) -> Int? {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif i < 0 || i >= size() {\nreturn nil\n}\nreturn tree[i]\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc left(i: Int) -> Int {\n2 * i + 1\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc right(i: Int) -> Int {\n2 * i + 2\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc parent(i: Int) -> Int {\n(i - 1) / 2\n}\n/* \u5c42\u5e8f\u904d\u5386 */\nfunc levelOrder() -> [Int] {\nvar res: [Int] = []\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor i in stride(from: 0, to: size(), by: 1) {\nif let val = val(i: i) {\nres.append(val)\n}\n}\nreturn res\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nprivate func dfs(i: Int, order: String, res: inout [Int]) {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nguard let val = val(i: i) else {\nreturn\n}\n// \u524d\u5e8f\u904d\u5386\nif order == \"pre\" {\nres.append(val)\n}\ndfs(i: left(i: i), order: order, res: &res)\n// \u4e2d\u5e8f\u904d\u5386\nif order == \"in\" {\nres.append(val)\n}\ndfs(i: right(i: i), order: order, res: &res)\n// \u540e\u5e8f\u904d\u5386\nif order == \"post\" {\nres.append(val)\n}\n}\n/* \u524d\u5e8f\u904d\u5386 */\nfunc preOrder() -> [Int] {\nvar res: [Int] = []\ndfs(i: 0, order: \"pre\", res: &res)\nreturn res\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc inOrder() -> [Int] {\nvar res: [Int] = []\ndfs(i: 0, order: \"in\", res: &res)\nreturn res\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfunc postOrder() -> [Int] {\nvar res: [Int] = []\ndfs(i: 0, order: \"post\", res: &res)\nreturn res\n}\n}\n
array_binary_tree.js
/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n#tree;\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor(arr) {\nthis.#tree = arr;\n}\n/* \u8282\u70b9\u6570\u91cf */\nsize() {\nreturn this.#tree.length;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nval(i) {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif (i < 0 || i >= this.size()) return null;\nreturn this.#tree[i];\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nleft(i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nright(i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nparent(i) {\nreturn (i - 1) / 2;\n}\n/* \u5c42\u5e8f\u904d\u5386 */\nlevelOrder() {\nlet res = [];\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (let i = 0; i < this.size(); i++) {\nif (this.val(i) !== null) res.push(this.val(i));\n}\nreturn res;\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n#dfs(i, order, res) {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nif (this.val(i) === null) return;\n// \u524d\u5e8f\u904d\u5386\nif (order === 'pre') res.push(this.val(i));\nthis.#dfs(this.left(i), order, res);\n// \u4e2d\u5e8f\u904d\u5386\nif (order === 'in') res.push(this.val(i));\nthis.#dfs(this.right(i), order, res);\n// \u540e\u5e8f\u904d\u5386\nif (order === 'post') res.push(this.val(i));\n}\n/* \u524d\u5e8f\u904d\u5386 */\npreOrder() {\nconst res = [];\nthis.#dfs(0, 'pre', res);\nreturn res;\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\ninOrder() {\nconst res = [];\nthis.#dfs(0, 'in', res);\nreturn res;\n}\n/* \u540e\u5e8f\u904d\u5386 */\npostOrder() {\nconst res = [];\nthis.#dfs(0, 'post', res);\nreturn res;\n}\n}\n
array_binary_tree.ts
/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n#tree: (number | null)[];\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor(arr: (number | null)[]) {\nthis.#tree = arr;\n}\n/* \u8282\u70b9\u6570\u91cf */\nsize(): number {\nreturn this.#tree.length;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nval(i: number): number | null {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif (i < 0 || i >= this.size()) return null;\nreturn this.#tree[i];\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nleft(i: number): number {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nright(i: number): number {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nparent(i: number): number {\nreturn (i - 1) / 2;\n}\n/* \u5c42\u5e8f\u904d\u5386 */\nlevelOrder(): number[] {\nlet res = [];\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (let i = 0; i < this.size(); i++) {\nif (this.val(i) !== null) res.push(this.val(i));\n}\nreturn res;\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n#dfs(i: number, order: Order, res: (number | null)[]): void {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nif (this.val(i) === null) return;\n// \u524d\u5e8f\u904d\u5386\nif (order === 'pre') res.push(this.val(i));\nthis.#dfs(this.left(i), order, res);\n// \u4e2d\u5e8f\u904d\u5386\nif (order === 'in') res.push(this.val(i));\nthis.#dfs(this.right(i), order, res);\n// \u540e\u5e8f\u904d\u5386\nif (order === 'post') res.push(this.val(i));\n}\n/* \u524d\u5e8f\u904d\u5386 */\npreOrder(): (number | null)[] {\nconst res = [];\nthis.#dfs(0, 'pre', res);\nreturn res;\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\ninOrder(): (number | null)[] {\nconst res = [];\nthis.#dfs(0, 'in', res);\nreturn res;\n}\n/* \u540e\u5e8f\u904d\u5386 */\npostOrder(): (number | null)[] {\nconst res = [];\nthis.#dfs(0, 'post', res);\nreturn res;\n}\n}\n
array_binary_tree.dart
/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\nlate List<int?> _tree;\n/* \u6784\u9020\u65b9\u6cd5 */\nArrayBinaryTree(this._tree);\n/* \u8282\u70b9\u6570\u91cf */\nint size() {\nreturn _tree.length;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nint? val(int i) {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif (i < 0 || i >= size()) {\nreturn null;\n}\nreturn _tree[i];\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint? left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint? right(int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint? parent(int i) {\nreturn (i - 1) ~/ 2;\n}\n/* \u5c42\u5e8f\u904d\u5386 */\nList<int> levelOrder() {\nList<int> res = [];\nfor (int i = 0; i < size(); i++) {\nif (val(i) != null) {\nres.add(val(i)!);\n}\n}\nreturn res;\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nvoid dfs(int i, String order, List<int?> res) {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nif (val(i) == null) {\nreturn;\n}\n// \u524d\u5e8f\u904d\u5386\nif (order == 'pre') {\nres.add(val(i));\n}\ndfs(left(i)!, order, res);\n// \u4e2d\u5e8f\u904d\u5386\nif (order == 'in') {\nres.add(val(i));\n}\ndfs(right(i)!, order, res);\n// \u540e\u5e8f\u904d\u5386\nif (order == 'post') {\nres.add(val(i));\n}\n}\n/* \u524d\u5e8f\u904d\u5386 */\nList<int?> preOrder() {\nList<int?> res = [];\ndfs(0, 'pre', res);\nreturn res;\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nList<int?> inOrder() {\nList<int?> res = [];\ndfs(0, 'in', res);\nreturn res;\n}\n/* \u540e\u5e8f\u904d\u5386 */\nList<int?> postOrder() {\nList<int?> res = [];\ndfs(0, 'post', res);\nreturn res;\n}\n}\n
array_binary_tree.rs
/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nstruct ArrayBinaryTree {\ntree: Vec<Option<i32>>,\n}\nimpl ArrayBinaryTree {\n/* \u6784\u9020\u65b9\u6cd5 */\nfn new(arr: Vec<Option<i32>>) -> Self {\nSelf { tree: arr }\n}\n/* \u8282\u70b9\u6570\u91cf */\nfn size(&self) -> i32 {\nself.tree.len() as i32\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nfn val(&self, i: i32) -> Option<i32> {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de None \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif i < 0 || i >= self.size() {\nNone\n} else {\nself.tree[i as usize]\n}\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfn left(&self, i: i32) -> i32 {\n2 * i + 1\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfn right(&self, i: i32) -> i32 {\n2 * i + 2\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfn parent(&self, i: i32) -> i32 {\n(i - 1) / 2\n}\n/* \u5c42\u5e8f\u904d\u5386 */\nfn level_order(&self) -> Vec<i32> {\nlet mut res = vec![];\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor i in 0..self.size() {\nif let Some(val) = self.val(i) {\nres.push(val)\n}\n}\nres\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nfn dfs(&self, i: i32, order: &str, res: &mut Vec<i32>) {\nif self.val(i).is_none() {\nreturn;\n}\nlet val = self.val(i).unwrap();\n// \u524d\u5e8f\u904d\u5386\nif order == \"pre\" {\nres.push(val);\n}\nself.dfs(self.left(i), order, res);\n// \u4e2d\u5e8f\u904d\u5386\nif order == \"in\" {\nres.push(val);\n}\nself.dfs(self.right(i), order, res);\n// \u540e\u5e8f\u904d\u5386\nif order == \"post\" {\nres.push(val);\n}\n}\n/* \u524d\u5e8f\u904d\u5386 */\nfn pre_order(&self) -> Vec<i32> {\nlet mut res = vec![];\nself.dfs(0, \"pre\", &mut res);\nres\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfn in_order(&self) -> Vec<i32> {\nlet mut res = vec![];\nself.dfs(0, \"in\", &mut res);\nres\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfn post_order(&self) -> Vec<i32> {\nlet mut res = vec![];\nself.dfs(0, \"post\", &mut res);\nres\n}\n}\n
array_binary_tree.c
/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nstruct arrayBinaryTree {\nvector *tree;\n};\ntypedef struct arrayBinaryTree arrayBinaryTree;\n/* \u6784\u9020\u51fd\u6570 */\narrayBinaryTree *newArrayBinaryTree(vector *arr) {\narrayBinaryTree *newABT = malloc(sizeof(arrayBinaryTree));\nnewABT->tree = arr;\nreturn newABT;\n}\n/* \u8282\u70b9\u6570\u91cf */\nint size(arrayBinaryTree *abt) {\nreturn abt->tree->size;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nint val(arrayBinaryTree *abt, int i) {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de INT_MAX \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif (i < 0 || i >= size(abt))\nreturn INT_MAX;\nreturn *(int *)abt->tree->data[i];\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nvoid dfs(arrayBinaryTree *abt, int i, const char *order, vector *res) {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nif (val(abt, i) == INT_MAX)\nreturn;\n// \u524d\u5e8f\u904d\u5386\nif (strcmp(order, \"pre\") == 0) {\nint tmp = val(abt, i);\nvectorPushback(res, &tmp, sizeof(tmp));\n}\ndfs(abt, left(i), order, res);\n// \u4e2d\u5e8f\u904d\u5386\nif (strcmp(order, \"in\") == 0) {\nint tmp = val(abt, i);\nvectorPushback(res, &tmp, sizeof(tmp));\n}\ndfs(abt, right(i), order, res);\n// \u540e\u5e8f\u904d\u5386\nif (strcmp(order, \"post\") == 0) {\nint tmp = val(abt, i);\nvectorPushback(res, &tmp, sizeof(tmp));\n}\n}\n/* \u5c42\u5e8f\u904d\u5386 */\nvector *levelOrder(arrayBinaryTree *abt) {\nvector *res = newVector();\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i < size(abt); i++) {\nif (val(abt, i) != INT_MAX) {\nint tmp = val(abt, i);\nvectorPushback(res, &tmp, sizeof(int));\n}\n}\nreturn res;\n}\n/* \u524d\u5e8f\u904d\u5386 */\nvector *preOrder(arrayBinaryTree *abt) {\nvector *res = newVector();\ndfs(abt, 0, \"pre\", res);\nreturn res;\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nvector *inOrder(arrayBinaryTree *abt) {\nvector *res = newVector();\ndfs(abt, 0, \"in\", res);\nreturn res;\n}\n/* \u540e\u5e8f\u904d\u5386 */\nvector *postOrder(arrayBinaryTree *abt) {\nvector *res = newVector();\ndfs(abt, 0, \"post\", res);\nreturn res;\n}\n
array_binary_tree.zig
[class]{ArrayBinaryTree}-[func]{}\n
"},{"location":"chapter_tree/array_representation_of_tree/#733","title":"7.3.3 \u00a0 \u4f18\u52bf\u4e0e\u5c40\u9650\u6027","text":"

\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a\u4e3b\u8981\u6709\u4ee5\u4e0b\u4f18\u70b9\u3002

  • \u6570\u7ec4\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\uff0c\u5bf9\u7f13\u5b58\u53cb\u597d\uff0c\u8bbf\u95ee\u4e0e\u904d\u5386\u901f\u5ea6\u8f83\u5feb\u3002
  • \u4e0d\u9700\u8981\u5b58\u50a8\u6307\u9488\uff0c\u6bd4\u8f83\u8282\u7701\u7a7a\u95f4\u3002
  • \u5141\u8bb8\u968f\u673a\u8bbf\u95ee\u8282\u70b9\u3002

\u7136\u800c\uff0c\u6570\u7ec4\u8868\u793a\u4e5f\u5b58\u5728\u4e00\u4e9b\u5c40\u9650\u6027\u3002

  • \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\u3002
  • \u5f53\u4e8c\u53c9\u6811\u4e2d\u5b58\u5728\u5927\u91cf \\(\\text{None}\\) \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
"},{"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\u56fe 7-24 \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

\u56fe 7-24 \u00a0 AVL \u6811\u5728\u5220\u9664\u8282\u70b9\u540e\u53d1\u751f\u9000\u5316

\u518d\u4f8b\u5982\uff0c\u5728\u56fe 7-25 \u7684\u5b8c\u7f8e\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e24\u4e2a\u8282\u70b9\u540e\uff0c\u6811\u5c06\u4e25\u91cd\u5411\u5de6\u503e\u659c\uff0c\u67e5\u627e\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u968f\u4e4b\u6076\u5316\u3002

\u56fe 7-25 \u00a0 AVL \u6811\u5728\u63d2\u5165\u8282\u70b9\u540e\u53d1\u751f\u9000\u5316

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":"

AVL \u6811\u65e2\u662f\u4e8c\u53c9\u641c\u7d22\u6811\u4e5f\u662f\u5e73\u8861\u4e8c\u53c9\u6811\uff0c\u540c\u65f6\u6ee1\u8db3\u8fd9\u4e24\u7c7b\u4e8c\u53c9\u6811\u7684\u6240\u6709\u6027\u8d28\uff0c\u56e0\u6b64\u4e5f\u88ab\u79f0\u4e3a\u300c\u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811 balanced binary search tree\u300d\u3002

"},{"location":"chapter_tree/avl_tree/#1","title":"1. \u00a0 \u8282\u70b9\u9ad8\u5ea6","text":"

\u7531\u4e8e AVL \u6811\u7684\u76f8\u5173\u64cd\u4f5c\u9700\u8981\u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u4e3a\u8282\u70b9\u7c7b\u6dfb\u52a0 height \u53d8\u91cf\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig
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: TreeNode | None = None   # \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nself.right: TreeNode | None = None  # \u53f3\u5b50\u8282\u70b9\u5f15\u7528\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
/* 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 {\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\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 {\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
/* 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\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
use std::rc::Rc;\nuse std::cell::RefCell;\n/* AVL \u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\nval: i32,                               // \u8282\u70b9\u503c\nheight: i32,                            // \u8282\u70b9\u9ad8\u5ea6\nleft: Option<Rc<RefCell<TreeNode>>>,    // \u5de6\u5b50\u8282\u70b9\nright: Option<Rc<RefCell<TreeNode>>>,   // \u53f3\u5b50\u8282\u70b9\n}\nimpl TreeNode {\n/* \u6784\u9020\u65b9\u6cd5 */\nfn new(val: i32) -> Rc<RefCell<Self>> {\nRc::new(RefCell::new(Self {\nval,\nheight: 0,\nleft: None,\nright: None\n}))\n}\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
\n

\u201c\u8282\u70b9\u9ad8\u5ea6\u201d\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

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.dart
/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode? node) {\n// \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\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
avl_tree.rs
/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfn height(node: OptionTreeNodeRc) -> i32 {\n// \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\nmatch node {\nSome(node) => node.borrow().height,\nNone => -1,\n}\n}\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfn update_height(node: OptionTreeNodeRc) {\nif let Some(node) = node {\nlet left = node.borrow().left.clone();\nlet right = node.borrow().right.clone();\n// \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nnode.borrow_mut().height = std::cmp::max(Self::height(left), Self::height(right)) + 1;\n}\n}\n
avl_tree.c
/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode *node) {\n// \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\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.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 = @max(self.height(node.?.left), self.height(node.?.right)) + 1;\n}\n
"},{"location":"chapter_tree/avl_tree/#2","title":"2. \u00a0 \u8282\u70b9\u5e73\u8861\u56e0\u5b50","text":"

\u8282\u70b9\u7684\u300c\u5e73\u8861\u56e0\u5b50 balance factor\u300d\u5b9a\u4e49\u4e3a\u8282\u70b9\u5de6\u5b50\u6811\u7684\u9ad8\u5ea6\u51cf\u53bb\u53f3\u5b50\u6811\u7684\u9ad8\u5ea6\uff0c\u540c\u65f6\u89c4\u5b9a\u7a7a\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u4e3a 0 \u3002\u6211\u4eec\u540c\u6837\u5c06\u83b7\u53d6\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u7684\u529f\u80fd\u5c01\u88c5\u6210\u51fd\u6570\uff0c\u65b9\u4fbf\u540e\u7eed\u4f7f\u7528\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
avl_tree.rs
/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfn balance_factor(node: OptionTreeNodeRc) -> i32 {\nmatch node {\n// \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nNone => 0,\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nSome(node) => {\nSelf::height(node.borrow().left.clone()) - Self::height(node.borrow().right.clone())\n}\n}\n}\n
avl_tree.c
/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode *node) {\n// \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\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.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

Note

\u8bbe\u5e73\u8861\u56e0\u5b50\u4e3a \\(f\\) \uff0c\u5219\u4e00\u68f5 AVL \u6811\u7684\u4efb\u610f\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u7686\u6ee1\u8db3 \\(-1 \\le f \\le 1\\) \u3002

"},{"location":"chapter_tree/avl_tree/#752-avl","title":"7.5.2 \u00a0 AVL \u6811\u65cb\u8f6c","text":"

AVL \u6811\u7684\u7279\u70b9\u5728\u4e8e\u201c\u65cb\u8f6c\u201d\u64cd\u4f5c\uff0c\u5b83\u80fd\u591f\u5728\u4e0d\u5f71\u54cd\u4e8c\u53c9\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u7684\u524d\u63d0\u4e0b\uff0c\u4f7f\u5931\u8861\u8282\u70b9\u91cd\u65b0\u6062\u590d\u5e73\u8861\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u65cb\u8f6c\u64cd\u4f5c\u65e2\u80fd\u4fdd\u6301\u201c\u4e8c\u53c9\u641c\u7d22\u6811\u201d\u7684\u6027\u8d28\uff0c\u4e5f\u80fd\u4f7f\u6811\u91cd\u65b0\u53d8\u4e3a\u201c\u5e73\u8861\u4e8c\u53c9\u6811\u201d\u3002

\u6211\u4eec\u5c06\u5e73\u8861\u56e0\u5b50\u7edd\u5bf9\u503c \\(> 1\\) \u7684\u8282\u70b9\u79f0\u4e3a\u201c\u5931\u8861\u8282\u70b9\u201d\u3002\u6839\u636e\u8282\u70b9\u5931\u8861\u60c5\u51b5\u7684\u4e0d\u540c\uff0c\u65cb\u8f6c\u64cd\u4f5c\u5206\u4e3a\u56db\u79cd\uff1a\u53f3\u65cb\u3001\u5de6\u65cb\u3001\u5148\u53f3\u65cb\u540e\u5de6\u65cb\u3001\u5148\u5de6\u65cb\u540e\u53f3\u65cb\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65cb\u8f6c\u64cd\u4f5c\u3002

"},{"location":"chapter_tree/avl_tree/#1_1","title":"1. \u00a0 \u53f3\u65cb","text":"

\u5982\u56fe 7-26 \u6240\u793a\uff0c\u8282\u70b9\u4e0b\u65b9\u4e3a\u5e73\u8861\u56e0\u5b50\u3002\u4ece\u5e95\u81f3\u9876\u770b\uff0c\u4e8c\u53c9\u6811\u4e2d\u9996\u4e2a\u5931\u8861\u8282\u70b9\u662f\u201c\u8282\u70b9 3\u201d\u3002\u6211\u4eec\u5173\u6ce8\u4ee5\u8be5\u5931\u8861\u8282\u70b9\u4e3a\u6839\u8282\u70b9\u7684\u5b50\u6811\uff0c\u5c06\u8be5\u8282\u70b9\u8bb0\u4e3a node \uff0c\u5176\u5de6\u5b50\u8282\u70b9\u8bb0\u4e3a child \uff0c\u6267\u884c\u201c\u53f3\u65cb\u201d\u64cd\u4f5c\u3002\u5b8c\u6210\u53f3\u65cb\u540e\uff0c\u5b50\u6811\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>

\u56fe 7-26 \u00a0 \u53f3\u65cb\u64cd\u4f5c\u6b65\u9aa4

\u5982\u56fe 7-27 \u6240\u793a\uff0c\u5f53\u8282\u70b9 child \u6709\u53f3\u5b50\u8282\u70b9\uff08\u8bb0\u4e3a grandChild \uff09\u65f6\uff0c\u9700\u8981\u5728\u53f3\u65cb\u4e2d\u6dfb\u52a0\u4e00\u6b65\uff1a\u5c06 grandChild \u4f5c\u4e3a node \u7684\u5de6\u5b50\u8282\u70b9\u3002

\u56fe 7-27 \u00a0 \u6709 grandChild \u7684\u53f3\u65cb\u64cd\u4f5c

\u201c\u5411\u53f3\u65cb\u8f6c\u201d\u662f\u4e00\u79cd\u5f62\u8c61\u5316\u7684\u8bf4\u6cd5\uff0c\u5b9e\u9645\u4e0a\u9700\u8981\u901a\u8fc7\u4fee\u6539\u8282\u70b9\u6307\u9488\u6765\u5b9e\u73b0\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
avl_tree.rs
/* \u53f3\u65cb\u64cd\u4f5c */\nfn right_rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\nmatch node {\nSome(node) => {\nlet child = node.borrow().left.clone().unwrap();\nlet grand_child = child.borrow().right.clone();\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.borrow_mut().right = Some(node.clone());\nnode.borrow_mut().left = grand_child;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nSelf::update_height(Some(node));\nSelf::update_height(Some(child.clone()));\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nSome(child)\n}\nNone => None,\n}\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.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
"},{"location":"chapter_tree/avl_tree/#2_1","title":"2. \u00a0 \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\u56fe 7-28 \u6240\u793a\u7684\u201c\u5de6\u65cb\u201d\u64cd\u4f5c\u3002

\u56fe 7-28 \u00a0 \u5de6\u65cb\u64cd\u4f5c

\u540c\u7406\uff0c\u5982\u56fe 7-29 \u6240\u793a\uff0c\u5f53\u8282\u70b9 child \u6709\u5de6\u5b50\u8282\u70b9\uff08\u8bb0\u4e3a grandChild \uff09\u65f6\uff0c\u9700\u8981\u5728\u5de6\u65cb\u4e2d\u6dfb\u52a0\u4e00\u6b65\uff1a\u5c06 grandChild \u4f5c\u4e3a node \u7684\u53f3\u5b50\u8282\u70b9\u3002

\u56fe 7-29 \u00a0 \u6709 grandChild \u7684\u5de6\u65cb\u64cd\u4f5c

\u53ef\u4ee5\u89c2\u5bdf\u5230\uff0c\u53f3\u65cb\u548c\u5de6\u65cb\u64cd\u4f5c\u5728\u903b\u8f91\u4e0a\u662f\u955c\u50cf\u5bf9\u79f0\u7684\uff0c\u5b83\u4eec\u5206\u522b\u89e3\u51b3\u7684\u4e24\u79cd\u5931\u8861\u60c5\u51b5\u4e5f\u662f\u5bf9\u79f0\u7684\u3002\u57fa\u4e8e\u5bf9\u79f0\u6027\uff0c\u6211\u4eec\u53ea\u9700\u5c06\u53f3\u65cb\u7684\u5b9e\u73b0\u4ee3\u7801\u4e2d\u7684\u6240\u6709\u7684 left \u66ff\u6362\u4e3a right \uff0c\u5c06\u6240\u6709\u7684 right \u66ff\u6362\u4e3a left \uff0c\u5373\u53ef\u5f97\u5230\u5de6\u65cb\u7684\u5b9e\u73b0\u4ee3\u7801\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
avl_tree.rs
/* \u5de6\u65cb\u64cd\u4f5c */\nfn left_rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\nmatch node {\nSome(node) => {\nlet child = node.borrow().right.clone().unwrap();\nlet grand_child = child.borrow().left.clone();\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.borrow_mut().left = Some(node.clone());\nnode.borrow_mut().right = grand_child;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nSelf::update_height(Some(node));\nSelf::update_height(Some(child.clone()));\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nSome(child)\n}\nNone => None,\n}\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.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
"},{"location":"chapter_tree/avl_tree/#3","title":"3. \u00a0 \u5148\u5de6\u65cb\u540e\u53f3\u65cb","text":"

\u5bf9\u4e8e\u56fe 7-30 \u4e2d\u7684\u5931\u8861\u8282\u70b9 3 \uff0c\u4ec5\u4f7f\u7528\u5de6\u65cb\u6216\u53f3\u65cb\u90fd\u65e0\u6cd5\u4f7f\u5b50\u6811\u6062\u590d\u5e73\u8861\u3002\u6b64\u65f6\u9700\u8981\u5148\u5bf9 child \u6267\u884c\u201c\u5de6\u65cb\u201d\uff0c\u518d\u5bf9 node \u6267\u884c\u201c\u53f3\u65cb\u201d\u3002

\u56fe 7-30 \u00a0 \u5148\u5de6\u65cb\u540e\u53f3\u65cb

"},{"location":"chapter_tree/avl_tree/#4","title":"4. \u00a0 \u5148\u53f3\u65cb\u540e\u5de6\u65cb","text":"

\u5982\u56fe 7-31 \u6240\u793a\uff0c\u5bf9\u4e8e\u4e0a\u8ff0\u5931\u8861\u4e8c\u53c9\u6811\u7684\u955c\u50cf\u60c5\u51b5\uff0c\u9700\u8981\u5148\u5bf9 child \u6267\u884c\u201c\u53f3\u65cb\u201d\uff0c\u7136\u540e\u5bf9 node \u6267\u884c\u201c\u5de6\u65cb\u201d\u3002

\u56fe 7-31 \u00a0 \u5148\u53f3\u65cb\u540e\u5de6\u65cb

"},{"location":"chapter_tree/avl_tree/#5","title":"5. \u00a0 \u65cb\u8f6c\u7684\u9009\u62e9","text":"

\u56fe 7-32 \u5c55\u793a\u7684\u56db\u79cd\u5931\u8861\u60c5\u51b5\u4e0e\u4e0a\u8ff0\u6848\u4f8b\u9010\u4e2a\u5bf9\u5e94\uff0c\u5206\u522b\u9700\u8981\u91c7\u7528\u53f3\u65cb\u3001\u5de6\u65cb\u3001\u5148\u53f3\u540e\u5de6\u3001\u5148\u5de6\u540e\u53f3\u7684\u65cb\u8f6c\u64cd\u4f5c\u3002

\u56fe 7-32 \u00a0 AVL \u6811\u7684\u56db\u79cd\u65cb\u8f6c\u60c5\u51b5

\u5982\u4e0b\u8868\u6240\u793a\uff0c\u6211\u4eec\u901a\u8fc7\u5224\u65ad\u5931\u8861\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u4ee5\u53ca\u8f83\u9ad8\u4e00\u4fa7\u5b50\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u7684\u6b63\u8d1f\u53f7\uff0c\u6765\u786e\u5b9a\u5931\u8861\u8282\u70b9\u5c5e\u4e8e\u56fe 7-32 \u4e2d\u7684\u54ea\u79cd\u60c5\u51b5\u3002

\u8868 7-3 \u00a0 \u56db\u79cd\u65cb\u8f6c\u60c5\u51b5\u7684\u9009\u62e9\u6761\u4ef6

\u5931\u8861\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50 \u5b50\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50 \u5e94\u91c7\u7528\u7684\u65cb\u8f6c\u65b9\u6cd5 \\(> 1\\) \uff08\u5373\u5de6\u504f\u6811\uff09 \\(\\geq 0\\) \u53f3\u65cb \\(> 1\\) \uff08\u5373\u5de6\u504f\u6811\uff09 \\(<0\\) \u5148\u5de6\u65cb\u540e\u53f3\u65cb \\(< -1\\) \uff08\u5373\u53f3\u504f\u6811\uff09 \\(\\leq 0\\) \u5de6\u65cb \\(< -1\\) \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

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node\n
avl_tree.cpp
/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode *rotate(TreeNode *node) {\n// \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\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\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\n
avl_tree.java
/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode rotate(TreeNode node) {\n// \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\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\u987b\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\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\n
avl_tree.go
/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfunc (t *aVLTree) rotate(node *TreeNode) *TreeNode {\n// \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n// Go \u63a8\u8350\u77ed\u53d8\u91cf\uff0c\u8fd9\u91cc bf \u6307\u4ee3 t.balanceFactor\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\u987b\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\u987b\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\u987b\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\u987b\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\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\n
avl_tree.rs
/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfn rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n// \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\nlet balance_factor = Self::balance_factor(node.clone());\n// \u5de6\u504f\u6811\nif balance_factor > 1 {\nlet node = node.unwrap();\nif Self::balance_factor(node.borrow().left.clone()) >= 0 {\n// \u53f3\u65cb\nSelf::right_rotate(Some(node))\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nlet left = node.borrow().left.clone();\nnode.borrow_mut().left = Self::left_rotate(left);\nSelf::right_rotate(Some(node))\n}\n}\n// \u53f3\u504f\u6811\nelse if balance_factor < -1 {\nlet node = node.unwrap();\nif Self::balance_factor(node.borrow().right.clone()) <= 0 {\n// \u5de6\u65cb\nSelf::left_rotate(Some(node))\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nlet right = node.borrow().right.clone();\nnode.borrow_mut().right = Self::right_rotate(right);\nSelf::left_rotate(Some(node))\n}\n} else {\n// \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nnode\n}\n}\n
avl_tree.c
/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode *rotate(TreeNode *node) {\n// \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\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\u987b\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\u987b\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/#1_2","title":"1. \u00a0 \u63d2\u5165\u8282\u70b9","text":"

AVL \u6811\u7684\u8282\u70b9\u63d2\u5165\u64cd\u4f5c\u4e0e\u4e8c\u53c9\u641c\u7d22\u6811\u5728\u4e3b\u4f53\u4e0a\u7c7b\u4f3c\u3002\u552f\u4e00\u7684\u533a\u522b\u5728\u4e8e\uff0c\u5728 AVL \u6811\u4e2d\u63d2\u5165\u8282\u70b9\u540e\uff0c\u4ece\u8be5\u8282\u70b9\u5230\u6839\u8282\u70b9\u7684\u8def\u5f84\u4e0a\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e00\u7cfb\u5217\u5931\u8861\u8282\u70b9\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u4ece\u8fd9\u4e2a\u8282\u70b9\u5f00\u59cb\uff0c\u81ea\u5e95\u5411\u4e0a\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u6240\u6709\u5931\u8861\u8282\u70b9\u6062\u590d\u5e73\u8861\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig avl_tree.py
def insert(self, val):\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.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.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.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.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\u51fd\u6570\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.(int) {\nnode.Left = t.insertHelper(node.Left, val)\n} else if val > node.Val.(int) {\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.swift
/* \u63d2\u5165\u8282\u70b9 */\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.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.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
avl_tree.rs
/* \u63d2\u5165\u8282\u70b9 */\nfn insert(&mut self, val: i32) {\nself.root = Self::insert_helper(self.root.clone(), val);\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfn insert_helper(node: OptionTreeNodeRc, val: i32) -> OptionTreeNodeRc {\nmatch node {\nSome(mut node) => {\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nmatch {\nlet node_val = node.borrow().val;\nnode_val\n}\n.cmp(&val)\n{\nOrdering::Greater => {\nlet left = node.borrow().left.clone();\nnode.borrow_mut().left = Self::insert_helper(left, val);\n}\nOrdering::Less => {\nlet right = node.borrow().right.clone();\nnode.borrow_mut().right = Self::insert_helper(right, val);\n}\nOrdering::Equal => {\nreturn Some(node); // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n}\n}\nSelf::update_height(Some(node.clone())); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = Self::rotate(Some(node)).unwrap();\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nSome(node)\n}\nNone => Some(TreeNode::new(val)),\n}\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\u51fd\u6570\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.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
"},{"location":"chapter_tree/avl_tree/#2_2","title":"2. \u00a0 \u5220\u9664\u8282\u70b9","text":"

\u7c7b\u4f3c\u5730\uff0c\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u5220\u9664\u8282\u70b9\u65b9\u6cd5\u7684\u57fa\u7840\u4e0a\uff0c\u9700\u8981\u4ece\u5e95\u81f3\u9876\u5730\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u6240\u6709\u5931\u8861\u8282\u70b9\u6062\u590d\u5e73\u8861\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig avl_tree.py
def remove(self, val: int):\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.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.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.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 ?? 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.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\u51fd\u6570\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.(int) {\nnode.Left = t.removeHelper(node.Left, val)\n} else if val > node.Val.(int) {\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.(int))\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.swift
/* \u5220\u9664\u8282\u70b9 */\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.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.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
avl_tree.rs
/* \u5220\u9664\u8282\u70b9 */\nfn remove(&self, val: i32) {\nSelf::remove_helper(self.root.clone(), val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfn remove_helper(node: OptionTreeNodeRc, val: i32) -> OptionTreeNodeRc {\nmatch node {\nSome(mut node) => {\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif val < node.borrow().val {\nlet left = node.borrow().left.clone();\nnode.borrow_mut().left = Self::remove_helper(left, val);\n} else if val > node.borrow().val {\nlet right = node.borrow().right.clone();\nnode.borrow_mut().right = Self::remove_helper(right, val);\n} else if node.borrow().left.is_none() || node.borrow().right.is_none() {\nlet child = if node.borrow().left.is_some() {\nnode.borrow().left.clone()\n} else {\nnode.borrow().right.clone()\n};\nmatch child {\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nNone => {\nreturn None;\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nSome(child) => node = child,\n}\n} else {\n// \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\nlet mut temp = node.borrow().right.clone().unwrap();\nloop {\nlet temp_left = temp.borrow().left.clone();\nif temp_left.is_none() {\nbreak;\n}\ntemp = temp_left.unwrap();\n}\nlet right = node.borrow().right.clone();\nnode.borrow_mut().right = Self::remove_helper(right, temp.borrow().val);\nnode.borrow_mut().val = temp.borrow().val;\n}\nSelf::update_height(Some(node.clone())); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = Self::rotate(Some(node)).unwrap();\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nSome(node)\n}\nNone => None,\n}\n}\n
avl_tree.c
/* \u5220\u9664\u8282\u70b9 */\n// \u7531\u4e8e\u5f15\u5165\u4e86 stdio.h \uff0c\u6b64\u5904\u65e0\u6cd5\u4f7f\u7528 remove \u5173\u952e\u8bcd\nvoid removeItem(aVLTree *tree, int val) {\nTreeNode *root = removeHelper(tree->root, val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\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.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
"},{"location":"chapter_tree/avl_tree/#3_1","title":"3. \u00a0 \u67e5\u627e\u8282\u70b9","text":"

AVL \u6811\u7684\u8282\u70b9\u67e5\u627e\u64cd\u4f5c\u4e0e\u4e8c\u53c9\u641c\u7d22\u6811\u4e00\u81f4\uff0c\u5728\u6b64\u4e0d\u518d\u8d58\u8ff0\u3002

"},{"location":"chapter_tree/avl_tree/#754-avl","title":"7.5.4 \u00a0 AVL \u6811\u5178\u578b\u5e94\u7528","text":"
  • \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\u3002
  • \u7528\u4e8e\u6784\u5efa\u6570\u636e\u5e93\u4e2d\u7684\u7d22\u5f15\u7cfb\u7edf\u3002
  • \u7ea2\u9ed1\u6811\u5728\u8bb8\u591a\u5e94\u7528\u4e2d\u6bd4 AVL \u6811\u66f4\u53d7\u6b22\u8fce\u3002\u8fd9\u662f\u56e0\u4e3a\u7ea2\u9ed1\u6811\u7684\u5e73\u8861\u6761\u4ef6\u76f8\u5bf9\u5bbd\u677e\uff0c\u5728\u7ea2\u9ed1\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\u6240\u9700\u7684\u65cb\u8f6c\u64cd\u4f5c\u76f8\u5bf9\u8f83\u5c11\uff0c\u5176\u8282\u70b9\u589e\u5220\u64cd\u4f5c\u7684\u5e73\u5747\u6548\u7387\u66f4\u9ad8\u3002
"},{"location":"chapter_tree/binary_search_tree/","title":"7.4 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811","text":"

\u5982\u56fe 7-16 \u6240\u793a\uff0c\u300c\u4e8c\u53c9\u641c\u7d22\u6811 binary search tree\u300d\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\u3002

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

\u56fe 7-16 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811

"},{"location":"chapter_tree/binary_search_tree/#741","title":"7.4.1 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u64cd\u4f5c","text":"

\u6211\u4eec\u5c06\u4e8c\u53c9\u641c\u7d22\u6811\u5c01\u88c5\u4e3a\u4e00\u4e2a\u7c7b ArrayBinaryTree \uff0c\u5e76\u58f0\u660e\u4e00\u4e2a\u6210\u5458\u53d8\u91cf root \uff0c\u6307\u5411\u6811\u7684\u6839\u8282\u70b9\u3002

"},{"location":"chapter_tree/binary_search_tree/#1","title":"1. \u00a0 \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\u5982\u56fe 7-17 \u6240\u793a\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u8282\u70b9 cur \uff0c\u4ece\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9 root \u51fa\u53d1\uff0c\u5faa\u73af\u6bd4\u8f83\u8282\u70b9\u503c cur.val \u548c num \u4e4b\u95f4\u7684\u5927\u5c0f\u5173\u7cfb\u3002

  • \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 \u3002
  • \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 \u3002
  • \u82e5 cur.val = num \uff0c\u8bf4\u660e\u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\u5e76\u8fd4\u56de\u8be5\u8282\u70b9\u3002
<1><2><3><4>

\u56fe 7-17 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u67e5\u627e\u8282\u70b9\u793a\u4f8b

\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u67e5\u627e\u64cd\u4f5c\u4e0e\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u4e00\u81f4\uff0c\u90fd\u662f\u6bcf\u8f6e\u6392\u9664\u4e00\u534a\u60c5\u51b5\u3002\u5faa\u73af\u6b21\u6570\u6700\u591a\u4e3a\u4e8c\u53c9\u6811\u7684\u9ad8\u5ea6\uff0c\u5f53\u4e8c\u53c9\u6811\u5e73\u8861\u65f6\uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_tree.py
def search(self, num: int) -> TreeNode | None:\n\"\"\"\u67e5\u627e\u8282\u70b9\"\"\"\ncur = 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.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.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.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.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.(int) < num {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nnode = node.Right\n} else if node.Val.(int) > 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.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.js
/* \u67e5\u627e\u8282\u70b9 */\nsearch(num) {\nlet cur = this.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 */\nsearch(num: number): TreeNode | null {\nlet cur = this.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.dart
/* \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.rs
/* \u67e5\u627e\u8282\u70b9 */\npub fn search(&self, num: i32) -> Option<TreeNodeRc> {\nlet mut cur = self.root.clone();\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile let Some(node) = cur.clone() {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif node.borrow().val < num {\ncur = node.borrow().right.clone();\n}\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse if node.borrow().val > num {\ncur = node.borrow().left.clone();\n}\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse {\nbreak;\n}\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\ncur\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.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
"},{"location":"chapter_tree/binary_search_tree/#2","title":"2. \u00a0 \u63d2\u5165\u8282\u70b9","text":"

\u7ed9\u5b9a\u4e00\u4e2a\u5f85\u63d2\u5165\u5143\u7d20 num \uff0c\u4e3a\u4e86\u4fdd\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u201c\u5de6\u5b50\u6811 < \u6839\u8282\u70b9 < \u53f3\u5b50\u6811\u201d\u7684\u6027\u8d28\uff0c\u63d2\u5165\u64cd\u4f5c\u6d41\u7a0b\u5982\u56fe 7-18 \u6240\u793a\u3002

  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{None}\\) \uff09\u65f6\u8df3\u51fa\u5faa\u73af\u3002
  2. \u5728\u8be5\u4f4d\u7f6e\u63d2\u5165\u8282\u70b9\uff1a\u521d\u59cb\u5316\u8282\u70b9 num \uff0c\u5c06\u8be5\u8282\u70b9\u7f6e\u4e8e \\(\\text{None}\\) \u7684\u4f4d\u7f6e\u3002

\u56fe 7-18 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u63d2\u5165\u8282\u70b9

\u5728\u4ee3\u7801\u5b9e\u73b0\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u4e24\u70b9\u3002

  • \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
  • \u4e3a\u4e86\u5b9e\u73b0\u63d2\u5165\u8282\u70b9\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u8282\u70b9 pre \u4fdd\u5b58\u4e0a\u4e00\u8f6e\u5faa\u73af\u7684\u8282\u70b9\u3002\u8fd9\u6837\u5728\u904d\u5386\u81f3 \\(\\text{None}\\) \u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u5230\u5176\u7236\u8282\u70b9\uff0c\u4ece\u800c\u5b8c\u6210\u8282\u70b9\u63d2\u5165\u64cd\u4f5c\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_tree.py
def insert(self, num: int):\n\"\"\"\u63d2\u5165\u8282\u70b9\"\"\"\n# \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\nif self._root is None:\nself._root = TreeNode(num)\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.cpp
/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\nif (root == nullptr) {\nroot = new TreeNode(num);\nreturn;\n}\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.java
/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\nif (root == null) {\nroot = new TreeNode(num);\nreturn;\n}\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.cs
/* \u63d2\u5165\u8282\u70b9 */\nvoid Insert(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\nif (root == null) {\nroot = new TreeNode(num);\nreturn;\n}\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(num);\nif (pre != null) {\nif (pre.val < num)\npre.right = node;\nelse\npre.left = node;\n}\n}\n
binary_search_tree.go
/* \u63d2\u5165\u8282\u70b9 */\nfunc (bst *binarySearchTree) insert(num int) {\ncur := bst.root\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\nif cur == nil {\nbst.root = NewTreeNode(num)\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.(int) < num {\ncur = cur.Right\n} else {\ncur = cur.Left\n}\n}\n// \u63d2\u5165\u8282\u70b9\nnode := NewTreeNode(num)\nif pre.Val.(int) < num {\npre.Right = node\n} else {\npre.Left = node\n}\n}\n
binary_search_tree.swift
/* \u63d2\u5165\u8282\u70b9 */\nfunc insert(num: Int) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\nif root == nil {\nroot = TreeNode(x: num)\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.js
/* \u63d2\u5165\u8282\u70b9 */\ninsert(num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\nif (this.root === null) {\nthis.root = new TreeNode(num);\nreturn;\n}\nlet cur = this.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\nconst 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 */\ninsert(num: number): void {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\nif (this.root === null) {\nthis.root = new TreeNode(num);\nreturn;\n}\nlet cur: TreeNode | null = this.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\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\nconst node = new TreeNode(num);\nif (pre!.val < num) pre!.right = node;\nelse pre!.left = node;\n}\n
binary_search_tree.dart
/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\nif (_root == null) {\n_root = TreeNode(num);\nreturn;\n}\nTreeNode? cur = _root;\nTreeNode? 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) 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\nelse\ncur = cur.left;\n}\n// \u63d2\u5165\u8282\u70b9\nTreeNode? node = TreeNode(num);\nif (pre!.val < num)\npre.right = node;\nelse\npre.left = node;\n}\n
binary_search_tree.rs
/* \u63d2\u5165\u8282\u70b9 */\npub fn insert(&mut self, num: i32) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\nif self.root.is_none() {\nself.root = Some(TreeNode::new(num));\nreturn;\n}\nlet mut cur = self.root.clone();\nlet mut pre = None;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile let Some(node) = cur.clone() {\n// \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif node.borrow().val == num {\nreturn;\n}\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\npre = cur.clone();\nif node.borrow().val < num {\ncur = node.borrow().right.clone();\n}\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse {\ncur = node.borrow().left.clone();\n}\n}\n// \u63d2\u5165\u8282\u70b9\nlet node = TreeNode::new(num);\nlet pre = pre.unwrap();\nif pre.borrow().val < num {\npre.borrow_mut().right = Some(Rc::clone(&node));\n} else {\npre.borrow_mut().left = Some(Rc::clone(&node));\n}\n}\n
binary_search_tree.c
/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(binarySearchTree *bst, int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\nif (bst->root == NULL) {\nbst->root = newTreeNode(num);\nreturn;\n}\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.zig
// \u63d2\u5165\u8282\u70b9\nfn insert(self: *Self, num: T) !void {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\nif (self.root == null) {\nself.root = try self.mem_allocator.create(inc.TreeNode(T));\nreturn;\n}\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

\u4e0e\u67e5\u627e\u8282\u70b9\u76f8\u540c\uff0c\u63d2\u5165\u8282\u70b9\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\u3002

"},{"location":"chapter_tree/binary_search_tree/#3","title":"3. \u00a0 \u5220\u9664\u8282\u70b9","text":"

\u5148\u5728\u4e8c\u53c9\u6811\u4e2d\u67e5\u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u518d\u5c06\u5176\u4ece\u4e8c\u53c9\u6811\u4e2d\u5220\u9664\u3002

\u4e0e\u63d2\u5165\u8282\u70b9\u7c7b\u4f3c\uff0c\u6211\u4eec\u9700\u8981\u4fdd\u8bc1\u5728\u5220\u9664\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u201c\u5de6\u5b50\u6811 < \u6839\u8282\u70b9 < \u53f3\u5b50\u6811\u201d\u7684\u6027\u8d28\u4ecd\u7136\u6ee1\u8db3\u3002

\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u76ee\u6807\u8282\u70b9\u7684\u5b50\u8282\u70b9\u6570\u91cf\uff0c\u5171\u5206\u4e3a 0\u30011 \u548c 2 \u8fd9\u4e09\u79cd\u60c5\u51b5\uff0c\u6267\u884c\u5bf9\u5e94\u7684\u5220\u9664\u8282\u70b9\u64cd\u4f5c\u3002

\u5982\u56fe 7-19 \u6240\u793a\uff0c\u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5ea6\u4e3a \\(0\\) \u65f6\uff0c\u8868\u793a\u8be5\u8282\u70b9\u662f\u53f6\u8282\u70b9\uff0c\u53ef\u4ee5\u76f4\u63a5\u5220\u9664\u3002

\u56fe 7-19 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 0 \uff09

\u5982\u56fe 7-20 \u6240\u793a\uff0c\u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5ea6\u4e3a \\(1\\) \u65f6\uff0c\u5c06\u5f85\u5220\u9664\u8282\u70b9\u66ff\u6362\u4e3a\u5176\u5b50\u8282\u70b9\u5373\u53ef\u3002

\u56fe 7-20 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 1 \uff09

\u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5ea6\u4e3a \\(2\\) \u65f6\uff0c\u6211\u4eec\u65e0\u6cd5\u76f4\u63a5\u5220\u9664\u5b83\uff0c\u800c\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u8282\u70b9\u66ff\u6362\u8be5\u8282\u70b9\u3002\u7531\u4e8e\u8981\u4fdd\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u201c\u5de6 \\(<\\) \u6839 \\(<\\) \u53f3\u201d\u7684\u6027\u8d28\uff0c\u56e0\u6b64\u8fd9\u4e2a\u8282\u70b9\u53ef\u4ee5\u662f\u53f3\u5b50\u6811\u7684\u6700\u5c0f\u8282\u70b9\u6216\u5de6\u5b50\u6811\u7684\u6700\u5927\u8282\u70b9\u3002

\u5047\u8bbe\u6211\u4eec\u9009\u62e9\u53f3\u5b50\u6811\u7684\u6700\u5c0f\u8282\u70b9\uff08\u5373\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\uff09\uff0c\u5219\u5220\u9664\u64cd\u4f5c\u6d41\u7a0b\u5982\u56fe 7-21 \u6240\u793a\u3002

  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 \u3002
  2. \u5c06 tmp \u7684\u503c\u8986\u76d6\u5f85\u5220\u9664\u8282\u70b9\u7684\u503c\uff0c\u5e76\u5728\u6811\u4e2d\u9012\u5f52\u5220\u9664\u8282\u70b9 tmp \u3002
<1><2><3><4>

\u56fe 7-21 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 2 \uff09

\u5220\u9664\u8282\u70b9\u64cd\u4f5c\u540c\u6837\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\uff0c\u5176\u4e2d\u67e5\u627e\u5f85\u5220\u9664\u8282\u70b9\u9700\u8981 \\(O(\\log n)\\) \u65f6\u95f4\uff0c\u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u540e\u7ee7\u8282\u70b9\u9700\u8981 \\(O(\\log n)\\) \u65f6\u95f4\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_tree.py
def remove(self, num: int):\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 = child\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.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.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.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)\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 ?? 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.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.(int) < 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.(int))\n// \u7528 tmp \u8986\u76d6 cur\ncur.Val = tmp.Val\n}\n}\n
binary_search_tree.swift
/* \u5220\u9664\u8282\u70b9 */\nfunc remove(num: Int) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\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 = 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\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.js
/* \u5220\u9664\u8282\u70b9 */\nremove(num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (this.root === null) return;\nlet cur = this.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\nconst child = cur.left !== null ? cur.left : cur.right;\n// \u5220\u9664\u8282\u70b9 cur\nif (cur !== this.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\nthis.root = 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\nthis.remove(tmp.val);\n// \u7528 tmp \u8986\u76d6 cur\ncur.val = tmp.val;\n}\n}\n
binary_search_tree.ts
/* \u5220\u9664\u8282\u70b9 */\nremove(num: number): void {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (this.root === null) return;\nlet cur: TreeNode | null = this.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) 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\nconst child: TreeNode | null =\ncur.left !== null ? cur.left : cur.right;\n// \u5220\u9664\u8282\u70b9 cur\nif (cur !== this.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\nthis.root = 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: TreeNode | null = cur.right;\nwhile (tmp!.left !== null) {\ntmp = tmp!.left;\n}\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nthis.remove(tmp!.val);\n// \u7528 tmp \u8986\u76d6 cur\ncur.val = tmp!.val;\n}\n}\n
binary_search_tree.dart
/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (_root == null) return;\nTreeNode? cur = _root;\nTreeNode? 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) 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\nelse\ncur = cur.left;\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\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\nTreeNode? child = 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\n_root = child;\n}\n} else {\n// \u5b50\u8282\u70b9\u6570\u91cf = 2\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\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.rs
/* \u5220\u9664\u8282\u70b9 */\npub fn remove(&mut self, num: i32) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif self.root.is_none() { return; }\nlet mut cur = self.root.clone();\nlet mut pre = None;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile let Some(node) = cur.clone() {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif node.borrow().val == num {\nbreak;\n}\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\npre = cur.clone();\nif node.borrow().val < num {\ncur = node.borrow().right.clone();\n}\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse {\ncur = node.borrow().left.clone();\n}\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif cur.is_none() {\nreturn;\n}\nlet cur = cur.unwrap();\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif cur.borrow().left.is_none() || cur.borrow().right.is_none() {\n// \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u8282\u70b9\nlet child = cur.borrow().left.clone().or_else(|| cur.borrow().right.clone());\nlet pre = pre.unwrap();\nlet left = pre.borrow().left.clone().unwrap();\n// \u5220\u9664\u8282\u70b9 cur\nif !Rc::ptr_eq(&cur, self.root.as_ref().unwrap()) {\nif Rc::ptr_eq(&left, &cur) {\npre.borrow_mut().left = child;\n} else {\npre.borrow_mut().right = child;\n}\n} else {\n// \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nself.root = 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 mut tmp = cur.borrow().right.clone();\nwhile let Some(node) = tmp.clone() {\nif node.borrow().left.is_some() {\ntmp = node.borrow().left.clone();\n} else {\nbreak;\n}\n}\nlet tmpval = tmp.unwrap().borrow().val;\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nself.remove(tmpval);\n// \u7528 tmp \u8986\u76d6 cur\ncur.borrow_mut().val = tmpval;\n}\n}\n
binary_search_tree.c
/* \u5220\u9664\u8282\u70b9 */\n// \u7531\u4e8e\u5f15\u5165\u4e86 stdio.h \uff0c\u6b64\u5904\u65e0\u6cd5\u4f7f\u7528 remove \u5173\u952e\u8bcd\nvoid removeItem(binarySearchTree *bst, int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\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\nremoveItem(bst, tmp->val);\n// \u7528 tmp \u8986\u76d6 cur\ncur->val = tmpVal;\n}\n}\n
binary_search_tree.zig
// \u5220\u9664\u8282\u70b9\nfn remove(self: *Self, num: T) void {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\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 tmp_val = tmp.?.val;\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nself.remove(tmp.?.val);\n// \u7528 tmp \u8986\u76d6 cur\ncur.?.val = tmp_val;\n}\n}\n
"},{"location":"chapter_tree/binary_search_tree/#4","title":"4. \u00a0 \u4e2d\u5e8f\u904d\u5386\u6709\u5e8f","text":"

\u5982\u56fe 7-22 \u6240\u793a\uff0c\u4e8c\u53c9\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u9075\u5faa\u201c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u201d\u7684\u904d\u5386\u987a\u5e8f\uff0c\u800c\u4e8c\u53c9\u641c\u7d22\u6811\u6ee1\u8db3\u201c\u5de6\u5b50\u8282\u70b9 \\(<\\) \u6839\u8282\u70b9 \\(<\\) \u53f3\u5b50\u8282\u70b9\u201d\u7684\u5927\u5c0f\u5173\u7cfb\u3002

\u8fd9\u610f\u5473\u7740\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u8fdb\u884c\u4e2d\u5e8f\u904d\u5386\u65f6\uff0c\u603b\u662f\u4f1a\u4f18\u5148\u904d\u5386\u4e0b\u4e00\u4e2a\u6700\u5c0f\u8282\u70b9\uff0c\u4ece\u800c\u5f97\u51fa\u4e00\u4e2a\u91cd\u8981\u6027\u8d28\uff1a\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u662f\u5347\u5e8f\u7684\u3002

\u5229\u7528\u4e2d\u5e8f\u904d\u5386\u5347\u5e8f\u7684\u6027\u8d28\uff0c\u6211\u4eec\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u83b7\u53d6\u6709\u5e8f\u6570\u636e\u4ec5\u9700 \\(O(n)\\) \u65f6\u95f4\uff0c\u65e0\u987b\u8fdb\u884c\u989d\u5916\u7684\u6392\u5e8f\u64cd\u4f5c\uff0c\u975e\u5e38\u9ad8\u6548\u3002

\u56fe 7-22 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217

"},{"location":"chapter_tree/binary_search_tree/#742","title":"7.4.2 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6548\u7387","text":"

\u7ed9\u5b9a\u4e00\u7ec4\u6570\u636e\uff0c\u6211\u4eec\u8003\u8651\u4f7f\u7528\u6570\u7ec4\u6216\u4e8c\u53c9\u641c\u7d22\u6811\u5b58\u50a8\u3002\u89c2\u5bdf\u8868 7-2 \uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u5404\u9879\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662f\u5bf9\u6570\u9636\uff0c\u5177\u6709\u7a33\u5b9a\u4e14\u9ad8\u6548\u7684\u6027\u80fd\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

\u8868 7-2 \u00a0 \u6570\u7ec4\u4e0e\u641c\u7d22\u6811\u7684\u6548\u7387\u5bf9\u6bd4

\u65e0\u5e8f\u6570\u7ec4 \u4e8c\u53c9\u641c\u7d22\u6811 \u67e5\u627e\u5143\u7d20 \\(O(n)\\) \\(O(\\log n)\\) \u63d2\u5165\u5143\u7d20 \\(O(1)\\) \\(O(\\log n)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(\\log n)\\)

\u5728\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u662f\u201c\u5e73\u8861\u201d\u7684\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728 \\(\\log n\\) \u8f6e\u5faa\u73af\u5185\u67e5\u627e\u4efb\u610f\u8282\u70b9\u3002

\u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u4e0d\u65ad\u5730\u63d2\u5165\u548c\u5220\u9664\u8282\u70b9\uff0c\u53ef\u80fd\u5bfc\u81f4\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u56fe 7-23 \u6240\u793a\u7684\u94fe\u8868\uff0c\u8fd9\u65f6\u5404\u79cd\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u4f1a\u9000\u5316\u4e3a \\(O(n)\\) \u3002

\u56fe 7-23 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\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\uff1a\u503c\u3001\u5de6\u5b50\u8282\u70b9\u5f15\u7528\u3001\u53f3\u5b50\u8282\u70b9\u5f15\u7528\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig
class TreeNode:\n\"\"\"\u4e8c\u53c9\u6811\u8282\u70b9\u7c7b\"\"\"\ndef __init__(self, val: int):\nself.val: int = val                # \u8282\u70b9\u503c\nself.left: TreeNode | None = None  # \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nself.right: TreeNode | None = None # \u53f3\u5b50\u8282\u70b9\u5f15\u7528\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
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nint val;         // \u8282\u70b9\u503c\nTreeNode left;   // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nTreeNode right;  // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\nTreeNode(int x) { val = x; }\n}\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nint val;          // \u8282\u70b9\u503c\nTreeNode? left;   // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nTreeNode? right;  // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\nTreeNode(int x) { val = x; }\n}\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype TreeNode struct {\nVal   int\nLeft  *TreeNode\nRight *TreeNode\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc NewTreeNode(v int) *TreeNode {\nreturn &TreeNode{\nLeft:  nil, // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nRight: nil, // \u53f3\u5b50\u8282\u70b9\u6307\u9488\nVal:   v,   // \u8282\u70b9\u503c\n}\n}\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nvar val: Int // \u8282\u70b9\u503c\nvar left: TreeNode? // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nvar right: TreeNode? // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\ninit(x: Int) {\nval = x\n}\n}\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nval; // \u8282\u70b9\u503c\nleft; // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nright; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\nconstructor(val, left, right) {\nthis.val = val === undefined ? 0 : val;\nthis.left = left === undefined ? null : left;\nthis.right = right === undefined ? null : right;\n}\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\u5f15\u7528\nthis.right = right === undefined ? null : right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\n}\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nint val;         // \u8282\u70b9\u503c\nTreeNode? left;  // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nTreeNode? right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\nTreeNode(this.val, [this.left, this.right]);\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n/* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\nval: i32,                               // \u8282\u70b9\u503c\nleft: Option<Rc<RefCell<TreeNode>>>,    // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nright: Option<Rc<RefCell<TreeNode>>>,   // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\nimpl TreeNode {\n/* \u6784\u9020\u65b9\u6cd5 */\nfn new(val: i32) -> Rc<RefCell<Self>> {\nRc::new(RefCell::new(Self {\nval,\nleft: None,\nright: None\n}))\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
\n

\u6bcf\u4e2a\u8282\u70b9\u90fd\u6709\u4e24\u4e2a\u5f15\u7528\uff08\u6307\u9488\uff09\uff0c\u5206\u522b\u6307\u5411\u300c\u5de6\u5b50\u8282\u70b9 left-child node\u300d\u548c\u300c\u53f3\u5b50\u8282\u70b9 right-child node\u300d\uff0c\u8be5\u8282\u70b9\u88ab\u79f0\u4e3a\u8fd9\u4e24\u4e2a\u5b50\u8282\u70b9\u7684\u300c\u7236\u8282\u70b9 parent node\u300d\u3002\u5f53\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u65f6\uff0c\u6211\u4eec\u5c06\u8be5\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u53ca\u5176\u4ee5\u4e0b\u8282\u70b9\u5f62\u6210\u7684\u6811\u79f0\u4e3a\u8be5\u8282\u70b9\u7684\u300c\u5de6\u5b50\u6811 left subtree\u300d\uff0c\u540c\u7406\u53ef\u5f97\u300c\u53f3\u5b50\u6811 right subtree\u300d\u3002

\u5728\u4e8c\u53c9\u6811\u4e2d\uff0c\u9664\u53f6\u8282\u70b9\u5916\uff0c\u5176\u4ed6\u6240\u6709\u8282\u70b9\u90fd\u5305\u542b\u5b50\u8282\u70b9\u548c\u975e\u7a7a\u5b50\u6811\u3002\u5982\u56fe 7-1 \u6240\u793a\uff0c\u5982\u679c\u5c06\u201c\u8282\u70b9 2\u201d\u89c6\u4e3a\u7236\u8282\u70b9\uff0c\u5219\u5176\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\u5206\u522b\u662f\u201c\u8282\u70b9 4\u201d\u548c\u201c\u8282\u70b9 5\u201d\uff0c\u5de6\u5b50\u6811\u662f\u201c\u8282\u70b9 4 \u53ca\u5176\u4ee5\u4e0b\u8282\u70b9\u5f62\u6210\u7684\u6811\u201d\uff0c\u53f3\u5b50\u6811\u662f\u201c\u8282\u70b9 5 \u53ca\u5176\u4ee5\u4e0b\u8282\u70b9\u5f62\u6210\u7684\u6811\u201d\u3002

\u56fe 7-1 \u00a0 \u7236\u8282\u70b9\u3001\u5b50\u8282\u70b9\u3001\u5b50\u6811

"},{"location":"chapter_tree/binary_tree/#711","title":"7.1.1 \u00a0 \u4e8c\u53c9\u6811\u5e38\u89c1\u672f\u8bed","text":"

\u4e8c\u53c9\u6811\u7684\u5e38\u7528\u672f\u8bed\u5982\u56fe 7-2 \u6240\u793a\u3002

  • \u300c\u6839\u8282\u70b9 root node\u300d\uff1a\u4f4d\u4e8e\u4e8c\u53c9\u6811\u9876\u5c42\u7684\u8282\u70b9\uff0c\u6ca1\u6709\u7236\u8282\u70b9\u3002
  • \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{None}\\) \u3002
  • \u300c\u8fb9 edge\u300d\uff1a\u8fde\u63a5\u4e24\u4e2a\u8282\u70b9\u7684\u7ebf\u6bb5\uff0c\u5373\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\u3002
  • \u8282\u70b9\u6240\u5728\u7684\u300c\u5c42 level\u300d\uff1a\u4ece\u9876\u81f3\u5e95\u9012\u589e\uff0c\u6839\u8282\u70b9\u6240\u5728\u5c42\u4e3a 1 \u3002
  • \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\u53d6\u503c\u8303\u56f4\u662f 0\u30011\u30012 \u3002
  • \u4e8c\u53c9\u6811\u7684\u300c\u9ad8\u5ea6 height\u300d\uff1a\u4ece\u6839\u8282\u70b9\u5230\u6700\u8fdc\u53f6\u8282\u70b9\u6240\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\u3002
  • \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\u3002
  • \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\u3002

\u56fe 7-2 \u00a0 \u4e8c\u53c9\u6811\u7684\u5e38\u7528\u672f\u8bed

Tip

\u8bf7\u6ce8\u610f\uff0c\u6211\u4eec\u901a\u5e38\u5c06\u201c\u9ad8\u5ea6\u201d\u548c\u201c\u6df1\u5ea6\u201d\u5b9a\u4e49\u4e3a\u201c\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":""},{"location":"chapter_tree/binary_tree/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u4e8c\u53c9\u6811","text":"

\u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u9996\u5148\u521d\u59cb\u5316\u8282\u70b9\uff0c\u7136\u540e\u6784\u5efa\u5f15\u7528\uff08\u6307\u9488\uff09\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_tree.py
# \u521d\u59cb\u5316\u4e8c\u53c9\u6811\n# \u521d\u59cb\u5316\u8282\u70b9\nn1 = TreeNode(val=1)\nn2 = TreeNode(val=2)\nn3 = TreeNode(val=3)\nn4 = TreeNode(val=4)\nn5 = TreeNode(val=5)\n# \u6784\u5efa\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.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.cs
/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode n1 = new(1);\nTreeNode n2 = new(2);\nTreeNode n3 = new(3);\nTreeNode n4 = new(4);\nTreeNode n5 = new(5);\n// \u6784\u5efa\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.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.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.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
binary_tree.rs
// \u521d\u59cb\u5316\u8282\u70b9\nlet n1 = TreeNode::new(1);\nlet n2 = TreeNode::new(2);\nlet n3 = TreeNode::new(3);\nlet n4 = TreeNode::new(4);\nlet n5 = TreeNode::new(5);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1.borrow_mut().left = Some(n2.clone());\nn1.borrow_mut().right = Some(n3);\nn2.borrow_mut().left = Some(n4);\nn2.borrow_mut().right = Some(n5);\n
binary_tree.c
/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode *n1 = newTreeNode(1);\nTreeNode *n2 = newTreeNode(2);\nTreeNode *n3 = newTreeNode(3);\nTreeNode *n4 = newTreeNode(4);\nTreeNode *n5 = newTreeNode(5);\n// \u6784\u5efa\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
"},{"location":"chapter_tree/binary_tree/#2","title":"2. \u00a0 \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9","text":"

\u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u5728\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u6307\u9488\u6765\u5b9e\u73b0\u3002\u56fe 7-3 \u7ed9\u51fa\u4e86\u4e00\u4e2a\u793a\u4f8b\u3002

\u56fe 7-3 \u00a0 \u5728\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9

PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_tree.py
# \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\np = TreeNode(0)\n# \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = p\np.left = n2\n# \u5220\u9664\u8282\u70b9 P\nn1.left = n2\n
binary_tree.cpp
/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode* P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1->left = P;\nP->left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1->left = n2;\n
binary_tree.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.cs
/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode P = new(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
binary_tree.go
/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\np := NewTreeNode(0)\nn1.Left = p\np.Left = n2\n// \u5220\u9664\u8282\u70b9 P\nn1.Left = n2\n
binary_tree.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.js
/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nlet P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
binary_tree.ts
/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nconst P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
binary_tree.dart
/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
binary_tree.rs
let p = TreeNode::new(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.borrow_mut().left = Some(p.clone());\np.borrow_mut().left = Some(n2.clone());\n// \u5220\u9664\u8282\u70b9 p\nn1.borrow_mut().left = Some(n2);\n
binary_tree.c
/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode *P = newTreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1->left = P;\nP->left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1->left = n2;\n
binary_tree.zig
\n

Note

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u63d2\u5165\u8282\u70b9\u53ef\u80fd\u4f1a\u6539\u53d8\u4e8c\u53c9\u6811\u7684\u539f\u6709\u903b\u8f91\u7ed3\u6784\uff0c\u800c\u5220\u9664\u8282\u70b9\u901a\u5e38\u610f\u5473\u7740\u5220\u9664\u8be5\u8282\u70b9\u53ca\u5176\u6240\u6709\u5b50\u6811\u3002\u56e0\u6b64\uff0c\u5728\u4e8c\u53c9\u6811\u4e2d\uff0c\u63d2\u5165\u4e0e\u5220\u9664\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_1","title":"1. \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811","text":"

\u300c\u5b8c\u7f8e\u4e8c\u53c9\u6811 perfect binary tree\u300d\u6240\u6709\u5c42\u7684\u8282\u70b9\u90fd\u88ab\u5b8c\u5168\u586b\u6ee1\u3002\u5728\u5b8c\u7f8e\u4e8c\u53c9\u6811\u4e2d\uff0c\u53f6\u8282\u70b9\u7684\u5ea6\u4e3a \\(0\\) \uff0c\u5176\u4f59\u6240\u6709\u8282\u70b9\u7684\u5ea6\u90fd\u4e3a \\(2\\) \uff1b\u82e5\u6811\u9ad8\u5ea6\u4e3a \\(h\\) \uff0c\u5219\u8282\u70b9\u603b\u6570\u4e3a \\(2^{h+1} - 1\\) \uff0c\u5448\u73b0\u6807\u51c6\u7684\u6307\u6570\u7ea7\u5173\u7cfb\uff0c\u53cd\u6620\u4e86\u81ea\u7136\u754c\u4e2d\u5e38\u89c1\u7684\u7ec6\u80de\u5206\u88c2\u73b0\u8c61\u3002

Tip

\u8bf7\u6ce8\u610f\uff0c\u5728\u4e2d\u6587\u793e\u533a\u4e2d\uff0c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u5e38\u88ab\u79f0\u4e3a\u300c\u6ee1\u4e8c\u53c9\u6811\u300d\u3002

\u56fe 7-4 \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811

"},{"location":"chapter_tree/binary_tree/#2_1","title":"2. \u00a0 \u5b8c\u5168\u4e8c\u53c9\u6811","text":"

\u5982\u56fe 7-5 \u6240\u793a\uff0c\u300c\u5b8c\u5168\u4e8c\u53c9\u6811 complete binary tree\u300d\u53ea\u6709\u6700\u5e95\u5c42\u7684\u8282\u70b9\u672a\u88ab\u586b\u6ee1\uff0c\u4e14\u6700\u5e95\u5c42\u8282\u70b9\u5c3d\u91cf\u9760\u5de6\u586b\u5145\u3002

\u56fe 7-5 \u00a0 \u5b8c\u5168\u4e8c\u53c9\u6811

"},{"location":"chapter_tree/binary_tree/#3","title":"3. \u00a0 \u5b8c\u6ee1\u4e8c\u53c9\u6811","text":"

\u5982\u56fe 7-6 \u6240\u793a\uff0c\u300c\u5b8c\u6ee1\u4e8c\u53c9\u6811 full binary tree\u300d\u9664\u4e86\u53f6\u8282\u70b9\u4e4b\u5916\uff0c\u5176\u4f59\u6240\u6709\u8282\u70b9\u90fd\u6709\u4e24\u4e2a\u5b50\u8282\u70b9\u3002

\u56fe 7-6 \u00a0 \u5b8c\u6ee1\u4e8c\u53c9\u6811

"},{"location":"chapter_tree/binary_tree/#4","title":"4. \u00a0 \u5e73\u8861\u4e8c\u53c9\u6811","text":"

\u5982\u56fe 7-7 \u6240\u793a\uff0c\u300c\u5e73\u8861\u4e8c\u53c9\u6811 balanced binary tree\u300d\u4e2d\u4efb\u610f\u8282\u70b9\u7684\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u7684\u9ad8\u5ea6\u4e4b\u5dee\u7684\u7edd\u5bf9\u503c\u4e0d\u8d85\u8fc7 1 \u3002

\u56fe 7-7 \u00a0 \u5e73\u8861\u4e8c\u53c9\u6811

"},{"location":"chapter_tree/binary_tree/#714","title":"7.1.4 \u00a0 \u4e8c\u53c9\u6811\u7684\u9000\u5316","text":"

\u56fe 7-8 \u5c55\u793a\u4e86\u4e8c\u53c9\u6811\u7684\u7406\u60f3\u4e0e\u9000\u5316\u72b6\u6001\u3002\u5f53\u4e8c\u53c9\u6811\u7684\u6bcf\u5c42\u8282\u70b9\u90fd\u88ab\u586b\u6ee1\u65f6\uff0c\u8fbe\u5230\u201c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u201d\uff1b\u800c\u5f53\u6240\u6709\u8282\u70b9\u90fd\u504f\u5411\u4e00\u4fa7\u65f6\uff0c\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u201c\u94fe\u8868\u201d\u3002

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

\u56fe 7-8 \u00a0 \u4e8c\u53c9\u6811\u7684\u6700\u4f73\u4e0e\u6700\u5dee\u7ed3\u6784

\u5982\u8868 7-1 \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

\u8868 7-1 \u00a0 \u4e8c\u53c9\u6811\u7684\u6700\u4f73\u4e0e\u6700\u5dee\u60c5\u51b5

\u5b8c\u7f8e\u4e8c\u53c9\u6811 \u94fe\u8868 \u7b2c \\(i\\) \u5c42\u7684\u8282\u70b9\u6570\u91cf \\(2^{i-1}\\) \\(1\\) \u9ad8\u5ea6 \\(h\\) \u6811\u7684\u53f6\u8282\u70b9\u6570\u91cf \\(2^h\\) \\(1\\) \u9ad8\u5ea6 \\(h\\) \u6811\u7684\u8282\u70b9\u603b\u6570 \\(2^{h+1} - 1\\) \\(h + 1\\) \u8282\u70b9\u603b\u6570 \\(n\\) \u6811\u7684\u9ad8\u5ea6 \\(\\log_2 (n+1) - 1\\) \\(n - 1\\)"},{"location":"chapter_tree/binary_tree_traversal/","title":"7.2 \u00a0 \u4e8c\u53c9\u6811\u904d\u5386","text":"

\u4ece\u7269\u7406\u7ed3\u6784\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u6811\u662f\u4e00\u79cd\u57fa\u4e8e\u94fe\u8868\u7684\u6570\u636e\u7ed3\u6784\uff0c\u56e0\u6b64\u5176\u904d\u5386\u65b9\u5f0f\u662f\u901a\u8fc7\u6307\u9488\u9010\u4e2a\u8bbf\u95ee\u8282\u70b9\u3002\u7136\u800c\uff0c\u6811\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u8fd9\u4f7f\u5f97\u904d\u5386\u6811\u6bd4\u904d\u5386\u94fe\u8868\u66f4\u52a0\u590d\u6742\uff0c\u9700\u8981\u501f\u52a9\u641c\u7d22\u7b97\u6cd5\u6765\u5b9e\u73b0\u3002

\u4e8c\u53c9\u6811\u5e38\u89c1\u7684\u904d\u5386\u65b9\u5f0f\u5305\u62ec\u5c42\u5e8f\u904d\u5386\u3001\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u548c\u540e\u5e8f\u904d\u5386\u7b49\u3002

"},{"location":"chapter_tree/binary_tree_traversal/#721","title":"7.2.1 \u00a0 \u5c42\u5e8f\u904d\u5386","text":"

\u5982\u56fe 7-9 \u6240\u793a\uff0c\u300c\u5c42\u5e8f\u904d\u5386 level-order traversal\u300d\u4ece\u9876\u90e8\u5230\u5e95\u90e8\u9010\u5c42\u904d\u5386\u4e8c\u53c9\u6811\uff0c\u5e76\u5728\u6bcf\u4e00\u5c42\u6309\u7167\u4ece\u5de6\u5230\u53f3\u7684\u987a\u5e8f\u8bbf\u95ee\u8282\u70b9\u3002

\u5c42\u5e8f\u904d\u5386\u672c\u8d28\u4e0a\u5c5e\u4e8e\u300c\u5e7f\u5ea6\u4f18\u5148\u904d\u5386 breadth-first traversal\u300d\uff0c\u5b83\u4f53\u73b0\u4e86\u4e00\u79cd\u201c\u4e00\u5708\u4e00\u5708\u5411\u5916\u6269\u5c55\u201d\u7684\u9010\u5c42\u904d\u5386\u65b9\u5f0f\u3002

\u56fe 7-9 \u00a0 \u4e8c\u53c9\u6811\u7684\u5c42\u5e8f\u904d\u5386

"},{"location":"chapter_tree/binary_tree_traversal/#1","title":"1. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u901a\u5e38\u501f\u52a9\u201c\u961f\u5217\u201d\u6765\u5b9e\u73b0\u3002\u961f\u5217\u9075\u5faa\u201c\u5148\u8fdb\u5148\u51fa\u201d\u7684\u89c4\u5219\uff0c\u800c\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u5219\u9075\u5faa\u201c\u9010\u5c42\u63a8\u8fdb\u201d\u7684\u89c4\u5219\uff0c\u4e24\u8005\u80cc\u540e\u7684\u601d\u60f3\u662f\u4e00\u81f4\u7684\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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<>();\nqueue.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.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.go
/* \u5c42\u5e8f\u904d\u5386 */\nfunc levelOrder(root *TreeNode) []any {\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([]any, 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.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.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.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
binary_tree_bfs.rs
/* \u5c42\u5e8f\u904d\u5386 */\nfn level_order(root: &Rc<RefCell<TreeNode>>) -> Vec<i32> {\n// \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u7ed3\u70b9\nlet mut que = VecDeque::new();\nque.push_back(Rc::clone(&root));\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nlet mut vec = Vec::new();\nwhile let Some(node) = que.pop_front() {                 // \u961f\u5217\u51fa\u961f\nvec.push(node.borrow().val);                         // \u4fdd\u5b58\u7ed3\u70b9\u503c\nif let Some(left) = node.borrow().left.as_ref() {\nque.push_back(Rc::clone(left));                  // \u5de6\u5b50\u7ed3\u70b9\u5165\u961f\n}\nif let Some(right) = node.borrow().right.as_ref() {\nque.push_back(Rc::clone(right));                 // \u53f3\u5b50\u7ed3\u70b9\u5165\u961f\n};\n}\nvec\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.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
"},{"location":"chapter_tree/binary_tree_traversal/#2","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"
  • \u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \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 \\(O(n)\\) \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 \\((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

\u56fe 7-10 \u5c55\u793a\u4e86\u5bf9\u4e8c\u53c9\u6811\u8fdb\u884c\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u5de5\u4f5c\u539f\u7406\u3002\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u5c31\u50cf\u662f\u7ed5\u7740\u6574\u4e2a\u4e8c\u53c9\u6811\u7684\u5916\u56f4\u201c\u8d70\u201d\u4e00\u5708\uff0c\u5728\u6bcf\u4e2a\u8282\u70b9\u90fd\u4f1a\u9047\u5230\u4e09\u4e2a\u4f4d\u7f6e\uff0c\u5206\u522b\u5bf9\u5e94\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u548c\u540e\u5e8f\u904d\u5386\u3002

\u56fe 7-10 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u524d\u3001\u4e2d\u3001\u540e\u5e8f\u904d\u5386

"},{"location":"chapter_tree/binary_tree_traversal/#1_1","title":"1. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u5b9e\u73b0\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_tree_dfs.py
def pre_order(root: TreeNode | 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):\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):\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.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.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.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.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.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.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.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
binary_tree_dfs.rs
/* \u524d\u5e8f\u904d\u5386 */\nfn pre_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\nlet mut result = vec![];\nif let Some(node) = root {\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u7ed3\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nresult.push(node.borrow().val);\nresult.append(&mut pre_order(node.borrow().left.as_ref()));\nresult.append(&mut pre_order(node.borrow().right.as_ref()));\n}\nresult\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfn in_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\nlet mut result = vec![];\nif let Some(node) = root {\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u7ed3\u70b9 -> \u53f3\u5b50\u6811\nresult.append(&mut in_order(node.borrow().left.as_ref()));\nresult.push(node.borrow().val);\nresult.append(&mut in_order(node.borrow().right.as_ref()));\n}\nresult\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfn post_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\nlet mut result = vec![];\nif let Some(node) = root {\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u7ed3\u70b9\nresult.append(&mut post_order(node.borrow().left.as_ref()));\nresult.append(&mut post_order(node.borrow().right.as_ref()));\nresult.push(node.borrow().val);\n}\nresult\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.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

Note

\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u4e5f\u53ef\u4ee5\u57fa\u4e8e\u8fed\u4ee3\u5b9e\u73b0\uff0c\u6709\u5174\u8da3\u7684\u540c\u5b66\u53ef\u4ee5\u81ea\u884c\u7814\u7a76\u3002

\u56fe 7-11 \u5c55\u793a\u4e86\u524d\u5e8f\u904d\u5386\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b\uff0c\u5176\u53ef\u5206\u4e3a\u201c\u9012\u201d\u548c\u201c\u5f52\u201d\u4e24\u4e2a\u9006\u5411\u7684\u90e8\u5206\u3002

  1. \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\u3002
  2. \u201c\u5f52\u201d\u8868\u793a\u51fd\u6570\u8fd4\u56de\uff0c\u4ee3\u8868\u5f53\u524d\u8282\u70b9\u5df2\u7ecf\u8bbf\u95ee\u5b8c\u6bd5\u3002
<1><2><3><4><5><6><7><8><9><10><11>

\u56fe 7-11 \u00a0 \u524d\u5e8f\u904d\u5386\u7684\u9012\u5f52\u8fc7\u7a0b

"},{"location":"chapter_tree/binary_tree_traversal/#2_1","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"
  • \u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \uff1a\u6240\u6709\u8282\u70b9\u88ab\u8bbf\u95ee\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002
  • \u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \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":""},{"location":"chapter_tree/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","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\u5b8c\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
"},{"location":"chapter_tree/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

\u5bf9\u4e8e\u53ea\u6709\u4e00\u4e2a\u8282\u70b9\u7684\u4e8c\u53c9\u6811\uff0c\u6811\u7684\u9ad8\u5ea6\u548c\u6839\u8282\u70b9\u7684\u6df1\u5ea6\u90fd\u662f \\(0\\) \u5417\uff1f

\u662f\u7684\uff0c\u56e0\u4e3a\u9ad8\u5ea6\u548c\u6df1\u5ea6\u901a\u5e38\u5b9a\u4e49\u4e3a\u201c\u8d70\u8fc7\u8fb9\u7684\u6570\u91cf\u201d\u3002

\u4e8c\u53c9\u6811\u4e2d\u7684\u63d2\u5165\u4e0e\u5220\u9664\u4e00\u822c\u90fd\u662f\u7531\u4e00\u5957\u64cd\u4f5c\u914d\u5408\u5b8c\u6210\u7684\uff0c\u8fd9\u91cc\u7684\u201c\u4e00\u5957\u64cd\u4f5c\u201d\u6307\u4ec0\u4e48\u5462\uff1f\u53ef\u4ee5\u7406\u89e3\u4e3a\u8d44\u6e90\u7684\u5b50\u8282\u70b9\u7684\u8d44\u6e90\u91ca\u653e\u5417\uff1f

\u62ff\u4e8c\u53c9\u641c\u7d22\u6811\u6765\u4e3e\u4f8b\uff0c\u5220\u9664\u8282\u70b9\u64cd\u4f5c\u8981\u5206\u4e3a\u4e09\u79cd\u60c5\u51b5\u5904\u7406\uff0c\u5176\u4e2d\u6bcf\u79cd\u60c5\u51b5\u90fd\u9700\u8981\u8fdb\u884c\u591a\u4e2a\u6b65\u9aa4\u7684\u8282\u70b9\u64cd\u4f5c\u3002

\u4e3a\u4ec0\u4e48 DFS \u904d\u5386\u4e8c\u53c9\u6811\u6709\u524d\u3001\u4e2d\u3001\u540e\u4e09\u79cd\u987a\u5e8f\uff0c\u5206\u522b\u6709\u4ec0\u4e48\u7528\u5462\uff1f

DFS \u7684\u524d\u3001\u4e2d\u3001\u540e\u5e8f\u904d\u5386\u548c\u8bbf\u95ee\u6570\u7ec4\u7684\u987a\u5e8f\u7c7b\u4f3c\uff0c\u662f\u904d\u5386\u4e8c\u53c9\u6811\u7684\u57fa\u672c\u65b9\u6cd5\uff0c\u5229\u7528\u8fd9\u4e09\u79cd\u904d\u5386\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u7279\u5b9a\u987a\u5e8f\u7684\u904d\u5386\u7ed3\u679c\u3002\u4f8b\u5982\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\uff0c\u7531\u4e8e\u7ed3\u70b9\u5927\u5c0f\u6ee1\u8db3 \u5de6\u5b50\u7ed3\u70b9\u503c < \u6839\u7ed3\u70b9\u503c < \u53f3\u5b50\u7ed3\u70b9\u503c \uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u8981\u6309\u7167 \u5de6->\u6839->\u53f3 \u7684\u4f18\u5148\u7ea7\u904d\u5386\u6811\uff0c\u5c31\u53ef\u4ee5\u83b7\u5f97\u6709\u5e8f\u7684\u8282\u70b9\u5e8f\u5217\u3002

\u53f3\u65cb\u64cd\u4f5c\u662f\u5904\u7406\u5931\u8861\u8282\u70b9 node\u3001child\u3001grand_child \u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u90a3 node \u7684\u7236\u8282\u70b9\u548c node \u539f\u6765\u7684\u8fde\u63a5\u4e0d\u9700\u8981\u7ef4\u62a4\u5417\uff1f\u53f3\u65cb\u64cd\u4f5c\u540e\u5c82\u4e0d\u662f\u65ad\u6389\u4e86\uff1f

\u6211\u4eec\u9700\u8981\u4ece\u9012\u5f52\u7684\u89c6\u89d2\u6765\u770b\u8fd9\u4e2a\u95ee\u9898\u3002\u53f3\u65cb\u64cd\u4f5c right_rotate(root) \u4f20\u5165\u7684\u662f\u5b50\u6811\u7684\u6839\u8282\u70b9\uff0c\u6700\u7ec8 return child \u8fd4\u56de\u65cb\u8f6c\u4e4b\u540e\u7684\u5b50\u6811\u7684\u6839\u8282\u70b9\u3002\u5b50\u6811\u7684\u6839\u8282\u70b9\u548c\u5176\u7236\u8282\u70b9\u7684\u8fde\u63a5\u662f\u5728\u8be5\u51fd\u6570\u8fd4\u56de\u540e\u5b8c\u6210\u7684\uff0c\u4e0d\u5c5e\u4e8e\u53f3\u65cb\u64cd\u4f5c\u7684\u7ef4\u62a4\u8303\u56f4\u3002

\u5728 C++ \u4e2d\uff0c\u51fd\u6570\u88ab\u5212\u5206\u5230 private \u548c public \u4e2d\uff0c\u8fd9\u65b9\u9762\u6709\u4ec0\u4e48\u8003\u91cf\u5417\uff1f\u4e3a\u4ec0\u4e48\u8981\u5c06 height() \u51fd\u6570\u548c updateHeight() \u51fd\u6570\u5206\u522b\u653e\u5728 public \u548c private \u4e2d\u5462\uff1f

\u4e3b\u8981\u770b\u65b9\u6cd5\u7684\u4f7f\u7528\u8303\u56f4\uff0c\u5982\u679c\u65b9\u6cd5\u53ea\u5728\u7c7b\u5185\u90e8\u4f7f\u7528\uff0c\u90a3\u4e48\u5c31\u8bbe\u8ba1\u4e3a private \u3002\u4f8b\u5982\uff0c\u7528\u6237\u5355\u72ec\u8c03\u7528 updateHeight() \u662f\u6ca1\u6709\u610f\u4e49\u7684\uff0c\u5b83\u53ea\u662f\u63d2\u5165\u3001\u5220\u9664\u64cd\u4f5c\u4e2d\u7684\u4e00\u6b65\u3002\u800c height() \u662f\u8bbf\u95ee\u7ed3\u70b9\u9ad8\u5ea6\uff0c\u7c7b\u4f3c\u4e8e vector.size() \uff0c\u56e0\u6b64\u8bbe\u7f6e\u6210 public \u4ee5\u4fbf\u4f7f\u7528\u3002

\u8bf7\u95ee\u5982\u4f55\u4ece\u4e00\u7ec4\u8f93\u5165\u6570\u636e\u6784\u5efa\u4e00\u4e2a\u4e8c\u53c9\u641c\u7d22\u6811\uff1f\u6839\u8282\u70b9\u7684\u9009\u62e9\u662f\u4e0d\u662f\u5f88\u91cd\u8981\uff1f

\u662f\u7684\uff0c\u6784\u5efa\u6811\u7684\u65b9\u6cd5\u5df2\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4ee3\u7801\u4e2d\u7684 build_tree() \u65b9\u6cd5\u4e2d\u7ed9\u51fa\u3002\u81f3\u4e8e\u6839\u8282\u70b9\u7684\u9009\u62e9\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u8f93\u5165\u6570\u636e\u6392\u5e8f\uff0c\u7136\u540e\u7528\u4e2d\u70b9\u5143\u7d20\u4f5c\u4e3a\u6839\u8282\u70b9\uff0c\u518d\u9012\u5f52\u5730\u6784\u5efa\u5de6\u53f3\u5b50\u6811\u3002\u8fd9\u6837\u505a\u53ef\u4ee5\u6700\u5927\u7a0b\u5ea6\u4fdd\u8bc1\u6811\u7684\u5e73\u8861\u6027\u3002

\u5728 Java \u4e2d\uff0c\u5b57\u7b26\u4e32\u5bf9\u6bd4\u662f\u5426\u4e00\u5b9a\u8981\u7528 equals() \u65b9\u6cd5\uff1f

\u5728 Java \u4e2d\uff0c\u5bf9\u4e8e\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c== \u7528\u4e8e\u5bf9\u6bd4\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\u662f\u5426\u76f8\u7b49\u3002\u5bf9\u4e8e\u5f15\u7528\u7c7b\u578b\uff0c\u4e24\u79cd\u7b26\u53f7\u7684\u5de5\u4f5c\u539f\u7406\u662f\u4e0d\u540c\u7684\u3002

  • == \uff1a\u7528\u6765\u6bd4\u8f83\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5373\u5b83\u4eec\u5728\u5185\u5b58\u4e2d\u7684\u4f4d\u7f6e\u662f\u5426\u76f8\u540c\u3002
  • equals()\uff1a\u7528\u6765\u5bf9\u6bd4\u4e24\u4e2a\u5bf9\u8c61\u7684\u503c\u662f\u5426\u76f8\u7b49\u3002

\u56e0\u6b64\u5982\u679c\u8981\u5bf9\u6bd4\u503c\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u7528 equals() \u3002\u7136\u800c\uff0c\u901a\u8fc7 String a = \"hi\"; String b = \"hi\"; \u521d\u59cb\u5316\u7684\u5b57\u7b26\u4e32\u90fd\u5b58\u50a8\u5728\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u4e2d\uff0c\u5b83\u4eec\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u56e0\u6b64\u4e5f\u53ef\u4ee5\u7528 a == b \u6765\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u3002

\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u5230\u6700\u5e95\u5c42\u4e4b\u524d\uff0c\u961f\u5217\u4e2d\u7684\u8282\u70b9\u6570\u91cf\u662f \\(2^h\\) \u5417\uff1f

\u662f\u7684\uff0c\u4f8b\u5982\u9ad8\u5ea6 \\(h = 2\\) \u7684\u6ee1\u4e8c\u53c9\u6811\uff0c\u5176\u8282\u70b9\u603b\u6570 \\(n = 7\\) \uff0c\u5219\u5e95\u5c42\u8282\u70b9\u6570\u91cf \\(4 = 2^h = (n + 1) / 2\\) \u3002

"}]} \ No newline at end of file +{"config":{"lang":["en"],"separator":"[\\s\\u200b\\u3000\\-\u3001\u3002\uff0c\uff0e\uff1f\uff01\uff1b]+","pipeline":["stemmer"]},"docs":[{"location":"","title":"Home","text":"\u300a Hello \u7b97\u6cd5 \u300b

\u52a8\u753b\u56fe\u89e3\u3001\u4e00\u952e\u8fd0\u884c\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u6559\u7a0b

\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

\u5168\u4e66\u52a8\u753b\u56fe\u89e3

\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

\u4ee3\u7801\u4e00\u952e\u8fd0\u884c

\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\u3001C++\u3001Python\u3001Go\u3001JS\u3001TS\u3001C#\u3001Swift\u3001Rust\u3001Dart\u3001Zig \u7b49\u8bed\u8a00

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

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

\u53ef\u8ba8\u8bba\u4e0e\u63d0\u95ee

\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\u540c\u5b66\u7684\u559c\u7231\u548c\u652f\u6301\u3002\u5728\u4e0e\u8bfb\u8005\u7684\u4ea4\u6d41\u671f\u95f4\uff0c\u6700\u5e38\u6536\u5230\u7684\u4e00\u4e2a\u95ee\u9898\u662f\u201c\u5982\u4f55\u5165\u95e8\u5b66\u4e60\u7b97\u6cd5\u201d\u3002\u6211\u9010\u6e10\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\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\u5373\u4f7f\u4e0d\u662f\u6700\u4f18\u89e3\uff0c\u4e5f\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\u53d1\u8868\u7684\u300a\u56fe\u89e3\u7b97\u6cd5\u6570\u636e\u7ed3\u6784\u300b\u5df2\u88ab\u8ba2\u9605 27 \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\u7684\u65e0\u79c1\u5949\u732e\u4f7f\u8fd9\u672c\u4e66\u53d8\u5f97\u66f4\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, night-cruise, 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 hpstoryC# justin-tseJS, TS krahetsJava, Python night-cruiseRust nuomi1Swift ReanonGo, C sjinzhRust, Zig"},{"location":"chapter_appendix/","title":"\u7b2c 16 \u7ae0 \u00a0 \u9644\u5f55","text":""},{"location":"chapter_appendix/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 16.1 \u00a0 \u7f16\u7a0b\u73af\u5883\u5b89\u88c5
  • 16.2 \u00a0 \u4e00\u8d77\u53c2\u4e0e\u521b\u4f5c
"},{"location":"chapter_appendix/contribution/","title":"16.2 \u00a0 \u4e00\u8d77\u53c2\u4e0e\u521b\u4f5c","text":"

\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\u7ed9\u8bfb\u8005\u63d0\u4f9b\u66f4\u4f18\u8d28\u7684\u5b66\u4e60\u8d44\u6e90\u3002

\u6240\u6709\u64b0\u7a3f\u4eba\u7684 GitHub ID \u5c06\u88ab\u5c55\u793a\u5728\u672c\u4e66\u7684\u4ed3\u5e93\u4e3b\u9875\u4e0a\uff0c\u4ee5\u611f\u8c22\u4ed6\u4eec\u5bf9\u5f00\u6e90\u793e\u533a\u7684\u65e0\u79c1\u5949\u732e\u3002

\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

\u7136\u800c\u5728\u672c\u5f00\u6e90\u4e66\u4e2d\uff0c\u5185\u5bb9\u66f4\u8fed\u7684\u65f6\u95f4\u88ab\u7f29\u77ed\u81f3\u6570\u65e5\u751a\u81f3\u51e0\u4e2a\u5c0f\u65f6\u3002

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

\u5982\u56fe 16-1 \u6240\u793a\uff0c\u6bcf\u4e2a\u9875\u9762\u7684\u53f3\u4e0a\u89d2\u90fd\u6709\u201c\u7f16\u8f91\u56fe\u6807\u201d\u3002\u60a8\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u4fee\u6539\u6587\u672c\u6216\u4ee3\u7801\u3002

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

\u56fe 16-1 \u00a0 \u9875\u9762\u7f16\u8f91\u6309\u952e

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

"},{"location":"chapter_appendix/contribution/#2","title":"2. \u00a0 \u5185\u5bb9\u521b\u4f5c","text":"

\u5982\u679c\u60a8\u6709\u5174\u8da3\u53c2\u4e0e\u6b64\u5f00\u6e90\u9879\u76ee\uff0c\u5305\u62ec\u5c06\u4ee3\u7801\u7ffb\u8bd1\u6210\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u3001\u6269\u5c55\u6587\u7ae0\u5185\u5bb9\u7b49\uff0c\u90a3\u4e48\u9700\u8981\u5b9e\u65bd\u4ee5\u4e0b Pull Request \u5de5\u4f5c\u6d41\u7a0b\u3002

  1. \u767b\u5f55 GitHub \uff0c\u5c06\u672c\u4ed3\u5e93 Fork \u5230\u4e2a\u4eba\u8d26\u53f7\u4e0b\u3002
  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\u3002
  3. \u5728\u672c\u5730\u8fdb\u884c\u5185\u5bb9\u521b\u4f5c\uff0c\u5e76\u8fdb\u884c\u5b8c\u6574\u6d4b\u8bd5\uff0c\u9a8c\u8bc1\u4ee3\u7801\u7684\u6b63\u786e\u6027\u3002
  4. \u5c06\u672c\u5730\u6240\u505a\u66f4\u6539 Commit \uff0c\u7136\u540e Push \u81f3\u8fdc\u7a0b\u4ed3\u5e93\u3002
  5. \u5237\u65b0\u4ed3\u5e93\u7f51\u9875\uff0c\u70b9\u51fb\u201cCreate pull request\u201d\u6309\u94ae\u5373\u53ef\u53d1\u8d77\u62c9\u53d6\u8bf7\u6c42\u3002
"},{"location":"chapter_appendix/contribution/#3-docker","title":"3. \u00a0 Docker \u90e8\u7f72","text":"

\u5728 hello-algo \u6839\u76ee\u5f55\u4e0b\uff0c\u6267\u884c\u4ee5\u4e0b Docker \u811a\u672c\uff0c\u5373\u53ef\u5728 http://localhost:8000 \u8bbf\u95ee\u672c\u9879\u76ee\u3002

docker-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":"16.1 \u00a0 \u7f16\u7a0b\u73af\u5883\u5b89\u88c5","text":""},{"location":"chapter_appendix/installation/#1-vscode","title":"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/#2-java","title":"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/#3-cc","title":"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\u987b\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/#4-python","title":"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/#5-go","title":"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/#6-javascript","title":"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/#7-c","title":"7. \u00a0 C# \u73af\u5883","text":"
  1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 .Net 6.0 \u3002
  2. \u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 C# Dev Kit \uff0c\u5b89\u88c5 C# Dev Kit \uff08\u914d\u7f6e\u6559\u7a0b\uff09\u3002
  3. \u4e5f\u53ef\u4f7f\u7528 Visual Studio\uff08\u5b89\u88c5\u6559\u7a0b\uff09\u3002
"},{"location":"chapter_appendix/installation/#8-swift","title":"8. \u00a0 Swift \u73af\u5883","text":"
  1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 Swift \u3002
  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/#9-dart","title":"9. \u00a0 Dart \u73af\u5883","text":"
  1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 Dart \u3002
  2. \u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 dart \uff0c\u5b89\u88c5 Dart \u3002
"},{"location":"chapter_appendix/installation/#10-rust","title":"10. \u00a0 Rust \u73af\u5883","text":"
  1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 Rust \u3002
  2. \u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 rust \uff0c\u5b89\u88c5 rust-analyzer \u3002
"},{"location":"chapter_array_and_linkedlist/","title":"\u7b2c 4 \u7ae0 \u00a0 \u6570\u7ec4\u4e0e\u94fe\u8868","text":"

Abstract

\u6570\u636e\u7ed3\u6784\u7684\u4e16\u754c\u5982\u540c\u4e00\u5835\u539a\u5b9e\u7684\u7816\u5899\u3002

\u6570\u7ec4\u7684\u7816\u5757\u6574\u9f50\u6392\u5217\uff0c\u9010\u4e2a\u7d27\u8d34\u3002\u94fe\u8868\u7684\u7816\u5757\u5206\u6563\u5404\u5904\uff0c\u8fde\u63a5\u7684\u85e4\u8513\u81ea\u7531\u5730\u7a7f\u68ad\u4e8e\u7816\u7f1d\u4e4b\u95f4\u3002

"},{"location":"chapter_array_and_linkedlist/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 4.1 \u00a0 \u6570\u7ec4
  • 4.2 \u00a0 \u94fe\u8868
  • 4.3 \u00a0 \u5217\u8868
  • 4.4 \u00a0 \u5c0f\u7ed3
"},{"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\u8be5\u5143\u7d20\u7684\u300c\u7d22\u5f15 index\u300d\u3002\u56fe 4-1 \u5c55\u793a\u4e86\u6570\u7ec4\u7684\u4e3b\u8981\u672f\u8bed\u548c\u6982\u5ff5\u3002

\u56fe 4-1 \u00a0 \u6570\u7ec4\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f

"},{"location":"chapter_array_and_linkedlist/array/#411","title":"4.1.1 \u00a0 \u6570\u7ec4\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/array/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u6570\u7ec4","text":"

\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u7528\u6570\u7ec4\u7684\u4e24\u79cd\u521d\u59cb\u5316\u65b9\u5f0f\uff1a\u65e0\u521d\u59cb\u503c\u3001\u7ed9\u5b9a\u521d\u59cb\u503c\u3002\u5728\u672a\u6307\u5b9a\u521d\u59cb\u503c\u7684\u60c5\u51b5\u4e0b\uff0c\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4f1a\u5c06\u6570\u7ec4\u5143\u7d20\u521d\u59cb\u5316\u4e3a \\(0\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig array.py
# \u521d\u59cb\u5316\u6570\u7ec4\narr: list[int] = [0] * 5  # [ 0, 0, 0, 0, 0 ]\nnums: list[int] = [1, 3, 2, 5, 4]  \n
array.cpp
/* \u521d\u59cb\u5316\u6570\u7ec4 */\n// \u5b58\u50a8\u5728\u6808\u4e0a\nint arr[5];\nint nums[5] { 1, 3, 2, 5, 4 };\n// \u5b58\u50a8\u5728\u5806\u4e0a\uff08\u9700\u8981\u624b\u52a8\u91ca\u653e\u7a7a\u95f4\uff09\nint* arr1 = new int[5];\nint* nums1 = new int[5] { 1, 3, 2, 5, 4 };\n
array.java
/* \u521d\u59cb\u5316\u6570\u7ec4 */\nint[] arr = new int[5]; // { 0, 0, 0, 0, 0 }\nint[] nums = { 1, 3, 2, 5, 4 };\n
array.cs
/* \u521d\u59cb\u5316\u6570\u7ec4 */\nint[] arr = new int[5]; // { 0, 0, 0, 0, 0 }\nint[] nums = { 1, 3, 2, 5, 4 };\n
array.go
/* \u521d\u59cb\u5316\u6570\u7ec4 */\nvar arr [5]int\n// \u5728 Go \u4e2d\uff0c\u6307\u5b9a\u957f\u5ea6\u65f6\uff08[5]int\uff09\u4e3a\u6570\u7ec4\uff0c\u4e0d\u6307\u5b9a\u957f\u5ea6\u65f6\uff08[]int\uff09\u4e3a\u5207\u7247\n// \u7531\u4e8e Go \u7684\u6570\u7ec4\u88ab\u8bbe\u8ba1\u4e3a\u5728\u7f16\u8bd1\u671f\u786e\u5b9a\u957f\u5ea6\uff0c\u56e0\u6b64\u53ea\u80fd\u4f7f\u7528\u5e38\u91cf\u6765\u6307\u5b9a\u957f\u5ea6\n// \u4e3a\u4e86\u65b9\u4fbf\u5b9e\u73b0\u6269\u5bb9 extend() \u65b9\u6cd5\uff0c\u4ee5\u4e0b\u5c06\u5207\u7247\uff08Slice\uff09\u770b\u4f5c\u6570\u7ec4\uff08Array\uff09\nnums := []int{1, 3, 2, 5, 4}\n
array.swift
/* \u521d\u59cb\u5316\u6570\u7ec4 */\nlet arr = Array(repeating: 0, count: 5) // [0, 0, 0, 0, 0]\nlet nums = [1, 3, 2, 5, 4]\n
array.js
/* \u521d\u59cb\u5316\u6570\u7ec4 */\nvar arr = new Array(5).fill(0);\nvar nums = [1, 3, 2, 5, 4];\n
array.ts
/* \u521d\u59cb\u5316\u6570\u7ec4 */\nlet arr: number[] = new Array(5).fill(0);\nlet nums: number[] = [1, 3, 2, 5, 4];\n
array.dart
/* \u521d\u59cb\u5316\u6570\u7ec4 */\nList<int> arr = List.filled(5, 0); // [0, 0, 0, 0, 0]\nList<int> nums = [1, 3, 2, 5, 4];\n
array.rs
/* \u521d\u59cb\u5316\u6570\u7ec4 */\nlet arr: Vec<i32> = vec![0; 5]; // [0, 0, 0, 0, 0]\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
array.c
/* \u521d\u59cb\u5316\u6570\u7ec4 */\nint arr[5] = { 0 }; // { 0, 0, 0, 0, 0 }\nint nums[5] = { 1, 3, 2, 5, 4 };\n
array.zig
// \u521d\u59cb\u5316\u6570\u7ec4\nvar arr = [_]i32{0} ** 5; // { 0, 0, 0, 0, 0 }\nvar nums = [_]i32{ 1, 3, 2, 5, 4 };\n
"},{"location":"chapter_array_and_linkedlist/array/#2","title":"2. \u00a0 \u8bbf\u95ee\u5143\u7d20","text":"

\u6570\u7ec4\u5143\u7d20\u88ab\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\uff0c\u8fd9\u610f\u5473\u7740\u8ba1\u7b97\u6570\u7ec4\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\u975e\u5e38\u5bb9\u6613\u3002\u7ed9\u5b9a\u6570\u7ec4\u5185\u5b58\u5730\u5740\uff08\u5373\u9996\u5143\u7d20\u5185\u5b58\u5730\u5740\uff09\u548c\u67d0\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u56fe 4-2 \u6240\u793a\u7684\u516c\u5f0f\u8ba1\u7b97\u5f97\u5230\u8be5\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\uff0c\u4ece\u800c\u76f4\u63a5\u8bbf\u95ee\u6b64\u5143\u7d20\u3002

\u56fe 4-2 \u00a0 \u6570\u7ec4\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\u8ba1\u7b97

\u89c2\u5bdf\u56fe 4-2 \uff0c\u6211\u4eec\u53d1\u73b0\u6570\u7ec4\u9996\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u4e3a \\(0\\) \uff0c\u8fd9\u4f3c\u4e4e\u6709\u4e9b\u53cd\u76f4\u89c9\uff0c\u56e0\u4e3a\u4ece \\(1\\) \u5f00\u59cb\u8ba1\u6570\u4f1a\u66f4\u81ea\u7136\u3002\u4f46\u4ece\u5730\u5740\u8ba1\u7b97\u516c\u5f0f\u7684\u89d2\u5ea6\u770b\uff0c\u7d22\u5f15\u7684\u542b\u4e49\u672c\u8d28\u4e0a\u662f\u5185\u5b58\u5730\u5740\u7684\u504f\u79fb\u91cf\u3002\u9996\u4e2a\u5143\u7d20\u7684\u5730\u5740\u504f\u79fb\u91cf\u662f \\(0\\) \uff0c\u56e0\u6b64\u5b83\u7684\u7d22\u5f15\u4e3a \\(0\\) \u4e5f\u662f\u5408\u7406\u7684\u3002

\u5728\u6570\u7ec4\u4e2d\u8bbf\u95ee\u5143\u7d20\u662f\u975e\u5e38\u9ad8\u6548\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u968f\u673a\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u5143\u7d20\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig array.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.cpp
/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int *nums, int size) {\n// \u5728\u533a\u95f4 [0, size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nint randomIndex = rand() % size;\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nint randomNum = nums[randomIndex];\nreturn randomNum;\n}\n
array.java
/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int[] nums) {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\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.cs
/* \u968f\u673a\u8bbf\u95ee\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.go
/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunc randomAccess(nums []int) (randomNum int) {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nrandomIndex := rand.Intn(len(nums))\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nrandomNum = nums[randomIndex]\nreturn\n}\n
array.swift
/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunc randomAccess(nums: [Int]) -> Int {\n// \u5728\u533a\u95f4 [0, nums.count) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nlet randomIndex = nums.indices.randomElement()!\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nlet randomNum = nums[randomIndex]\nreturn randomNum\n}\n
array.js
/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunction randomAccess(nums) {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\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\u8bbf\u95ee\u5143\u7d20 */\nfunction randomAccess(nums: number[]): number {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\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.dart
/* \u968f\u673a\u8bbf\u95ee\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
array.rs
/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfn random_access(nums: &[i32]) -> i32 {\n// \u5728\u533a\u95f4 [0, nums.len()) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nlet random_index = rand::thread_rng().gen_range(0..nums.len());\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nlet random_num = nums[random_index];\nrandom_num\n}\n
array.c
/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int *nums, int size) {\n// \u5728\u533a\u95f4 [0, size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nint randomIndex = rand() % size;\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nint randomNum = nums[randomIndex];\nreturn randomNum;\n}\n
array.zig
// \u968f\u673a\u8bbf\u95ee\u5143\u7d20\nfn randomAccess(nums: []i32) i32 {\n// \u5728\u533a\u95f4 [0, nums.len) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6574\u6570\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
"},{"location":"chapter_array_and_linkedlist/array/#3","title":"3. \u00a0 \u63d2\u5165\u5143\u7d20","text":"

\u6570\u7ec4\u5143\u7d20\u5728\u5185\u5b58\u4e2d\u662f\u201c\u7d27\u6328\u7740\u7684\u201d\uff0c\u5b83\u4eec\u4e4b\u95f4\u6ca1\u6709\u7a7a\u95f4\u518d\u5b58\u653e\u4efb\u4f55\u6570\u636e\u3002\u5982\u56fe 4-3 \u6240\u793a\uff0c\u5982\u679c\u60f3\u8981\u5728\u6570\u7ec4\u4e2d\u95f4\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\uff0c\u5219\u9700\u8981\u5c06\u8be5\u5143\u7d20\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u4e4b\u540e\u518d\u628a\u5143\u7d20\u8d4b\u503c\u7ed9\u8be5\u7d22\u5f15\u3002

\u56fe 4-3 \u00a0 \u6570\u7ec4\u63d2\u5165\u5143\u7d20\u793a\u4f8b

\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u56fa\u5b9a\u7684\uff0c\u56e0\u6b64\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\u5fc5\u5b9a\u4f1a\u5bfc\u81f4\u6570\u7ec4\u5c3e\u90e8\u5143\u7d20\u7684\u201c\u4e22\u5931\u201d\u3002\u6211\u4eec\u5c06\u8fd9\u4e2a\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u7559\u5728\u5217\u8868\u7ae0\u8282\u4e2d\u8ba8\u8bba\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig array.py
def insert(nums: list[int], num: int, index: int):\n\"\"\"\u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num\"\"\"\n# \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\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.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.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.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.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.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.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.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
array.rs
/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfn insert(nums: &mut Vec<i32>, num: i32, index: usize) {\n// \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor i in (index + 1..nums.len()).rev() {\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.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
"},{"location":"chapter_array_and_linkedlist/array/#4","title":"4. \u00a0 \u5220\u9664\u5143\u7d20","text":"

\u540c\u7406\uff0c\u5982\u56fe 4-4 \u6240\u793a\uff0c\u82e5\u60f3\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

\u56fe 4-4 \u00a0 \u6570\u7ec4\u5220\u9664\u5143\u7d20\u793a\u4f8b

\u8bf7\u6ce8\u610f\uff0c\u5220\u9664\u5143\u7d20\u5b8c\u6210\u540e\uff0c\u539f\u5148\u672b\u5c3e\u7684\u5143\u7d20\u53d8\u5f97\u201c\u65e0\u610f\u4e49\u201d\u4e86\uff0c\u6240\u4ee5\u6211\u4eec\u65e0\u987b\u7279\u610f\u53bb\u4fee\u6539\u5b83\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig array.py
def remove(nums: list[int], index: int):\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.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.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.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.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.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.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.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
array.rs
/* \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 */\nfn remove(nums: &mut Vec<i32>, index: usize) {\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor i in index..nums.len() - 1 {\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.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

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

  • \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\u201c\u65e0\u610f\u4e49\u201d\u7684\uff0c\u4f46\u8fd9\u6837\u505a\u4e5f\u4f1a\u9020\u6210\u90e8\u5206\u5185\u5b58\u7a7a\u95f4\u7684\u6d6a\u8d39\u3002
"},{"location":"chapter_array_and_linkedlist/array/#5","title":"5. \u00a0 \u904d\u5386\u6570\u7ec4","text":"

\u5728\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u65e2\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u904d\u5386\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig array.py
def traverse(nums: list[int]):\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.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.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.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.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.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.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 (const 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 (const num of 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
array.rs
/* \u904d\u5386\u6570\u7ec4 */\nfn traverse(nums: &[i32]) {\nlet mut _count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor _ in 0..nums.len() {\n_count += 1;\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor _ in nums {\n_count += 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.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
"},{"location":"chapter_array_and_linkedlist/array/#6","title":"6. \u00a0 \u67e5\u627e\u5143\u7d20","text":"

\u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\u9700\u8981\u904d\u5386\u6570\u7ec4\uff0c\u6bcf\u8f6e\u5224\u65ad\u5143\u7d20\u503c\u662f\u5426\u5339\u914d\uff0c\u82e5\u5339\u914d\u5219\u8f93\u51fa\u5bf9\u5e94\u7d22\u5f15\u3002

\u56e0\u4e3a\u6570\u7ec4\u662f\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u6240\u4ee5\u4e0a\u8ff0\u67e5\u627e\u64cd\u4f5c\u88ab\u79f0\u4e3a\u201c\u7ebf\u6027\u67e5\u627e\u201d\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig array.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.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.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.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.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.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.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.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
array.rs
/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfn find(nums: &[i32], target: i32) -> Option<usize> {\nfor i in 0..nums.len() {\nif nums[i] == target {\nreturn Some(i);\n}\n}\nNone\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.zig
// \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\nfn find(nums: []i32, target: i32) i32 {\nfor (nums, 0..) |num, i| {\nif (num == target) return @intCast(i);\n}\nreturn -1;\n}\n
"},{"location":"chapter_array_and_linkedlist/array/#7","title":"7. \u00a0 \u6269\u5bb9\u6570\u7ec4","text":"

\u5728\u590d\u6742\u7684\u7cfb\u7edf\u73af\u5883\u4e2d\uff0c\u7a0b\u5e8f\u96be\u4ee5\u4fdd\u8bc1\u6570\u7ec4\u4e4b\u540e\u7684\u5185\u5b58\u7a7a\u95f4\u662f\u53ef\u7528\u7684\uff0c\u4ece\u800c\u65e0\u6cd5\u5b89\u5168\u5730\u6269\u5c55\u6570\u7ec4\u5bb9\u91cf\u3002\u56e0\u6b64\u5728\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u4e0d\u53ef\u53d8\u7684\u3002

\u5982\u679c\u6211\u4eec\u5e0c\u671b\u6269\u5bb9\u6570\u7ec4\uff0c\u5219\u9700\u91cd\u65b0\u5efa\u7acb\u4e00\u4e2a\u66f4\u5927\u7684\u6570\u7ec4\uff0c\u7136\u540e\u628a\u539f\u6570\u7ec4\u5143\u7d20\u4f9d\u6b21\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\u3002\u8fd9\u662f\u4e00\u4e2a \\(O(n)\\) \u7684\u64cd\u4f5c\uff0c\u5728\u6570\u7ec4\u5f88\u5927\u7684\u60c5\u51b5\u4e0b\u662f\u975e\u5e38\u8017\u65f6\u7684\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
array.rs
/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfn extend(nums: Vec<i32>, enlarge: usize) -> Vec<i32> {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\nlet mut res: Vec<i32> = vec![0; nums.len() + enlarge];\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\nfor i in 0..nums.len() {\nres[i] = nums[i];\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nres\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.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);\n@memset(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
"},{"location":"chapter_array_and_linkedlist/array/#412","title":"4.1.2 \u00a0 \u6570\u7ec4\u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"

\u6570\u7ec4\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u5185\uff0c\u4e14\u5143\u7d20\u7c7b\u578b\u76f8\u540c\u3002\u8fd9\u79cd\u505a\u6cd5\u5305\u542b\u4e30\u5bcc\u7684\u5148\u9a8c\u4fe1\u606f\uff0c\u7cfb\u7edf\u53ef\u4ee5\u5229\u7528\u8fd9\u4e9b\u4fe1\u606f\u6765\u4f18\u5316\u6570\u636e\u7ed3\u6784\u7684\u64cd\u4f5c\u6548\u7387\u3002

  • \u7a7a\u95f4\u6548\u7387\u9ad8: \u6570\u7ec4\u4e3a\u6570\u636e\u5206\u914d\u4e86\u8fde\u7eed\u7684\u5185\u5b58\u5757\uff0c\u65e0\u987b\u989d\u5916\u7684\u7ed3\u6784\u5f00\u9500\u3002
  • \u652f\u6301\u968f\u673a\u8bbf\u95ee: \u6570\u7ec4\u5141\u8bb8\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u8bbf\u95ee\u4efb\u4f55\u5143\u7d20\u3002
  • \u7f13\u5b58\u5c40\u90e8\u6027: \u5f53\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

\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u662f\u4e00\u628a\u53cc\u5203\u5251\uff0c\u5176\u5b58\u5728\u4ee5\u4e0b\u7f3a\u70b9\u3002

  • \u63d2\u5165\u4e0e\u5220\u9664\u6548\u7387\u4f4e:\u5f53\u6570\u7ec4\u4e2d\u5143\u7d20\u8f83\u591a\u65f6\uff0c\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u9700\u8981\u79fb\u52a8\u5927\u91cf\u7684\u5143\u7d20\u3002
  • \u957f\u5ea6\u4e0d\u53ef\u53d8: \u6570\u7ec4\u5728\u521d\u59cb\u5316\u540e\u957f\u5ea6\u5c31\u56fa\u5b9a\u4e86\uff0c\u6269\u5bb9\u6570\u7ec4\u9700\u8981\u5c06\u6240\u6709\u6570\u636e\u590d\u5236\u5230\u65b0\u6570\u7ec4\uff0c\u5f00\u9500\u5f88\u5927\u3002
  • \u7a7a\u95f4\u6d6a\u8d39: \u5982\u679c\u6570\u7ec4\u5206\u914d\u7684\u5927\u5c0f\u8d85\u8fc7\u4e86\u5b9e\u9645\u6240\u9700\uff0c\u90a3\u4e48\u591a\u4f59\u7684\u7a7a\u95f4\u5c31\u88ab\u6d6a\u8d39\u4e86\u3002
"},{"location":"chapter_array_and_linkedlist/array/#413","title":"4.1.3 \u00a0 \u6570\u7ec4\u5178\u578b\u5e94\u7528","text":"

\u6570\u7ec4\u662f\u4e00\u79cd\u57fa\u7840\u4e14\u5e38\u89c1\u7684\u6570\u636e\u7ed3\u6784\uff0c\u65e2\u9891\u7e41\u5e94\u7528\u5728\u5404\u7c7b\u7b97\u6cd5\u4e4b\u4e2d\uff0c\u4e5f\u53ef\u7528\u4e8e\u5b9e\u73b0\u5404\u79cd\u590d\u6742\u6570\u636e\u7ed3\u6784\u3002

  • \u968f\u673a\u8bbf\u95ee\uff1a\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
  • \u6392\u5e8f\u548c\u641c\u7d22\uff1a\u6570\u7ec4\u662f\u6392\u5e8f\u548c\u641c\u7d22\u7b97\u6cd5\u6700\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u3002\u5feb\u901f\u6392\u5e8f\u3001\u5f52\u5e76\u6392\u5e8f\u3001\u4e8c\u5206\u67e5\u627e\u7b49\u90fd\u4e3b\u8981\u5728\u6570\u7ec4\u4e0a\u8fdb\u884c\u3002
  • \u67e5\u627e\u8868\uff1a\u5f53\u6211\u4eec\u9700\u8981\u5feb\u901f\u67e5\u627e\u4e00\u4e2a\u5143\u7d20\u6216\u8005\u9700\u8981\u67e5\u627e\u4e00\u4e2a\u5143\u7d20\u7684\u5bf9\u5e94\u5173\u7cfb\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u4f5c\u4e3a\u67e5\u627e\u8868\u3002\u5047\u5982\u6211\u4eec\u60f3\u8981\u5b9e\u73b0\u5b57\u7b26\u5230 ASCII \u7801\u7684\u6620\u5c04\uff0c\u5219\u53ef\u4ee5\u5c06\u5b57\u7b26\u7684 ASCII \u7801\u503c\u4f5c\u4e3a\u7d22\u5f15\uff0c\u5bf9\u5e94\u7684\u5143\u7d20\u5b58\u653e\u5728\u6570\u7ec4\u4e2d\u7684\u5bf9\u5e94\u4f4d\u7f6e\u3002
  • \u673a\u5668\u5b66\u4e60\uff1a\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
  • \u6570\u636e\u7ed3\u6784\u5b9e\u73b0\uff1a\u6570\u7ec4\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u5806\u3001\u56fe\u7b49\u6570\u636e\u7ed3\u6784\u3002\u4f8b\u5982\uff0c\u56fe\u7684\u90bb\u63a5\u77e9\u9635\u8868\u793a\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\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\u5728\u4e00\u4e2a\u590d\u6742\u7684\u7cfb\u7edf\u8fd0\u884c\u73af\u5883\u4e0b\uff0c\u7a7a\u95f2\u7684\u5185\u5b58\u7a7a\u95f4\u53ef\u80fd\u6563\u843d\u5728\u5185\u5b58\u5404\u5904\u3002\u6211\u4eec\u77e5\u9053\uff0c\u5b58\u50a8\u6570\u7ec4\u7684\u5185\u5b58\u7a7a\u95f4\u5fc5\u987b\u662f\u8fde\u7eed\u7684\uff0c\u800c\u5f53\u6570\u7ec4\u975e\u5e38\u5927\u65f6\uff0c\u5185\u5b58\u53ef\u80fd\u65e0\u6cd5\u63d0\u4f9b\u5982\u6b64\u5927\u7684\u8fde\u7eed\u7a7a\u95f4\u3002\u6b64\u65f6\u94fe\u8868\u7684\u7075\u6d3b\u6027\u4f18\u52bf\u5c31\u4f53\u73b0\u51fa\u6765\u4e86\u3002

\u300c\u94fe\u8868 linked list\u300d\u662f\u4e00\u79cd\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a\u8282\u70b9\u5bf9\u8c61\uff0c\u5404\u4e2a\u8282\u70b9\u901a\u8fc7\u201c\u5f15\u7528\u201d\u76f8\u8fde\u63a5\u3002\u5f15\u7528\u8bb0\u5f55\u4e86\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u5185\u5b58\u5730\u5740\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u4ece\u5f53\u524d\u8282\u70b9\u8bbf\u95ee\u5230\u4e0b\u4e00\u4e2a\u8282\u70b9\u3002

\u94fe\u8868\u7684\u8bbe\u8ba1\u4f7f\u5f97\u5404\u4e2a\u8282\u70b9\u53ef\u4ee5\u88ab\u5206\u6563\u5b58\u50a8\u5728\u5185\u5b58\u5404\u5904\uff0c\u5b83\u4eec\u7684\u5185\u5b58\u5730\u5740\u662f\u65e0\u987b\u8fde\u7eed\u7684\u3002

\u56fe 4-5 \u00a0 \u94fe\u8868\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f

\u89c2\u5bdf\u56fe 4-5 \uff0c\u94fe\u8868\u7684\u7ec4\u6210\u5355\u4f4d\u662f\u300c\u8282\u70b9 node\u300d\u5bf9\u8c61\u3002\u6bcf\u4e2a\u8282\u70b9\u90fd\u5305\u542b\u4e24\u9879\u6570\u636e\uff1a\u8282\u70b9\u7684\u201c\u503c\u201d\u548c\u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u201c\u5f15\u7528\u201d\u3002

  • \u94fe\u8868\u7684\u9996\u4e2a\u8282\u70b9\u88ab\u79f0\u4e3a\u201c\u5934\u8282\u70b9\u201d\uff0c\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u88ab\u79f0\u4e3a\u201c\u5c3e\u8282\u70b9\u201d\u3002
  • \u5c3e\u8282\u70b9\u6307\u5411\u7684\u662f\u201c\u7a7a\u201d\uff0c\u5b83\u5728 Java\u3001C++ \u548c Python \u4e2d\u5206\u522b\u88ab\u8bb0\u4e3a \\(\\text{null}\\)\u3001\\(\\text{nullptr}\\) \u548c \\(\\text{None}\\) \u3002
  • \u5728 C\u3001C++\u3001Go \u548c Rust \u7b49\u652f\u6301\u6307\u9488\u7684\u8bed\u8a00\u4e2d\uff0c\u4e0a\u8ff0\u7684\u201c\u5f15\u7528\u201d\u5e94\u88ab\u66ff\u6362\u4e3a\u201c\u6307\u9488\u201d\u3002

\u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u94fe\u8868\u8282\u70b9 ListNode \u9664\u4e86\u5305\u542b\u503c\uff0c\u8fd8\u9700\u989d\u5916\u4fdd\u5b58\u4e00\u4e2a\u5f15\u7528\uff08\u6307\u9488\uff09\u3002\u56e0\u6b64\u5728\u76f8\u540c\u6570\u636e\u91cf\u4e0b\uff0c\u94fe\u8868\u6bd4\u6570\u7ec4\u5360\u7528\u66f4\u591a\u7684\u5185\u5b58\u7a7a\u95f4\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig
class ListNode:\n\"\"\"\u94fe\u8868\u8282\u70b9\u7c7b\"\"\"\ndef __init__(self, val: int):\nself.val: int = val               # \u8282\u70b9\u503c\nself.next: ListNode | None = None # \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\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\nListNode(int x) : val(x), next(nullptr) {}  // \u6784\u9020\u51fd\u6570\n};\n
/* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\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 {\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\u7ed3\u6784\u4f53 */\ntype ListNode struct {\nVal  int       // \u8282\u70b9\u503c\nNext *ListNode // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\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 {\nvar val: Int // \u8282\u70b9\u503c\nvar next: ListNode? // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\ninit(x: Int) { // \u6784\u9020\u51fd\u6570\nval = x\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\u7c7b */\nclass ListNode {\nint val; // \u8282\u70b9\u503c\nListNode? next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\nListNode(this.val, [this.next]); // \u6784\u9020\u51fd\u6570\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n/* \u94fe\u8868\u8282\u70b9\u7c7b */\n#[derive(Debug)]\nstruct ListNode {\nval: i32, // \u8282\u70b9\u503c\nnext: Option<Rc<RefCell<ListNode>>>, // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\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\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\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\n// \u6784\u9020\u51fd\u6570\npub fn init(self: *Self, x: i32) void {\nself.val = x;\nself.next = null;\n}\n};\n}\n
"},{"location":"chapter_array_and_linkedlist/linked_list/#421","title":"4.2.1 \u00a0 \u94fe\u8868\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/linked_list/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u94fe\u8868","text":"

\u5efa\u7acb\u94fe\u8868\u5206\u4e3a\u4e24\u6b65\uff0c\u7b2c\u4e00\u6b65\u662f\u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\u5bf9\u8c61\uff0c\u7b2c\u4e8c\u6b65\u662f\u6784\u5efa\u5f15\u7528\u6307\u5411\u5173\u7cfb\u3002\u521d\u59cb\u5316\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4ece\u94fe\u8868\u7684\u5934\u8282\u70b9\u51fa\u53d1\uff0c\u901a\u8fc7\u5f15\u7528\u6307\u5411 next \u4f9d\u6b21\u8bbf\u95ee\u6240\u6709\u8282\u70b9\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.cs
/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode n0 = new(1);\nListNode n1 = new(3);\nListNode n2 = new(2);\nListNode n3 = new(5);\nListNode n4 = new(4);\n// \u6784\u5efa\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.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.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.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
linked_list.rs
/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nlet n0 = Rc::new(RefCell::new(ListNode { val: 1, next: None }));\nlet n1 = Rc::new(RefCell::new(ListNode { val: 3, next: None }));\nlet n2 = Rc::new(RefCell::new(ListNode { val: 2, next: None }));\nlet n3 = Rc::new(RefCell::new(ListNode { val: 5, next: None }));\nlet n4 = Rc::new(RefCell::new(ListNode { val: 4, next: None }));\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.borrow_mut().next = Some(n1.clone());\nn1.borrow_mut().next = Some(n2.clone());\nn2.borrow_mut().next = Some(n3.clone());\nn3.borrow_mut().next = Some(n4.clone());\n
linked_list.c
/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode* n0 = newListNode(1);\nListNode* n1 = newListNode(3);\nListNode* n2 = newListNode(2);\nListNode* n3 = newListNode(5);\nListNode* n4 = newListNode(4);\n// \u6784\u5efa\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

\u6570\u7ec4\u6574\u4f53\u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u6bd4\u5982\u6570\u7ec4 nums \u5305\u542b\u5143\u7d20 nums[0] \u548c nums[1] \u7b49\uff0c\u800c\u94fe\u8868\u662f\u7531\u591a\u4e2a\u72ec\u7acb\u7684\u8282\u70b9\u5bf9\u8c61\u7ec4\u6210\u7684\u3002\u6211\u4eec\u901a\u5e38\u5c06\u5934\u8282\u70b9\u5f53\u4f5c\u94fe\u8868\u7684\u4ee3\u79f0\uff0c\u6bd4\u5982\u4ee5\u4e0a\u4ee3\u7801\u4e2d\u7684\u94fe\u8868\u53ef\u88ab\u8bb0\u505a\u94fe\u8868 n0 \u3002

"},{"location":"chapter_array_and_linkedlist/linked_list/#2","title":"2. \u00a0 \u63d2\u5165\u8282\u70b9","text":"

\u5728\u94fe\u8868\u4e2d\u63d2\u5165\u8282\u70b9\u975e\u5e38\u5bb9\u6613\u3002\u5982\u56fe 4-6 \u6240\u793a\uff0c\u5047\u8bbe\u6211\u4eec\u60f3\u5728\u76f8\u90bb\u7684\u4e24\u4e2a\u8282\u70b9 n0 \u548c n1 \u4e4b\u95f4\u63d2\u5165\u4e00\u4e2a\u65b0\u8282\u70b9 P \uff0c\u5219\u53ea\u9700\u8981\u6539\u53d8\u4e24\u4e2a\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\u5373\u53ef\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002

\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u5728\u6570\u7ec4\u4e2d\u63d2\u5165\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u5728\u5927\u6570\u636e\u91cf\u4e0b\u7684\u6548\u7387\u8f83\u4f4e\u3002

\u56fe 4-6 \u00a0 \u94fe\u8868\u63d2\u5165\u8282\u70b9\u793a\u4f8b

PythonC++JavaC#GoSwiftJSTSDartRustCZig linked_list.py
def insert(n0: ListNode, P: ListNode):\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.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.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.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.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.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.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.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
linked_list.rs
/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\n#[allow(non_snake_case)]\npub fn insert<T>(n0: &Rc<RefCell<ListNode<T>>>, P: Rc<RefCell<ListNode<T>>>) {\nlet n1 =  n0.borrow_mut().next.take();\nP.borrow_mut().next = n1;\nn0.borrow_mut().next = Some(P);\n}\n
linked_list.c
/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode *n0, ListNode *P) {\nListNode *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
"},{"location":"chapter_array_and_linkedlist/linked_list/#3","title":"3. \u00a0 \u5220\u9664\u8282\u70b9","text":"

\u5982\u56fe 4-7 \u6240\u793a\uff0c\u5728\u94fe\u8868\u4e2d\u5220\u9664\u8282\u70b9\u4e5f\u975e\u5e38\u65b9\u4fbf\uff0c\u53ea\u9700\u6539\u53d8\u4e00\u4e2a\u8282\u70b9\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\u5373\u53ef\u3002

\u8bf7\u6ce8\u610f\uff0c\u5c3d\u7ba1\u5728\u5220\u9664\u64cd\u4f5c\u5b8c\u6210\u540e\u8282\u70b9 P \u4ecd\u7136\u6307\u5411 n1 \uff0c\u4f46\u5b9e\u9645\u4e0a\u904d\u5386\u6b64\u94fe\u8868\u5df2\u7ecf\u65e0\u6cd5\u8bbf\u95ee\u5230 P \uff0c\u8fd9\u610f\u5473\u7740 P \u5df2\u7ecf\u4e0d\u518d\u5c5e\u4e8e\u8be5\u94fe\u8868\u4e86\u3002

\u56fe 4-7 \u00a0 \u94fe\u8868\u5220\u9664\u8282\u70b9

PythonC++JavaC#GoSwiftJSTSDartRustCZig linked_list.py
def remove(n0: ListNode):\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.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.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.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.go
/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunc removeItem(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.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.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.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
linked_list.rs
/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\n#[allow(non_snake_case)]\npub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {\nif n0.borrow().next.is_none() {return};\n// n0 -> P -> n1\nlet P = n0.borrow_mut().next.take();\nif let Some(node) = P {\nlet n1 = node.borrow_mut().next.take();\nn0.borrow_mut().next = n1;\n}\n}\n
linked_list.c
/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nvoid removeItem(ListNode *n0) {\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.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
"},{"location":"chapter_array_and_linkedlist/linked_list/#4","title":"4. \u00a0 \u8bbf\u95ee\u8282\u70b9","text":"

\u5728\u94fe\u8868\u8bbf\u95ee\u8282\u70b9\u7684\u6548\u7387\u8f83\u4f4e\u3002\u5982\u4e0a\u8282\u6240\u8ff0\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u4e0b\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u5143\u7d20\u3002\u94fe\u8868\u5219\u4e0d\u7136\uff0c\u7a0b\u5e8f\u9700\u8981\u4ece\u5934\u8282\u70b9\u51fa\u53d1\uff0c\u9010\u4e2a\u5411\u540e\u904d\u5386\uff0c\u76f4\u81f3\u627e\u5230\u76ee\u6807\u8282\u70b9\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8bbf\u95ee\u94fe\u8868\u7684\u7b2c \\(i\\) \u4e2a\u8282\u70b9\u9700\u8981\u5faa\u73af \\(i - 1\\) \u8f6e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig linked_list.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.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.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.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.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.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.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.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
linked_list.rs
/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\npub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Rc<RefCell<ListNode<T>>> {\nif index <= 0 {return head};\nif let Some(node) = &head.borrow_mut().next {\nreturn access(node.clone(), index - 1);\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.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
"},{"location":"chapter_array_and_linkedlist/linked_list/#5","title":"5. \u00a0 \u67e5\u627e\u8282\u70b9","text":"

\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\u6b64\u8fc7\u7a0b\u4e5f\u5c5e\u4e8e\u7ebf\u6027\u67e5\u627e\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
linked_list.rs
/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\npub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T, index: i32) -> i32 {\nif head.borrow().val == target {return index};\nif let Some(node) = &head.borrow_mut().next {\nreturn find(node.clone(), target, index + 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.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
"},{"location":"chapter_array_and_linkedlist/linked_list/#422-vs","title":"4.2.2 \u00a0 \u6570\u7ec4 VS \u94fe\u8868","text":"

\u8868 4-1 \u603b\u7ed3\u5bf9\u6bd4\u4e86\u6570\u7ec4\u548c\u94fe\u8868\u7684\u5404\u9879\u7279\u70b9\u4e0e\u64cd\u4f5c\u6548\u7387\u3002\u7531\u4e8e\u5b83\u4eec\u91c7\u7528\u4e24\u79cd\u76f8\u53cd\u7684\u5b58\u50a8\u7b56\u7565\uff0c\u56e0\u6b64\u5404\u79cd\u6027\u8d28\u548c\u64cd\u4f5c\u6548\u7387\u4e5f\u5448\u73b0\u5bf9\u7acb\u7684\u7279\u70b9\u3002

\u8868 4-1 \u00a0 \u6570\u7ec4\u4e0e\u94fe\u8868\u7684\u6548\u7387\u5bf9\u6bd4

\u6570\u7ec4 \u94fe\u8868 \u5b58\u50a8\u65b9\u5f0f \u8fde\u7eed\u5185\u5b58\u7a7a\u95f4 \u5206\u6563\u5185\u5b58\u7a7a\u95f4 \u7f13\u5b58\u5c40\u90e8\u6027 \u53cb\u597d \u4e0d\u53cb\u597d \u5bb9\u91cf\u6269\u5c55 \u957f\u5ea6\u4e0d\u53ef\u53d8 \u53ef\u7075\u6d3b\u6269\u5c55 \u5185\u5b58\u6548\u7387 \u5360\u7528\u5185\u5b58\u5c11\u3001\u6d6a\u8d39\u90e8\u5206\u7a7a\u95f4 \u5360\u7528\u5185\u5b58\u591a \u8bbf\u95ee\u5143\u7d20 \\(O(1)\\) \\(O(n)\\) \u6dfb\u52a0\u5143\u7d20 \\(O(n)\\) \\(O(1)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(1)\\)"},{"location":"chapter_array_and_linkedlist/linked_list/#423","title":"4.2.3 \u00a0 \u5e38\u89c1\u94fe\u8868\u7c7b\u578b","text":"

\u5982\u56fe 4-8 \u6240\u793a\uff0c\u5e38\u89c1\u7684\u94fe\u8868\u7c7b\u578b\u5305\u62ec\u4e09\u79cd\u3002

  • \u5355\u5411\u94fe\u8868\uff1a\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\u5f15\u7528\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\u79f0\u4e3a\u5c3e\u8282\u70b9\uff0c\u5c3e\u8282\u70b9\u6307\u5411\u7a7a \\(\\text{None}\\) \u3002
  • \u73af\u5f62\u94fe\u8868\uff1a\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\uff1a\u4e0e\u5355\u5411\u94fe\u8868\u76f8\u6bd4\uff0c\u53cc\u5411\u94fe\u8868\u8bb0\u5f55\u4e86\u4e24\u4e2a\u65b9\u5411\u7684\u5f15\u7528\u3002\u53cc\u5411\u94fe\u8868\u7684\u8282\u70b9\u5b9a\u4e49\u540c\u65f6\u5305\u542b\u6307\u5411\u540e\u7ee7\u8282\u70b9\uff08\u4e0b\u4e00\u4e2a\u8282\u70b9\uff09\u548c\u524d\u9a71\u8282\u70b9\uff08\u4e0a\u4e00\u4e2a\u8282\u70b9\uff09\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\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
PythonC++JavaC#GoSwiftJSTSDartRustCZig
class ListNode:\n\"\"\"\u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b\"\"\"\ndef __init__(self, val: int):\nself.val: int = val                # \u8282\u70b9\u503c\nself.next: ListNode | None = None  # \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\nself.prev: ListNode | None = None  # \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct ListNode {\nint val;         // \u8282\u70b9\u503c\nListNode *next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\nListNode *prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\nListNode(int x) : val(x), next(nullptr), prev(nullptr) {}  // \u6784\u9020\u51fd\u6570\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\u5f15\u7528\nListNode prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\nListNode(int x) { val = x; }  // \u6784\u9020\u51fd\u6570\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\u5f15\u7528\nListNode prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\nListNode(int x) => val = x;  // \u6784\u9020\u51fd\u6570\n}\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\nPrev *DoublyListNode // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\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 {\nvar val: Int // \u8282\u70b9\u503c\nvar next: ListNode? // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\nvar prev: ListNode? // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\ninit(x: Int) { // \u6784\u9020\u51fd\u6570\nval = x\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\u5f15\u7528\nthis.prev = prev  ===  undefined ? null : prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n}\n}\n
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\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\u5f15\u7528\nthis.prev = prev  ===  undefined ? null : prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n}\n}\n
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nint val;        // \u8282\u70b9\u503c\nListNode next;  // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\nListNode prev;  // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\nListNode(this.val, [this.next, this.prev]);  // \u6784\u9020\u51fd\u6570\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b\u578b */\n#[derive(Debug)]\nstruct ListNode {\nval: i32, // \u8282\u70b9\u503c\nnext: Option<Rc<RefCell<ListNode>>>, // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\nprev: Option<Rc<RefCell<ListNode>>>, // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n}\n/* \u6784\u9020\u51fd\u6570 */\nimpl ListNode {\nfn new(val: i32) -> Self {\nListNode {\nval,\nnext: None,\nprev: None,\n}\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\nstruct ListNode *prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\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\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\nprev: ?*Self = null, // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\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

\u56fe 4-8 \u00a0 \u5e38\u89c1\u94fe\u8868\u79cd\u7c7b

"},{"location":"chapter_array_and_linkedlist/linked_list/#424","title":"4.2.4 \u00a0 \u94fe\u8868\u5178\u578b\u5e94\u7528","text":"

\u5355\u5411\u94fe\u8868\u901a\u5e38\u7528\u4e8e\u5b9e\u73b0\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u548c\u56fe\u7b49\u6570\u636e\u7ed3\u6784\u3002

  • \u6808\u4e0e\u961f\u5217\uff1a\u5f53\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u90fd\u5728\u94fe\u8868\u7684\u4e00\u7aef\u8fdb\u884c\u65f6\uff0c\u5b83\u8868\u73b0\u51fa\u5148\u8fdb\u540e\u51fa\u7684\u7684\u7279\u6027\uff0c\u5bf9\u5e94\u6808\uff1b\u5f53\u63d2\u5165\u64cd\u4f5c\u5728\u94fe\u8868\u7684\u4e00\u7aef\u8fdb\u884c\uff0c\u5220\u9664\u64cd\u4f5c\u5728\u94fe\u8868\u7684\u53e6\u4e00\u7aef\u8fdb\u884c\uff0c\u5b83\u8868\u73b0\u51fa\u5148\u8fdb\u5148\u51fa\u7684\u7279\u6027\uff0c\u5bf9\u5e94\u961f\u5217\u3002
  • \u54c8\u5e0c\u8868\uff1a\u94fe\u5730\u5740\u6cd5\u662f\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u7684\u4e3b\u6d41\u65b9\u6848\u4e4b\u4e00\uff0c\u5728\u8be5\u65b9\u6848\u4e2d\uff0c\u6240\u6709\u51b2\u7a81\u7684\u5143\u7d20\u90fd\u4f1a\u88ab\u653e\u5230\u4e00\u4e2a\u94fe\u8868\u4e2d\u3002
  • \u56fe\uff1a\u90bb\u63a5\u8868\u662f\u8868\u793a\u56fe\u7684\u4e00\u79cd\u5e38\u7528\u65b9\u5f0f\uff0c\u5728\u5176\u4e2d\uff0c\u56fe\u7684\u6bcf\u4e2a\u9876\u70b9\u90fd\u4e0e\u4e00\u4e2a\u94fe\u8868\u76f8\u5173\u8054\uff0c\u94fe\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u4ee3\u8868\u4e0e\u8be5\u9876\u70b9\u76f8\u8fde\u7684\u5176\u4ed6\u9876\u70b9\u3002

\u53cc\u5411\u94fe\u8868\u5e38\u88ab\u7528\u4e8e\u9700\u8981\u5feb\u901f\u67e5\u627e\u524d\u4e00\u4e2a\u548c\u4e0b\u4e00\u4e2a\u5143\u7d20\u7684\u573a\u666f\u3002

  • \u9ad8\u7ea7\u6570\u636e\u7ed3\u6784\uff1a\u6bd4\u5982\u5728\u7ea2\u9ed1\u6811\u3001B \u6811\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u8bbf\u95ee\u8282\u70b9\u7684\u7236\u8282\u70b9\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5728\u8282\u70b9\u4e2d\u4fdd\u5b58\u4e00\u4e2a\u6307\u5411\u7236\u8282\u70b9\u7684\u5f15\u7528\u6765\u5b9e\u73b0\uff0c\u7c7b\u4f3c\u4e8e\u53cc\u5411\u94fe\u8868\u3002
  • \u6d4f\u89c8\u5668\u5386\u53f2\uff1a\u5728\u7f51\u9875\u6d4f\u89c8\u5668\u4e2d\uff0c\u5f53\u7528\u6237\u70b9\u51fb\u524d\u8fdb\u6216\u540e\u9000\u6309\u94ae\u65f6\uff0c\u6d4f\u89c8\u5668\u9700\u8981\u77e5\u9053\u7528\u6237\u8bbf\u95ee\u8fc7\u7684\u524d\u4e00\u4e2a\u548c\u540e\u4e00\u4e2a\u7f51\u9875\u3002\u53cc\u5411\u94fe\u8868\u7684\u7279\u6027\u4f7f\u5f97\u8fd9\u79cd\u64cd\u4f5c\u53d8\u5f97\u7b80\u5355\u3002
  • LRU \u7b97\u6cd5\uff1a\u5728\u7f13\u5b58\u6dd8\u6c70\u7b97\u6cd5\uff08LRU\uff09\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5feb\u901f\u627e\u5230\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684\u6570\u636e\uff0c\u4ee5\u53ca\u652f\u6301\u5feb\u901f\u5730\u6dfb\u52a0\u548c\u5220\u9664\u8282\u70b9\u3002\u8fd9\u65f6\u5019\u4f7f\u7528\u53cc\u5411\u94fe\u8868\u5c31\u975e\u5e38\u5408\u9002\u3002

\u5faa\u73af\u94fe\u8868\u5e38\u88ab\u7528\u4e8e\u9700\u8981\u5468\u671f\u6027\u64cd\u4f5c\u7684\u573a\u666f\uff0c\u6bd4\u5982\u64cd\u4f5c\u7cfb\u7edf\u7684\u8d44\u6e90\u8c03\u5ea6\u3002

  • \u65f6\u95f4\u7247\u8f6e\u8f6c\u8c03\u5ea6\u7b97\u6cd5\uff1a\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u65f6\u95f4\u7247\u8f6e\u8f6c\u8c03\u5ea6\u7b97\u6cd5\u662f\u4e00\u79cd\u5e38\u89c1\u7684 CPU \u8c03\u5ea6\u7b97\u6cd5\uff0c\u5b83\u9700\u8981\u5bf9\u4e00\u7ec4\u8fdb\u7a0b\u8fdb\u884c\u5faa\u73af\u3002\u6bcf\u4e2a\u8fdb\u7a0b\u88ab\u8d4b\u4e88\u4e00\u4e2a\u65f6\u95f4\u7247\uff0c\u5f53\u65f6\u95f4\u7247\u7528\u5b8c\u65f6\uff0cCPU \u5c06\u5207\u6362\u5230\u4e0b\u4e00\u4e2a\u8fdb\u7a0b\u3002\u8fd9\u79cd\u5faa\u73af\u7684\u64cd\u4f5c\u5c31\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u94fe\u8868\u6765\u5b9e\u73b0\u3002
  • \u6570\u636e\u7f13\u51b2\u533a\uff1a\u5728\u67d0\u4e9b\u6570\u636e\u7f13\u51b2\u533a\u7684\u5b9e\u73b0\u4e2d\uff0c\u4e5f\u53ef\u80fd\u4f1a\u4f7f\u7528\u5230\u5faa\u73af\u94fe\u8868\u3002\u6bd4\u5982\u5728\u97f3\u9891\u3001\u89c6\u9891\u64ad\u653e\u5668\u4e2d\uff0c\u6570\u636e\u6d41\u53ef\u80fd\u4f1a\u88ab\u5206\u6210\u591a\u4e2a\u7f13\u51b2\u5757\u5e76\u653e\u5165\u4e00\u4e2a\u5faa\u73af\u94fe\u8868\uff0c\u4ee5\u4fbf\u5b9e\u73b0\u65e0\u7f1d\u64ad\u653e\u3002
"},{"location":"chapter_array_and_linkedlist/list/","title":"4.3 \u00a0 \u5217\u8868","text":"

\u300c\u5217\u8868 list\u300d\u662f\u4e00\u4e2a\u62bd\u8c61\u7684\u6570\u636e\u7ed3\u6784\u6982\u5ff5\uff0c\u5b83\u8868\u793a\u5143\u7d20\u7684\u6709\u5e8f\u96c6\u5408\uff0c\u652f\u6301\u5143\u7d20\u8bbf\u95ee\u3001\u4fee\u6539\u3001\u6dfb\u52a0\u3001\u5220\u9664\u548c\u904d\u5386\u7b49\u64cd\u4f5c\uff0c\u65e0\u9700\u4f7f\u7528\u8005\u8003\u8651\u5bb9\u91cf\u9650\u5236\u7684\u95ee\u9898\u3002\u5217\u8868\u53ef\u4ee5\u57fa\u4e8e\u94fe\u8868\u6216\u6570\u7ec4\u5b9e\u73b0\u3002

  • \u94fe\u8868\u5929\u7136\u53ef\u4ee5\u88ab\u770b\u4f5c\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u652f\u6301\u5143\u7d20\u589e\u5220\u67e5\u6539\u64cd\u4f5c\uff0c\u5e76\u4e14\u53ef\u4ee5\u7075\u6d3b\u52a8\u6001\u6269\u5bb9\u3002
  • \u6570\u7ec4\u4e5f\u652f\u6301\u5143\u7d20\u589e\u5220\u67e5\u6539\uff0c\u4f46\u7531\u4e8e\u5176\u957f\u5ea6\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u53ea\u80fd\u88ab\u770b\u4f5c\u662f\u4e00\u4e2a\u5177\u6709\u957f\u5ea6\u9650\u5236\u7684\u5217\u8868\u3002

\u5f53\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u5217\u8868\u65f6\uff0c\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6027\u8d28\u4f1a\u5bfc\u81f4\u5217\u8868\u7684\u5b9e\u7528\u6027\u964d\u4f4e\u3002\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u901a\u5e38\u65e0\u6cd5\u4e8b\u5148\u786e\u5b9a\u9700\u8981\u5b58\u50a8\u591a\u5c11\u6570\u636e\uff0c\u4ece\u800c\u96be\u4ee5\u9009\u62e9\u5408\u9002\u7684\u5217\u8868\u957f\u5ea6\u3002\u82e5\u957f\u5ea6\u8fc7\u5c0f\uff0c\u5219\u5f88\u53ef\u80fd\u65e0\u6cd5\u6ee1\u8db3\u4f7f\u7528\u9700\u6c42\uff1b\u82e5\u957f\u5ea6\u8fc7\u5927\uff0c\u5219\u4f1a\u9020\u6210\u5185\u5b58\u7a7a\u95f4\u7684\u6d6a\u8d39\u3002

\u4e3a\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u300c\u52a8\u6001\u6570\u7ec4 dynamic array\u300d\u6765\u5b9e\u73b0\u5217\u8868\u3002\u5b83\u7ee7\u627f\u4e86\u6570\u7ec4\u7684\u5404\u9879\u4f18\u70b9\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u52a8\u6001\u6269\u5bb9\u3002

\u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u6807\u51c6\u5e93\u63d0\u4f9b\u7684\u5217\u8868\u90fd\u662f\u57fa\u4e8e\u52a8\u6001\u6570\u7ec4\u5b9e\u73b0\u7684\uff0c\u4f8b\u5982 Python \u4e2d\u7684 list \u3001Java \u4e2d\u7684 ArrayList \u3001C++ \u4e2d\u7684 vector \u548c C# \u4e2d\u7684 List \u7b49\u3002\u5728\u63a5\u4e0b\u6765\u7684\u8ba8\u8bba\u4e2d\uff0c\u6211\u4eec\u5c06\u628a\u201c\u5217\u8868\u201d\u548c\u201c\u52a8\u6001\u6570\u7ec4\u201d\u89c6\u4e3a\u7b49\u540c\u7684\u6982\u5ff5\u3002

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

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

PythonC++JavaC#GoSwiftJSTSDartRustCZig list.py
# \u521d\u59cb\u5316\u5217\u8868\n# \u65e0\u521d\u59cb\u503c\nnums1: nums[int] = []\n# \u6709\u521d\u59cb\u503c\nnums: nums[int] = [1, 3, 2, 5, 4]\n
list.cpp
/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u9700\u6ce8\u610f\uff0cC++ \u4e2d vector \u5373\u662f\u672c\u6587\u63cf\u8ff0\u7684 nums\n// \u65e0\u521d\u59cb\u503c\nvector<int> nums1;\n// \u6709\u521d\u59cb\u503c\nvector<int> nums = { 1, 3, 2, 5, 4 };\n
list.java
/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<Integer> nums1 = new ArrayList<>();\n// \u6709\u521d\u59cb\u503c\uff08\u6ce8\u610f\u6570\u7ec4\u7684\u5143\u7d20\u7c7b\u578b\u9700\u4e3a int[] \u7684\u5305\u88c5\u7c7b Integer[]\uff09\nInteger[] numbers = new Integer[] { 1, 3, 2, 5, 4 };\nList<Integer> nums = new ArrayList<>(Arrays.asList(numbers));\n
list.cs
/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<int> nums1 = new();\n// \u6709\u521d\u59cb\u503c\nint[] numbers = new int[] { 1, 3, 2, 5, 4 };\nList<int> nums = numbers.ToList();\n
list_test.go
/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nnums1 := []int\n// \u6709\u521d\u59cb\u503c\nnums := []int{1, 3, 2, 5, 4}\n
list.swift
/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nlet nums1: [Int] = []\n// \u6709\u521d\u59cb\u503c\nvar nums = [1, 3, 2, 5, 4]\n
list.js
/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nconst nums1 = [];\n// \u6709\u521d\u59cb\u503c\nconst nums = [1, 3, 2, 5, 4];\n
list.ts
/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nconst nums1: number[] = [];\n// \u6709\u521d\u59cb\u503c\nconst nums: number[] = [1, 3, 2, 5, 4];\n
list.dart
/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<int> nums1 = [];\n// \u6709\u521d\u59cb\u503c\nList<int> nums = [1, 3, 2, 5, 4];\n
list.rs
/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nlet nums1: Vec<i32> = Vec::new();\n// \u6709\u521d\u59cb\u503c\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
list.c
// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
list.zig
// \u521d\u59cb\u5316\u5217\u8868\nvar nums = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer nums.deinit();\ntry nums.appendSlice(&[_]i32{ 1, 3, 2, 5, 4 });\n
"},{"location":"chapter_array_and_linkedlist/list/#2","title":"2. \u00a0 \u8bbf\u95ee\u5143\u7d20","text":"

\u5217\u8868\u672c\u8d28\u4e0a\u662f\u6570\u7ec4\uff0c\u56e0\u6b64\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u8bbf\u95ee\u548c\u66f4\u65b0\u5143\u7d20\uff0c\u6548\u7387\u5f88\u9ad8\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig list.py
# \u8bbf\u95ee\u5143\u7d20\nnum: int = nums[1]  # \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n# \u66f4\u65b0\u5143\u7d20\nnums[1] = 0    # \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.cpp
/* \u8bbf\u95ee\u5143\u7d20 */\nint num = nums[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.java
/* \u8bbf\u95ee\u5143\u7d20 */\nint num = nums.get(1);  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums.set(1, 0);  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.cs
/* \u8bbf\u95ee\u5143\u7d20 */\nint num = nums[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list_test.go
/* \u8bbf\u95ee\u5143\u7d20 */\nnum := nums[1]  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0     // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.swift
/* \u8bbf\u95ee\u5143\u7d20 */\nlet num = nums[1] // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0 // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.js
/* \u8bbf\u95ee\u5143\u7d20 */\nconst num = nums[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.ts
/* \u8bbf\u95ee\u5143\u7d20 */\nconst num: number = nums[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.dart
/* \u8bbf\u95ee\u5143\u7d20 */\nint num = nums[1];  // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0;  // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.rs
/* \u8bbf\u95ee\u5143\u7d20 */\nlet num: i32 = nums[1];    // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0;               // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.c
// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
list.zig
// \u8bbf\u95ee\u5143\u7d20\nvar num = nums.items[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n// \u66f4\u65b0\u5143\u7d20\nnums.items[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0  \n
"},{"location":"chapter_array_and_linkedlist/list/#3","title":"3. \u00a0 \u63d2\u5165\u4e0e\u5220\u9664\u5143\u7d20","text":"

\u76f8\u8f83\u4e8e\u6570\u7ec4\uff0c\u5217\u8868\u53ef\u4ee5\u81ea\u7531\u5730\u6dfb\u52a0\u4e0e\u5220\u9664\u5143\u7d20\u3002\u5728\u5217\u8868\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff0c\u4f46\u63d2\u5165\u548c\u5220\u9664\u5143\u7d20\u7684\u6548\u7387\u4ecd\u4e0e\u6570\u7ec4\u76f8\u540c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

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

\u4e0e\u6570\u7ec4\u4e00\u6837\uff0c\u5217\u8868\u53ef\u4ee5\u6839\u636e\u7d22\u5f15\u904d\u5386\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u904d\u5386\u5404\u5143\u7d20\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig list.py
# \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868\ncount = 0\nfor i in range(len(nums)):\ncount += 1\n# \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20\ncount = 0\nfor num in nums:\ncount += 1\n
list.cpp
/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (int num : nums) {\ncount++;\n}\n
list.java
/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (int num : nums) {\ncount++;\n}\n
list.cs
/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < nums.Count; i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nforeach (int num in nums) {\ncount++;\n}\n
list_test.go
/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\ncount := 0\nfor i := 0; i < len(nums); i++ {\ncount++\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0\nfor range nums {\ncount++\n}\n
list.swift
/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nvar count = 0\nfor _ in nums.indices {\ncount += 1\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0\nfor _ in nums {\ncount += 1\n}\n
list.js
/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (const num of nums) {\ncount++;\n}\n
list.ts
/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (const num of nums) {\ncount++;\n}\n
list.dart
/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < nums.length; i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (int num in nums) {\ncount++;\n}\n
list.rs
/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nlet mut count = 0;\nfor (index, value) in nums.iter().enumerate() {\ncount += 1;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\nlet mut count = 0;\nfor value in nums.iter() {\ncount += 1;\n}\n
list.c
// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
list.zig
// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868\nvar count: i32 = 0;\nvar i: i32 = 0;\nwhile (i < nums.items.len) : (i += 1) {\ncount += 1;\n}\n// \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20\ncount = 0;\nfor (nums.items) |_| {\ncount += 1;\n}\n
"},{"location":"chapter_array_and_linkedlist/list/#5","title":"5. \u00a0 \u62fc\u63a5\u5217\u8868","text":"

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

PythonC++JavaC#GoSwiftJSTSDartRustCZig list.py
# \u62fc\u63a5\u4e24\u4e2a\u5217\u8868\nnums1: nums[int] = [6, 8, 7, 10, 9]\nnums += nums1  # \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.cpp
/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nvector<int> nums1 = { 6, 8, 7, 10, 9 };\n// \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\nnums.insert(nums.end(), nums1.begin(), nums1.end());\n
list.java
/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<Integer> nums1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));\nnums.addAll(nums1);  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.cs
/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<int> nums1 = new() { 6, 8, 7, 10, 9 };\nnums.AddRange(nums1);  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list_test.go
/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nnums1 := []int{6, 8, 7, 10, 9}\nnums = append(nums, nums1...)  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.swift
/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nlet nums1 = [6, 8, 7, 10, 9]\nnums.append(contentsOf: nums1) // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.js
/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nconst nums1 = [6, 8, 7, 10, 9];\nnums.push(...nums1);  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.ts
/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nconst nums1: number[] = [6, 8, 7, 10, 9];\nnums.push(...nums1);  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.dart
/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<int> nums1 = [6, 8, 7, 10, 9];\nnums.addAll(nums1);  // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.rs
/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nlet nums1: Vec<i32> = vec![6, 8, 7, 10, 9];\nnums.extend(nums1);\n
list.c
// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
list.zig
// \u62fc\u63a5\u4e24\u4e2a\u5217\u8868\nvar nums1 = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer nums1.deinit();\ntry nums1.appendSlice(&[_]i32{ 6, 8, 7, 10, 9 });\ntry nums.insertSlice(nums.items.len, nums1.items); // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
"},{"location":"chapter_array_and_linkedlist/list/#6","title":"6. \u00a0 \u6392\u5e8f\u5217\u8868","text":"

\u5b8c\u6210\u5217\u8868\u6392\u5e8f\u540e\uff0c\u6211\u4eec\u4fbf\u53ef\u4ee5\u4f7f\u7528\u5728\u6570\u7ec4\u7c7b\u7b97\u6cd5\u9898\u4e2d\u7ecf\u5e38\u8003\u5bdf\u7684\u201c\u4e8c\u5206\u67e5\u627e\u201d\u548c\u201c\u53cc\u6307\u9488\u201d\u7b97\u6cd5\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig list.py
# \u6392\u5e8f\u5217\u8868\nnums.sort()  # \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.cpp
/* \u6392\u5e8f\u5217\u8868 */\nsort(nums.begin(), nums.end());  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.java
/* \u6392\u5e8f\u5217\u8868 */\nCollections.sort(nums);  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.cs
/* \u6392\u5e8f\u5217\u8868 */\nnums.Sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list_test.go
/* \u6392\u5e8f\u5217\u8868 */\nsort.Ints(nums)  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.swift
/* \u6392\u5e8f\u5217\u8868 */\nnums.sort() // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.js
/* \u6392\u5e8f\u5217\u8868 */  nums.sort((a, b) => a - b);  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.ts
/* \u6392\u5e8f\u5217\u8868 */\nnums.sort((a, b) => a - b);  // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.dart
/* \u6392\u5e8f\u5217\u8868 */\nnums.sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.rs
/* \u6392\u5e8f\u5217\u8868 */\nnums.sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.c
// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
list.zig
// \u6392\u5e8f\u5217\u8868\nstd.sort.sort(i32, nums.items, {}, comptime std.sort.asc(i32));\n
"},{"location":"chapter_array_and_linkedlist/list/#432","title":"4.3.2 \u00a0 \u5217\u8868\u5b9e\u73b0","text":"

\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u90fd\u63d0\u4f9b\u5185\u7f6e\u7684\u5217\u8868\uff0c\u4f8b\u5982 Java\u3001C++\u3001Python \u7b49\u3002\u5b83\u4eec\u7684\u5b9e\u73b0\u6bd4\u8f83\u590d\u6742\uff0c\u5404\u4e2a\u53c2\u6570\u7684\u8bbe\u5b9a\u4e5f\u975e\u5e38\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

\u4e3a\u4e86\u52a0\u6df1\u5bf9\u5217\u8868\u5de5\u4f5c\u539f\u7406\u7684\u7406\u89e3\uff0c\u6211\u4eec\u5c1d\u8bd5\u5b9e\u73b0\u4e00\u4e2a\u7b80\u6613\u7248\u5217\u8868\uff0c\u5305\u62ec\u4ee5\u4e0b\u4e09\u4e2a\u91cd\u70b9\u8bbe\u8ba1\u3002

  • \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\u82e5\u63d2\u5165\u5143\u7d20\u65f6\u5217\u8868\u5bb9\u91cf\u5df2\u6ee1\uff0c\u5219\u9700\u8981\u8fdb\u884c\u6269\u5bb9\u3002\u9996\u5148\u6839\u636e\u6269\u5bb9\u500d\u6570\u521b\u5efa\u4e00\u4e2a\u66f4\u5927\u7684\u6570\u7ec4\uff0c\u518d\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
PythonC++JavaC#GoSwiftJSTSDartRustCZig 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._arr: 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._arr[index]\ndef set(self, num: int, index: int):\n\"\"\"\u66f4\u65b0\u5143\u7d20\"\"\"\nif index < 0 or index >= self._size:\nraise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\nself._arr[index] = num\ndef add(self, num: int):\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._arr[self._size] = num\nself._size += 1\ndef insert(self, num: int, index: int):\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# \u5c06\u7d22\u5f15 index \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._arr[j + 1] = self._arr[j]\nself._arr[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._arr[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._arr[j] = self._arr[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):\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._arr = self._arr + [0] * self.capacity() * (self._extend_ratio - 1)\n# \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nself._capacity = len(self._arr)\ndef to_array(self) -> list[int]:\n\"\"\"\u8fd4\u56de\u6709\u6548\u957f\u5ea6\u7684\u5217\u8868\"\"\"\nreturn self._arr[: self._size]\n
my_list.cpp
/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate:\nint *arr;             // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nint arrCapacity = 10; // \u5217\u8868\u5bb9\u91cf\nint arrSize = 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() {\narr = new int[arrCapacity];\n}\n/* \u6790\u6784\u65b9\u6cd5 */\n~MyList() {\ndelete[] arr;\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\nint size() {\nreturn arrSize;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nint capacity() {\nreturn arrCapacity;\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 arr[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\");\narr[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();\narr[size()] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\narrSize++;\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// \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--) {\narr[j + 1] = arr[j];\n}\narr[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\narrSize++;\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 = arr[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++) {\narr[j] = arr[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\narrSize--;\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 = arr;\narr = 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++) {\narr[i] = tmp[i];\n}\n// \u91ca\u653e\u5185\u5b58\ndelete[] tmp;\narrCapacity = 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] = arr[i];\n}\nreturn vec;\n}\n};\n
my_list.java
/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate int[] arr; // \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() {\narr = 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 arr[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\");\narr[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();\narr[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--) {\narr[j + 1] = arr[j];\n}\narr[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 = arr[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++) {\narr[j] = arr[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\narr = Arrays.copyOf(arr, capacity() * extendRatio);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\ncapacity = arr.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[] arr = new int[size];\nfor (int i = 0; i < size; i++) {\narr[i] = get(i);\n}\nreturn arr;\n}\n}\n
my_list.cs
/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate int[] arr;           // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nprivate int arrCapacity = 10;    // \u5217\u8868\u5bb9\u91cf\nprivate int arrSize = 0;         // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nprivate readonly int extendRatio = 2;  // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n/* \u6784\u9020\u65b9\u6cd5 */\npublic MyList() {\narr = new int[arrCapacity];\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\npublic int Size() {\nreturn arrSize;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\npublic int Capacity() {\nreturn arrCapacity;\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 >= arrSize)\nthrow new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\nreturn arr[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\npublic void Set(int index, int num) {\nif (index < 0 || index >= arrSize)\nthrow new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\narr[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 (arrSize == arrCapacity)\nExtendCapacity();\narr[arrSize] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\narrSize++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\npublic void Insert(int index, int num) {\nif (index < 0 || index >= arrSize)\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 (arrSize == arrCapacity)\nExtendCapacity();\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (int j = arrSize - 1; j >= index; j--) {\narr[j + 1] = arr[j];\n}\narr[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\narrSize++;\n}\n/* \u5220\u9664\u5143\u7d20 */\npublic int Remove(int index) {\nif (index < 0 || index >= arrSize)\nthrow new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\nint num = arr[index];\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (int j = index; j < arrSize - 1; j++) {\narr[j] = arr[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\narrSize--;\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 arrCapacity * extendRatio \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nArray.Resize(ref arr, arrCapacity * extendRatio);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\narrCapacity = arr.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[] arr = new int[arrSize];\nfor (int i = 0; i < arrSize; i++) {\narr[i] = Get(i);\n}\nreturn arr;\n}\n}\n
my_list.go
/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\ntype myList struct {\narrCapacity int\narr         []int\narrSize     int\nextendRatio int\n}\n/* \u6784\u9020\u51fd\u6570 */\nfunc newMyList() *myList {\nreturn &myList{\narrCapacity: 10,              // \u5217\u8868\u5bb9\u91cf\narr:         make([]int, 10), // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\narrSize:     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.arrSize\n}\n/*  \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nfunc (l *myList) capacity() int {\nreturn l.arrCapacity\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.arrSize {\npanic(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nreturn l.arr[index]\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nfunc (l *myList) set(num, index int) {\nif index < 0 || index >= l.arrSize {\npanic(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nl.arr[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.arrSize == l.arrCapacity {\nl.extendCapacity()\n}\nl.arr[l.arrSize] = num\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nl.arrSize++\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nfunc (l *myList) insert(num, index int) {\nif index < 0 || index >= l.arrSize {\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.arrSize == l.arrCapacity {\nl.extendCapacity()\n}\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor j := l.arrSize - 1; j >= index; j-- {\nl.arr[j+1] = l.arr[j]\n}\nl.arr[index] = num\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nl.arrSize++\n}\n/* \u5220\u9664\u5143\u7d20 */\nfunc (l *myList) remove(index int) int {\nif index < 0 || index >= l.arrSize {\npanic(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nnum := l.arr[index]\n// \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor j := index; j < l.arrSize-1; j++ {\nl.arr[j] = l.arr[j+1]\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nl.arrSize--\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.arr = append(l.arr, make([]int, l.arrCapacity*(l.extendRatio-1))...)\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nl.arrCapacity = len(l.arr)\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.arr[:l.arrSize]\n}\n
my_list.swift
/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate var arr: [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() {\narr = 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 arr[index]\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nfunc set(index: Int, num: Int) {\nif index < 0 || index >= _size {\nfatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n}\narr[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}\narr[_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 }) {\narr[j + 1] = arr[j]\n}\narr[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 = arr[index]\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor j in index ..< (_size - 1) {\narr[j] = arr[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\narr = arr + Array(repeating: 0, count: _capacity * (extendRatio - 1))\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n_capacity = arr.count\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\nfunc toArray() -> [Int] {\nvar arr = Array(repeating: 0, count: _size)\nfor i in 0 ..< _size {\narr[i] = get(index: i)\n}\nreturn arr\n}\n}\n
my_list.js
/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\n#arr = new Array(); // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n#capacity = 10; // \u5217\u8868\u5bb9\u91cf\n#size = 0; // \u5217\u8868\u957f\u5ea6\uff08\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.#arr = 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.#arr[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nset(index, num) {\nif (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\nthis.#arr[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.#arr[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.#arr[j + 1] = this.#arr[j];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nthis.#arr[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.#arr[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.#arr[j] = this.#arr[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.#arr = this.#arr.concat(\nnew Array(this.capacity() * (this.#extendRatio - 1))\n);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nthis.#capacity = this.#arr.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 arr = new Array(size);\nfor (let i = 0; i < size; i++) {\narr[i] = this.get(i);\n}\nreturn arr;\n}\n}\n
my_list.ts
/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate arr: 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.arr = 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.arr[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.arr[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.arr[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.arr[j + 1] = this.arr[j];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nthis.arr[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.arr[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.arr[j] = this.arr[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.arr = this.arr.concat(\nnew Array(this.capacity() * (this.extendRatio - 1))\n);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nthis._capacity = this.arr.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 arr = new Array(size);\nfor (let i = 0; i < size; i++) {\narr[i] = this.get(i);\n}\nreturn arr;\n}\n}\n
my_list.dart
/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nlate List<int> _arr; // \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_arr = 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 _arr[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nvoid set(int index, int num) {\nif (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n_arr[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_arr[_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_arr[j + 1] = _arr[j];\n}\n_arr[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 = _arr[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_arr[j] = _arr[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, _arr);\n// \u66f4\u65b0 _arr \u7684\u5f15\u7528\n_arr = _newNums;\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n_capacity = _arr.length;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\nList<int> toArray() {\nList<int> arr = [];\nfor (var i = 0; i < _size; i++) {\narr.add(get(i));\n}\nreturn arr;\n}\n}\n
my_list.rs
/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\n#[allow(dead_code)]\nstruct MyList {\narr: Vec<i32>,       // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\ncapacity: usize,      // \u5217\u8868\u5bb9\u91cf\nsize: usize,          // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nextend_ratio: usize,  // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n}\n#[allow(unused,unused_comparisons)]\nimpl MyList {\n/* \u6784\u9020\u65b9\u6cd5 */\npub fn new(capacity: usize) -> Self {\nlet mut vec = Vec::new(); vec.resize(capacity, 0);\nSelf {\narr: vec,\ncapacity,\nsize: 0,\nextend_ratio: 2,\n}\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\npub fn size(&self) -> usize {\nreturn self.size;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\npub fn capacity(&self) -> usize {\nreturn self.capacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\npub fn get(&self, index: usize) -> i32 {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif index < 0 || index >= self.size {panic!(\"\u7d22\u5f15\u8d8a\u754c\")};\nreturn self.arr[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\npub fn set(&mut self, index: usize, num: i32) {\nif index < 0 || index >= self.size {panic!(\"\u7d22\u5f15\u8d8a\u754c\")};\nself.arr[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\npub fn add(&mut self, num: i32) {\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}\nself.arr[self.size] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.size += 1;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\npub fn insert(&mut self, index: usize, num: i32) {\nif index < 0 || 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() {\nself.extend_capacity();\n}\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor j in (index..self.size).rev() {\nself.arr[j + 1] = self.arr[j];\n}\nself.arr[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.size += 1;\n}\n/* \u5220\u9664\u5143\u7d20 */\npub fn remove(&mut self, index: usize) -> i32 {\nif index < 0 || index >= self.size() {panic!(\"\u7d22\u5f15\u8d8a\u754c\")};\nlet num = self.arr[index];\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor j in (index..self.size - 1) {\nself.arr[j] = self.arr[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.size -= 1;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\npub fn extend_capacity(&mut self) {\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extend_ratio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nlet new_capacity = self.capacity * self.extend_ratio;\nself.arr.resize(new_capacity, 0);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nself.capacity = new_capacity;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\npub fn to_array(&mut self) -> Vec<i32> {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nlet mut arr = Vec::new();\nfor i in 0..self.size {\narr.push(self.get(i));\n}\narr\n}\n}\n
my_list.c
/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nstruct myList {\nint *arr;        // \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 *nums = malloc(sizeof(myList));\nnums->capacity = 10;\nnums->arr = malloc(sizeof(int) * nums->capacity);\nnums->size = 0;\nnums->extendRatio = 2;\nreturn nums;\n}\n/* \u6790\u6784\u51fd\u6570 */\nvoid delMyList(myList *nums) {\nfree(nums->arr);\nfree(nums);\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6 */\nint size(myList *nums) {\nreturn nums->size;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nint capacity(myList *nums) {\nreturn nums->capacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\nint get(myList *nums, int index) {\nassert(index >= 0 && index < nums->size);\nreturn nums->arr[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nvoid set(myList *nums, int index, int num) {\nassert(index >= 0 && index < nums->size);\nnums->arr[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nvoid add(myList *nums, int num) {\nif (size(nums) == capacity(nums)) {\nextendCapacity(nums); // \u6269\u5bb9\n}\nnums->arr[size(nums)] = num;\nnums->size++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nvoid insert(myList *nums, int index, int num) {\nassert(index >= 0 && index < size(nums));\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (size(nums) == capacity(nums)) {\nextendCapacity(nums); // \u6269\u5bb9\n}\nfor (int i = size(nums); i > index; --i) {\nnums->arr[i] = nums->arr[i - 1];\n}\nnums->arr[index] = num;\nnums->size++;\n}\n/* \u5220\u9664\u5143\u7d20 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nint removeNum(myList *nums, int index) {\nassert(index >= 0 && index < size(nums));\nint num = nums->arr[index];\nfor (int i = index; i < size(nums) - 1; i++) {\nnums->arr[i] = nums->arr[i + 1];\n}\nnums->size--;\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\nvoid extendCapacity(myList *nums) {\n// \u5148\u5206\u914d\u7a7a\u95f4\nint newCapacity = capacity(nums) * nums->extendRatio;\nint *extend = (int *)malloc(sizeof(int) * newCapacity);\nint *temp = nums->arr;\n// \u62f7\u8d1d\u65e7\u6570\u636e\u5230\u65b0\u6570\u636e\nfor (int i = 0; i < size(nums); i++)\nextend[i] = nums->arr[i];\n// \u91ca\u653e\u65e7\u6570\u636e\nfree(temp);\n// \u66f4\u65b0\u65b0\u6570\u636e\nnums->arr = extend;\nnums->capacity = newCapacity;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a Array \u7528\u4e8e\u6253\u5370 */\nint *toArray(myList *nums) {\nreturn nums->arr;\n}\n
my_list.zig
// \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0\nfn MyList(comptime T: type) type {\nreturn struct {\nconst Self = @This();\narr: []T = undefined,                        // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\narrCapacity: usize = 10,                     // \u5217\u8868\u5bb9\u91cf\nnumSize: usize = 0,                           // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nextendRatio: 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\u51fd\u6570\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.arr = try self.mem_allocator.alloc(T, self.arrCapacity);\n@memset(self.arr, @as(T, 0));\n}\n// \u6790\u6784\u51fd\u6570\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.numSize;\n}\n// \u83b7\u53d6\u5217\u8868\u5bb9\u91cf\npub fn capacity(self: *Self) usize {\nreturn self.arrCapacity;\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.arr[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.arr[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.arr[self.size()] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.numSize += 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// \u5c06\u7d22\u5f15 index \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.arr[j + 1] = self.arr[j];\n}\nself.arr[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.numSize += 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.arr[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.arr[j] = self.arr[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.numSize -= 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 size * extendRatio \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nvar newCapacity = self.capacity() * self.extendRatio;\nvar extend = try self.mem_allocator.alloc(T, newCapacity);\n@memset(extend, @as(T, 0));\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nstd.mem.copy(T, extend, self.arr);\nself.arr = extend;\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nself.arrCapacity = 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 arr = try self.mem_allocator.alloc(T, self.size());\n@memset(arr, @as(T, 0));\nfor (arr, 0..) |*num, i| {\nnum.* = self.get(i);\n}\nreturn arr;\n}\n};\n}\n
"},{"location":"chapter_array_and_linkedlist/summary/","title":"4.4 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_array_and_linkedlist/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
  • \u6570\u7ec4\u548c\u94fe\u8868\u662f\u4e24\u79cd\u57fa\u672c\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5206\u522b\u4ee3\u8868\u6570\u636e\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u4e24\u79cd\u5b58\u50a8\u65b9\u5f0f\uff1a\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u548c\u5206\u6563\u7a7a\u95f4\u5b58\u50a8\u3002\u4e24\u8005\u7684\u7279\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\u5f15\u7528\uff08\u6307\u9488\uff09\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
"},{"location":"chapter_array_and_linkedlist/summary/#2-q-a","title":"2. \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

\u5b58\u50a8\u5728\u6808\u4e0a\u548c\u5806\u4e0a\u7684\u6570\u7ec4\u90fd\u88ab\u5b58\u50a8\u5728\u8fde\u7eed\u5185\u5b58\u7a7a\u95f4\u5185\uff0c\u6570\u636e\u64cd\u4f5c\u6548\u7387\u662f\u57fa\u672c\u4e00\u81f4\u7684\u3002\u7136\u800c\uff0c\u6808\u548c\u5806\u5177\u6709\u5404\u81ea\u7684\u7279\u70b9\uff0c\u4ece\u800c\u5bfc\u81f4\u4ee5\u4e0b\u4e0d\u540c\u70b9\u3002

  1. \u5206\u914d\u548c\u91ca\u653e\u6548\u7387\uff1a\u6808\u662f\u4e00\u5757\u8f83\u5c0f\u7684\u5185\u5b58\uff0c\u5206\u914d\u7531\u7f16\u8bd1\u5668\u81ea\u52a8\u5b8c\u6210\uff1b\u800c\u5806\u5185\u5b58\u76f8\u5bf9\u66f4\u5927\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u52a8\u6001\u5206\u914d\uff0c\u66f4\u5bb9\u6613\u788e\u7247\u5316\u3002\u56e0\u6b64\uff0c\u5806\u4e0a\u7684\u5206\u914d\u548c\u91ca\u653e\u64cd\u4f5c\u901a\u5e38\u6bd4\u6808\u4e0a\u7684\u6162\u3002
  2. \u5927\u5c0f\u9650\u5236\uff1a\u6808\u5185\u5b58\u76f8\u5bf9\u8f83\u5c0f\uff0c\u5806\u7684\u5927\u5c0f\u4e00\u822c\u53d7\u9650\u4e8e\u53ef\u7528\u5185\u5b58\u3002\u56e0\u6b64\u5806\u66f4\u52a0\u9002\u5408\u5b58\u50a8\u5927\u578b\u6570\u7ec4\u3002
  3. \u7075\u6d3b\u6027\uff1a\u6808\u4e0a\u7684\u6570\u7ec4\u7684\u5927\u5c0f\u9700\u8981\u5728\u7f16\u8bd1\u65f6\u786e\u5b9a\uff0c\u800c\u5806\u4e0a\u7684\u6570\u7ec4\u7684\u5927\u5c0f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u786e\u5b9a\u3002

\u4e3a\u4ec0\u4e48\u6570\u7ec4\u8981\u6c42\u76f8\u540c\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u800c\u5728\u94fe\u8868\u4e2d\u5374\u6ca1\u6709\u5f3a\u8c03\u540c\u7c7b\u578b\u5462\uff1f

\u94fe\u8868\u7531\u7ed3\u70b9\u7ec4\u6210\uff0c\u7ed3\u70b9\u4e4b\u95f4\u901a\u8fc7\u5f15\u7528\uff08\u6307\u9488\uff09\u8fde\u63a5\uff0c\u5404\u4e2a\u7ed3\u70b9\u53ef\u4ee5\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u4f8b\u5982 int\u3001double\u3001string\u3001object \u7b49\u3002

\u76f8\u5bf9\u5730\uff0c\u6570\u7ec4\u5143\u7d20\u5219\u5fc5\u987b\u662f\u76f8\u540c\u7c7b\u578b\u7684\uff0c\u8fd9\u6837\u624d\u80fd\u901a\u8fc7\u8ba1\u7b97\u504f\u79fb\u91cf\u6765\u83b7\u53d6\u5bf9\u5e94\u5143\u7d20\u4f4d\u7f6e\u3002\u4f8b\u5982\uff0c\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\u957f\u5ea6\u7684\u5143\u7d20\u3002

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

\u5220\u9664\u8282\u70b9\u540e\uff0c\u662f\u5426\u9700\u8981\u628a P.next \u8bbe\u4e3a \\(\\text{None}\\) \u5462\uff1f

\u4e0d\u4fee\u6539 P.next \u4e5f\u53ef\u4ee5\u3002\u4ece\u8be5\u94fe\u8868\u7684\u89d2\u5ea6\u770b\uff0c\u4ece\u5934\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\u3001Python\u3001Go \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 \u548c C++ \u7b49\u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u624b\u52a8\u91ca\u653e\u8282\u70b9\u5185\u5b58\u3002

\u5728\u94fe\u8868\u4e2d\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u3002\u4f46\u662f\u589e\u5220\u4e4b\u524d\u90fd\u9700\u8981 \\(O(n)\\) \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\u7684\u793a\u610f\u56fe\u53ea\u662f\u5b9a\u6027\u8868\u793a\uff0c\u5b9a\u91cf\u8868\u793a\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u5206\u6790\u3002

  • \u4e0d\u540c\u7c7b\u578b\u7684\u7ed3\u70b9\u503c\u5360\u7528\u7684\u7a7a\u95f4\u662f\u4e0d\u540c\u7684\uff0c\u6bd4\u5982 int\u3001long\u3001double \u548c\u5b9e\u4f8b\u5bf9\u8c61\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

\u5728 Python \u4e2d\u521d\u59cb\u5316 n = [1, 2, 3] \u540e\uff0c\u8fd9 3 \u4e2a\u5143\u7d20\u7684\u5730\u5740\u662f\u76f8\u8fde\u7684\uff0c\u4f46\u662f\u521d\u59cb\u5316 m = [2, 1, 3] \u4f1a\u53d1\u73b0\u5b83\u4eec\u6bcf\u4e2a\u5143\u7d20\u7684 id \u5e76\u4e0d\u662f\u8fde\u7eed\u7684\uff0c\u800c\u662f\u5206\u522b\u8ddf n \u4e2d\u7684\u76f8\u540c\u3002\u8fd9\u4e9b\u5143\u7d20\u5730\u5740\u4e0d\u8fde\u7eed\uff0c\u90a3\u4e48 m \u8fd8\u662f\u6570\u7ec4\u5417\uff1f

\u5047\u5982\u628a\u5217\u8868\u5143\u7d20\u6362\u6210\u94fe\u8868\u8282\u70b9 n = [n1, n2, n3, n4, n5] \uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u8fd9\u4e94\u4e2a\u8282\u70b9\u5bf9\u8c61\u4e5f\u662f\u88ab\u5206\u6563\u5b58\u50a8\u5728\u5185\u5b58\u5404\u5904\u7684\u3002\u7136\u800c\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u5217\u8868\u7d22\u5f15\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u83b7\u53d6\u5230\u8282\u70b9\u5185\u5b58\u5730\u5740\uff0c\u4ece\u800c\u8bbf\u95ee\u5230\u5bf9\u5e94\u7684\u8282\u70b9\u3002\u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u4e2d\u5b58\u50a8\u7684\u662f\u8282\u70b9\u7684\u5f15\u7528\uff0c\u800c\u975e\u8282\u70b9\u672c\u8eab\u3002

\u4e0e\u8bb8\u591a\u8bed\u8a00\u4e0d\u540c\u7684\u662f\uff0c\u5728 Python \u4e2d\u6570\u5b57\u4e5f\u88ab\u5305\u88c5\u4e3a\u5bf9\u8c61\uff0c\u5217\u8868\u4e2d\u5b58\u50a8\u7684\u4e0d\u662f\u6570\u5b57\u672c\u8eab\uff0c\u800c\u662f\u5bf9\u6570\u5b57\u7684\u5f15\u7528\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4f1a\u53d1\u73b0\u4e24\u4e2a\u6570\u7ec4\u4e2d\u7684\u76f8\u540c\u6570\u5b57\u62e5\u6709\u540c\u4e00\u4e2a id \uff0c\u5e76\u4e14\u8fd9\u4e9b\u6570\u5b57\u7684\u5185\u5b58\u5730\u5740\u662f\u65e0\u987b\u8fde\u7eed\u7684\u3002

C++ STL \u91cc\u9762\u7684 std::list \u5df2\u7ecf\u5b9e\u73b0\u4e86\u53cc\u5411\u94fe\u8868\uff0c\u4f46\u597d\u50cf\u4e00\u4e9b\u7b97\u6cd5\u7684\u4e66\u4e0a\u90fd\u4e0d\u600e\u4e48\u76f4\u63a5\u7528\u8fd9\u4e2a\uff0c\u662f\u4e0d\u662f\u6709\u4ec0\u4e48\u5c40\u9650\u6027\u5462?

\u4e00\u65b9\u9762\uff0c\u6211\u4eec\u5f80\u5f80\u66f4\u9752\u7750\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u7b97\u6cd5\uff0c\u800c\u53ea\u6709\u5728\u5fc5\u8981\u65f6\u624d\u4f7f\u7528\u94fe\u8868\uff0c\u4e3b\u8981\u6709\u4e24\u4e2a\u539f\u56e0\u3002

  • \u7a7a\u95f4\u5f00\u9500\uff1a\u7531\u4e8e\u6bcf\u4e2a\u5143\u7d20\u9700\u8981\u4e24\u4e2a\u989d\u5916\u7684\u6307\u9488\uff08\u4e00\u4e2a\u7528\u4e8e\u524d\u4e00\u4e2a\u5143\u7d20\uff0c\u4e00\u4e2a\u7528\u4e8e\u540e\u4e00\u4e2a\u5143\u7d20\uff09\uff0c\u6240\u4ee5 std::list \u901a\u5e38\u6bd4 std::vector \u66f4\u5360\u7528\u7a7a\u95f4\u3002
  • \u7f13\u5b58\u4e0d\u53cb\u597d\uff1a\u7531\u4e8e\u6570\u636e\u4e0d\u662f\u8fde\u7eed\u5b58\u653e\u7684\uff0cstd::list \u5bf9\u7f13\u5b58\u7684\u5229\u7528\u7387\u8f83\u4f4e\u3002\u4e00\u822c\u60c5\u51b5\u4e0b\uff0cstd::vector \u7684\u6027\u80fd\u4f1a\u66f4\u597d\u3002

\u53e6\u4e00\u65b9\u9762\uff0c\u5fc5\u8981\u4f7f\u7528\u94fe\u8868\u7684\u60c5\u51b5\u4e3b\u8981\u662f\u4e8c\u53c9\u6811\u548c\u56fe\u3002\u6808\u548c\u961f\u5217\u5f80\u5f80\u4f1a\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b\u7684 stack \u548c queue \uff0c\u800c\u975e\u94fe\u8868\u3002

"},{"location":"chapter_backtracking/","title":"\u7b2c 13 \u7ae0 \u00a0 \u56de\u6eaf","text":"

Abstract

\u6211\u4eec\u5982\u540c\u8ff7\u5bab\u4e2d\u7684\u63a2\u7d22\u8005\uff0c\u5728\u524d\u8fdb\u7684\u9053\u8def\u4e0a\u53ef\u80fd\u4f1a\u9047\u5230\u56f0\u96be\u3002

\u56de\u6eaf\u7684\u529b\u91cf\u8ba9\u6211\u4eec\u80fd\u591f\u91cd\u65b0\u5f00\u59cb\uff0c\u4e0d\u65ad\u5c1d\u8bd5\uff0c\u6700\u7ec8\u627e\u5230\u901a\u5f80\u5149\u660e\u7684\u51fa\u53e3\u3002

"},{"location":"chapter_backtracking/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 13.1 \u00a0 \u56de\u6eaf\u7b97\u6cd5
  • 13.2 \u00a0 \u5168\u6392\u5217\u95ee\u9898
  • 13.3 \u00a0 \u5b50\u96c6\u548c\u95ee\u9898
  • 13.4 \u00a0 N \u7687\u540e\u95ee\u9898
  • 13.5 \u00a0 \u5c0f\u7ed3
"},{"location":"chapter_backtracking/backtracking_algorithm/","title":"13.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\u201c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u201d\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\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5229\u7528\u524d\u5e8f\u904d\u5386\u6784\u9020\u4e00\u4e2a\u56de\u6eaf\u95ee\u9898\uff0c\u9010\u6b65\u4e86\u89e3\u56de\u6eaf\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u3002

\u4f8b\u9898\u4e00

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

\u5bf9\u4e8e\u6b64\u9898\uff0c\u6211\u4eec\u524d\u5e8f\u904d\u5386\u8fd9\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\u76f8\u5173\u8fc7\u7a0b\u5b9e\u73b0\u5982\u56fe 13-1 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig preorder_traversal_i_compact.py
def pre_order(root: TreeNode):\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.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.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.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.go
/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunc preOrderI(root *TreeNode, res *[]*TreeNode) {\nif root == nil {\nreturn\n}\nif (root.Val).(int) == 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.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.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.dart
/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode? root, List<TreeNode> res) {\nif (root == null) {\nreturn;\n}\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.add(root);\n}\npreOrder(root.left, res);\npreOrder(root.right, res);\n}\n
preorder_traversal_i_compact.rs
/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfn pre_order(res: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<Rc<RefCell<TreeNode>>>) {\nif root.is_none() {\nreturn;\n}\nif let Some(node) = root {\nif node.borrow().val == 7 {\n// \u8bb0\u5f55\u89e3\nres.push(node.clone());\n}\npre_order(res, node.borrow().left.clone());\npre_order(res, node.borrow().right.clone());\n}\n}\n
preorder_traversal_i_compact.c
/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode *root) {\nif (root == NULL) {\nreturn;\n}\nif (root->val == 7) {\n// \u8bb0\u5f55\u89e3\nvectorPushback(res, root, sizeof(int));\n}\npreOrder(root->left);\npreOrder(root->right);\n}\n
preorder_traversal_i_compact.zig
[class]{}-[func]{preOrder}\n

\u56fe 13-1 \u00a0 \u5728\u524d\u5e8f\u904d\u5386\u4e2d\u641c\u7d22\u8282\u70b9

"},{"location":"chapter_backtracking/backtracking_algorithm/#1311","title":"13.1.1 \u00a0 \u5c1d\u8bd5\u4e0e\u56de\u9000","text":"

\u4e4b\u6240\u4ee5\u79f0\u4e4b\u4e3a\u56de\u6eaf\u7b97\u6cd5\uff0c\u662f\u56e0\u4e3a\u8be5\u7b97\u6cd5\u5728\u641c\u7d22\u89e3\u7a7a\u95f4\u65f6\u4f1a\u91c7\u7528\u201c\u5c1d\u8bd5\u201d\u4e0e\u201c\u56de\u9000\u201d\u7684\u7b56\u7565\u3002\u5f53\u7b97\u6cd5\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u9047\u5230\u67d0\u4e2a\u72b6\u6001\u65e0\u6cd5\u7ee7\u7eed\u524d\u8fdb\u6216\u65e0\u6cd5\u5f97\u5230\u6ee1\u8db3\u6761\u4ef6\u7684\u89e3\u65f6\uff0c\u5b83\u4f1a\u64a4\u9500\u4e0a\u4e00\u6b65\u7684\u9009\u62e9\uff0c\u9000\u56de\u5230\u4e4b\u524d\u7684\u72b6\u6001\uff0c\u5e76\u5c1d\u8bd5\u5176\u4ed6\u53ef\u80fd\u7684\u9009\u62e9\u3002

\u5bf9\u4e8e\u4f8b\u9898\u4e00\uff0c\u8bbf\u95ee\u6bcf\u4e2a\u8282\u70b9\u90fd\u4ee3\u8868\u4e00\u6b21\u201c\u5c1d\u8bd5\u201d\uff0c\u800c\u8d8a\u8fc7\u53f6\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\u4ec5\u4ec5\u5305\u62ec\u51fd\u6570\u8fd4\u56de\u3002\u4e3a\u89e3\u91ca\u8fd9\u4e00\u70b9\uff0c\u6211\u4eec\u5bf9\u4f8b\u9898\u4e00\u7a0d\u4f5c\u62d3\u5c55\u3002

\u4f8b\u9898\u4e8c

\u5728\u4e8c\u53c9\u6811\u4e2d\u641c\u7d22\u6240\u6709\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\uff0c\u8bf7\u8fd4\u56de\u6839\u8282\u70b9\u5230\u8fd9\u4e9b\u8282\u70b9\u7684\u8def\u5f84\u3002

\u5728\u4f8b\u9898\u4e00\u4ee3\u7801\u7684\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u5217\u8868 path \u8bb0\u5f55\u8bbf\u95ee\u8fc7\u7684\u8282\u70b9\u8def\u5f84\u3002\u5f53\u8bbf\u95ee\u5230\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\u65f6\uff0c\u5219\u590d\u5236 path \u5e76\u6dfb\u52a0\u8fdb\u7ed3\u679c\u5217\u8868 res \u3002\u904d\u5386\u5b8c\u6210\u540e\uff0cres \u4e2d\u4fdd\u5b58\u7684\u5c31\u662f\u6240\u6709\u7684\u89e3\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig preorder_traversal_ii_compact.py
def pre_order(root: TreeNode):\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.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.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.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.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 root.Val.(int) == 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.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.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.dart
/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(\nTreeNode? root,\nList<TreeNode> path,\nList<List<TreeNode>> res,\n) {\nif (root == null) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.add(root);\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.add(List.from(path));\n}\npreOrder(root.left, path, res);\npreOrder(root.right, path, res);\n// \u56de\u9000\npath.removeLast();\n}\n
preorder_traversal_ii_compact.rs
/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfn pre_order(res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>, path: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<Rc<RefCell<TreeNode>>>) {\nif root.is_none() {\nreturn;\n}\nif let Some(node) = root {\n// \u5c1d\u8bd5\npath.push(node.clone());\nif node.borrow().val == 7 {\n// \u8bb0\u5f55\u89e3\nres.push(path.clone());\n}\npre_order(res, path, node.borrow().left.clone());\npre_order(res, path, node.borrow().right.clone());\n// \u56de\u9000\npath.remove(path.len() -  1);\n}\n}\n
preorder_traversal_ii_compact.c
/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(TreeNode *root, vector *path, vector *res) {\nif (root == NULL) {\nreturn;\n}\n// \u5c1d\u8bd5\nvectorPushback(path, root, sizeof(TreeNode));\nif (root->val == 7) {\n// \u8bb0\u5f55\u89e3\nvector *newPath = newVector();\nfor (int i = 0; i < path->size; i++) {\nvectorPushback(newPath, path->data[i], sizeof(int));\n}\nvectorPushback(res, newPath, sizeof(vector));\n}\npreOrder(root->left, path, res);\npreOrder(root->right, path, res);\n// \u56de\u9000\nvectorPopback(path);\n}\n
preorder_traversal_ii_compact.zig
[class]{}-[func]{preOrder}\n

\u5728\u6bcf\u6b21\u201c\u5c1d\u8bd5\u201d\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5c06\u5f53\u524d\u8282\u70b9\u6dfb\u52a0\u8fdb path \u6765\u8bb0\u5f55\u8def\u5f84\uff1b\u800c\u5728\u201c\u56de\u9000\u201d\u524d\uff0c\u6211\u4eec\u9700\u8981\u5c06\u8be5\u8282\u70b9\u4ece path \u4e2d\u5f39\u51fa\uff0c\u4ee5\u6062\u590d\u672c\u6b21\u5c1d\u8bd5\u4e4b\u524d\u7684\u72b6\u6001\u3002

\u89c2\u5bdf\u56fe 13-2 \u6240\u793a\u7684\u8fc7\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5c1d\u8bd5\u548c\u56de\u9000\u7406\u89e3\u4e3a\u201c\u524d\u8fdb\u201d\u4e0e\u201c\u64a4\u9500\u201d\uff0c\u4e24\u4e2a\u64cd\u4f5c\u662f\u4e92\u4e3a\u9006\u5411\u7684\u3002

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

\u56fe 13-2 \u00a0 \u5c1d\u8bd5\u4e0e\u56de\u9000

"},{"location":"chapter_backtracking/backtracking_algorithm/#1312","title":"13.1.2 \u00a0 \u526a\u679d","text":"

\u590d\u6742\u7684\u56de\u6eaf\u95ee\u9898\u901a\u5e38\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a\u7ea6\u675f\u6761\u4ef6\uff0c\u7ea6\u675f\u6761\u4ef6\u901a\u5e38\u53ef\u7528\u4e8e\u201c\u526a\u679d\u201d\u3002

\u4f8b\u9898\u4e09

\u5728\u4e8c\u53c9\u6811\u4e2d\u641c\u7d22\u6240\u6709\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\uff0c\u8bf7\u8fd4\u56de\u6839\u8282\u70b9\u5230\u8fd9\u4e9b\u8282\u70b9\u7684\u8def\u5f84\uff0c\u5e76\u8981\u6c42\u8def\u5f84\u4e2d\u4e0d\u5305\u542b\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\u3002

\u4e3a\u4e86\u6ee1\u8db3\u4ee5\u4e0a\u7ea6\u675f\u6761\u4ef6\uff0c\u6211\u4eec\u9700\u8981\u6dfb\u52a0\u526a\u679d\u64cd\u4f5c\uff1a\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u82e5\u9047\u5230\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\uff0c\u5219\u63d0\u524d\u8fd4\u56de\uff0c\u505c\u6b62\u7ee7\u7eed\u641c\u7d22\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig preorder_traversal_iii_compact.py
def pre_order(root: TreeNode):\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.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.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.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.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 root.Val.(int) == 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.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.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.dart
/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(\nTreeNode? root,\nList<TreeNode> path,\nList<List<TreeNode>> res,\n) {\nif (root == null || root.val == 3) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.add(root);\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.add(List.from(path));\n}\npreOrder(root.left, path, res);\npreOrder(root.right, path, res);\n// \u56de\u9000\npath.removeLast();\n}\n
preorder_traversal_iii_compact.rs
/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfn pre_order(res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>, path: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<Rc<RefCell<TreeNode>>>) {\n// \u526a\u679d\nif root.is_none() || root.as_ref().unwrap().borrow().val == 3 {\nreturn;\n}\nif let Some(node) = root {\n// \u5c1d\u8bd5\npath.push(node.clone());\nif node.borrow().val == 7 {\n// \u8bb0\u5f55\u89e3\nres.push(path.clone());\n}\npre_order(res, path, node.borrow().left.clone());\npre_order(res, path, node.borrow().right.clone());\n// \u56de\u9000\npath.remove(path.len() -  1);\n}\n}\n
preorder_traversal_iii_compact.c
/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(TreeNode *root, vector *path, vector *res) {\n// \u526a\u679d\nif (root == NULL || root->val == 3) {\nreturn;\n}\n// \u5c1d\u8bd5\nvectorPushback(path, root, sizeof(TreeNode));\nif (root->val == 7) {\n// \u8bb0\u5f55\u89e3\nvector *newPath = newVector();\nfor (int i = 0; i < path->size; i++) {\nvectorPushback(newPath, path->data[i], sizeof(int));\n}\nvectorPushback(res, newPath, sizeof(vector));\nres->depth++;\n}\npreOrder(root->left, path, res);\npreOrder(root->right, path, res);\n// \u56de\u9000\nvectorPopback(path);\n}\n
preorder_traversal_iii_compact.zig
[class]{}-[func]{preOrder}\n

\u526a\u679d\u662f\u4e00\u4e2a\u975e\u5e38\u5f62\u8c61\u7684\u540d\u8bcd\u3002\u5982\u56fe 13-3 \u6240\u793a\uff0c\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u201c\u526a\u6389\u201d\u4e86\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u641c\u7d22\u5206\u652f\uff0c\u907f\u514d\u8bb8\u591a\u65e0\u610f\u4e49\u7684\u5c1d\u8bd5\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u641c\u7d22\u6548\u7387\u3002

\u56fe 13-3 \u00a0 \u6839\u636e\u7ea6\u675f\u6761\u4ef6\u526a\u679d

"},{"location":"chapter_backtracking/backtracking_algorithm/#1313","title":"13.1.3 \u00a0 \u6846\u67b6\u4ee3\u7801","text":"

\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c1d\u8bd5\u5c06\u56de\u6eaf\u7684\u201c\u5c1d\u8bd5\u3001\u56de\u9000\u3001\u526a\u679d\u201d\u7684\u4e3b\u4f53\u6846\u67b6\u63d0\u70bc\u51fa\u6765\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u901a\u7528\u6027\u3002

\u5728\u4ee5\u4e0b\u6846\u67b6\u4ee3\u7801\u4e2d\uff0cstate \u8868\u793a\u95ee\u9898\u7684\u5f53\u524d\u72b6\u6001\uff0cchoices \u8868\u793a\u5f53\u524d\u72b6\u6001\u4e0b\u53ef\u4ee5\u505a\u51fa\u7684\u9009\u62e9\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig
def backtrack(state: State, choices: list[choice], res: list[state]):\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)\n# \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\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 */\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);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\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, List<Choice> choices, List<State> res) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\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, List<Choice> choices, List<State> res) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif (IsSolution(state)) {\n// \u8bb0\u5f55\u89e3\nRecordSolution(state, res);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\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 *State, choices []Choice, res *[]State) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif isSolution(state) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res)\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\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 */\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)\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\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
/* \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);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\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);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\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, List<Choice>, List<State> res) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\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
/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfn backtrack(state: &mut State, choices: &Vec<Choice>, res: &mut Vec<State>) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif is_solution(state) {\n// \u8bb0\u5f55\u89e3\nrecord_solution(state, res);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\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 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}\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);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\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
\n

\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u57fa\u4e8e\u6846\u67b6\u4ee3\u7801\u6765\u89e3\u51b3\u4f8b\u9898\u4e09\u3002\u72b6\u6001 state \u4e3a\u8282\u70b9\u904d\u5386\u8def\u5f84\uff0c\u9009\u62e9 choices \u4e3a\u5f53\u524d\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\uff0c\u7ed3\u679c res \u662f\u8def\u5f84\u5217\u8868\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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)\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.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);\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.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);\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.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);\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.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)\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.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)\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.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);\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);\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.dart
/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool isSolution(List<TreeNode> state) {\nreturn state.isNotEmpty && state.last.val == 7;\n}\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\nres.add(List.from(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.removeLast();\n}\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid backtrack(\nList<TreeNode> state,\nList<TreeNode?> choices,\nList<List<TreeNode>> res,\n) {\n// \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (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, [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.rs
/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfn is_solution(state: &mut Vec<Rc<RefCell<TreeNode>>>) -> bool {\nreturn !state.is_empty() && state.get(state.len() - 1).unwrap().borrow().val == 7;\n}\n/* \u8bb0\u5f55\u89e3 */\nfn record_solution(state: &mut Vec<Rc<RefCell<TreeNode>>>, res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>) {\nres.push(state.clone());\n}\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfn is_valid(_: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Rc<RefCell<TreeNode>>) -> bool {\nreturn choice.borrow().val != 3;\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nfn make_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Rc<RefCell<TreeNode>>) {\nstate.push(choice);\n}\n/* \u6062\u590d\u72b6\u6001 */\nfn undo_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, _: Rc<RefCell<TreeNode>>) {\nstate.remove(state.len() - 1);\n}\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfn backtrack(state: &mut Vec<Rc<RefCell<TreeNode>>>, choices: &mut Vec<Rc<RefCell<TreeNode>>>, res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>) {\n// \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\nif is_solution(state) {\n// \u8bb0\u5f55\u89e3\nrecord_solution(state, res);\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 is_valid(state, choice.clone()) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmake_choice(state, choice.clone());\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, &mut vec![choice.borrow().left.clone().unwrap(), choice.borrow().right.clone().unwrap()], res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundo_choice(state, choice.clone());\n}\n}\n}\n
preorder_traversal_iii_template.c
/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool isSolution(vector *state) {\nreturn state->size != 0 && ((TreeNode *)(state->data[state->size - 1]))->val == 7;\n}\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(vector *state, vector *res) {\nvector *newPath = newVector();\nfor (int i = 0; i < state->size; i++) {\nvectorPushback(newPath, state->data[i], sizeof(int));\n}\nvectorPushback(res, newPath, sizeof(vector));\n}\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nbool isValid(vector *state, TreeNode *choice) {\nreturn choice != NULL && choice->val != 3;\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(vector *state, TreeNode *choice) {\nvectorPushback(state, choice, sizeof(TreeNode));\n}\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(vector *state, TreeNode *choice) {\nvectorPopback(state);\n}\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid backtrack(vector *state, vector *choices, vector *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 (int i = 0; i < choices->size; i++) {\nTreeNode *choice = choices->data[i];\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 *nextChoices = newVector();\nvectorPushback(nextChoices, choice->left, sizeof(TreeNode));\nvectorPushback(nextChoices, choice->right, sizeof(TreeNode));\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.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

\u6839\u636e\u9898\u610f\uff0c\u6211\u4eec\u5728\u627e\u5230\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\u540e\u5e94\u8be5\u7ee7\u7eed\u641c\u7d22\uff0c\u56e0\u6b64\u9700\u8981\u5c06\u8bb0\u5f55\u89e3\u4e4b\u540e\u7684 return \u8bed\u53e5\u5220\u9664\u3002\u56fe 13-4 \u5bf9\u6bd4\u4e86\u4fdd\u7559\u6216\u5220\u9664 return \u8bed\u53e5\u7684\u641c\u7d22\u8fc7\u7a0b\u3002

\u56fe 13-4 \u00a0 \u4fdd\u7559\u4e0e\u5220\u9664 return \u7684\u641c\u7d22\u8fc7\u7a0b\u5bf9\u6bd4

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

"},{"location":"chapter_backtracking/backtracking_algorithm/#1314","title":"13.1.4 \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

\u8868 13-1 \u00a0 \u5e38\u89c1\u7684\u56de\u6eaf\u7b97\u6cd5\u672f\u8bed

\u540d\u8bcd \u5b9a\u4e49 \u4f8b\u9898\u4e09 \u89e3 Solution \u89e3\u662f\u6ee1\u8db3\u95ee\u9898\u7279\u5b9a\u6761\u4ef6\u7684\u7b54\u6848\uff0c\u53ef\u80fd\u6709\u4e00\u4e2a\u6216\u591a\u4e2a \u6839\u8282\u70b9\u5230\u8282\u70b9 \\(7\\) \u7684\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u6240\u6709\u8def\u5f84 \u7ea6\u675f\u6761\u4ef6 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 \u8def\u5f84\u4e2d\u4e0d\u5305\u542b\u8282\u70b9 \\(3\\) \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 \u5c1d\u8bd5 Attempt \u5c1d\u8bd5\u662f\u6839\u636e\u53ef\u7528\u9009\u62e9\u6765\u63a2\u7d22\u89e3\u7a7a\u95f4\u7684\u8fc7\u7a0b\uff0c\u5305\u62ec\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\uff0c\u68c0\u67e5\u662f\u5426\u4e3a\u89e3 \u9012\u5f52\u8bbf\u95ee\u5de6\uff08\u53f3\uff09\u5b50\u8282\u70b9\uff0c\u5c06\u8282\u70b9\u6dfb\u52a0\u8fdb path \uff0c\u5224\u65ad\u8282\u70b9\u7684\u503c\u662f\u5426\u4e3a \\(7\\) \u56de\u9000 Backtracking \u56de\u9000\u6307\u9047\u5230\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u72b6\u6001\u65f6\uff0c\u64a4\u9500\u524d\u9762\u505a\u51fa\u7684\u9009\u62e9\uff0c\u56de\u5230\u4e0a\u4e00\u4e2a\u72b6\u6001 \u5f53\u8d8a\u8fc7\u53f6\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

\u95ee\u9898\u3001\u89e3\u3001\u72b6\u6001\u7b49\u6982\u5ff5\u662f\u901a\u7528\u7684\uff0c\u5728\u5206\u6cbb\u3001\u56de\u6eaf\u3001\u52a8\u6001\u89c4\u5212\u3001\u8d2a\u5fc3\u7b49\u7b97\u6cd5\u4e2d\u90fd\u6709\u6d89\u53ca\u3002

"},{"location":"chapter_backtracking/backtracking_algorithm/#1315","title":"13.1.5 \u00a0 \u4f18\u52bf\u4e0e\u5c40\u9650\u6027","text":"

\u56de\u6eaf\u7b97\u6cd5\u672c\u8d28\u4e0a\u662f\u4e00\u79cd\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u7b97\u6cd5\uff0c\u5b83\u5c1d\u8bd5\u6240\u6709\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\u76f4\u5230\u627e\u5230\u6ee1\u8db3\u6761\u4ef6\u7684\u89e3\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u80fd\u591f\u627e\u5230\u6240\u6709\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u800c\u4e14\u5728\u5408\u7406\u7684\u526a\u679d\u64cd\u4f5c\u4e0b\uff0c\u5177\u6709\u5f88\u9ad8\u7684\u6548\u7387\u3002

\u7136\u800c\uff0c\u5728\u5904\u7406\u5927\u89c4\u6a21\u6216\u8005\u590d\u6742\u95ee\u9898\u65f6\uff0c\u56de\u6eaf\u7b97\u6cd5\u7684\u8fd0\u884c\u6548\u7387\u53ef\u80fd\u96be\u4ee5\u63a5\u53d7\u3002

  • \u65f6\u95f4\uff1a\u56de\u6eaf\u7b97\u6cd5\u901a\u5e38\u9700\u8981\u904d\u5386\u72b6\u6001\u7a7a\u95f4\u7684\u6240\u6709\u53ef\u80fd\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u8fbe\u5230\u6307\u6570\u9636\u6216\u9636\u4e58\u9636\u3002
  • \u7a7a\u95f4\uff1a\u5728\u9012\u5f52\u8c03\u7528\u4e2d\u9700\u8981\u4fdd\u5b58\u5f53\u524d\u7684\u72b6\u6001\uff08\u4f8b\u5982\u8def\u5f84\u3001\u7528\u4e8e\u526a\u679d\u7684\u8f85\u52a9\u53d8\u91cf\u7b49\uff09\uff0c\u5f53\u6df1\u5ea6\u5f88\u5927\u65f6\uff0c\u7a7a\u95f4\u9700\u6c42\u53ef\u80fd\u4f1a\u53d8\u5f97\u5f88\u5927\u3002

\u5373\u4fbf\u5982\u6b64\uff0c\u56de\u6eaf\u7b97\u6cd5\u4ecd\u7136\u662f\u67d0\u4e9b\u641c\u7d22\u95ee\u9898\u548c\u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\u7684\u6700\u4f73\u89e3\u51b3\u65b9\u6848\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u95ee\u9898\uff0c\u7531\u4e8e\u65e0\u6cd5\u9884\u6d4b\u54ea\u4e9b\u9009\u62e9\u53ef\u751f\u6210\u6709\u6548\u7684\u89e3\uff0c\u56e0\u6b64\u6211\u4eec\u5fc5\u987b\u5bf9\u6240\u6709\u53ef\u80fd\u7684\u9009\u62e9\u8fdb\u884c\u904d\u5386\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5173\u952e\u662f\u5982\u4f55\u8fdb\u884c\u6548\u7387\u4f18\u5316\uff0c\u5e38\u89c1\u7684\u6548\u7387\u4f18\u5316\u65b9\u6cd5\u6709\u4e24\u79cd\u3002

  • \u526a\u679d\uff1a\u907f\u514d\u641c\u7d22\u90a3\u4e9b\u80af\u5b9a\u4e0d\u4f1a\u4ea7\u751f\u89e3\u7684\u8def\u5f84\uff0c\u4ece\u800c\u8282\u7701\u65f6\u95f4\u548c\u7a7a\u95f4\u3002
  • \u542f\u53d1\u5f0f\u641c\u7d22\uff1a\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u5f15\u5165\u4e00\u4e9b\u7b56\u7565\u6216\u8005\u4f30\u8ba1\u503c\uff0c\u4ece\u800c\u4f18\u5148\u641c\u7d22\u6700\u6709\u53ef\u80fd\u4ea7\u751f\u6709\u6548\u89e3\u7684\u8def\u5f84\u3002
"},{"location":"chapter_backtracking/backtracking_algorithm/#1316","title":"13.1.6 \u00a0 \u56de\u6eaf\u5178\u578b\u4f8b\u9898","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

\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

\u8bf7\u6ce8\u610f\uff0c\u5bf9\u4e8e\u8bb8\u591a\u7ec4\u5408\u4f18\u5316\u95ee\u9898\uff0c\u56de\u6eaf\u90fd\u4e0d\u662f\u6700\u4f18\u89e3\u51b3\u65b9\u6848\u3002

  • 0-1 \u80cc\u5305\u95ee\u9898\u901a\u5e38\u4f7f\u7528\u52a8\u6001\u89c4\u5212\u89e3\u51b3\uff0c\u4ee5\u8fbe\u5230\u66f4\u9ad8\u7684\u65f6\u95f4\u6548\u7387\u3002
  • \u65c5\u884c\u5546\u662f\u4e00\u4e2a\u8457\u540d\u7684 NP-Hard \u95ee\u9898\uff0c\u5e38\u7528\u89e3\u6cd5\u6709\u9057\u4f20\u7b97\u6cd5\u548c\u8681\u7fa4\u7b97\u6cd5\u7b49\u3002
  • \u6700\u5927\u56e2\u95ee\u9898\u662f\u56fe\u8bba\u4e2d\u7684\u4e00\u4e2a\u7ecf\u5178\u95ee\u9898\uff0c\u53ef\u7528\u8d2a\u5fc3\u7b49\u542f\u53d1\u5f0f\u7b97\u6cd5\u6765\u89e3\u51b3\u3002
"},{"location":"chapter_backtracking/n_queens_problem/","title":"13.4 \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\u56fe 13-15 \u6240\u793a\uff0c\u5f53 \\(n = 4\\) \u65f6\uff0c\u5171\u53ef\u4ee5\u627e\u5230\u4e24\u4e2a\u89e3\u3002\u4ece\u56de\u6eaf\u7b97\u6cd5\u7684\u89d2\u5ea6\u770b\uff0c\\(n \\times n\\) \u5927\u5c0f\u7684\u68cb\u76d8\u5171\u6709 \\(n^2\\) \u4e2a\u683c\u5b50\uff0c\u7ed9\u51fa\u4e86\u6240\u6709\u7684\u9009\u62e9 choices \u3002\u5728\u9010\u4e2a\u653e\u7f6e\u7687\u540e\u7684\u8fc7\u7a0b\u4e2d\uff0c\u68cb\u76d8\u72b6\u6001\u5728\u4e0d\u65ad\u5730\u53d8\u5316\uff0c\u6bcf\u4e2a\u65f6\u523b\u7684\u68cb\u76d8\u5c31\u662f\u72b6\u6001 state \u3002

\u56fe 13-15 \u00a0 4 \u7687\u540e\u95ee\u9898\u7684\u89e3

\u56fe 13-16 \u5c55\u793a\u4e86\u672c\u9898\u7684\u4e09\u4e2a\u7ea6\u675f\u6761\u4ef6\uff1a\u591a\u4e2a\u7687\u540e\u4e0d\u80fd\u5728\u540c\u4e00\u884c\u3001\u540c\u4e00\u5217\u3001\u540c\u4e00\u5bf9\u89d2\u7ebf\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5bf9\u89d2\u7ebf\u5206\u4e3a\u4e3b\u5bf9\u89d2\u7ebf \\ \u548c\u6b21\u5bf9\u89d2\u7ebf / \u4e24\u79cd\u3002

\u56fe 13-16 \u00a0 n \u7687\u540e\u95ee\u9898\u7684\u7ea6\u675f\u6761\u4ef6

"},{"location":"chapter_backtracking/n_queens_problem/#1","title":"1. \u00a0 \u9010\u884c\u653e\u7f6e\u7b56\u7565","text":"

\u7687\u540e\u7684\u6570\u91cf\u548c\u68cb\u76d8\u7684\u884c\u6570\u90fd\u4e3a \\(n\\) \uff0c\u56e0\u6b64\u6211\u4eec\u5bb9\u6613\u5f97\u5230\u4e00\u4e2a\u63a8\u8bba\uff1a\u68cb\u76d8\u6bcf\u884c\u90fd\u5141\u8bb8\u4e14\u53ea\u5141\u8bb8\u653e\u7f6e\u4e00\u4e2a\u7687\u540e\u3002

\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u53d6\u9010\u884c\u653e\u7f6e\u7b56\u7565\uff1a\u4ece\u7b2c\u4e00\u884c\u5f00\u59cb\uff0c\u5728\u6bcf\u884c\u653e\u7f6e\u4e00\u4e2a\u7687\u540e\uff0c\u76f4\u81f3\u6700\u540e\u4e00\u884c\u7ed3\u675f\u3002

\u5982\u56fe 13-17 \u6240\u793a\uff0c\u4e3a \\(4\\) \u7687\u540e\u95ee\u9898\u7684\u9010\u884c\u653e\u7f6e\u8fc7\u7a0b\u3002\u53d7\u753b\u5e45\u9650\u5236\uff0c\u56fe 13-17 \u4ec5\u5c55\u5f00\u4e86\u7b2c\u4e00\u884c\u7684\u5176\u4e2d\u4e00\u4e2a\u641c\u7d22\u5206\u652f\uff0c\u5e76\u4e14\u5c06\u4e0d\u6ee1\u8db3\u5217\u7ea6\u675f\u548c\u5bf9\u89d2\u7ebf\u7ea6\u675f\u7684\u65b9\u6848\u90fd\u8fdb\u884c\u4e86\u526a\u679d\u3002

\u56fe 13-17 \u00a0 \u9010\u884c\u653e\u7f6e\u7b56\u7565

\u672c\u8d28\u4e0a\u770b\uff0c\u9010\u884c\u653e\u7f6e\u7b56\u7565\u8d77\u5230\u4e86\u526a\u679d\u7684\u4f5c\u7528\uff0c\u5b83\u907f\u514d\u4e86\u540c\u4e00\u884c\u51fa\u73b0\u591a\u4e2a\u7687\u540e\u7684\u6240\u6709\u641c\u7d22\u5206\u652f\u3002

"},{"location":"chapter_backtracking/n_queens_problem/#2","title":"2. \u00a0 \u5217\u4e0e\u5bf9\u89d2\u7ebf\u526a\u679d","text":"

\u4e3a\u4e86\u6ee1\u8db3\u5217\u7ea6\u675f\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u5e03\u5c14\u578b\u6570\u7ec4 cols \u8bb0\u5f55\u6bcf\u4e00\u5217\u662f\u5426\u6709\u7687\u540e\u3002\u5728\u6bcf\u6b21\u51b3\u5b9a\u653e\u7f6e\u524d\uff0c\u6211\u4eec\u901a\u8fc7 cols \u5c06\u5df2\u6709\u7687\u540e\u7684\u5217\u8fdb\u884c\u526a\u679d\uff0c\u5e76\u5728\u56de\u6eaf\u4e2d\u52a8\u6001\u66f4\u65b0 cols \u7684\u72b6\u6001\u3002

\u90a3\u4e48\uff0c\u5982\u4f55\u5904\u7406\u5bf9\u89d2\u7ebf\u7ea6\u675f\u5462\uff1f\u8bbe\u68cb\u76d8\u4e2d\u67d0\u4e2a\u683c\u5b50\u7684\u884c\u5217\u7d22\u5f15\u4e3a \\((row, col)\\) \uff0c\u9009\u5b9a\u77e9\u9635\u4e2d\u7684\u67d0\u6761\u4e3b\u5bf9\u89d2\u7ebf\uff0c\u6211\u4eec\u53d1\u73b0\u8be5\u5bf9\u89d2\u7ebf\u4e0a\u6240\u6709\u683c\u5b50\u7684\u884c\u7d22\u5f15\u51cf\u5217\u7d22\u5f15\u90fd\u76f8\u7b49\uff0c\u5373\u5bf9\u89d2\u7ebf\u4e0a\u6240\u6709\u683c\u5b50\u7684 \\(row - col\\) \u4e3a\u6052\u5b9a\u503c\u3002

\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4e24\u4e2a\u683c\u5b50\u6ee1\u8db3 \\(row_1 - col_1 = row_2 - col_2\\) \uff0c\u5219\u5b83\u4eec\u4e00\u5b9a\u5904\u5728\u540c\u4e00\u6761\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u3002\u5229\u7528\u8be5\u89c4\u5f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u501f\u52a9\u56fe 13-18 \u6240\u793a\u7684\u6570\u7ec4 diag1 \uff0c\u8bb0\u5f55\u6bcf\u6761\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\u3002

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

\u56fe 13-18 \u00a0 \u5904\u7406\u5217\u7ea6\u675f\u548c\u5bf9\u89d2\u7ebf\u7ea6\u675f

"},{"location":"chapter_backtracking/n_queens_problem/#3","title":"3. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u8bf7\u6ce8\u610f\uff0c\\(n\\) \u7ef4\u65b9\u9635\u4e2d \\(row - col\\) \u7684\u8303\u56f4\u662f \\([-n + 1, n - 1]\\) \uff0c\\(row + col\\) \u7684\u8303\u56f4\u662f \\([0, 2n - 2]\\) \uff0c\u6240\u4ee5\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\u7684\u6570\u91cf\u90fd\u4e3a \\(2n - 1\\) \uff0c\u5373\u6570\u7ec4 diag1 \u548c diag2 \u7684\u957f\u5ea6\u90fd\u4e3a \\(2n - 1\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\u7687\u540e\nif not cols[col] and not diags1[diag1] and not 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.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\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\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.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\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\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.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();\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\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\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();\nfor (int i = 0; i < n; i++) {\nList<string> row = new();\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();\nBacktrack(0, n, state, res, cols, diags1, diags2);\nreturn res;\n}\n
n_queens.go
/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nfunc backtrack(row, n int, state *[][]string, res *[][][]string, cols, diags1, diags2 *[]bool) {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\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\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\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\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\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.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\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\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.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\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\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\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\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.dart
/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nvoid backtrack(\nint row,\nint n,\nList<List<String>> state,\nList<List<List<String>>> res,\nList<bool> cols,\nList<bool> diags1,\nList<bool> diags2,\n) {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (row == n) {\nList<List<String>> copyState = [];\nfor (List<String> sRow in state) {\ncopyState.add(List.from(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\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\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 + 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] = false;\ndiags1[diag1] = false;\ndiags2[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 = List.generate(n, (index) => List.filled(n, \"#\"));\nList<bool> cols = List.filled(n, false); // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nList<bool> diags1 = List.filled(2 * n - 1, false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nList<bool> diags2 = List.filled(2 * n - 1, false); // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nList<List<List<String>>> res = [];\nbacktrack(0, n, state, res, cols, diags1, diags2);\nreturn res;\n}\n
n_queens.rs
/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nfn backtrack(row: usize, n: usize, state: &mut Vec<Vec<String>>, res: &mut Vec<Vec<Vec<String>>>,\ncols: &mut [bool], diags1: &mut [bool], diags2: &mut [bool]) {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif row == n {\nlet mut copy_state: Vec<Vec<String>> = Vec::new();\nfor s_row in state.clone() {\ncopy_state.push(s_row);\n}\nres.push(copy_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 + n - 1 - col;\nlet diag2 = row + col;\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\u7687\u540e\nif !cols[col] && !diags1[diag1] && !diags2[diag2] {\n// \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\nstate.get_mut(row).unwrap()[col] = \"Q\".into();\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\nstate.get_mut(row).unwrap()[col] = \"#\".into();\n(cols[col], diags1[diag1], diags2[diag2]) = (false, false, false);\n}\n}\n}\n/* \u6c42\u89e3 N \u7687\u540e */\nfn n_queens(n: usize) -> Vec<Vec<Vec<String>>> {\n// \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\nlet mut state: Vec<Vec<String>> = Vec::new();\nfor _ in 0..n {\nlet mut row: Vec<String> = Vec::new();\nfor _ in 0..n {\nrow.push(\"#\".into());\n}\nstate.push(row);\n}\nlet mut cols = vec![false; n]; // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nlet mut diags1 = vec![false; 2 * n - 1]; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nlet mut diags2 = vec![false; 2 * n - 1]; // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nlet mut res: Vec<Vec<Vec<String>>> = Vec::new();\nbacktrack(0, n, &mut state, &mut res, &mut cols, &mut diags1, &mut diags2);\nres\n}\n
n_queens.c
/* \u653e\u7f6e\u7ed3\u679c */\nstruct result {\nchar ***data;\nint size;\n};\ntypedef struct result Result;\n/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nvoid backtrack(int row, int n, char state[MAX_N][MAX_N], Result *res, bool cols[MAX_N], bool diags1[2 * MAX_N - 1], bool diags2[2 * MAX_N - 1]) {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (row == n) {\nres->data[res->size] = (char **)malloc(sizeof(char *) * n);\nfor (int i = 0; i < n; ++i) {\nres->data[res->size][i] = (char *)malloc(sizeof(char) * (n + 1));\nstrcpy(res->data[res->size][i], state[i]);\n}\nres->size++;\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\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\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 */\nResult *nQueens(int n) {\nchar state[MAX_N][MAX_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\nfor (int i = 0; i < n; ++i) {\nfor (int j = 0; j < n; ++j) {\nstate[i][j] = '#';\n}\nstate[i][n] = '\\0';\n}\nbool cols[MAX_N] = {false};           // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nbool diags1[2 * MAX_N - 1] = {false}; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nbool diags2[2 * MAX_N - 1] = {false}; // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nResult *res = malloc(sizeof(Result));\nres->data = (char ***)malloc(sizeof(char **) * MAX_RES);\nres->size = 0;\nbacktrack(0, n, state, res, cols, diags1, diags2);\nreturn res;\n}\n
n_queens.zig
[class]{}-[func]{backtrack}\n[class]{}-[func]{nQueens}\n

\u9010\u884c\u653e\u7f6e \\(n\\) \u6b21\uff0c\u8003\u8651\u5217\u7ea6\u675f\uff0c\u5219\u4ece\u7b2c\u4e00\u884c\u5230\u6700\u540e\u4e00\u884c\u5206\u522b\u6709 \\(n\\)\u3001\\(n-1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \u4e2a\u9009\u62e9\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n!)\\) \u3002\u5b9e\u9645\u4e0a\uff0c\u6839\u636e\u5bf9\u89d2\u7ebf\u7ea6\u675f\u7684\u526a\u679d\u4e5f\u80fd\u591f\u5927\u5e45\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

\u6570\u7ec4 state \u4f7f\u7528 \\(O(n^2)\\) \u7a7a\u95f4\uff0c\u6570\u7ec4 cols\u3001diags1 \u548c diags2 \u7686\u4f7f\u7528 \\(O(n)\\) \u7a7a\u95f4\u3002\u6700\u5927\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002\u56e0\u6b64\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002

"},{"location":"chapter_backtracking/permutations_problem/","title":"13.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

\u8868 13-2 \u5217\u4e3e\u4e86\u51e0\u4e2a\u793a\u4f8b\u6570\u636e\uff0c\u5305\u62ec\u8f93\u5165\u6570\u7ec4\u548c\u5bf9\u5e94\u7684\u6240\u6709\u6392\u5217\u3002

\u8868 13-2 \u00a0 \u5168\u6392\u5217\u793a\u4f8b

\u8f93\u5165\u6570\u7ec4 \u6240\u6709\u6392\u5217 \\([1]\\) \\([1]\\) \\([1, 2]\\) \\([1, 2], [2, 1]\\) \\([1, 2, 3]\\) \\([1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]\\)"},{"location":"chapter_backtracking/permutations_problem/#1321","title":"13.2.1 \u00a0 \u65e0\u76f8\u7b49\u5143\u7d20\u7684\u60c5\u51b5","text":"

Question

\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\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\u4ee3\u7801\u7684\u89d2\u5ea6\u770b\uff0c\u5019\u9009\u96c6\u5408 choices \u662f\u8f93\u5165\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u72b6\u6001 state \u662f\u76f4\u81f3\u76ee\u524d\u5df2\u88ab\u9009\u62e9\u7684\u5143\u7d20\u3002\u8bf7\u6ce8\u610f\uff0c\u6bcf\u4e2a\u5143\u7d20\u53ea\u5141\u8bb8\u88ab\u9009\u62e9\u4e00\u6b21\uff0c\u56e0\u6b64 state \u4e2d\u7684\u6240\u6709\u5143\u7d20\u90fd\u5e94\u8be5\u662f\u552f\u4e00\u7684\u3002

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

\u56fe 13-5 \u00a0 \u5168\u6392\u5217\u7684\u9012\u5f52\u6811

"},{"location":"chapter_backtracking/permutations_problem/#1","title":"1. \u00a0 \u91cd\u590d\u9009\u62e9\u526a\u679d","text":"

\u4e3a\u4e86\u5b9e\u73b0\u6bcf\u4e2a\u5143\u7d20\u53ea\u88ab\u9009\u62e9\u4e00\u6b21\uff0c\u6211\u4eec\u8003\u8651\u5f15\u5165\u4e00\u4e2a\u5e03\u5c14\u578b\u6570\u7ec4 selected \uff0c\u5176\u4e2d selected[i] \u8868\u793a choices[i] \u662f\u5426\u5df2\u88ab\u9009\u62e9\uff0c\u5e76\u57fa\u4e8e\u5b83\u5b9e\u73b0\u4ee5\u4e0b\u526a\u679d\u64cd\u4f5c\u3002

  • \u5728\u505a\u51fa\u9009\u62e9 choice[i] \u540e\uff0c\u6211\u4eec\u5c31\u5c06 selected[i] \u8d4b\u503c\u4e3a \\(\\text{True}\\) \uff0c\u4ee3\u8868\u5b83\u5df2\u88ab\u9009\u62e9\u3002
  • \u904d\u5386\u9009\u62e9\u5217\u8868 choices \u65f6\uff0c\u8df3\u8fc7\u6240\u6709\u5df2\u88ab\u9009\u62e9\u8fc7\u7684\u8282\u70b9\uff0c\u5373\u526a\u679d\u3002

\u5982\u56fe 13-6 \u6240\u793a\uff0c\u5047\u8bbe\u6211\u4eec\u7b2c\u4e00\u8f6e\u9009\u62e9 1 \uff0c\u7b2c\u4e8c\u8f6e\u9009\u62e9 3 \uff0c\u7b2c\u4e09\u8f6e\u9009\u62e9 2 \uff0c\u5219\u9700\u8981\u5728\u7b2c\u4e8c\u8f6e\u526a\u6389\u5143\u7d20 1 \u7684\u5206\u652f\uff0c\u5728\u7b2c\u4e09\u8f6e\u526a\u6389\u5143\u7d20 1 \u548c\u5143\u7d20 3 \u7684\u5206\u652f\u3002

\u56fe 13-6 \u00a0 \u5168\u6392\u5217\u526a\u679d\u793a\u4f8b

\u89c2\u5bdf\u56fe 13-6 \u53d1\u73b0\uff0c\u8be5\u526a\u679d\u64cd\u4f5c\u5c06\u641c\u7d22\u7a7a\u95f4\u5927\u5c0f\u4ece \\(O(n^n)\\) \u964d\u4f4e\u81f3 \\(O(n!)\\) \u3002

"},{"location":"chapter_backtracking/permutations_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u60f3\u6e05\u695a\u4ee5\u4e0a\u4fe1\u606f\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u6846\u67b6\u4ee3\u7801\u4e2d\u505a\u201c\u5b8c\u5f62\u586b\u7a7a\u201d\u4e86\u3002\u4e3a\u4e86\u7f29\u77ed\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

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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();\nBacktrack(new List<int>(), nums, new bool[nums.Length], res);\nreturn res;\n}\n
permutations_i.go
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunc backtrackI(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\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.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.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.dart
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(\nList<int> state,\nList<int> choices,\nList<bool> selected,\nList<List<int>> res,\n) {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (state.length == choices.length) {\nres.add(List.from(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.removeLast();\n}\n}\n}\n/* \u5168\u6392\u5217 I */\nList<List<int>> permutationsI(List<int> nums) {\nList<List<int>> res = [];\nbacktrack([], nums, List.filled(nums.length, false), res);\nreturn res;\n}\n
permutations_i.rs
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfn backtrack(mut state: Vec<i32>, choices: &[i32], selected: &mut [bool], res: &mut Vec<Vec<i32>>) {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif state.len() == choices.len() {\nres.push(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor i in 0..choices.len() {\nlet 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(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state.clone(), 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.len() - 1);\n}\n}\n}\n/* \u5168\u6392\u5217 I */\nfn permutations_i(nums: &mut [i32]) -> Vec<Vec<i32>> {\nlet mut res = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nbacktrack(Vec::new(), nums, &mut vec![false; nums.len()], &mut res);\nres\n}\n
permutations_i.c
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(vector *state, vector *choices, vector *selected, vector *res) {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (state->size == choices->size) {\nvector *newState = newVector();\nfor (int i = 0; i < state->size; i++) {\nvectorPushback(newState, state->data[i], sizeof(int));\n}\nvectorPushback(res, newState, sizeof(vector));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i < choices->size; i++) {\nint *choice = malloc(sizeof(int));\n*choice = *((int *)(choices->data[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\nbool select = *((bool *)(selected->data[i]));\nif (!select) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n*((bool *)selected->data[i]) = true;\nvectorPushback(state, choice, sizeof(int));\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\n*((bool *)selected->data[i]) = false;\nvectorPopback(state);\n}\n}\n}\n/* \u5168\u6392\u5217 I */\nvector *permutationsI(vector *nums) {\nvector *iState = newVector();\nint select[3] = {false, false, false};\nvector *bSelected = newVector();\nfor (int i = 0; i < nums->size; i++) {\nvectorPushback(bSelected, &select[i], sizeof(int));\n}\nvector *res = newVector();\n// \u524d\u5e8f\u904d\u5386\nbacktrack(iState, nums, bSelected, res);\nreturn res;\n}\n
permutations_i.zig
[class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsI}\n
"},{"location":"chapter_backtracking/permutations_problem/#1322","title":"13.2.2 \u00a0 \u8003\u8651\u76f8\u7b49\u5143\u7d20\u7684\u60c5\u51b5","text":"

Question

\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u8fd4\u56de\u6240\u6709\u4e0d\u91cd\u590d\u7684\u6392\u5217\u3002

\u5047\u8bbe\u8f93\u5165\u6570\u7ec4\u4e3a \\([1, 1, 2]\\) \u3002\u4e3a\u4e86\u65b9\u4fbf\u533a\u5206\u4e24\u4e2a\u91cd\u590d\u5143\u7d20 \\(1\\) \uff0c\u6211\u4eec\u5c06\u7b2c\u4e8c\u4e2a \\(1\\) \u8bb0\u4e3a \\(\\hat{1}\\) \u3002

\u5982\u56fe 13-7 \u6240\u793a\uff0c\u4e0a\u8ff0\u65b9\u6cd5\u751f\u6210\u7684\u6392\u5217\u6709\u4e00\u534a\u90fd\u662f\u91cd\u590d\u7684\u3002

\u56fe 13-7 \u00a0 \u91cd\u590d\u6392\u5217

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

"},{"location":"chapter_backtracking/permutations_problem/#1_1","title":"1. \u00a0 \u76f8\u7b49\u5143\u7d20\u526a\u679d","text":"

\u89c2\u5bdf\u56fe 13-8 \uff0c\u5728\u7b2c\u4e00\u8f6e\u4e2d\uff0c\u9009\u62e9 \\(1\\) \u6216\u9009\u62e9 \\(\\hat{1}\\) \u662f\u7b49\u4ef7\u7684\uff0c\u5728\u8fd9\u4e24\u4e2a\u9009\u62e9\u4e4b\u4e0b\u751f\u6210\u7684\u6240\u6709\u6392\u5217\u90fd\u662f\u91cd\u590d\u7684\u3002\u56e0\u6b64\u5e94\u8be5\u628a \\(\\hat{1}\\) \u526a\u679d\u6389\u3002

\u540c\u7406\uff0c\u5728\u7b2c\u4e00\u8f6e\u9009\u62e9 \\(2\\) \u4e4b\u540e\uff0c\u7b2c\u4e8c\u8f6e\u9009\u62e9\u4e2d\u7684 \\(1\\) \u548c \\(\\hat{1}\\) \u4e5f\u4f1a\u4ea7\u751f\u91cd\u590d\u5206\u652f\uff0c\u56e0\u6b64\u4e5f\u5e94\u5c06\u7b2c\u4e8c\u8f6e\u7684 \\(\\hat{1}\\) \u526a\u679d\u3002

\u672c\u8d28\u4e0a\u770b\uff0c\u6211\u4eec\u7684\u76ee\u6807\u662f\u5728\u67d0\u4e00\u8f6e\u9009\u62e9\u4e2d\uff0c\u4fdd\u8bc1\u591a\u4e2a\u76f8\u7b49\u7684\u5143\u7d20\u4ec5\u88ab\u9009\u62e9\u4e00\u6b21\u3002

\u56fe 13-8 \u00a0 \u91cd\u590d\u6392\u5217\u526a\u679d

"},{"location":"chapter_backtracking/permutations_problem/#2_1","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u5728\u4e0a\u4e00\u9898\u7684\u4ee3\u7801\u7684\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u8003\u8651\u5728\u6bcf\u4e00\u8f6e\u9009\u62e9\u4e2d\u5f00\u542f\u4e00\u4e2a\u54c8\u5e0c\u8868 duplicated \uff0c\u7528\u4e8e\u8bb0\u5f55\u8be5\u8f6e\u4e2d\u5df2\u7ecf\u5c1d\u8bd5\u8fc7\u7684\u5143\u7d20\uff0c\u5e76\u5c06\u91cd\u590d\u5143\u7d20\u526a\u679d\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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();\nBacktrack(new List<int>(), nums, new bool[nums.Length], res);\nreturn res;\n}\n
permutations_ii.go
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunc backtrackII(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\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.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.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.dart
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid backtrack(\nList<int> state,\nList<int> choices,\nList<bool> selected,\nList<List<int>> res,\n) {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (state.length == choices.length) {\nres.add(List.from(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nSet<int> duplicated = {};\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.removeLast();\n}\n}\n}\n/* \u5168\u6392\u5217 II */\nList<List<int>> permutationsII(List<int> nums) {\nList<List<int>> res = [];\nbacktrack([], nums, List.filled(nums.length, false), res);\nreturn res;\n}\n
permutations_ii.rs
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfn backtrack(mut state: Vec<i32>, choices: &[i32], selected: &mut [bool], res: &mut Vec<Vec<i32>>) {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif state.len() == choices.len() {\nres.push(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nlet mut duplicated = HashSet::<i32>::new();\nfor i in 0..choices.len() {\nlet 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.insert(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.clone(), 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.len() - 1);\n}\n}\n}\n/* \u5168\u6392\u5217 II */\nfn permutations_ii(nums: &mut [i32]) -> Vec<Vec<i32>> {\nlet mut res = Vec::new();\nbacktrack(Vec::new(), nums, &mut vec![false; nums.len()], &mut res);\nres\n}\n
permutations_ii.c
[class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsII}\n
permutations_ii.zig
[class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsII}\n

\u5047\u8bbe\u5143\u7d20\u4e24\u4e24\u4e4b\u95f4\u4e92\u4e0d\u76f8\u540c\uff0c\u5219 \\(n\\) \u4e2a\u5143\u7d20\u5171\u6709 \\(n!\\) \u79cd\u6392\u5217\uff08\u9636\u4e58\uff09\uff1b\u5728\u8bb0\u5f55\u7ed3\u679c\u65f6\uff0c\u9700\u8981\u590d\u5236\u957f\u5ea6\u4e3a \\(n\\) \u7684\u5217\u8868\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n!n)\\) \u3002

\u6700\u5927\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002selected \u4f7f\u7528 \\(O(n)\\) \u7a7a\u95f4\u3002\u540c\u4e00\u65f6\u523b\u6700\u591a\u5171\u6709 \\(n\\) \u4e2a duplicated \uff0c\u4f7f\u7528 \\(O(n^2)\\) \u7a7a\u95f4\u3002\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002

"},{"location":"chapter_backtracking/permutations_problem/#3","title":"3. \u00a0 \u4e24\u79cd\u526a\u679d\u5bf9\u6bd4","text":"

\u8bf7\u6ce8\u610f\uff0c\u867d\u7136 selected \u548c duplicated \u90fd\u7528\u4f5c\u526a\u679d\uff0c\u4f46\u4e24\u8005\u7684\u76ee\u6807\u662f\u4e0d\u540c\u7684\u3002

  • \u91cd\u590d\u9009\u62e9\u526a\u679d\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
  • \u76f8\u7b49\u5143\u7d20\u526a\u679d\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\u3002

\u56fe 13-9 \u5c55\u793a\u4e86\u4e24\u4e2a\u526a\u679d\u6761\u4ef6\u7684\u751f\u6548\u8303\u56f4\u3002\u6ce8\u610f\uff0c\u6811\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u4ee3\u8868\u4e00\u4e2a\u9009\u62e9\uff0c\u4ece\u6839\u8282\u70b9\u5230\u53f6\u8282\u70b9\u7684\u8def\u5f84\u4e0a\u7684\u5404\u4e2a\u8282\u70b9\u6784\u6210\u4e00\u4e2a\u6392\u5217\u3002

\u56fe 13-9 \u00a0 \u4e24\u79cd\u526a\u679d\u6761\u4ef6\u7684\u4f5c\u7528\u8303\u56f4

"},{"location":"chapter_backtracking/subset_sum_problem/","title":"13.3 \u00a0 \u5b50\u96c6\u548c\u95ee\u9898","text":""},{"location":"chapter_backtracking/subset_sum_problem/#1331","title":"13.3.1 \u00a0 \u65e0\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"

Question

\u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570\u6570\u7ec4 nums \u548c\u4e00\u4e2a\u76ee\u6807\u6b63\u6574\u6570 target \uff0c\u8bf7\u627e\u51fa\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\uff0c\u4f7f\u5f97\u7ec4\u5408\u4e2d\u7684\u5143\u7d20\u548c\u7b49\u4e8e target \u3002\u7ed9\u5b9a\u6570\u7ec4\u65e0\u91cd\u590d\u5143\u7d20\uff0c\u6bcf\u4e2a\u5143\u7d20\u53ef\u4ee5\u88ab\u9009\u53d6\u591a\u6b21\u3002\u8bf7\u4ee5\u5217\u8868\u5f62\u5f0f\u8fd4\u56de\u8fd9\u4e9b\u7ec4\u5408\uff0c\u5217\u8868\u4e2d\u4e0d\u5e94\u5305\u542b\u91cd\u590d\u7ec4\u5408\u3002

\u4f8b\u5982\uff0c\u8f93\u5165\u96c6\u5408 \\(\\{3, 4, 5\\}\\) \u548c\u76ee\u6807\u6574\u6570 \\(9\\) \uff0c\u89e3\u4e3a \\(\\{3, 3, 3\\}, \\{4, 5\\}\\) \u3002\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u4e24\u70b9\u3002

  • \u8f93\u5165\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u88ab\u65e0\u9650\u6b21\u91cd\u590d\u9009\u53d6\u3002
  • \u5b50\u96c6\u662f\u4e0d\u533a\u5206\u5143\u7d20\u987a\u5e8f\u7684\uff0c\u6bd4\u5982 \\(\\{4, 5\\}\\) \u548c \\(\\{5, 4\\}\\) \u662f\u540c\u4e00\u4e2a\u5b50\u96c6\u3002
"},{"location":"chapter_backtracking/subset_sum_problem/#1","title":"1. \u00a0 \u53c2\u8003\u5168\u6392\u5217\u89e3\u6cd5","text":"

\u7c7b\u4f3c\u4e8e\u5168\u6392\u5217\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u5b50\u96c6\u7684\u751f\u6210\u8fc7\u7a0b\u60f3\u8c61\u6210\u4e00\u7cfb\u5217\u9009\u62e9\u7684\u7ed3\u679c\uff0c\u5e76\u5728\u9009\u62e9\u8fc7\u7a0b\u4e2d\u5b9e\u65f6\u66f4\u65b0\u201c\u5143\u7d20\u548c\u201d\uff0c\u5f53\u5143\u7d20\u548c\u7b49\u4e8e target \u65f6\uff0c\u5c31\u5c06\u5b50\u96c6\u8bb0\u5f55\u81f3\u7ed3\u679c\u5217\u8868\u3002

\u800c\u4e0e\u5168\u6392\u5217\u95ee\u9898\u4e0d\u540c\u7684\u662f\uff0c\u672c\u9898\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u88ab\u65e0\u9650\u6b21\u9009\u53d6\uff0c\u56e0\u6b64\u65e0\u987b\u501f\u52a9 selected \u5e03\u5c14\u5217\u8868\u6765\u8bb0\u5f55\u5143\u7d20\u662f\u5426\u5df2\u88ab\u9009\u62e9\u3002\u6211\u4eec\u53ef\u4ee5\u5bf9\u5168\u6392\u5217\u4ee3\u7801\u8fdb\u884c\u5c0f\u5e45\u4fee\u6539\uff0c\u521d\u6b65\u5f97\u5230\u89e3\u9898\u4ee3\u7801\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig subset_sum_i_naive.py
def backtrack(\nstate: list[int],\ntarget: int,\ntotal: int,\nchoices: list[int],\nres: list[list[int]],\n):\n\"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I\"\"\"\n# \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif total == target:\nres.append(list(state))\nreturn\n# \u904d\u5386\u6240\u6709\u9009\u62e9\nfor i in range(len(choices)):\n# \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif total + choices[i] > target:\ncontinue\n# \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.append(choices[i])\n# \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target, total + choices[i], choices, res)\n# \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop()\ndef subset_sum_i_naive(nums: list[int], target: int) -> list[list[int]]:\n\"\"\"\u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09\"\"\"\nstate = []  # \u72b6\u6001\uff08\u5b50\u96c6\uff09\ntotal = 0  # \u5b50\u96c6\u548c\nres = []  # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, total, nums, res)\nreturn res\n
subset_sum_i_naive.cpp
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(vector<int> &state, int target, int total, vector<int> &choices, vector<vector<int>> &res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (total == target) {\nres.push_back(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (size_t i = 0; i < choices.size(); i++) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif (total + choices[i] > target) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.push_back(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target, total + choices[i], choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop_back();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nvector<vector<int>> subsetSumINaive(vector<int> &nums, int target) {\nvector<int> state;       // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nint total = 0;           // \u5b50\u96c6\u548c\nvector<vector<int>> res; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, total, nums, res);\nreturn res;\n}\n
subset_sum_i_naive.java
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(List<Integer> state, int target, int total, int[] choices, List<List<Integer>> res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (total == target) {\nres.add(new ArrayList<>(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i < choices.length; i++) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif (total + choices[i] > target) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target, total + choices[i], choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.remove(state.size() - 1);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nList<List<Integer>> subsetSumINaive(int[] nums, int target) {\nList<Integer> state = new ArrayList<>(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nint total = 0; // \u5b50\u96c6\u548c\nList<List<Integer>> res = new ArrayList<>(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, total, nums, res);\nreturn res;\n}\n
subset_sum_i_naive.cs
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid Backtrack(List<int> state, int target, int total, int[] choices, List<List<int>> res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (total == target) {\nres.Add(new List<int>(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i < choices.Length; i++) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif (total + choices[i] > target) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.Add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nBacktrack(state, target, total + choices[i], choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.RemoveAt(state.Count - 1);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nList<List<int>> SubsetSumINaive(int[] nums, int target) {\nList<int> state = new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nint total = 0; // \u5b50\u96c6\u548c\nList<List<int>> res = new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nBacktrack(state, target, total, nums, res);\nreturn res;\n}\n
subset_sum_i_naive.go
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrackSubsetSumINaive(total, target int, state, choices *[]int, res *[][]int) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == total {\nnewState := append([]int{}, *state...)\n*res = append(*res, newState)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor i := 0; i < len(*choices); i++ {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif total+(*choices)[i] > target {\ncontinue\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n*state = append(*state, (*choices)[i])\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrackSubsetSumINaive(total+(*choices)[i], target, state, choices, res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n*state = (*state)[:len(*state)-1]\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunc subsetSumINaive(nums []int, target int) [][]int {\nstate := make([]int, 0) // \u72b6\u6001\uff08\u5b50\u96c6\uff09\ntotal := 0              // \u5b50\u96c6\u548c\nres := make([][]int, 0) // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrackSubsetSumINaive(total, target, &state, &nums, &res)\nreturn res\n}\n
subset_sum_i_naive.swift
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrack(state: inout [Int], target: Int, total: Int, choices: [Int], res: inout [[Int]]) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif total == target {\nres.append(state)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor i in stride(from: 0, to: choices.count, by: 1) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif total + choices[i] > target {\ncontinue\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.append(choices[i])\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state: &state, target: target, total: total + choices[i], choices: choices, res: &res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.removeLast()\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunc subsetSumINaive(nums: [Int], target: Int) -> [[Int]] {\nvar state: [Int] = [] // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nlet total = 0 // \u5b50\u96c6\u548c\nvar res: [[Int]] = [] // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state: &state, target: target, total: total, choices: nums, res: &res)\nreturn res\n}\n
subset_sum_i_naive.js
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(state, target, total, choices, res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (total === target) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (let i = 0; i < choices.length; i++) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif (total + choices[i] > target) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target, total + choices[i], choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunction subsetSumINaive(nums, target) {\nconst state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nconst total = 0; // \u5b50\u96c6\u548c\nconst res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, total, nums, res);\nreturn res;\n}\n
subset_sum_i_naive.ts
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(\nstate: number[],\ntarget: number,\ntotal: number,\nchoices: number[],\nres: number[][]\n): void {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (total === target) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (let i = 0; i < choices.length; i++) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif (total + choices[i] > target) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target, total + choices[i], choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunction subsetSumINaive(nums: number[], target: number): number[][] {\nconst state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nconst total = 0; // \u5b50\u96c6\u548c\nconst res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, total, nums, res);\nreturn res;\n}\n
subset_sum_i_naive.dart
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(\nList<int> state,\nint target,\nint total,\nList<int> choices,\nList<List<int>> res,\n) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (total == target) {\nres.add(List.from(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i < choices.length; i++) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif (total + choices[i] > target) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target, total + choices[i], choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.removeLast();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nList<List<int>> subsetSumINaive(List<int> nums, int target) {\nList<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nint total = 0; // \u5143\u7d20\u548c\nList<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, total, nums, res);\nreturn res;\n}\n
subset_sum_i_naive.rs
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfn backtrack(mut state: Vec<i32>, target: i32, total: i32, choices: &[i32], res: &mut Vec<Vec<i32>>) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif total == target {\nres.push(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor i in 0..choices.len() {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif total + choices[i] > target {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state.clone(), target, total + choices[i], choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfn subset_sum_i_naive(nums: &[i32], target: i32) -> Vec<Vec<i32>> {\nlet state = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nlet total = 0; // \u5b50\u96c6\u548c\nlet mut res = Vec::new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, total, nums, &mut res);\nres\n}\n
subset_sum_i_naive.c
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(vector *state, int target, int total, vector *choices, vector *res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (total == target) {\nvector *tmpVector = newVector();\nfor (int i = 0; i < state->size; i++) {\nvectorPushback(tmpVector, state->data[i], sizeof(int));\n}\nvectorPushback(res, tmpVector, sizeof(vector));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (size_t i = 0; i < choices->size; i++) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif (total + *(int *)(choices->data[i]) > target) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nvectorPushback(state, choices->data[i], sizeof(int));\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target, total + *(int *)(choices->data[i]), choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nvectorPopback(state);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nvector *subsetSumINaive(vector *nums, int target) {\nvector *state = newVector(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nint total = 0;               // \u5b50\u96c6\u548c\nvector *res = newVector();   // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, total, nums, res);\nreturn res;\n}\n
subset_sum_i_naive.zig
[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumINaive}\n

\u5411\u4ee5\u4e0a\u4ee3\u7801\u8f93\u5165\u6570\u7ec4 \\([3, 4, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a \\([3, 3, 3], [4, 5], [5, 4]\\) \u3002\u867d\u7136\u6210\u529f\u627e\u51fa\u4e86\u6240\u6709\u548c\u4e3a \\(9\\) \u7684\u5b50\u96c6\uff0c\u4f46\u5176\u4e2d\u5b58\u5728\u91cd\u590d\u7684\u5b50\u96c6 \\([4, 5]\\) \u548c \\([5, 4]\\) \u3002

\u8fd9\u662f\u56e0\u4e3a\u641c\u7d22\u8fc7\u7a0b\u662f\u533a\u5206\u9009\u62e9\u987a\u5e8f\u7684\uff0c\u7136\u800c\u5b50\u96c6\u4e0d\u533a\u5206\u9009\u62e9\u987a\u5e8f\u3002\u5982\u56fe 13-10 \u6240\u793a\uff0c\u5148\u9009 \\(4\\) \u540e\u9009 \\(5\\) \u4e0e\u5148\u9009 \\(5\\) \u540e\u9009 \\(4\\) \u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u5206\u652f\uff0c\u4f46\u4e24\u8005\u5bf9\u5e94\u540c\u4e00\u4e2a\u5b50\u96c6\u3002

\u56fe 13-10 \u00a0 \u5b50\u96c6\u641c\u7d22\u4e0e\u8d8a\u754c\u526a\u679d

\u4e3a\u4e86\u53bb\u9664\u91cd\u590d\u5b50\u96c6\uff0c\u4e00\u79cd\u76f4\u63a5\u7684\u601d\u8def\u662f\u5bf9\u7ed3\u679c\u5217\u8868\u8fdb\u884c\u53bb\u91cd\u3002\u4f46\u8fd9\u4e2a\u65b9\u6cd5\u6548\u7387\u5f88\u4f4e\uff0c\u6709\u4e24\u65b9\u9762\u539f\u56e0\u3002

  • \u5f53\u6570\u7ec4\u5143\u7d20\u8f83\u591a\uff0c\u5c24\u5176\u662f\u5f53 target \u8f83\u5927\u65f6\uff0c\u641c\u7d22\u8fc7\u7a0b\u4f1a\u4ea7\u751f\u5927\u91cf\u7684\u91cd\u590d\u5b50\u96c6\u3002
  • \u6bd4\u8f83\u5b50\u96c6\uff08\u6570\u7ec4\uff09\u7684\u5f02\u540c\u975e\u5e38\u8017\u65f6\uff0c\u9700\u8981\u5148\u6392\u5e8f\u6570\u7ec4\uff0c\u518d\u6bd4\u8f83\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u5f02\u540c\u3002
"},{"location":"chapter_backtracking/subset_sum_problem/#2","title":"2. \u00a0 \u91cd\u590d\u5b50\u96c6\u526a\u679d","text":"

\u6211\u4eec\u8003\u8651\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u901a\u8fc7\u526a\u679d\u8fdb\u884c\u53bb\u91cd\u3002\u89c2\u5bdf\u56fe 13-11 \uff0c\u91cd\u590d\u5b50\u96c6\u662f\u5728\u4ee5\u4e0d\u540c\u987a\u5e8f\u9009\u62e9\u6570\u7ec4\u5143\u7d20\u65f6\u4ea7\u751f\u7684\uff0c\u4f8b\u5982\u4ee5\u4e0b\u60c5\u51b5\u3002

  1. \u5f53\u7b2c\u4e00\u8f6e\u548c\u7b2c\u4e8c\u8f6e\u5206\u522b\u9009\u62e9 \\(3\\) \u548c \\(4\\) \u65f6\uff0c\u4f1a\u751f\u6210\u5305\u542b\u8fd9\u4e24\u4e2a\u5143\u7d20\u7684\u6240\u6709\u5b50\u96c6\uff0c\u8bb0\u4e3a \\([3, 4, \\dots]\\) \u3002
  2. \u4e4b\u540e\uff0c\u5f53\u7b2c\u4e00\u8f6e\u9009\u62e9 \\(4\\) \u65f6\uff0c\u5219\u7b2c\u4e8c\u8f6e\u5e94\u8be5\u8df3\u8fc7 \\(3\\) \uff0c\u56e0\u4e3a\u8be5\u9009\u62e9\u4ea7\u751f\u7684\u5b50\u96c6 \\([4, 3, \\dots]\\) \u548c 1. \u4e2d\u751f\u6210\u7684\u5b50\u96c6\u5b8c\u5168\u91cd\u590d\u3002

\u5728\u641c\u7d22\u4e2d\uff0c\u6bcf\u4e00\u5c42\u7684\u9009\u62e9\u90fd\u662f\u4ece\u5de6\u5230\u53f3\u88ab\u9010\u4e2a\u5c1d\u8bd5\u7684\uff0c\u56e0\u6b64\u8d8a\u9760\u53f3\u7684\u5206\u652f\u88ab\u526a\u6389\u7684\u8d8a\u591a\u3002

  1. \u524d\u4e24\u8f6e\u9009\u62e9 \\(3\\) \u548c \\(5\\) \uff0c\u751f\u6210\u5b50\u96c6 \\([3, 5, \\dots]\\) \u3002
  2. \u524d\u4e24\u8f6e\u9009\u62e9 \\(4\\) \u548c \\(5\\) \uff0c\u751f\u6210\u5b50\u96c6 \\([4, 5, \\dots]\\) \u3002
  3. \u82e5\u7b2c\u4e00\u8f6e\u9009\u62e9 \\(5\\) \uff0c\u5219\u7b2c\u4e8c\u8f6e\u5e94\u8be5\u8df3\u8fc7 \\(3\\) \u548c \\(4\\) \uff0c\u56e0\u4e3a\u5b50\u96c6 \\([5, 3, \\dots]\\) \u548c \\([5, 4, \\dots]\\) \u4e0e\u7b2c 1. \u548c 2. \u6b65\u4e2d\u63cf\u8ff0\u7684\u5b50\u96c6\u5b8c\u5168\u91cd\u590d\u3002

\u56fe 13-11 \u00a0 \u4e0d\u540c\u9009\u62e9\u987a\u5e8f\u5bfc\u81f4\u7684\u91cd\u590d\u5b50\u96c6

\u603b\u7ed3\u6765\u770b\uff0c\u7ed9\u5b9a\u8f93\u5165\u6570\u7ec4 \\([x_1, x_2, \\dots, x_n]\\) \uff0c\u8bbe\u641c\u7d22\u8fc7\u7a0b\u4e2d\u7684\u9009\u62e9\u5e8f\u5217\u4e3a \\([x_{i_1}, x_{i_2}, \\dots, x_{i_m}]\\) \uff0c\u5219\u8be5\u9009\u62e9\u5e8f\u5217\u9700\u8981\u6ee1\u8db3 \\(i_1 \\leq i_2 \\leq \\dots \\leq i_m\\) \uff0c\u4e0d\u6ee1\u8db3\u8be5\u6761\u4ef6\u7684\u9009\u62e9\u5e8f\u5217\u90fd\u4f1a\u9020\u6210\u91cd\u590d\uff0c\u5e94\u5f53\u526a\u679d\u3002

"},{"location":"chapter_backtracking/subset_sum_problem/#3","title":"3. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u4e3a\u5b9e\u73b0\u8be5\u526a\u679d\uff0c\u6211\u4eec\u521d\u59cb\u5316\u53d8\u91cf start \uff0c\u7528\u4e8e\u6307\u793a\u904d\u5386\u8d77\u70b9\u3002\u5f53\u505a\u51fa\u9009\u62e9 \\(x_{i}\\) \u540e\uff0c\u8bbe\u5b9a\u4e0b\u4e00\u8f6e\u4ece\u7d22\u5f15 \\(i\\) \u5f00\u59cb\u904d\u5386\u3002\u8fd9\u6837\u505a\u5c31\u53ef\u4ee5\u8ba9\u9009\u62e9\u5e8f\u5217\u6ee1\u8db3 \\(i_1 \\leq i_2 \\leq \\dots \\leq i_m\\) \uff0c\u4ece\u800c\u4fdd\u8bc1\u5b50\u96c6\u552f\u4e00\u3002

\u9664\u6b64\u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u5bf9\u4ee3\u7801\u8fdb\u884c\u4e86\u4ee5\u4e0b\u4e24\u9879\u4f18\u5316\u3002

  • \u5728\u5f00\u542f\u641c\u7d22\u524d\uff0c\u5148\u5c06\u6570\u7ec4 nums \u6392\u5e8f\u3002\u5728\u904d\u5386\u6240\u6709\u9009\u62e9\u65f6\uff0c\u5f53\u5b50\u96c6\u548c\u8d85\u8fc7 target \u65f6\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\uff0c\u56e0\u4e3a\u540e\u8fb9\u7684\u5143\u7d20\u66f4\u5927\uff0c\u5176\u5b50\u96c6\u548c\u90fd\u4e00\u5b9a\u4f1a\u8d85\u8fc7 target \u3002
  • \u7701\u53bb\u5143\u7d20\u548c\u53d8\u91cf total \uff0c\u901a\u8fc7\u5728 target \u4e0a\u6267\u884c\u51cf\u6cd5\u6765\u7edf\u8ba1\u5143\u7d20\u548c\uff0c\u5f53 target \u7b49\u4e8e \\(0\\) \u65f6\u8bb0\u5f55\u89e3\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig subset_sum_i.py
def backtrack(\nstate: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n\"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I\"\"\"\n# \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == 0:\nres.append(list(state))\nreturn\n# \u904d\u5386\u6240\u6709\u9009\u62e9\n# \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor i in range(start, len(choices)):\n# \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n# \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif target - choices[i] < 0:\nbreak\n# \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.append(choices[i])\n# \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i, res)\n# \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop()\ndef subset_sum_i(nums: list[int], target: int) -> list[list[int]]:\n\"\"\"\u6c42\u89e3\u5b50\u96c6\u548c I\"\"\"\nstate = []  # \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort()  # \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nstart = 0  # \u904d\u5386\u8d77\u59cb\u70b9\nres = []  # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res)\nreturn res\n
subset_sum_i.cpp
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(vector<int> &state, int target, vector<int> &choices, int start, vector<vector<int>> &res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nres.push_back(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor (int i = start; i < choices.size(); i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] < 0) {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.push_back(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop_back();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nvector<vector<int>> subsetSumI(vector<int> &nums, int target) {\nvector<int> state;              // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nsort(nums.begin(), nums.end()); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0;                  // \u904d\u5386\u8d77\u59cb\u70b9\nvector<vector<int>> res;        // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_i.java
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nres.add(new ArrayList<>(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor (int i = start; i < choices.length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] < 0) {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.remove(state.size() - 1);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nList<List<Integer>> subsetSumI(int[] nums, int target) {\nList<Integer> state = new ArrayList<>(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nArrays.sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nList<List<Integer>> res = new ArrayList<>(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_i.cs
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nres.Add(new List<int>(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor (int i = start; i < choices.Length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] < 0) {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.Add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nBacktrack(state, target - choices[i], choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.RemoveAt(state.Count - 1);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nList<List<int>> SubsetSumI(int[] nums, int target) {\nList<int> state = new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nArray.Sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nList<List<int>> res = new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nBacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_i.go
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrackSubsetSumI(start, target int, state, choices *[]int, res *[][]int) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == 0 {\nnewState := append([]int{}, *state...)\n*res = append(*res, newState)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor i := start; i < len(*choices); i++ {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif target-(*choices)[i] < 0 {\nbreak\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n*state = append(*state, (*choices)[i])\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrackSubsetSumI(i, target-(*choices)[i], state, choices, res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n*state = (*state)[:len(*state)-1]\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunc subsetSumI(nums []int, target int) [][]int {\nstate := make([]int, 0) // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nsort.Ints(nums)         // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nstart := 0              // \u904d\u5386\u8d77\u59cb\u70b9\nres := make([][]int, 0) // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrackSubsetSumI(start, target, &state, &nums, &res)\nreturn res\n}\n
subset_sum_i.swift
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == 0 {\nres.append(state)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor i in stride(from: start, to: choices.count, by: 1) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif target - choices[i] < 0 {\nbreak\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.append(choices[i])\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state: &state, target: target - choices[i], choices: choices, start: i, res: &res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.removeLast()\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunc subsetSumI(nums: [Int], target: Int) -> [[Int]] {\nvar state: [Int] = [] // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nlet nums = nums.sorted() // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nlet start = 0 // \u904d\u5386\u8d77\u59cb\u70b9\nvar res: [[Int]] = [] // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state: &state, target: target, choices: nums, start: start, res: &res)\nreturn res\n}\n
subset_sum_i.js
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(state, target, choices, start, res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target === 0) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor (let i = start; i < choices.length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] < 0) {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunction subsetSumI(nums, target) {\nconst state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort((a, b) => a - b); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nconst start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nconst res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_i.ts
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(\nstate: number[],\ntarget: number,\nchoices: number[],\nstart: number,\nres: number[][]\n): void {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target === 0) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor (let i = start; i < choices.length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] < 0) {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunction subsetSumI(nums: number[], target: number): number[][] {\nconst state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort((a, b) => a - b); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nconst start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nconst res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_i.dart
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(\nList<int> state,\nint target,\nList<int> choices,\nint start,\nList<List<int>> res,\n) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nres.add(List.from(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor (int i = start; i < choices.length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] < 0) {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.removeLast();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nList<List<int>> subsetSumI(List<int> nums, int target) {\nList<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nList<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_i.rs
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfn backtrack(mut state: Vec<i32>, target: i32, choices: &[i32], start: usize, res: &mut Vec<Vec<i32>>) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == 0 {\nres.push(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor i in start..choices.len() {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif target - choices[i] < 0 {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state.clone(), target - choices[i], choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfn subset_sum_i(nums: &mut [i32], target: i32) -> Vec<Vec<i32>> {\nlet state = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nlet start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nlet mut res = Vec::new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, &mut res);\nres\n}\n
subset_sum_i.c
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(vector *state, int target, vector *choices, int start, vector *res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nvector *tmpVector = newVector();\nfor (int i = 0; i < state->size; i++) {\nvectorPushback(tmpVector, state->data[i], sizeof(int));\n}\nvectorPushback(res, tmpVector, sizeof(vector));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor (int i = start; i < choices->size; i++) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif (target - *(int *)(choices->data[i]) < 0) {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nvectorPushback(state, choices->data[i], sizeof(int));\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - *(int *)(choices->data[i]), choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nvectorPopback(state);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nvector *subsetSumI(vector *nums, int target) {\nvector *state = newVector();                        // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nqsort(nums->data, nums->size, sizeof(int *), comp); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0;                                      // \u5b50\u96c6\u548c\nvector *res = newVector();                          // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_i.zig
[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumI}\n

\u5982\u56fe 13-12 \u6240\u793a\uff0c\u4e3a\u5c06\u6570\u7ec4 \\([3, 4, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \u8f93\u5165\u5230\u4ee5\u4e0a\u4ee3\u7801\u540e\u7684\u6574\u4f53\u56de\u6eaf\u8fc7\u7a0b\u3002

\u56fe 13-12 \u00a0 \u5b50\u96c6\u548c I \u56de\u6eaf\u8fc7\u7a0b

"},{"location":"chapter_backtracking/subset_sum_problem/#1332","title":"13.3.2 \u00a0 \u8003\u8651\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"

Question

\u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570\u6570\u7ec4 nums \u548c\u4e00\u4e2a\u76ee\u6807\u6b63\u6574\u6570 target \uff0c\u8bf7\u627e\u51fa\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\uff0c\u4f7f\u5f97\u7ec4\u5408\u4e2d\u7684\u5143\u7d20\u548c\u7b49\u4e8e target \u3002\u7ed9\u5b9a\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u6bcf\u4e2a\u5143\u7d20\u53ea\u53ef\u88ab\u9009\u62e9\u4e00\u6b21\u3002\u8bf7\u4ee5\u5217\u8868\u5f62\u5f0f\u8fd4\u56de\u8fd9\u4e9b\u7ec4\u5408\uff0c\u5217\u8868\u4e2d\u4e0d\u5e94\u5305\u542b\u91cd\u590d\u7ec4\u5408\u3002

\u76f8\u6bd4\u4e8e\u4e0a\u9898\uff0c\u672c\u9898\u7684\u8f93\u5165\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u8fd9\u5f15\u5165\u4e86\u65b0\u7684\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u7ed9\u5b9a\u6570\u7ec4 \\([4, \\hat{4}, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \uff0c\u5219\u73b0\u6709\u4ee3\u7801\u7684\u8f93\u51fa\u7ed3\u679c\u4e3a \\([4, 5], [\\hat{4}, 5]\\) \uff0c\u51fa\u73b0\u4e86\u91cd\u590d\u5b50\u96c6\u3002

\u9020\u6210\u8fd9\u79cd\u91cd\u590d\u7684\u539f\u56e0\u662f\u76f8\u7b49\u5143\u7d20\u5728\u67d0\u8f6e\u4e2d\u88ab\u591a\u6b21\u9009\u62e9\u3002\u5728\u56fe 13-13 \u4e2d\uff0c\u7b2c\u4e00\u8f6e\u5171\u6709\u4e09\u4e2a\u9009\u62e9\uff0c\u5176\u4e2d\u4e24\u4e2a\u90fd\u4e3a \\(4\\) \uff0c\u4f1a\u4ea7\u751f\u4e24\u4e2a\u91cd\u590d\u7684\u641c\u7d22\u5206\u652f\uff0c\u4ece\u800c\u8f93\u51fa\u91cd\u590d\u5b50\u96c6\uff1b\u540c\u7406\uff0c\u7b2c\u4e8c\u8f6e\u7684\u4e24\u4e2a \\(4\\) \u4e5f\u4f1a\u4ea7\u751f\u91cd\u590d\u5b50\u96c6\u3002

\u56fe 13-13 \u00a0 \u76f8\u7b49\u5143\u7d20\u5bfc\u81f4\u7684\u91cd\u590d\u5b50\u96c6

"},{"location":"chapter_backtracking/subset_sum_problem/#1_1","title":"1. \u00a0 \u76f8\u7b49\u5143\u7d20\u526a\u679d","text":"

\u4e3a\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u9700\u8981\u9650\u5236\u76f8\u7b49\u5143\u7d20\u5728\u6bcf\u4e00\u8f6e\u4e2d\u53ea\u88ab\u9009\u62e9\u4e00\u6b21\u3002\u5b9e\u73b0\u65b9\u5f0f\u6bd4\u8f83\u5de7\u5999\uff1a\u7531\u4e8e\u6570\u7ec4\u662f\u5df2\u6392\u5e8f\u7684\uff0c\u56e0\u6b64\u76f8\u7b49\u5143\u7d20\u90fd\u662f\u76f8\u90bb\u7684\u3002\u8fd9\u610f\u5473\u7740\u5728\u67d0\u8f6e\u9009\u62e9\u4e2d\uff0c\u82e5\u5f53\u524d\u5143\u7d20\u4e0e\u5176\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u5219\u8bf4\u660e\u5b83\u5df2\u7ecf\u88ab\u9009\u62e9\u8fc7\uff0c\u56e0\u6b64\u76f4\u63a5\u8df3\u8fc7\u5f53\u524d\u5143\u7d20\u3002

\u4e0e\u6b64\u540c\u65f6\uff0c\u672c\u9898\u89c4\u5b9a\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u53ea\u80fd\u88ab\u9009\u62e9\u4e00\u6b21\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5229\u7528\u53d8\u91cf start \u6765\u6ee1\u8db3\u8be5\u7ea6\u675f\uff1a\u5f53\u505a\u51fa\u9009\u62e9 \\(x_{i}\\) \u540e\uff0c\u8bbe\u5b9a\u4e0b\u4e00\u8f6e\u4ece\u7d22\u5f15 \\(i + 1\\) \u5f00\u59cb\u5411\u540e\u904d\u5386\u3002\u8fd9\u6837\u5373\u80fd\u53bb\u9664\u91cd\u590d\u5b50\u96c6\uff0c\u4e5f\u80fd\u907f\u514d\u91cd\u590d\u9009\u62e9\u5143\u7d20\u3002

"},{"location":"chapter_backtracking/subset_sum_problem/#2_1","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"PythonC++JavaC#GoSwiftJSTSDartRustCZig subset_sum_ii.py
def backtrack(\nstate: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n\"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II\"\"\"\n# \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == 0:\nres.append(list(state))\nreturn\n# \u904d\u5386\u6240\u6709\u9009\u62e9\n# \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n# \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor i in range(start, len(choices)):\n# \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n# \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif target - choices[i] < 0:\nbreak\n# \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif i > start and choices[i] == choices[i - 1]:\ncontinue\n# \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.append(choices[i])\n# \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i + 1, res)\n# \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop()\ndef subset_sum_ii(nums: list[int], target: int) -> list[list[int]]:\n\"\"\"\u6c42\u89e3\u5b50\u96c6\u548c II\"\"\"\nstate = []  # \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort()  # \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nstart = 0  # \u904d\u5386\u8d77\u59cb\u70b9\nres = []  # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res)\nreturn res\n
subset_sum_ii.cpp
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(vector<int> &state, int target, vector<int> &choices, int start, vector<vector<int>> &res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nres.push_back(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor (int i = start; i < choices.size(); i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] < 0) {\nbreak;\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif (i > start && choices[i] == choices[i - 1]) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.push_back(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i + 1, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop_back();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nvector<vector<int>> subsetSumII(vector<int> &nums, int target) {\nvector<int> state;              // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nsort(nums.begin(), nums.end()); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0;                  // \u904d\u5386\u8d77\u59cb\u70b9\nvector<vector<int>> res;        // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_ii.java
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nres.add(new ArrayList<>(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor (int i = start; i < choices.length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] < 0) {\nbreak;\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif (i > start && choices[i] == choices[i - 1]) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i + 1, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.remove(state.size() - 1);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nList<List<Integer>> subsetSumII(int[] nums, int target) {\nList<Integer> state = new ArrayList<>(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nArrays.sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nList<List<Integer>> res = new ArrayList<>(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_ii.cs
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nres.Add(new List<int>(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor (int i = start; i < choices.Length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] < 0) {\nbreak;\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif (i > start && choices[i] == choices[i - 1]) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.Add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nBacktrack(state, target - choices[i], choices, i + 1, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.RemoveAt(state.Count - 1);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nList<List<int>> SubsetSumII(int[] nums, int target) {\nList<int> state = new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nArray.Sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nList<List<int>> res = new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nBacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_ii.go
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunc backtrackSubsetSumII(start, target int, state, choices *[]int, res *[][]int) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == 0 {\nnewState := append([]int{}, *state...)\n*res = append(*res, newState)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor i := start; i < len(*choices); i++ {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif target-(*choices)[i] < 0 {\nbreak\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif i > start && (*choices)[i] == (*choices)[i-1] {\ncontinue\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n*state = append(*state, (*choices)[i])\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrackSubsetSumII(i+1, target-(*choices)[i], state, choices, res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n*state = (*state)[:len(*state)-1]\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunc subsetSumII(nums []int, target int) [][]int {\nstate := make([]int, 0) // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nsort.Ints(nums)         // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nstart := 0              // \u904d\u5386\u8d77\u59cb\u70b9\nres := make([][]int, 0) // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrackSubsetSumII(start, target, &state, &nums, &res)\nreturn res\n}\n
subset_sum_ii.swift
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunc backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == 0 {\nres.append(state)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor i in stride(from: start, to: choices.count, by: 1) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif target - choices[i] < 0 {\nbreak\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif i > start, choices[i] == choices[i - 1] {\ncontinue\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.append(choices[i])\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state: &state, target: target - choices[i], choices: choices, start: i + 1, res: &res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.removeLast()\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunc subsetSumII(nums: [Int], target: Int) -> [[Int]] {\nvar state: [Int] = [] // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nlet nums = nums.sorted() // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nlet start = 0 // \u904d\u5386\u8d77\u59cb\u70b9\nvar res: [[Int]] = [] // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state: &state, target: target, choices: nums, start: start, res: &res)\nreturn res\n}\n
subset_sum_ii.js
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunction backtrack(state, target, choices, start, res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target === 0) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor (let i = start; i < choices.length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] < 0) {\nbreak;\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif (i > start && choices[i] === choices[i - 1]) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i + 1, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunction subsetSumII(nums, target) {\nconst state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort((a, b) => a - b); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nconst start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nconst res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_ii.ts
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunction backtrack(\nstate: number[],\ntarget: number,\nchoices: number[],\nstart: number,\nres: number[][]\n): void {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target === 0) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor (let i = start; i < choices.length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] < 0) {\nbreak;\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif (i > start && choices[i] === choices[i - 1]) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i + 1, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunction subsetSumII(nums: number[], target: number): number[][] {\nconst state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort((a, b) => a - b); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nconst start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nconst res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_ii.dart
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(\nList<int> state,\nint target,\nList<int> choices,\nint start,\nList<List<int>> res,\n) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nres.add(List.from(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor (int i = start; i < choices.length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] < 0) {\nbreak;\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif (i > start && choices[i] == choices[i - 1]) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i + 1, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.removeLast();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nList<List<int>> subsetSumII(List<int> nums, int target) {\nList<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nList<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_ii.rs
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfn backtrack(mut state: Vec<i32>, target: i32, choices: &[i32], start: usize, res: &mut Vec<Vec<i32>>) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == 0 {\nres.push(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor i in start..choices.len() {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif target - choices[i] < 0 {\nbreak;\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif i > start && choices[i] == choices[i - 1] {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state.clone(), target - choices[i], choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfn subset_sum_ii(nums: &mut [i32], target: i32) -> Vec<Vec<i32>> {\nlet state = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nlet start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nlet mut res = Vec::new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, &mut res);\nres\n}\n
subset_sum_ii.c
/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(vector *state, int target, vector *choices, int start, vector *res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nvector *tmpVector = newVector();\nfor (int i = 0; i < state->size; i++) {\nvectorPushback(tmpVector, state->data[i], sizeof(int));\n}\nvectorPushback(res, tmpVector, sizeof(vector));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor (int i = start; i < choices->size; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - *(int *)(choices->data[i]) < 0) {\ncontinue;\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif (i > start && *(int *)(choices->data[i]) == *(int *)(choices->data[i - 1])) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nvectorPushback(state, choices->data[i], sizeof(int));\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - *(int *)(choices->data[i]), choices, i + 1, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nvectorPopback(state);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nvector *subsetSumII(vector *nums, int target) {\nvector *state = newVector();                        // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nqsort(nums->data, nums->size, sizeof(int *), comp); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0;                                      // \u5b50\u96c6\u548c\nvector *res = newVector();                          // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n
subset_sum_ii.zig
[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumII}\n

\u56fe 13-14 \u5c55\u793a\u4e86\u6570\u7ec4 \\([4, 4, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \u7684\u56de\u6eaf\u8fc7\u7a0b\uff0c\u5171\u5305\u542b\u56db\u79cd\u526a\u679d\u64cd\u4f5c\u3002\u8bf7\u4f60\u5c06\u56fe\u793a\u4e0e\u4ee3\u7801\u6ce8\u91ca\u76f8\u7ed3\u5408\uff0c\u7406\u89e3\u6574\u4e2a\u641c\u7d22\u8fc7\u7a0b\uff0c\u4ee5\u53ca\u6bcf\u79cd\u526a\u679d\u64cd\u4f5c\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002

\u56fe 13-14 \u00a0 \u5b50\u96c6\u548c II \u56de\u6eaf\u8fc7\u7a0b

"},{"location":"chapter_backtracking/summary/","title":"13.5 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_backtracking/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
  • \u56de\u6eaf\u7b97\u6cd5\u672c\u8d28\u662f\u7a77\u4e3e\u6cd5\uff0c\u901a\u8fc7\u5bf9\u89e3\u7a7a\u95f4\u8fdb\u884c\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u6765\u5bfb\u627e\u7b26\u5408\u6761\u4ef6\u7684\u89e3\u3002\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u9047\u5230\u6ee1\u8db3\u6761\u4ef6\u7684\u89e3\u5219\u8bb0\u5f55\uff0c\u76f4\u81f3\u627e\u5230\u6240\u6709\u89e3\u6216\u904d\u5386\u5b8c\u6210\u540e\u7ed3\u675f\u3002
  • \u56de\u6eaf\u7b97\u6cd5\u7684\u641c\u7d22\u8fc7\u7a0b\u5305\u62ec\u5c1d\u8bd5\u4e0e\u56de\u9000\u4e24\u4e2a\u90e8\u5206\u3002\u5b83\u901a\u8fc7\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u6765\u5c1d\u8bd5\u5404\u79cd\u9009\u62e9\uff0c\u5f53\u9047\u5230\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u60c5\u51b5\u65f6\uff0c\u5219\u64a4\u9500\u4e0a\u4e00\u6b65\u7684\u9009\u62e9\uff0c\u9000\u56de\u5230\u4e4b\u524d\u7684\u72b6\u6001\uff0c\u5e76\u7ee7\u7eed\u5c1d\u8bd5\u5176\u4ed6\u9009\u62e9\u3002\u5c1d\u8bd5\u4e0e\u56de\u9000\u662f\u4e24\u4e2a\u65b9\u5411\u76f8\u53cd\u7684\u64cd\u4f5c\u3002
  • \u56de\u6eaf\u95ee\u9898\u901a\u5e38\u5305\u542b\u591a\u4e2a\u7ea6\u675f\u6761\u4ef6\uff0c\u5b83\u4eec\u53ef\u7528\u4e8e\u5b9e\u73b0\u526a\u679d\u64cd\u4f5c\u3002\u526a\u679d\u53ef\u4ee5\u63d0\u524d\u7ed3\u675f\u4e0d\u5fc5\u8981\u7684\u641c\u7d22\u5206\u652f\uff0c\u5927\u5e45\u63d0\u5347\u641c\u7d22\u6548\u7387\u3002
  • \u56de\u6eaf\u7b97\u6cd5\u4e3b\u8981\u53ef\u7528\u4e8e\u89e3\u51b3\u641c\u7d22\u95ee\u9898\u548c\u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\u3002\u7ec4\u5408\u4f18\u5316\u95ee\u9898\u867d\u7136\u53ef\u4ee5\u7528\u56de\u6eaf\u7b97\u6cd5\u89e3\u51b3\uff0c\u4f46\u5f80\u5f80\u5b58\u5728\u66f4\u9ad8\u6548\u7387\u6216\u66f4\u597d\u6548\u679c\u7684\u89e3\u6cd5\u3002
  • \u5168\u6392\u5217\u95ee\u9898\u65e8\u5728\u641c\u7d22\u7ed9\u5b9a\u96c6\u5408\u7684\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u3002\u6211\u4eec\u501f\u52a9\u4e00\u4e2a\u6570\u7ec4\u6765\u8bb0\u5f55\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u88ab\u9009\u62e9\uff0c\u526a\u679d\u6389\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\u7684\u641c\u7d22\u5206\u652f\uff0c\u786e\u4fdd\u6bcf\u4e2a\u5143\u7d20\u53ea\u88ab\u9009\u62e9\u4e00\u6b21\u3002
  • \u5728\u5168\u6392\u5217\u95ee\u9898\u4e2d\uff0c\u5982\u679c\u96c6\u5408\u4e2d\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff0c\u5219\u6700\u7ec8\u7ed3\u679c\u4f1a\u51fa\u73b0\u91cd\u590d\u6392\u5217\u3002\u6211\u4eec\u9700\u8981\u7ea6\u675f\u76f8\u7b49\u5143\u7d20\u5728\u6bcf\u8f6e\u4e2d\u53ea\u80fd\u88ab\u9009\u62e9\u4e00\u6b21\uff0c\u8fd9\u901a\u5e38\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868\u6765\u5b9e\u73b0\u3002
  • \u5b50\u96c6\u548c\u95ee\u9898\u7684\u76ee\u6807\u662f\u5728\u7ed9\u5b9a\u96c6\u5408\u4e2d\u627e\u5230\u548c\u4e3a\u76ee\u6807\u503c\u7684\u6240\u6709\u5b50\u96c6\u3002\u96c6\u5408\u4e0d\u533a\u5206\u5143\u7d20\u987a\u5e8f\uff0c\u800c\u641c\u7d22\u8fc7\u7a0b\u4f1a\u8f93\u51fa\u6240\u6709\u987a\u5e8f\u7684\u7ed3\u679c\uff0c\u4ea7\u751f\u91cd\u590d\u5b50\u96c6\u3002\u6211\u4eec\u5728\u56de\u6eaf\u524d\u5c06\u6570\u636e\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u8bbe\u7f6e\u4e00\u4e2a\u53d8\u91cf\u6765\u6307\u793a\u6bcf\u4e00\u8f6e\u7684\u904d\u5386\u8d77\u70b9\uff0c\u4ece\u800c\u5c06\u751f\u6210\u91cd\u590d\u5b50\u96c6\u7684\u641c\u7d22\u5206\u652f\u8fdb\u884c\u526a\u679d\u3002
  • \u5bf9\u4e8e\u5b50\u96c6\u548c\u95ee\u9898\uff0c\u6570\u7ec4\u4e2d\u7684\u76f8\u7b49\u5143\u7d20\u4f1a\u4ea7\u751f\u91cd\u590d\u96c6\u5408\u3002\u6211\u4eec\u5229\u7528\u6570\u7ec4\u5df2\u6392\u5e8f\u7684\u524d\u7f6e\u6761\u4ef6\uff0c\u901a\u8fc7\u5224\u65ad\u76f8\u90bb\u5143\u7d20\u662f\u5426\u76f8\u7b49\u5b9e\u73b0\u526a\u679d\uff0c\u4ece\u800c\u786e\u4fdd\u76f8\u7b49\u5143\u7d20\u5728\u6bcf\u8f6e\u4e2d\u53ea\u80fd\u88ab\u9009\u4e2d\u4e00\u6b21\u3002
  • \\(n\\) \u7687\u540e\u65e8\u5728\u5bfb\u627e\u5c06 \\(n\\) \u4e2a\u7687\u540e\u653e\u7f6e\u5230 \\(n \\times n\\) \u5c3a\u5bf8\u68cb\u76d8\u4e0a\u7684\u65b9\u6848\uff0c\u8981\u6c42\u6240\u6709\u7687\u540e\u4e24\u4e24\u4e4b\u95f4\u65e0\u6cd5\u653b\u51fb\u5bf9\u65b9\u3002\u8be5\u95ee\u9898\u7684\u7ea6\u675f\u6761\u4ef6\u6709\u884c\u7ea6\u675f\u3001\u5217\u7ea6\u675f\u3001\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\u7ea6\u675f\u3002\u4e3a\u6ee1\u8db3\u884c\u7ea6\u675f\uff0c\u6211\u4eec\u91c7\u7528\u6309\u884c\u653e\u7f6e\u7684\u7b56\u7565\uff0c\u4fdd\u8bc1\u6bcf\u4e00\u884c\u653e\u7f6e\u4e00\u4e2a\u7687\u540e\u3002
  • \u5217\u7ea6\u675f\u548c\u5bf9\u89d2\u7ebf\u7ea6\u675f\u7684\u5904\u7406\u65b9\u5f0f\u7c7b\u4f3c\u3002\u5bf9\u4e8e\u5217\u7ea6\u675f\uff0c\u6211\u4eec\u5229\u7528\u4e00\u4e2a\u6570\u7ec4\u6765\u8bb0\u5f55\u6bcf\u4e00\u5217\u662f\u5426\u6709\u7687\u540e\uff0c\u4ece\u800c\u6307\u793a\u9009\u4e2d\u7684\u683c\u5b50\u662f\u5426\u5408\u6cd5\u3002\u5bf9\u4e8e\u5bf9\u89d2\u7ebf\u7ea6\u675f\uff0c\u6211\u4eec\u501f\u52a9\u4e24\u4e2a\u6570\u7ec4\u6765\u5206\u522b\u8bb0\u5f55\u8be5\u4e3b\u3001\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u5b58\u5728\u7687\u540e\uff1b\u96be\u70b9\u5728\u4e8e\u627e\u5904\u5728\u5230\u540c\u4e00\u4e3b\uff08\u526f\uff09\u5bf9\u89d2\u7ebf\u4e0a\u683c\u5b50\u6ee1\u8db3\u7684\u884c\u5217\u7d22\u5f15\u89c4\u5f8b\u3002
"},{"location":"chapter_backtracking/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

\u600e\u4e48\u7406\u89e3\u56de\u6eaf\u548c\u9012\u5f52\u7684\u5173\u7cfb\uff1f

\u603b\u7684\u6765\u770b\uff0c\u56de\u6eaf\u662f\u4e00\u79cd\u201c\u7b97\u6cd5\u7b56\u7565\u201d\uff0c\u800c\u9012\u5f52\u66f4\u50cf\u662f\u4e00\u4e2a\u201c\u5de5\u5177\u201d\u3002

  • \u56de\u6eaf\u7b97\u6cd5\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u5b9e\u73b0\u3002\u7136\u800c\uff0c\u56de\u6eaf\u662f\u9012\u5f52\u7684\u5e94\u7528\u573a\u666f\u4e4b\u4e00\uff0c\u662f\u9012\u5f52\u5728\u641c\u7d22\u95ee\u9898\u4e2d\u7684\u5e94\u7528\u3002
  • \u9012\u5f52\u7684\u7ed3\u6784\u4f53\u73b0\u4e86\u201c\u5b50\u95ee\u9898\u5206\u89e3\u201d\u7684\u89e3\u9898\u8303\u5f0f\uff0c\u5e38\u7528\u4e8e\u89e3\u51b3\u5206\u6cbb\u3001\u56de\u6eaf\u3001\u52a8\u6001\u89c4\u5212\uff08\u8bb0\u5fc6\u5316\u9012\u5f52\uff09\u7b49\u95ee\u9898\u3002
"},{"location":"chapter_computational_complexity/","title":"\u7b2c 2 \u7ae0 \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"

Abstract

\u590d\u6742\u5ea6\u5206\u6790\u72b9\u5982\u6d69\u701a\u7684\u7b97\u6cd5\u5b87\u5b99\u4e2d\u7684\u65f6\u7a7a\u5411\u5bfc\u3002

\u5b83\u5e26\u9886\u6211\u4eec\u5728\u65f6\u95f4\u4e0e\u7a7a\u95f4\u8fd9\u4e24\u4e2a\u7ef4\u5ea6\u4e0a\u6df1\u5165\u63a2\u7d22\uff0c\u5bfb\u627e\u66f4\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u6848\u3002

"},{"location":"chapter_computational_complexity/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 2.1 \u00a0 \u7b97\u6cd5\u6548\u7387\u8bc4\u4f30
  • 2.2 \u00a0 \u8fed\u4ee3\u4e0e\u9012\u5f52
  • 2.3 \u00a0 \u65f6\u95f4\u590d\u6742\u5ea6
  • 2.4 \u00a0 \u7a7a\u95f4\u590d\u6742\u5ea6
  • 2.5 \u00a0 \u5c0f\u7ed3
"},{"location":"chapter_computational_complexity/iteration_and_recursion/","title":"2.2 \u00a0 \u8fed\u4ee3\u4e0e\u9012\u5f52","text":"

\u5728\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u4e2d\uff0c\u91cd\u590d\u6267\u884c\u67d0\u4e2a\u4efb\u52a1\u662f\u5f88\u5e38\u89c1\u7684\uff0c\u5176\u4e0e\u7b97\u6cd5\u7684\u590d\u6742\u5ea6\u5bc6\u5207\u76f8\u5173\u3002\u800c\u8981\u91cd\u590d\u6267\u884c\u67d0\u4e2a\u4efb\u52a1\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u9009\u7528\u4e24\u79cd\u57fa\u672c\u7684\u7a0b\u5e8f\u7ed3\u6784\uff1a\u8fed\u4ee3\u548c\u9012\u5f52\u3002

"},{"location":"chapter_computational_complexity/iteration_and_recursion/#221","title":"2.2.1 \u00a0 \u8fed\u4ee3","text":"

\u300c\u8fed\u4ee3 iteration\u300d\u662f\u4e00\u79cd\u91cd\u590d\u6267\u884c\u67d0\u4e2a\u4efb\u52a1\u7684\u63a7\u5236\u7ed3\u6784\u3002\u5728\u8fed\u4ee3\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u5728\u6ee1\u8db3\u4e00\u5b9a\u7684\u6761\u4ef6\u4e0b\u91cd\u590d\u6267\u884c\u67d0\u6bb5\u4ee3\u7801\uff0c\u76f4\u5230\u8fd9\u4e2a\u6761\u4ef6\u4e0d\u518d\u6ee1\u8db3\u3002

"},{"location":"chapter_computational_complexity/iteration_and_recursion/#1-for","title":"1. \u00a0 for \u5faa\u73af","text":"

for \u5faa\u73af\u662f\u6700\u5e38\u89c1\u7684\u8fed\u4ee3\u5f62\u5f0f\u4e4b\u4e00\uff0c\u9002\u5408\u9884\u5148\u77e5\u9053\u8fed\u4ee3\u6b21\u6570\u65f6\u4f7f\u7528\u3002

\u4ee5\u4e0b\u51fd\u6570\u57fa\u4e8e for \u5faa\u73af\u5b9e\u73b0\u4e86\u6c42\u548c \\(1 + 2 + \\dots + n\\) \uff0c\u6c42\u548c\u7ed3\u679c\u4f7f\u7528\u53d8\u91cf res \u8bb0\u5f55\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cPython \u4e2d range(a, b) \u5bf9\u5e94\u7684\u533a\u95f4\u662f\u201c\u5de6\u95ed\u53f3\u5f00\u201d\u7684\uff0c\u5bf9\u5e94\u7684\u904d\u5386\u8303\u56f4\u4e3a \\(a, a + 1, \\dots, b-1\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig iteration.py
def for_loop(n: int) -> int:\n\"\"\"for \u5faa\u73af\"\"\"\nres = 0\n# \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nfor i in range(1, n + 1):\nres += i\nreturn res\n
iteration.cpp
/* for \u5faa\u73af */\nint forLoop(int n) {\nint res = 0;\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nfor (int i = 1; i <= n; ++i) {\nres += i;\n}\nreturn res;\n}\n
iteration.java
/* for \u5faa\u73af */\nint forLoop(int n) {\nint res = 0;\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nfor (int i = 1; i <= n; i++) {\nres += i;\n}\nreturn res;\n}\n
iteration.cs
/* for \u5faa\u73af */\nint ForLoop(int n) {\nint res = 0;\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nfor (int i = 1; i <= n; i++) {\nres += i;\n}\nreturn res;\n}\n
iteration.go
/* for \u5faa\u73af */\nfunc forLoop(n int) int {\nres := 0\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nfor i := 1; i <= n; i++ {\nres += i\n}\nreturn res\n}\n
iteration.swift
/* for \u5faa\u73af */\nfunc forLoop(n: Int) -> Int {\nvar res = 0\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nfor i in 1 ... n {\nres += i\n}\nreturn res\n}\n
iteration.js
/* for \u5faa\u73af */\nfunction forLoop(n) {\nlet res = 0;\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nfor (let i = 1; i <= n; i++) {\nres += i;\n}\nreturn res;\n}\n
iteration.ts
/* for \u5faa\u73af */\nfunction forLoop(n: number): number {\nlet res = 0;\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nfor (let i = 1; i <= n; i++) {\nres += i;\n}\nreturn res;\n}\n
iteration.dart
/* for \u5faa\u73af */\nint forLoop(int n) {\nint res = 0;\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nfor (int i = 1; i <= n; i++) {\nres += i;\n}\nreturn res;\n}\n
iteration.rs
/* for \u5faa\u73af */\nfn for_loop(n: i32) -> i32 {\nlet mut res = 0;\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nfor i in 1..=n {\nres += i;\n}\nres\n} 
iteration.c
/* for \u5faa\u73af */\nint forLoop(int n) {\nint res = 0;\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nfor (int i = 1; i <= n; i++) {\nres += i;\n}\nreturn res;\n}\n
iteration.zig
[class]{}-[func]{forLoop}\n

\u56fe 2-1 \u5c55\u793a\u4e86\u8be5\u6c42\u548c\u51fd\u6570\u7684\u6d41\u7a0b\u6846\u56fe\u3002

\u56fe 2-1 \u00a0 \u6c42\u548c\u51fd\u6570\u7684\u6d41\u7a0b\u6846\u56fe

\u6b64\u6c42\u548c\u51fd\u6570\u7684\u64cd\u4f5c\u6570\u91cf\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u6210\u6b63\u6bd4\uff0c\u6216\u8005\u8bf4\u6210\u201c\u7ebf\u6027\u5173\u7cfb\u201d\u3002\u5b9e\u9645\u4e0a\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u63cf\u8ff0\u7684\u5c31\u662f\u8fd9\u4e2a\u201c\u7ebf\u6027\u5173\u7cfb\u201d\u3002\u76f8\u5173\u5185\u5bb9\u5c06\u4f1a\u5728\u4e0b\u4e00\u8282\u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u3002

"},{"location":"chapter_computational_complexity/iteration_and_recursion/#2-while","title":"2. \u00a0 while \u5faa\u73af","text":"

\u4e0e for \u5faa\u73af\u7c7b\u4f3c\uff0cwhile \u5faa\u73af\u4e5f\u662f\u4e00\u79cd\u5b9e\u73b0\u8fed\u4ee3\u7684\u65b9\u6cd5\u3002\u5728 while \u5faa\u73af\u4e2d\uff0c\u7a0b\u5e8f\u6bcf\u8f6e\u90fd\u4f1a\u5148\u68c0\u67e5\u6761\u4ef6\uff0c\u5982\u679c\u6761\u4ef6\u4e3a\u771f\u5219\u7ee7\u7eed\u6267\u884c\uff0c\u5426\u5219\u5c31\u7ed3\u675f\u5faa\u73af\u3002

\u4e0b\u9762\uff0c\u6211\u4eec\u7528 while \u5faa\u73af\u6765\u5b9e\u73b0\u6c42\u548c \\(1 + 2 + \\dots + n\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig iteration.py
def while_loop(n: int) -> int:\n\"\"\"while \u5faa\u73af\"\"\"\nres = 0\ni = 1  # \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n# \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nwhile i <= n:\nres += i\ni += 1  # \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\nreturn res\n
iteration.cpp
/* while \u5faa\u73af */\nint whileLoop(int n) {\nint res = 0;\nint i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nwhile (i <= n) {\nres += i;\ni++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n}\nreturn res;\n}\n
iteration.java
/* while \u5faa\u73af */\nint whileLoop(int n) {\nint res = 0;\nint i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nwhile (i <= n) {\nres += i;\ni++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n}\nreturn res;\n}\n
iteration.cs
/* while \u5faa\u73af */\nint WhileLoop(int n) {\nint res = 0;\nint i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nwhile (i <= n) {\nres += i;\ni += 1; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n}\nreturn res;\n}\n
iteration.go
/* while \u5faa\u73af */\nfunc whileLoop(n int) int {\nres := 0\n// \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\ni := 1\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nfor i <= n {\nres += i\n// \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\ni++\n}\nreturn res\n}\n
iteration.swift
/* while \u5faa\u73af */\nfunc whileLoop(n: Int) -> Int {\nvar res = 0\nvar i = 1 // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nwhile i <= n {\nres += i\ni += 1 // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n}\nreturn res\n}\n
iteration.js
/* while \u5faa\u73af */\nfunction whileLoop(n) {\nlet res = 0;\nlet i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nwhile (i <= n) {\nres += i;\ni++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n}\nreturn res;\n}\n
iteration.ts
/* while \u5faa\u73af */\nfunction whileLoop(n: number): number {\nlet res = 0;\nlet i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nwhile (i <= n) {\nres += i;\ni++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n}\nreturn res;\n}\n
iteration.dart
/* while \u5faa\u73af */\nint whileLoop(int n) {\nint res = 0;\nint i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nwhile (i <= n) {\nres += i;\ni++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n}\nreturn res;\n}\n
iteration.rs
/* while \u5faa\u73af */\nfn while_loop(n: i32) -> i32 {\nlet mut res = 0;\nlet mut i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nwhile i <= n {\nres += i;\ni += 1; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n}\nres\n}\n
iteration.c
/* while \u5faa\u73af */\nint whileLoop(int n) {\nint res = 0;\nint i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\nwhile (i <= n) {\nres += i;\ni++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n}\nreturn res;\n}\n
iteration.zig
[class]{}-[func]{whileLoop}\n

\u5728 while \u5faa\u73af\u4e2d\uff0c\u7531\u4e8e\u521d\u59cb\u5316\u548c\u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\u7684\u6b65\u9aa4\u662f\u72ec\u7acb\u5728\u5faa\u73af\u7ed3\u6784\u4e4b\u5916\u7684\uff0c\u56e0\u6b64\u5b83\u6bd4 for \u5faa\u73af\u7684\u81ea\u7531\u5ea6\u66f4\u9ad8\u3002

\u4f8b\u5982\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u6761\u4ef6\u53d8\u91cf \\(i\\) \u6bcf\u8f6e\u8fdb\u884c\u4e86\u4e24\u6b21\u66f4\u65b0\uff0c\u8fd9\u79cd\u60c5\u51b5\u5c31\u4e0d\u592a\u65b9\u4fbf\u7528 for \u5faa\u73af\u5b9e\u73b0\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig iteration.py
def while_loop_ii(n: int) -> int:\n\"\"\"while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09\"\"\"\nres = 0\ni = 1  # \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n# \u5faa\u73af\u6c42\u548c 1, 4, ...\nwhile i <= n:\nres += i\n# \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\ni += 1\ni *= 2\nreturn res\n
iteration.cpp
/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\nint res = 0;\nint i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 4, ...\nwhile (i <= n) {\nres += i;\n// \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\ni++;\ni *= 2;\n}\nreturn res;\n}\n
iteration.java
/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\nint res = 0;\nint i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 4, ...\nwhile (i <= n) {\nres += i;\n// \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\ni++;\ni *= 2;\n}\nreturn res;\n}\n
iteration.cs
/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint WhileLoopII(int n) {\nint res = 0;\nint i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 2, 4, 5...\nwhile (i <= n) {\nres += i;\n// \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\ni += 1; i *= 2;\n}\nreturn res;\n}\n
iteration.go
/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunc whileLoopII(n int) int {\nres := 0\n// \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\ni := 1\n// \u5faa\u73af\u6c42\u548c 1, 4, ...\nfor i <= n {\nres += i\n// \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\ni++\ni *= 2\n}\nreturn res\n}\n
iteration.swift
/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunc whileLoopII(n: Int) -> Int {\nvar res = 0\nvar i = 1 // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 4, ...\nwhile i <= n {\nres += i\n// \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\ni += 1\ni *= 2\n}\nreturn res\n}\n
iteration.js
/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunction whileLoopII(n) {\nlet res = 0;\nlet i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 4, ...\nwhile (i <= n) {\nres += i;\n// \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\ni++;\ni *= 2;\n}\nreturn res;\n}\n
iteration.ts
/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunction whileLoopII(n: number): number {\nlet res = 0;\nlet i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 4, ...\nwhile (i <= n) {\nres += i;\n// \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\ni++;\ni *= 2;\n}\nreturn res;\n}\n
iteration.dart
/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\nint res = 0;\nint i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 4, ...\nwhile (i <= n) {\nres += i;\n// \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\ni++;\ni *= 2;\n}\nreturn res;\n}\n
iteration.rs
/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfn while_loop_ii(n: i32) -> i32 {\nlet mut res = 0;\nlet mut i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 4, ...\nwhile i <= n {\nres += i;\n// \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\ni += 1;\ni *= 2;\n}\nres\n}\n
iteration.c
/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\nint res = 0;\nint i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n// \u5faa\u73af\u6c42\u548c 1, 4, ...\nwhile (i <= n) {\nres += i;\n// \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\ni++;\ni *= 2;\n}\nreturn res;\n}\n
iteration.zig
[class]{}-[func]{whileLoopII}\n

\u603b\u7684\u6765\u8bf4\uff0cfor \u5faa\u73af\u7684\u4ee3\u7801\u66f4\u52a0\u7d27\u51d1\uff0cwhile \u5faa\u73af\u66f4\u52a0\u7075\u6d3b\uff0c\u4e24\u8005\u90fd\u53ef\u4ee5\u5b9e\u73b0\u8fed\u4ee3\u7ed3\u6784\u3002\u9009\u62e9\u4f7f\u7528\u54ea\u4e00\u4e2a\u5e94\u8be5\u6839\u636e\u7279\u5b9a\u95ee\u9898\u7684\u9700\u6c42\u6765\u51b3\u5b9a\u3002

"},{"location":"chapter_computational_complexity/iteration_and_recursion/#3","title":"3. \u00a0 \u5d4c\u5957\u5faa\u73af","text":"

\u6211\u4eec\u53ef\u4ee5\u5728\u4e00\u4e2a\u5faa\u73af\u7ed3\u6784\u5185\u5d4c\u5957\u53e6\u4e00\u4e2a\u5faa\u73af\u7ed3\u6784\uff0c\u4ee5 for \u5faa\u73af\u4e3a\u4f8b\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig iteration.py
def nested_for_loop(n: int) -> str:\n\"\"\"\u53cc\u5c42 for \u5faa\u73af\"\"\"\nres = \"\"\n# \u5faa\u73af i = 1, 2, ..., n-1, n\nfor i in range(1, n + 1):\n# \u5faa\u73af j = 1, 2, ..., n-1, n\nfor j in range(1, n + 1):\nres += f\"({i}, {j}), \"\nreturn res\n
iteration.cpp
/* \u53cc\u5c42 for \u5faa\u73af */\nstring nestedForLoop(int n) {\nostringstream res;\n// \u5faa\u73af i = 1, 2, ..., n-1, n\nfor (int i = 1; i <= n; ++i) {\n// \u5faa\u73af j = 1, 2, ..., n-1, n\nfor (int j = 1; j <= n; ++j) {\nres << \"(\" << i << \", \" << j << \"), \";\n}\n}\nreturn res.str();\n}\n
iteration.java
/* \u53cc\u5c42 for \u5faa\u73af */\nString nestedForLoop(int n) {\nStringBuilder res = new StringBuilder();\n// \u5faa\u73af i = 1, 2, ..., n-1, n\nfor (int i = 1; i <= n; i++) {\n// \u5faa\u73af j = 1, 2, ..., n-1, n\nfor (int j = 1; j <= n; j++) {\nres.append(\"(\" + i + \", \" + j + \"), \");\n}\n}\nreturn res.toString();\n}\n
iteration.cs
/* \u53cc\u5c42 for \u5faa\u73af */\nstring NestedForLoop(int n) {\nStringBuilder res = new();\n// \u5faa\u73af i = 1, 2, ..., n-1, n\nfor (int i = 1; i <= n; i++) {\n// \u5faa\u73af j = 1, 2, ..., n-1, n\nfor (int j = 1; j <= n; j++) {\nres.Append($\"({i}, {j}), \");\n}\n}\nreturn res.ToString();\n}\n
iteration.go
/* \u53cc\u5c42 for \u5faa\u73af */\nfunc nestedForLoop(n int) string {\nres := \"\"\n// \u5faa\u73af i = 1, 2, ..., n-1, n\nfor i := 1; i <= n; i++ {\nfor j := 1; j <= n; j++ {\n// \u5faa\u73af j = 1, 2, ..., n-1, n\nres += fmt.Sprintf(\"(%d, %d), \", i, j)\n}\n}\nreturn res\n}\n
iteration.swift
/* \u53cc\u5c42 for \u5faa\u73af */\nfunc nestedForLoop(n: Int) -> String {\nvar res = \"\"\n// \u5faa\u73af i = 1, 2, ..., n-1, n\nfor i in 1 ... n {\n// \u5faa\u73af j = 1, 2, ..., n-1, n\nfor j in 1 ... n {\nres.append(\"(\\(i), \\(j)), \")\n}\n}\nreturn res\n}\n
iteration.js
/* \u53cc\u5c42 for \u5faa\u73af */\nfunction nestedForLoop(n) {\nlet res = '';\n// \u5faa\u73af i = 1, 2, ..., n-1, n\nfor (let i = 1; i <= n; i++) {\n// \u5faa\u73af j = 1, 2, ..., n-1, n\nfor (let j = 1; j <= n; j++) {\nres += `(${i}, ${j}), `;\n}\n}\nreturn res;\n}\n
iteration.ts
/* \u53cc\u5c42 for \u5faa\u73af */\nfunction nestedForLoop(n: number): string {\nlet res = '';\n// \u5faa\u73af i = 1, 2, ..., n-1, n\nfor (let i = 1; i <= n; i++) {\n// \u5faa\u73af j = 1, 2, ..., n-1, n\nfor (let j = 1; j <= n; j++) {\nres += `(${i}, ${j}), `;\n}\n}\nreturn res;\n}\n
iteration.dart
/* \u53cc\u5c42 for \u5faa\u73af */\nString nestedForLoop(int n) {\nString res = \"\";\n// \u5faa\u73af i = 1, 2, ..., n-1, n\nfor (int i = 1; i <= n; i++) {\n// \u5faa\u73af j = 1, 2, ..., n-1, n\nfor (int j = 1; j <= n; j++) {\nres += \"($i, $j), \";\n}\n}\nreturn res;\n}\n
iteration.rs
/* \u53cc\u5c42 for \u5faa\u73af */\nfn nested_for_loop(n: i32) -> String {\nlet mut res = vec![];\n// \u5faa\u73af i = 1, 2, ..., n-1, n\nfor i in 1..=n {\n// \u5faa\u73af j = 1, 2, ..., n-1, n\nfor j in 1..=n {\nres.push(format!(\"({}, {}), \", i, j));\n}\n}\nres.join(\"\")\n}\n
iteration.c
/* \u53cc\u5c42 for \u5faa\u73af */\nchar *nestedForLoop(int n) {\n// n * n \u4e3a\u5bf9\u5e94\u70b9\u6570\u91cf\uff0c\"(i, j), \" \u5bf9\u5e94\u5b57\u7b26\u4e32\u957f\u6700\u5927\u4e3a 6+10*2\uff0c\u52a0\u4e0a\u6700\u540e\u4e00\u4e2a\u7a7a\u5b57\u7b26 \\0 \u7684\u989d\u5916\u7a7a\u95f4\nint size = n * n * 26 + 1;\nchar *res = malloc(size * sizeof(char));\n// \u5faa\u73af i = 1, 2, ..., n-1, n\nfor (int i = 1; i <= n; i++) {\n// \u5faa\u73af j = 1, 2, ..., n-1, n\nfor (int j = 1; j <= n; j++) {\nchar tmp[26];\nsnprintf(tmp, sizeof(tmp), \"(%d, %d), \", i, j);\nstrncat(res, tmp, size - strlen(res) - 1);\n}\n}\nreturn res;\n}\n
iteration.zig
[class]{}-[func]{nestedForLoop}\n

\u56fe 2-2 \u7ed9\u51fa\u4e86\u8be5\u5d4c\u5957\u5faa\u73af\u7684\u6d41\u7a0b\u6846\u56fe\u3002

\u56fe 2-2 \u00a0 \u5d4c\u5957\u5faa\u73af\u7684\u6d41\u7a0b\u6846\u56fe

\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u51fd\u6570\u7684\u64cd\u4f5c\u6570\u91cf\u4e0e \\(n^2\\) \u6210\u6b63\u6bd4\uff0c\u6216\u8005\u8bf4\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u548c\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u6210\u201c\u5e73\u65b9\u5173\u7cfb\u201d\u3002

\u6211\u4eec\u53ef\u4ee5\u7ee7\u7eed\u6dfb\u52a0\u5d4c\u5957\u5faa\u73af\uff0c\u6bcf\u4e00\u6b21\u5d4c\u5957\u90fd\u662f\u4e00\u6b21\u201c\u5347\u7ef4\u201d\uff0c\u5c06\u4f1a\u4f7f\u65f6\u95f4\u590d\u6742\u5ea6\u63d0\u9ad8\u81f3\u201c\u7acb\u65b9\u5173\u7cfb\u201d\u3001\u201c\u56db\u6b21\u65b9\u5173\u7cfb\u201d\u3001\u4ee5\u6b64\u7c7b\u63a8\u3002

"},{"location":"chapter_computational_complexity/iteration_and_recursion/#222","title":"2.2.2 \u00a0 \u9012\u5f52","text":"

\u300c\u9012\u5f52 recursion\u300d\u662f\u4e00\u79cd\u7b97\u6cd5\u7b56\u7565\uff0c\u901a\u8fc7\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u6765\u89e3\u51b3\u95ee\u9898\u3002\u5b83\u4e3b\u8981\u5305\u542b\u4e24\u4e2a\u9636\u6bb5\u3002

  1. \u9012\uff1a\u7a0b\u5e8f\u4e0d\u65ad\u6df1\u5165\u5730\u8c03\u7528\u81ea\u8eab\uff0c\u901a\u5e38\u4f20\u5165\u66f4\u5c0f\u6216\u66f4\u7b80\u5316\u7684\u53c2\u6570\uff0c\u76f4\u5230\u8fbe\u5230\u201c\u7ec8\u6b62\u6761\u4ef6\u201d\u3002
  2. \u5f52\uff1a\u89e6\u53d1\u201c\u7ec8\u6b62\u6761\u4ef6\u201d\u540e\uff0c\u7a0b\u5e8f\u4ece\u6700\u6df1\u5c42\u7684\u9012\u5f52\u51fd\u6570\u5f00\u59cb\u9010\u5c42\u8fd4\u56de\uff0c\u6c47\u805a\u6bcf\u4e00\u5c42\u7684\u7ed3\u679c\u3002

\u800c\u4ece\u5b9e\u73b0\u7684\u89d2\u5ea6\u770b\uff0c\u9012\u5f52\u4ee3\u7801\u4e3b\u8981\u5305\u542b\u4e09\u4e2a\u8981\u7d20\u3002

  1. \u7ec8\u6b62\u6761\u4ef6\uff1a\u7528\u4e8e\u51b3\u5b9a\u4ec0\u4e48\u65f6\u5019\u7531\u201c\u9012\u201d\u8f6c\u201c\u5f52\u201d\u3002
  2. \u9012\u5f52\u8c03\u7528\uff1a\u5bf9\u5e94\u201c\u9012\u201d\uff0c\u51fd\u6570\u8c03\u7528\u81ea\u8eab\uff0c\u901a\u5e38\u8f93\u5165\u66f4\u5c0f\u6216\u66f4\u7b80\u5316\u7684\u53c2\u6570\u3002
  3. \u8fd4\u56de\u7ed3\u679c\uff1a\u5bf9\u5e94\u201c\u5f52\u201d\uff0c\u5c06\u5f53\u524d\u9012\u5f52\u5c42\u7ea7\u7684\u7ed3\u679c\u8fd4\u56de\u81f3\u4e0a\u4e00\u5c42\u3002

\u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u6211\u4eec\u53ea\u9700\u8c03\u7528\u51fd\u6570 recur(n) \uff0c\u5c31\u53ef\u4ee5\u5b8c\u6210 \\(1 + 2 + \\dots + n\\) \u7684\u8ba1\u7b97\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig recursion.py
def recur(n: int) -> int:\n\"\"\"\u9012\u5f52\"\"\"\n# \u7ec8\u6b62\u6761\u4ef6\nif n == 1:\nreturn 1\n# \u9012\uff1a\u9012\u5f52\u8c03\u7528\nres = recur(n - 1)\n# \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nreturn n + res\n
recursion.cpp
/* \u9012\u5f52 */\nint recur(int n) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n == 1)\nreturn 1;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nint res = recur(n - 1);\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nreturn n + res;\n}\n
recursion.java
/* \u9012\u5f52 */\nint recur(int n) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n == 1)\nreturn 1;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nint res = recur(n - 1);\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nreturn n + res;\n}\n
recursion.cs
/* \u9012\u5f52 */\nint Recur(int n) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n == 1)\nreturn 1;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nint res = Recur(n - 1);\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nreturn n + res;\n}\n
recursion.go
/* \u9012\u5f52 */\nfunc recur(n int) int {\n// \u7ec8\u6b62\u6761\u4ef6\nif n == 1 {\nreturn 1\n}\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nres := recur(n - 1)\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nreturn n + res\n}\n
recursion.swift
/* \u9012\u5f52 */\nfunc recur(n: Int) -> Int {\n// \u7ec8\u6b62\u6761\u4ef6\nif n == 1 {\nreturn 1\n}\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nlet res = recur(n: n - 1)\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nreturn n + res\n}\n
recursion.js
/* \u9012\u5f52 */\nfunction recur(n) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n === 1) return 1;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nconst res = recur(n - 1);\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nreturn n + res;\n}\n
recursion.ts
/* \u9012\u5f52 */\nfunction recur(n: number): number {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n === 1) return 1;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nconst res = recur(n - 1);\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nreturn n + res;\n}\n
recursion.dart
/* \u9012\u5f52 */\nint recur(int n) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n == 1) return 1;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nint res = recur(n - 1);\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nreturn n + res;\n}\n
recursion.rs
/* \u9012\u5f52 */\nfn recur(n: i32) -> i32 {\n// \u7ec8\u6b62\u6761\u4ef6\nif n == 1 {\nreturn 1;\n}\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nlet res = recur(n - 1);\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nn + res\n}\n
recursion.c
/* \u9012\u5f52 */\nint recur(int n) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n == 1)\nreturn 1;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nint res = recur(n - 1);\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nreturn n + res;\n}\n
recursion.zig
[class]{}-[func]{recur}\n

\u56fe 2-3 \u5c55\u793a\u4e86\u8be5\u51fd\u6570\u7684\u9012\u5f52\u8fc7\u7a0b\u3002

\u56fe 2-3 \u00a0 \u6c42\u548c\u51fd\u6570\u7684\u9012\u5f52\u8fc7\u7a0b

\u867d\u7136\u4ece\u8ba1\u7b97\u89d2\u5ea6\u770b\uff0c\u8fed\u4ee3\u4e0e\u9012\u5f52\u53ef\u4ee5\u5f97\u5230\u76f8\u540c\u7684\u7ed3\u679c\uff0c\u4f46\u5b83\u4eec\u4ee3\u8868\u4e86\u4e24\u79cd\u5b8c\u5168\u4e0d\u540c\u7684\u601d\u8003\u548c\u89e3\u51b3\u95ee\u9898\u7684\u8303\u5f0f\u3002

  • \u8fed\u4ee3\uff1a\u201c\u81ea\u4e0b\u800c\u4e0a\u201d\u5730\u89e3\u51b3\u95ee\u9898\u3002\u4ece\u6700\u57fa\u7840\u7684\u6b65\u9aa4\u5f00\u59cb\uff0c\u7136\u540e\u4e0d\u65ad\u91cd\u590d\u6216\u7d2f\u52a0\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u76f4\u5230\u4efb\u52a1\u5b8c\u6210\u3002
  • \u9012\u5f52\uff1a\u201c\u81ea\u4e0a\u800c\u4e0b\u201d\u5730\u89e3\u51b3\u95ee\u9898\u3002\u5c06\u539f\u95ee\u9898\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u8fd9\u4e9b\u5b50\u95ee\u9898\u548c\u539f\u95ee\u9898\u5177\u6709\u76f8\u540c\u7684\u5f62\u5f0f\u3002\u63a5\u4e0b\u6765\u5c06\u5b50\u95ee\u9898\u7ee7\u7eed\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u76f4\u5230\u57fa\u672c\u60c5\u51b5\u65f6\u505c\u6b62\uff08\u57fa\u672c\u60c5\u51b5\u7684\u89e3\u662f\u5df2\u77e5\u7684\uff09\u3002

\u4ee5\u4e0a\u8ff0\u7684\u6c42\u548c\u51fd\u6570\u4e3a\u4f8b\uff0c\u8bbe\u95ee\u9898 \\(f(n) = 1 + 2 + \\dots + n\\) \u3002

  • \u8fed\u4ee3\uff1a\u5728\u5faa\u73af\u4e2d\u6a21\u62df\u6c42\u548c\u8fc7\u7a0b\uff0c\u4ece \\(1\\) \u904d\u5386\u5230 \\(n\\) \uff0c\u6bcf\u8f6e\u6267\u884c\u6c42\u548c\u64cd\u4f5c\uff0c\u5373\u53ef\u6c42\u5f97 \\(f(n)\\) \u3002
  • \u9012\u5f52\uff1a\u5c06\u95ee\u9898\u5206\u89e3\u4e3a\u5b50\u95ee\u9898 \\(f(n) = n + f(n-1)\\) \uff0c\u4e0d\u65ad\uff08\u9012\u5f52\u5730\uff09\u5206\u89e3\u4e0b\u53bb\uff0c\u76f4\u81f3\u57fa\u672c\u60c5\u51b5 \\(f(1) = 1\\) \u65f6\u7ec8\u6b62\u3002
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#1","title":"1. \u00a0 \u8c03\u7528\u6808","text":"

\u9012\u5f52\u51fd\u6570\u6bcf\u6b21\u8c03\u7528\u81ea\u8eab\u65f6\uff0c\u7cfb\u7edf\u90fd\u4f1a\u4e3a\u65b0\u5f00\u542f\u7684\u51fd\u6570\u5206\u914d\u5185\u5b58\uff0c\u4ee5\u5b58\u50a8\u5c40\u90e8\u53d8\u91cf\u3001\u8c03\u7528\u5730\u5740\u548c\u5176\u4ed6\u4fe1\u606f\u7b49\u3002\u8fd9\u5c06\u5bfc\u81f4\u4e24\u65b9\u9762\u7684\u7ed3\u679c\u3002

  • \u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u6570\u636e\u90fd\u5b58\u50a8\u5728\u79f0\u4e3a\u201c\u6808\u5e27\u7a7a\u95f4\u201d\u7684\u5185\u5b58\u533a\u57df\u4e2d\uff0c\u76f4\u81f3\u51fd\u6570\u8fd4\u56de\u540e\u624d\u4f1a\u88ab\u91ca\u653e\u3002\u56e0\u6b64\uff0c\u9012\u5f52\u901a\u5e38\u6bd4\u8fed\u4ee3\u66f4\u52a0\u8017\u8d39\u5185\u5b58\u7a7a\u95f4\u3002
  • \u9012\u5f52\u8c03\u7528\u51fd\u6570\u4f1a\u4ea7\u751f\u989d\u5916\u7684\u5f00\u9500\u3002\u56e0\u6b64\u9012\u5f52\u901a\u5e38\u6bd4\u5faa\u73af\u7684\u65f6\u95f4\u6548\u7387\u66f4\u4f4e\u3002

\u5982\u56fe 2-4 \u6240\u793a\uff0c\u5728\u89e6\u53d1\u7ec8\u6b62\u6761\u4ef6\u524d\uff0c\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684\u9012\u5f52\u51fd\u6570\uff0c\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \u3002

\u56fe 2-4 \u00a0 \u9012\u5f52\u8c03\u7528\u6df1\u5ea6

\u5728\u5b9e\u9645\u4e2d\uff0c\u7f16\u7a0b\u8bed\u8a00\u5141\u8bb8\u7684\u9012\u5f52\u6df1\u5ea6\u901a\u5e38\u662f\u6709\u9650\u7684\uff0c\u8fc7\u6df1\u7684\u9012\u5f52\u53ef\u80fd\u5bfc\u81f4\u6808\u6ea2\u51fa\u62a5\u9519\u3002

"},{"location":"chapter_computational_complexity/iteration_and_recursion/#2","title":"2. \u00a0 \u5c3e\u9012\u5f52","text":"

\u6709\u8da3\u7684\u662f\uff0c\u5982\u679c\u51fd\u6570\u5728\u8fd4\u56de\u524d\u7684\u6700\u540e\u4e00\u6b65\u624d\u8fdb\u884c\u9012\u5f52\u8c03\u7528\uff0c\u5219\u8be5\u51fd\u6570\u53ef\u4ee5\u88ab\u7f16\u8bd1\u5668\u6216\u89e3\u91ca\u5668\u4f18\u5316\uff0c\u4f7f\u5176\u5728\u7a7a\u95f4\u6548\u7387\u4e0a\u4e0e\u8fed\u4ee3\u76f8\u5f53\u3002\u8fd9\u79cd\u60c5\u51b5\u88ab\u79f0\u4e3a\u300c\u5c3e\u9012\u5f52 tail recursion\u300d\u3002

  • \u666e\u901a\u9012\u5f52\uff1a\u5f53\u51fd\u6570\u8fd4\u56de\u5230\u4e0a\u4e00\u5c42\u7ea7\u7684\u51fd\u6570\u540e\uff0c\u9700\u8981\u7ee7\u7eed\u6267\u884c\u4ee3\u7801\uff0c\u56e0\u6b64\u7cfb\u7edf\u9700\u8981\u4fdd\u5b58\u4e0a\u4e00\u5c42\u8c03\u7528\u7684\u4e0a\u4e0b\u6587\u3002
  • \u5c3e\u9012\u5f52\uff1a\u9012\u5f52\u8c03\u7528\u662f\u51fd\u6570\u8fd4\u56de\u524d\u7684\u6700\u540e\u4e00\u4e2a\u64cd\u4f5c\uff0c\u8fd9\u610f\u5473\u7740\u51fd\u6570\u8fd4\u56de\u5230\u4e0a\u4e00\u5c42\u7ea7\u540e\uff0c\u65e0\u9700\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\uff0c\u56e0\u6b64\u7cfb\u7edf\u65e0\u9700\u4fdd\u5b58\u4e0a\u4e00\u5c42\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u3002

\u4ee5\u8ba1\u7b97 \\(1 + 2 + \\dots + n\\) \u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u7ed3\u679c\u53d8\u91cf res \u8bbe\u4e3a\u51fd\u6570\u53c2\u6570\uff0c\u4ece\u800c\u5b9e\u73b0\u5c3e\u9012\u5f52\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig recursion.py
def tail_recur(n, res):\n\"\"\"\u5c3e\u9012\u5f52\"\"\"\n# \u7ec8\u6b62\u6761\u4ef6\nif n == 0:\nreturn res\n# \u5c3e\u9012\u5f52\u8c03\u7528\nreturn tail_recur(n - 1, res + n)\n
recursion.cpp
/* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n == 0)\nreturn res;\n// \u5c3e\u9012\u5f52\u8c03\u7528\nreturn tailRecur(n - 1, res + n);\n}\n
recursion.java
/* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n == 0)\nreturn res;\n// \u5c3e\u9012\u5f52\u8c03\u7528\nreturn tailRecur(n - 1, res + n);\n}\n
recursion.cs
/* \u5c3e\u9012\u5f52 */\nint TailRecur(int n, int res) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n == 0)\nreturn res;\n// \u5c3e\u9012\u5f52\u8c03\u7528\nreturn TailRecur(n - 1, res + n);\n}\n
recursion.go
/* \u5c3e\u9012\u5f52 */\nfunc tailRecur(n int, res int) int {\n// \u7ec8\u6b62\u6761\u4ef6\nif n == 0 {\nreturn res\n}\n// \u5c3e\u9012\u5f52\u8c03\u7528\nreturn tailRecur(n-1, res+n)\n}\n
recursion.swift
/* \u5c3e\u9012\u5f52 */\nfunc tailRecur(n: Int, res: Int) -> Int {\n// \u7ec8\u6b62\u6761\u4ef6\nif n == 0 {\nreturn res\n}\n// \u5c3e\u9012\u5f52\u8c03\u7528\nreturn tailRecur(n: n - 1, res: res + n)\n}\n
recursion.js
/* \u5c3e\u9012\u5f52 */\nfunction tailRecur(n, res) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n === 0) return res;\n// \u5c3e\u9012\u5f52\u8c03\u7528\nreturn tailRecur(n - 1, res + n);\n}\n
recursion.ts
/* \u5c3e\u9012\u5f52 */\nfunction tailRecur(n: number, res: number): number {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n === 0) return res;\n// \u5c3e\u9012\u5f52\u8c03\u7528\nreturn tailRecur(n - 1, res + n);\n}\n
recursion.dart
/* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n == 0) return res;\n// \u5c3e\u9012\u5f52\u8c03\u7528\nreturn tailRecur(n - 1, res + n);\n}\n
recursion.rs
/* \u5c3e\u9012\u5f52 */\nfn tail_recur(n: i32, res: i32) -> i32 {\n// \u7ec8\u6b62\u6761\u4ef6\nif n == 0 {\nreturn res;\n}\n// \u5c3e\u9012\u5f52\u8c03\u7528\ntail_recur(n - 1, res + n)\n}\n
recursion.c
/* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (n == 0)\nreturn res;\n// \u5c3e\u9012\u5f52\u8c03\u7528\nreturn tailRecur(n - 1, res + n);\n}\n
recursion.zig
[class]{}-[func]{tailRecur}\n

\u5c3e\u9012\u5f52\u7684\u6267\u884c\u8fc7\u7a0b\u5982\u56fe 2-5 \u6240\u793a\u3002\u5bf9\u6bd4\u666e\u901a\u9012\u5f52\u548c\u5c3e\u9012\u5f52\uff0c\u6c42\u548c\u64cd\u4f5c\u7684\u6267\u884c\u70b9\u662f\u4e0d\u540c\u7684\u3002

  • \u666e\u901a\u9012\u5f52\uff1a\u6c42\u548c\u64cd\u4f5c\u662f\u5728\u201c\u5f52\u201d\u7684\u8fc7\u7a0b\u4e2d\u6267\u884c\u7684\uff0c\u6bcf\u5c42\u8fd4\u56de\u540e\u90fd\u8981\u518d\u6267\u884c\u4e00\u6b21\u6c42\u548c\u64cd\u4f5c\u3002
  • \u5c3e\u9012\u5f52\uff1a\u6c42\u548c\u64cd\u4f5c\u662f\u5728\u201c\u9012\u201d\u7684\u8fc7\u7a0b\u4e2d\u6267\u884c\u7684\uff0c\u201c\u5f52\u201d\u7684\u8fc7\u7a0b\u53ea\u9700\u5c42\u5c42\u8fd4\u56de\u3002

\u56fe 2-5 \u00a0 \u5c3e\u9012\u5f52\u8fc7\u7a0b

Tip

\u8bf7\u6ce8\u610f\uff0c\u8bb8\u591a\u7f16\u8bd1\u5668\u6216\u89e3\u91ca\u5668\u5e76\u4e0d\u652f\u6301\u5c3e\u9012\u5f52\u4f18\u5316\u3002\u4f8b\u5982\uff0cPython \u9ed8\u8ba4\u4e0d\u652f\u6301\u5c3e\u9012\u5f52\u4f18\u5316\uff0c\u56e0\u6b64\u5373\u4f7f\u51fd\u6570\u662f\u5c3e\u9012\u5f52\u5f62\u5f0f\uff0c\u4f46\u4ecd\u7136\u53ef\u80fd\u4f1a\u9047\u5230\u6808\u6ea2\u51fa\u95ee\u9898\u3002

"},{"location":"chapter_computational_complexity/iteration_and_recursion/#3_1","title":"3. \u00a0 \u9012\u5f52\u6811","text":"

\u5f53\u5904\u7406\u4e0e\u201c\u5206\u6cbb\u201d\u76f8\u5173\u7684\u7b97\u6cd5\u95ee\u9898\u65f6\uff0c\u9012\u5f52\u5f80\u5f80\u6bd4\u8fed\u4ee3\u7684\u601d\u8def\u66f4\u52a0\u76f4\u89c2\u3001\u4ee3\u7801\u66f4\u52a0\u6613\u8bfb\u3002\u4ee5\u201c\u6590\u6ce2\u90a3\u5951\u6570\u5217\u201d\u4e3a\u4f8b\u3002

Question

\u7ed9\u5b9a\u4e00\u4e2a\u6590\u6ce2\u90a3\u5951\u6570\u5217 \\(0, 1, 1, 2, 3, 5, 8, 13, \\dots\\) \uff0c\u6c42\u8be5\u6570\u5217\u7684\u7b2c \\(n\\) \u4e2a\u6570\u5b57\u3002

\u8bbe\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u7b2c \\(n\\) \u4e2a\u6570\u5b57\u4e3a \\(f(n)\\) \uff0c\u6613\u5f97\u4e24\u4e2a\u7ed3\u8bba\u3002

  • \u6570\u5217\u7684\u524d\u4e24\u4e2a\u6570\u5b57\u4e3a \\(f(1) = 0\\) \u548c \\(f(2) = 1\\) \u3002
  • \u6570\u5217\u4e2d\u7684\u6bcf\u4e2a\u6570\u5b57\u662f\u524d\u4e24\u4e2a\u6570\u5b57\u7684\u548c\uff0c\u5373 \\(f(n) = f(n - 1) + f(n - 2)\\) \u3002

\u6309\u7167\u9012\u63a8\u5173\u7cfb\u8fdb\u884c\u9012\u5f52\u8c03\u7528\uff0c\u5c06\u524d\u4e24\u4e2a\u6570\u5b57\u4f5c\u4e3a\u7ec8\u6b62\u6761\u4ef6\uff0c\u4fbf\u53ef\u5199\u51fa\u9012\u5f52\u4ee3\u7801\u3002\u8c03\u7528 fib(n) \u5373\u53ef\u5f97\u5230\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u7b2c \\(n\\) \u4e2a\u6570\u5b57\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig recursion.py
def fib(n: int) -> int:\n\"\"\"\u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52\"\"\"\n# \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\nif n == 1 or n == 2:\nreturn n - 1\n# \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\nres = fib(n - 1) + fib(n - 2)\n# \u8fd4\u56de\u7ed3\u679c f(n)\nreturn res\n
recursion.cpp
/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n// \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\nif (n == 1 || n == 2)\nreturn n - 1;\n// \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\nint res = fib(n - 1) + fib(n - 2);\n// \u8fd4\u56de\u7ed3\u679c f(n)\nreturn res;\n}\n
recursion.java
/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n// \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\nif (n == 1 || n == 2)\nreturn n - 1;\n// \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\nint res = fib(n - 1) + fib(n - 2);\n// \u8fd4\u56de\u7ed3\u679c f(n)\nreturn res;\n}\n
recursion.cs
/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint Fib(int n) {\n// \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\nif (n == 1 || n == 2)\nreturn n - 1;\n// \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\nint res = Fib(n - 1) + Fib(n - 2);\n// \u8fd4\u56de\u7ed3\u679c f(n)\nreturn res;\n}\n
recursion.go
/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunc fib(n int) int {\n// \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\nif n == 1 || n == 2 {\nreturn n - 1\n}\n// \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\nres := fib(n-1) + fib(n-2)\n// \u8fd4\u56de\u7ed3\u679c f(n)\nreturn res\n}\n
recursion.swift
/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunc fib(n: Int) -> Int {\n// \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\nif n == 1 || n == 2 {\nreturn n - 1\n}\n// \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\nlet res = fib(n: n - 1) + fib(n: n - 2)\n// \u8fd4\u56de\u7ed3\u679c f(n)\nreturn res\n}\n
recursion.js
/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunction fib(n) {\n// \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\nif (n === 1 || n === 2) return n - 1;\n// \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\nconst res = fib(n - 1) + fib(n - 2);\n// \u8fd4\u56de\u7ed3\u679c f(n)\nreturn res;\n}\n
recursion.ts
/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunction fib(n: number): number {\n// \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\nif (n === 1 || n === 2) return n - 1;\n// \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\nconst res = fib(n - 1) + fib(n - 2);\n// \u8fd4\u56de\u7ed3\u679c f(n)\nreturn res;\n}\n
recursion.dart
/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n// \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\nif (n == 1 || n == 2) return n - 1;\n// \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\nint res = fib(n - 1) + fib(n - 2);\n// \u8fd4\u56de\u7ed3\u679c f(n)\nreturn res;\n}\n
recursion.rs
/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfn fib(n: i32) -> i32 {\n// \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\nif n == 1 || n == 2 {\nreturn n - 1;\n}\n// \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\nlet res = fib(n - 1) + fib(n - 2);\n// \u8fd4\u56de\u7ed3\u679c\nres\n}\n
recursion.c
/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n// \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\nif (n == 1 || n == 2)\nreturn n - 1;\n// \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\nint res = fib(n - 1) + fib(n - 2);\n// \u8fd4\u56de\u7ed3\u679c f(n)\nreturn res;\n}\n
recursion.zig
[class]{}-[func]{fib}\n

\u89c2\u5bdf\u4ee5\u4e0a\u4ee3\u7801\uff0c\u6211\u4eec\u5728\u51fd\u6570\u5185\u9012\u5f52\u8c03\u7528\u4e86\u4e24\u4e2a\u51fd\u6570\uff0c\u8fd9\u610f\u5473\u7740\u4ece\u4e00\u4e2a\u8c03\u7528\u4ea7\u751f\u4e86\u4e24\u4e2a\u8c03\u7528\u5206\u652f\u3002\u5982\u56fe 2-6 \u6240\u793a\uff0c\u8fd9\u6837\u4e0d\u65ad\u9012\u5f52\u8c03\u7528\u4e0b\u53bb\uff0c\u6700\u7ec8\u5c06\u4ea7\u751f\u4e00\u4e2a\u5c42\u6570\u4e3a \\(n\\) \u7684\u300c\u9012\u5f52\u6811 recursion tree\u300d\u3002

\u56fe 2-6 \u00a0 \u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u9012\u5f52\u6811

\u672c\u8d28\u4e0a\u770b\uff0c\u9012\u5f52\u4f53\u73b0\u201c\u5c06\u95ee\u9898\u5206\u89e3\u4e3a\u66f4\u5c0f\u5b50\u95ee\u9898\u201d\u7684\u601d\u7ef4\u8303\u5f0f\uff0c\u8fd9\u79cd\u5206\u6cbb\u7b56\u7565\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002

  • \u4ece\u7b97\u6cd5\u89d2\u5ea6\u770b\uff0c\u641c\u7d22\u3001\u6392\u5e8f\u3001\u56de\u6eaf\u3001\u5206\u6cbb\u3001\u52a8\u6001\u89c4\u5212\u7b49\u8bb8\u591a\u91cd\u8981\u7b97\u6cd5\u7b56\u7565\u90fd\u76f4\u63a5\u6216\u95f4\u63a5\u5730\u5e94\u7528\u8fd9\u79cd\u601d\u7ef4\u65b9\u5f0f\u3002
  • \u4ece\u6570\u636e\u7ed3\u6784\u89d2\u5ea6\u770b\uff0c\u9012\u5f52\u5929\u7136\u9002\u5408\u5904\u7406\u94fe\u8868\u3001\u6811\u548c\u56fe\u7684\u76f8\u5173\u95ee\u9898\uff0c\u56e0\u4e3a\u5b83\u4eec\u975e\u5e38\u9002\u5408\u7528\u5206\u6cbb\u601d\u60f3\u8fdb\u884c\u5206\u6790\u3002
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#223","title":"2.2.3 \u00a0 \u4e24\u8005\u5bf9\u6bd4","text":"

\u603b\u7ed3\u4ee5\u4e0a\u5185\u5bb9\uff0c\u5982\u8868 2-1 \u6240\u793a\uff0c\u8fed\u4ee3\u548c\u9012\u5f52\u5728\u5b9e\u73b0\u3001\u6027\u80fd\u548c\u9002\u7528\u6027\u4e0a\u6709\u6240\u4e0d\u540c\u3002

\u8868 2-1 \u00a0 \u8fed\u4ee3\u4e0e\u9012\u5f52\u7279\u70b9\u5bf9\u6bd4

\u8fed\u4ee3 \u9012\u5f52 \u5b9e\u73b0\u65b9\u5f0f \u5faa\u73af\u7ed3\u6784 \u51fd\u6570\u8c03\u7528\u81ea\u8eab \u65f6\u95f4\u6548\u7387 \u6548\u7387\u901a\u5e38\u8f83\u9ad8\uff0c\u65e0\u51fd\u6570\u8c03\u7528\u5f00\u9500 \u6bcf\u6b21\u51fd\u6570\u8c03\u7528\u90fd\u4f1a\u4ea7\u751f\u5f00\u9500 \u5185\u5b58\u4f7f\u7528 \u901a\u5e38\u4f7f\u7528\u56fa\u5b9a\u5927\u5c0f\u7684\u5185\u5b58\u7a7a\u95f4 \u7d2f\u79ef\u51fd\u6570\u8c03\u7528\u53ef\u80fd\u4f7f\u7528\u5927\u91cf\u7684\u6808\u5e27\u7a7a\u95f4 \u9002\u7528\u95ee\u9898 \u9002\u7528\u4e8e\u7b80\u5355\u5faa\u73af\u4efb\u52a1\uff0c\u4ee3\u7801\u76f4\u89c2\u3001\u53ef\u8bfb\u6027\u597d \u9002\u7528\u4e8e\u5b50\u95ee\u9898\u5206\u89e3\uff0c\u5982\u6811\u3001\u56fe\u3001\u5206\u6cbb\u3001\u56de\u6eaf\u7b49\uff0c\u4ee3\u7801\u7ed3\u6784\u7b80\u6d01\u3001\u6e05\u6670

Tip

\u5982\u679c\u611f\u89c9\u4ee5\u4e0b\u5185\u5bb9\u7406\u89e3\u56f0\u96be\uff0c\u53ef\u4ee5\u5728\u8bfb\u5b8c\u201c\u6808\u201d\u7ae0\u8282\u540e\u518d\u6765\u590d\u4e60\u3002

\u90a3\u4e48\uff0c\u8fed\u4ee3\u548c\u9012\u5f52\u5177\u6709\u4ec0\u4e48\u5185\u5728\u8054\u7cfb\u5462\uff1f\u4ee5\u4e0a\u8ff0\u7684\u9012\u5f52\u51fd\u6570\u4e3a\u4f8b\uff0c\u6c42\u548c\u64cd\u4f5c\u5728\u9012\u5f52\u7684\u201c\u5f52\u201d\u9636\u6bb5\u8fdb\u884c\u3002\u8fd9\u610f\u5473\u7740\u6700\u521d\u88ab\u8c03\u7528\u7684\u51fd\u6570\u5b9e\u9645\u4e0a\u662f\u6700\u540e\u5b8c\u6210\u5176\u6c42\u548c\u64cd\u4f5c\u7684\uff0c\u8fd9\u79cd\u5de5\u4f5c\u673a\u5236\u4e0e\u6808\u7684\u201c\u5148\u5165\u540e\u51fa\u201d\u539f\u5219\u662f\u5f02\u66f2\u540c\u5de5\u7684\u3002

\u4e8b\u5b9e\u4e0a\uff0c\u201c\u8c03\u7528\u6808\u201d\u548c\u201c\u6808\u5e27\u7a7a\u95f4\u201d\u8fd9\u7c7b\u9012\u5f52\u672f\u8bed\u5df2\u7ecf\u6697\u793a\u4e86\u9012\u5f52\u4e0e\u6808\u4e4b\u95f4\u7684\u5bc6\u5207\u5173\u7cfb\u3002

  1. \u9012\uff1a\u5f53\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u7cfb\u7edf\u4f1a\u5728\u201c\u8c03\u7528\u6808\u201d\u4e0a\u4e3a\u8be5\u51fd\u6570\u5206\u914d\u65b0\u7684\u6808\u5e27\uff0c\u7528\u4e8e\u5b58\u50a8\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\u3001\u53c2\u6570\u3001\u8fd4\u56de\u5730\u5740\u7b49\u6570\u636e\u3002
  2. \u5f52\uff1a\u5f53\u51fd\u6570\u5b8c\u6210\u6267\u884c\u5e76\u8fd4\u56de\u65f6\uff0c\u5bf9\u5e94\u7684\u6808\u5e27\u4f1a\u4ece\u201c\u8c03\u7528\u6808\u201d\u4e0a\u88ab\u79fb\u9664\uff0c\u6062\u590d\u4e4b\u524d\u51fd\u6570\u7684\u6267\u884c\u73af\u5883\u3002

\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u8c03\u7528\u6808\u7684\u884c\u4e3a\uff0c\u4ece\u800c\u5c06\u9012\u5f52\u8f6c\u5316\u4e3a\u8fed\u4ee3\u5f62\u5f0f\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig recursion.py
def for_loop_recur(n: int) -> int:\n\"\"\"\u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52\"\"\"\n# \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\nstack = []\nres = 0\n# \u9012\uff1a\u9012\u5f52\u8c03\u7528\nfor i in range(n, 0, -1):\n# \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\nstack.append(i)\n# \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nwhile stack:\n# \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\nres += stack.pop()\n# res = 1+2+3+...+n\nreturn res\n
recursion.cpp
/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n// \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\nstack<int> stack;\nint res = 0;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nfor (int i = n; i > 0; i--) {\n// \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\nstack.push(i);\n}\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nwhile (!stack.empty()) {\n// \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\nres += stack.top();\nstack.pop();\n}\n// res = 1+2+3+...+n\nreturn res;\n}\n
recursion.java
/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n// \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\nStack<Integer> stack = new Stack<>();\nint res = 0;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nfor (int i = n; i > 0; i--) {\n// \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\nstack.push(i);\n}\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nwhile (!stack.isEmpty()) {\n// \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\nres += stack.pop();\n}\n// res = 1+2+3+...+n\nreturn res;\n}\n
recursion.cs
/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint ForLoopRecur(int n) {\n// \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\nStack<int> stack = new();\nint res = 0;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nfor (int i = n; i > 0; i--) {\n// \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\nstack.Push(i);\n}\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nwhile (stack.Count > 0) {\n// \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\nres += stack.Pop();\n}\n// res = 1+2+3+...+n\nreturn res;\n}\n
recursion.go
/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunc forLoopRecur(n int) int {\n// \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\nstack := list.New()\nres := 0\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nfor i := n; i > 0; i-- {\n// \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\nstack.PushBack(i)\n}\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nfor stack.Len() != 0 {\n// \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\nres += stack.Back().Value.(int)\nstack.Remove(stack.Back())\n}\n// res = 1+2+3+...+n\nreturn res\n}\n
recursion.swift
/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunc forLoopRecur(n: Int) -> Int {\n// \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\nvar stack: [Int] = []\nvar res = 0\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nfor i in stride(from: n, to: 0, by: -1) {\n// \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\nstack.append(i)\n}\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nwhile !stack.isEmpty {\n// \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\nres += stack.removeLast()\n}\n// res = 1+2+3+...+n\nreturn res\n}\n
recursion.js
/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunction forLoopRecur(n) {\n// \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\nconst stack = [];\nlet res = 0;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nfor (let i = 1; i <= n; i++) {\n// \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\nstack.push(i);\n}\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nwhile (stack.length) { // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\nres += stack.pop();\n}\n// res = 1+2+3+...+n\nreturn res;\n}\n
recursion.ts
/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunction forLoopRecur(n: number): number {\n// \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808 \nconst stack: number[] = [];\nlet res: number = 0;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nfor (let i = 1; i <= n; i++) {\n// \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\nstack.push(i);\n}\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nwhile (stack.length) { // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\nres += stack.pop();\n}\n// res = 1+2+3+...+n\nreturn res;\n}\n
recursion.dart
/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n// \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\nList<int> stack = [];\nint res = 0;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nfor (int i = n; i > 0; i--) {\n// \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\nstack.add(i);\n}\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nwhile (!stack.isEmpty) {\n// \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\nres += stack.removeLast();\n}\n// res = 1+2+3+...+n\nreturn res;\n}\n
recursion.rs
/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfn for_loop_recur(n: i32) -> i32 {\n// \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\nlet mut stack = Vec::new();\nlet mut res = 0;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nfor i in (1..=n).rev() {\n// \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\nstack.push(i);\n}\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nwhile !stack.is_empty() {\n// \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\nres += stack.pop().unwrap();\n}\n// res = 1+2+3+...+n\nres\n}\n
recursion.c
/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\nint stack[1000]; // \u501f\u52a9\u4e00\u4e2a\u5927\u6570\u7ec4\u6765\u6a21\u62df\u6808\nint top = 0;\nint res = 0;\n// \u9012\uff1a\u9012\u5f52\u8c03\u7528\nfor (int i = n; i > 0; i--) {\n// \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\nstack[top++] = i;\n}\n// \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\nwhile (top >= 0) {\n// \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\nres += stack[top--];\n}\n// res = 1+2+3+...+n\nreturn res;\n}\n
recursion.zig
[class]{}-[func]{forLoopRecur}\n

\u89c2\u5bdf\u4ee5\u4e0a\u4ee3\u7801\uff0c\u5f53\u9012\u5f52\u88ab\u8f6c\u6362\u4e3a\u8fed\u4ee3\u540e\uff0c\u4ee3\u7801\u53d8\u5f97\u66f4\u52a0\u590d\u6742\u4e86\u3002\u5c3d\u7ba1\u8fed\u4ee3\u548c\u9012\u5f52\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4e92\u76f8\u8f6c\u6362\uff0c\u4f46\u4e5f\u4e0d\u4e00\u5b9a\u503c\u5f97\u8fd9\u6837\u505a\uff0c\u6709\u4ee5\u4e0b\u4e24\u70b9\u539f\u56e0\u3002

  • \u8f6c\u5316\u540e\u7684\u4ee3\u7801\u53ef\u80fd\u66f4\u52a0\u96be\u4ee5\u7406\u89e3\uff0c\u53ef\u8bfb\u6027\u66f4\u5dee\u3002
  • \u5bf9\u4e8e\u67d0\u4e9b\u590d\u6742\u95ee\u9898\uff0c\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\u7684\u884c\u4e3a\u53ef\u80fd\u975e\u5e38\u56f0\u96be\u3002

\u603b\u4e4b\uff0c\u9009\u62e9\u8fed\u4ee3\u8fd8\u662f\u9012\u5f52\u53d6\u51b3\u4e8e\u7279\u5b9a\u95ee\u9898\u7684\u6027\u8d28\u3002\u5728\u7f16\u7a0b\u5b9e\u8df5\u4e2d\uff0c\u6743\u8861\u4e24\u8005\u7684\u4f18\u52a3\u5e76\u6839\u636e\u60c5\u5883\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002

"},{"location":"chapter_computational_complexity/performance_evaluation/","title":"2.1 \u00a0 \u7b97\u6cd5\u6548\u7387\u8bc4\u4f30","text":"

\u5728\u7b97\u6cd5\u8bbe\u8ba1\u4e2d\uff0c\u6211\u4eec\u5148\u540e\u8ffd\u6c42\u4ee5\u4e0b\u4e24\u4e2a\u5c42\u9762\u7684\u76ee\u6807\u3002

  1. \u627e\u5230\u95ee\u9898\u89e3\u6cd5\uff1a\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\uff1a\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

\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u80fd\u591f\u89e3\u51b3\u95ee\u9898\u7684\u524d\u63d0\u4e0b\uff0c\u7b97\u6cd5\u6548\u7387\u5df2\u6210\u4e3a\u8861\u91cf\u7b97\u6cd5\u4f18\u52a3\u7684\u4e3b\u8981\u8bc4\u4ef7\u6307\u6807\uff0c\u5b83\u5305\u62ec\u4ee5\u4e0b\u4e24\u4e2a\u7ef4\u5ea6\u3002

  • \u65f6\u95f4\u6548\u7387\uff1a\u7b97\u6cd5\u8fd0\u884c\u901f\u5ea6\u7684\u5feb\u6162\u3002
  • \u7a7a\u95f4\u6548\u7387\uff1a\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\u800c\u6709\u6548\u5730\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\u81f3\u5173\u91cd\u8981\uff0c\u56e0\u4e3a\u53ea\u6709\u8fd9\u6837\u6211\u4eec\u624d\u80fd\u5c06\u5404\u79cd\u7b97\u6cd5\u8fdb\u884c\u5bf9\u6bd4\uff0c\u4ece\u800c\u6307\u5bfc\u7b97\u6cd5\u8bbe\u8ba1\u4e0e\u4f18\u5316\u8fc7\u7a0b\u3002

\u6548\u7387\u8bc4\u4f30\u65b9\u6cd5\u4e3b\u8981\u5206\u4e3a\u4e24\u79cd\uff1a\u5b9e\u9645\u6d4b\u8bd5\u3001\u7406\u8bba\u4f30\u7b97\u3002

"},{"location":"chapter_computational_complexity/performance_evaluation/#211","title":"2.1.1 \u00a0 \u5b9e\u9645\u6d4b\u8bd5","text":"

\u5047\u8bbe\u6211\u4eec\u73b0\u5728\u6709\u7b97\u6cd5 A \u548c\u7b97\u6cd5 B \uff0c\u5b83\u4eec\u90fd\u80fd\u89e3\u51b3\u540c\u4e00\u95ee\u9898\uff0c\u73b0\u5728\u9700\u8981\u5bf9\u6bd4\u8fd9\u4e24\u4e2a\u7b97\u6cd5\u7684\u6548\u7387\u3002\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u627e\u4e00\u53f0\u8ba1\u7b97\u673a\uff0c\u8fd0\u884c\u8fd9\u4e24\u4e2a\u7b97\u6cd5\uff0c\u5e76\u76d1\u63a7\u8bb0\u5f55\u5b83\u4eec\u7684\u8fd0\u884c\u65f6\u95f4\u548c\u5185\u5b58\u5360\u7528\u60c5\u51b5\u3002\u8fd9\u79cd\u8bc4\u4f30\u65b9\u5f0f\u80fd\u591f\u53cd\u6620\u771f\u5b9e\u60c5\u51b5\uff0c\u4f46\u4e5f\u5b58\u5728\u8f83\u5927\u5c40\u9650\u6027\u3002

\u4e00\u65b9\u9762\uff0c\u96be\u4ee5\u6392\u9664\u6d4b\u8bd5\u73af\u5883\u7684\u5e72\u6270\u56e0\u7d20\u3002\u786c\u4ef6\u914d\u7f6e\u4f1a\u5f71\u54cd\u7b97\u6cd5\u7684\u6027\u80fd\u8868\u73b0\u3002\u6bd4\u5982\u5728\u67d0\u53f0\u8ba1\u7b97\u673a\u4e2d\uff0c\u7b97\u6cd5 A \u7684\u8fd0\u884c\u65f6\u95f4\u6bd4\u7b97\u6cd5 B \u77ed\uff1b\u4f46\u5728\u53e6\u4e00\u53f0\u914d\u7f6e\u4e0d\u540c\u7684\u8ba1\u7b97\u673a\u4e2d\uff0c\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\u7edf\u8ba1\u5e73\u5747\u6548\u7387\uff0c\u800c\u8fd9\u662f\u4e0d\u73b0\u5b9e\u7684\u3002

\u53e6\u4e00\u65b9\u9762\uff0c\u5c55\u5f00\u5b8c\u6574\u6d4b\u8bd5\u975e\u5e38\u8017\u8d39\u8d44\u6e90\u3002\u968f\u7740\u8f93\u5165\u6570\u636e\u91cf\u7684\u53d8\u5316\uff0c\u7b97\u6cd5\u4f1a\u8868\u73b0\u51fa\u4e0d\u540c\u7684\u6548\u7387\u3002\u4f8b\u5982\uff0c\u5728\u8f93\u5165\u6570\u636e\u91cf\u8f83\u5c0f\u65f6\uff0c\u7b97\u6cd5 A \u7684\u8fd0\u884c\u65f6\u95f4\u6bd4\u7b97\u6cd5 B \u66f4\u5c11\uff1b\u800c\u8f93\u5165\u6570\u636e\u91cf\u8f83\u5927\u65f6\uff0c\u6d4b\u8bd5\u7ed3\u679c\u53ef\u80fd\u6070\u6070\u76f8\u53cd\u3002\u56e0\u6b64\uff0c\u4e3a\u4e86\u5f97\u5230\u6709\u8bf4\u670d\u529b\u7684\u7ed3\u8bba\uff0c\u6211\u4eec\u9700\u8981\u6d4b\u8bd5\u5404\u79cd\u89c4\u6a21\u7684\u8f93\u5165\u6570\u636e\uff0c\u800c\u8fd9\u9700\u8981\u8017\u8d39\u5927\u91cf\u7684\u8ba1\u7b97\u8d44\u6e90\u3002

"},{"location":"chapter_computational_complexity/performance_evaluation/#212","title":"2.1.2 \u00a0 \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\u6e10\u8fd1\u590d\u6742\u5ea6\u5206\u6790 asymptotic complexity analysis\u300d\uff0c\u7b80\u79f0\u300c\u590d\u6742\u5ea6\u5206\u6790\u300d\u3002

\u590d\u6742\u5ea6\u5206\u6790\u4f53\u73b0\u7b97\u6cd5\u8fd0\u884c\u6240\u9700\u7684\u65f6\u95f4\uff08\u7a7a\u95f4\uff09\u8d44\u6e90\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u5b83\u63cf\u8ff0\u4e86\u968f\u7740\u8f93\u5165\u6570\u636e\u5927\u5c0f\u7684\u589e\u52a0\uff0c\u7b97\u6cd5\u6267\u884c\u6240\u9700\u65f6\u95f4\u548c\u7a7a\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u3002\u8fd9\u4e2a\u5b9a\u4e49\u6709\u4e9b\u62d7\u53e3\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u5206\u4e3a\u4e09\u4e2a\u91cd\u70b9\u6765\u7406\u89e3\u3002

  • \u201c\u65f6\u95f4\u548c\u7a7a\u95f4\u8d44\u6e90\u201d\u5206\u522b\u5bf9\u5e94\u300c\u65f6\u95f4\u590d\u6742\u5ea6 time complexity\u300d\u548c\u300c\u7a7a\u95f4\u590d\u6742\u5ea6 space complexity\u300d\u3002
  • \u201c\u968f\u7740\u8f93\u5165\u6570\u636e\u5927\u5c0f\u7684\u589e\u52a0\u201d\u610f\u5473\u7740\u590d\u6742\u5ea6\u53cd\u6620\u4e86\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u4e0e\u8f93\u5165\u6570\u636e\u4f53\u91cf\u4e4b\u95f4\u7684\u5173\u7cfb\u3002
  • \u201c\u65f6\u95f4\u548c\u7a7a\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u201d\u8868\u793a\u590d\u6742\u5ea6\u5206\u6790\u5173\u6ce8\u7684\u4e0d\u662f\u8fd0\u884c\u65f6\u95f4\u6216\u5360\u7528\u7a7a\u95f4\u7684\u5177\u4f53\u503c\uff0c\u800c\u662f\u65f6\u95f4\u6216\u7a7a\u95f4\u589e\u957f\u7684\u201c\u5feb\u6162\u201d\u3002

\u590d\u6742\u5ea6\u5206\u6790\u514b\u670d\u4e86\u5b9e\u9645\u6d4b\u8bd5\u65b9\u6cd5\u7684\u5f0a\u7aef\uff0c\u4f53\u73b0\u5728\u4ee5\u4e0b\u4e24\u4e2a\u65b9\u9762\u3002

  • \u5b83\u72ec\u7acb\u4e8e\u6d4b\u8bd5\u73af\u5883\uff0c\u5206\u6790\u7ed3\u679c\u9002\u7528\u4e8e\u6240\u6709\u8fd0\u884c\u5e73\u53f0\u3002
  • \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

Tip

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

\u590d\u6742\u5ea6\u5206\u6790\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u628a\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\u7684\u201c\u6807\u5c3a\u201d\uff0c\u4f7f\u6211\u4eec\u53ef\u4ee5\u8861\u91cf\u6267\u884c\u67d0\u4e2a\u7b97\u6cd5\u6240\u9700\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u8d44\u6e90\uff0c\u5bf9\u6bd4\u4e0d\u540c\u7b97\u6cd5\u4e4b\u95f4\u7684\u6548\u7387\u3002

\u590d\u6742\u5ea6\u662f\u4e2a\u6570\u5b66\u6982\u5ff5\uff0c\u5bf9\u4e8e\u521d\u5b66\u8005\u53ef\u80fd\u6bd4\u8f83\u62bd\u8c61\uff0c\u5b66\u4e60\u96be\u5ea6\u76f8\u5bf9\u8f83\u9ad8\u3002\u4ece\u8fd9\u4e2a\u89d2\u5ea6\u770b\uff0c\u590d\u6742\u5ea6\u5206\u6790\u53ef\u80fd\u4e0d\u592a\u9002\u5408\u4f5c\u4e3a\u6700\u5148\u4ecb\u7ecd\u7684\u5185\u5bb9\u3002\u7136\u800c\uff0c\u5f53\u6211\u4eec\u8ba8\u8bba\u67d0\u4e2a\u6570\u636e\u7ed3\u6784\u6216\u7b97\u6cd5\u7684\u7279\u70b9\u65f6\uff0c\u96be\u4ee5\u907f\u514d\u8981\u5206\u6790\u5176\u8fd0\u884c\u901f\u5ea6\u548c\u7a7a\u95f4\u4f7f\u7528\u60c5\u51b5\u3002

\u7efc\u4e0a\u6240\u8ff0\uff0c\u5efa\u8bae\u4f60\u5728\u6df1\u5165\u5b66\u4e60\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u4e4b\u524d\uff0c\u5148\u5bf9\u590d\u6742\u5ea6\u5206\u6790\u5efa\u7acb\u521d\u6b65\u7684\u4e86\u89e3\uff0c\u4ee5\u4fbf\u80fd\u591f\u5b8c\u6210\u7b80\u5355\u7b97\u6cd5\u7684\u590d\u6742\u5ea6\u5206\u6790\u3002

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

\u300c\u7a7a\u95f4\u590d\u6742\u5ea6 space complexity\u300d\u7528\u4e8e\u8861\u91cf\u7b97\u6cd5\u5360\u7528\u5185\u5b58\u7a7a\u95f4\u968f\u7740\u6570\u636e\u91cf\u53d8\u5927\u65f6\u7684\u589e\u957f\u8d8b\u52bf\u3002\u8fd9\u4e2a\u6982\u5ff5\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u975e\u5e38\u7c7b\u4f3c\uff0c\u53ea\u9700\u5c06\u201c\u8fd0\u884c\u65f6\u95f4\u201d\u66ff\u6362\u4e3a\u201c\u5360\u7528\u5185\u5b58\u7a7a\u95f4\u201d\u3002

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

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

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

\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u7edf\u8ba1\u8303\u56f4\u662f\u201c\u6682\u5b58\u7a7a\u95f4\u201d\u52a0\u4e0a\u201c\u8f93\u51fa\u7a7a\u95f4\u201d\u3002

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

  • \u6682\u5b58\u6570\u636e\uff1a\u7528\u4e8e\u4fdd\u5b58\u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u5404\u79cd\u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u7b49\u3002
  • \u6808\u5e27\u7a7a\u95f4\uff1a\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
  • \u6307\u4ee4\u7a7a\u95f4\uff1a\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

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

\u56fe 2-15 \u00a0 \u7b97\u6cd5\u4f7f\u7528\u7684\u76f8\u5173\u7a7a\u95f4

PythonC++JavaC#GoSwiftJSTSDartRustCZig
class Node:\n\"\"\"\u7c7b\"\"\"\ndef __init__(self, x: int):\nself.val: int = x              # \u8282\u70b9\u503c\nself.next: Node | None = None  # \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\ndef function() -> int:\n\"\"\"\u51fd\u6570\"\"\"\n# \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\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 */\nstruct Node {\nint val;\nNode *next;\nNode(int x) : val(x), next(nullptr) {}\n};\n/* \u51fd\u6570 */\nint func() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\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
/* \u7c7b */\nclass Node {\nint val;\nNode next;\nNode(int x) { val = x; }\n}\n/* \u51fd\u6570 */\nint function() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\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
/* \u7c7b */\nclass Node {\nint val;\nNode next;\nNode(int x) { val = x; }\n}\n/* \u51fd\u6570 */\nint Function() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\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(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 */\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// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\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 {\nvar val: Int\nvar next: Node?\ninit(x: Int) {\nval = x\n}\n}\n/* \u51fd\u6570 */\nfunc function() -> Int {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\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
/* \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// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\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// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\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
/* \u7c7b */\nclass Node {\nint val;\nNode next;\nNode(this.val, [this.next]);\n}\n/* \u51fd\u6570 */\nint function() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\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
use std::rc::Rc;\nuse std::cell::RefCell;\n/* \u7ed3\u6784\u4f53 */\nstruct Node {\nval: i32,\nnext: Option<Rc<RefCell<Node>>>,\n}\n/* \u521b\u5efa Node \u7ed3\u6784\u4f53 */\nimpl Node {\nfn new(val: i32) -> Self {\nSelf { val: val, next: None }\n}\n}\n/* \u51fd\u6570 */\nfn function() -> i32 {      // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\nreturn 0;\n}\nfn algorithm(n: i32) -> i32 {       // \u8f93\u5165\u6570\u636e\nconst a: i32 = 0;               // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nlet mut b = 0;                  // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nlet node = Node::new(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
/* \u51fd\u6570 */\nint func() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\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
\n
"},{"location":"chapter_computational_complexity/space_complexity/#242","title":"2.4.2 \u00a0 \u63a8\u7b97\u65b9\u6cd5","text":"

\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u63a8\u7b97\u65b9\u6cd5\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u5927\u81f4\u76f8\u540c\uff0c\u53ea\u9700\u5c06\u7edf\u8ba1\u5bf9\u8c61\u4ece\u201c\u64cd\u4f5c\u6570\u91cf\u201d\u8f6c\u4e3a\u201c\u4f7f\u7528\u7a7a\u95f4\u5927\u5c0f\u201d\u3002

\u800c\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u540c\u7684\u662f\uff0c\u6211\u4eec\u901a\u5e38\u53ea\u5173\u6ce8\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u3002\u8fd9\u662f\u56e0\u4e3a\u5185\u5b58\u7a7a\u95f4\u662f\u4e00\u9879\u786c\u6027\u8981\u6c42\uff0c\u6211\u4eec\u5fc5\u987b\u786e\u4fdd\u5728\u6240\u6709\u8f93\u5165\u6570\u636e\u4e0b\u90fd\u6709\u8db3\u591f\u7684\u5185\u5b58\u7a7a\u95f4\u9884\u7559\u3002

\u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e2d\u7684\u201c\u6700\u5dee\u201d\u6709\u4e24\u5c42\u542b\u4e49\u3002

  1. \u4ee5\u6700\u5dee\u8f93\u5165\u6570\u636e\u4e3a\u51c6\uff1a\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)\\) \u3002
  2. \u4ee5\u7b97\u6cd5\u8fd0\u884c\u4e2d\u7684\u5cf0\u503c\u5185\u5b58\u4e3a\u51c6\uff1a\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)\\) \u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig
def algorithm(n: int):\na = 0               # O(1)\nb = [0] * 10000     # O(1)\nif n > 10:\nnums = [0] * n  # O(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
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)\nint[] b = new int[10000];    // O(1)\nif (n > 10) {\nint[] nums = new int[n]; // O(n)\n}\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
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
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)\nList<int> b = List.filled(10000, 0);  // O(1)\nif (n > 10) {\nList<int> nums = List.filled(n, 0); // O(n)\n}\n}\n
fn algorithm(n: i32) {\nlet a = 0;                              // O(1)\nlet b = [0; 10000];                     // O(1)\nif n > 10 {\nlet nums = vec![0; n as usize];     // O(n)\n}\n}\n
void algorithm(int n) {\nint a = 0;               // O(1)\nint b[10000];            // O(1)\nif (n > 10)\nint nums[n] = {0};   // O(n)\n}\n
\n

\u5728\u9012\u5f52\u51fd\u6570\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u7edf\u8ba1\u6808\u5e27\u7a7a\u95f4\u3002\u4f8b\u5982\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff1a

  • \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
  • \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
PythonC++JavaC#GoSwiftJSTSDartRustCZig
def function() -> int:\n# \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0\ndef loop(n: int):\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
int func() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\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// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\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 Function() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\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) */\nint Recur(int n) {\nif (n == 1) return 1;\nreturn Recur(n - 1);\n}\n
func function() int {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\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
@discardableResult\nfunc function() -> Int {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\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
function constFunc() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\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// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\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 function() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\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
fn function() -> i32 {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nfn loop(n: i32) {\nfor i in 0..n {\nfunction();\n}\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(n: i32) {\nif n == 1 {\nreturn;\n}\nrecur(n - 1);\n}\n
int func() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\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
\n
"},{"location":"chapter_computational_complexity/space_complexity/#243","title":"2.4.3 \u00a0 \u5e38\u89c1\u7c7b\u578b","text":"

\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u56fe 2-16 \u5c55\u793a\u4e86\u5e38\u89c1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u7c7b\u578b\uff08\u4ece\u4f4e\u5230\u9ad8\u6392\u5217\uff09\u3002

\\[ \\begin{aligned} O(1) < O(\\log n) < O(n) < O(n^2) < O(2^n) \\newline \\text{\u5e38\u6570\u9636} < \\text{\u5bf9\u6570\u9636} < \\text{\u7ebf\u6027\u9636} < \\text{\u5e73\u65b9\u9636} < \\text{\u6307\u6570\u9636} \\end{aligned} \\]

\u56fe 2-16 \u00a0 \u5e38\u89c1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u7c7b\u578b

"},{"location":"chapter_computational_complexity/space_complexity/#1-o1","title":"1. \u00a0 \u5e38\u6570\u9636 \\(O(1)\\)","text":"

\u5e38\u6570\u9636\u5e38\u89c1\u4e8e\u6570\u91cf\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\u7684\u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u3002

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u5faa\u73af\u4e2d\u521d\u59cb\u5316\u53d8\u91cf\u6216\u8c03\u7528\u51fd\u6570\u800c\u5360\u7528\u7684\u5185\u5b58\uff0c\u5728\u8fdb\u5165\u4e0b\u4e00\u5faa\u73af\u540e\u5c31\u4f1a\u88ab\u91ca\u653e\uff0c\u56e0\u6b64\u4e0d\u4f1a\u7d2f\u79ef\u5360\u7528\u7a7a\u95f4\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.py
def function() -> int:\n\"\"\"\u51fd\u6570\"\"\"\n# \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0\ndef constant(n: int):\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.cpp
/* \u51fd\u6570 */\nint func() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \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.java
/* \u51fd\u6570 */\nint function() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \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.cs
/* \u51fd\u6570 */\nint Function() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \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(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.go
/* \u51fd\u6570 */\nfunc function() int {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\nreturn 0\n}\n/* \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.swift
/* \u51fd\u6570 */\n@discardableResult\nfunc function() -> Int {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0\n}\n/* \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.js
/* \u51fd\u6570 */\nfunction constFunc() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \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
/* \u51fd\u6570 */\nfunction constFunc(): number {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \u5e38\u6570\u9636 */\nfunction constant(n: number): void {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\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.dart
/* \u51fd\u6570 */\nint function() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \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);\nListNode node = ListNode(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
space_complexity.rs
/* \u51fd\u6570 */\nfn function() ->i32 {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \u5e38\u6570\u9636 */\n#[allow(unused)]\nfn constant(n: i32) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst A: i32 = 0;\nlet b = 0;\nlet nums = vec![0; 10000];\nlet node = ListNode::new(0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor i in 0..n {\nlet c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor i in 0..n {\nfunction();\n}\n}\n
space_complexity.c
/* \u51fd\u6570 */\nint func() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \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.zig
// \u51fd\u6570\nfn function() i32 {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n// \u5e38\u6570\u9636\nfn constant(n: i32) void {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\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
"},{"location":"chapter_computational_complexity/space_complexity/#2-on","title":"2. \u00a0 \u7ebf\u6027\u9636 \\(O(n)\\)","text":"

\u7ebf\u6027\u9636\u5e38\u89c1\u4e8e\u5143\u7d20\u6570\u91cf\u4e0e \\(n\\) \u6210\u6b63\u6bd4\u7684\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u7b49\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.py
def linear(n: int):\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\nhmap = dict[int, str]()\nfor i in range(n):\nhmap[i] = str(i)\n
space_complexity.cpp
/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\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.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.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.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.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.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.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
space_complexity.rs
/* \u7ebf\u6027\u9636 */\n#[allow(unused)]\nfn linear(n: i32) {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\nlet mut nums = vec![0; n as usize];\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nlet mut nodes = Vec::new();\nfor i in 0..n {\nnodes.push(ListNode::new(i))\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nlet mut map = HashMap::new();\nfor i in 0..n {\nmap.insert(i, i.to_string());\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.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

\u5982\u56fe 2-17 \u6240\u793a\uff0c\u6b64\u51fd\u6570\u7684\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u5373\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684 linear_recur() \u51fd\u6570\uff0c\u4f7f\u7528 \\(O(n)\\) \u5927\u5c0f\u7684\u6808\u5e27\u7a7a\u95f4\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.py
def linear_recur(n: int):\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.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.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.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.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.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.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.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
space_complexity.rs
/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn linear_recur(n: i32) {\nprintln!(\"\u9012\u5f52 n = {}\", n);\nif n == 1 {return};\nlinear_recur(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.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

\u56fe 2-17 \u00a0 \u9012\u5f52\u51fd\u6570\u4ea7\u751f\u7684\u7ebf\u6027\u9636\u7a7a\u95f4\u590d\u6742\u5ea6

"},{"location":"chapter_computational_complexity/space_complexity/#3-on2","title":"3. \u00a0 \u5e73\u65b9\u9636 \\(O(n^2)\\)","text":"

\u5e73\u65b9\u9636\u5e38\u89c1\u4e8e\u77e9\u9635\u548c\u56fe\uff0c\u5143\u7d20\u6570\u91cf\u4e0e \\(n\\) \u6210\u5e73\u65b9\u5173\u7cfb\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.py
def quadratic(n: int):\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.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.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.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.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.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.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.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
space_complexity.rs
/* \u5e73\u65b9\u9636 */\n#[allow(unused)]\nfn quadratic(n: i32) {\n// \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\nlet num_matrix = vec![vec![0; n as usize]; n as usize];\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nlet mut num_list = Vec::new();\nfor i in 0..n {\nlet mut tmp = Vec::new();\nfor j in 0..n {\ntmp.push(0);\n}\nnum_list.push(tmp);\n}\n}\n
space_complexity.c
/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\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.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

\u5982\u56fe 2-18 \u6240\u793a\uff0c\u8be5\u51fd\u6570\u7684\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u5728\u6bcf\u4e2a\u9012\u5f52\u51fd\u6570\u4e2d\u90fd\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u6570\u7ec4\uff0c\u957f\u5ea6\u5206\u522b\u4e3a \\(n\\)\u3001\\(n-1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \uff0c\u5e73\u5747\u957f\u5ea6\u4e3a \\(n / 2\\) \uff0c\u56e0\u6b64\u603b\u4f53\u5360\u7528 \\(O(n^2)\\) \u7a7a\u95f4\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.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.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.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.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.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.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.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.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
space_complexity.rs
/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn quadratic_recur(n: i32) -> i32 {\nif n <= 0 {return 0};\n// \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\nlet nums = vec![0; n as usize];\nprintln!(\"\u9012\u5f52 n = {} \u4e2d\u7684 nums \u957f\u5ea6 = {}\", n, nums.len());\nreturn quadratic_recur(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);\nint res = quadraticRecur(n - 1);\nfree(nums);\nreturn res;\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

\u56fe 2-18 \u00a0 \u9012\u5f52\u51fd\u6570\u4ea7\u751f\u7684\u5e73\u65b9\u9636\u7a7a\u95f4\u590d\u6742\u5ea6

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

\u6307\u6570\u9636\u5e38\u89c1\u4e8e\u4e8c\u53c9\u6811\u3002\u89c2\u5bdf\u56fe 2-19 \uff0c\u9ad8\u5ea6\u4e3a \\(n\\) \u7684\u201c\u6ee1\u4e8c\u53c9\u6811\u201d\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(2^n - 1\\) \uff0c\u5360\u7528 \\(O(2^n)\\) \u7a7a\u95f4\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.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.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.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.cs
/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode? BuildTree(int n) {\nif (n == 0) return null;\nTreeNode root = new(0) {\nleft = BuildTree(n - 1),\nright = BuildTree(n - 1)\n};\nreturn root;\n}\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.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.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.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
space_complexity.rs
/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfn build_tree(n: i32) -> Option<Rc<RefCell<TreeNode>>> {\nif n == 0 {return None};\nlet root = TreeNode::new(0);\nroot.borrow_mut().left = build_tree(n - 1);\nroot.borrow_mut().right = build_tree(n - 1);\nreturn Some(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.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

\u56fe 2-19 \u00a0 \u6ee1\u4e8c\u53c9\u6811\u4ea7\u751f\u7684\u6307\u6570\u9636\u7a7a\u95f4\u590d\u6742\u5ea6

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

\u5bf9\u6570\u9636\u5e38\u89c1\u4e8e\u5206\u6cbb\u7b97\u6cd5\u3002\u4f8b\u5982\u5f52\u5e76\u6392\u5e8f\uff0c\u8f93\u5165\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\uff0c\u6bcf\u8f6e\u9012\u5f52\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5212\u5206\u4e3a\u4e24\u534a\uff0c\u5f62\u6210\u9ad8\u5ea6\u4e3a \\(\\log n\\) \u7684\u9012\u5f52\u6811\uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u6808\u5e27\u7a7a\u95f4\u3002

\u518d\u4f8b\u5982\u5c06\u6570\u5b57\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\uff0c\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570 \\(n\\) \uff0c\u5b83\u7684\u4f4d\u6570\u4e3a \\(\\log_{10} n + 1\\) \uff0c\u5373\u5bf9\u5e94\u5b57\u7b26\u4e32\u957f\u5ea6\u4e3a \\(\\log_{10} n + 1\\) \uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log_{10} n + 1) = O(\\log n)\\) \u3002

"},{"location":"chapter_computational_complexity/space_complexity/#244","title":"2.4.4 \u00a0 \u6743\u8861\u65f6\u95f4\u4e0e\u7a7a\u95f4","text":"

\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u90fd\u80fd\u8fbe\u5230\u6700\u4f18\u3002\u7136\u800c\u5728\u5b9e\u9645\u60c5\u51b5\u4e2d\uff0c\u540c\u65f6\u4f18\u5316\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u901a\u5e38\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\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\u901a\u5e38\u662f\u66f4\u5e38\u7528\u7684\u7b56\u7565\u3002\u5f53\u7136\uff0c\u5728\u6570\u636e\u91cf\u5f88\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u63a7\u5236\u7a7a\u95f4\u590d\u6742\u5ea6\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002

"},{"location":"chapter_computational_complexity/summary/","title":"2.5 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_computational_complexity/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"

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

  • \u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u662f\u8861\u91cf\u7b97\u6cd5\u4f18\u52a3\u7684\u4e24\u4e2a\u4e3b\u8981\u8bc4\u4ef7\u6307\u6807\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\u7684\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\u5bf9\u5e94\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c\uff0c\u53cd\u6620\u5f53 \\(n\\) \u8d8b\u5411\u6b63\u65e0\u7a77\u65f6\uff0c\u64cd\u4f5c\u6570\u91cf \\(T(n)\\) \u7684\u589e\u957f\u7ea7\u522b\u3002
  • \u63a8\u7b97\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u4e3a\u4e24\u6b65\uff0c\u9996\u5148\u7edf\u8ba1\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)\\)\u3001\\(O(\\log n)\\)\u3001\\(O(n)\\)\u3001\\(O(n \\log n)\\)\u3001\\(O(n^2)\\)\u3001\\(O(2^n)\\) \u548c \\(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

  • \u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u4f5c\u7528\u7c7b\u4f3c\u4e8e\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\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)\\)\u3001\\(O(\\log n)\\)\u3001\\(O(n)\\)\u3001\\(O(n^2)\\) \u548c \\(O(2^n)\\) \u7b49\u3002
"},{"location":"chapter_computational_complexity/summary/#2-q-a","title":"2. \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\u3001Python\u3001C++\u3001Go\u3001C# \u7b49\uff09\u90fd\u4e0d\u652f\u6301\u81ea\u52a8\u4f18\u5316\u5c3e\u9012\u5f52\uff0c\u56e0\u6b64\u901a\u5e38\u8ba4\u4e3a\u7a7a\u95f4\u590d\u6742\u5ea6\u662f \\(O(n)\\) \u3002

\u51fd\u6570\u548c\u65b9\u6cd5\u8fd9\u4e24\u4e2a\u672f\u8bed\u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f

\u51fd\u6570\uff08function\uff09\u53ef\u4ee5\u88ab\u72ec\u7acb\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\u88ab\u9690\u5f0f\u4f20\u9012\u7ed9\u8c03\u7528\u5b83\u7684\u5bf9\u8c61\uff0c\u80fd\u591f\u5bf9\u7c7b\u7684\u5b9e\u4f8b\u4e2d\u5305\u542b\u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002

\u4e0b\u9762\u4ee5\u51e0\u4e2a\u5e38\u89c1\u7684\u7f16\u7a0b\u8bed\u8a00\u6765\u8bf4\u660e\u3002

  • C \u8bed\u8a00\u662f\u8fc7\u7a0b\u5f0f\u7f16\u7a0b\u8bed\u8a00\uff0c\u6ca1\u6709\u9762\u5411\u5bf9\u8c61\u7684\u6982\u5ff5\uff0c\u6240\u4ee5\u53ea\u6709\u51fd\u6570\u3002\u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u7ed3\u6784\u4f53\uff08struct\uff09\u6765\u6a21\u62df\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff0c\u4e0e\u7ed3\u6784\u4f53\u76f8\u5173\u8054\u7684\u51fd\u6570\u5c31\u76f8\u5f53\u4e8e\u5176\u4ed6\u8bed\u8a00\u4e2d\u7684\u65b9\u6cd5\u3002
  • Java \u548c C# \u662f\u9762\u5411\u5bf9\u8c61\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u4ee3\u7801\u5757\uff08\u65b9\u6cd5\uff09\u901a\u5e38\u90fd\u662f\u4f5c\u4e3a\u67d0\u4e2a\u7c7b\u7684\u4e00\u90e8\u5206\u3002\u9759\u6001\u65b9\u6cd5\u7684\u884c\u4e3a\u7c7b\u4f3c\u4e8e\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u88ab\u7ed1\u5b9a\u5728\u7c7b\u4e0a\uff0c\u4e0d\u80fd\u8bbf\u95ee\u7279\u5b9a\u7684\u5b9e\u4f8b\u53d8\u91cf\u3002
  • C++ \u548c Python \u65e2\u652f\u6301\u8fc7\u7a0b\u5f0f\u7f16\u7a0b\uff08\u51fd\u6570\uff09\uff0c\u4e5f\u652f\u6301\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08\u65b9\u6cd5\uff09\u3002

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

\u4e0d\u662f\uff0c\u8be5\u56fe\u7247\u5c55\u793a\u7684\u662f\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u5176\u53cd\u6620\u7684\u662f\u589e\u957f\u8d8b\u52bf\uff0c\u800c\u4e0d\u662f\u5360\u7528\u7a7a\u95f4\u7684\u7edd\u5bf9\u5927\u5c0f\u3002

\u5047\u8bbe\u53d6 \\(n = 8\\) \uff0c\u4f60\u53ef\u80fd\u4f1a\u53d1\u73b0\u6bcf\u6761\u66f2\u7ebf\u7684\u503c\u4e0e\u51fd\u6570\u5bf9\u5e94\u4e0d\u4e0a\u3002\u8fd9\u662f\u56e0\u4e3a\u6bcf\u6761\u66f2\u7ebf\u90fd\u5305\u542b\u4e00\u4e2a\u5e38\u6570\u9879\uff0c\u7528\u4e8e\u5c06\u53d6\u503c\u8303\u56f4\u538b\u7f29\u5230\u4e00\u4e2a\u89c6\u89c9\u8212\u9002\u7684\u8303\u56f4\u5185\u3002

\u5728\u5b9e\u9645\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u901a\u5e38\u4e0d\u77e5\u9053\u6bcf\u4e2a\u65b9\u6cd5\u7684\u201c\u5e38\u6570\u9879\u201d\u590d\u6742\u5ea6\u662f\u591a\u5c11\uff0c\u6240\u4ee5\u4e00\u822c\u65e0\u6cd5\u4ec5\u51ed\u590d\u6742\u5ea6\u6765\u9009\u62e9 \\(n = 8\\) \u4e4b\u4e0b\u7684\u6700\u4f18\u89e3\u6cd5\u3002\u4f46\u5bf9\u4e8e \\(n = 8^5\\) \u5c31\u5f88\u597d\u9009\u4e86\uff0c\u8fd9\u65f6\u589e\u957f\u8d8b\u52bf\u5df2\u7ecf\u5360\u4e3b\u5bfc\u4e86\u3002

"},{"location":"chapter_computational_complexity/time_complexity/","title":"2.3 \u00a0 \u65f6\u95f4\u590d\u6742\u5ea6","text":"

\u8fd0\u884c\u65f6\u95f4\u53ef\u4ee5\u76f4\u89c2\u4e14\u51c6\u786e\u5730\u53cd\u6620\u7b97\u6cd5\u7684\u6548\u7387\u3002\u5982\u679c\u6211\u4eec\u60f3\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 print() \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\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig
# \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\ndef algorithm(n: int):\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\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\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++\nConsole.WriteLine(0);      // 5 ns\n}\n}\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\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
// \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++\nprint(0); // 5 ns\n}\n}\n
// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfn algorithm(n: i32) {\nlet mut 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 \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\nprintln!(\"{}\", 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
\n

\u6839\u636e\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5f97\u5230\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u4e3a \\(6n + 12\\) ns \uff1a

\\[ 1 + 1 + 10 + (1 + 5) \\times n = 6n + 12 \\]

\u4f46\u5b9e\u9645\u4e0a\uff0c\u7edf\u8ba1\u7b97\u6cd5\u7684\u8fd0\u884c\u65f6\u95f4\u65e2\u4e0d\u5408\u7406\u4e5f\u4e0d\u73b0\u5b9e\u3002\u9996\u5148\uff0c\u6211\u4eec\u4e0d\u5e0c\u671b\u5c06\u9884\u4f30\u65f6\u95f4\u548c\u8fd0\u884c\u5e73\u53f0\u7ed1\u5b9a\uff0c\u56e0\u4e3a\u7b97\u6cd5\u9700\u8981\u5728\u5404\u79cd\u4e0d\u540c\u7684\u5e73\u53f0\u4e0a\u8fd0\u884c\u3002\u5176\u6b21\uff0c\u6211\u4eec\u5f88\u96be\u83b7\u77e5\u6bcf\u79cd\u64cd\u4f5c\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u8fd9\u7ed9\u9884\u4f30\u8fc7\u7a0b\u5e26\u6765\u4e86\u6781\u5927\u7684\u96be\u5ea6\u3002

"},{"location":"chapter_computational_complexity/time_complexity/#231","title":"2.3.1 \u00a0 \u7edf\u8ba1\u65f6\u95f4\u589e\u957f\u8d8b\u52bf","text":"

\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u7edf\u8ba1\u7684\u4e0d\u662f\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\uff0c\u800c\u662f\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u968f\u7740\u6570\u636e\u91cf\u53d8\u5927\u65f6\u7684\u589e\u957f\u8d8b\u52bf\u3002

\u201c\u65f6\u95f4\u589e\u957f\u8d8b\u52bf\u201d\u8fd9\u4e2a\u6982\u5ff5\u6bd4\u8f83\u62bd\u8c61\uff0c\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u4f8b\u5b50\u6765\u52a0\u4ee5\u7406\u89e3\u3002\u5047\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u7ed9\u5b9a\u4e09\u4e2a\u7b97\u6cd5\u51fd\u6570 A\u3001B \u548c C \uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig
# \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_A(n: int):\nprint(0)\n# \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\ndef algorithm_B(n: int):\nfor _ in range(n):\nprint(0)\n# \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_C(n: int):\nfor _ in range(1000000):\nprint(0)\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\ncout << 0 << endl;\n}\n// \u7b97\u6cd5 B \u7684\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 \u7684\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 \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\nSystem.out.println(0);\n}\n// \u7b97\u6cd5 B \u7684\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 \u7684\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 \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid AlgorithmA(int n) {\nConsole.WriteLine(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid AlgorithmB(int n) {\nfor (int i = 0; i < n; i++) {\nConsole.WriteLine(0);\n}\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid AlgorithmC(int n) {\nfor (int i = 0; i < 1000000; i++) {\nConsole.WriteLine(0);\n}\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithm_A(n int) {\nfmt.Println(0)\n}\n// \u7b97\u6cd5 B \u7684\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 \u7684\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 \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithmA(n: Int) {\nprint(0)\n}\n// \u7b97\u6cd5 B \u7684\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 \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithmC(n: Int) {\nfor _ in 0 ..< 1000000 {\nprint(0)\n}\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_A(n) {\nconsole.log(0);\n}\n// \u7b97\u6cd5 B \u7684\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 \u7684\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 \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_A(n: number): void {\nconsole.log(0);\n}\n// \u7b97\u6cd5 B \u7684\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 \u7684\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 \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithmA(int n) {\nprint(0);\n}\n// \u7b97\u6cd5 B \u7684\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 \u7684\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
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfn algorithm_A(n: i32) {\nprintln!(\"{}\", 0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfn algorithm_B(n: i32) {\nfor _ in 0..n {\nprintln!(\"{}\", 0);\n}\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfn algorithm_C(n: i32) {\nfor _ in 0..1000000 {\nprintln!(\"{}\", 0);\n}\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\nprintf(\"%d\", 0);\n}\n// \u7b97\u6cd5 B \u7684\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 \u7684\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
\n

\u56fe 2-7 \u5c55\u793a\u4e86\u4ee5\u4e0a\u4e09\u4e2a\u7b97\u6cd5\u51fd\u6570\u7684\u65f6\u95f4\u590d\u6742\u5ea6\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\u201c\u5e38\u6570\u9636\u201d\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\u201c\u7ebf\u6027\u9636\u201d\u3002
  • \u7b97\u6cd5 C \u4e2d\u7684\u6253\u5370\u64cd\u4f5c\u9700\u8981\u5faa\u73af \\(1000000\\) \u6b21\uff0c\u867d\u7136\u8fd0\u884c\u65f6\u95f4\u5f88\u957f\uff0c\u4f46\u5b83\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\u3002\u56e0\u6b64 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u548c A \u76f8\u540c\uff0c\u4ecd\u4e3a\u201c\u5e38\u6570\u9636\u201d\u3002

\u56fe 2-7 \u00a0 \u7b97\u6cd5 A\u3001B \u548c C \u7684\u65f6\u95f4\u589e\u957f\u8d8b\u52bf

\u76f8\u8f83\u4e8e\u76f4\u63a5\u7edf\u8ba1\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u6709\u54ea\u4e9b\u7279\u70b9\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 \u66f4\u6162\uff0c\u5728 \\(n > 1000000\\) \u65f6\u6bd4\u7b97\u6cd5 C \u66f4\u6162\u3002\u4e8b\u5b9e\u4e0a\uff0c\u53ea\u8981\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u8db3\u591f\u5927\uff0c\u590d\u6742\u5ea6\u4e3a\u201c\u5e38\u6570\u9636\u201d\u7684\u7b97\u6cd5\u4e00\u5b9a\u4f18\u4e8e\u201c\u7ebf\u6027\u9636\u201d\u7684\u7b97\u6cd5\uff0c\u8fd9\u6b63\u662f\u65f6\u95f4\u589e\u957f\u8d8b\u52bf\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\u4e00\u6765\u4f30\u7b97\u96be\u5ea6\u5c31\u5927\u5927\u964d\u4f4e\u4e86\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\u7684\u9ad8\u4f4e\u3002\u5f53\u7136\uff0c\u5c3d\u7ba1\u5b58\u5728\u4e0a\u8ff0\u95ee\u9898\uff0c\u590d\u6742\u5ea6\u5206\u6790\u4ecd\u7136\u662f\u8bc4\u5224\u7b97\u6cd5\u6548\u7387\u6700\u6709\u6548\u4e14\u5e38\u7528\u7684\u65b9\u6cd5\u3002
"},{"location":"chapter_computational_complexity/time_complexity/#232","title":"2.3.2 \u00a0 \u51fd\u6570\u6e10\u8fd1\u4e0a\u754c","text":"

\u7ed9\u5b9a\u4e00\u4e2a\u8f93\u5165\u5927\u5c0f\u4e3a \\(n\\) \u7684\u51fd\u6570\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig
def algorithm(n: int):\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
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
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\nConsole.WriteLine(0);   // +1\n}\n}\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
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
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\nprint(0); // +1\n}\n}\n
fn algorithm(n: i32) {\nlet mut a = 1;   // +1\na = a + 1;      // +1\na = a * 2;      // +1\n// \u5faa\u73af n \u6b21\nfor _ in 0..n { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\nprintln!(\"{}\", 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}  
\n

\u8bbe\u7b97\u6cd5\u7684\u64cd\u4f5c\u6570\u91cf\u662f\u4e00\u4e2a\u5173\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u7684\u51fd\u6570\uff0c\u8bb0\u4e3a \\(T(n)\\) \uff0c\u5219\u4ee5\u4e0a\u51fd\u6570\u7684\u7684\u64cd\u4f5c\u6570\u91cf\u4e3a\uff1a

\\[ T(n) = 3 + 2n \\]

\\(T(n)\\) \u662f\u4e00\u6b21\u51fd\u6570\uff0c\u8bf4\u660e\u5176\u8fd0\u884c\u65f6\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u662f\u7ebf\u6027\u7684\uff0c\u56e0\u6b64\u5b83\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f\u7ebf\u6027\u9636\u3002

\u6211\u4eec\u5c06\u7ebf\u6027\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u8bb0\u4e3a \\(O(n)\\) \uff0c\u8fd9\u4e2a\u6570\u5b66\u7b26\u53f7\u79f0\u4e3a\u300c\u5927 \\(O\\) \u8bb0\u53f7 big-\\(O\\) notation\u300d\uff0c\u8868\u793a\u51fd\u6570 \\(T(n)\\) \u7684\u300c\u6e10\u8fd1\u4e0a\u754c asymptotic upper bound\u300d\u3002

\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u672c\u8d28\u4e0a\u662f\u8ba1\u7b97\u201c\u64cd\u4f5c\u6570\u91cf\u51fd\u6570 \\(T(n)\\)\u201d\u7684\u6e10\u8fd1\u4e0a\u754c\uff0c\u5176\u5177\u6709\u660e\u786e\u7684\u6570\u5b66\u5b9a\u4e49\u3002

\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c

\u82e5\u5b58\u5728\u6b63\u5b9e\u6570 \\(c\\) \u548c\u5b9e\u6570 \\(n_0\\) \uff0c\u4f7f\u5f97\u5bf9\u4e8e\u6240\u6709\u7684 \\(n > n_0\\) \uff0c\u5747\u6709 \\(T(n) \\leq c \\cdot f(n)\\) \uff0c\u5219\u53ef\u8ba4\u4e3a \\(f(n)\\) \u7ed9\u51fa\u4e86 \\(T(n)\\) \u7684\u4e00\u4e2a\u6e10\u8fd1\u4e0a\u754c\uff0c\u8bb0\u4e3a \\(T(n) = O(f(n))\\) \u3002

\u5982\u56fe 2-8 \u6240\u793a\uff0c\u8ba1\u7b97\u6e10\u8fd1\u4e0a\u754c\u5c31\u662f\u5bfb\u627e\u4e00\u4e2a\u51fd\u6570 \\(f(n)\\) \uff0c\u4f7f\u5f97\u5f53 \\(n\\) \u8d8b\u5411\u4e8e\u65e0\u7a77\u5927\u65f6\uff0c\\(T(n)\\) \u548c \\(f(n)\\) \u5904\u4e8e\u76f8\u540c\u7684\u589e\u957f\u7ea7\u522b\uff0c\u4ec5\u76f8\u5dee\u4e00\u4e2a\u5e38\u6570\u9879 \\(c\\) \u7684\u500d\u6570\u3002

\u56fe 2-8 \u00a0 \u51fd\u6570\u7684\u6e10\u8fd1\u4e0a\u754c

"},{"location":"chapter_computational_complexity/time_complexity/#233","title":"2.3.3 \u00a0 \u63a8\u7b97\u65b9\u6cd5","text":"

\u6e10\u8fd1\u4e0a\u754c\u7684\u6570\u5b66\u5473\u513f\u6709\u70b9\u91cd\uff0c\u5982\u679c\u4f60\u611f\u89c9\u6ca1\u6709\u5b8c\u5168\u7406\u89e3\uff0c\u4e5f\u65e0\u987b\u62c5\u5fc3\u3002\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\u5c31\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":"1. \u00a0 \u7b2c\u4e00\u6b65\uff1a\u7edf\u8ba1\u64cd\u4f5c\u6570\u91cf","text":"

\u9488\u5bf9\u4ee3\u7801\uff0c\u9010\u884c\u4ece\u4e0a\u5230\u4e0b\u8ba1\u7b97\u5373\u53ef\u3002\u7136\u800c\uff0c\u7531\u4e8e\u4e0a\u8ff0 \\(c \\cdot f(n)\\) \u4e2d\u7684\u5e38\u6570\u9879 \\(c\\) \u53ef\u4ee5\u53d6\u4efb\u610f\u5927\u5c0f\uff0c\u56e0\u6b64\u64cd\u4f5c\u6570\u91cf \\(T(n)\\) \u4e2d\u7684\u5404\u79cd\u7cfb\u6570\u3001\u5e38\u6570\u9879\u90fd\u53ef\u4ee5\u88ab\u5ffd\u7565\u3002\u6839\u636e\u6b64\u539f\u5219\uff0c\u53ef\u4ee5\u603b\u7ed3\u51fa\u4ee5\u4e0b\u8ba1\u6570\u7b80\u5316\u6280\u5de7\u3002

  1. \u5ffd\u7565 \\(T(n)\\) \u4e2d\u7684\u5e38\u6570\u9879\u3002\u56e0\u4e3a\u5b83\u4eec\u90fd\u4e0e \\(n\\) \u65e0\u5173\uff0c\u6240\u4ee5\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\u7b2c 1. \u70b9\u548c\u7b2c 2. \u70b9\u7684\u6280\u5de7\u3002

\u7ed9\u5b9a\u4e00\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u4e0a\u8ff0\u6280\u5de7\u6765\u7edf\u8ba1\u64cd\u4f5c\u6570\u91cf\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig
def 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 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
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
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++) {\nConsole.WriteLine(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++) {\nConsole.WriteLine(0);\n}\n}\n}\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
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
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++) {\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
fn algorithm(n: i32) {\nlet mut 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 i in 0..(5 * n + 1) {\nprintln!(\"{}\", 0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor i in 0..(2 * n) {\nfor j in 0..(n + 1) {\nprintln!(\"{}\", 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
\n

\u4ee5\u4e0b\u516c\u5f0f\u5c55\u793a\u4e86\u4f7f\u7528\u4e0a\u8ff0\u6280\u5de7\u524d\u540e\u7684\u7edf\u8ba1\u7ed3\u679c\uff0c\u4e24\u8005\u63a8\u51fa\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n^2)\\) \u3002

\\[ \\begin{aligned} T(n) & = 2n(n + 1) + (5n + 1) + 2 & \\text{\u5b8c\u6574\u7edf\u8ba1 (-.-|||)} \\newline & = 2n^2 + 7n + 3 \\newline T(n) & = n^2 + n & \\text{\u5077\u61d2\u7edf\u8ba1 (o.O)} \\end{aligned} \\]"},{"location":"chapter_computational_complexity/time_complexity/#2","title":"2. \u00a0 \u7b2c\u4e8c\u6b65\uff1a\u5224\u65ad\u6e10\u8fd1\u4e0a\u754c","text":"

\u65f6\u95f4\u590d\u6742\u5ea6\u7531\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

\u8868 2-2 \u5c55\u793a\u4e86\u4e00\u4e9b\u4f8b\u5b50\uff0c\u5176\u4e2d\u4e00\u4e9b\u5938\u5f20\u7684\u503c\u662f\u4e3a\u4e86\u5f3a\u8c03\u201c\u7cfb\u6570\u65e0\u6cd5\u64bc\u52a8\u9636\u6570\u201d\u8fd9\u4e00\u7ed3\u8bba\u3002\u5f53 \\(n\\) \u8d8b\u4e8e\u65e0\u7a77\u5927\u65f6\uff0c\u8fd9\u4e9b\u5e38\u6570\u53d8\u5f97\u65e0\u8db3\u8f7b\u91cd\u3002

\u8868 2-2 \u00a0 \u4e0d\u540c\u64cd\u4f5c\u6570\u91cf\u5bf9\u5e94\u7684\u65f6\u95f4\u590d\u6742\u5ea6

\u64cd\u4f5c\u6570\u91cf \\(T(n)\\) \u65f6\u95f4\u590d\u6742\u5ea6 \\(O(f(n))\\) \\(100000\\) \\(O(1)\\) \\(3n + 2\\) \\(O(n)\\) \\(2n^2 + 3n + 2\\) \\(O(n^2)\\) \\(n^3 + 10000n^2\\) \\(O(n^3)\\) \\(2^n + 10000n^{10000}\\) \\(O(2^n)\\)"},{"location":"chapter_computational_complexity/time_complexity/#234","title":"2.3.4 \u00a0 \u5e38\u89c1\u7c7b\u578b","text":"

\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u5e38\u89c1\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7c7b\u578b\u5982\u56fe 2-9 \u6240\u793a\uff08\u6309\u7167\u4ece\u4f4e\u5230\u9ad8\u7684\u987a\u5e8f\u6392\u5217\uff09\u3002

\\[ \\begin{aligned} O(1) < O(\\log n) < O(n) < O(n \\log n) < O(n^2) < O(2^n) < O(n!) \\newline \\text{\u5e38\u6570\u9636} < \\text{\u5bf9\u6570\u9636} < \\text{\u7ebf\u6027\u9636} < \\text{\u7ebf\u6027\u5bf9\u6570\u9636} < \\text{\u5e73\u65b9\u9636} < \\text{\u6307\u6570\u9636} < \\text{\u9636\u4e58\u9636} \\end{aligned} \\]

\u56fe 2-9 \u00a0 \u5e38\u89c1\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7c7b\u578b

"},{"location":"chapter_computational_complexity/time_complexity/#1-o1","title":"1. \u00a0 \u5e38\u6570\u9636 \\(O(1)\\)","text":"

\u5e38\u6570\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\uff0c\u5373\u4e0d\u968f\u7740 \\(n\\) \u7684\u53d8\u5316\u800c\u53d8\u5316\u3002

\u5728\u4ee5\u4e0b\u51fd\u6570\u4e2d\uff0c\u5c3d\u7ba1\u64cd\u4f5c\u6570\u91cf size \u53ef\u80fd\u5f88\u5927\uff0c\u4f46\u7531\u4e8e\u5176\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
time_complexity.rs
/* \u5e38\u6570\u9636 */\nfn constant(n: i32) -> i32 {\n_ = n;\nlet mut count = 0;\nlet size = 100_000;\nfor _ in 0..size {\ncount += 1;\n}\ncount\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.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
"},{"location":"chapter_computational_complexity/time_complexity/#2-on","title":"2. \u00a0 \u7ebf\u6027\u9636 \\(O(n)\\)","text":"

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

PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.py
def linear(n: int) -> int:\n\"\"\"\u7ebf\u6027\u9636\"\"\"\ncount = 0\nfor _ in range(n):\ncount += 1\nreturn count\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.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.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.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.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.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.dart
/* \u7ebf\u6027\u9636 */\nint linear(int n) {\nint count = 0;\nfor (var i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n
time_complexity.rs
/* \u7ebf\u6027\u9636 */\nfn linear(n: i32) -> i32 {\nlet mut count = 0;\nfor _ in 0..n {\ncount += 1;\n}\ncount\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.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

\u904d\u5386\u6570\u7ec4\u548c\u904d\u5386\u94fe\u8868\u7b49\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u6570\u7ec4\u6216\u94fe\u8868\u7684\u957f\u5ea6\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.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.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.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.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.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.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.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.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
time_complexity.rs
/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfn array_traversal(nums: &[i32]) -> i32 {\nlet mut count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor _ in nums {\ncount += 1;\n}\ncount\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.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

\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u9700\u6839\u636e\u8f93\u5165\u6570\u636e\u7684\u7c7b\u578b\u6765\u5177\u4f53\u786e\u5b9a\u3002\u6bd4\u5982\u5728\u7b2c\u4e00\u4e2a\u793a\u4f8b\u4e2d\uff0c\u53d8\u91cf \\(n\\) \u4e3a\u8f93\u5165\u6570\u636e\u5927\u5c0f\uff1b\u5728\u7b2c\u4e8c\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6570\u7ec4\u957f\u5ea6 \\(n\\) \u4e3a\u6570\u636e\u5927\u5c0f\u3002

"},{"location":"chapter_computational_complexity/time_complexity/#3-on2","title":"3. \u00a0 \u5e73\u65b9\u9636 \\(O(n^2)\\)","text":"

\u5e73\u65b9\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u76f8\u5bf9\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u4ee5\u5e73\u65b9\u7ea7\u522b\u589e\u957f\u3002\u5e73\u65b9\u9636\u901a\u5e38\u51fa\u73b0\u5728\u5d4c\u5957\u5faa\u73af\u4e2d\uff0c\u5916\u5c42\u5faa\u73af\u548c\u5185\u5c42\u5faa\u73af\u90fd\u4e3a \\(O(n)\\) \uff0c\u56e0\u6b64\u603b\u4f53\u4e3a \\(O(n^2)\\) \uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
time_complexity.rs
/* \u5e73\u65b9\u9636 */\nfn quadratic(n: i32) -> i32 {\nlet mut 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}\ncount\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.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

\u56fe 2-10 \u5bf9\u6bd4\u4e86\u5e38\u6570\u9636\u3001\u7ebf\u6027\u9636\u548c\u5e73\u65b9\u9636\u4e09\u79cd\u65f6\u95f4\u590d\u6742\u5ea6\u3002

\u56fe 2-10 \u00a0 \u5e38\u6570\u9636\u3001\u7ebf\u6027\u9636\u548c\u5e73\u65b9\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

\u4ee5\u5192\u6ce1\u6392\u5e8f\u4e3a\u4f8b\uff0c\u5916\u5c42\u5faa\u73af\u6267\u884c \\(n - 1\\) \u6b21\uff0c\u5185\u5c42\u5faa\u73af\u6267\u884c \\(n-1\\)\u3001\\(n-2\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \u6b21\uff0c\u5e73\u5747\u4e3a \\(n / 2\\) \u6b21\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O((n - 1) n / 2) = O(n^2)\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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]\n(nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\ncount += 3;  // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\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.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.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.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
time_complexity.rs
/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfn bubble_sort(nums: &mut [i32]) -> i32 {\nlet mut count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor i in (1..nums.len()).rev() {\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \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}\ncount\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.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 = @as(i32, @intCast(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
"},{"location":"chapter_computational_complexity/time_complexity/#4-o2n","title":"4. \u00a0 \u6307\u6570\u9636 \\(O(2^n)\\)","text":"

\u751f\u7269\u5b66\u7684\u201c\u7ec6\u80de\u5206\u88c2\u201d\u662f\u6307\u6570\u9636\u589e\u957f\u7684\u5178\u578b\u4f8b\u5b50\uff1a\u521d\u59cb\u72b6\u6001\u4e3a \\(1\\) \u4e2a\u7ec6\u80de\uff0c\u5206\u88c2\u4e00\u8f6e\u540e\u53d8\u4e3a \\(2\\) \u4e2a\uff0c\u5206\u88c2\u4e24\u8f6e\u540e\u53d8\u4e3a \\(4\\) \u4e2a\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u5206\u88c2 \\(n\\) \u8f6e\u540e\u6709 \\(2^n\\) \u4e2a\u7ec6\u80de\u3002

\u56fe 2-11 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6a21\u62df\u4e86\u7ec6\u80de\u5206\u88c2\u7684\u8fc7\u7a0b\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.py
def exponential(n: int) -> int:\n\"\"\"\u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\"\"\"\ncount = 0\nbase = 1\n# \u7ec6\u80de\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.cpp
/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\nint count = 0, base = 1;\n// \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\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.java
/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\nint count = 0, base = 1;\n// \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\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.cs
/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint Exponential(int n) {\nint count = 0, bas = 1;\n// \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\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.go
/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09*/\nfunc exponential(n int) int {\ncount, base := 0, 1\n// \u7ec6\u80de\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.swift
/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunc exponential(n: Int) -> Int {\nvar count = 0\nvar base = 1\n// \u7ec6\u80de\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.js
/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction exponential(n) {\nlet count = 0,\nbase = 1;\n// \u7ec6\u80de\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// \u7ec6\u80de\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.dart
/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\nint count = 0, base = 1;\n// \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\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
time_complexity.rs
/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfn exponential(n: i32) -> i32 {\nlet mut count = 0;\nlet mut base = 1;\n// \u7ec6\u80de\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\ncount\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// \u7ec6\u80de\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.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// \u7ec6\u80de\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

\u56fe 2-11 \u00a0 \u6307\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

\u5728\u5b9e\u9645\u7b97\u6cd5\u4e2d\uff0c\u6307\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u9012\u5f52\u51fd\u6570\u4e2d\u3002\u4f8b\u5982\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u5176\u9012\u5f52\u5730\u4e00\u5206\u4e3a\u4e8c\uff0c\u7ecf\u8fc7 \\(n\\) \u6b21\u5206\u88c2\u540e\u505c\u6b62\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.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.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.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.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.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.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.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.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
time_complexity.rs
/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn exp_recur(n: i32) -> i32 {\nif n == 1 {\nreturn 1;\n}\nexp_recur(n - 1) + exp_recur(n - 1) + 1\n}\n
time_complexity.c
/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\nif (n == 1)\nreturn 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.zig
// \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn expRecur(n: i32) i32 {\nif (n == 1) return 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n

\u6307\u6570\u9636\u589e\u957f\u975e\u5e38\u8fc5\u901f\uff0c\u5728\u7a77\u4e3e\u6cd5\uff08\u66b4\u529b\u641c\u7d22\u3001\u56de\u6eaf\u7b49\uff09\u4e2d\u6bd4\u8f83\u5e38\u89c1\u3002\u5bf9\u4e8e\u6570\u636e\u89c4\u6a21\u8f83\u5927\u7684\u95ee\u9898\uff0c\u6307\u6570\u9636\u662f\u4e0d\u53ef\u63a5\u53d7\u7684\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528\u52a8\u6001\u89c4\u5212\u6216\u8d2a\u5fc3\u7b49\u7b97\u6cd5\u6765\u89e3\u51b3\u3002

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

\u4e0e\u6307\u6570\u9636\u76f8\u53cd\uff0c\u5bf9\u6570\u9636\u53cd\u6620\u4e86\u201c\u6bcf\u8f6e\u7f29\u51cf\u5230\u4e00\u534a\u201d\u7684\u60c5\u51b5\u3002\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u7531\u4e8e\u6bcf\u8f6e\u7f29\u51cf\u5230\u4e00\u534a\uff0c\u56e0\u6b64\u5faa\u73af\u6b21\u6570\u662f \\(\\log_2 n\\) \uff0c\u5373 \\(2^n\\) \u7684\u53cd\u51fd\u6570\u3002

\u56fe 2-12 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6a21\u62df\u4e86\u201c\u6bcf\u8f6e\u7f29\u51cf\u5230\u4e00\u534a\u201d\u7684\u8fc7\u7a0b\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log_2 n)\\) \uff0c\u7b80\u8bb0\u4e3a \\(O(\\log n)\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
time_complexity.rs
/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfn logarithmic(mut n: f32) -> i32 {\nlet mut count = 0;\nwhile n > 1.0 {\nn = n / 2.0;\ncount += 1;\n}\ncount\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.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

\u56fe 2-12 \u00a0 \u5bf9\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

\u4e0e\u6307\u6570\u9636\u7c7b\u4f3c\uff0c\u5bf9\u6570\u9636\u4e5f\u5e38\u51fa\u73b0\u4e8e\u9012\u5f52\u51fd\u6570\u4e2d\u3002\u4ee5\u4e0b\u4ee3\u7801\u5f62\u6210\u4e86\u4e00\u4e2a\u9ad8\u5ea6\u4e3a \\(\\log_2 n\\) \u7684\u9012\u5f52\u6811\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
time_complexity.rs
/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn log_recur(n: f32) -> i32 {\nif n <= 1.0 {\nreturn 0;\n}\nlog_recur(n / 2.0) + 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.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

\u5bf9\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u7b97\u6cd5\u4e2d\uff0c\u4f53\u73b0\u4e86\u201c\u4e00\u5206\u4e3a\u591a\u201d\u548c\u201c\u5316\u7e41\u4e3a\u7b80\u201d\u7684\u7b97\u6cd5\u601d\u60f3\u3002\u5b83\u589e\u957f\u7f13\u6162\uff0c\u662f\u4ec5\u6b21\u4e8e\u5e38\u6570\u9636\u7684\u7406\u60f3\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002

\\(O(\\log n)\\) \u7684\u5e95\u6570\u662f\u591a\u5c11\uff1f

\u51c6\u786e\u6765\u8bf4\uff0c\u201c\u4e00\u5206\u4e3a \\(m\\)\u201d\u5bf9\u5e94\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(\\log_m n)\\) \u3002\u800c\u901a\u8fc7\u5bf9\u6570\u6362\u5e95\u516c\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u5177\u6709\u4e0d\u540c\u5e95\u6570\u7684\u3001\u76f8\u7b49\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a

\\[ O(\\log_m n) = O(\\log_k n / \\log_k m) = O(\\log_k n) \\]

\u4e5f\u5c31\u662f\u8bf4\uff0c\u5e95\u6570 \\(m\\) \u53ef\u4ee5\u5728\u4e0d\u5f71\u54cd\u590d\u6742\u5ea6\u7684\u524d\u63d0\u4e0b\u8f6c\u6362\u3002\u56e0\u6b64\u6211\u4eec\u901a\u5e38\u4f1a\u7701\u7565\u5e95\u6570 \\(m\\) \uff0c\u5c06\u5bf9\u6570\u9636\u76f4\u63a5\u8bb0\u4e3a \\(O(\\log n)\\) \u3002

"},{"location":"chapter_computational_complexity/time_complexity/#6-on-log-n","title":"6. \u00a0 \u7ebf\u6027\u5bf9\u6570\u9636 \\(O(n \\log n)\\)","text":"

\u7ebf\u6027\u5bf9\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u5d4c\u5957\u5faa\u73af\u4e2d\uff0c\u4e24\u5c42\u5faa\u73af\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u522b\u4e3a \\(O(\\log n)\\) \u548c \\(O(n)\\) \u3002\u76f8\u5173\u4ee3\u7801\u5982\u4e0b\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.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.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.java
/* \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) + LinearLogRecur(n / 2);\nfor (int i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n
time_complexity.go
/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunc linearLogRecur(n float64) int {\nif n <= 1 {\nreturn 1\n}\ncount := linearLogRecur(n/2) + linearLogRecur(n/2)\nfor i := 0.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.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.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
time_complexity.rs
/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfn linear_log_recur(n: f32) -> i32 {\nif n <= 1.0 {\nreturn 1;\n}\nlet mut count = linear_log_recur(n / 2.0) + linear_log_recur(n / 2.0);\nfor _ in 0 ..n as i32 {\ncount += 1;\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.zig
// \u7ebf\u6027\u5bf9\u6570\u9636\nfn linearLogRecur(n: f32) i32 {\nif (n <= 1) return 1;\nvar count: i32 = linearLogRecur(n / 2) + linearLogRecur(n / 2);\nvar i: f32 = 0;\nwhile (i < n) : (i += 1) {\ncount += 1;\n}\nreturn count;\n}\n

\u56fe 2-13 \u5c55\u793a\u4e86\u7ebf\u6027\u5bf9\u6570\u9636\u7684\u751f\u6210\u65b9\u5f0f\u3002\u4e8c\u53c9\u6811\u7684\u6bcf\u4e00\u5c42\u7684\u64cd\u4f5c\u603b\u6570\u90fd\u4e3a \\(n\\) \uff0c\u6811\u5171\u6709 \\(\\log_2 n + 1\\) \u5c42\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002

\u56fe 2-13 \u00a0 \u7ebf\u6027\u5bf9\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

\u4e3b\u6d41\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4e3a \\(O(n \\log n)\\) \uff0c\u4f8b\u5982\u5feb\u901f\u6392\u5e8f\u3001\u5f52\u5e76\u6392\u5e8f\u3001\u5806\u6392\u5e8f\u7b49\u3002

"},{"location":"chapter_computational_complexity/time_complexity/#7-on","title":"7. \u00a0 \u9636\u4e58\u9636 \\(O(n!)\\)","text":"

\u9636\u4e58\u9636\u5bf9\u5e94\u6570\u5b66\u4e0a\u7684\u201c\u5168\u6392\u5217\u201d\u95ee\u9898\u3002\u7ed9\u5b9a \\(n\\) \u4e2a\u4e92\u4e0d\u91cd\u590d\u7684\u5143\u7d20\uff0c\u6c42\u5176\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u65b9\u6848\uff0c\u65b9\u6848\u6570\u91cf\u4e3a\uff1a

\\[ n! = n \\times (n - 1) \\times (n - 2) \\times \\dots \\times 2 \\times 1 \\]

\u9636\u4e58\u901a\u5e38\u4f7f\u7528\u9012\u5f52\u5b9e\u73b0\u3002\u5982\u56fe 2-14 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u7b2c\u4e00\u5c42\u5206\u88c2\u51fa \\(n\\) \u4e2a\uff0c\u7b2c\u4e8c\u5c42\u5206\u88c2\u51fa \\(n - 1\\) \u4e2a\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u7b2c \\(n\\) \u5c42\u65f6\u505c\u6b62\u5206\u88c2\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.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.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.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.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.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.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.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.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
time_complexity.rs
/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn factorial_recur(n: i32) -> i32 {\nif n == 0 {\nreturn 1;\n}\nlet mut count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor _ in 0..n {\ncount += factorial_recur(n - 1);\n}\ncount\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.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

\u56fe 2-14 \u00a0 \u9636\u4e58\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6

\u8bf7\u6ce8\u610f\uff0c\u56e0\u4e3a\u5f53 \\(n \\geq 4\\) \u65f6\u6052\u6709 \\(n! > 2^n\\) \uff0c\u6240\u4ee5\u9636\u4e58\u9636\u6bd4\u6307\u6570\u9636\u589e\u957f\u5f97\u66f4\u5feb\uff0c\u5728 \\(n\\) \u8f83\u5927\u65f6\u4e5f\u662f\u4e0d\u53ef\u63a5\u53d7\u7684\u3002

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

\u7b97\u6cd5\u7684\u65f6\u95f4\u6548\u7387\u5f80\u5f80\u4e0d\u662f\u56fa\u5b9a\u7684\uff0c\u800c\u662f\u4e0e\u8f93\u5165\u6570\u636e\u7684\u5206\u5e03\u6709\u5173\u3002\u5047\u8bbe\u8f93\u5165\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4 nums \uff0c\u5176\u4e2d nums \u7531\u4ece \\(1\\) \u81f3 \\(n\\) \u7684\u6570\u5b57\u7ec4\u6210\uff0c\u6bcf\u4e2a\u6570\u5b57\u53ea\u51fa\u73b0\u4e00\u6b21\uff1b\u4f46\u5143\u7d20\u987a\u5e8f\u662f\u968f\u673a\u6253\u4e71\u7684\uff0c\u4efb\u52a1\u76ee\u6807\u662f\u8fd4\u56de\u5143\u7d20 \\(1\\) \u7684\u7d22\u5f15\u3002\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\u3002

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

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

PythonC++JavaC#GoSwiftJSTSDartRustCZig worst_best_time_complexity.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.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.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.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++) {\nint index = new Random().Next(i, nums.Length);\n(nums[i], nums[index]) = (nums[index], nums[i]);\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.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.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.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.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
worst_best_time_complexity.rs
/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfn random_numbers(n: i32) -> Vec<i32> {\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nlet mut nums = (1..=n).collect::<Vec<i32>>();\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nnums.shuffle(&mut thread_rng());\nnums\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfn find_one(nums: &[i32]) -> Option<usize> {\nfor i in 0..nums.len() {\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nif nums[i] == 1 {\nreturn Some(i);\n}\n}\nNone\n}\n
worst_best_time_complexity.c
/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint *randomNumbers(int n) {\n// \u5206\u914d\u5806\u533a\u5185\u5b58\uff08\u521b\u5efa\u4e00\u7ef4\u53ef\u53d8\u957f\u6570\u7ec4\uff1a\u6570\u7ec4\u4e2d\u5143\u7d20\u6570\u91cf\u4e3a n \uff0c\u5143\u7d20\u7c7b\u578b\u4e3a int \uff09\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.zig
// \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71\nfn randomNumbers(comptime n: usize) [n]i32 {\nvar nums: [n]i32 = undefined;\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (&nums, 0..) |*num, i| {\nnum.* = @as(i32, @intCast(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\nfn findOne(nums: []i32) i32 {\nfor (nums, 0..) |num, i| {\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nif (num == 1) return @intCast(i);\n}\nreturn -1;\n}\n

\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6211\u4eec\u5728\u5b9e\u9645\u4e2d\u5f88\u5c11\u4f7f\u7528\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u56e0\u4e3a\u901a\u5e38\u53ea\u6709\u5728\u5f88\u5c0f\u6982\u7387\u4e0b\u624d\u80fd\u8fbe\u5230\uff0c\u53ef\u80fd\u4f1a\u5e26\u6765\u4e00\u5b9a\u7684\u8bef\u5bfc\u6027\u3002\u800c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u66f4\u4e3a\u5b9e\u7528\uff0c\u56e0\u4e3a\u5b83\u7ed9\u51fa\u4e86\u4e00\u4e2a\u6548\u7387\u5b89\u5168\u503c\uff0c\u8ba9\u6211\u4eec\u53ef\u4ee5\u653e\u5fc3\u5730\u4f7f\u7528\u7b97\u6cd5\u3002

\u4ece\u4e0a\u8ff0\u793a\u4f8b\u53ef\u4ee5\u770b\u51fa\uff0c\u6700\u5dee\u6216\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u53ea\u51fa\u73b0\u4e8e\u201c\u7279\u6b8a\u7684\u6570\u636e\u5206\u5e03\u201d\uff0c\u8fd9\u4e9b\u60c5\u51b5\u7684\u51fa\u73b0\u6982\u7387\u53ef\u80fd\u5f88\u5c0f\uff0c\u5e76\u4e0d\u80fd\u771f\u5b9e\u5730\u53cd\u6620\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u4f53\u73b0\u7b97\u6cd5\u5728\u968f\u673a\u8f93\u5165\u6570\u636e\u4e0b\u7684\u8fd0\u884c\u6548\u7387\uff0c\u7528 \\(\\Theta\\) \u8bb0\u53f7\u6765\u8868\u793a\u3002

\u5bf9\u4e8e\u90e8\u5206\u7b97\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u5730\u63a8\u7b97\u51fa\u968f\u673a\u6570\u636e\u5206\u5e03\u4e0b\u7684\u5e73\u5747\u60c5\u51b5\u3002\u6bd4\u5982\u4e0a\u8ff0\u793a\u4f8b\uff0c\u7531\u4e8e\u8f93\u5165\u6570\u7ec4\u662f\u88ab\u6253\u4e71\u7684\uff0c\u56e0\u6b64\u5143\u7d20 \\(1\\) \u51fa\u73b0\u5728\u4efb\u610f\u7d22\u5f15\u7684\u6982\u7387\u90fd\u662f\u76f8\u7b49\u7684\uff0c\u90a3\u4e48\u7b97\u6cd5\u7684\u5e73\u5747\u5faa\u73af\u6b21\u6570\u5c31\u662f\u6570\u7ec4\u957f\u5ea6\u7684\u4e00\u534a \\(n / 2\\) \uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(\\Theta(n / 2) = \\Theta(n)\\) \u3002

\u4f46\u5bf9\u4e8e\u8f83\u4e3a\u590d\u6742\u7684\u7b97\u6cd5\uff0c\u8ba1\u7b97\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u5f80\u5f80\u662f\u6bd4\u8f83\u56f0\u96be\u7684\uff0c\u56e0\u4e3a\u5f88\u96be\u5206\u6790\u51fa\u5728\u6570\u636e\u5206\u5e03\u4e0b\u7684\u6574\u4f53\u6570\u5b66\u671f\u671b\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4f5c\u4e3a\u7b97\u6cd5\u6548\u7387\u7684\u8bc4\u5224\u6807\u51c6\u3002

\u4e3a\u4ec0\u4e48\u5f88\u5c11\u770b\u5230 \\(\\Theta\\) \u7b26\u53f7\uff1f

\u53ef\u80fd\u7531\u4e8e \\(O\\) \u7b26\u53f7\u8fc7\u4e8e\u6717\u6717\u4e0a\u53e3\uff0c\u6211\u4eec\u5e38\u5e38\u4f7f\u7528\u5b83\u6765\u8868\u793a\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u3002\u4f46\u4ece\u4e25\u683c\u610f\u4e49\u4e0a\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":"\u7b2c 3 \u7ae0 \u00a0 \u6570\u636e\u7ed3\u6784","text":"

Abstract

\u6570\u636e\u7ed3\u6784\u5982\u540c\u4e00\u526f\u7a33\u56fa\u800c\u591a\u6837\u7684\u6846\u67b6\u3002

\u5b83\u4e3a\u6570\u636e\u7684\u6709\u5e8f\u7ec4\u7ec7\u63d0\u4f9b\u4e86\u84dd\u56fe\uff0c\u4f7f\u7b97\u6cd5\u5f97\u4ee5\u5728\u6b64\u57fa\u7840\u4e0a\u751f\u52a8\u8d77\u6765\u3002

"},{"location":"chapter_data_structure/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 3.1 \u00a0 \u6570\u636e\u7ed3\u6784\u5206\u7c7b
  • 3.2 \u00a0 \u57fa\u672c\u6570\u636e\u7c7b\u578b
  • 3.3 \u00a0 \u6570\u5b57\u7f16\u7801 *
  • 3.4 \u00a0 \u5b57\u7b26\u7f16\u7801 *
  • 3.5 \u00a0 \u5c0f\u7ed3
"},{"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\uff0c\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\u7c7b\u578b\u3002

  • \u6574\u6570\u7c7b\u578b byte\u3001short\u3001int\u3001long \u3002
  • \u6d6e\u70b9\u6570\u7c7b\u578b float\u3001double \uff0c\u7528\u4e8e\u8868\u793a\u5c0f\u6570\u3002
  • \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\u3002
  • \u5e03\u5c14\u7c7b\u578b bool \uff0c\u7528\u4e8e\u8868\u793a\u201c\u662f\u201d\u4e0e\u201c\u5426\u201d\u5224\u65ad\u3002

\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4ee5\u4e8c\u8fdb\u5236\u7684\u5f62\u5f0f\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u4e2d\u3002\u4e00\u4e2a\u4e8c\u8fdb\u5236\u4f4d\u5373\u4e3a \\(1\\) \u6bd4\u7279\u3002\u5728\u7edd\u5927\u591a\u6570\u73b0\u4ee3\u7cfb\u7edf\u4e2d\uff0c\\(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\u3002\u4e0b\u9762\u4ee5 Java \u4e3a\u4f8b\u3002

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

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

\u8868 3-1 \u00a0 \u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u5360\u7528\u7a7a\u95f4\u548c\u53d6\u503c\u8303\u56f4

\u7c7b\u578b \u7b26\u53f7 \u5360\u7528\u7a7a\u95f4 \u6700\u5c0f\u503c \u6700\u5927\u503c \u9ed8\u8ba4\u503c \u6574\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}\\)

\u5bf9\u4e8e\u8868 3-1 \uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\u3002

  • C \u548c C++ \u672a\u660e\u786e\u89c4\u5b9a\u57fa\u672c\u6570\u636e\u7c7b\u578b\u5927\u5c0f\uff0c\u800c\u56e0\u5b9e\u73b0\u548c\u5e73\u53f0\u5404\u5f02\u3002\u8868 3-1 \u9075\u5faa LP64 \u6570\u636e\u6a21\u578b\uff0c\u5176\u7528\u4e8e\u5305\u62ec Linux \u548c macOS \u5728\u5185\u7684 Unix 64 \u4f4d\u64cd\u4f5c\u7cfb\u7edf\u3002
  • \u5b57\u7b26 char \u7684\u5927\u5c0f\u5728 C \u548c C++ \u4e2d\u4e3a 1 \u5b57\u8282\uff0c\u5728\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4e2d\u53d6\u51b3\u4e8e\u7279\u5b9a\u7684\u5b57\u7b26\u7f16\u7801\u65b9\u6cd5\uff0c\u8be6\u89c1\u201c\u5b57\u7b26\u7f16\u7801\u201d\u7ae0\u8282\u3002
  • \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\u8fd9\u662f\u56e0\u4e3a\u73b0\u4ee3\u8ba1\u7b97\u673a CPU \u901a\u5e38\u5c06 1 \u5b57\u8282\u4f5c\u4e3a\u6700\u5c0f\u5bfb\u5740\u5185\u5b58\u5355\u5143\u3002

\u90a3\u4e48\uff0c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e0e\u6570\u636e\u7ed3\u6784\u4e4b\u95f4\u6709\u4ec0\u4e48\u8054\u7cfb\u5462\uff1f\u6211\u4eec\u77e5\u9053\uff0c\u6570\u636e\u7ed3\u6784\u662f\u5728\u8ba1\u7b97\u673a\u4e2d\u7ec4\u7ec7\u4e0e\u5b58\u50a8\u6570\u636e\u7684\u65b9\u5f0f\u3002\u5b83\u7684\u4e3b\u8bed\u662f\u201c\u7ed3\u6784\u201d\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 \u6216\u662f\u5b57\u7b26 char \uff0c\u5219\u4e0e\u201c\u6570\u636e\u7ed3\u6784\u201d\u65e0\u5173\u3002

\u6362\u53e5\u8bdd\u8bf4\uff0c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u63d0\u4f9b\u4e86\u6570\u636e\u7684\u201c\u5185\u5bb9\u7c7b\u578b\u201d\uff0c\u800c\u6570\u636e\u7ed3\u6784\u63d0\u4f9b\u4e86\u6570\u636e\u7684\u201c\u7ec4\u7ec7\u65b9\u5f0f\u201d\u3002\u4f8b\u5982\u4ee5\u4e0b\u4ee3\u7801\uff0c\u6211\u4eec\u7528\u76f8\u540c\u7684\u6570\u636e\u7ed3\u6784\uff08\u6570\u7ec4\uff09\u6765\u5b58\u50a8\u4e0e\u8868\u793a\u4e0d\u540c\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec int\u3001float\u3001char\u3001bool \u7b49\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig
# \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nnumbers: list[int] = [0] * 5\ndecimals: list[float] = [0.0] * 5\n# Python \u7684\u5b57\u7b26\u5e94\u88ab\u770b\u4f5c\u957f\u5ea6\u4e3a\u4e00\u7684\u5b57\u7b26\u4e32\ncharacters: list[str] = ['0'] * 5\nbools: list[bool] = [False] * 5\n# Python \u7684\u5217\u8868\u53ef\u4ee5\u81ea\u7531\u5b58\u50a8\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5bf9\u8c61\u5f15\u7528\ndata = [0, 0.0, 'a', False, ListNode(0)]\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint numbers[5];\nfloat decimals[5];\nchar characters[5];\nbool bools[5];\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nboolean[] bools = new boolean[5];\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nbool[] bools = new bool[5];\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nvar numbers = [5]int{}\nvar decimals = [5]float64{}\nvar characters = [5]byte{}\nvar bools = [5]bool{}\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nlet numbers = Array(repeating: Int(), count: 5)\nlet decimals = Array(repeating: Double(), count: 5)\nlet characters = Array(repeating: Character(\"a\"), count: 5)\nlet bools = Array(repeating: Bool(), count: 5)\n
// JavaScript \u7684\u6570\u7ec4\u53ef\u4ee5\u81ea\u7531\u5b58\u50a8\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5bf9\u8c61\nconst array = [0, 0.0, 'a', false];\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nconst numbers: number[] = [];\nconst characters: string[] = [];\nconst bools: boolean[] = [];\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nList<int> numbers = List.filled(5, 0);\nList<double> decimals = List.filled(5, 0.0);\nList<String> characters = List.filled(5, 'a');\nList<bool> bools = List.filled(5, false);\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nlet numbers: Vec<i32> = vec![0; 5];\nlet decimals: Vec<float> = vec![0.0, 5];\nlet characters: Vec<char> = vec!['0'; 5];\nlet bools: Vec<bool> = vec![false; 5];\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint numbers[10];\nfloat decimals[10];\nchar characters[10];\nbool bools[10];\n
\n
"},{"location":"chapter_data_structure/character_encoding/","title":"3.4 \u00a0 \u5b57\u7b26\u7f16\u7801 *","text":"

\u5728\u8ba1\u7b97\u673a\u4e2d\uff0c\u6240\u6709\u6570\u636e\u90fd\u662f\u4ee5\u4e8c\u8fdb\u5236\u6570\u7684\u5f62\u5f0f\u5b58\u50a8\u7684\uff0c\u5b57\u7b26 char \u4e5f\u4e0d\u4f8b\u5916\u3002\u4e3a\u4e86\u8868\u793a\u5b57\u7b26\uff0c\u6211\u4eec\u9700\u8981\u5efa\u7acb\u4e00\u5957\u201c\u5b57\u7b26\u96c6\u201d\uff0c\u89c4\u5b9a\u6bcf\u4e2a\u5b57\u7b26\u548c\u4e8c\u8fdb\u5236\u6570\u4e4b\u95f4\u7684\u4e00\u4e00\u5bf9\u5e94\u5173\u7cfb\u3002\u6709\u4e86\u5b57\u7b26\u96c6\u4e4b\u540e\uff0c\u8ba1\u7b97\u673a\u5c31\u53ef\u4ee5\u901a\u8fc7\u67e5\u8868\u5b8c\u6210\u4e8c\u8fdb\u5236\u6570\u5230\u5b57\u7b26\u7684\u8f6c\u6362\u3002

"},{"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\u5982\u56fe 3-6 \u6240\u793a\uff0cASCII \u7801\u5305\u62ec\u82f1\u6587\u5b57\u6bcd\u7684\u5927\u5c0f\u5199\u3001\u6570\u5b57 0 ~ 9\u3001\u4e00\u4e9b\u6807\u70b9\u7b26\u53f7\uff0c\u4ee5\u53ca\u4e00\u4e9b\u63a7\u5236\u5b57\u7b26\uff08\u5982\u6362\u884c\u7b26\u548c\u5236\u8868\u7b26\uff09\u3002

\u56fe 3-6 \u00a0 ASCII \u7801

\u7136\u800c\uff0cASCII \u7801\u4ec5\u80fd\u591f\u8868\u793a\u82f1\u6587\u3002\u968f\u7740\u8ba1\u7b97\u673a\u7684\u5168\u7403\u5316\uff0c\u8bde\u751f\u4e86\u4e00\u79cd\u80fd\u591f\u8868\u793a\u66f4\u591a\u8bed\u8a00\u7684\u5b57\u7b26\u96c6\u300cEASCII\u300d\u3002\u5b83\u5728 ASCII \u7684 7 \u4f4d\u57fa\u7840\u4e0a\u6269\u5c55\u5230 8 \u4f4d\uff0c\u80fd\u591f\u8868\u793a 256 \u4e2a\u4e0d\u540c\u7684\u5b57\u7b26\u3002

\u5728\u4e16\u754c\u8303\u56f4\u5185\uff0c\u9646\u7eed\u51fa\u73b0\u4e86\u4e00\u6279\u9002\u7528\u4e8e\u4e0d\u540c\u5730\u533a\u7684 EASCII \u5b57\u7b26\u96c6\u3002\u8fd9\u4e9b\u5b57\u7b26\u96c6\u7684\u524d 128 \u4e2a\u5b57\u7b26\u7edf\u4e00\u4e3a ASCII \u7801\uff0c\u540e 128 \u4e2a\u5b57\u7b26\u5b9a\u4e49\u4e0d\u540c\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u8bed\u8a00\u7684\u9700\u6c42\u3002

"},{"location":"chapter_data_structure/character_encoding/#342-gbk","title":"3.4.2 \u00a0 GBK \u5b57\u7b26\u96c6","text":"

\u540e\u6765\u4eba\u4eec\u53d1\u73b0\uff0cEASCII \u7801\u4ecd\u7136\u65e0\u6cd5\u6ee1\u8db3\u8bb8\u591a\u8bed\u8a00\u7684\u5b57\u7b26\u6570\u91cf\u8981\u6c42\u3002\u6bd4\u5982\u6c49\u5b57\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\u300cGBK\u300d\u5b57\u7b26\u96c6\u662f\u5728 GB2312 \u7684\u57fa\u7840\u4e0a\u6269\u5c55\u5f97\u5230\u7684\uff0c\u5b83\u5171\u6536\u5f55\u4e86 21886 \u4e2a\u6c49\u5b57\u3002\u5728 GBK \u7684\u7f16\u7801\u65b9\u6848\u4e2d\uff0cASCII \u5b57\u7b26\u4f7f\u7528\u4e00\u4e2a\u5b57\u8282\u8868\u793a\uff0c\u6c49\u5b57\u4f7f\u7528\u4e24\u4e2a\u5b57\u8282\u8868\u793a\u3002

"},{"location":"chapter_data_structure/character_encoding/#343-unicode","title":"3.4.3 \u00a0 Unicode \u5b57\u7b26\u96c6","text":"

\u968f\u7740\u8ba1\u7b97\u673a\u7684\u84ec\u52c3\u53d1\u5c55\uff0c\u5b57\u7b26\u96c6\u4e0e\u7f16\u7801\u6807\u51c6\u767e\u82b1\u9f50\u653e\uff0c\u800c\u8fd9\u5e26\u6765\u4e86\u8bb8\u591a\u95ee\u9898\u3002\u4e00\u65b9\u9762\uff0c\u8fd9\u4e9b\u5b57\u7b26\u96c6\u4e00\u822c\u53ea\u5b9a\u4e49\u4e86\u7279\u5b9a\u8bed\u8a00\u7684\u5b57\u7b26\uff0c\u65e0\u6cd5\u5728\u591a\u8bed\u8a00\u73af\u5883\u4e0b\u6b63\u5e38\u5de5\u4f5c\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u540c\u4e00\u79cd\u8bed\u8a00\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\u7814\u7a76\u4eba\u5458\u5c31\u5728\u60f3\uff1a\u5982\u679c\u63a8\u51fa\u4e00\u4e2a\u8db3\u591f\u5b8c\u6574\u7684\u5b57\u7b26\u96c6\uff0c\u5c06\u4e16\u754c\u8303\u56f4\u5185\u7684\u6240\u6709\u8bed\u8a00\u548c\u7b26\u53f7\u90fd\u6536\u5f55\u5176\u4e2d\uff0c\u4e0d\u5c31\u53ef\u4ee5\u89e3\u51b3\u8de8\u8bed\u8a00\u73af\u5883\u548c\u4e71\u7801\u95ee\u9898\u4e86\u5417\uff1f\u5728\u8fd9\u79cd\u60f3\u6cd5\u7684\u9a71\u52a8\u4e0b\uff0c\u4e00\u4e2a\u5927\u800c\u5168\u7684\u5b57\u7b26\u96c6 Unicode \u5e94\u8fd0\u800c\u751f\u3002

\u300cUnicode\u300d\u7684\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\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a 2 \u5b57\u8282\u7684\u7f16\u7801\uff0c\u7cfb\u7edf\u5982\u4f55\u786e\u8ba4\u5b83\u662f\u4e00\u4e2a 2 \u5b57\u8282\u7684\u5b57\u7b26\u8fd8\u662f\u4e24\u4e2a 1 \u5b57\u8282\u7684\u5b57\u7b26\uff1f

\u5bf9\u4e8e\u4ee5\u4e0a\u95ee\u9898\uff0c\u4e00\u79cd\u76f4\u63a5\u7684\u89e3\u51b3\u65b9\u6848\u662f\u5c06\u6240\u6709\u5b57\u7b26\u5b58\u50a8\u4e3a\u7b49\u957f\u7684\u7f16\u7801\u3002\u5982\u56fe 3-7 \u6240\u793a\uff0c\u201cHello\u201d\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 1 \u5b57\u8282\uff0c\u201c\u7b97\u6cd5\u201d\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 2 \u5b57\u8282\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u9ad8\u4f4d\u586b 0 \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

\u56fe 3-7 \u00a0 Unicode \u7f16\u7801\u793a\u4f8b

\u7136\u800c ASCII \u7801\u5df2\u7ecf\u5411\u6211\u4eec\u8bc1\u660e\uff0c\u7f16\u7801\u82f1\u6587\u53ea\u9700\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\u4e24\u500d\uff0c\u975e\u5e38\u6d6a\u8d39\u5185\u5b58\u7a7a\u95f4\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u4e00\u79cd\u66f4\u52a0\u9ad8\u6548\u7684 Unicode \u7f16\u7801\u65b9\u6cd5\u3002

"},{"location":"chapter_data_structure/character_encoding/#344-utf-8","title":"3.4.4 \u00a0 UTF-8 \u7f16\u7801","text":"

\u76ee\u524d\uff0cUTF-8 \u5df2\u6210\u4e3a\u56fd\u9645\u4e0a\u4f7f\u7528\u6700\u5e7f\u6cdb\u7684 Unicode \u7f16\u7801\u65b9\u6cd5\u3002\u5b83\u662f\u4e00\u79cd\u53ef\u53d8\u957f\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\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\u3002

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

\u56fe 3-8 \u5c55\u793a\u4e86\u201cHello\u7b97\u6cd5\u201d\u5bf9\u5e94\u7684 UTF-8 \u7f16\u7801\u3002\u89c2\u5bdf\u53d1\u73b0\uff0c\u7531\u4e8e\u6700\u9ad8 \\(n\\) \u4f4d\u90fd\u88ab\u8bbe\u7f6e\u4e3a \\(1\\) \uff0c\u56e0\u6b64\u7cfb\u7edf\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u6700\u9ad8\u4f4d \\(1\\) \u7684\u4e2a\u6570\u6765\u89e3\u6790\u51fa\u5b57\u7b26\u7684\u957f\u5ea6\u4e3a \\(n\\) \u3002

\u4f46\u4e3a\u4ec0\u4e48\u8981\u5c06\u5176\u4f59\u6240\u6709\u5b57\u8282\u7684\u9ad8 2 \u4f4d\u90fd\u8bbe\u7f6e\u4e3a \\(10\\) \u5462\uff1f\u5b9e\u9645\u4e0a\uff0c\u8fd9\u4e2a \\(10\\) \u80fd\u591f\u8d77\u5230\u6821\u9a8c\u7b26\u7684\u4f5c\u7528\u3002\u5047\u8bbe\u7cfb\u7edf\u4ece\u4e00\u4e2a\u9519\u8bef\u7684\u5b57\u8282\u5f00\u59cb\u89e3\u6790\u6587\u672c\uff0c\u5b57\u8282\u5934\u90e8\u7684 \\(10\\) \u80fd\u591f\u5e2e\u52a9\u7cfb\u7edf\u5feb\u901f\u7684\u5224\u65ad\u51fa\u5f02\u5e38\u3002

\u4e4b\u6240\u4ee5\u5c06 \\(10\\) \u5f53\u4f5c\u6821\u9a8c\u7b26\uff0c\u662f\u56e0\u4e3a\u5728 UTF-8 \u7f16\u7801\u89c4\u5219\u4e0b\uff0c\u4e0d\u53ef\u80fd\u6709\u5b57\u7b26\u7684\u6700\u9ad8\u4e24\u4f4d\u662f \\(10\\) \u3002\u8fd9\u4e2a\u7ed3\u8bba\u53ef\u4ee5\u7528\u53cd\u8bc1\u6cd5\u6765\u8bc1\u660e\uff1a\u5047\u8bbe\u4e00\u4e2a\u5b57\u7b26\u7684\u6700\u9ad8\u4e24\u4f4d\u662f \\(10\\) \uff0c\u8bf4\u660e\u8be5\u5b57\u7b26\u7684\u957f\u5ea6\u4e3a \\(1\\) \uff0c\u5bf9\u5e94 ASCII \u7801\u3002\u800c ASCII \u7801\u7684\u6700\u9ad8\u4f4d\u5e94\u8be5\u662f \\(0\\) \uff0c\u4e0e\u5047\u8bbe\u77db\u76fe\u3002

\u56fe 3-8 \u00a0 UTF-8 \u7f16\u7801\u793a\u4f8b

\u9664\u4e86 UTF-8 \u4e4b\u5916\uff0c\u5e38\u89c1\u7684\u7f16\u7801\u65b9\u5f0f\u8fd8\u5305\u62ec\u4ee5\u4e0b\u4e24\u79cd\u3002

  • 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\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 ASCII \u5b57\u7b26\u5360\u6bd4\u8f83\u9ad8\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\u5728\u7b49\u957f\u7f16\u7801\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u770b\u4f5c\u6570\u7ec4\u6765\u5904\u7406\uff0c\u8fd9\u79cd\u505a\u6cd5\u5177\u6709\u4ee5\u4e0b\u4f18\u70b9\u3002

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

\u5b9e\u9645\u4e0a\uff0c\u7f16\u7a0b\u8bed\u8a00\u7684\u5b57\u7b26\u7f16\u7801\u65b9\u6848\u8bbe\u8ba1\u662f\u4e00\u4e2a\u5f88\u6709\u8da3\u7684\u8bdd\u9898\uff0c\u5176\u6d89\u53ca\u5230\u8bb8\u591a\u56e0\u7d20\u3002

  • 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\u4ece\u800c\u4e27\u5931\u4e86\u7b49\u957f\u7f16\u7801\u7684\u4f18\u52bf\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u5904\u7406\u4ee3\u7406\u5bf9\u9700\u8981\u589e\u52a0\u989d\u5916\u4ee3\u7801\uff0c\u8fd9\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\u4e00\u4e9b\u4e0d\u540c\u7684\u7f16\u7801\u65b9\u6848\u3002

  • Python \u4e2d\u7684 str \u4f7f\u7528 Unicode \u7f16\u7801\uff0c\u5e76\u91c7\u7528\u4e00\u79cd\u7075\u6d3b\u7684\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u5b58\u50a8\u7684\u5b57\u7b26\u957f\u5ea6\u53d6\u51b3\u4e8e\u5b57\u7b26\u4e32\u4e2d\u6700\u5927\u7684 Unicode \u7801\u70b9\u3002\u82e5\u5b57\u7b26\u4e32\u4e2d\u5168\u90e8\u662f ASCII \u5b57\u7b26\uff0c\u5219\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 1 \u4e2a\u5b57\u8282\uff1b\u5982\u679c\u6709\u5b57\u7b26\u8d85\u51fa\u4e86 ASCII \u8303\u56f4\uff0c\u4f46\u5168\u90e8\u5728\u57fa\u672c\u591a\u8bed\u8a00\u5e73\u9762\uff08BMP\uff09\u5185\uff0c\u5219\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 2 \u4e2a\u5b57\u8282\uff1b\u5982\u679c\u6709\u8d85\u51fa BMP \u7684\u5b57\u7b26\uff0c\u5219\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 4 \u4e2a\u5b57\u8282\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\u901a\u5e38\u4f1a\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a UTF-8 \u683c\u5f0f\uff0c\u4ee5\u8fbe\u5230\u6700\u4f18\u7684\u517c\u5bb9\u6027\u548c\u7a7a\u95f4\u6548\u7387\u3002

"},{"location":"chapter_data_structure/classification_of_data_structure/","title":"3.1 \u00a0 \u6570\u636e\u7ed3\u6784\u5206\u7c7b","text":"

\u5e38\u89c1\u7684\u6570\u636e\u7ed3\u6784\u5305\u62ec\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\uff0c\u5b83\u4eec\u53ef\u4ee5\u4ece\u201c\u903b\u8f91\u7ed3\u6784\u201d\u548c\u201c\u7269\u7406\u7ed3\u6784\u201d\u4e24\u4e2a\u7ef4\u5ea6\u8fdb\u884c\u5206\u7c7b\u3002

"},{"location":"chapter_data_structure/classification_of_data_structure/#311","title":"3.1.1 \u00a0 \u903b\u8f91\u7ed3\u6784\uff1a\u7ebf\u6027\u4e0e\u975e\u7ebf\u6027","text":"

\u903b\u8f91\u7ed3\u6784\u63ed\u793a\u4e86\u6570\u636e\u5143\u7d20\u4e4b\u95f4\u7684\u903b\u8f91\u5173\u7cfb\u3002\u5728\u6570\u7ec4\u548c\u94fe\u8868\u4e2d\uff0c\u6570\u636e\u6309\u7167\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

\u5982\u56fe 3-1 \u6240\u793a\uff0c\u903b\u8f91\u7ed3\u6784\u53ef\u88ab\u5206\u4e3a\u201c\u7ebf\u6027\u201d\u548c\u201c\u975e\u7ebf\u6027\u201d\u4e24\u5927\u7c7b\u3002\u7ebf\u6027\u7ed3\u6784\u6bd4\u8f83\u76f4\u89c2\uff0c\u6307\u6570\u636e\u5728\u903b\u8f91\u5173\u7cfb\u4e0a\u5448\u7ebf\u6027\u6392\u5217\uff1b\u975e\u7ebf\u6027\u7ed3\u6784\u5219\u76f8\u53cd\uff0c\u5448\u975e\u7ebf\u6027\u6392\u5217\u3002

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

\u56fe 3-1 \u00a0 \u7ebf\u6027\u4e0e\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784

\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u8fdb\u4e00\u6b65\u88ab\u5212\u5206\u4e3a\u6811\u5f62\u7ed3\u6784\u548c\u7f51\u72b6\u7ed3\u6784\u3002

  • \u7ebf\u6027\u7ed3\u6784\uff1a\u6570\u7ec4\u3001\u94fe\u8868\u3001\u961f\u5217\u3001\u6808\u3001\u54c8\u5e0c\u8868\uff0c\u5143\u7d20\u4e4b\u95f4\u662f\u4e00\u5bf9\u4e00\u7684\u987a\u5e8f\u5173\u7cfb\u3002
  • \u6811\u5f62\u7ed3\u6784\uff1a\u6811\u3001\u5806\u3001\u54c8\u5e0c\u8868\uff0c\u5143\u7d20\u4e4b\u95f4\u662f\u4e00\u5bf9\u591a\u7684\u5173\u7cfb\u3002
  • \u7f51\u72b6\u7ed3\u6784\uff1a\u56fe\uff0c\u5143\u7d20\u4e4b\u95f4\u662f\u591a\u5bf9\u591a\u7684\u5173\u7cfb\u3002
"},{"location":"chapter_data_structure/classification_of_data_structure/#312","title":"3.1.2 \u00a0 \u7269\u7406\u7ed3\u6784\uff1a\u8fde\u7eed\u4e0e\u5206\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\u786c\u76d8\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\u5185\u5b58\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\u56fe 3-2 \u5c55\u793a\u4e86\u4e00\u4e2a\u8ba1\u7b97\u673a\u5185\u5b58\u6761\uff0c\u5176\u4e2d\u6bcf\u4e2a\u9ed1\u8272\u65b9\u5757\u90fd\u5305\u542b\u4e00\u5757\u5185\u5b58\u7a7a\u95f4\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u5185\u5b58\u60f3\u8c61\u6210\u4e00\u4e2a\u5de8\u5927\u7684 Excel \u8868\u683c\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5355\u5143\u683c\u90fd\u53ef\u4ee5\u5b58\u50a8\u4e00\u5b9a\u5927\u5c0f\u7684\u6570\u636e\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\u5185\u5b58\u5730\u5740\u6765\u8bbf\u95ee\u76ee\u6807\u4f4d\u7f6e\u7684\u6570\u636e\u3002\u5982\u56fe 3-2 \u6240\u793a\uff0c\u8ba1\u7b97\u673a\u6839\u636e\u7279\u5b9a\u89c4\u5219\u4e3a\u8868\u683c\u4e2d\u7684\u6bcf\u4e2a\u5355\u5143\u683c\u5206\u914d\u7f16\u53f7\uff0c\u786e\u4fdd\u6bcf\u4e2a\u5185\u5b58\u7a7a\u95f4\u90fd\u6709\u552f\u4e00\u7684\u5185\u5b58\u5730\u5740\u3002\u6709\u4e86\u8fd9\u4e9b\u5730\u5740\uff0c\u7a0b\u5e8f\u4fbf\u53ef\u4ee5\u8bbf\u95ee\u5185\u5b58\u4e2d\u7684\u6570\u636e\u3002

\u56fe 3-2 \u00a0 \u5185\u5b58\u6761\u3001\u5185\u5b58\u7a7a\u95f4\u3001\u5185\u5b58\u5730\u5740

\u5185\u5b58\u662f\u6240\u6709\u7a0b\u5e8f\u7684\u5171\u4eab\u8d44\u6e90\uff0c\u5f53\u67d0\u5757\u5185\u5b58\u88ab\u67d0\u4e2a\u7a0b\u5e8f\u5360\u7528\u65f6\uff0c\u5219\u65e0\u6cd5\u88ab\u5176\u4ed6\u7a0b\u5e8f\u540c\u65f6\u4f7f\u7528\u4e86\u3002\u56e0\u6b64\u5728\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u8bbe\u8ba1\u4e2d\uff0c\u5185\u5b58\u8d44\u6e90\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u6bd4\u5982\uff0c\u7b97\u6cd5\u6240\u5360\u7528\u7684\u5185\u5b58\u5cf0\u503c\u4e0d\u5e94\u8d85\u8fc7\u7cfb\u7edf\u5269\u4f59\u7a7a\u95f2\u5185\u5b58\uff1b\u5982\u679c\u7f3a\u5c11\u8fde\u7eed\u5927\u5757\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u90a3\u4e48\u6240\u9009\u7528\u7684\u6570\u636e\u7ed3\u6784\u5fc5\u987b\u80fd\u591f\u5b58\u50a8\u5728\u5206\u6563\u7684\u5185\u5b58\u7a7a\u95f4\u5185\u3002

\u5982\u56fe 3-3 \u6240\u793a\uff0c\u7269\u7406\u7ed3\u6784\u53cd\u6620\u4e86\u6570\u636e\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u53ef\u5206\u4e3a\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\uff08\u6570\u7ec4\uff09\u548c\u5206\u6563\u7a7a\u95f4\u5b58\u50a8\uff08\u94fe\u8868\uff09\u3002\u7269\u7406\u7ed3\u6784\u4ece\u5e95\u5c42\u51b3\u5b9a\u4e86\u6570\u636e\u7684\u8bbf\u95ee\u3001\u66f4\u65b0\u3001\u589e\u5220\u7b49\u64cd\u4f5c\u65b9\u6cd5\uff0c\u540c\u65f6\u5728\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u65b9\u9762\u5448\u73b0\u51fa\u4e92\u8865\u7684\u7279\u70b9\u3002

\u56fe 3-3 \u00a0 \u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u4e0e\u5206\u6563\u7a7a\u95f4\u5b58\u50a8

\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6240\u6709\u6570\u636e\u7ed3\u6784\u90fd\u662f\u57fa\u4e8e\u6570\u7ec4\u3001\u94fe\u8868\u6216\u4e8c\u8005\u7684\u7ec4\u5408\u5b9e\u73b0\u7684\u3002\u4f8b\u5982\uff0c\u6808\u548c\u961f\u5217\u65e2\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u94fe\u8868\u5b9e\u73b0\uff1b\u800c\u54c8\u5e0c\u8868\u7684\u5b9e\u73b0\u53ef\u80fd\u540c\u65f6\u5305\u542b\u6570\u7ec4\u548c\u94fe\u8868\u3002

  • \u57fa\u4e8e\u6570\u7ec4\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\u3002
  • \u57fa\u4e8e\u94fe\u8868\u53ef\u5b9e\u73b0\uff1a\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\u7b49\u3002

\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u4e5f\u88ab\u79f0\u4e3a\u201c\u9759\u6001\u6570\u636e\u7ed3\u6784\u201d\uff0c\u8fd9\u610f\u5473\u7740\u6b64\u7c7b\u6570\u636e\u7ed3\u6784\u5728\u521d\u59cb\u5316\u540e\u957f\u5ea6\u4e0d\u53ef\u53d8\u3002\u76f8\u5bf9\u5e94\u5730\uff0c\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u88ab\u79f0\u4e3a\u201c\u52a8\u6001\u6570\u636e\u7ed3\u6784\u201d\uff0c\u8fd9\u7c7b\u6570\u636e\u7ed3\u6784\u5728\u521d\u59cb\u5316\u540e\uff0c\u4ecd\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u5bf9\u5176\u957f\u5ea6\u8fdb\u884c\u8c03\u6574\u3002

Tip

\u5982\u679c\u4f60\u611f\u89c9\u7269\u7406\u7ed3\u6784\u7406\u89e3\u8d77\u6765\u6709\u56f0\u96be\uff0c\u5efa\u8bae\u5148\u9605\u8bfb\u4e0b\u4e00\u7ae0\u201c\u6570\u7ec4\u4e0e\u94fe\u8868\u201d\uff0c\u7136\u540e\u518d\u56de\u987e\u672c\u8282\u5185\u5bb9\u3002

"},{"location":"chapter_data_structure/number_encoding/","title":"3.3 \u00a0 \u6570\u5b57\u7f16\u7801 *","text":"

Note

\u5728\u672c\u4e66\u4e2d\uff0c\u6807\u9898\u5e26\u6709\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\u53ef\u4ee5\u5148\u8df3\u8fc7\uff0c\u7b49\u5b66\u5b8c\u5fc5\u8bfb\u7ae0\u8282\u540e\u518d\u5355\u72ec\u653b\u514b\u3002

"},{"location":"chapter_data_structure/number_encoding/#331","title":"3.3.1 \u00a0 \u6574\u6570\u7f16\u7801","text":"

\u5728\u4e0a\u4e00\u8282\u7684\u8868\u683c\u4e2d\u6211\u4eec\u53d1\u73b0\uff0c\u6240\u6709\u6574\u6570\u7c7b\u578b\u80fd\u591f\u8868\u793a\u7684\u8d1f\u6570\u90fd\u6bd4\u6b63\u6570\u591a\u4e00\u4e2a\uff0c\u4f8b\u5982 byte \u7684\u53d6\u503c\u8303\u56f4\u662f \\([-128, 127]\\) \u3002\u8fd9\u4e2a\u73b0\u8c61\u6bd4\u8f83\u53cd\u76f4\u89c9\uff0c\u5b83\u7684\u5185\u5728\u539f\u56e0\u6d89\u53ca\u5230\u539f\u7801\u3001\u53cd\u7801\u3001\u8865\u7801\u7684\u76f8\u5173\u77e5\u8bc6\u3002

\u9996\u5148\u9700\u8981\u6307\u51fa\uff0c\u6570\u5b57\u662f\u4ee5\u201c\u8865\u7801\u201d\u7684\u5f62\u5f0f\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u4e2d\u7684\u3002\u5728\u5206\u6790\u8fd9\u6837\u505a\u7684\u539f\u56e0\u4e4b\u524d\uff0c\u6211\u4eec\u9996\u5148\u7ed9\u51fa\u4e09\u8005\u7684\u5b9a\u4e49\u3002

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

\u56fe 3-4 \u5c55\u793a\u4e86\u539f\u7801\u3001\u53cd\u7801\u548c\u8865\u7801\u4e4b\u95f4\u7684\u8f6c\u6362\u65b9\u6cd5\u3002

\u56fe 3-4 \u00a0 \u539f\u7801\u3001\u53cd\u7801\u4e0e\u8865\u7801\u4e4b\u95f4\u7684\u76f8\u4e92\u8f6c\u6362

\u300c\u539f\u7801 true form\u300d\u867d\u7136\u6700\u76f4\u89c2\uff0c\u4f46\u5b58\u5728\u4e00\u4e9b\u5c40\u9650\u6027\u3002\u4e00\u65b9\u9762\uff0c\u8d1f\u6570\u7684\u539f\u7801\u4e0d\u80fd\u76f4\u63a5\u7528\u4e8e\u8fd0\u7b97\u3002\u4f8b\u5982\u5728\u539f\u7801\u4e0b\u8ba1\u7b97 \\(1 + (-2)\\) \uff0c\u5f97\u5230\u7684\u7ed3\u679c\u662f \\(-3\\) \uff0c\u8fd9\u663e\u7136\u662f\u4e0d\u5bf9\u7684\u3002

\\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 + 1000 \\; 0010 \\newline & = 1000 \\; 0011 \\newline & \\rightarrow -3 \\end{aligned} \\]

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

\\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 \\; \\text{(\u539f\u7801)} + 1000 \\; 0010 \\; \\text{(\u539f\u7801)} \\newline & = 0000 \\; 0001 \\; \\text{(\u53cd\u7801)} + 1111 \\; 1101 \\; \\text{(\u53cd\u7801)} \\newline & = 1111 \\; 1110 \\; \\text{(\u53cd\u7801)} \\newline & = 1000 \\; 0001 \\; \\text{(\u539f\u7801)} \\newline & \\rightarrow -1 \\end{aligned} \\]

\u53e6\u4e00\u65b9\u9762\uff0c\u6570\u5b57\u96f6\u7684\u539f\u7801\u6709 \\(+0\\) \u548c \\(-0\\) \u4e24\u79cd\u8868\u793a\u65b9\u5f0f\u3002\u8fd9\u610f\u5473\u7740\u6570\u5b57\u96f6\u5bf9\u5e94\u7740\u4e24\u4e2a\u4e0d\u540c\u7684\u4e8c\u8fdb\u5236\u7f16\u7801\uff0c\u5176\u53ef\u80fd\u4f1a\u5e26\u6765\u6b67\u4e49\u3002\u6bd4\u5982\u5728\u6761\u4ef6\u5224\u65ad\u4e2d\uff0c\u5982\u679c\u6ca1\u6709\u533a\u5206\u6b63\u96f6\u548c\u8d1f\u96f6\uff0c\u5219\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5224\u65ad\u7ed3\u679c\u51fa\u9519\u3002\u800c\u5982\u679c\u6211\u4eec\u60f3\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 & \\rightarrow 0000 \\; 0000 \\newline -0 & \\rightarrow 1000 \\; 0000 \\end{aligned} \\]

\u4e0e\u539f\u7801\u4e00\u6837\uff0c\u53cd\u7801\u4e5f\u5b58\u5728\u6b63\u8d1f\u96f6\u6b67\u4e49\u95ee\u9898\uff0c\u56e0\u6b64\u8ba1\u7b97\u673a\u8fdb\u4e00\u6b65\u5f15\u5165\u4e86\u300c\u8865\u7801 2's complement code\u300d\u3002\u6211\u4eec\u5148\u6765\u89c2\u5bdf\u4e00\u4e0b\u8d1f\u96f6\u7684\u539f\u7801\u3001\u53cd\u7801\u3001\u8865\u7801\u7684\u8f6c\u6362\u8fc7\u7a0b\uff1a

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

\u5728\u8d1f\u96f6\u7684\u53cd\u7801\u57fa\u7840\u4e0a\u52a0 \\(1\\) \u4f1a\u4ea7\u751f\u8fdb\u4f4d\uff0c\u4f46 byte \u7c7b\u578b\u7684\u957f\u5ea6\u53ea\u6709 8 \u4f4d\uff0c\u56e0\u6b64\u6ea2\u51fa\u5230\u7b2c 9 \u4f4d\u7684 \\(1\\) \u4f1a\u88ab\u820d\u5f03\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8d1f\u96f6\u7684\u8865\u7801\u4e3a \\(0000 \\; 0000\\) \uff0c\u4e0e\u6b63\u96f6\u7684\u8865\u7801\u76f8\u540c\u3002\u8fd9\u610f\u5473\u7740\u5728\u8865\u7801\u8868\u793a\u4e2d\u53ea\u5b58\u5728\u4e00\u4e2a\u96f6\uff0c\u6b63\u8d1f\u96f6\u6b67\u4e49\u4ece\u800c\u5f97\u5230\u89e3\u51b3\u3002

\u8fd8\u5269\u4f59\u6700\u540e\u4e00\u4e2a\u7591\u60d1\uff1abyte \u7c7b\u578b\u7684\u53d6\u503c\u8303\u56f4\u662f \\([-128, 127]\\) \uff0c\u591a\u51fa\u6765\u7684\u4e00\u4e2a\u8d1f\u6570 \\(-128\\) \u662f\u5982\u4f55\u5f97\u5230\u7684\u5462\uff1f\u6211\u4eec\u6ce8\u610f\u5230\uff0c\u533a\u95f4 \\([-127, +127]\\) \u5185\u7684\u6240\u6709\u6574\u6570\u90fd\u6709\u5bf9\u5e94\u7684\u539f\u7801\u3001\u53cd\u7801\u548c\u8865\u7801\uff0c\u5e76\u4e14\u539f\u7801\u548c\u8865\u7801\u4e4b\u95f4\u662f\u53ef\u4ee5\u4e92\u76f8\u8f6c\u6362\u7684\u3002

\u7136\u800c\uff0c\u8865\u7801 \\(1000 \\; 0000\\) \u662f\u4e00\u4e2a\u4f8b\u5916\uff0c\u5b83\u5e76\u6ca1\u6709\u5bf9\u5e94\u7684\u539f\u7801\u3002\u6839\u636e\u8f6c\u6362\u65b9\u6cd5\uff0c\u6211\u4eec\u5f97\u5230\u8be5\u8865\u7801\u7684\u539f\u7801\u4e3a \\(0000 \\; 0000\\) \u3002\u8fd9\u663e\u7136\u662f\u77db\u76fe\u7684\uff0c\u56e0\u4e3a\u8be5\u539f\u7801\u8868\u793a\u6570\u5b57 \\(0\\) \uff0c\u5b83\u7684\u8865\u7801\u5e94\u8be5\u662f\u81ea\u8eab\u3002\u8ba1\u7b97\u673a\u89c4\u5b9a\u8fd9\u4e2a\u7279\u6b8a\u7684\u8865\u7801 \\(1000 \\; 0000\\) \u4ee3\u8868 \\(-128\\) \u3002\u5b9e\u9645\u4e0a\uff0c\\((-1) + (-127)\\) \u5728\u8865\u7801\u4e0b\u7684\u8ba1\u7b97\u7ed3\u679c\u5c31\u662f \\(-128\\) \u3002

\\[ \\begin{aligned} & (-127) + (-1) \\newline & \\rightarrow 1111 \\; 1111 \\; \\text{(\u539f\u7801)} + 1000 \\; 0001 \\; \\text{(\u539f\u7801)} \\newline & = 1000 \\; 0000 \\; \\text{(\u53cd\u7801)} + 1111 \\; 1110 \\; \\text{(\u53cd\u7801)} \\newline & = 1000 \\; 0001 \\; \\text{(\u8865\u7801)} + 1111 \\; 1111 \\; \\text{(\u8865\u7801)} \\newline & = 1000 \\; 0000 \\; \\text{(\u8865\u7801)} \\newline & \\rightarrow -128 \\end{aligned} \\]

\u4f60\u53ef\u80fd\u5df2\u7ecf\u53d1\u73b0\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\u8fd0\u7b97\u901f\u5ea6\u66f4\u5feb\u3002

\u8bf7\u6ce8\u610f\uff0c\u8fd9\u5e76\u4e0d\u610f\u5473\u7740\u8ba1\u7b97\u673a\u53ea\u80fd\u505a\u52a0\u6cd5\u3002\u901a\u8fc7\u5c06\u52a0\u6cd5\u4e0e\u4e00\u4e9b\u57fa\u672c\u903b\u8f91\u8fd0\u7b97\u7ed3\u5408\uff0c\u8ba1\u7b97\u673a\u80fd\u591f\u5b9e\u73b0\u5404\u79cd\u5176\u4ed6\u7684\u6570\u5b66\u8fd0\u7b97\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u51cf\u6cd5 \\(a - b\\) \u53ef\u4ee5\u8f6c\u6362\u4e3a\u8ba1\u7b97\u52a0\u6cd5 \\(a + (-b)\\) \uff1b\u8ba1\u7b97\u4e58\u6cd5\u548c\u9664\u6cd5\u53ef\u4ee5\u8f6c\u6362\u4e3a\u8ba1\u7b97\u591a\u6b21\u52a0\u6cd5\u6216\u51cf\u6cd5\u3002

\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u603b\u7ed3\u51fa\u8ba1\u7b97\u673a\u4f7f\u7528\u8865\u7801\u7684\u539f\u56e0\uff1a\u57fa\u4e8e\u8865\u7801\u8868\u793a\uff0c\u8ba1\u7b97\u673a\u53ef\u4ee5\u7528\u540c\u6837\u7684\u7535\u8def\u548c\u64cd\u4f5c\u6765\u5904\u7406\u6b63\u6570\u548c\u8d1f\u6570\u7684\u52a0\u6cd5\uff0c\u4e0d\u9700\u8981\u8bbe\u8ba1\u7279\u6b8a\u7684\u786c\u4ef6\u7535\u8def\u6765\u5904\u7406\u51cf\u6cd5\uff0c\u5e76\u4e14\u65e0\u987b\u7279\u522b\u5904\u7406\u6b63\u8d1f\u96f6\u7684\u6b67\u4e49\u95ee\u9898\u3002\u8fd9\u5927\u5927\u7b80\u5316\u4e86\u786c\u4ef6\u8bbe\u8ba1\uff0c\u63d0\u9ad8\u4e86\u8fd0\u7b97\u6548\u7387\u3002

\u8865\u7801\u7684\u8bbe\u8ba1\u975e\u5e38\u7cbe\u5999\uff0c\u56e0\u7bc7\u5e45\u5173\u7cfb\u6211\u4eec\u5c31\u5148\u4ecb\u7ecd\u5230\u8fd9\u91cc\uff0c\u5efa\u8bae\u6709\u5174\u8da3\u7684\u8bfb\u8005\u8fdb\u4e00\u6b65\u6df1\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\u8bb0\u4e00\u4e2a 32-bit \u957f\u5ea6\u7684\u4e8c\u8fdb\u5236\u6570\u4e3a\uff1a

\\[ b_{31} b_{30} b_{29} \\ldots b_2 b_1 b_0 \\]

\u6839\u636e IEEE 754 \u6807\u51c6\uff0c32-bit \u957f\u5ea6\u7684 float \u7531\u4ee5\u4e0b\u4e09\u4e2a\u90e8\u5206\u6784\u6210\u3002

  • \u7b26\u53f7\u4f4d \\(\\mathrm{S}\\) \uff1a\u5360 1 bit \uff0c\u5bf9\u5e94 \\(b_{31}\\) \u3002
  • \u6307\u6570\u4f4d \\(\\mathrm{E}\\) \uff1a\u5360 8 bits \uff0c\u5bf9\u5e94 \\(b_{30} b_{29} \\ldots b_{23}\\) \u3002
  • \u5206\u6570\u4f4d \\(\\mathrm{N}\\) \uff1a\u5360 23 bits \uff0c\u5bf9\u5e94 \\(b_{22} b_{21} \\ldots b_0\\) \u3002

\u4e8c\u8fdb\u5236\u6570 float \u5bf9\u5e94\u7684\u503c\u7684\u8ba1\u7b97\u65b9\u6cd5\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\uff1a

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

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

\\[ \\begin{aligned} \\mathrm{S} \\in & \\{ 0, 1\\}, \\quad \\mathrm{E} \\in \\{ 1, 2, \\dots, 254 \\} \\newline (1 + \\mathrm{N}) = & (1 + \\sum_{i=1}^{23} b_{23-i} 2^{-i}) \\subset [1, 2 - 2^{-23}] \\end{aligned} \\]

\u56fe 3-5 \u00a0 IEEE 754 \u6807\u51c6\u4e0b\u7684 float \u7684\u8ba1\u7b97\u793a\u4f8b

\u89c2\u5bdf\u56fe 3-5 \uff0c\u7ed9\u5b9a\u4e00\u4e2a\u793a\u4f8b\u6570\u636e \\(\\mathrm{S} = 0\\) \uff0c \\(\\mathrm{E} = 124\\) \uff0c\\(\\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\\) \uff0c\u5219\u6709\uff1a

\\[ \\text { val } = (-1)^0 \\times 2^{124 - 127} \\times (1 + 0.375) = 0.171875 \\]

\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u56de\u7b54\u6700\u521d\u7684\u95ee\u9898\uff1afloat \u7684\u8868\u793a\u65b9\u5f0f\u5305\u542b\u6307\u6570\u4f4d\uff0c\u5bfc\u81f4\u5176\u53d6\u503c\u8303\u56f4\u8fdc\u5927\u4e8e int \u3002\u6839\u636e\u4ee5\u4e0a\u8ba1\u7b97\uff0cfloat \u53ef\u8868\u793a\u7684\u6700\u5927\u6b63\u6570\u4e3a \\(2^{254 - 127} \\times (2 - 2^{-23}) \\approx 3.4 \\times 10^{38}\\) \uff0c\u5207\u6362\u7b26\u53f7\u4f4d\u4fbf\u53ef\u5f97\u5230\u6700\u5c0f\u8d1f\u6570\u3002

\u5c3d\u7ba1\u6d6e\u70b9\u6570 float \u6269\u5c55\u4e86\u53d6\u503c\u8303\u56f4\uff0c\u4f46\u5176\u526f\u4f5c\u7528\u662f\u727a\u7272\u4e86\u7cbe\u5ea6\u3002\u6574\u6570\u7c7b\u578b int \u5c06\u5168\u90e8 32 \u4f4d\u7528\u4e8e\u8868\u793a\u6570\u5b57\uff0c\u6570\u5b57\u662f\u5747\u5300\u5206\u5e03\u7684\uff1b\u800c\u7531\u4e8e\u6307\u6570\u4f4d\u7684\u5b58\u5728\uff0c\u6d6e\u70b9\u6570 float \u7684\u6570\u503c\u8d8a\u5927\uff0c\u76f8\u90bb\u4e24\u4e2a\u6570\u5b57\u4e4b\u95f4\u7684\u5dee\u503c\u5c31\u4f1a\u8d8b\u5411\u8d8a\u5927\u3002

\u5982\u8868 3-2 \u6240\u793a\uff0c\u6307\u6570\u4f4d \\(E = 0\\) \u548c \\(E = 255\\) \u5177\u6709\u7279\u6b8a\u542b\u4e49\uff0c\u7528\u4e8e\u8868\u793a\u96f6\u3001\u65e0\u7a77\u5927\u3001\\(\\mathrm{NaN}\\) \u7b49\u3002

\u8868 3-2 \u00a0 \u6307\u6570\u4f4d\u542b\u4e49

\u6307\u6570\u4f4d E \u5206\u6570\u4f4d \\(\\mathrm{N} = 0\\) \u5206\u6570\u4f4d \\(\\mathrm{N} \\ne 0\\) \u8ba1\u7b97\u516c\u5f0f \\(0\\) \\(\\pm 0\\) \u6b21\u6b63\u89c4\u6570 \\((-1)^{\\mathrm{S}} \\times 2^{-126} \\times (0.\\mathrm{N})\\) \\(1, 2, \\dots, 254\\) \u6b63\u89c4\u6570 \u6b63\u89c4\u6570 \\((-1)^{\\mathrm{S}} \\times 2^{(\\mathrm{E} -127)} \\times (1.\\mathrm{N})\\) \\(255\\) \\(\\pm \\infty\\) \\(\\mathrm{NaN}\\)

\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6b21\u6b63\u89c4\u6570\u663e\u8457\u63d0\u5347\u4e86\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\u3002\u6700\u5c0f\u6b63\u6b63\u89c4\u6570\u4e3a \\(2^{-126}\\) \uff0c\u6700\u5c0f\u6b63\u6b21\u6b63\u89c4\u6570\u4e3a \\(2^{-126} \\times 2^{-23}\\) \u3002

\u53cc\u7cbe\u5ea6 double \u4e5f\u91c7\u7528\u7c7b\u4f3c float \u7684\u8868\u793a\u65b9\u6cd5\uff0c\u5728\u6b64\u4e0d\u505a\u8d58\u8ff0\u3002

"},{"location":"chapter_data_structure/summary/","title":"3.5 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_data_structure/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
  • \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\u5206\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
  • \u8ba1\u7b97\u673a\u4e2d\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u5305\u62ec\u6574\u6570 byte\u3001short\u3001int\u3001long \uff0c\u6d6e\u70b9\u6570 float\u3001double \uff0c\u5b57\u7b26 char \u548c\u5e03\u5c14 boolean \u3002\u5b83\u4eec\u7684\u53d6\u503c\u8303\u56f4\u53d6\u51b3\u4e8e\u5360\u7528\u7a7a\u95f4\u5927\u5c0f\u548c\u8868\u793a\u65b9\u5f0f\u3002
  • \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 \u548c C# \u7b49\u7f16\u7a0b\u8bed\u8a00\u9ed8\u8ba4\u4f7f\u7528 UTF-16 \u7f16\u7801\u3002
"},{"location":"chapter_data_structure/summary/#2-q-a","title":"2. \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\u94fe\u5f0f\u5730\u5740\u201d\uff08\u540e\u7eed\u54c8\u5e0c\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 byte \u5417\uff1f

char \u7c7b\u578b\u7684\u957f\u5ea6\u7531\u7f16\u7a0b\u8bed\u8a00\u91c7\u7528\u7684\u7f16\u7801\u65b9\u6cd5\u51b3\u5b9a\u3002\u4f8b\u5982\uff0cJava\u3001JS\u3001TS\u3001C# \u90fd\u91c7\u7528 UTF-16 \u7f16\u7801\uff08\u4fdd\u5b58 Unicode \u7801\u70b9\uff09\uff0c\u56e0\u6b64 char \u7c7b\u578b\u7684\u957f\u5ea6\u4e3a 2 bytes \u3002

"},{"location":"chapter_divide_and_conquer/","title":"\u7b2c 12 \u7ae0 \u00a0 \u5206\u6cbb","text":"

Abstract

\u96be\u9898\u88ab\u9010\u5c42\u62c6\u89e3\uff0c\u6bcf\u4e00\u6b21\u7684\u62c6\u89e3\u90fd\u4f7f\u5b83\u53d8\u5f97\u66f4\u4e3a\u7b80\u5355\u3002

\u5206\u800c\u6cbb\u4e4b\u63ed\u793a\u4e86\u4e00\u4e2a\u91cd\u8981\u7684\u4e8b\u5b9e\uff1a\u4ece\u7b80\u5355\u505a\u8d77\uff0c\u4e00\u5207\u90fd\u4e0d\u518d\u590d\u6742\u3002

"},{"location":"chapter_divide_and_conquer/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 12.1 \u00a0 \u5206\u6cbb\u7b97\u6cd5
  • 12.2 \u00a0 \u5206\u6cbb\u641c\u7d22\u7b56\u7565
  • 12.3 \u00a0 \u6784\u5efa\u6811\u95ee\u9898
  • 12.4 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898
  • 12.5 \u00a0 \u5c0f\u7ed3
"},{"location":"chapter_divide_and_conquer/binary_search_recur/","title":"12.2 \u00a0 \u5206\u6cbb\u641c\u7d22\u7b56\u7565","text":"

\u6211\u4eec\u5df2\u7ecf\u5b66\u8fc7\uff0c\u641c\u7d22\u7b97\u6cd5\u5206\u4e3a\u4e24\u5927\u7c7b\u3002

  • \u66b4\u529b\u641c\u7d22\uff1a\u5b83\u901a\u8fc7\u904d\u5386\u6570\u636e\u7ed3\u6784\u5b9e\u73b0\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002
  • \u81ea\u9002\u5e94\u641c\u7d22\uff1a\u5b83\u5229\u7528\u7279\u6709\u7684\u6570\u636e\u7ec4\u7ec7\u5f62\u5f0f\u6216\u5148\u9a8c\u4fe1\u606f\uff0c\u53ef\u8fbe\u5230 \\(O(\\log n)\\) \u751a\u81f3 \\(O(1)\\) \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002

\u5b9e\u9645\u4e0a\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u7684\u641c\u7d22\u7b97\u6cd5\u901a\u5e38\u90fd\u662f\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u5b9e\u73b0\u7684\uff0c\u4f8b\u5982\u4e8c\u5206\u67e5\u627e\u548c\u6811\u3002

  • \u4e8c\u5206\u67e5\u627e\u7684\u6bcf\u4e00\u6b65\u90fd\u5c06\u95ee\u9898\uff08\u5728\u6570\u7ec4\u4e2d\u641c\u7d22\u76ee\u6807\u5143\u7d20\uff09\u5206\u89e3\u4e3a\u4e00\u4e2a\u5c0f\u95ee\u9898\uff08\u5728\u6570\u7ec4\u7684\u4e00\u534a\u4e2d\u641c\u7d22\u76ee\u6807\u5143\u7d20\uff09\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4e00\u76f4\u6301\u7eed\u5230\u6570\u7ec4\u4e3a\u7a7a\u6216\u627e\u5230\u76ee\u6807\u5143\u7d20\u4e3a\u6b62\u3002
  • \u6811\u662f\u5206\u6cbb\u5173\u7cfb\u7684\u4ee3\u8868\uff0c\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u3001AVL \u6811\u3001\u5806\u7b49\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u5404\u79cd\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7686\u4e3a \\(O(\\log n)\\) \u3002

\u4e8c\u5206\u67e5\u627e\u7684\u5206\u6cbb\u7b56\u7565\u5982\u4e0b\u6240\u793a\u3002

  • \u95ee\u9898\u53ef\u4ee5\u88ab\u5206\u89e3\uff1a\u4e8c\u5206\u67e5\u627e\u9012\u5f52\u5730\u5c06\u539f\u95ee\u9898\uff08\u5728\u6570\u7ec4\u4e2d\u8fdb\u884c\u67e5\u627e\uff09\u5206\u89e3\u4e3a\u5b50\u95ee\u9898\uff08\u5728\u6570\u7ec4\u7684\u4e00\u534a\u4e2d\u8fdb\u884c\u67e5\u627e\uff09\uff0c\u8fd9\u662f\u901a\u8fc7\u6bd4\u8f83\u4e2d\u95f4\u5143\u7d20\u548c\u76ee\u6807\u5143\u7d20\u6765\u5b9e\u73b0\u7684\u3002
  • \u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff1a\u5728\u4e8c\u5206\u67e5\u627e\u4e2d\uff0c\u6bcf\u8f6e\u53ea\u5904\u7406\u4e00\u4e2a\u5b50\u95ee\u9898\uff0c\u5b83\u4e0d\u53d7\u53e6\u5916\u5b50\u95ee\u9898\u7684\u5f71\u54cd\u3002
  • \u5b50\u95ee\u9898\u7684\u89e3\u65e0\u987b\u5408\u5e76\uff1a\u4e8c\u5206\u67e5\u627e\u65e8\u5728\u67e5\u627e\u4e00\u4e2a\u7279\u5b9a\u5143\u7d20\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u8fdb\u884c\u5408\u5e76\u3002\u5f53\u5b50\u95ee\u9898\u5f97\u5230\u89e3\u51b3\u65f6\uff0c\u539f\u95ee\u9898\u4e5f\u4f1a\u540c\u65f6\u5f97\u5230\u89e3\u51b3\u3002

\u5206\u6cbb\u80fd\u591f\u63d0\u5347\u641c\u7d22\u6548\u7387\uff0c\u672c\u8d28\u4e0a\u662f\u56e0\u4e3a\u66b4\u529b\u641c\u7d22\u6bcf\u8f6e\u53ea\u80fd\u6392\u9664\u4e00\u4e2a\u9009\u9879\uff0c\u800c\u5206\u6cbb\u641c\u7d22\u6bcf\u8f6e\u53ef\u4ee5\u6392\u9664\u4e00\u534a\u9009\u9879\u3002

"},{"location":"chapter_divide_and_conquer/binary_search_recur/#1","title":"1. \u00a0 \u57fa\u4e8e\u5206\u6cbb\u5b9e\u73b0\u4e8c\u5206","text":"

\u5728\u4e4b\u524d\u7684\u7ae0\u8282\u4e2d\uff0c\u4e8c\u5206\u67e5\u627e\u662f\u57fa\u4e8e\u9012\u63a8\uff08\u8fed\u4ee3\uff09\u5b9e\u73b0\u7684\u3002\u73b0\u5728\u6211\u4eec\u57fa\u4e8e\u5206\u6cbb\uff08\u9012\u5f52\uff09\u6765\u5b9e\u73b0\u5b83\u3002

Question

\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6709\u5e8f\u6570\u7ec4 nums \uff0c\u6570\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u90fd\u662f\u552f\u4e00\u7684\uff0c\u8bf7\u67e5\u627e\u5143\u7d20 target \u3002

\u4ece\u5206\u6cbb\u89d2\u5ea6\uff0c\u6211\u4eec\u5c06\u641c\u7d22\u533a\u95f4 \\([i, j]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u8bb0\u4e3a \\(f(i, j)\\) \u3002

\u4ece\u539f\u95ee\u9898 \\(f(0, n-1)\\) \u4e3a\u8d77\u59cb\u70b9\uff0c\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u4e8c\u5206\u67e5\u627e\u3002

  1. \u8ba1\u7b97\u641c\u7d22\u533a\u95f4 \\([i, j]\\) \u7684\u4e2d\u70b9 \\(m\\) \uff0c\u6839\u636e\u5b83\u6392\u9664\u4e00\u534a\u641c\u7d22\u533a\u95f4\u3002
  2. \u9012\u5f52\u6c42\u89e3\u89c4\u6a21\u51cf\u5c0f\u4e00\u534a\u7684\u5b50\u95ee\u9898\uff0c\u53ef\u80fd\u4e3a \\(f(i, m-1)\\) \u6216 \\(f(m+1, j)\\) \u3002
  3. \u5faa\u73af\u7b2c 1. \u548c 2. \u6b65\uff0c\u76f4\u81f3\u627e\u5230 target \u6216\u533a\u95f4\u4e3a\u7a7a\u65f6\u8fd4\u56de\u3002

\u56fe 12-4 \u5c55\u793a\u4e86\u5728\u6570\u7ec4\u4e2d\u4e8c\u5206\u67e5\u627e\u5143\u7d20 \\(6\\) \u7684\u5206\u6cbb\u8fc7\u7a0b\u3002

\u56fe 12-4 \u00a0 \u4e8c\u5206\u67e5\u627e\u7684\u5206\u6cbb\u8fc7\u7a0b

\u5728\u5b9e\u73b0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u9012\u5f52\u51fd\u6570 dfs() \u6765\u6c42\u89e3\u95ee\u9898 \\(f(i, j)\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_recur.py
def dfs(nums: list[int], target: int, i: int, j: int) -> int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j)\"\"\"\n# \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif i > j:\nreturn -1\n# \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nm = (i + j) // 2\nif nums[m] < target:\n# \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m + 1, j)\nelif nums[m] > target:\n# \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m - 1)\nelse:\n# \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m\ndef binary_search(nums: list[int], target: int) -> int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\"\"\"\nn = len(nums)\n# \u6c42\u89e3\u95ee\u9898 f(0, n-1)\nreturn dfs(nums, target, 0, n - 1)\n
binary_search_recur.cpp
/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(vector<int> &nums, int target, int i, int j) {\n// \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif (i > j) {\nreturn -1;\n}\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nint m = (i + j) / 2;\nif (nums[m] < target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m + 1, j);\n} else if (nums[m] > target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m - 1);\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(vector<int> &nums, int target) {\nint n = nums.size();\n// \u6c42\u89e3\u95ee\u9898 f(0, n-1)\nreturn dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.java
/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(int[] nums, int target, int i, int j) {\n// \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif (i > j) {\nreturn -1;\n}\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nint m = (i + j) / 2;\nif (nums[m] < target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m + 1, j);\n} else if (nums[m] > target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m - 1);\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(int[] nums, int target) {\nint n = nums.length;\n// \u6c42\u89e3\u95ee\u9898 f(0, n-1)\nreturn dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.cs
/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint DFS(int[] nums, int target, int i, int j) {\n// \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif (i > j) {\nreturn -1;\n}\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nint m = (i + j) / 2;\nif (nums[m] < target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn DFS(nums, target, m + 1, j);\n} else if (nums[m] > target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn DFS(nums, target, i, m - 1);\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nint BinarySearch(int[] nums, int target) {\nint n = nums.Length;\n// \u6c42\u89e3\u95ee\u9898 f(0, n-1)\nreturn DFS(nums, target, 0, n - 1);\n}\n
binary_search_recur.go
/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunc dfs(nums []int, target, i, j int) int {\n// \u5982\u679c\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u6ca1\u6709\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif i > j {\nreturn -1\n}\n//    \u8ba1\u7b97\u7d22\u5f15\u4e2d\u70b9\nm := i + ((j - i) >> 1)\n//\u5224\u65ad\u4e2d\u70b9\u4e0e\u76ee\u6807\u5143\u7d20\u5927\u5c0f\nif nums[m] < target {\n// \u5c0f\u4e8e\u5219\u9012\u5f52\u53f3\u534a\u6570\u7ec4\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m+1, j)\n} else if nums[m] > target {\n// \u5c0f\u4e8e\u5219\u9012\u5f52\u5de6\u534a\u6570\u7ec4\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m-1)\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nfunc binarySearch(nums []int, target int) int {\nn := len(nums)\nreturn dfs(nums, target, 0, n-1)\n}\n
binary_search_recur.swift
/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunc dfs(nums: [Int], target: Int, i: Int, j: Int) -> Int {\n// \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif i > j {\nreturn -1\n}\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nlet m = (i + j) / 2\nif nums[m] < target {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums: nums, target: target, i: m + 1, j: j)\n} else if nums[m] > target {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums: nums, target: target, i: i, j: m - 1)\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nfunc binarySearch(nums: [Int], target: Int) -> Int {\nlet n = nums.count\n// \u6c42\u89e3\u95ee\u9898 f(0, n-1)\nreturn dfs(nums: nums, target: target, i: 0, j: n - 1)\n}\n
binary_search_recur.js
/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunction dfs(nums, target, i, j) {\n// \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif (i > j) {\nreturn -1;\n}\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nconst m = i + ((j - i) >> 1);\nif (nums[m] < target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m + 1, j);\n} else if (nums[m] > target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m - 1);\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nfunction binarySearch(nums, target) {\nconst n = nums.length;\n// \u6c42\u89e3\u95ee\u9898 f(0, n-1)\nreturn dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.ts
/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunction dfs(nums: number[], target: number, i: number, j: number): number {\n// \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif (i > j) {\nreturn -1;\n}\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nconst m = i + ((j - i) >> 1);\nif (nums[m] < target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m + 1, j);\n} else if (nums[m] > target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m - 1);\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nfunction binarySearch(nums: number[], target: number): number {\nconst n = nums.length;\n// \u6c42\u89e3\u95ee\u9898 f(0, n-1)\nreturn dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.dart
/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(List<int> nums, int target, int i, int j) {\n// \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif (i > j) {\nreturn -1;\n}\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nint m = (i + j) ~/ 2;\nif (nums[m] < target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m + 1, j);\n} else if (nums[m] > target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m - 1);\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(List<int> nums, int target) {\nint n = nums.length;\n// \u6c42\u89e3\u95ee\u9898 f(0, n-1)\nreturn dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.rs
/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfn dfs(nums: &[i32], target: i32, i: i32, j: i32) -> i32 {\n// \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif i > j { return -1; }\nlet m: i32 = (i + j) / 2;\nif nums[m as usize] < target {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m + 1, j);\n} else if nums[m as usize] > target {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m - 1);\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nfn binary_search(nums: &[i32], target: i32) -> i32 {\nlet n = nums.len() as i32;\n// \u6c42\u89e3\u95ee\u9898 f(0, n-1)\ndfs(nums, target, 0, n - 1)\n}\n
binary_search_recur.c
/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(int nums[], int target, int i, int j) {\n// \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif (i > j) {\nreturn -1;\n}\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nint m = (i + j) / 2;\nif (nums[m] < target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m + 1, j);\n} else if (nums[m] > target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m - 1);\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(int nums[], int target, int numsSize) {\nint n = numsSize;\n// \u6c42\u89e3\u95ee\u9898 f(0, n-1)\nreturn dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.zig
[class]{}-[func]{dfs}\n[class]{}-[func]{binarySearch}\n
"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/","title":"12.3 \u00a0 \u6784\u5efa\u4e8c\u53c9\u6811\u95ee\u9898","text":"

Question

\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811\u7684\u524d\u5e8f\u904d\u5386 preorder \u548c\u4e2d\u5e8f\u904d\u5386 inorder \uff0c\u8bf7\u4ece\u4e2d\u6784\u5efa\u4e8c\u53c9\u6811\uff0c\u8fd4\u56de\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\u3002

\u56fe 12-5 \u00a0 \u6784\u5efa\u4e8c\u53c9\u6811\u7684\u793a\u4f8b\u6570\u636e

"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#1","title":"1. \u00a0 \u5224\u65ad\u662f\u5426\u4e3a\u5206\u6cbb\u95ee\u9898","text":"

\u539f\u95ee\u9898\u5b9a\u4e49\u4e3a\u4ece preorder \u548c inorder \u6784\u5efa\u4e8c\u53c9\u6811\uff0c\u5176\u662f\u4e00\u4e2a\u5178\u578b\u7684\u5206\u6cbb\u95ee\u9898\u3002

  • \u95ee\u9898\u53ef\u4ee5\u88ab\u5206\u89e3\uff1a\u4ece\u5206\u6cbb\u7684\u89d2\u5ea6\u5207\u5165\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u539f\u95ee\u9898\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\u3001\u6784\u5efa\u53f3\u5b50\u6811\uff0c\u52a0\u4e0a\u4e00\u6b65\u64cd\u4f5c\uff1a\u521d\u59cb\u5316\u6839\u8282\u70b9\u3002\u800c\u5bf9\u4e8e\u6bcf\u4e2a\u5b50\u6811\uff08\u5b50\u95ee\u9898\uff09\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u590d\u7528\u4ee5\u4e0a\u5212\u5206\u65b9\u6cd5\uff0c\u5c06\u5176\u5212\u5206\u4e3a\u66f4\u5c0f\u7684\u5b50\u6811\uff08\u5b50\u95ee\u9898\uff09\uff0c\u76f4\u81f3\u8fbe\u5230\u6700\u5c0f\u5b50\u95ee\u9898\uff08\u7a7a\u5b50\u6811\uff09\u65f6\u7ec8\u6b62\u3002
  • \u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff1a\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u662f\u76f8\u4e92\u72ec\u7acb\u7684\uff0c\u5b83\u4eec\u4e4b\u95f4\u6ca1\u6709\u4ea4\u96c6\u3002\u5728\u6784\u5efa\u5de6\u5b50\u6811\u65f6\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5173\u6ce8\u4e2d\u5e8f\u904d\u5386\u548c\u524d\u5e8f\u904d\u5386\u4e2d\u4e0e\u5de6\u5b50\u6811\u5bf9\u5e94\u7684\u90e8\u5206\u3002\u53f3\u5b50\u6811\u540c\u7406\u3002
  • \u5b50\u95ee\u9898\u7684\u89e3\u53ef\u4ee5\u5408\u5e76\uff1a\u4e00\u65e6\u5f97\u5230\u4e86\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\uff08\u5b50\u95ee\u9898\u7684\u89e3\uff09\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u5b83\u4eec\u94fe\u63a5\u5230\u6839\u8282\u70b9\u4e0a\uff0c\u5f97\u5230\u539f\u95ee\u9898\u7684\u89e3\u3002
"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#2","title":"2. \u00a0 \u5982\u4f55\u5212\u5206\u5b50\u6811","text":"

\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u8fd9\u9053\u9898\u662f\u53ef\u4ee5\u4f7f\u7528\u5206\u6cbb\u6765\u6c42\u89e3\u7684\uff0c\u4f46\u5982\u4f55\u901a\u8fc7\u524d\u5e8f\u904d\u5386 preorder \u548c\u4e2d\u5e8f\u904d\u5386 inorder \u6765\u5212\u5206\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u5462\uff1f

\u6839\u636e\u5b9a\u4e49\uff0cpreorder \u548c inorder \u90fd\u53ef\u4ee5\u88ab\u5212\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\u3002

  • \u524d\u5e8f\u904d\u5386\uff1a[ \u6839\u8282\u70b9 | \u5de6\u5b50\u6811 | \u53f3\u5b50\u6811 ] \uff0c\u4f8b\u5982\u56fe 12-5 \u7684\u6811\u5bf9\u5e94 [ 3 | 9 | 2 1 7 ] \u3002
  • \u4e2d\u5e8f\u904d\u5386\uff1a[ \u5de6\u5b50\u6811 | \u6839\u8282\u70b9 \uff5c \u53f3\u5b50\u6811 ] \uff0c\u4f8b\u5982\u56fe 12-5 \u7684\u6811\u5bf9\u5e94 [ 9 | 3 | 1 2 7 ] \u3002

\u4ee5\u4e0a\u56fe\u6570\u636e\u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u56fe 12-6 \u6240\u793a\u7684\u6b65\u9aa4\u5f97\u5230\u5212\u5206\u7ed3\u679c\u3002

  1. \u524d\u5e8f\u904d\u5386\u7684\u9996\u5143\u7d20 3 \u662f\u6839\u8282\u70b9\u7684\u503c\u3002
  2. \u67e5\u627e\u6839\u8282\u70b9 3 \u5728 inorder \u4e2d\u7684\u7d22\u5f15\uff0c\u5229\u7528\u8be5\u7d22\u5f15\u53ef\u5c06 inorder \u5212\u5206\u4e3a [ 9 | 3 \uff5c 1 2 7 ] \u3002
  3. \u6839\u636e inorder \u5212\u5206\u7ed3\u679c\uff0c\u6613\u5f97\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u7684\u8282\u70b9\u6570\u91cf\u5206\u522b\u4e3a 1 \u548c 3 \uff0c\u4ece\u800c\u53ef\u5c06 preorder \u5212\u5206\u4e3a [ 3 | 9 | 2 1 7 ] \u3002

\u56fe 12-6 \u00a0 \u5728\u524d\u5e8f\u548c\u4e2d\u5e8f\u904d\u5386\u4e2d\u5212\u5206\u5b50\u6811

"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#3","title":"3. \u00a0 \u57fa\u4e8e\u53d8\u91cf\u63cf\u8ff0\u5b50\u6811\u533a\u95f4","text":"

\u6839\u636e\u4ee5\u4e0a\u5212\u5206\u65b9\u6cd5\uff0c\u6211\u4eec\u5df2\u7ecf\u5f97\u5230\u6839\u8282\u70b9\u3001\u5de6\u5b50\u6811\u3001\u53f3\u5b50\u6811\u5728 preorder \u548c inorder \u4e2d\u7684\u7d22\u5f15\u533a\u95f4\u3002\u800c\u4e3a\u4e86\u63cf\u8ff0\u8fd9\u4e9b\u7d22\u5f15\u533a\u95f4\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u51e0\u4e2a\u6307\u9488\u53d8\u91cf\u3002

  • \u5c06\u5f53\u524d\u6811\u7684\u6839\u8282\u70b9\u5728 preorder \u4e2d\u7684\u7d22\u5f15\u8bb0\u4e3a \\(i\\) \u3002
  • \u5c06\u5f53\u524d\u6811\u7684\u6839\u8282\u70b9\u5728 inorder \u4e2d\u7684\u7d22\u5f15\u8bb0\u4e3a \\(m\\) \u3002
  • \u5c06\u5f53\u524d\u6811\u5728 inorder \u4e2d\u7684\u7d22\u5f15\u533a\u95f4\u8bb0\u4e3a \\([l, r]\\) \u3002

\u5982\u8868 12-1 \u6240\u793a\uff0c\u901a\u8fc7\u4ee5\u4e0a\u53d8\u91cf\u5373\u53ef\u8868\u793a\u6839\u8282\u70b9\u5728 preorder \u4e2d\u7684\u7d22\u5f15\uff0c\u4ee5\u53ca\u5b50\u6811\u5728 inorder \u4e2d\u7684\u7d22\u5f15\u533a\u95f4\u3002

\u8868 12-1 \u00a0 \u6839\u8282\u70b9\u548c\u5b50\u6811\u5728\u524d\u5e8f\u548c\u4e2d\u5e8f\u904d\u5386\u4e2d\u7684\u7d22\u5f15

\u6839\u8282\u70b9\u5728 preorder \u4e2d\u7684\u7d22\u5f15 \u5b50\u6811\u5728 inorder \u4e2d\u7684\u7d22\u5f15\u533a\u95f4 \u5f53\u524d\u6811 \\(i\\) \\([l, r]\\) \u5de6\u5b50\u6811 \\(i + 1\\) \\([l, m-1]\\) \u53f3\u5b50\u6811 \\(i + 1 + (m - l)\\) \\([m+1, r]\\)

\u8bf7\u6ce8\u610f\uff0c\u53f3\u5b50\u6811\u6839\u8282\u70b9\u7d22\u5f15\u4e2d\u7684 \\((m-l)\\) \u7684\u542b\u4e49\u662f\u201c\u5de6\u5b50\u6811\u7684\u8282\u70b9\u6570\u91cf\u201d\uff0c\u5efa\u8bae\u914d\u5408\u56fe 12-7 \u7406\u89e3\u3002

\u56fe 12-7 \u00a0 \u6839\u8282\u70b9\u548c\u5de6\u53f3\u5b50\u6811\u7684\u7d22\u5f15\u533a\u95f4\u8868\u793a

"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#4","title":"4. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u4e3a\u4e86\u63d0\u5347\u67e5\u8be2 \\(m\\) \u7684\u6548\u7387\uff0c\u6211\u4eec\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868 hmap \u6765\u5b58\u50a8\u6570\u7ec4 inorder \u4e2d\u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig build_tree.py
def dfs(\npreorder: list[int],\ninorder_map: dict[int, int],\ni: int,\nl: int,\nr: int,\n) -> TreeNode | None:\n\"\"\"\u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb\"\"\"\n# \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif r - l < 0:\nreturn None\n# \u521d\u59cb\u5316\u6839\u8282\u70b9\nroot = TreeNode(preorder[i])\n# \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nm = inorder_map[preorder[i]]\n# \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.left = dfs(preorder, inorder_map, i + 1, l, m - 1)\n# \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r)\n# \u8fd4\u56de\u6839\u8282\u70b9\nreturn root\ndef build_tree(preorder: list[int], inorder: list[int]) -> TreeNode | None:\n\"\"\"\u6784\u5efa\u4e8c\u53c9\u6811\"\"\"\n# \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\ninorder_map = {val: i for i, val in enumerate(inorder)}\nroot = dfs(preorder, inorder_map, 0, 0, len(inorder) - 1)\nreturn root\n
build_tree.cpp
/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode *dfs(vector<int> &preorder, unordered_map<int, int> &inorderMap, int i, int l, int r) {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif (r - l < 0)\nreturn NULL;\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nTreeNode *root = new TreeNode(preorder[i]);\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nint m = inorderMap[preorder[i]];\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot->left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot->right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n// \u8fd4\u56de\u6839\u8282\u70b9\nreturn root;\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\nunordered_map<int, int> inorderMap;\nfor (int i = 0; i < inorder.size(); i++) {\ninorderMap[inorder[i]] = i;\n}\nTreeNode *root = dfs(preorder, inorderMap, 0, 0, inorder.size() - 1);\nreturn root;\n}\n
build_tree.java
/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode dfs(int[] preorder, Map<Integer, Integer> inorderMap, int i, int l, int r) {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif (r - l < 0)\nreturn null;\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nTreeNode root = new TreeNode(preorder[i]);\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nint m = inorderMap.get(preorder[i]);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n// \u8fd4\u56de\u6839\u8282\u70b9\nreturn root;\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode buildTree(int[] preorder, int[] inorder) {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\nMap<Integer, Integer> inorderMap = new HashMap<>();\nfor (int i = 0; i < inorder.length; i++) {\ninorderMap.put(inorder[i], i);\n}\nTreeNode root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\nreturn root;\n}\n
build_tree.cs
/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode DFS(int[] preorder, Dictionary<int, int> inorderMap, int i, int l, int r) {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif (r - l < 0)\nreturn null;\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nTreeNode root = new(preorder[i]);\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nint m = inorderMap[preorder[i]];\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.left = DFS(preorder, inorderMap, i + 1, l, m - 1);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.right = DFS(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n// \u8fd4\u56de\u6839\u8282\u70b9\nreturn root;\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode BuildTree(int[] preorder, int[] inorder) {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\nDictionary<int, int> inorderMap = new();\nfor (int i = 0; i < inorder.Length; i++) {\ninorderMap.TryAdd(inorder[i], i);\n}\nTreeNode root = DFS(preorder, inorderMap, 0, 0, inorder.Length - 1);\nreturn root;\n}\n
build_tree.go
/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunc dfsBuildTree(preorder []int, inorderMap map[int]int, i, l, r int) *TreeNode {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif r-l < 0 {\nreturn nil\n}\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nroot := NewTreeNode(preorder[i])\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nm := inorderMap[preorder[i]]\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.Left = dfsBuildTree(preorder, inorderMap, i+1, l, m-1)\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.Right = dfsBuildTree(preorder, inorderMap, i+1+m-l, m+1, r)\n// \u8fd4\u56de\u6839\u8282\u70b9\nreturn root\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunc buildTree(preorder, inorder []int) *TreeNode {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\ninorderMap := make(map[int]int, len(inorder))\nfor i := 0; i < len(inorder); i++ {\ninorderMap[inorder[i]] = i\n}\nroot := dfsBuildTree(preorder, inorderMap, 0, 0, len(inorder)-1)\nreturn root\n}\n
build_tree.swift
/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunc dfs(preorder: [Int], inorderMap: [Int: Int], i: Int, l: Int, r: Int) -> TreeNode? {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif r - l < 0 {\nreturn nil\n}\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nlet root = TreeNode(x: preorder[i])\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nlet m = inorderMap[preorder[i]]!\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.left = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1, l: l, r: m - 1)\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.right = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1 + m - l, l: m + 1, r: r)\n// \u8fd4\u56de\u6839\u8282\u70b9\nreturn root\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunc buildTree(preorder: [Int], inorder: [Int]) -> TreeNode? {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\nlet inorderMap = inorder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }\nreturn dfs(preorder: preorder, inorderMap: inorderMap, i: 0, l: 0, r: inorder.count - 1)\n}\n
build_tree.js
/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunction dfs(preorder, inorderMap, i, l, r) {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif (r - l < 0) return null;\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nconst root = new TreeNode(preorder[i]);\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nconst m = inorderMap.get(preorder[i]);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n// \u8fd4\u56de\u6839\u8282\u70b9\nreturn root;\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunction buildTree(preorder, inorder) {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\nlet inorderMap = new Map();\nfor (let i = 0; i < inorder.length; i++) {\ninorderMap.set(inorder[i], i);\n}\nconst root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\nreturn root;\n}\n
build_tree.ts
/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunction dfs(\npreorder: number[],\ninorderMap: Map<number, number>,\ni: number,\nl: number,\nr: number\n): TreeNode | null {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif (r - l < 0) return null;\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nconst root: TreeNode = new TreeNode(preorder[i]);\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nconst m = inorderMap.get(preorder[i]);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n// \u8fd4\u56de\u6839\u8282\u70b9\nreturn root;\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunction buildTree(preorder: number[], inorder: number[]): TreeNode | null {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\nlet inorderMap = new Map<number, number>();\nfor (let i = 0; i < inorder.length; i++) {\ninorderMap.set(inorder[i], i);\n}\nconst root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\nreturn root;\n}\n
build_tree.dart
/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode? dfs(\nList<int> preorder,\nMap<int, int> inorderMap,\nint i,\nint l,\nint r,\n) {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif (r - l < 0) {\nreturn null;\n}\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nTreeNode? root = TreeNode(preorder[i]);\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nint m = inorderMap[preorder[i]]!;\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n// \u8fd4\u56de\u6839\u8282\u70b9\nreturn root;\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode? buildTree(List<int> preorder, List<int> inorder) {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\nMap<int, int> inorderMap = {};\nfor (int i = 0; i < inorder.length; i++) {\ninorderMap[inorder[i]] = i;\n}\nTreeNode? root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\nreturn root;\n}\n
build_tree.rs
/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfn dfs(preorder: &[i32], inorder_map: &HashMap<i32, i32>, i: i32, l: i32, r: i32) -> Option<Rc<RefCell<TreeNode>>> {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif r - l < 0 { return None; }\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nlet root = TreeNode::new(preorder[i as usize]);\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nlet m = inorder_map.get(&preorder[i as usize]).unwrap();\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.borrow_mut().left = dfs(preorder, inorder_map, i + 1, l, m - 1);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.borrow_mut().right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r);\n// \u8fd4\u56de\u6839\u8282\u70b9\nSome(root)\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfn build_tree(preorder: &[i32], inorder: &[i32]) -> Option<Rc<RefCell<TreeNode>>> {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\nlet mut inorder_map: HashMap<i32, i32> = HashMap::new();\nfor i in 0..inorder.len() {\ninorder_map.insert(inorder[i], i as i32);\n}\nlet root = dfs(preorder, &inorder_map, 0, 0, inorder.len() as i32 - 1);\nroot\n}\n
build_tree.c
[class]{}-[func]{dfs}\n[class]{}-[func]{buildTree}\n
build_tree.zig
[class]{}-[func]{dfs}\n[class]{}-[func]{buildTree}\n

\u56fe 12-8 \u5c55\u793a\u4e86\u6784\u5efa\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b\uff0c\u5404\u4e2a\u8282\u70b9\u662f\u5728\u5411\u4e0b\u201c\u9012\u201d\u7684\u8fc7\u7a0b\u4e2d\u5efa\u7acb\u7684\uff0c\u800c\u5404\u6761\u8fb9\uff08\u5373\u5f15\u7528\uff09\u662f\u5728\u5411\u4e0a\u201c\u5f52\u201d\u7684\u8fc7\u7a0b\u4e2d\u5efa\u7acb\u7684\u3002

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

\u56fe 12-8 \u00a0 \u6784\u5efa\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b

\u6bcf\u4e2a\u9012\u5f52\u51fd\u6570\u5185\u7684\u524d\u5e8f\u904d\u5386 preorder \u548c\u4e2d\u5e8f\u904d\u5386 inorder \u7684\u5212\u5206\u7ed3\u679c\u5982\u56fe 12-9 \u6240\u793a\u3002

\u56fe 12-9 \u00a0 \u6bcf\u4e2a\u9012\u5f52\u51fd\u6570\u4e2d\u7684\u5212\u5206\u7ed3\u679c

\u8bbe\u6811\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u521d\u59cb\u5316\u6bcf\u4e00\u4e2a\u8282\u70b9\uff08\u6267\u884c\u4e00\u4e2a\u9012\u5f52\u51fd\u6570 dfs() \uff09\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002\u56e0\u6b64\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

\u54c8\u5e0c\u8868\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u5373\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u94fe\u8868\u65f6\uff0c\u9012\u5f52\u6df1\u5ea6\u8fbe\u5230 \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u7684\u6808\u5e27\u7a7a\u95f4\u3002\u56e0\u6b64\u603b\u4f53\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

"},{"location":"chapter_divide_and_conquer/divide_and_conquer/","title":"12.1 \u00a0 \u5206\u6cbb\u7b97\u6cd5","text":"

\u300c\u5206\u6cbb divide and conquer\u300d\uff0c\u5168\u79f0\u5206\u800c\u6cbb\u4e4b\uff0c\u662f\u4e00\u79cd\u975e\u5e38\u91cd\u8981\u4e14\u5e38\u89c1\u7684\u7b97\u6cd5\u7b56\u7565\u3002\u5206\u6cbb\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u5b9e\u73b0\uff0c\u5305\u62ec\u201c\u5206\u201d\u548c\u201c\u6cbb\u201d\u4e24\u4e2a\u6b65\u9aa4\u3002

  1. \u5206\uff08\u5212\u5206\u9636\u6bb5\uff09\uff1a\u9012\u5f52\u5730\u5c06\u539f\u95ee\u9898\u5206\u89e3\u4e3a\u4e24\u4e2a\u6216\u591a\u4e2a\u5b50\u95ee\u9898\uff0c\u76f4\u81f3\u5230\u8fbe\u6700\u5c0f\u5b50\u95ee\u9898\u65f6\u7ec8\u6b62\u3002
  2. \u6cbb\uff08\u5408\u5e76\u9636\u6bb5\uff09\uff1a\u4ece\u5df2\u77e5\u89e3\u7684\u6700\u5c0f\u5b50\u95ee\u9898\u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5730\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u8fdb\u884c\u5408\u5e76\uff0c\u4ece\u800c\u6784\u5efa\u51fa\u539f\u95ee\u9898\u7684\u89e3\u3002

\u5982\u56fe 12-1 \u6240\u793a\uff0c\u201c\u5f52\u5e76\u6392\u5e8f\u201d\u662f\u5206\u6cbb\u7b56\u7565\u7684\u5178\u578b\u5e94\u7528\u4e4b\u4e00\u3002

  1. \u5206\uff1a\u9012\u5f52\u5730\u5c06\u539f\u6570\u7ec4\uff08\u539f\u95ee\u9898\uff09\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff08\u5b50\u95ee\u9898\uff09\uff0c\u76f4\u5230\u5b50\u6570\u7ec4\u53ea\u5269\u4e00\u4e2a\u5143\u7d20\uff08\u6700\u5c0f\u5b50\u95ee\u9898\uff09\u3002
  2. \u6cbb\uff1a\u4ece\u5e95\u81f3\u9876\u5730\u5c06\u6709\u5e8f\u7684\u5b50\u6570\u7ec4\uff08\u5b50\u95ee\u9898\u7684\u89e3\uff09\u8fdb\u884c\u5408\u5e76\uff0c\u4ece\u800c\u5f97\u5230\u6709\u5e8f\u7684\u539f\u6570\u7ec4\uff08\u539f\u95ee\u9898\u7684\u89e3\uff09\u3002

\u56fe 12-1 \u00a0 \u5f52\u5e76\u6392\u5e8f\u7684\u5206\u6cbb\u7b56\u7565

"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1211","title":"12.1.1 \u00a0 \u5982\u4f55\u5224\u65ad\u5206\u6cbb\u95ee\u9898","text":"

\u4e00\u4e2a\u95ee\u9898\u662f\u5426\u9002\u5408\u4f7f\u7528\u5206\u6cbb\u89e3\u51b3\uff0c\u901a\u5e38\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u51e0\u4e2a\u5224\u65ad\u4f9d\u636e\u3002

  1. \u95ee\u9898\u53ef\u4ee5\u88ab\u5206\u89e3\uff1a\u539f\u95ee\u9898\u53ef\u4ee5\u88ab\u5206\u89e3\u6210\u89c4\u6a21\u66f4\u5c0f\u3001\u7c7b\u4f3c\u7684\u5b50\u95ee\u9898\uff0c\u4ee5\u53ca\u80fd\u591f\u4ee5\u76f8\u540c\u65b9\u5f0f\u9012\u5f52\u5730\u8fdb\u884c\u5212\u5206\u3002
  2. \u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff1a\u5b50\u95ee\u9898\u4e4b\u95f4\u662f\u6ca1\u6709\u91cd\u53e0\u7684\uff0c\u4e92\u76f8\u6ca1\u6709\u4f9d\u8d56\uff0c\u53ef\u4ee5\u88ab\u72ec\u7acb\u89e3\u51b3\u3002
  3. \u5b50\u95ee\u9898\u7684\u89e3\u53ef\u4ee5\u88ab\u5408\u5e76\uff1a\u539f\u95ee\u9898\u7684\u89e3\u901a\u8fc7\u5408\u5e76\u5b50\u95ee\u9898\u7684\u89e3\u5f97\u6765\u3002

\u663e\u7136\uff0c\u5f52\u5e76\u6392\u5e8f\u662f\u6ee1\u8db3\u4ee5\u4e0a\u4e09\u6761\u5224\u65ad\u4f9d\u636e\u7684\u3002

  1. \u95ee\u9898\u53ef\u4ee5\u88ab\u5206\u89e3\uff1a\u9012\u5f52\u5730\u5c06\u6570\u7ec4\uff08\u539f\u95ee\u9898\uff09\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff08\u5b50\u95ee\u9898\uff09\u3002
  2. \u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff1a\u6bcf\u4e2a\u5b50\u6570\u7ec4\u90fd\u53ef\u4ee5\u72ec\u7acb\u5730\u8fdb\u884c\u6392\u5e8f\uff08\u5b50\u95ee\u9898\u53ef\u4ee5\u72ec\u7acb\u8fdb\u884c\u6c42\u89e3\uff09\u3002
  3. \u5b50\u95ee\u9898\u7684\u89e3\u53ef\u4ee5\u88ab\u5408\u5e76\uff1a\u4e24\u4e2a\u6709\u5e8f\u5b50\u6570\u7ec4\uff08\u5b50\u95ee\u9898\u7684\u89e3\uff09\u53ef\u4ee5\u88ab\u5408\u5e76\u4e3a\u4e00\u4e2a\u6709\u5e8f\u6570\u7ec4\uff08\u539f\u95ee\u9898\u7684\u89e3\uff09\u3002
"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1212","title":"12.1.2 \u00a0 \u901a\u8fc7\u5206\u6cbb\u63d0\u5347\u6548\u7387","text":"

\u5206\u6cbb\u4e0d\u4ec5\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u51b3\u7b97\u6cd5\u95ee\u9898\uff0c\u5f80\u5f80\u8fd8\u53ef\u4ee5\u5e26\u6765\u7b97\u6cd5\u6548\u7387\u7684\u63d0\u5347\u3002\u5728\u6392\u5e8f\u7b97\u6cd5\u4e2d\uff0c\u5feb\u901f\u6392\u5e8f\u3001\u5f52\u5e76\u6392\u5e8f\u3001\u5806\u6392\u5e8f\u76f8\u8f83\u4e8e\u9009\u62e9\u3001\u5192\u6ce1\u3001\u63d2\u5165\u6392\u5e8f\u66f4\u5feb\uff0c\u5c31\u662f\u56e0\u4e3a\u5b83\u4eec\u5e94\u7528\u4e86\u5206\u6cbb\u7b56\u7565\u3002

\u90a3\u4e48\uff0c\u6211\u4eec\u4e0d\u7981\u53d1\u95ee\uff1a\u4e3a\u4ec0\u4e48\u5206\u6cbb\u53ef\u4ee5\u63d0\u5347\u7b97\u6cd5\u6548\u7387\uff0c\u5176\u5e95\u5c42\u903b\u8f91\u662f\u4ec0\u4e48\uff1f\u6362\u53e5\u8bdd\u8bf4\uff0c\u5c06\u5927\u95ee\u9898\u5206\u89e3\u4e3a\u591a\u4e2a\u5b50\u95ee\u9898\u3001\u89e3\u51b3\u5b50\u95ee\u9898\u3001\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u5408\u5e76\u4e3a\u539f\u95ee\u9898\u7684\u89e3\uff0c\u8fd9\u51e0\u6b65\u7684\u6548\u7387\u4e3a\u4ec0\u4e48\u6bd4\u76f4\u63a5\u89e3\u51b3\u539f\u95ee\u9898\u7684\u6548\u7387\u66f4\u9ad8\uff1f\u8fd9\u4e2a\u95ee\u9898\u53ef\u4ee5\u4ece\u64cd\u4f5c\u6570\u91cf\u548c\u5e76\u884c\u8ba1\u7b97\u4e24\u65b9\u9762\u6765\u8ba8\u8bba\u3002

"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1","title":"1. \u00a0 \u64cd\u4f5c\u6570\u91cf\u4f18\u5316","text":"

\u4ee5\u201c\u5192\u6ce1\u6392\u5e8f\u201d\u4e3a\u4f8b\uff0c\u5176\u5904\u7406\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\u9700\u8981 \\(O(n^2)\\) \u65f6\u95f4\u3002\u5047\u8bbe\u6211\u4eec\u6309\u7167\u56fe 12-2 \u6240\u793a\u7684\u65b9\u5f0f\uff0c\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u5219\u5212\u5206\u9700\u8981 \\(O(n)\\) \u65f6\u95f4\uff0c\u6392\u5e8f\u6bcf\u4e2a\u5b50\u6570\u7ec4\u9700\u8981 \\(O((n / 2)^2)\\) \u65f6\u95f4\uff0c\u5408\u5e76\u4e24\u4e2a\u5b50\u6570\u7ec4\u9700\u8981 \\(O(n)\\) \u65f6\u95f4\uff0c\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\uff1a

\\[ O(n + (\\frac{n}{2})^2 \\times 2 + n) = O(\\frac{n^2}{2} + 2n) \\]

\u56fe 12-2 \u00a0 \u5212\u5206\u6570\u7ec4\u524d\u540e\u7684\u5192\u6ce1\u6392\u5e8f

\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8ba1\u7b97\u4ee5\u4e0b\u4e0d\u7b49\u5f0f\uff0c\u5176\u5de6\u8fb9\u548c\u53f3\u8fb9\u5206\u522b\u4e3a\u5212\u5206\u524d\u548c\u5212\u5206\u540e\u7684\u64cd\u4f5c\u603b\u6570\uff1a

\\[ \\begin{aligned} n^2 & > \\frac{n^2}{2} + 2n \\newline n^2 - \\frac{n^2}{2} - 2n & > 0 \\newline n(n - 4) & > 0 \\end{aligned} \\]

\u8fd9\u610f\u5473\u7740\u5f53 \\(n > 4\\) \u65f6\uff0c\u5212\u5206\u540e\u7684\u64cd\u4f5c\u6570\u91cf\u66f4\u5c11\uff0c\u6392\u5e8f\u6548\u7387\u5e94\u8be5\u66f4\u9ad8\u3002\u8bf7\u6ce8\u610f\uff0c\u5212\u5206\u540e\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4ecd\u7136\u662f\u5e73\u65b9\u9636 \\(O(n^2)\\) \uff0c\u53ea\u662f\u590d\u6742\u5ea6\u4e2d\u7684\u5e38\u6570\u9879\u53d8\u5c0f\u4e86\u3002

\u8fdb\u4e00\u6b65\u60f3\uff0c\u5982\u679c\u6211\u4eec\u628a\u5b50\u6570\u7ec4\u4e0d\u65ad\u5730\u518d\u4ece\u4e2d\u70b9\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u76f4\u81f3\u5b50\u6570\u7ec4\u53ea\u5269\u4e00\u4e2a\u5143\u7d20\u65f6\u505c\u6b62\u5212\u5206\u5462\uff1f\u8fd9\u79cd\u601d\u8def\u5b9e\u9645\u4e0a\u5c31\u662f\u201c\u5f52\u5e76\u6392\u5e8f\u201d\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002

\u518d\u601d\u8003\uff0c\u5982\u679c\u6211\u4eec\u591a\u8bbe\u7f6e\u51e0\u4e2a\u5212\u5206\u70b9\uff0c\u5c06\u539f\u6570\u7ec4\u5e73\u5747\u5212\u5206\u4e3a \\(k\\) \u4e2a\u5b50\u6570\u7ec4\u5462\uff1f\u8fd9\u79cd\u60c5\u51b5\u4e0e\u201c\u6876\u6392\u5e8f\u201d\u975e\u5e38\u7c7b\u4f3c\uff0c\u5b83\u975e\u5e38\u9002\u5408\u6392\u5e8f\u6d77\u91cf\u6570\u636e\uff0c\u7406\u8bba\u4e0a\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u8fbe\u5230 \\(O(n + k)\\) \u3002

"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#2","title":"2. \u00a0 \u5e76\u884c\u8ba1\u7b97\u4f18\u5316","text":"

\u6211\u4eec\u77e5\u9053\uff0c\u5206\u6cbb\u751f\u6210\u7684\u5b50\u95ee\u9898\u662f\u76f8\u4e92\u72ec\u7acb\u7684\uff0c\u56e0\u6b64\u901a\u5e38\u53ef\u4ee5\u5e76\u884c\u89e3\u51b3\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5206\u6cbb\u4e0d\u4ec5\u53ef\u4ee5\u964d\u4f4e\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u8fd8\u6709\u5229\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u7684\u5e76\u884c\u4f18\u5316\u3002

\u5e76\u884c\u4f18\u5316\u5728\u591a\u6838\u6216\u591a\u5904\u7406\u5668\u7684\u73af\u5883\u4e2d\u5c24\u5176\u6709\u6548\uff0c\u56e0\u4e3a\u7cfb\u7edf\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u591a\u4e2a\u5b50\u95ee\u9898\uff0c\u66f4\u52a0\u5145\u5206\u5730\u5229\u7528\u8ba1\u7b97\u8d44\u6e90\uff0c\u4ece\u800c\u663e\u8457\u51cf\u5c11\u603b\u4f53\u7684\u8fd0\u884c\u65f6\u95f4\u3002

\u6bd4\u5982\u5728\u56fe 12-3 \u6240\u793a\u7684\u201c\u6876\u6392\u5e8f\u201d\u4e2d\uff0c\u6211\u4eec\u5c06\u6d77\u91cf\u7684\u6570\u636e\u5e73\u5747\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\uff0c\u5219\u53ef\u6240\u6709\u6876\u7684\u6392\u5e8f\u4efb\u52a1\u5206\u6563\u5230\u5404\u4e2a\u8ba1\u7b97\u5355\u5143\uff0c\u5b8c\u6210\u540e\u518d\u8fdb\u884c\u7ed3\u679c\u5408\u5e76\u3002

\u56fe 12-3 \u00a0 \u6876\u6392\u5e8f\u7684\u5e76\u884c\u8ba1\u7b97

"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1213","title":"12.1.3 \u00a0 \u5206\u6cbb\u5e38\u89c1\u5e94\u7528","text":"

\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u53ef\u4ee5\u7528\u6765\u89e3\u51b3\u8bb8\u591a\u7ecf\u5178\u7b97\u6cd5\u95ee\u9898\u3002

  • \u5bfb\u627e\u6700\u8fd1\u70b9\u5bf9\uff1a\u8be5\u7b97\u6cd5\u9996\u5148\u5c06\u70b9\u96c6\u5206\u6210\u4e24\u90e8\u5206\uff0c\u7136\u540e\u5206\u522b\u627e\u51fa\u4e24\u90e8\u5206\u4e2d\u7684\u6700\u8fd1\u70b9\u5bf9\uff0c\u6700\u540e\u518d\u627e\u51fa\u8de8\u8d8a\u4e24\u90e8\u5206\u7684\u6700\u8fd1\u70b9\u5bf9\u3002
  • \u5927\u6574\u6570\u4e58\u6cd5\uff1a\u4f8b\u5982 Karatsuba \u7b97\u6cd5\uff0c\u5b83\u662f\u5c06\u5927\u6574\u6570\u4e58\u6cd5\u5206\u89e3\u4e3a\u51e0\u4e2a\u8f83\u5c0f\u7684\u6574\u6570\u7684\u4e58\u6cd5\u548c\u52a0\u6cd5\u3002
  • \u77e9\u9635\u4e58\u6cd5\uff1a\u4f8b\u5982 Strassen \u7b97\u6cd5\uff0c\u5b83\u662f\u5c06\u5927\u77e9\u9635\u4e58\u6cd5\u5206\u89e3\u4e3a\u591a\u4e2a\u5c0f\u77e9\u9635\u7684\u4e58\u6cd5\u548c\u52a0\u6cd5\u3002
  • \u6c49\u8bfa\u5854\u95ee\u9898\uff1a\u6c49\u8bfa\u5854\u95ee\u9898\u53ef\u4ee5\u89c6\u4e3a\u5178\u578b\u7684\u5206\u6cbb\u7b56\u7565\uff0c\u901a\u8fc7\u9012\u5f52\u89e3\u51b3\u3002
  • \u6c42\u89e3\u9006\u5e8f\u5bf9\uff1a\u5728\u4e00\u4e2a\u5e8f\u5217\u4e2d\uff0c\u5982\u679c\u524d\u9762\u7684\u6570\u5b57\u5927\u4e8e\u540e\u9762\u7684\u6570\u5b57\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u6570\u5b57\u6784\u6210\u4e00\u4e2a\u9006\u5e8f\u5bf9\u3002\u6c42\u89e3\u9006\u5e8f\u5bf9\u95ee\u9898\u53ef\u4ee5\u901a\u8fc7\u5206\u6cbb\u7684\u601d\u60f3\uff0c\u501f\u52a9\u5f52\u5e76\u6392\u5e8f\u8fdb\u884c\u6c42\u89e3\u3002

\u53e6\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u5728\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u7684\u8bbe\u8ba1\u4e2d\u5e94\u7528\u975e\u5e38\u5e7f\u6cdb\u3002

  • \u4e8c\u5206\u67e5\u627e\uff1a\u4e8c\u5206\u67e5\u627e\u662f\u5c06\u6709\u5e8f\u6570\u7ec4\u4ece\u4e2d\u70b9\u7d22\u5f15\u5206\u4e3a\u4e24\u90e8\u5206\uff0c\u7136\u540e\u6839\u636e\u76ee\u6807\u503c\u4e0e\u4e2d\u95f4\u5143\u7d20\u503c\u6bd4\u8f83\u7ed3\u679c\uff0c\u51b3\u5b9a\u6392\u9664\u54ea\u4e00\u534a\u533a\u95f4\uff0c\u7136\u540e\u5728\u5269\u4f59\u533a\u95f4\u6267\u884c\u76f8\u540c\u7684\u4e8c\u5206\u64cd\u4f5c\u3002
  • \u5f52\u5e76\u6392\u5e8f\uff1a\u6587\u7ae0\u5f00\u5934\u5df2\u4ecb\u7ecd\uff0c\u4e0d\u518d\u8d58\u8ff0\u3002
  • \u5feb\u901f\u6392\u5e8f\uff1a\u5feb\u901f\u6392\u5e8f\u662f\u9009\u53d6\u4e00\u4e2a\u57fa\u51c6\u503c\uff0c\u7136\u540e\u628a\u6570\u7ec4\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u4e00\u4e2a\u5b50\u6570\u7ec4\u7684\u5143\u7d20\u6bd4\u57fa\u51c6\u503c\u5c0f\uff0c\u53e6\u4e00\u5b50\u6570\u7ec4\u7684\u5143\u7d20\u6bd4\u57fa\u51c6\u503c\u5927\uff0c\u7136\u540e\u518d\u5bf9\u8fd9\u4e24\u90e8\u5206\u8fdb\u884c\u76f8\u540c\u7684\u5212\u5206\u64cd\u4f5c\uff0c\u76f4\u81f3\u5b50\u6570\u7ec4\u53ea\u5269\u4e0b\u4e00\u4e2a\u5143\u7d20\u3002
  • \u6876\u6392\u5e8f\uff1a\u6876\u6392\u5e8f\u7684\u57fa\u672c\u601d\u60f3\u662f\u5c06\u6570\u636e\u5206\u6563\u5230\u591a\u4e2a\u6876\uff0c\u7136\u540e\u5bf9\u6bcf\u4e2a\u6876\u5185\u7684\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\uff0c\u6700\u540e\u5c06\u5404\u4e2a\u6876\u7684\u5143\u7d20\u4f9d\u6b21\u53d6\u51fa\uff0c\u4ece\u800c\u5f97\u5230\u4e00\u4e2a\u6709\u5e8f\u6570\u7ec4\u3002
  • \u6811\uff1a\u4f8b\u5982\u4e8c\u53c9\u641c\u7d22\u6811\u3001AVL \u6811\u3001\u7ea2\u9ed1\u6811\u3001B \u6811\u3001B+ \u6811\u7b49\uff0c\u5b83\u4eec\u7684\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u7b49\u64cd\u4f5c\u90fd\u53ef\u4ee5\u89c6\u4e3a\u5206\u6cbb\u7684\u5e94\u7528\u3002
  • \u5806\uff1a\u5806\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u5176\u5404\u79cd\u64cd\u4f5c\uff0c\u5982\u63d2\u5165\u3001\u5220\u9664\u548c\u5806\u5316\uff0c\u5b9e\u9645\u4e0a\u90fd\u9690\u542b\u4e86\u5206\u6cbb\u7684\u601d\u60f3\u3002
  • \u54c8\u5e0c\u8868\uff1a\u867d\u7136\u54c8\u5e0c\u8868\u6765\u5e76\u4e0d\u76f4\u63a5\u5e94\u7528\u5206\u6cbb\uff0c\u4f46\u67d0\u4e9b\u54c8\u5e0c\u51b2\u7a81\u89e3\u51b3\u7b56\u7565\u95f4\u63a5\u5e94\u7528\u4e86\u5206\u6cbb\u7b56\u7565\uff0c\u4f8b\u5982\uff0c\u94fe\u5f0f\u5730\u5740\u4e2d\u7684\u957f\u94fe\u8868\u4f1a\u88ab\u8f6c\u5316\u4e3a\u7ea2\u9ed1\u6811\uff0c\u4ee5\u63d0\u5347\u67e5\u8be2\u6548\u7387\u3002

\u53ef\u4ee5\u770b\u51fa\uff0c\u5206\u6cbb\u662f\u4e00\u79cd\u201c\u6da6\u7269\u7ec6\u65e0\u58f0\u201d\u7684\u7b97\u6cd5\u601d\u60f3\uff0c\u9690\u542b\u5728\u5404\u79cd\u7b97\u6cd5\u4e0e\u6570\u636e\u7ed3\u6784\u4e4b\u4e2d\u3002

"},{"location":"chapter_divide_and_conquer/hanota_problem/","title":"12.4 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898","text":"

\u5728\u5f52\u5e76\u6392\u5e8f\u548c\u6784\u5efa\u4e8c\u53c9\u6811\u4e2d\uff0c\u6211\u4eec\u90fd\u662f\u5c06\u539f\u95ee\u9898\u5206\u89e3\u4e3a\u4e24\u4e2a\u89c4\u6a21\u4e3a\u539f\u95ee\u9898\u4e00\u534a\u7684\u5b50\u95ee\u9898\u3002\u7136\u800c\u5bf9\u4e8e\u6c49\u8bfa\u5854\u95ee\u9898\uff0c\u6211\u4eec\u91c7\u7528\u4e0d\u540c\u7684\u5206\u89e3\u7b56\u7565\u3002

Question

\u7ed9\u5b9a\u4e09\u6839\u67f1\u5b50\uff0c\u8bb0\u4e3a A\u3001B \u548c C \u3002\u8d77\u59cb\u72b6\u6001\u4e0b\uff0c\u67f1\u5b50 A \u4e0a\u5957\u7740 \\(n\\) \u4e2a\u5706\u76d8\uff0c\u5b83\u4eec\u4ece\u4e0a\u5230\u4e0b\u6309\u7167\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u6392\u5217\u3002\u6211\u4eec\u7684\u4efb\u52a1\u662f\u8981\u628a\u8fd9 \\(n\\) \u4e2a\u5706\u76d8\u79fb\u5230\u67f1\u5b50 C \u4e0a\uff0c\u5e76\u4fdd\u6301\u5b83\u4eec\u7684\u539f\u6709\u987a\u5e8f\u4e0d\u53d8\u3002\u5728\u79fb\u52a8\u5706\u76d8\u7684\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u9075\u5b88\u4ee5\u4e0b\u89c4\u5219\u3002

  1. \u5706\u76d8\u53ea\u80fd\u4ece\u4e00\u4e2a\u67f1\u5b50\u9876\u90e8\u62ff\u51fa\uff0c\u4ece\u53e6\u4e00\u4e2a\u67f1\u5b50\u9876\u90e8\u653e\u5165\u3002
  2. \u6bcf\u6b21\u53ea\u80fd\u79fb\u52a8\u4e00\u4e2a\u5706\u76d8\u3002
  3. \u5c0f\u5706\u76d8\u5fc5\u987b\u65f6\u523b\u4f4d\u4e8e\u5927\u5706\u76d8\u4e4b\u4e0a\u3002

\u56fe 12-10 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898\u793a\u4f8b

\u6211\u4eec\u5c06\u89c4\u6a21\u4e3a \\(i\\) \u7684\u6c49\u8bfa\u5854\u95ee\u9898\u8bb0\u505a \\(f(i)\\) \u3002\u4f8b\u5982 \\(f(3)\\) \u4ee3\u8868\u5c06 \\(3\\) \u4e2a\u5706\u76d8\u4ece A \u79fb\u52a8\u81f3 C \u7684\u6c49\u8bfa\u5854\u95ee\u9898\u3002

"},{"location":"chapter_divide_and_conquer/hanota_problem/#1","title":"1. \u00a0 \u8003\u8651\u57fa\u672c\u60c5\u51b5","text":"

\u5982\u56fe 12-11 \u6240\u793a\uff0c\u5bf9\u4e8e\u95ee\u9898 \\(f(1)\\) \uff0c\u5373\u5f53\u53ea\u6709\u4e00\u4e2a\u5706\u76d8\u65f6\uff0c\u6211\u4eec\u5c06\u5b83\u76f4\u63a5\u4ece A \u79fb\u52a8\u81f3 C \u5373\u53ef\u3002

<1><2>

\u56fe 12-11 \u00a0 \u89c4\u6a21\u4e3a 1 \u95ee\u9898\u7684\u89e3

\u5982\u56fe 12-12 \u6240\u793a\uff0c\u5bf9\u4e8e\u95ee\u9898 \\(f(2)\\) \uff0c\u5373\u5f53\u6709\u4e24\u4e2a\u5706\u76d8\u65f6\uff0c\u7531\u4e8e\u8981\u65f6\u523b\u6ee1\u8db3\u5c0f\u5706\u76d8\u5728\u5927\u5706\u76d8\u4e4b\u4e0a\uff0c\u56e0\u6b64\u9700\u8981\u501f\u52a9 B \u6765\u5b8c\u6210\u79fb\u52a8\u3002

  1. \u5148\u5c06\u4e0a\u9762\u7684\u5c0f\u5706\u76d8\u4ece A \u79fb\u81f3 B \u3002
  2. \u518d\u5c06\u5927\u5706\u76d8\u4ece A \u79fb\u81f3 C \u3002
  3. \u6700\u540e\u5c06\u5c0f\u5706\u76d8\u4ece B \u79fb\u81f3 C \u3002
<1><2><3><4>

\u56fe 12-12 \u00a0 \u89c4\u6a21\u4e3a 2 \u95ee\u9898\u7684\u89e3

\u89e3\u51b3\u95ee\u9898 \\(f(2)\\) \u7684\u8fc7\u7a0b\u53ef\u603b\u7ed3\u4e3a\uff1a\u5c06\u4e24\u4e2a\u5706\u76d8\u501f\u52a9 B \u4ece A \u79fb\u81f3 C \u3002\u5176\u4e2d\uff0cC \u79f0\u4e3a\u76ee\u6807\u67f1\u3001B \u79f0\u4e3a\u7f13\u51b2\u67f1\u3002

"},{"location":"chapter_divide_and_conquer/hanota_problem/#2","title":"2. \u00a0 \u5b50\u95ee\u9898\u5206\u89e3","text":"

\u5bf9\u4e8e\u95ee\u9898 \\(f(3)\\) \uff0c\u5373\u5f53\u6709\u4e09\u4e2a\u5706\u76d8\u65f6\uff0c\u60c5\u51b5\u53d8\u5f97\u7a0d\u5fae\u590d\u6742\u4e86\u4e00\u4e9b\u3002

\u56e0\u4e3a\u5df2\u77e5 \\(f(1)\\) \u548c \\(f(2)\\) \u7684\u89e3\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ece\u5206\u6cbb\u89d2\u5ea6\u601d\u8003\uff0c\u5c06 A \u9876\u90e8\u7684\u4e24\u4e2a\u5706\u76d8\u770b\u505a\u4e00\u4e2a\u6574\u4f53\uff0c\u6267\u884c\u56fe 12-13 \u6240\u793a\u7684\u6b65\u9aa4\u3002\u8fd9\u6837\u4e09\u4e2a\u5706\u76d8\u5c31\u88ab\u987a\u5229\u5730\u4ece A \u79fb\u52a8\u81f3 C \u4e86\u3002

  1. \u4ee4 B \u4e3a\u76ee\u6807\u67f1\u3001C \u4e3a\u7f13\u51b2\u67f1\uff0c\u5c06\u4e24\u4e2a\u5706\u76d8\u4ece A \u79fb\u52a8\u81f3 B \u3002
  2. \u5c06 A \u4e2d\u5269\u4f59\u7684\u4e00\u4e2a\u5706\u76d8\u4ece A \u76f4\u63a5\u79fb\u52a8\u81f3 C \u3002
  3. \u4ee4 C \u4e3a\u76ee\u6807\u67f1\u3001A \u4e3a\u7f13\u51b2\u67f1\uff0c\u5c06\u4e24\u4e2a\u5706\u76d8\u4ece B \u79fb\u52a8\u81f3 C \u3002
<1><2><3><4>

\u56fe 12-13 \u00a0 \u89c4\u6a21\u4e3a 3 \u95ee\u9898\u7684\u89e3

\u672c\u8d28\u4e0a\u770b\uff0c\u6211\u4eec\u5c06\u95ee\u9898 \\(f(3)\\) \u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u95ee\u9898 \\(f(2)\\) \u548c\u5b50\u95ee\u9898 \\(f(1)\\) \u3002\u6309\u987a\u5e8f\u89e3\u51b3\u8fd9\u4e09\u4e2a\u5b50\u95ee\u9898\u4e4b\u540e\uff0c\u539f\u95ee\u9898\u968f\u4e4b\u5f97\u5230\u89e3\u51b3\u3002\u8fd9\u8bf4\u660e\u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff0c\u800c\u4e14\u89e3\u662f\u53ef\u4ee5\u5408\u5e76\u7684\u3002

\u81f3\u6b64\uff0c\u6211\u4eec\u53ef\u603b\u7ed3\u51fa\u56fe 12-14 \u6240\u793a\u7684\u6c49\u8bfa\u5854\u95ee\u9898\u7684\u5206\u6cbb\u7b56\u7565\uff1a\u5c06\u539f\u95ee\u9898 \\(f(n)\\) \u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u95ee\u9898 \\(f(n-1)\\) \u548c\u4e00\u4e2a\u5b50\u95ee\u9898 \\(f(1)\\) \uff0c\u5e76\u6309\u7167\u4ee5\u4e0b\u987a\u5e8f\u89e3\u51b3\u8fd9\u4e09\u4e2a\u5b50\u95ee\u9898\u3002

  1. \u5c06 \\(n-1\\) \u4e2a\u5706\u76d8\u501f\u52a9 C \u4ece A \u79fb\u81f3 B \u3002
  2. \u5c06\u5269\u4f59 \\(1\\) \u4e2a\u5706\u76d8\u4ece A \u76f4\u63a5\u79fb\u81f3 C \u3002
  3. \u5c06 \\(n-1\\) \u4e2a\u5706\u76d8\u501f\u52a9 A \u4ece B \u79fb\u81f3 C \u3002

\u5bf9\u4e8e\u8fd9\u4e24\u4e2a\u5b50\u95ee\u9898 \\(f(n-1)\\) \uff0c\u53ef\u4ee5\u901a\u8fc7\u76f8\u540c\u7684\u65b9\u5f0f\u8fdb\u884c\u9012\u5f52\u5212\u5206\uff0c\u76f4\u81f3\u8fbe\u5230\u6700\u5c0f\u5b50\u95ee\u9898 \\(f(1)\\) \u3002\u800c \\(f(1)\\) \u7684\u89e3\u662f\u5df2\u77e5\u7684\uff0c\u53ea\u9700\u4e00\u6b21\u79fb\u52a8\u64cd\u4f5c\u5373\u53ef\u3002

\u56fe 12-14 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898\u7684\u5206\u6cbb\u7b56\u7565

"},{"location":"chapter_divide_and_conquer/hanota_problem/#3","title":"3. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u5728\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u9012\u5f52\u51fd\u6570 dfs(i, src, buf, tar) \uff0c\u5b83\u7684\u4f5c\u7528\u662f\u5c06\u67f1 src \u9876\u90e8\u7684 \\(i\\) \u4e2a\u5706\u76d8\u501f\u52a9\u7f13\u51b2\u67f1 buf \u79fb\u52a8\u81f3\u76ee\u6807\u67f1 tar \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig hanota.py
def move(src: list[int], tar: list[int]):\n\"\"\"\u79fb\u52a8\u4e00\u4e2a\u5706\u76d8\"\"\"\n# \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\npan = src.pop()\n# \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.append(pan)\ndef dfs(i: int, src: list[int], buf: list[int], tar: list[int]):\n\"\"\"\u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i)\"\"\"\n# \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif i == 1:\nmove(src, tar)\nreturn\n# \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i - 1, src, tar, buf)\n# \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src, tar)\n# \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i - 1, buf, src, tar)\ndef solve_hanota(A: list[int], B: list[int], C: list[int]):\n\"\"\"\u6c42\u89e3\u6c49\u8bfa\u5854\"\"\"\nn = len(A)\n# \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(n, A, B, C)\n
hanota.cpp
/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(vector<int> &src, vector<int> &tar) {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\nint pan = src.back();\nsrc.pop_back();\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.push_back(pan);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nvoid dfs(int i, vector<int> &src, vector<int> &buf, vector<int> &tar) {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif (i == 1) {\nmove(src, tar);\nreturn;\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i - 1, src, tar, buf);\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src, tar);\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i - 1, buf, src, tar);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nvoid solveHanota(vector<int> &A, vector<int> &B, vector<int> &C) {\nint n = A.size();\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(n, A, B, C);\n}\n
hanota.java
/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(List<Integer> src, List<Integer> tar) {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\nInteger pan = src.remove(src.size() - 1);\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.add(pan);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nvoid dfs(int i, List<Integer> src, List<Integer> buf, List<Integer> tar) {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif (i == 1) {\nmove(src, tar);\nreturn;\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i - 1, src, tar, buf);\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src, tar);\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i - 1, buf, src, tar);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nvoid solveHanota(List<Integer> A, List<Integer> B, List<Integer> C) {\nint n = A.size();\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(n, A, B, C);\n}\n
hanota.cs
/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid Move(List<int> src, List<int> tar) {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\nint pan = src[^1];\nsrc.RemoveAt(src.Count - 1);\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.Add(pan);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nvoid DFS(int i, List<int> src, List<int> buf, List<int> tar) {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif (i == 1) {\nMove(src, tar);\nreturn;\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\nDFS(i - 1, src, tar, buf);\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nMove(src, tar);\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\nDFS(i - 1, buf, src, tar);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nvoid SolveHanota(List<int> A, List<int> B, List<int> C) {\nint n = A.Count;\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\nDFS(n, A, B, C);\n}\n
hanota.go
/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunc move(src, tar *list.List) {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\npan := src.Back()\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.PushBack(pan.Value)\n// \u79fb\u9664 src \u9876\u90e8\u5706\u76d8\nsrc.Remove(pan)\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nfunc dfsHanota(i int, src, buf, tar *list.List) {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif i == 1 {\nmove(src, tar)\nreturn\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfsHanota(i-1, src, tar, buf)\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src, tar)\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfsHanota(i-1, buf, src, tar)\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nfunc solveHanota(A, B, C *list.List) {\nn := A.Len()\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfsHanota(n, A, B, C)\n}\n
hanota.swift
/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunc move(src: inout [Int], tar: inout [Int]) {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\nlet pan = src.popLast()!\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.append(pan)\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nfunc dfs(i: Int, src: inout [Int], buf: inout [Int], tar: inout [Int]) {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif i == 1 {\nmove(src: &src, tar: &tar)\nreturn\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i: i - 1, src: &src, buf: &tar, tar: &buf)\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src: &src, tar: &tar)\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i: i - 1, src: &buf, buf: &src, tar: &tar)\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nfunc solveHanota(A: inout [Int], B: inout [Int], C: inout [Int]) {\nlet n = A.count\n// \u5217\u8868\u5c3e\u90e8\u662f\u67f1\u5b50\u9876\u90e8\n// \u5c06 src \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(i: n, src: &A, buf: &B, tar: &C)\n}\n
hanota.js
/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunction move(src, tar) {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\nconst pan = src.pop();\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.push(pan);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nfunction dfs(i, src, buf, tar) {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif (i === 1) {\nmove(src, tar);\nreturn;\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i - 1, src, tar, buf);\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src, tar);\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i - 1, buf, src, tar);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nfunction solveHanota(A, B, C) {\nconst n = A.length;\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(n, A, B, C);\n}\n
hanota.ts
/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunction move(src: number[], tar: number[]): void {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\nconst pan = src.pop();\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.push(pan);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nfunction dfs(i: number, src: number[], buf: number[], tar: number[]): void {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif (i === 1) {\nmove(src, tar);\nreturn;\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i - 1, src, tar, buf);\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src, tar);\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i - 1, buf, src, tar);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nfunction solveHanota(A: number[], B: number[], C: number[]): void {\nconst n = A.length;\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(n, A, B, C);\n}\n
hanota.dart
/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(List<int> src, List<int> tar) {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\nint pan = src.removeLast();\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.add(pan);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nvoid dfs(int i, List<int> src, List<int> buf, List<int> tar) {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif (i == 1) {\nmove(src, tar);\nreturn;\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i - 1, src, tar, buf);\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src, tar);\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i - 1, buf, src, tar);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nvoid solveHanota(List<int> A, List<int> B, List<int> C) {\nint n = A.length;\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(n, A, B, C);\n}\n
hanota.rs
/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfn move_pan(src: &mut Vec<i32>, tar: &mut Vec<i32>) {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\nlet pan = src.remove(src.len() - 1);\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.push(pan);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nfn dfs(i: i32, src: &mut Vec<i32>, buf: &mut Vec<i32>, tar: &mut Vec<i32>) {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif i == 1 {\nmove_pan(src, tar);\nreturn;\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i - 1, src, tar, buf);\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove_pan(src, tar);\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i - 1, buf, src, tar);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nfn solve_hanota(A: &mut Vec<i32>, B: &mut Vec<i32>, C: &mut Vec<i32>) {\nlet n = A.len() as i32;\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(n, A, B, C);\n}\n
hanota.c
/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(vector *src, vector *tar) {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\nint *panTemp = vectorBack(src);\nint *pan = malloc(sizeof(int));\n*pan = *panTemp;\nvectorPopback(src);\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\nvectorPushback(tar, pan, sizeof(int));\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nvoid dfs(int i, vector *src, vector *buf, vector *tar) {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif (i == 1) {\nmove(src, tar);\nreturn;\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i - 1, src, tar, buf);\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src, tar);\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i - 1, buf, src, tar);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nvoid solveHanota(vector *A, vector *B, vector *C) {\nint n = A->size;\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(n, A, B, C);\n}\n
hanota.zig
[class]{}-[func]{move}\n[class]{}-[func]{dfs}\n[class]{}-[func]{solveHanota}\n

\u5982\u56fe 12-15 \u6240\u793a\uff0c\u6c49\u8bfa\u5854\u95ee\u9898\u5f62\u6210\u4e00\u4e2a\u9ad8\u5ea6\u4e3a \\(n\\) \u7684\u9012\u5f52\u6811\uff0c\u6bcf\u4e2a\u8282\u70b9\u4ee3\u8868\u4e00\u4e2a\u5b50\u95ee\u9898\u3001\u5bf9\u5e94\u4e00\u4e2a\u5f00\u542f\u7684 dfs() \u51fd\u6570\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

\u56fe 12-15 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898\u7684\u9012\u5f52\u6811

Quote

\u6c49\u8bfa\u5854\u95ee\u9898\u6e90\u81ea\u4e00\u79cd\u53e4\u8001\u7684\u4f20\u8bf4\u6545\u4e8b\u3002\u5728\u53e4\u5370\u5ea6\u7684\u4e00\u4e2a\u5bfa\u5e99\u91cc\uff0c\u50e7\u4fa3\u4eec\u6709\u4e09\u6839\u9ad8\u5927\u7684\u94bb\u77f3\u67f1\u5b50\uff0c\u4ee5\u53ca \\(64\\) \u4e2a\u5927\u5c0f\u4e0d\u4e00\u7684\u91d1\u5706\u76d8\u3002\u50e7\u4fa3\u4eec\u4e0d\u65ad\u5730\u79fb\u52a8\u539f\u76d8\uff0c\u4ed6\u4eec\u76f8\u4fe1\u5728\u6700\u540e\u4e00\u4e2a\u5706\u76d8\u88ab\u6b63\u786e\u653e\u7f6e\u7684\u90a3\u4e00\u523b\uff0c\u8fd9\u4e2a\u4e16\u754c\u5c31\u4f1a\u7ed3\u675f\u3002

\u7136\u800c\uff0c\u5373\u4f7f\u50e7\u4fa3\u4eec\u6bcf\u79d2\u949f\u79fb\u52a8\u4e00\u6b21\uff0c\u603b\u5171\u9700\u8981\u5927\u7ea6 \\(2^{64} \\approx 1.84\u00d710^{19}\\) \u79d2\uff0c\u5408\u7ea6 \\(5850\\) \u4ebf\u5e74\uff0c\u8fdc\u8fdc\u8d85\u8fc7\u4e86\u73b0\u5728\u5bf9\u5b87\u5b99\u5e74\u9f84\u7684\u4f30\u8ba1\u3002\u6240\u4ee5\uff0c\u5018\u82e5\u8fd9\u4e2a\u4f20\u8bf4\u662f\u771f\u7684\uff0c\u6211\u4eec\u5e94\u8be5\u4e0d\u9700\u8981\u62c5\u5fc3\u4e16\u754c\u672b\u65e5\u7684\u5230\u6765\u3002

"},{"location":"chapter_divide_and_conquer/summary/","title":"12.5 \u00a0 \u5c0f\u7ed3","text":"
  • \u5206\u6cbb\u7b97\u6cd5\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u7b97\u6cd5\u8bbe\u8ba1\u7b56\u7565\uff0c\u5305\u62ec\u5206\uff08\u5212\u5206\uff09\u548c\u6cbb\uff08\u5408\u5e76\uff09\u4e24\u4e2a\u9636\u6bb5\uff0c\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u5b9e\u73b0\u3002
  • \u5224\u65ad\u662f\u5426\u662f\u5206\u6cbb\u7b97\u6cd5\u95ee\u9898\u7684\u4f9d\u636e\u5305\u62ec\uff1a\u95ee\u9898\u80fd\u5426\u88ab\u5206\u89e3\u3001\u5b50\u95ee\u9898\u662f\u5426\u72ec\u7acb\u3001\u5b50\u95ee\u9898\u662f\u5426\u53ef\u4ee5\u88ab\u5408\u5e76\u3002
  • \u5f52\u5e76\u6392\u5e8f\u662f\u5206\u6cbb\u7b56\u7565\u7684\u5178\u578b\u5e94\u7528\uff0c\u5176\u9012\u5f52\u5730\u5c06\u6570\u7ec4\u5212\u5206\u4e3a\u7b49\u957f\u7684\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u76f4\u5230\u53ea\u5269\u4e00\u4e2a\u5143\u7d20\u65f6\u5f00\u59cb\u9010\u5c42\u5408\u5e76\uff0c\u4ece\u800c\u5b8c\u6210\u6392\u5e8f\u3002
  • \u5f15\u5165\u5206\u6cbb\u7b56\u7565\u5f80\u5f80\u53ef\u4ee5\u5e26\u6765\u7b97\u6cd5\u6548\u7387\u7684\u63d0\u5347\u3002\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u7b56\u7565\u51cf\u5c11\u4e86\u64cd\u4f5c\u6570\u91cf\uff1b\u53e6\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u540e\u6709\u5229\u4e8e\u7cfb\u7edf\u7684\u5e76\u884c\u4f18\u5316\u3002
  • \u5206\u6cbb\u65e2\u53ef\u4ee5\u89e3\u51b3\u8bb8\u591a\u7b97\u6cd5\u95ee\u9898\uff0c\u4e5f\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u8bbe\u8ba1\u4e2d\uff0c\u5904\u5904\u53ef\u89c1\u5176\u8eab\u5f71\u3002
  • \u76f8\u8f83\u4e8e\u66b4\u529b\u641c\u7d22\uff0c\u81ea\u9002\u5e94\u641c\u7d22\u6548\u7387\u66f4\u9ad8\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u7684\u641c\u7d22\u7b97\u6cd5\u901a\u5e38\u90fd\u662f\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u5b9e\u73b0\u7684\u3002
  • \u4e8c\u5206\u67e5\u627e\u662f\u5206\u6cbb\u7b56\u7565\u7684\u53e6\u4e00\u4e2a\u5178\u578b\u5e94\u7528\uff0c\u5b83\u4e0d\u5305\u542b\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u8fdb\u884c\u5408\u5e76\u7684\u6b65\u9aa4\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u5206\u6cbb\u5b9e\u73b0\u4e8c\u5206\u67e5\u627e\u3002
  • \u5728\u6784\u5efa\u4e8c\u53c9\u6811\u95ee\u9898\u4e2d\uff0c\u6784\u5efa\u6811\uff08\u539f\u95ee\u9898\uff09\u53ef\u4ee5\u88ab\u5212\u5206\u4e3a\u6784\u5efa\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\uff08\u5b50\u95ee\u9898\uff09\uff0c\u5176\u53ef\u4ee5\u901a\u8fc7\u5212\u5206\u524d\u5e8f\u904d\u5386\u548c\u4e2d\u5e8f\u904d\u5386\u7684\u7d22\u5f15\u533a\u95f4\u6765\u5b9e\u73b0\u3002
  • \u5728\u6c49\u8bfa\u5854\u95ee\u9898\u4e2d\uff0c\u4e00\u4e2a\u89c4\u6a21\u4e3a \\(n\\) \u7684\u95ee\u9898\u53ef\u4ee5\u88ab\u5212\u5206\u4e3a\u4e24\u4e2a\u89c4\u6a21\u4e3a \\(n-1\\) \u7684\u5b50\u95ee\u9898\u548c\u4e00\u4e2a\u89c4\u6a21\u4e3a \\(1\\) \u7684\u5b50\u95ee\u9898\u3002\u6309\u987a\u5e8f\u89e3\u51b3\u8fd9\u4e09\u4e2a\u5b50\u95ee\u9898\u540e\uff0c\u539f\u95ee\u9898\u968f\u4e4b\u5f97\u5230\u89e3\u51b3\u3002
"},{"location":"chapter_dynamic_programming/","title":"\u7b2c 14 \u7ae0 \u00a0 \u52a8\u6001\u89c4\u5212","text":"

Abstract

\u5c0f\u6eaa\u6c47\u5165\u6cb3\u6d41\uff0c\u6c5f\u6cb3\u6c47\u5165\u5927\u6d77\u3002

\u52a8\u6001\u89c4\u5212\u5c06\u5c0f\u95ee\u9898\u7684\u89e3\u6c47\u96c6\u6210\u5927\u95ee\u9898\u7684\u7b54\u6848\uff0c\u4e00\u6b65\u6b65\u5f15\u9886\u6211\u4eec\u8d70\u5411\u89e3\u51b3\u95ee\u9898\u7684\u5f7c\u5cb8\u3002

"},{"location":"chapter_dynamic_programming/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 14.1 \u00a0 \u521d\u63a2\u52a8\u6001\u89c4\u5212
  • 14.2 \u00a0 DP \u95ee\u9898\u7279\u6027
  • 14.3 \u00a0 DP \u89e3\u9898\u601d\u8def
  • 14.4 \u00a0 0-1 \u80cc\u5305\u95ee\u9898
  • 14.5 \u00a0 \u5b8c\u5168\u80cc\u5305\u95ee\u9898
  • 14.6 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898
  • 14.7 \u00a0 \u5c0f\u7ed3
"},{"location":"chapter_dynamic_programming/dp_problem_features/","title":"14.2 \u00a0 \u52a8\u6001\u89c4\u5212\u95ee\u9898\u7279\u6027","text":"

\u5728\u4e0a\u8282\u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u52a8\u6001\u89c4\u5212\u662f\u5982\u4f55\u901a\u8fc7\u5b50\u95ee\u9898\u5206\u89e3\u6765\u6c42\u89e3\u95ee\u9898\u7684\u3002\u5b9e\u9645\u4e0a\uff0c\u5b50\u95ee\u9898\u5206\u89e3\u662f\u4e00\u79cd\u901a\u7528\u7684\u7b97\u6cd5\u601d\u8def\uff0c\u5728\u5206\u6cbb\u3001\u52a8\u6001\u89c4\u5212\u3001\u56de\u6eaf\u4e2d\u7684\u4fa7\u91cd\u70b9\u4e0d\u540c\u3002

  • \u5206\u6cbb\u7b97\u6cd5\u9012\u5f52\u5730\u5c06\u539f\u95ee\u9898\u5212\u5206\u4e3a\u591a\u4e2a\u76f8\u4e92\u72ec\u7acb\u7684\u5b50\u95ee\u9898\uff0c\u76f4\u81f3\u6700\u5c0f\u5b50\u95ee\u9898\uff0c\u5e76\u5728\u56de\u6eaf\u4e2d\u5408\u5e76\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u6700\u7ec8\u5f97\u5230\u539f\u95ee\u9898\u7684\u89e3\u3002
  • \u52a8\u6001\u89c4\u5212\u4e5f\u5bf9\u95ee\u9898\u8fdb\u884c\u9012\u5f52\u5206\u89e3\uff0c\u4f46\u4e0e\u5206\u6cbb\u7b97\u6cd5\u7684\u4e3b\u8981\u533a\u522b\u662f\uff0c\u52a8\u6001\u89c4\u5212\u4e2d\u7684\u5b50\u95ee\u9898\u662f\u76f8\u4e92\u4f9d\u8d56\u7684\uff0c\u5728\u5206\u89e3\u8fc7\u7a0b\u4e2d\u4f1a\u51fa\u73b0\u8bb8\u591a\u91cd\u53e0\u5b50\u95ee\u9898\u3002
  • \u56de\u6eaf\u7b97\u6cd5\u5728\u5c1d\u8bd5\u548c\u56de\u9000\u4e2d\u7a77\u4e3e\u6240\u6709\u53ef\u80fd\u7684\u89e3\uff0c\u5e76\u901a\u8fc7\u526a\u679d\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u641c\u7d22\u5206\u652f\u3002\u539f\u95ee\u9898\u7684\u89e3\u7531\u4e00\u7cfb\u5217\u51b3\u7b56\u6b65\u9aa4\u6784\u6210\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6bcf\u4e2a\u51b3\u7b56\u6b65\u9aa4\u4e4b\u524d\u7684\u5b50\u5e8f\u5217\u770b\u4f5c\u4e3a\u4e00\u4e2a\u5b50\u95ee\u9898\u3002

\u5b9e\u9645\u4e0a\uff0c\u52a8\u6001\u89c4\u5212\u5e38\u7528\u6765\u6c42\u89e3\u6700\u4f18\u5316\u95ee\u9898\uff0c\u5b83\u4eec\u4e0d\u4ec5\u5305\u542b\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u8fd8\u5177\u6709\u53e6\u5916\u4e24\u5927\u7279\u6027\uff1a\u6700\u4f18\u5b50\u7ed3\u6784\u3001\u65e0\u540e\u6548\u6027\u3002

"},{"location":"chapter_dynamic_programming/dp_problem_features/#1421","title":"14.2.1 \u00a0 \u6700\u4f18\u5b50\u7ed3\u6784","text":"

\u6211\u4eec\u5bf9\u722c\u697c\u68af\u95ee\u9898\u7a0d\u4f5c\u6539\u52a8\uff0c\u4f7f\u4e4b\u66f4\u52a0\u9002\u5408\u5c55\u793a\u6700\u4f18\u5b50\u7ed3\u6784\u6982\u5ff5\u3002

\u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7

\u7ed9\u5b9a\u4e00\u4e2a\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\uff0c\u6bcf\u4e00\u9636\u697c\u68af\u4e0a\u90fd\u8d34\u6709\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\uff0c\u8868\u793a\u4f60\u5728\u8be5\u53f0\u9636\u6240\u9700\u8981\u4ed8\u51fa\u7684\u4ee3\u4ef7\u3002\u7ed9\u5b9a\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\u6570\u7ec4 \\(cost\\) \uff0c\u5176\u4e2d \\(cost[i]\\) \u8868\u793a\u5728\u7b2c \\(i\\) \u4e2a\u53f0\u9636\u9700\u8981\u4ed8\u51fa\u7684\u4ee3\u4ef7\uff0c\\(cost[0]\\) \u4e3a\u5730\u9762\u8d77\u59cb\u70b9\u3002\u8bf7\u8ba1\u7b97\u6700\u5c11\u9700\u8981\u4ed8\u51fa\u591a\u5c11\u4ee3\u4ef7\u624d\u80fd\u5230\u8fbe\u9876\u90e8\uff1f

\u5982\u56fe 14-6 \u6240\u793a\uff0c\u82e5\u7b2c \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u9636\u7684\u4ee3\u4ef7\u5206\u522b\u4e3a \\(1\\)\u3001\\(10\\)\u3001\\(1\\) \uff0c\u5219\u4ece\u5730\u9762\u722c\u5230\u7b2c \\(3\\) \u9636\u7684\u6700\u5c0f\u4ee3\u4ef7\u4e3a \\(2\\) \u3002

\u56fe 14-6 \u00a0 \u722c\u5230\u7b2c 3 \u9636\u7684\u6700\u5c0f\u4ee3\u4ef7

\u8bbe \\(dp[i]\\) \u4e3a\u722c\u5230\u7b2c \\(i\\) \u9636\u7d2f\u8ba1\u4ed8\u51fa\u7684\u4ee3\u4ef7\uff0c\u7531\u4e8e\u7b2c \\(i\\) \u9636\u53ea\u53ef\u80fd\u4ece \\(i - 1\\) \u9636\u6216 \\(i - 2\\) \u9636\u8d70\u6765\uff0c\u56e0\u6b64 \\(dp[i]\\) \u53ea\u53ef\u80fd\u7b49\u4e8e \\(dp[i - 1] + cost[i]\\) \u6216 \\(dp[i - 2] + cost[i]\\) \u3002\u4e3a\u4e86\u5c3d\u53ef\u80fd\u51cf\u5c11\u4ee3\u4ef7\uff0c\u6211\u4eec\u5e94\u8be5\u9009\u62e9\u4e24\u8005\u4e2d\u8f83\u5c0f\u7684\u90a3\u4e00\u4e2a\uff1a

\\[ dp[i] = \\min(dp[i-1], dp[i-2]) + cost[i] \\]

\u8fd9\u4fbf\u53ef\u4ee5\u5f15\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\u7684\u542b\u4e49\uff1a\u539f\u95ee\u9898\u7684\u6700\u4f18\u89e3\u662f\u4ece\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u6784\u5efa\u5f97\u6765\u7684\u3002

\u672c\u9898\u663e\u7136\u5177\u6709\u6700\u4f18\u5b50\u7ed3\u6784\uff1a\u6211\u4eec\u4ece\u4e24\u4e2a\u5b50\u95ee\u9898\u6700\u4f18\u89e3 \\(dp[i-1]\\) \u548c \\(dp[i-2]\\) \u4e2d\u6311\u9009\u51fa\u8f83\u4f18\u7684\u90a3\u4e00\u4e2a\uff0c\u5e76\u7528\u5b83\u6784\u5efa\u51fa\u539f\u95ee\u9898 \\(dp[i]\\) \u7684\u6700\u4f18\u89e3\u3002

\u90a3\u4e48\uff0c\u4e0a\u8282\u7684\u722c\u697c\u68af\u9898\u76ee\u6709\u6ca1\u6709\u6700\u4f18\u5b50\u7ed3\u6784\u5462\uff1f\u5b83\u7684\u76ee\u6807\u662f\u6c42\u89e3\u65b9\u6848\u6570\u91cf\uff0c\u770b\u4f3c\u662f\u4e00\u4e2a\u8ba1\u6570\u95ee\u9898\uff0c\u4f46\u5982\u679c\u6362\u4e00\u79cd\u95ee\u6cd5\uff1a\u201c\u6c42\u89e3\u6700\u5927\u65b9\u6848\u6570\u91cf\u201d\u3002\u6211\u4eec\u610f\u5916\u5730\u53d1\u73b0\uff0c\u867d\u7136\u9898\u76ee\u4fee\u6539\u524d\u540e\u662f\u7b49\u4ef7\u7684\uff0c\u4f46\u6700\u4f18\u5b50\u7ed3\u6784\u6d6e\u73b0\u51fa\u6765\u4e86\uff1a\u7b2c \\(n\\) \u9636\u6700\u5927\u65b9\u6848\u6570\u91cf\u7b49\u4e8e\u7b2c \\(n-1\\) \u9636\u548c\u7b2c \\(n-2\\) \u9636\u6700\u5927\u65b9\u6848\u6570\u91cf\u4e4b\u548c\u3002\u6240\u4ee5\u8bf4\uff0c\u6700\u4f18\u5b50\u7ed3\u6784\u7684\u89e3\u91ca\u65b9\u5f0f\u6bd4\u8f83\u7075\u6d3b\uff0c\u5728\u4e0d\u540c\u95ee\u9898\u4e2d\u4f1a\u6709\u4e0d\u540c\u7684\u542b\u4e49\u3002

\u6839\u636e\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff0c\u4ee5\u53ca\u521d\u59cb\u72b6\u6001 \\(dp[1] = cost[1]\\) \u548c \\(dp[2] = cost[2]\\) \uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f97\u5230\u52a8\u6001\u89c4\u5212\u4ee3\u7801\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig min_cost_climbing_stairs_dp.py
def min_cost_climbing_stairs_dp(cost: list[int]) -> int:\n\"\"\"\u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(cost) - 1\nif n == 1 or n == 2:\nreturn cost[n]\n# \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\ndp = [0] * (n + 1)\n# \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1], dp[2] = cost[1], cost[2]\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in range(3, n + 1):\ndp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\nreturn dp[n]\n
min_cost_climbing_stairs_dp.cpp
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(vector<int> &cost) {\nint n = cost.size() - 1;\nif (n == 1 || n == 2)\nreturn cost[n];\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvector<int> dp(n + 1);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1];\ndp[2] = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n}\nreturn dp[n];\n}\n
min_cost_climbing_stairs_dp.java
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(int[] cost) {\nint n = cost.length - 1;\nif (n == 1 || n == 2)\nreturn cost[n];\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nint[] dp = new int[n + 1];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1];\ndp[2] = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n}\nreturn dp[n];\n}\n
min_cost_climbing_stairs_dp.cs
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint MinCostClimbingStairsDP(int[] cost) {\nint n = cost.Length - 1;\nif (n == 1 || n == 2)\nreturn cost[n];\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nint[] dp = new int[n + 1];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1];\ndp[2] = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i] = Math.Min(dp[i - 1], dp[i - 2]) + cost[i];\n}\nreturn dp[n];\n}\n
min_cost_climbing_stairs_dp.go
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDP(cost []int) int {\nn := len(cost) - 1\nif n == 1 || n == 2 {\nreturn cost[n]\n}\nmin := func(a, b int) int {\nif a < b {\nreturn a\n}\nreturn b\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\ndp := make([]int, n+1)\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1]\ndp[2] = cost[2]\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i := 3; i <= n; i++ {\ndp[i] = min(dp[i-1], dp[i-2]) + cost[i]\n}\nreturn dp[n]\n}\n
min_cost_climbing_stairs_dp.swift
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDP(cost: [Int]) -> Int {\nlet n = cost.count - 1\nif n == 1 || n == 2 {\nreturn cost[n]\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvar dp = Array(repeating: 0, count: n + 1)\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1]\ndp[2] = cost[2]\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in stride(from: 3, through: n, by: 1) {\ndp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n}\nreturn dp[n]\n}\n
min_cost_climbing_stairs_dp.js
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction minCostClimbingStairsDP(cost) {\nconst n = cost.length - 1;\nif (n === 1 || n === 2) {\nreturn cost[n];\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nconst dp = new Array(n + 1);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1];\ndp[2] = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (let i = 3; i <= n; i++) {\ndp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n}\nreturn dp[n];\n}\n
min_cost_climbing_stairs_dp.ts
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction minCostClimbingStairsDP(cost: Array<number>): number {\nconst n = cost.length - 1;\nif (n === 1 || n === 2) {\nreturn cost[n];\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nconst dp = new Array(n + 1);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1];\ndp[2] = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (let i = 3; i <= n; i++) {\ndp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n}\nreturn dp[n];\n}\n
min_cost_climbing_stairs_dp.dart
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(List<int> cost) {\nint n = cost.length - 1;\nif (n == 1 || n == 2) return cost[n];\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nList<int> dp = List.filled(n + 1, 0);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1];\ndp[2] = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n}\nreturn dp[n];\n}\n
min_cost_climbing_stairs_dp.rs
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfn min_cost_climbing_stairs_dp(cost: &[i32]) -> i32 {\nlet n = cost.len() - 1;\nif n == 1 || n == 2 { return cost[n]; }\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nlet mut dp = vec![-1; n + 1];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1];\ndp[2] = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in 3..=n {\ndp[i] = cmp::min(dp[i - 1], dp[i - 2]) + cost[i];\n}\ndp[n]\n}\n
min_cost_climbing_stairs_dp.c
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(int cost[], int costSize) {\nint n = costSize - 1;\nif (n == 1 || n == 2)\nreturn cost[n];\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nint dp[n + 1];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1];\ndp[2] = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n}\nreturn dp[n];\n}\n
min_cost_climbing_stairs_dp.zig
// \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212\nfn minCostClimbingStairsDP(comptime cost: []i32) i32 {\ncomptime var n = cost.len - 1;\nif (n == 1 or n == 2) {\nreturn cost[n];\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvar dp = [_]i32{-1} ** (n + 1);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1];\ndp[2] = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (3..n + 1) |i| {\ndp[i] = @min(dp[i - 1], dp[i - 2]) + cost[i];\n}\nreturn dp[n];\n}\n

\u56fe 14-7 \u5c55\u793a\u4e86\u4ee5\u4e0a\u4ee3\u7801\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b\u3002

\u56fe 14-7 \u00a0 \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

\u672c\u9898\u4e5f\u53ef\u4ee5\u8fdb\u884c\u7a7a\u95f4\u4f18\u5316\uff0c\u5c06\u4e00\u7ef4\u538b\u7f29\u81f3\u96f6\u7ef4\uff0c\u4f7f\u5f97\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n)\\) \u964d\u4f4e\u81f3 \\(O(1)\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig min_cost_climbing_stairs_dp.py
def min_cost_climbing_stairs_dp_comp(cost: list[int]) -> int:\n\"\"\"\u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(cost) - 1\nif n == 1 or n == 2:\nreturn cost[n]\na, b = cost[1], cost[2]\nfor i in range(3, n + 1):\na, b = b, min(a, b) + cost[i]\nreturn b\n
min_cost_climbing_stairs_dp.cpp
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(vector<int> &cost) {\nint n = cost.size() - 1;\nif (n == 1 || n == 2)\nreturn cost[n];\nint a = cost[1], b = cost[2];\nfor (int i = 3; i <= n; i++) {\nint tmp = b;\nb = min(a, tmp) + cost[i];\na = tmp;\n}\nreturn b;\n}\n
min_cost_climbing_stairs_dp.java
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(int[] cost) {\nint n = cost.length - 1;\nif (n == 1 || n == 2)\nreturn cost[n];\nint a = cost[1], b = cost[2];\nfor (int i = 3; i <= n; i++) {\nint tmp = b;\nb = Math.min(a, tmp) + cost[i];\na = tmp;\n}\nreturn b;\n}\n
min_cost_climbing_stairs_dp.cs
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint MinCostClimbingStairsDPComp(int[] cost) {\nint n = cost.Length - 1;\nif (n == 1 || n == 2)\nreturn cost[n];\nint a = cost[1], b = cost[2];\nfor (int i = 3; i <= n; i++) {\nint tmp = b;\nb = Math.Min(a, tmp) + cost[i];\na = tmp;\n}\nreturn b;\n}\n
min_cost_climbing_stairs_dp.go
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDPComp(cost []int) int {\nn := len(cost) - 1\nif n == 1 || n == 2 {\nreturn cost[n]\n}\nmin := func(a, b int) int {\nif a < b {\nreturn a\n}\nreturn b\n}\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\na, b := cost[1], cost[2]\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i := 3; i <= n; i++ {\ntmp := b\nb = min(a, tmp) + cost[i]\na = tmp\n}\nreturn b\n}\n
min_cost_climbing_stairs_dp.swift
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDPComp(cost: [Int]) -> Int {\nlet n = cost.count - 1\nif n == 1 || n == 2 {\nreturn cost[n]\n}\nvar (a, b) = (cost[1], cost[2])\nfor i in stride(from: 3, through: n, by: 1) {\n(a, b) = (b, min(a, b) + cost[i])\n}\nreturn b\n}\n
min_cost_climbing_stairs_dp.js
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction minCostClimbingStairsDPComp(cost) {\nconst n = cost.length - 1;\nif (n === 1 || n === 2) {\nreturn cost[n];\n}\nlet a = cost[1],\nb = cost[2];\nfor (let i = 3; i <= n; i++) {\nconst tmp = b;\nb = Math.min(a, tmp) + cost[i];\na = tmp;\n}\nreturn b;\n}\n
min_cost_climbing_stairs_dp.ts
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction minCostClimbingStairsDPComp(cost: Array<number>): number {\nconst n = cost.length - 1;\nif (n === 1 || n === 2) {\nreturn cost[n];\n}\nlet a = cost[1],\nb = cost[2];\nfor (let i = 3; i <= n; i++) {\nconst tmp = b;\nb = Math.min(a, tmp) + cost[i];\na = tmp;\n}\nreturn b;\n}\n
min_cost_climbing_stairs_dp.dart
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(List<int> cost) {\nint n = cost.length - 1;\nif (n == 1 || n == 2) return cost[n];\nint a = cost[1], b = cost[2];\nfor (int i = 3; i <= n; i++) {\nint tmp = b;\nb = min(a, tmp) + cost[i];\na = tmp;\n}\nreturn b;\n}\n
min_cost_climbing_stairs_dp.rs
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn min_cost_climbing_stairs_dp_comp(cost: &[i32]) -> i32 {\nlet n = cost.len() - 1;\nif n == 1 || n == 2 { return cost[n] };\nlet (mut a, mut b) = (cost[1], cost[2]);\nfor i in 3..=n {\nlet tmp = b;\nb = cmp::min(a, tmp) + cost[i];\na = tmp;\n}\nb\n}\n
min_cost_climbing_stairs_dp.c
/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(int cost[], int costSize) {\nint n = costSize - 1;\nif (n == 1 || n == 2)\nreturn cost[n];\nint a = cost[1], b = cost[2];\nfor (int i = 3; i <= n; i++) {\nint tmp = b;\nb = min(a, tmp) + cost[i];\na = tmp;\n}\nreturn b;\n}\n
min_cost_climbing_stairs_dp.zig
// \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn minCostClimbingStairsDPComp(cost: []i32) i32 {\nvar n = cost.len - 1;\nif (n == 1 or n == 2) {\nreturn cost[n];\n}\nvar a = cost[1];\nvar b = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (3..n + 1) |i| {\nvar tmp = b;\nb = @min(a, tmp) + cost[i];\na = tmp;\n}\nreturn b;\n}\n
"},{"location":"chapter_dynamic_programming/dp_problem_features/#1422","title":"14.2.2 \u00a0 \u65e0\u540e\u6548\u6027","text":"

\u65e0\u540e\u6548\u6027\u662f\u52a8\u6001\u89c4\u5212\u80fd\u591f\u6709\u6548\u89e3\u51b3\u95ee\u9898\u7684\u91cd\u8981\u7279\u6027\u4e4b\u4e00\uff0c\u5b9a\u4e49\u4e3a\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u786e\u5b9a\u7684\u72b6\u6001\uff0c\u5b83\u7684\u672a\u6765\u53d1\u5c55\u53ea\u4e0e\u5f53\u524d\u72b6\u6001\u6709\u5173\uff0c\u800c\u4e0e\u5f53\u524d\u72b6\u6001\u8fc7\u53bb\u6240\u7ecf\u5386\u8fc7\u7684\u6240\u6709\u72b6\u6001\u65e0\u5173\u3002

\u4ee5\u722c\u697c\u68af\u95ee\u9898\u4e3a\u4f8b\uff0c\u7ed9\u5b9a\u72b6\u6001 \\(i\\) \uff0c\u5b83\u4f1a\u53d1\u5c55\u51fa\u72b6\u6001 \\(i+1\\) \u548c\u72b6\u6001 \\(i+2\\) \uff0c\u5206\u522b\u5bf9\u5e94\u8df3 \\(1\\) \u6b65\u548c\u8df3 \\(2\\) \u6b65\u3002\u5728\u505a\u51fa\u8fd9\u4e24\u79cd\u9009\u62e9\u65f6\uff0c\u6211\u4eec\u65e0\u987b\u8003\u8651\u72b6\u6001 \\(i\\) \u4e4b\u524d\u7684\u72b6\u6001\uff0c\u5b83\u4eec\u5bf9\u72b6\u6001 \\(i\\) \u7684\u672a\u6765\u6ca1\u6709\u5f71\u54cd\u3002

\u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u5411\u722c\u697c\u68af\u95ee\u9898\u6dfb\u52a0\u4e00\u4e2a\u7ea6\u675f\uff0c\u60c5\u51b5\u5c31\u4e0d\u4e00\u6837\u4e86\u3002

\u5e26\u7ea6\u675f\u722c\u697c\u68af

\u7ed9\u5b9a\u4e00\u4e2a\u5171\u6709 \\(n\\) \u9636\u7684\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\uff0c\u4f46\u4e0d\u80fd\u8fde\u7eed\u4e24\u8f6e\u8df3 \\(1\\) \u9636\uff0c\u8bf7\u95ee\u6709\u591a\u5c11\u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\u3002

\u4f8b\u5982\u56fe 14-8 \uff0c\u722c\u4e0a\u7b2c \\(3\\) \u9636\u4ec5\u5269 \\(2\\) \u79cd\u53ef\u884c\u65b9\u6848\uff0c\u5176\u4e2d\u8fde\u7eed\u4e09\u6b21\u8df3 \\(1\\) \u9636\u7684\u65b9\u6848\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\uff0c\u56e0\u6b64\u88ab\u820d\u5f03\u3002

\u56fe 14-8 \u00a0 \u5e26\u7ea6\u675f\u722c\u5230\u7b2c 3 \u9636\u7684\u65b9\u6848\u6570\u91cf

\u5728\u8be5\u95ee\u9898\u4e2d\uff0c\u5982\u679c\u4e0a\u4e00\u8f6e\u662f\u8df3 \\(1\\) \u9636\u4e0a\u6765\u7684\uff0c\u90a3\u4e48\u4e0b\u4e00\u8f6e\u5c31\u5fc5\u987b\u8df3 \\(2\\) \u9636\u3002\u8fd9\u610f\u5473\u7740\uff0c\u4e0b\u4e00\u6b65\u9009\u62e9\u4e0d\u80fd\u7531\u5f53\u524d\u72b6\u6001\uff08\u5f53\u524d\u697c\u68af\u9636\u6570\uff09\u72ec\u7acb\u51b3\u5b9a\uff0c\u8fd8\u548c\u524d\u4e00\u4e2a\u72b6\u6001\uff08\u4e0a\u8f6e\u697c\u68af\u9636\u6570\uff09\u6709\u5173\u3002

\u4e0d\u96be\u53d1\u73b0\uff0c\u6b64\u95ee\u9898\u5df2\u4e0d\u6ee1\u8db3\u65e0\u540e\u6548\u6027\uff0c\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b \\(dp[i] = dp[i-1] + dp[i-2]\\) \u4e5f\u5931\u6548\u4e86\uff0c\u56e0\u4e3a \\(dp[i-1]\\) \u4ee3\u8868\u672c\u8f6e\u8df3 \\(1\\) \u9636\uff0c\u4f46\u5176\u4e2d\u5305\u542b\u4e86\u8bb8\u591a\u201c\u4e0a\u4e00\u8f6e\u8df3 \\(1\\) \u9636\u4e0a\u6765\u7684\u201d\u65b9\u6848\uff0c\u800c\u4e3a\u4e86\u6ee1\u8db3\u7ea6\u675f\uff0c\u6211\u4eec\u5c31\u4e0d\u80fd\u5c06 \\(dp[i-1]\\) \u76f4\u63a5\u8ba1\u5165 \\(dp[i]\\) \u4e2d\u3002

\u4e3a\u6b64\uff0c\u6211\u4eec\u9700\u8981\u6269\u5c55\u72b6\u6001\u5b9a\u4e49\uff1a\u72b6\u6001 \\([i, j]\\) \u8868\u793a\u5904\u5728\u7b2c \\(i\\) \u9636\u3001\u5e76\u4e14\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(j\\) \u9636\uff0c\u5176\u4e2d \\(j \\in \\{1, 2\\}\\) \u3002\u6b64\u72b6\u6001\u5b9a\u4e49\u6709\u6548\u5730\u533a\u5206\u4e86\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(1\\) \u9636\u8fd8\u662f \\(2\\) \u9636\uff0c\u6211\u4eec\u53ef\u4ee5\u636e\u6b64\u6765\u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u4ece\u4f55\u800c\u6765\u7684\u3002

  • \u5f53\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(1\\) \u9636\u65f6\uff0c\u4e0a\u4e0a\u4e00\u8f6e\u53ea\u80fd\u9009\u62e9\u8df3 \\(2\\) \u9636\uff0c\u5373 \\(dp[i, 1]\\) \u53ea\u80fd\u4ece \\(dp[i-1, 2]\\) \u8f6c\u79fb\u8fc7\u6765\u3002
  • \u5f53\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(2\\) \u9636\u65f6\uff0c\u4e0a\u4e0a\u4e00\u8f6e\u53ef\u9009\u62e9\u8df3 \\(1\\) \u9636\u6216\u8df3 \\(2\\) \u9636\uff0c\u5373 \\(dp[i, 2]\\) \u53ef\u4ee5\u4ece \\(dp[i-2, 1]\\) \u6216 \\(dp[i-2, 2]\\) \u8f6c\u79fb\u8fc7\u6765\u3002

\u5982\u56fe 14-9 \u6240\u793a\uff0c\u5728\u8be5\u5b9a\u4e49\u4e0b\uff0c\\(dp[i, j]\\) \u8868\u793a\u72b6\u6001 \\([i, j]\\) \u5bf9\u5e94\u7684\u65b9\u6848\u6570\u3002\u6b64\u65f6\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a

\\[ \\begin{cases} dp[i, 1] = dp[i-1, 2] \\\\ dp[i, 2] = dp[i-2, 1] + dp[i-2, 2] \\end{cases} \\]

\u56fe 14-9 \u00a0 \u8003\u8651\u7ea6\u675f\u4e0b\u7684\u9012\u63a8\u5173\u7cfb

\u6700\u7ec8\uff0c\u8fd4\u56de \\(dp[n, 1] + dp[n, 2]\\) \u5373\u53ef\uff0c\u4e24\u8005\u4e4b\u548c\u4ee3\u8868\u722c\u5230\u7b2c \\(n\\) \u9636\u7684\u65b9\u6848\u603b\u6570\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_constraint_dp.py
def climbing_stairs_constraint_dp(n: int) -> int:\n\"\"\"\u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nif n == 1 or n == 2:\nreturn 1\n# \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\ndp = [[0] * 3 for _ in range(n + 1)]\n# \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1], dp[1][2] = 1, 0\ndp[2][1], dp[2][2] = 0, 1\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in range(3, n + 1):\ndp[i][1] = dp[i - 1][2]\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2]\nreturn dp[n][1] + dp[n][2]\n
climbing_stairs_constraint_dp.cpp
/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\nif (n == 1 || n == 2) {\nreturn 1;\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvector<vector<int>> dp(n + 1, vector<int>(3, 0));\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1;\ndp[1][2] = 0;\ndp[2][1] = 0;\ndp[2][2] = 1;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i][1] = dp[i - 1][2];\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n}\nreturn dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.java
/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\nif (n == 1 || n == 2) {\nreturn 1;\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nint[][] dp = new int[n + 1][3];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1;\ndp[1][2] = 0;\ndp[2][1] = 0;\ndp[2][2] = 1;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i][1] = dp[i - 1][2];\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n}\nreturn dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.cs
/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint ClimbingStairsConstraintDP(int n) {\nif (n == 1 || n == 2) {\nreturn 1;\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nint[,] dp = new int[n + 1, 3];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1, 1] = 1;\ndp[1, 2] = 0;\ndp[2, 1] = 0;\ndp[2, 2] = 1;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i, 1] = dp[i - 1, 2];\ndp[i, 2] = dp[i - 2, 1] + dp[i - 2, 2];\n}\nreturn dp[n, 1] + dp[n, 2];\n}\n
climbing_stairs_constraint_dp.go
/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsConstraintDP(n int) int {\nif n == 1 || n == 2 {\nreturn 1\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\ndp := make([][3]int, n+1)\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1\ndp[1][2] = 0\ndp[2][1] = 0\ndp[2][2] = 1\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i := 3; i <= n; i++ {\ndp[i][1] = dp[i-1][2]\ndp[i][2] = dp[i-2][1] + dp[i-2][2]\n}\nreturn dp[n][1] + dp[n][2]\n}\n
climbing_stairs_constraint_dp.swift
/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsConstraintDP(n: Int) -> Int {\nif n == 1 || n == 2 {\nreturn 1\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvar dp = Array(repeating: Array(repeating: 0, count: 3), count: n + 1)\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1\ndp[1][2] = 0\ndp[2][1] = 0\ndp[2][2] = 1\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in stride(from: 3, through: n, by: 1) {\ndp[i][1] = dp[i - 1][2]\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n}\nreturn dp[n][1] + dp[n][2]\n}\n
climbing_stairs_constraint_dp.js
/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsConstraintDP(n) {\nif (n === 1 || n === 2) {\nreturn 1;\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nconst dp = Array.from(new Array(n + 1), () => new Array(3));\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1;\ndp[1][2] = 0;\ndp[2][1] = 0;\ndp[2][2] = 1;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (let i = 3; i <= n; i++) {\ndp[i][1] = dp[i - 1][2];\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n}\nreturn dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.ts
/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsConstraintDP(n: number): number {\nif (n === 1 || n === 2) {\nreturn 1;\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nconst dp = Array.from({ length: n + 1 }, () => new Array(3));\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1;\ndp[1][2] = 0;\ndp[2][1] = 0;\ndp[2][2] = 1;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (let i = 3; i <= n; i++) {\ndp[i][1] = dp[i - 1][2];\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n}\nreturn dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.dart
/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\nif (n == 1 || n == 2) {\nreturn 1;\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nList<List<int>> dp = List.generate(n + 1, (index) => List.filled(3, 0));\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1;\ndp[1][2] = 0;\ndp[2][1] = 0;\ndp[2][2] = 1;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i][1] = dp[i - 1][2];\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n}\nreturn dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.rs
/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfn climbing_stairs_constraint_dp(n: usize) -> i32 {\nif n == 1 || n == 2 { return 1 };\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nlet mut dp = vec![vec![-1; 3]; n + 1];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1;\ndp[1][2] = 0;\ndp[2][1] = 0;\ndp[2][2] = 1;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in 3..=n {\ndp[i][1] = dp[i - 1][2];\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n}\ndp[n][1] + dp[n][2]\n}\n
climbing_stairs_constraint_dp.c
/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\nif (n == 1 || n == 2) {\nreturn 1;\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nint dp[n + 1][3];\nmemset(dp, 0, sizeof(dp));\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1;\ndp[1][2] = 0;\ndp[2][1] = 0;\ndp[2][2] = 1;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i][1] = dp[i - 1][2];\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n}\nreturn dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.zig
// \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212\nfn climbingStairsConstraintDP(comptime n: usize) i32 {\nif (n == 1 or n == 2) {\nreturn 1;\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvar dp = [_][3]i32{ [_]i32{ -1, -1, -1 } } ** (n + 1);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1;\ndp[1][2] = 0;\ndp[2][1] = 0;\ndp[2][2] = 1;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (3..n + 1) |i| {\ndp[i][1] = dp[i - 1][2];\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n}\nreturn dp[n][1] + dp[n][2];\n}\n

\u5728\u4e0a\u9762\u7684\u6848\u4f8b\u4e2d\uff0c\u7531\u4e8e\u4ec5\u9700\u591a\u8003\u8651\u524d\u9762\u4e00\u4e2a\u72b6\u6001\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u901a\u8fc7\u6269\u5c55\u72b6\u6001\u5b9a\u4e49\uff0c\u4f7f\u5f97\u95ee\u9898\u91cd\u65b0\u6ee1\u8db3\u65e0\u540e\u6548\u6027\u3002\u7136\u800c\uff0c\u67d0\u4e9b\u95ee\u9898\u5177\u6709\u975e\u5e38\u4e25\u91cd\u7684\u201c\u6709\u540e\u6548\u6027\u201d\u3002

\u722c\u697c\u68af\u4e0e\u969c\u788d\u751f\u6210

\u7ed9\u5b9a\u4e00\u4e2a\u5171\u6709 \\(n\\) \u9636\u7684\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\u3002\u89c4\u5b9a\u5f53\u722c\u5230\u7b2c \\(i\\) \u9636\u65f6\uff0c\u7cfb\u7edf\u81ea\u52a8\u4f1a\u7ed9\u7b2c \\(2i\\) \u9636\u4e0a\u653e\u4e0a\u969c\u788d\u7269\uff0c\u4e4b\u540e\u6240\u6709\u8f6e\u90fd\u4e0d\u5141\u8bb8\u8df3\u5230\u7b2c \\(2i\\) \u9636\u4e0a\u3002\u4f8b\u5982\uff0c\u524d\u4e24\u8f6e\u5206\u522b\u8df3\u5230\u4e86\u7b2c \\(2\\)\u3001\\(3\\) \u9636\u4e0a\uff0c\u5219\u4e4b\u540e\u5c31\u4e0d\u80fd\u8df3\u5230\u7b2c \\(4\\)\u3001\\(6\\) \u9636\u4e0a\u3002\u8bf7\u95ee\u6709\u591a\u5c11\u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\u3002

\u5728\u8fd9\u4e2a\u95ee\u9898\u4e2d\uff0c\u4e0b\u6b21\u8df3\u8dc3\u4f9d\u8d56\u4e8e\u8fc7\u53bb\u6240\u6709\u7684\u72b6\u6001\uff0c\u56e0\u4e3a\u6bcf\u4e00\u6b21\u8df3\u8dc3\u90fd\u4f1a\u5728\u66f4\u9ad8\u7684\u9636\u68af\u4e0a\u8bbe\u7f6e\u969c\u788d\uff0c\u5e76\u5f71\u54cd\u672a\u6765\u7684\u8df3\u8dc3\u3002\u5bf9\u4e8e\u8fd9\u7c7b\u95ee\u9898\uff0c\u52a8\u6001\u89c4\u5212\u5f80\u5f80\u96be\u4ee5\u89e3\u51b3\u3002

\u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u590d\u6742\u7684\u7ec4\u5408\u4f18\u5316\u95ee\u9898\uff08\u4f8b\u5982\u65c5\u884c\u5546\u95ee\u9898\uff09\u90fd\u4e0d\u6ee1\u8db3\u65e0\u540e\u6548\u6027\u3002\u5bf9\u4e8e\u8fd9\u7c7b\u95ee\u9898\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u9009\u62e9\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\uff0c\u4f8b\u5982\u542f\u53d1\u5f0f\u641c\u7d22\u3001\u9057\u4f20\u7b97\u6cd5\u3001\u5f3a\u5316\u5b66\u4e60\u7b49\uff0c\u4ece\u800c\u5728\u6709\u9650\u65f6\u95f4\u5185\u5f97\u5230\u53ef\u7528\u7684\u5c40\u90e8\u6700\u4f18\u89e3\u3002

"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/","title":"14.3 \u00a0 \u52a8\u6001\u89c4\u5212\u89e3\u9898\u601d\u8def","text":"

\u4e0a\u4e24\u8282\u4ecb\u7ecd\u4e86\u52a8\u6001\u89c4\u5212\u95ee\u9898\u7684\u4e3b\u8981\u7279\u5f81\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u4e00\u8d77\u63a2\u7a76\u4e24\u4e2a\u66f4\u52a0\u5b9e\u7528\u7684\u95ee\u9898\u3002

  1. \u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u95ee\u9898\u662f\u4e0d\u662f\u52a8\u6001\u89c4\u5212\u95ee\u9898\uff1f
  2. \u6c42\u89e3\u52a8\u6001\u89c4\u5212\u95ee\u9898\u8be5\u4ece\u4f55\u5904\u5165\u624b\uff0c\u5b8c\u6574\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f
"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1431","title":"14.3.1 \u00a0 \u95ee\u9898\u5224\u65ad","text":"

\u603b\u7684\u6765\u8bf4\uff0c\u5982\u679c\u4e00\u4e2a\u95ee\u9898\u5305\u542b\u91cd\u53e0\u5b50\u95ee\u9898\u3001\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u5e76\u6ee1\u8db3\u65e0\u540e\u6548\u6027\uff0c\u90a3\u4e48\u5b83\u901a\u5e38\u5c31\u9002\u5408\u7528\u52a8\u6001\u89c4\u5212\u6c42\u89e3\u3002\u7136\u800c\uff0c\u6211\u4eec\u5f88\u96be\u4ece\u95ee\u9898\u63cf\u8ff0\u4e0a\u76f4\u63a5\u63d0\u53d6\u51fa\u8fd9\u4e9b\u7279\u6027\u3002\u56e0\u6b64\u6211\u4eec\u901a\u5e38\u4f1a\u653e\u5bbd\u6761\u4ef6\uff0c\u5148\u89c2\u5bdf\u95ee\u9898\u662f\u5426\u9002\u5408\u4f7f\u7528\u56de\u6eaf\uff08\u7a77\u4e3e\uff09\u89e3\u51b3\u3002

\u9002\u5408\u7528\u56de\u6eaf\u89e3\u51b3\u7684\u95ee\u9898\u901a\u5e38\u6ee1\u8db3\u201c\u51b3\u7b56\u6811\u6a21\u578b\u201d\uff0c\u8fd9\u79cd\u95ee\u9898\u53ef\u4ee5\u4f7f\u7528\u6811\u5f62\u7ed3\u6784\u6765\u63cf\u8ff0\uff0c\u5176\u4e2d\u6bcf\u4e00\u4e2a\u8282\u70b9\u4ee3\u8868\u4e00\u4e2a\u51b3\u7b56\uff0c\u6bcf\u4e00\u6761\u8def\u5f84\u4ee3\u8868\u4e00\u4e2a\u51b3\u7b56\u5e8f\u5217\u3002

\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c\u95ee\u9898\u5305\u542b\u660e\u786e\u7684\u51b3\u7b56\u6982\u5ff5\uff0c\u5e76\u4e14\u89e3\u662f\u901a\u8fc7\u4e00\u7cfb\u5217\u51b3\u7b56\u4ea7\u751f\u7684\uff0c\u90a3\u4e48\u5b83\u5c31\u6ee1\u8db3\u51b3\u7b56\u6811\u6a21\u578b\uff0c\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528\u56de\u6eaf\u6765\u89e3\u51b3\u3002

\u5728\u6b64\u57fa\u7840\u4e0a\uff0c\u52a8\u6001\u89c4\u5212\u95ee\u9898\u8fd8\u6709\u4e00\u4e9b\u5224\u65ad\u7684\u201c\u52a0\u5206\u9879\u201d\u3002

  • \u95ee\u9898\u5305\u542b\u6700\u5927\uff08\u5c0f\uff09\u6216\u6700\u591a\uff08\u5c11\uff09\u7b49\u6700\u4f18\u5316\u63cf\u8ff0\u3002
  • \u95ee\u9898\u7684\u72b6\u6001\u80fd\u591f\u4f7f\u7528\u4e00\u4e2a\u5217\u8868\u3001\u591a\u7ef4\u77e9\u9635\u6216\u6811\u6765\u8868\u793a\uff0c\u5e76\u4e14\u4e00\u4e2a\u72b6\u6001\u4e0e\u5176\u5468\u56f4\u7684\u72b6\u6001\u5b58\u5728\u9012\u63a8\u5173\u7cfb\u3002

\u76f8\u5e94\u5730\uff0c\u4e5f\u5b58\u5728\u4e00\u4e9b\u201c\u51cf\u5206\u9879\u201d\u3002

  • \u95ee\u9898\u7684\u76ee\u6807\u662f\u627e\u51fa\u6240\u6709\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u800c\u4e0d\u662f\u627e\u51fa\u6700\u4f18\u89e3\u3002
  • \u95ee\u9898\u63cf\u8ff0\u4e2d\u6709\u660e\u663e\u7684\u6392\u5217\u7ec4\u5408\u7684\u7279\u5f81\uff0c\u9700\u8981\u8fd4\u56de\u5177\u4f53\u7684\u591a\u4e2a\u65b9\u6848\u3002

\u5982\u679c\u4e00\u4e2a\u95ee\u9898\u6ee1\u8db3\u51b3\u7b56\u6811\u6a21\u578b\uff0c\u5e76\u5177\u6709\u8f83\u4e3a\u660e\u663e\u7684\u201c\u52a0\u5206\u9879\u201c\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5047\u8bbe\u5b83\u662f\u4e00\u4e2a\u52a8\u6001\u89c4\u5212\u95ee\u9898\uff0c\u5e76\u5728\u6c42\u89e3\u8fc7\u7a0b\u4e2d\u9a8c\u8bc1\u5b83\u3002

"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1432","title":"14.3.2 \u00a0 \u95ee\u9898\u6c42\u89e3\u6b65\u9aa4","text":"

\u52a8\u6001\u89c4\u5212\u7684\u89e3\u9898\u6d41\u7a0b\u4f1a\u56e0\u95ee\u9898\u7684\u6027\u8d28\u548c\u96be\u5ea6\u800c\u6709\u6240\u4e0d\u540c\uff0c\u4f46\u901a\u5e38\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u63cf\u8ff0\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u5efa\u7acb \\(dp\\) \u8868\uff0c\u63a8\u5bfc\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff0c\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u7b49\u3002

\u4e3a\u4e86\u66f4\u5f62\u8c61\u5730\u5c55\u793a\u89e3\u9898\u6b65\u9aa4\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u7ecf\u5178\u95ee\u9898\u201c\u6700\u5c0f\u8def\u5f84\u548c\u201d\u6765\u4e3e\u4f8b\u3002

Question

\u7ed9\u5b9a\u4e00\u4e2a \\(n \\times m\\) \u7684\u4e8c\u7ef4\u7f51\u683c grid \uff0c\u7f51\u683c\u4e2d\u7684\u6bcf\u4e2a\u5355\u5143\u683c\u5305\u542b\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\uff0c\u8868\u793a\u8be5\u5355\u5143\u683c\u7684\u4ee3\u4ef7\u3002\u673a\u5668\u4eba\u4ee5\u5de6\u4e0a\u89d2\u5355\u5143\u683c\u4e3a\u8d77\u59cb\u70b9\uff0c\u6bcf\u6b21\u53ea\u80fd\u5411\u4e0b\u6216\u8005\u5411\u53f3\u79fb\u52a8\u4e00\u6b65\uff0c\u76f4\u81f3\u5230\u8fbe\u53f3\u4e0b\u89d2\u5355\u5143\u683c\u3002\u8bf7\u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230\u53f3\u4e0b\u89d2\u7684\u6700\u5c0f\u8def\u5f84\u548c\u3002

\u56fe 14-10 \u5c55\u793a\u4e86\u4e00\u4e2a\u4f8b\u5b50\uff0c\u7ed9\u5b9a\u7f51\u683c\u7684\u6700\u5c0f\u8def\u5f84\u548c\u4e3a \\(13\\) \u3002

\u56fe 14-10 \u00a0 \u6700\u5c0f\u8def\u5f84\u548c\u793a\u4f8b\u6570\u636e

\u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868

\u672c\u9898\u7684\u6bcf\u4e00\u8f6e\u7684\u51b3\u7b56\u5c31\u662f\u4ece\u5f53\u524d\u683c\u5b50\u5411\u4e0b\u6216\u5411\u53f3\u4e00\u6b65\u3002\u8bbe\u5f53\u524d\u683c\u5b50\u7684\u884c\u5217\u7d22\u5f15\u4e3a \\([i, j]\\) \uff0c\u5219\u5411\u4e0b\u6216\u5411\u53f3\u8d70\u4e00\u6b65\u540e\uff0c\u7d22\u5f15\u53d8\u4e3a \\([i+1, j]\\) \u6216 \\([i, j+1]\\) \u3002\u56e0\u6b64\uff0c\u72b6\u6001\u5e94\u5305\u542b\u884c\u7d22\u5f15\u548c\u5217\u7d22\u5f15\u4e24\u4e2a\u53d8\u91cf\uff0c\u8bb0\u4e3a \\([i, j]\\) \u3002

\u72b6\u6001 \\([i, j]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u4e3a\uff1a\u4ece\u8d77\u59cb\u70b9 \\([0, 0]\\) \u8d70\u5230 \\([i, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\uff0c\u89e3\u8bb0\u4e3a \\(dp[i, j]\\) \u3002

\u81f3\u6b64\uff0c\u6211\u4eec\u5c31\u5f97\u5230\u4e86\u56fe 14-11 \u6240\u793a\u7684\u4e8c\u7ef4 \\(dp\\) \u77e9\u9635\uff0c\u5176\u5c3a\u5bf8\u4e0e\u8f93\u5165\u7f51\u683c \\(grid\\) \u76f8\u540c\u3002

\u56fe 14-11 \u00a0 \u72b6\u6001\u5b9a\u4e49\u4e0e dp \u8868

Note

\u52a8\u6001\u89c4\u5212\u548c\u56de\u6eaf\u8fc7\u7a0b\u53ef\u4ee5\u88ab\u63cf\u8ff0\u4e3a\u4e00\u4e2a\u51b3\u7b56\u5e8f\u5217\uff0c\u800c\u72b6\u6001\u7531\u6240\u6709\u51b3\u7b56\u53d8\u91cf\u6784\u6210\u3002\u5b83\u5e94\u5f53\u5305\u542b\u63cf\u8ff0\u89e3\u9898\u8fdb\u5ea6\u7684\u6240\u6709\u53d8\u91cf\uff0c\u5176\u5305\u542b\u4e86\u8db3\u591f\u7684\u4fe1\u606f\uff0c\u80fd\u591f\u7528\u6765\u63a8\u5bfc\u51fa\u4e0b\u4e00\u4e2a\u72b6\u6001\u3002

\u6bcf\u4e2a\u72b6\u6001\u90fd\u5bf9\u5e94\u4e00\u4e2a\u5b50\u95ee\u9898\uff0c\u6211\u4eec\u4f1a\u5b9a\u4e49\u4e00\u4e2a \\(dp\\) \u8868\u6765\u5b58\u50a8\u6240\u6709\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u72b6\u6001\u7684\u6bcf\u4e2a\u72ec\u7acb\u53d8\u91cf\u90fd\u662f \\(dp\\) \u8868\u7684\u4e00\u4e2a\u7ef4\u5ea6\u3002\u672c\u8d28\u4e0a\u770b\uff0c\\(dp\\) \u8868\u662f\u72b6\u6001\u548c\u5b50\u95ee\u9898\u7684\u89e3\u4e4b\u95f4\u7684\u6620\u5c04\u3002

\u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b

\u5bf9\u4e8e\u72b6\u6001 \\([i, j]\\) \uff0c\u5b83\u53ea\u80fd\u4ece\u4e0a\u8fb9\u683c\u5b50 \\([i-1, j]\\) \u548c\u5de6\u8fb9\u683c\u5b50 \\([i, j-1]\\) \u8f6c\u79fb\u800c\u6765\u3002\u56e0\u6b64\u6700\u4f18\u5b50\u7ed3\u6784\u4e3a\uff1a\u5230\u8fbe \\([i, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\u7531 \\([i, j-1]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\u4e0e \\([i-1, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\uff0c\u8fd9\u4e24\u8005\u8f83\u5c0f\u7684\u90a3\u4e00\u4e2a\u51b3\u5b9a\u3002

\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u53ef\u63a8\u51fa\u56fe 14-12 \u6240\u793a\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff1a

\\[ dp[i, j] = \\min(dp[i-1, j], dp[i, j-1]) + grid[i, j] \\]

\u56fe 14-12 \u00a0 \u6700\u4f18\u5b50\u7ed3\u6784\u4e0e\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b

Note

\u6839\u636e\u5b9a\u4e49\u597d\u7684 \\(dp\\) \u8868\uff0c\u601d\u8003\u539f\u95ee\u9898\u548c\u5b50\u95ee\u9898\u7684\u5173\u7cfb\uff0c\u627e\u51fa\u901a\u8fc7\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u6765\u6784\u9020\u539f\u95ee\u9898\u7684\u6700\u4f18\u89e3\u7684\u65b9\u6cd5\uff0c\u5373\u6700\u4f18\u5b50\u7ed3\u6784\u3002

\u4e00\u65e6\u6211\u4eec\u627e\u5230\u4e86\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u6784\u5efa\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u3002

\u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f

\u5728\u672c\u9898\u4e2d\uff0c\u9996\u884c\u7684\u72b6\u6001\u53ea\u80fd\u4ece\u5176\u5de6\u8fb9\u7684\u72b6\u6001\u5f97\u6765\uff0c\u9996\u5217\u7684\u72b6\u6001\u53ea\u80fd\u4ece\u5176\u4e0a\u8fb9\u7684\u72b6\u6001\u5f97\u6765\uff0c\u56e0\u6b64\u9996\u884c \\(i = 0\\) \u548c\u9996\u5217 \\(j = 0\\) \u662f\u8fb9\u754c\u6761\u4ef6\u3002

\u5982\u56fe 14-13 \u6240\u793a\uff0c\u7531\u4e8e\u6bcf\u4e2a\u683c\u5b50\u662f\u7531\u5176\u5de6\u65b9\u683c\u5b50\u548c\u4e0a\u65b9\u683c\u5b50\u8f6c\u79fb\u800c\u6765\uff0c\u56e0\u6b64\u6211\u4eec\u4f7f\u7528\u91c7\u7528\u5faa\u73af\u6765\u904d\u5386\u77e9\u9635\uff0c\u5916\u5faa\u73af\u904d\u5386\u5404\u884c\u3001\u5185\u5faa\u73af\u904d\u5386\u5404\u5217\u3002

\u56fe 14-13 \u00a0 \u8fb9\u754c\u6761\u4ef6\u4e0e\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f

Note

\u8fb9\u754c\u6761\u4ef6\u5728\u52a8\u6001\u89c4\u5212\u4e2d\u7528\u4e8e\u521d\u59cb\u5316 \\(dp\\) \u8868\uff0c\u5728\u641c\u7d22\u4e2d\u7528\u4e8e\u526a\u679d\u3002

\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f\u7684\u6838\u5fc3\u662f\u8981\u4fdd\u8bc1\u5728\u8ba1\u7b97\u5f53\u524d\u95ee\u9898\u7684\u89e3\u65f6\uff0c\u6240\u6709\u5b83\u4f9d\u8d56\u7684\u66f4\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\u90fd\u5df2\u7ecf\u88ab\u6b63\u786e\u5730\u8ba1\u7b97\u51fa\u6765\u3002

\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u6211\u4eec\u5df2\u7ecf\u53ef\u4ee5\u76f4\u63a5\u5199\u51fa\u52a8\u6001\u89c4\u5212\u4ee3\u7801\u3002\u7136\u800c\u5b50\u95ee\u9898\u5206\u89e3\u662f\u4e00\u79cd\u4ece\u9876\u81f3\u5e95\u7684\u601d\u60f3\uff0c\u56e0\u6b64\u6309\u7167\u201c\u66b4\u529b\u641c\u7d22 \\(\\rightarrow\\) \u8bb0\u5fc6\u5316\u641c\u7d22 \\(\\rightarrow\\) \u52a8\u6001\u89c4\u5212\u201d\u7684\u987a\u5e8f\u5b9e\u73b0\u66f4\u52a0\u7b26\u5408\u601d\u7ef4\u4e60\u60ef\u3002

"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1","title":"1. \u00a0 \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u641c\u7d22","text":"

\u4ece\u72b6\u6001 \\([i, j]\\) \u5f00\u59cb\u641c\u7d22\uff0c\u4e0d\u65ad\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u72b6\u6001 \\([i-1, j]\\) \u548c \\([i, j-1]\\) \uff0c\u9012\u5f52\u51fd\u6570\u5305\u62ec\u4ee5\u4e0b\u8981\u7d20\u3002

  • \u9012\u5f52\u53c2\u6570\uff1a\u72b6\u6001 \\([i, j]\\) \u3002
  • \u8fd4\u56de\u503c\uff1a\u4ece \\([0, 0]\\) \u5230 \\([i, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c \\(dp[i, j]\\) \u3002
  • \u7ec8\u6b62\u6761\u4ef6\uff1a\u5f53 \\(i = 0\\) \u4e14 \\(j = 0\\) \u65f6\uff0c\u8fd4\u56de\u4ee3\u4ef7 \\(grid[0, 0]\\) \u3002
  • \u526a\u679d\uff1a\u5f53 \\(i < 0\\) \u65f6\u6216 \\(j < 0\\) \u65f6\u7d22\u5f15\u8d8a\u754c\uff0c\u6b64\u65f6\u8fd4\u56de\u4ee3\u4ef7 \\(+\\infty\\) \uff0c\u4ee3\u8868\u4e0d\u53ef\u884c\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig min_path_sum.py
def min_path_sum_dfs(grid: list[list[int]], i: int, j: int) -> int:\n\"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22\"\"\"\n# \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif i == 0 and j == 0:\nreturn grid[0][0]\n# \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif i < 0 or j < 0:\nreturn inf\n# \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nup = min_path_sum_dfs(grid, i - 1, j)\nleft = min_path_sum_dfs(grid, i, j - 1)\n# \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn min(left, up) + grid[i][j]\n
min_path_sum.cpp
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(vector<vector<int>> &grid, int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 && j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\nreturn INT_MAX;\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint up = minPathSumDFS(grid, i - 1, j);\nint left = minPathSumDFS(grid, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\n}\n
min_path_sum.java
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(int[][] grid, int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 && j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\nreturn Integer.MAX_VALUE;\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint up = minPathSumDFS(grid, i - 1, j);\nint left = minPathSumDFS(grid, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn Math.min(left, up) + grid[i][j];\n}\n
min_path_sum.cs
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint MinPathSumDFS(int[][] grid, int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 && j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\nreturn int.MaxValue;\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint up = MinPathSumDFS(grid, i - 1, j);\nint left = MinPathSumDFS(grid, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn Math.Min(left, up) + grid[i][j];\n}\n
min_path_sum.go
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc minPathSumDFS(grid [][]int, i, j int) int {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif i == 0 && j == 0 {\nreturn grid[0][0]\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif i < 0 || j < 0 {\nreturn math.MaxInt\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nup := minPathSumDFS(grid, i-1, j)\nleft := minPathSumDFS(grid, i, j-1)\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn int(math.Min(float64(left), float64(up))) + grid[i][j]\n}\n
min_path_sum.swift
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc minPathSumDFS(grid: [[Int]], i: Int, j: Int) -> Int {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif i == 0, j == 0 {\nreturn grid[0][0]\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif i < 0 || j < 0 {\nreturn .max\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nlet up = minPathSumDFS(grid: grid, i: i - 1, j: j)\nlet left = minPathSumDFS(grid: grid, i: i, j: j - 1)\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn min(left, up) + grid[i][j]\n}\n
min_path_sum.js
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunction minPathSumDFS(grid, i, j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i === 0 && j === 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\nreturn Infinity;\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nconst up = minPathSumDFS(grid, i - 1, j);\nconst left = minPathSumDFS(grid, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn Math.min(left, up) + grid[i][j];\n}\n
min_path_sum.ts
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunction minPathSumDFS(\ngrid: Array<Array<number>>,\ni: number,\nj: number\n): number {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i === 0 && j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\nreturn Infinity;\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nconst up = minPathSumDFS(grid, i - 1, j);\nconst left = minPathSumDFS(grid, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn Math.min(left, up) + grid[i][j];\n}\n
min_path_sum.dart
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(List<List<int>> grid, int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 && j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\n// \u5728 Dart \u4e2d\uff0cint \u7c7b\u578b\u662f\u56fa\u5b9a\u8303\u56f4\u7684\u6574\u6570\uff0c\u4e0d\u5b58\u5728\u8868\u793a\u201c\u65e0\u7a77\u5927\u201d\u7684\u503c\nreturn BigInt.from(2).pow(31).toInt();\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint up = minPathSumDFS(grid, i - 1, j);\nint left = minPathSumDFS(grid, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn min(left, up) + grid[i][j];\n}\n
min_path_sum.rs
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfn min_path_sum_dfs(grid: &Vec<Vec<i32>>, i: i32, j: i32) -> i32 {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif i == 0 && j == 0 {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif i < 0 || j < 0 {\nreturn i32::MAX;\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nlet up = min_path_sum_dfs(grid, i - 1, j);\nlet left = min_path_sum_dfs(grid, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nstd::cmp::min(left, up) + grid[i as usize][j as usize]\n}\n
min_path_sum.c
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(int gridCols, int grid[][gridCols], int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 && j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\nreturn INT_MAX;\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint up = minPathSumDFS(gridCols, grid, i - 1, j);\nint left = minPathSumDFS(gridCols, grid, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\n}\n
min_path_sum.zig
// \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22\nfn minPathSumDFS(grid: anytype, i: i32, j: i32) i32 {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 and j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 or j < 0) {\nreturn std.math.maxInt(i32);\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nvar up = minPathSumDFS(grid, i - 1, j);\nvar left = minPathSumDFS(grid, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn @min(left, up) + grid[@as(usize, @intCast(i))][@as(usize, @intCast(j))];\n}\n

\u56fe 14-14 \u7ed9\u51fa\u4e86\u4ee5 \\(dp[2, 1]\\) \u4e3a\u6839\u8282\u70b9\u7684\u9012\u5f52\u6811\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e9b\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u5176\u6570\u91cf\u4f1a\u968f\u7740\u7f51\u683c grid \u7684\u5c3a\u5bf8\u53d8\u5927\u800c\u6025\u5267\u589e\u591a\u3002

\u672c\u8d28\u4e0a\u770b\uff0c\u9020\u6210\u91cd\u53e0\u5b50\u95ee\u9898\u7684\u539f\u56e0\u4e3a\uff1a\u5b58\u5728\u591a\u6761\u8def\u5f84\u53ef\u4ee5\u4ece\u5de6\u4e0a\u89d2\u5230\u8fbe\u67d0\u4e00\u5355\u5143\u683c\u3002

\u56fe 14-14 \u00a0 \u66b4\u529b\u641c\u7d22\u9012\u5f52\u6811

\u6bcf\u4e2a\u72b6\u6001\u90fd\u6709\u5411\u4e0b\u548c\u5411\u53f3\u4e24\u79cd\u9009\u62e9\uff0c\u4ece\u5de6\u4e0a\u89d2\u8d70\u5230\u53f3\u4e0b\u89d2\u603b\u5171\u9700\u8981 \\(m + n - 2\\) \u6b65\uff0c\u6240\u4ee5\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^{m + n})\\) \u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u79cd\u8ba1\u7b97\u65b9\u5f0f\u672a\u8003\u8651\u4e34\u8fd1\u7f51\u683c\u8fb9\u754c\u7684\u60c5\u51b5\uff0c\u5f53\u5230\u8fbe\u7f51\u7edc\u8fb9\u754c\u65f6\u53ea\u5269\u4e0b\u4e00\u79cd\u9009\u62e9\u3002\u56e0\u6b64\u5b9e\u9645\u7684\u8def\u5f84\u6570\u91cf\u4f1a\u5c11\u4e00\u4e9b\u3002

"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#2","title":"2. \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22","text":"

\u6211\u4eec\u5f15\u5165\u4e00\u4e2a\u548c\u7f51\u683c grid \u76f8\u540c\u5c3a\u5bf8\u7684\u8bb0\u5fc6\u5217\u8868 mem \uff0c\u7528\u4e8e\u8bb0\u5f55\u5404\u4e2a\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5e76\u5c06\u91cd\u53e0\u5b50\u95ee\u9898\u8fdb\u884c\u526a\u679d\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig min_path_sum.py
def min_path_sum_dfs_mem(\ngrid: list[list[int]], mem: list[list[int]], i: int, j: int\n) -> int:\n\"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n# \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif i == 0 and j == 0:\nreturn grid[0][0]\n# \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif i < 0 or j < 0:\nreturn inf\n# \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif mem[i][j] != -1:\nreturn mem[i][j]\n# \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nup = min_path_sum_dfs_mem(grid, mem, i - 1, j)\nleft = min_path_sum_dfs_mem(grid, mem, i, j - 1)\n# \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = min(left, up) + grid[i][j]\nreturn mem[i][j]\n
min_path_sum.cpp
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(vector<vector<int>> &grid, vector<vector<int>> &mem, int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 && j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\nreturn INT_MAX;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][j] != -1) {\nreturn mem[i][j];\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint up = minPathSumDFSMem(grid, mem, i - 1, j);\nint left = minPathSumDFSMem(grid, mem, i, j - 1);\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\nreturn mem[i][j];\n}\n
min_path_sum.java
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 && j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\nreturn Integer.MAX_VALUE;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][j] != -1) {\nreturn mem[i][j];\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint up = minPathSumDFSMem(grid, mem, i - 1, j);\nint left = minPathSumDFSMem(grid, mem, i, j - 1);\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = Math.min(left, up) + grid[i][j];\nreturn mem[i][j];\n}\n
min_path_sum.cs
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint MinPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 && j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\nreturn int.MaxValue;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][j] != -1) {\nreturn mem[i][j];\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint up = MinPathSumDFSMem(grid, mem, i - 1, j);\nint left = MinPathSumDFSMem(grid, mem, i, j - 1);\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = Math.Min(left, up) + grid[i][j];\nreturn mem[i][j];\n}\n
min_path_sum.go
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc minPathSumDFSMem(grid, mem [][]int, i, j int) int {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif i == 0 && j == 0 {\nreturn grid[0][0]\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif i < 0 || j < 0 {\nreturn math.MaxInt\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif mem[i][j] != -1 {\nreturn mem[i][j]\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nup := minPathSumDFSMem(grid, mem, i-1, j)\nleft := minPathSumDFSMem(grid, mem, i, j-1)\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = int(math.Min(float64(left), float64(up))) + grid[i][j]\nreturn mem[i][j]\n}\n
min_path_sum.swift
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc minPathSumDFSMem(grid: [[Int]], mem: inout [[Int]], i: Int, j: Int) -> Int {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif i == 0, j == 0 {\nreturn grid[0][0]\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif i < 0 || j < 0 {\nreturn .max\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif mem[i][j] != -1 {\nreturn mem[i][j]\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nlet up = minPathSumDFSMem(grid: grid, mem: &mem, i: i - 1, j: j)\nlet left = minPathSumDFSMem(grid: grid, mem: &mem, i: i, j: j - 1)\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = min(left, up) + grid[i][j]\nreturn mem[i][j]\n}\n
min_path_sum.js
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction minPathSumDFSMem(grid, mem, i, j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i === 0 && j === 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\nreturn Infinity;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][j] !== -1) {\nreturn mem[i][j];\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nconst up = minPathSumDFSMem(grid, mem, i - 1, j);\nconst left = minPathSumDFSMem(grid, mem, i, j - 1);\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = Math.min(left, up) + grid[i][j];\nreturn mem[i][j];\n}\n
min_path_sum.ts
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction minPathSumDFSMem(\ngrid: Array<Array<number>>,\nmem: Array<Array<number>>,\ni: number,\nj: number\n): number {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i === 0 && j === 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\nreturn Infinity;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][j] != -1) {\nreturn mem[i][j];\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nconst up = minPathSumDFSMem(grid, mem, i - 1, j);\nconst left = minPathSumDFSMem(grid, mem, i, j - 1);\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = Math.min(left, up) + grid[i][j];\nreturn mem[i][j];\n}\n
min_path_sum.dart
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(List<List<int>> grid, List<List<int>> mem, int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 && j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\n// \u5728 Dart \u4e2d\uff0cint \u7c7b\u578b\u662f\u56fa\u5b9a\u8303\u56f4\u7684\u6574\u6570\uff0c\u4e0d\u5b58\u5728\u8868\u793a\u201c\u65e0\u7a77\u5927\u201d\u7684\u503c\nreturn BigInt.from(2).pow(31).toInt();\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][j] != -1) {\nreturn mem[i][j];\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint up = minPathSumDFSMem(grid, mem, i - 1, j);\nint left = minPathSumDFSMem(grid, mem, i, j - 1);\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = min(left, up) + grid[i][j];\nreturn mem[i][j];\n}\n
min_path_sum.rs
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn min_path_sum_dfs_mem(grid: &Vec<Vec<i32>>, mem: &mut Vec<Vec<i32>>, i: i32, j: i32) -> i32 {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif i == 0 && j == 0 {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif i < 0 || j < 0 {\nreturn i32::MAX;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif mem[i as usize][j as usize] != -1 {\nreturn mem[i as usize][j as usize];\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nlet up = min_path_sum_dfs_mem(grid, mem, i - 1, j);\nlet left = min_path_sum_dfs_mem(grid, mem, i, j - 1);\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i as usize][j as usize] = std::cmp::min(left, up) + grid[i as usize][j as usize];\nmem[i as usize][j as usize]\n}\n
min_path_sum.c
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(int gridCols, int grid[][gridCols], int mem[][gridCols], int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 && j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 || j < 0) {\nreturn INT_MAX;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][j] != -1) {\nreturn mem[i][j];\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint up = minPathSumDFSMem(gridCols, grid, mem, i - 1, j);\nint left = minPathSumDFSMem(gridCols, grid, mem, i, j - 1);\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\nreturn mem[i][j];\n}\n
min_path_sum.zig
// \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\nfn minPathSumDFSMem(grid: anytype, mem: anytype, i: i32, j: i32) i32 {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 and j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i < 0 or j < 0) {\nreturn std.math.maxInt(i32);\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))] != -1) {\nreturn mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))];\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nvar up = minPathSumDFSMem(grid, mem, i - 1, j);\nvar left = minPathSumDFSMem(grid, mem, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[@as(usize, @intCast(i))][@as(usize, @intCast(j))] = @min(left, up) + grid[@as(usize, @intCast(i))][@as(usize, @intCast(j))];\nreturn mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))];\n}\n

\u5982\u56fe 14-15 \u6240\u793a\uff0c\u5728\u5f15\u5165\u8bb0\u5fc6\u5316\u540e\uff0c\u6240\u6709\u5b50\u95ee\u9898\u7684\u89e3\u53ea\u9700\u8ba1\u7b97\u4e00\u6b21\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u72b6\u6001\u603b\u6570\uff0c\u5373\u7f51\u683c\u5c3a\u5bf8 \\(O(nm)\\) \u3002

\u56fe 14-15 \u00a0 \u8bb0\u5fc6\u5316\u641c\u7d22\u9012\u5f52\u6811

"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#3","title":"3. \u00a0 \u65b9\u6cd5\u4e09\uff1a\u52a8\u6001\u89c4\u5212","text":"

\u57fa\u4e8e\u8fed\u4ee3\u5b9e\u73b0\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig min_path_sum.py
def min_path_sum_dp(grid: list[list[int]]) -> int:\n\"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nn, m = len(grid), len(grid[0])\n# \u521d\u59cb\u5316 dp \u8868\ndp = [[0] * m for _ in range(n)]\ndp[0][0] = grid[0][0]\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor j in range(1, m):\ndp[0][j] = dp[0][j - 1] + grid[0][j]\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor i in range(1, n):\ndp[i][0] = dp[i - 1][0] + grid[i][0]\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in range(1, n):\nfor j in range(1, m):\ndp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\nreturn dp[n - 1][m - 1]\n
min_path_sum.cpp
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(vector<vector<int>> &grid) {\nint n = grid.size(), m = grid[0].size();\n// \u521d\u59cb\u5316 dp \u8868\nvector<vector<int>> dp(n, vector<int>(m));\ndp[0][0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j < m; j++) {\ndp[0][j] = dp[0][j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor (int i = 1; i < n; i++) {\ndp[i][0] = dp[i - 1][0] + grid[i][0];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i < n; i++) {\nfor (int j = 1; j < m; j++) {\ndp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n}\n}\nreturn dp[n - 1][m - 1];\n}\n
min_path_sum.java
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(int[][] grid) {\nint n = grid.length, m = grid[0].length;\n// \u521d\u59cb\u5316 dp \u8868\nint[][] dp = new int[n][m];\ndp[0][0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j < m; j++) {\ndp[0][j] = dp[0][j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor (int i = 1; i < n; i++) {\ndp[i][0] = dp[i - 1][0] + grid[i][0];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i < n; i++) {\nfor (int j = 1; j < m; j++) {\ndp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n}\n}\nreturn dp[n - 1][m - 1];\n}\n
min_path_sum.cs
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint MinPathSumDP(int[][] grid) {\nint n = grid.Length, m = grid[0].Length;\n// \u521d\u59cb\u5316 dp \u8868\nint[,] dp = new int[n, m];\ndp[0, 0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j < m; j++) {\ndp[0, j] = dp[0, j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor (int i = 1; i < n; i++) {\ndp[i, 0] = dp[i - 1, 0] + grid[i][0];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i < n; i++) {\nfor (int j = 1; j < m; j++) {\ndp[i, j] = Math.Min(dp[i, j - 1], dp[i - 1, j]) + grid[i][j];\n}\n}\nreturn dp[n - 1, m - 1];\n}\n
min_path_sum.go
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDP(grid [][]int) int {\nn, m := len(grid), len(grid[0])\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([][]int, n)\nfor i := 0; i < n; i++ {\ndp[i] = make([]int, m)\n}\ndp[0][0] = grid[0][0]\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor j := 1; j < m; j++ {\ndp[0][j] = dp[0][j-1] + grid[0][j]\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor i := 1; i < n; i++ {\ndp[i][0] = dp[i-1][0] + grid[i][0]\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i := 1; i < n; i++ {\nfor j := 1; j < m; j++ {\ndp[i][j] = int(math.Min(float64(dp[i][j-1]), float64(dp[i-1][j]))) + grid[i][j]\n}\n}\nreturn dp[n-1][m-1]\n}\n
min_path_sum.swift
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDP(grid: [[Int]]) -> Int {\nlet n = grid.count\nlet m = grid[0].count\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: Array(repeating: 0, count: m), count: n)\ndp[0][0] = grid[0][0]\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor j in stride(from: 1, to: m, by: 1) {\ndp[0][j] = dp[0][j - 1] + grid[0][j]\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor i in stride(from: 1, to: n, by: 1) {\ndp[i][0] = dp[i - 1][0] + grid[i][0]\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in stride(from: 1, to: n, by: 1) {\nfor j in stride(from: 1, to: m, by: 1) {\ndp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n}\n}\nreturn dp[n - 1][m - 1]\n}\n
min_path_sum.js
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction minPathSumDP(grid) {\nconst n = grid.length,\nm = grid[0].length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: n }, () =>\nArray.from({ length: m }, () => 0)\n);\ndp[0][0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (let j = 1; j < m; j++) {\ndp[0][j] = dp[0][j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor (let i = 1; i < n; i++) {\ndp[i][0] = dp[i - 1][0] + grid[i][0];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (let i = 1; i < n; i++) {\nfor (let j = 1; j < m; j++) {\ndp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n}\n}\nreturn dp[n - 1][m - 1];\n}\n
min_path_sum.ts
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction minPathSumDP(grid: Array<Array<number>>): number {\nconst n = grid.length,\nm = grid[0].length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: n }, () =>\nArray.from({ length: m }, () => 0)\n);\ndp[0][0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (let j = 1; j < m; j++) {\ndp[0][j] = dp[0][j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor (let i = 1; i < n; i++) {\ndp[i][0] = dp[i - 1][0] + grid[i][0];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (let i = 1; i < n; i++) {\nfor (let j: number = 1; j < m; j++) {\ndp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n}\n}\nreturn dp[n - 1][m - 1];\n}\n
min_path_sum.dart
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(List<List<int>> grid) {\nint n = grid.length, m = grid[0].length;\n// \u521d\u59cb\u5316 dp \u8868\nList<List<int>> dp = List.generate(n, (i) => List.filled(m, 0));\ndp[0][0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j < m; j++) {\ndp[0][j] = dp[0][j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor (int i = 1; i < n; i++) {\ndp[i][0] = dp[i - 1][0] + grid[i][0];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i < n; i++) {\nfor (int j = 1; j < m; j++) {\ndp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n}\n}\nreturn dp[n - 1][m - 1];\n}\n
min_path_sum.rs
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfn min_path_sum_dp(grid: &Vec<Vec<i32>>) -> i32 {\nlet (n, m) = (grid.len(), grid[0].len());\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![vec![0; m]; n];\ndp[0][0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor j in 1..m {\ndp[0][j] = dp[0][j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor i in 1..n {\ndp[i][0] = dp[i - 1][0] + grid[i][0];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in 1..n {\nfor j in 1..m {\ndp[i][j] = std::cmp::min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n}\n}\ndp[n - 1][m - 1]\n}\n
min_path_sum.c
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(int gridCols, int grid[][gridCols], int n, int m) {\n// \u521d\u59cb\u5316 dp \u8868\nint dp[n][m];\ndp[0][0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j < m; j++) {\ndp[0][j] = dp[0][j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor (int i = 1; i < n; i++) {\ndp[i][0] = dp[i - 1][0] + grid[i][0];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i < n; i++) {\nfor (int j = 1; j < m; j++) {\ndp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n}\n}\nreturn dp[n - 1][m - 1];\n}\n
min_path_sum.zig
// \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212\nfn minPathSumDP(comptime grid: anytype) i32 {\ncomptime var n = grid.len;\ncomptime var m = grid[0].len;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_][m]i32{[_]i32{0} ** m} ** n;\ndp[0][0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (1..m) |j| {\ndp[0][j] = dp[0][j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor (1..n) |i| {\ndp[i][0] = dp[i - 1][0] + grid[i][0];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (1..n) |i| {\nfor (1..m) |j| {\ndp[i][j] = @min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n}\n}\nreturn dp[n - 1][m - 1];\n}\n

\u56fe 14-16 \u5c55\u793a\u4e86\u6700\u5c0f\u8def\u5f84\u548c\u7684\u72b6\u6001\u8f6c\u79fb\u8fc7\u7a0b\uff0c\u5176\u904d\u5386\u4e86\u6574\u4e2a\u7f51\u683c\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nm)\\) \u3002

\u6570\u7ec4 dp \u5927\u5c0f\u4e3a \\(n \\times m\\) \uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nm)\\) \u3002

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

\u56fe 14-16 \u00a0 \u6700\u5c0f\u8def\u5f84\u548c\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#4","title":"4. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

\u7531\u4e8e\u6bcf\u4e2a\u683c\u5b50\u53ea\u4e0e\u5176\u5de6\u8fb9\u548c\u4e0a\u8fb9\u7684\u683c\u5b50\u6709\u5173\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u53ea\u7528\u4e00\u4e2a\u5355\u884c\u6570\u7ec4\u6765\u5b9e\u73b0 \\(dp\\) \u8868\u3002

\u8bf7\u6ce8\u610f\uff0c\u56e0\u4e3a\u6570\u7ec4 dp \u53ea\u80fd\u8868\u793a\u4e00\u884c\u7684\u72b6\u6001\uff0c\u6240\u4ee5\u6211\u4eec\u65e0\u6cd5\u63d0\u524d\u521d\u59cb\u5316\u9996\u5217\u72b6\u6001\uff0c\u800c\u662f\u5728\u904d\u5386\u6bcf\u884c\u4e2d\u66f4\u65b0\u5b83\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig min_path_sum.py
def min_path_sum_dp_comp(grid: list[list[int]]) -> int:\n\"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\nn, m = len(grid), len(grid[0])\n# \u521d\u59cb\u5316 dp \u8868\ndp = [0] * m\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0]\nfor j in range(1, m):\ndp[j] = dp[j - 1] + grid[0][j]\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor i in range(1, n):\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0]\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor j in range(1, m):\ndp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\nreturn dp[m - 1]\n
min_path_sum.cpp
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(vector<vector<int>> &grid) {\nint n = grid.size(), m = grid[0].size();\n// \u521d\u59cb\u5316 dp \u8868\nvector<int> dp(m);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0];\nfor (int j = 1; j < m; j++) {\ndp[j] = dp[j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i < n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j < m; j++) {\ndp[j] = min(dp[j - 1], dp[j]) + grid[i][j];\n}\n}\nreturn dp[m - 1];\n}\n
min_path_sum.java
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(int[][] grid) {\nint n = grid.length, m = grid[0].length;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[m];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0];\nfor (int j = 1; j < m; j++) {\ndp[j] = dp[j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i < n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j < m; j++) {\ndp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n}\n}\nreturn dp[m - 1];\n}\n
min_path_sum.cs
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint MinPathSumDPComp(int[][] grid) {\nint n = grid.Length, m = grid[0].Length;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[m];\ndp[0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j < m; j++) {\ndp[j] = dp[j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i < n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j < m; j++) {\ndp[j] = Math.Min(dp[j - 1], dp[j]) + grid[i][j];\n}\n}\nreturn dp[m - 1];\n}\n
min_path_sum.go
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDPComp(grid [][]int) int {\nn, m := len(grid), len(grid[0])\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([]int, m)\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0]\nfor j := 1; j < m; j++ {\ndp[j] = dp[j-1] + grid[0][j]\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i := 1; i < n; i++ {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0]\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor j := 1; j < m; j++ {\ndp[j] = int(math.Min(float64(dp[j-1]), float64(dp[j]))) + grid[i][j]\n}\n}\nreturn dp[m-1]\n}\n
min_path_sum.swift
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDPComp(grid: [[Int]]) -> Int {\nlet n = grid.count\nlet m = grid[0].count\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: 0, count: m)\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0]\nfor j in stride(from: 1, to: m, by: 1) {\ndp[j] = dp[j - 1] + grid[0][j]\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor i in stride(from: 1, to: n, by: 1) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0]\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor j in stride(from: 1, to: m, by: 1) {\ndp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n}\n}\nreturn dp[m - 1]\n}\n
min_path_sum.js
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction minPathSumDPComp(grid) {\nconst n = grid.length,\nm = grid[0].length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = new Array(m);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0];\nfor (let j = 1; j < m; j++) {\ndp[j] = dp[j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (let i = 1; i < n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (let j = 1; j < m; j++) {\ndp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n}\n}\nreturn dp[m - 1];\n}\n
min_path_sum.ts
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction minPathSumDPComp(grid: Array<Array<number>>): number {\nconst n = grid.length,\nm = grid[0].length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = new Array(m);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0];\nfor (let j = 1; j < m; j++) {\ndp[j] = dp[j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (let i = 1; i < n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (let j = 1; j < m; j++) {\ndp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n}\n}\nreturn dp[m - 1];\n}\n
min_path_sum.dart
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(List<List<int>> grid) {\nint n = grid.length, m = grid[0].length;\n// \u521d\u59cb\u5316 dp \u8868\nList<int> dp = List.filled(m, 0);\ndp[0] = grid[0][0];\nfor (int j = 1; j < m; j++) {\ndp[j] = dp[j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i < n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j < m; j++) {\ndp[j] = min(dp[j - 1], dp[j]) + grid[i][j];\n}\n}\nreturn dp[m - 1];\n}\n
min_path_sum.rs
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn min_path_sum_dp_comp(grid: &Vec<Vec<i32>>) -> i32 {\nlet (n, m) = (grid.len(), grid[0].len());\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![0; m];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0];\nfor j in 1..m {\ndp[j] = dp[j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor i in 1..n {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor j in 1..m {\ndp[j] = std::cmp::min(dp[j - 1], dp[j]) + grid[i][j];\n}\n}\ndp[m - 1]\n}\n
min_path_sum.c
/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(int gridCols, int grid[][gridCols], int n, int m) {\n// \u521d\u59cb\u5316 dp \u8868\nint dp[m];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0];\nfor (int j = 1; j < m; j++) {\ndp[j] = dp[j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i < n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j < m; j++) {\ndp[j] = min(dp[j - 1], dp[j]) + grid[i][j];\n}\n}\nreturn dp[m - 1];\n}\n
min_path_sum.zig
// \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn minPathSumDPComp(comptime grid: anytype) i32 {\ncomptime var n = grid.len;\ncomptime var m = grid[0].len;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_]i32{0} ** m;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0];\nfor (1..m) |j| {\ndp[j] = dp[j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (1..n) |i| {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0];\nfor (1..m) |j| {\ndp[j] = @min(dp[j - 1], dp[j]) + grid[i][j];\n}\n}\nreturn dp[m - 1];\n}\n
"},{"location":"chapter_dynamic_programming/edit_distance_problem/","title":"14.6 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898","text":"

\u7f16\u8f91\u8ddd\u79bb\uff0c\u4e5f\u88ab\u79f0\u4e3a Levenshtein \u8ddd\u79bb\uff0c\u6307\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e4b\u95f4\u4e92\u76f8\u8f6c\u6362\u7684\u6700\u5c0f\u4fee\u6539\u6b21\u6570\uff0c\u901a\u5e38\u7528\u4e8e\u5728\u4fe1\u606f\u68c0\u7d22\u548c\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4e2d\u5ea6\u91cf\u4e24\u4e2a\u5e8f\u5217\u7684\u76f8\u4f3c\u5ea6\u3002

Question

\u8f93\u5165\u4e24\u4e2a\u5b57\u7b26\u4e32 \\(s\\) \u548c \\(t\\) \uff0c\u8fd4\u56de\u5c06 \\(s\\) \u8f6c\u6362\u4e3a \\(t\\) \u6240\u9700\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u3002

\u4f60\u53ef\u4ee5\u5728\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u8fdb\u884c\u4e09\u79cd\u7f16\u8f91\u64cd\u4f5c\uff1a\u63d2\u5165\u4e00\u4e2a\u5b57\u7b26\u3001\u5220\u9664\u4e00\u4e2a\u5b57\u7b26\u3001\u66ff\u6362\u5b57\u7b26\u4e3a\u4efb\u610f\u4e00\u4e2a\u5b57\u7b26\u3002

\u5982\u56fe 14-27 \u6240\u793a\uff0c\u5c06 kitten \u8f6c\u6362\u4e3a sitting \u9700\u8981\u7f16\u8f91 3 \u6b65\uff0c\u5305\u62ec 2 \u6b21\u66ff\u6362\u64cd\u4f5c\u4e0e 1 \u6b21\u6dfb\u52a0\u64cd\u4f5c\uff1b\u5c06 hello \u8f6c\u6362\u4e3a algo \u9700\u8981 3 \u6b65\uff0c\u5305\u62ec 2 \u6b21\u66ff\u6362\u64cd\u4f5c\u548c 1 \u6b21\u5220\u9664\u64cd\u4f5c\u3002

\u56fe 14-27 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u7684\u793a\u4f8b\u6570\u636e

\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898\u53ef\u4ee5\u5f88\u81ea\u7136\u5730\u7528\u51b3\u7b56\u6811\u6a21\u578b\u6765\u89e3\u91ca\u3002\u5b57\u7b26\u4e32\u5bf9\u5e94\u6811\u8282\u70b9\uff0c\u4e00\u8f6e\u51b3\u7b56\uff08\u4e00\u6b21\u7f16\u8f91\u64cd\u4f5c\uff09\u5bf9\u5e94\u6811\u7684\u4e00\u6761\u8fb9\u3002

\u5982\u56fe 14-28 \u6240\u793a\uff0c\u5728\u4e0d\u9650\u5236\u64cd\u4f5c\u7684\u60c5\u51b5\u4e0b\uff0c\u6bcf\u4e2a\u8282\u70b9\u90fd\u53ef\u4ee5\u6d3e\u751f\u51fa\u8bb8\u591a\u6761\u8fb9\uff0c\u6bcf\u6761\u8fb9\u5bf9\u5e94\u4e00\u79cd\u64cd\u4f5c\uff0c\u8fd9\u610f\u5473\u7740\u4ece hello \u8f6c\u6362\u5230 algo \u6709\u8bb8\u591a\u79cd\u53ef\u80fd\u7684\u8def\u5f84\u3002

\u4ece\u51b3\u7b56\u6811\u7684\u89d2\u5ea6\u770b\uff0c\u672c\u9898\u7684\u76ee\u6807\u662f\u6c42\u89e3\u8282\u70b9 hello \u548c\u8282\u70b9 algo \u4e4b\u95f4\u7684\u6700\u77ed\u8def\u5f84\u3002

\u56fe 14-28 \u00a0 \u57fa\u4e8e\u51b3\u7b56\u6811\u6a21\u578b\u8868\u793a\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898

"},{"location":"chapter_dynamic_programming/edit_distance_problem/#1","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"

\u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868

\u6bcf\u4e00\u8f6e\u7684\u51b3\u7b56\u662f\u5bf9\u5b57\u7b26\u4e32 \\(s\\) \u8fdb\u884c\u4e00\u6b21\u7f16\u8f91\u64cd\u4f5c\u3002

\u6211\u4eec\u5e0c\u671b\u5728\u7f16\u8f91\u64cd\u4f5c\u7684\u8fc7\u7a0b\u4e2d\uff0c\u95ee\u9898\u7684\u89c4\u6a21\u9010\u6e10\u7f29\u5c0f\uff0c\u8fd9\u6837\u624d\u80fd\u6784\u5efa\u5b50\u95ee\u9898\u3002\u8bbe\u5b57\u7b26\u4e32 \\(s\\) \u548c \\(t\\) \u7684\u957f\u5ea6\u5206\u522b\u4e3a \\(n\\) \u548c \\(m\\) \uff0c\u6211\u4eec\u5148\u8003\u8651\u4e24\u5b57\u7b26\u4e32\u5c3e\u90e8\u7684\u5b57\u7b26 \\(s[n-1]\\) \u548c \\(t[m-1]\\) \u3002

  • \u82e5 \\(s[n-1]\\) \u548c \\(t[m-1]\\) \u76f8\u540c\uff0c\u6211\u4eec\u53ef\u4ee5\u8df3\u8fc7\u5b83\u4eec\uff0c\u76f4\u63a5\u8003\u8651 \\(s[n-2]\\) \u548c \\(t[m-2]\\) \u3002
  • \u82e5 \\(s[n-1]\\) \u548c \\(t[m-1]\\) \u4e0d\u540c\uff0c\u6211\u4eec\u9700\u8981\u5bf9 \\(s\\) \u8fdb\u884c\u4e00\u6b21\u7f16\u8f91\uff08\u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\uff09\uff0c\u4f7f\u5f97\u4e24\u5b57\u7b26\u4e32\u5c3e\u90e8\u7684\u5b57\u7b26\u76f8\u540c\uff0c\u4ece\u800c\u53ef\u4ee5\u8df3\u8fc7\u5b83\u4eec\uff0c\u8003\u8651\u89c4\u6a21\u66f4\u5c0f\u7684\u95ee\u9898\u3002

\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u5728\u5b57\u7b26\u4e32 \\(s\\) \u4e2d\u8fdb\u884c\u7684\u6bcf\u4e00\u8f6e\u51b3\u7b56\uff08\u7f16\u8f91\u64cd\u4f5c\uff09\uff0c\u90fd\u4f1a\u4f7f\u5f97 \\(s\\) \u548c \\(t\\) \u4e2d\u5269\u4f59\u7684\u5f85\u5339\u914d\u5b57\u7b26\u53d1\u751f\u53d8\u5316\u3002\u56e0\u6b64\uff0c\u72b6\u6001\u4e3a\u5f53\u524d\u5728 \\(s\\) \u548c \\(t\\) \u4e2d\u8003\u8651\u7684\u7b2c \\(i\\) \u548c \\(j\\) \u4e2a\u5b57\u7b26\uff0c\u8bb0\u4e3a \\([i, j]\\) \u3002

\u72b6\u6001 \\([i, j]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\uff1a\u5c06 \\(s\\) \u7684\u524d \\(i\\) \u4e2a\u5b57\u7b26\u66f4\u6539\u4e3a \\(t\\) \u7684\u524d \\(j\\) \u4e2a\u5b57\u7b26\u6240\u9700\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u3002

\u81f3\u6b64\uff0c\u5f97\u5230\u4e00\u4e2a\u5c3a\u5bf8\u4e3a \\((i+1) \\times (j+1)\\) \u7684\u4e8c\u7ef4 \\(dp\\) \u8868\u3002

\u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b

\u8003\u8651\u5b50\u95ee\u9898 \\(dp[i, j]\\) \uff0c\u5176\u5bf9\u5e94\u7684\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u5c3e\u90e8\u5b57\u7b26\u4e3a \\(s[i-1]\\) \u548c \\(t[j-1]\\) \uff0c\u53ef\u6839\u636e\u4e0d\u540c\u7f16\u8f91\u64cd\u4f5c\u5206\u4e3a\u56fe 14-29 \u6240\u793a\u7684\u4e09\u79cd\u60c5\u51b5\u3002

  1. \u5728 \\(s[i-1]\\) \u4e4b\u540e\u6dfb\u52a0 \\(t[j-1]\\) \uff0c\u5219\u5269\u4f59\u5b50\u95ee\u9898 \\(dp[i, j-1]\\) \u3002
  2. \u5220\u9664 \\(s[i-1]\\) \uff0c\u5219\u5269\u4f59\u5b50\u95ee\u9898 \\(dp[i-1, j]\\) \u3002
  3. \u5c06 \\(s[i-1]\\) \u66ff\u6362\u4e3a \\(t[j-1]\\) \uff0c\u5219\u5269\u4f59\u5b50\u95ee\u9898 \\(dp[i-1, j-1]\\) \u3002

\u56fe 14-29 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u7684\u72b6\u6001\u8f6c\u79fb

\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u53ef\u5f97\u6700\u4f18\u5b50\u7ed3\u6784\uff1a\\(dp[i, j]\\) \u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u7b49\u4e8e \\(dp[i, j-1]\\)\u3001\\(dp[i-1, j]\\)\u3001\\(dp[i-1, j-1]\\) \u4e09\u8005\u4e2d\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\uff0c\u518d\u52a0\u4e0a\u672c\u6b21\u7684\u7f16\u8f91\u6b65\u6570 \\(1\\) \u3002\u5bf9\u5e94\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a

\\[ dp[i, j] = \\min(dp[i, j-1], dp[i-1, j], dp[i-1, j-1]) + 1 \\]

\u8bf7\u6ce8\u610f\uff0c\u5f53 \\(s[i-1]\\) \u548c \\(t[j-1]\\) \u76f8\u540c\u65f6\uff0c\u65e0\u987b\u7f16\u8f91\u5f53\u524d\u5b57\u7b26\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a

\\[ dp[i, j] = dp[i-1, j-1] \\]

\u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f

\u5f53\u4e24\u5b57\u7b26\u4e32\u90fd\u4e3a\u7a7a\u65f6\uff0c\u7f16\u8f91\u6b65\u6570\u4e3a \\(0\\) \uff0c\u5373 \\(dp[0, 0] = 0\\) \u3002\u5f53 \\(s\\) \u4e3a\u7a7a\u4f46 \\(t\\) \u4e0d\u4e3a\u7a7a\u65f6\uff0c\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u7b49\u4e8e \\(t\\) \u7684\u957f\u5ea6\uff0c\u5373\u9996\u884c \\(dp[0, j] = j\\) \u3002\u5f53 \\(s\\) \u4e0d\u4e3a\u7a7a\u4f46 \\(t\\) \u4e3a\u7a7a\u65f6\uff0c\u7b49\u4e8e \\(s\\) \u7684\u957f\u5ea6\uff0c\u5373\u9996\u5217 \\(dp[i, 0] = i\\) \u3002

\u89c2\u5bdf\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff0c\u89e3 \\(dp[i, j]\\) \u4f9d\u8d56\u5de6\u65b9\u3001\u4e0a\u65b9\u3001\u5de6\u4e0a\u65b9\u7684\u89e3\uff0c\u56e0\u6b64\u901a\u8fc7\u4e24\u5c42\u5faa\u73af\u6b63\u5e8f\u904d\u5386\u6574\u4e2a \\(dp\\) \u8868\u5373\u53ef\u3002

"},{"location":"chapter_dynamic_programming/edit_distance_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"PythonC++JavaC#GoSwiftJSTSDartRustCZig edit_distance.py
def edit_distance_dp(s: str, t: str) -> int:\n\"\"\"\u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nn, m = len(s), len(t)\ndp = [[0] * (m + 1) for _ in range(n + 1)]\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor i in range(1, n + 1):\ndp[i][0] = i\nfor j in range(1, m + 1):\ndp[0][j] = j\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in range(1, n + 1):\nfor j in range(1, m + 1):\nif s[i - 1] == t[j - 1]:\n# \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1]\nelse:\n# \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1\nreturn dp[n][m]\n
edit_distance.cpp
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(string s, string t) {\nint n = s.length(), m = t.length();\nvector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int i = 1; i <= n; i++) {\ndp[i][0] = i;\n}\nfor (int j = 1; j <= m; j++) {\ndp[0][j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i <= n; i++) {\nfor (int j = 1; j <= m; j++) {\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1];\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n}\n}\n}\nreturn dp[n][m];\n}\n
edit_distance.java
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(String s, String t) {\nint n = s.length(), m = t.length();\nint[][] dp = new int[n + 1][m + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int i = 1; i <= n; i++) {\ndp[i][0] = i;\n}\nfor (int j = 1; j <= m; j++) {\ndp[0][j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i <= n; i++) {\nfor (int j = 1; j <= m; j++) {\nif (s.charAt(i - 1) == t.charAt(j - 1)) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1];\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n}\n}\n}\nreturn dp[n][m];\n}\n
edit_distance.cs
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint EditDistanceDP(string s, string t) {\nint n = s.Length, m = t.Length;\nint[,] dp = new int[n + 1, m + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int i = 1; i <= n; i++) {\ndp[i, 0] = i;\n}\nfor (int j = 1; j <= m; j++) {\ndp[0, j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i <= n; i++) {\nfor (int j = 1; j <= m; j++) {\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i, j] = dp[i - 1, j - 1];\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i, j] = Math.Min(Math.Min(dp[i, j - 1], dp[i - 1, j]), dp[i - 1, j - 1]) + 1;\n}\n}\n}\nreturn dp[n, m];\n}\n
edit_distance.go
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDP(s string, t string) int {\nn := len(s)\nm := len(t)\ndp := make([][]int, n+1)\nfor i := 0; i <= n; i++ {\ndp[i] = make([]int, m+1)\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor i := 1; i <= n; i++ {\ndp[i][0] = i\n}\nfor j := 1; j <= m; j++ {\ndp[0][j] = j\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i := 1; i <= n; i++ {\nfor j := 1; j <= m; j++ {\nif s[i-1] == t[j-1] {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i-1][j-1]\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = MinInt(MinInt(dp[i][j-1], dp[i-1][j]), dp[i-1][j-1]) + 1\n}\n}\n}\nreturn dp[n][m]\n}\n
edit_distance.swift
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDP(s: String, t: String) -> Int {\nlet n = s.utf8CString.count\nlet m = t.utf8CString.count\nvar dp = Array(repeating: Array(repeating: 0, count: m + 1), count: n + 1)\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor i in stride(from: 1, through: n, by: 1) {\ndp[i][0] = i\n}\nfor j in stride(from: 1, through: m, by: 1) {\ndp[0][j] = j\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in stride(from: 1, through: n, by: 1) {\nfor j in stride(from: 1, through: m, by: 1) {\nif s.utf8CString[i - 1] == t.utf8CString[j - 1] {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1]\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1\n}\n}\n}\nreturn dp[n][m]\n}\n
edit_distance.js
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction editDistanceDP(s, t) {\nconst n = s.length,\nm = t.length;\nconst dp = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0));\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (let i = 1; i <= n; i++) {\ndp[i][0] = i;\n}\nfor (let j = 1; j <= m; j++) {\ndp[0][j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (let i = 1; i <= n; i++) {\nfor (let j = 1; j <= m; j++) {\nif (s.charAt(i - 1) === t.charAt(j - 1)) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1];\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] =\nMath.min(\nMath.min(dp[i][j - 1], dp[i - 1][j]),\ndp[i - 1][j - 1]\n) + 1;\n}\n}\n}\nreturn dp[n][m];\n}\n
edit_distance.ts
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction editDistanceDP(s: string, t: string): number {\nconst n = s.length,\nm = t.length;\nconst dp = Array.from({ length: n + 1 }, () =>\nArray.from({ length: m + 1 }, () => 0)\n);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (let i = 1; i <= n; i++) {\ndp[i][0] = i;\n}\nfor (let j = 1; j <= m; j++) {\ndp[0][j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (let i = 1; i <= n; i++) {\nfor (let j = 1; j <= m; j++) {\nif (s.charAt(i - 1) === t.charAt(j - 1)) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1];\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] =\nMath.min(\nMath.min(dp[i][j - 1], dp[i - 1][j]),\ndp[i - 1][j - 1]\n) + 1;\n}\n}\n}\nreturn dp[n][m];\n}\n
edit_distance.dart
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(String s, String t) {\nint n = s.length, m = t.length;\nList<List<int>> dp = List.generate(n + 1, (_) => List.filled(m + 1, 0));\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int i = 1; i <= n; i++) {\ndp[i][0] = i;\n}\nfor (int j = 1; j <= m; j++) {\ndp[0][j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i <= n; i++) {\nfor (int j = 1; j <= m; j++) {\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1];\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n}\n}\n}\nreturn dp[n][m];\n}\n
edit_distance.rs
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfn edit_distance_dp(s: &str, t: &str) -> i32 {\nlet (n, m) = (s.len(), t.len());\nlet mut dp = vec![vec![0; m + 1]; n + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor i in 1..= n {\ndp[i][0] = i as i32;\n}\nfor j in 1..m {\ndp[0][j] = j as i32;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in 1..=n {\nfor j in 1..=m {\nif s.chars().nth(i - 1) == t.chars().nth(j - 1) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1];\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = std::cmp::min(std::cmp::min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n}\n}\n}\ndp[n][m]\n}\n
edit_distance.c
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(char *s, char *t, int n, int m) {\nint dp[n + 1][m + 1];\nmemset(dp, 0, sizeof(dp));\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int i = 1; i <= n; i++) {\ndp[i][0] = i;\n}\nfor (int j = 1; j <= m; j++) {\ndp[0][j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i <= n; i++) {\nfor (int j = 1; j <= m; j++) {\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1];\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n}\n}\n}\nreturn dp[n][m];\n}\n
edit_distance.zig
// \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212\nfn editDistanceDP(comptime s: []const u8, comptime t: []const u8) i32 {\ncomptime var n = s.len;\ncomptime var m = t.len;\nvar dp = [_][m + 1]i32{[_]i32{0} ** (m + 1)} ** (n + 1);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (1..n + 1) |i| {\ndp[i][0] = @intCast(i);\n}\nfor (1..m + 1) |j| {\ndp[0][j] = @intCast(j);\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (1..n + 1) |i| {\nfor (1..m + 1) |j| {\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1];\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = @min(@min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n}\n}\n}\nreturn dp[n][m];\n}\n

\u5982\u56fe 14-30 \u6240\u793a\uff0c\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898\u7684\u72b6\u6001\u8f6c\u79fb\u8fc7\u7a0b\u4e0e\u80cc\u5305\u95ee\u9898\u975e\u5e38\u7c7b\u4f3c\uff0c\u90fd\u53ef\u4ee5\u770b\u4f5c\u662f\u586b\u5199\u4e00\u4e2a\u4e8c\u7ef4\u7f51\u683c\u7684\u8fc7\u7a0b\u3002

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

\u56fe 14-30 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

"},{"location":"chapter_dynamic_programming/edit_distance_problem/#3","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

\u7531\u4e8e \\(dp[i,j]\\) \u662f\u7531\u4e0a\u65b9 \\(dp[i-1, j]\\)\u3001\u5de6\u65b9 \\(dp[i, j-1]\\)\u3001\u5de6\u4e0a\u65b9\u72b6\u6001 \\(dp[i-1, j-1]\\) \u8f6c\u79fb\u800c\u6765\uff0c\u800c\u6b63\u5e8f\u904d\u5386\u4f1a\u4e22\u5931\u5de6\u4e0a\u65b9 \\(dp[i-1, j-1]\\) \uff0c\u5012\u5e8f\u904d\u5386\u65e0\u6cd5\u63d0\u524d\u6784\u5efa \\(dp[i, j-1]\\) \uff0c\u56e0\u6b64\u4e24\u79cd\u904d\u5386\u987a\u5e8f\u90fd\u4e0d\u53ef\u53d6\u3002

\u4e3a\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u53d8\u91cf leftup \u6765\u6682\u5b58\u5de6\u4e0a\u65b9\u7684\u89e3 \\(dp[i-1, j-1]\\) \uff0c\u4ece\u800c\u53ea\u9700\u8003\u8651\u5de6\u65b9\u548c\u4e0a\u65b9\u7684\u89e3\u3002\u6b64\u65f6\u7684\u60c5\u51b5\u4e0e\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u76f8\u540c\uff0c\u53ef\u4f7f\u7528\u6b63\u5e8f\u904d\u5386\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig edit_distance.py
def edit_distance_dp_comp(s: str, t: str) -> int:\n\"\"\"\u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\nn, m = len(s), len(t)\ndp = [0] * (m + 1)\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor j in range(1, m + 1):\ndp[j] = j\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor i in range(1, n + 1):\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nleftup = dp[0]  # \u6682\u5b58 dp[i-1, j-1]\ndp[0] += 1\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor j in range(1, m + 1):\ntemp = dp[j]\nif s[i - 1] == t[j - 1]:\n# \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup\nelse:\n# \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = min(dp[j - 1], dp[j], leftup) + 1\nleftup = temp  # \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\nreturn dp[m]\n
edit_distance.cpp
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(string s, string t) {\nint n = s.length(), m = t.length();\nvector<int> dp(m + 1, 0);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j <= m; j++) {\ndp[j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i <= n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nint leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j <= m; j++) {\nint temp = dp[j];\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup;\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;\n}\nleftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m];\n}\n
edit_distance.java
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(String s, String t) {\nint n = s.length(), m = t.length();\nint[] dp = new int[m + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j <= m; j++) {\ndp[j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i <= n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nint leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j <= m; j++) {\nint temp = dp[j];\nif (s.charAt(i - 1) == t.charAt(j - 1)) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup;\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = Math.min(Math.min(dp[j - 1], dp[j]), leftup) + 1;\n}\nleftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m];\n}\n
edit_distance.cs
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint EditDistanceDPComp(string s, string t) {\nint n = s.Length, m = t.Length;\nint[] dp = new int[m + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j <= m; j++) {\ndp[j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i <= n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nint leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j <= m; j++) {\nint temp = dp[j];\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup;\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = Math.Min(Math.Min(dp[j - 1], dp[j]), leftup) + 1;\n}\nleftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m];\n}\n
edit_distance.go
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDPComp(s string, t string) int {\nn := len(s)\nm := len(t)\ndp := make([]int, m+1)\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor j := 1; j <= m; j++ {\ndp[j] = j\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor i := 1; i <= n; i++ {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nleftUp := dp[0] // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor j := 1; j <= m; j++ {\ntemp := dp[j]\nif s[i-1] == t[j-1] {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftUp\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = MinInt(MinInt(dp[j-1], dp[j]), leftUp) + 1\n}\nleftUp = temp // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m]\n}\n
edit_distance.swift
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDPComp(s: String, t: String) -> Int {\nlet n = s.utf8CString.count\nlet m = t.utf8CString.count\nvar dp = Array(repeating: 0, count: m + 1)\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor j in stride(from: 1, through: m, by: 1) {\ndp[j] = j\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor i in stride(from: 1, through: n, by: 1) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nvar leftup = dp[0] // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor j in stride(from: 1, through: m, by: 1) {\nlet temp = dp[j]\nif s.utf8CString[i - 1] == t.utf8CString[j - 1] {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1\n}\nleftup = temp // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m]\n}\n
edit_distance.js
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction editDistanceDPComp(s, t) {\nconst n = s.length,\nm = t.length;\nconst dp = new Array(m + 1).fill(0);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (let j = 1; j <= m; j++) {\ndp[j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (let i = 1; i <= n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nlet leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (let j = 1; j <= m; j++) {\nconst temp = dp[j];\nif (s.charAt(i - 1) === t.charAt(j - 1)) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup;\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = Math.min(Math.min(dp[j - 1], dp[j]), leftup) + 1;\n}\nleftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m];\n}\n
edit_distance.ts
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction editDistanceDPComp(s: string, t: string): number {\nconst n = s.length,\nm = t.length;\nconst dp = new Array(m + 1).fill(0);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (let j = 1; j <= m; j++) {\ndp[j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (let i = 1; i <= n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nlet leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (let j = 1; j <= m; j++) {\nconst temp = dp[j];\nif (s.charAt(i - 1) === t.charAt(j - 1)) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup;\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = Math.min(Math.min(dp[j - 1], dp[j]), leftup) + 1;\n}\nleftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m];\n}\n
edit_distance.dart
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(String s, String t) {\nint n = s.length, m = t.length;\nList<int> dp = List.filled(m + 1, 0);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j <= m; j++) {\ndp[j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i <= n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nint leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j <= m; j++) {\nint temp = dp[j];\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup;\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;\n}\nleftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m];\n}\n
edit_distance.rs
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn edit_distance_dp_comp(s: &str, t: &str) -> i32 {\nlet (n, m) = (s.len(), t.len());\nlet mut dp = vec![0; m + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor j in 1..m {\ndp[j] = j as i32;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor i in 1..=n {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nlet mut leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i as i32;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor j in 1..=m {\nlet temp = dp[j];\nif s.chars().nth(i - 1) == t.chars().nth(j - 1) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup;\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = std::cmp::min(std::cmp::min(dp[j - 1], dp[j]), leftup) + 1;\n}\nleftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\ndp[m]\n}\n
edit_distance.c
/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(char *s, char *t, int n, int m) {\nint dp[m + 1];\nmemset(dp, 0, sizeof(dp));\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j <= m; j++) {\ndp[j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i <= n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nint leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j <= m; j++) {\nint temp = dp[j];\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup;\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;\n}\nleftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m];\n}\n
edit_distance.zig
// \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn editDistanceDPComp(comptime s: []const u8, comptime t: []const u8) i32 {\ncomptime var n = s.len;\ncomptime var m = t.len;\nvar dp = [_]i32{0} ** (m + 1);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (1..m + 1) |j| {\ndp[j] = @intCast(j);\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (1..n + 1) |i| {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nvar leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = @intCast(i);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (1..m + 1) |j| {\nvar temp = dp[j];\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup;\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = @min(@min(dp[j - 1], dp[j]), leftup) + 1;\n}\nleftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m];\n}\n
"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/","title":"14.1 \u00a0 \u521d\u63a2\u52a8\u6001\u89c4\u5212","text":"

\u300c\u52a8\u6001\u89c4\u5212 dynamic programming\u300d\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u7b97\u6cd5\u8303\u5f0f\uff0c\u5b83\u5c06\u4e00\u4e2a\u95ee\u9898\u5206\u89e3\u4e3a\u4e00\u7cfb\u5217\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u5e76\u901a\u8fc7\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\u6765\u907f\u514d\u91cd\u590d\u8ba1\u7b97\uff0c\u4ece\u800c\u5927\u5e45\u63d0\u5347\u65f6\u95f4\u6548\u7387\u3002

\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u4ece\u4e00\u4e2a\u7ecf\u5178\u4f8b\u9898\u5165\u624b\uff0c\u5148\u7ed9\u51fa\u5b83\u7684\u66b4\u529b\u56de\u6eaf\u89e3\u6cd5\uff0c\u89c2\u5bdf\u5176\u4e2d\u5305\u542b\u7684\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u518d\u9010\u6b65\u5bfc\u51fa\u66f4\u9ad8\u6548\u7684\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\u3002

\u722c\u697c\u68af

\u7ed9\u5b9a\u4e00\u4e2a\u5171\u6709 \\(n\\) \u9636\u7684\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\uff0c\u8bf7\u95ee\u6709\u591a\u5c11\u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\u3002

\u5982\u56fe 14-1 \u6240\u793a\uff0c\u5bf9\u4e8e\u4e00\u4e2a \\(3\\) \u9636\u697c\u68af\uff0c\u5171\u6709 \\(3\\) \u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\u3002

\u56fe 14-1 \u00a0 \u722c\u5230\u7b2c 3 \u9636\u7684\u65b9\u6848\u6570\u91cf

\u672c\u9898\u7684\u76ee\u6807\u662f\u6c42\u89e3\u65b9\u6848\u6570\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u901a\u8fc7\u56de\u6eaf\u6765\u7a77\u4e3e\u6240\u6709\u53ef\u80fd\u6027\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5c06\u722c\u697c\u68af\u60f3\u8c61\u4e3a\u4e00\u4e2a\u591a\u8f6e\u9009\u62e9\u7684\u8fc7\u7a0b\uff1a\u4ece\u5730\u9762\u51fa\u53d1\uff0c\u6bcf\u8f6e\u9009\u62e9\u4e0a \\(1\\) \u9636\u6216 \\(2\\) \u9636\uff0c\u6bcf\u5f53\u5230\u8fbe\u697c\u68af\u9876\u90e8\u65f6\u5c31\u5c06\u65b9\u6848\u6570\u91cf\u52a0 \\(1\\) \uff0c\u5f53\u8d8a\u8fc7\u697c\u68af\u9876\u90e8\u65f6\u5c31\u5c06\u5176\u526a\u679d\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_backtrack.py
def backtrack(choices: list[int], state: int, n: int, res: list[int]) -> int:\n\"\"\"\u56de\u6eaf\"\"\"\n# \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif state == n:\nres[0] += 1\n# \u904d\u5386\u6240\u6709\u9009\u62e9\nfor choice in choices:\n# \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif state + choice > n:\nbreak\n# \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res)\n# \u56de\u9000\ndef climbing_stairs_backtrack(n: int) -> int:\n\"\"\"\u722c\u697c\u68af\uff1a\u56de\u6eaf\"\"\"\nchoices = [1, 2]  # \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nstate = 0  # \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nres = [0]  # \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(choices, state, n, res)\nreturn res[0]\n
climbing_stairs_backtrack.cpp
/* \u56de\u6eaf */\nvoid backtrack(vector<int> &choices, int state, int n, vector<int> &res) {\n// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif (state == n)\nres[0]++;\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (auto &choice : choices) {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif (state + choice > n)\nbreak;\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res);\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\nvector<int> choices = {1, 2}; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nint state = 0;                // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nvector<int> res = {0};        // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(choices, state, n, res);\nreturn res[0];\n}\n
climbing_stairs_backtrack.java
/* \u56de\u6eaf */\nvoid backtrack(List<Integer> choices, int state, int n, List<Integer> res) {\n// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif (state == n)\nres.set(0, res.get(0) + 1);\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (Integer choice : choices) {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif (state + choice > n)\nbreak;\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res);\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\nList<Integer> choices = Arrays.asList(1, 2); // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nint state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nList<Integer> res = new ArrayList<>();\nres.add(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(choices, state, n, res);\nreturn res.get(0);\n}\n
climbing_stairs_backtrack.cs
/* \u56de\u6eaf */\nvoid Backtrack(List<int> choices, int state, int n, List<int> res) {\n// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif (state == n)\nres[0]++;\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nforeach (int choice in choices) {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif (state + choice > n)\nbreak;\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nBacktrack(choices, state + choice, n, res);\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint ClimbingStairsBacktrack(int n) {\nList<int> choices = new() { 1, 2 }; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nint state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nList<int> res = new() { 0 }; // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nBacktrack(choices, state, n, res);\nreturn res[0];\n}\n
climbing_stairs_backtrack.go
/* \u56de\u6eaf */\nfunc backtrack(choices []int, state, n int, res []int) {\n// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif state == n {\nres[0] = res[0] + 1\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor _, choice := range choices {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif state+choice > n {\nbreak\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state+choice, n, res)\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunc climbingStairsBacktrack(n int) int {\n// \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nchoices := []int{1, 2}\n// \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nstate := 0\nres := make([]int, 1)\n// \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nres[0] = 0\nbacktrack(choices, state, n, res)\nreturn res[0]\n}\n
climbing_stairs_backtrack.swift
/* \u56de\u6eaf */\nfunc backtrack(choices: [Int], state: Int, n: Int, res: inout [Int]) {\n// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif state == n {\nres[0] += 1\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor choice in choices {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif state + choice > n {\nbreak\n}\nbacktrack(choices: choices, state: state + choice, n: n, res: &res)\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunc climbingStairsBacktrack(n: Int) -> Int {\nlet choices = [1, 2] // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nlet state = 0 // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nvar res: [Int] = []\nres.append(0) // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(choices: choices, state: state, n: n, res: &res)\nreturn res[0]\n}\n
climbing_stairs_backtrack.js
/* \u56de\u6eaf */\nfunction backtrack(choices, state, n, res) {\n// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif (state === n) res.set(0, res.get(0) + 1);\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (const choice of choices) {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif (state + choice > n) break;\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res);\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunction climbingStairsBacktrack(n) {\nconst choices = [1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nconst state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nconst res = new Map();\nres.set(0, 0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(choices, state, n, res);\nreturn res.get(0);\n}\n
climbing_stairs_backtrack.ts
/* \u56de\u6eaf */\nfunction backtrack(\nchoices: number[],\nstate: number,\nn: number,\nres: Map<0, any>\n): void {\n// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif (state === n) res.set(0, res.get(0) + 1);\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (const choice of choices) {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif (state + choice > n) break;\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res);\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunction climbingStairsBacktrack(n: number): number {\nconst choices = [1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nconst state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nconst res = new Map();\nres.set(0, 0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(choices, state, n, res);\nreturn res.get(0);\n}\n
climbing_stairs_backtrack.dart
/* \u56de\u6eaf */\nvoid backtrack(List<int> choices, int state, int n, List<int> res) {\n// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif (state == n) {\nres[0]++;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int choice in choices) {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif (state + choice > n) break;\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res);\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\nList<int> choices = [1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nint state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nList<int> res = [];\nres.add(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(choices, state, n, res);\nreturn res[0];\n}\n
climbing_stairs_backtrack.rs
/* \u56de\u6eaf */\nfn backtrack(choices: &[i32], state: i32, n: i32, res: &mut [i32]) {\n// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif state == n { res[0] = res[0] + 1; }\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor &choice in choices {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif state + choice > n { break; }\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res);\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfn climbing_stairs_backtrack(n: usize) -> i32 {\nlet choices = vec![ 1, 2 ]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nlet state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nlet mut res = Vec::new();\nres.push(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(&choices, state, n as i32, &mut res);\nres[0]\n}\n
climbing_stairs_backtrack.c
/* \u56de\u6eaf */\nvoid backtrack(int *choices, int state, int n, int *res, int len) {\n// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif (state == n)\nres[0]++;\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i < len; i++) {\nint choice = choices[i];\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif (state + choice > n)\nbreak;\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res, len);\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\nint choices[2] = {1, 2}; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nint state = 0;           // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nint *res = (int *)malloc(sizeof(int));\n*res = 0;                // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nint len = sizeof(choices) / sizeof(int);\nbacktrack(choices, state, n, res, len);\nint result = *res;\nfree(res);\nreturn result;\n}\n
climbing_stairs_backtrack.zig
// \u56de\u6eaf\nfn backtrack(choices: []i32, state: i32, n: i32, res: std.ArrayList(i32)) void {\n// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif (state == n) {\nres.items[0] = res.items[0] + 1;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (choices) |choice| {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif (state + choice > n) {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res);\n// \u56de\u9000\n}\n}\n// \u722c\u697c\u68af\uff1a\u56de\u6eaf\nfn climbingStairsBacktrack(n: usize) !i32 {\nvar choices = [_]i32{ 1, 2 }; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nvar state: i32 = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nvar res = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer res.deinit();\ntry res.append(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(&choices, state, @intCast(n), res);\nreturn res.items[0];\n}\n
"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1411","title":"14.1.1 \u00a0 \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u641c\u7d22","text":"

\u56de\u6eaf\u7b97\u6cd5\u901a\u5e38\u5e76\u4e0d\u663e\u5f0f\u5730\u5bf9\u95ee\u9898\u8fdb\u884c\u62c6\u89e3\uff0c\u800c\u662f\u5c06\u95ee\u9898\u770b\u4f5c\u4e00\u7cfb\u5217\u51b3\u7b56\u6b65\u9aa4\uff0c\u901a\u8fc7\u8bd5\u63a2\u548c\u526a\u679d\uff0c\u641c\u7d22\u6240\u6709\u53ef\u80fd\u7684\u89e3\u3002

\u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u4ece\u95ee\u9898\u5206\u89e3\u7684\u89d2\u5ea6\u5206\u6790\u8fd9\u9053\u9898\u3002\u8bbe\u722c\u5230\u7b2c \\(i\\) \u9636\u5171\u6709 \\(dp[i]\\) \u79cd\u65b9\u6848\uff0c\u90a3\u4e48 \\(dp[i]\\) \u5c31\u662f\u539f\u95ee\u9898\uff0c\u5176\u5b50\u95ee\u9898\u5305\u62ec:

\\[ dp[i-1], dp[i-2], \\dots, dp[2], dp[1] \\]

\u7531\u4e8e\u6bcf\u8f6e\u53ea\u80fd\u4e0a \\(1\\) \u9636\u6216 \\(2\\) \u9636\uff0c\u56e0\u6b64\u5f53\u6211\u4eec\u7ad9\u5728\u7b2c \\(i\\) \u9636\u697c\u68af\u4e0a\u65f6\uff0c\u4e0a\u4e00\u8f6e\u53ea\u53ef\u80fd\u7ad9\u5728\u7b2c \\(i - 1\\) \u9636\u6216\u7b2c \\(i - 2\\) \u9636\u4e0a\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u4eec\u53ea\u80fd\u4ece\u7b2c \\(i -1\\) \u9636\u6216\u7b2c \\(i - 2\\) \u9636\u524d\u5f80\u7b2c \\(i\\) \u9636\u3002

\u7531\u6b64\u4fbf\u53ef\u5f97\u51fa\u4e00\u4e2a\u91cd\u8981\u63a8\u8bba\uff1a\u722c\u5230\u7b2c \\(i - 1\\) \u9636\u7684\u65b9\u6848\u6570\u52a0\u4e0a\u722c\u5230\u7b2c \\(i - 2\\) \u9636\u7684\u65b9\u6848\u6570\u5c31\u7b49\u4e8e\u722c\u5230\u7b2c \\(i\\) \u9636\u7684\u65b9\u6848\u6570\u3002\u516c\u5f0f\u5982\u4e0b\uff1a

\\[ dp[i] = dp[i-1] + dp[i-2] \\]

\u8fd9\u610f\u5473\u7740\u5728\u722c\u697c\u68af\u95ee\u9898\u4e2d\uff0c\u5404\u4e2a\u5b50\u95ee\u9898\u4e4b\u95f4\u5b58\u5728\u9012\u63a8\u5173\u7cfb\uff0c\u539f\u95ee\u9898\u7684\u89e3\u53ef\u4ee5\u7531\u5b50\u95ee\u9898\u7684\u89e3\u6784\u5efa\u5f97\u6765\u3002\u56fe 14-2 \u5c55\u793a\u4e86\u8be5\u9012\u63a8\u5173\u7cfb\u3002

\u56fe 14-2 \u00a0 \u65b9\u6848\u6570\u91cf\u9012\u63a8\u5173\u7cfb

\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9012\u63a8\u516c\u5f0f\u5f97\u5230\u66b4\u529b\u641c\u7d22\u89e3\u6cd5\u3002\u4ee5 \\(dp[n]\\) \u4e3a\u8d77\u59cb\u70b9\uff0c\u9012\u5f52\u5730\u5c06\u4e00\u4e2a\u8f83\u5927\u95ee\u9898\u62c6\u89e3\u4e3a\u4e24\u4e2a\u8f83\u5c0f\u95ee\u9898\u7684\u548c\uff0c\u76f4\u81f3\u5230\u8fbe\u6700\u5c0f\u5b50\u95ee\u9898 \\(dp[1]\\) \u548c \\(dp[2]\\) \u65f6\u8fd4\u56de\u3002\u5176\u4e2d\uff0c\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\u662f\u5df2\u77e5\u7684\uff0c\u5373 \\(dp[1] = 1\\)\u3001\\(dp[2] = 2\\) \uff0c\u8868\u793a\u722c\u5230\u7b2c \\(1\\)\u3001\\(2\\) \u9636\u5206\u522b\u6709 \\(1\\)\u3001\\(2\\) \u79cd\u65b9\u6848\u3002

\u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u5b83\u548c\u6807\u51c6\u56de\u6eaf\u4ee3\u7801\u90fd\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff0c\u4f46\u66f4\u52a0\u7b80\u6d01\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_dfs.py
def dfs(i: int) -> int:\n\"\"\"\u641c\u7d22\"\"\"\n# \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif i == 1 or i == 2:\nreturn i\n# dp[i] = dp[i-1] + dp[i-2]\ncount = dfs(i - 1) + dfs(i - 2)\nreturn count\ndef climbing_stairs_dfs(n: int) -> int:\n\"\"\"\u722c\u697c\u68af\uff1a\u641c\u7d22\"\"\"\nreturn dfs(n)\n
climbing_stairs_dfs.cpp
/* \u641c\u7d22 */\nint dfs(int i) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2)\nreturn i;\n// dp[i] = dp[i-1] + dp[i-2]\nint count = dfs(i - 1) + dfs(i - 2);\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\nreturn dfs(n);\n}\n
climbing_stairs_dfs.java
/* \u641c\u7d22 */\nint dfs(int i) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2)\nreturn i;\n// dp[i] = dp[i-1] + dp[i-2]\nint count = dfs(i - 1) + dfs(i - 2);\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\nreturn dfs(n);\n}\n
climbing_stairs_dfs.cs
/* \u641c\u7d22 */\nint DFS(int i) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2)\nreturn i;\n// dp[i] = dp[i-1] + dp[i-2]\nint count = DFS(i - 1) + DFS(i - 2);\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint ClimbingStairsDFS(int n) {\nreturn DFS(n);\n}\n
climbing_stairs_dfs.go
/* \u641c\u7d22 */\nfunc dfs(i int) int {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif i == 1 || i == 2 {\nreturn i\n}\n// dp[i] = dp[i-1] + dp[i-2]\ncount := dfs(i-1) + dfs(i-2)\nreturn count\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunc climbingStairsDFS(n int) int {\nreturn dfs(n)\n}\n
climbing_stairs_dfs.swift
/* \u641c\u7d22 */\nfunc dfs(i: Int) -> Int {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif i == 1 || i == 2 {\nreturn i\n}\n// dp[i] = dp[i-1] + dp[i-2]\nlet count = dfs(i: i - 1) + dfs(i: i - 2)\nreturn count\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunc climbingStairsDFS(n: Int) -> Int {\ndfs(i: n)\n}\n
climbing_stairs_dfs.js
/* \u641c\u7d22 */\nfunction dfs(i) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i === 1 || i === 2) return i;\n// dp[i] = dp[i-1] + dp[i-2]\nconst count = dfs(i - 1) + dfs(i - 2);\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunction climbingStairsDFS(n) {\nreturn dfs(n);\n}\n
climbing_stairs_dfs.ts
/* \u641c\u7d22 */\nfunction dfs(i: number): number {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i === 1 || i === 2) return i;\n// dp[i] = dp[i-1] + dp[i-2]\nconst count = dfs(i - 1) + dfs(i - 2);\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunction climbingStairsDFS(n: number): number {\nreturn dfs(n);\n}\n
climbing_stairs_dfs.dart
/* \u641c\u7d22 */\nint dfs(int i) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2) return i;\n// dp[i] = dp[i-1] + dp[i-2]\nint count = dfs(i - 1) + dfs(i - 2);\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\nreturn dfs(n);\n}\n
climbing_stairs_dfs.rs
/* \u641c\u7d22 */\nfn dfs(i: usize) -> i32 {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif i == 1 || i == 2 { return i as i32; }\n// dp[i] = dp[i-1] + dp[i-2]\nlet count = dfs(i - 1) + dfs(i - 2);\ncount\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfn climbing_stairs_dfs(n: usize) -> i32 {\ndfs(n) }\n
climbing_stairs_dfs.c
/* \u641c\u7d22 */\nint dfs(int i) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2)\nreturn i;\n// dp[i] = dp[i-1] + dp[i-2]\nint count = dfs(i - 1) + dfs(i - 2);\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\nreturn dfs(n);\n}\n
climbing_stairs_dfs.zig
// \u641c\u7d22\nfn dfs(i: usize) i32 {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 or i == 2) {\nreturn @intCast(i);\n}\n// dp[i] = dp[i-1] + dp[i-2]\nvar count = dfs(i - 1) + dfs(i - 2);\nreturn count;\n}\n// \u722c\u697c\u68af\uff1a\u641c\u7d22\nfn climbingStairsDFS(comptime n: usize) i32 {\nreturn dfs(n);\n}\n

\u56fe 14-3 \u5c55\u793a\u4e86\u66b4\u529b\u641c\u7d22\u5f62\u6210\u7684\u9012\u5f52\u6811\u3002\u5bf9\u4e8e\u95ee\u9898 \\(dp[n]\\) \uff0c\u5176\u9012\u5f52\u6811\u7684\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \u3002\u6307\u6570\u9636\u5c5e\u4e8e\u7206\u70b8\u5f0f\u589e\u957f\uff0c\u5982\u679c\u6211\u4eec\u8f93\u5165\u4e00\u4e2a\u6bd4\u8f83\u5927\u7684 \\(n\\) \uff0c\u5219\u4f1a\u9677\u5165\u6f2b\u957f\u7684\u7b49\u5f85\u4e4b\u4e2d\u3002

\u56fe 14-3 \u00a0 \u722c\u697c\u68af\u5bf9\u5e94\u9012\u5f52\u6811

\u89c2\u5bdf\u56fe 14-3 \uff0c\u6307\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f\u7531\u4e8e\u201c\u91cd\u53e0\u5b50\u95ee\u9898\u201d\u5bfc\u81f4\u7684\u3002\u4f8b\u5982 \\(dp[9]\\) \u88ab\u5206\u89e3\u4e3a \\(dp[8]\\) \u548c \\(dp[7]\\) \uff0c\\(dp[8]\\) \u88ab\u5206\u89e3\u4e3a \\(dp[7]\\) \u548c \\(dp[6]\\) \uff0c\u4e24\u8005\u90fd\u5305\u542b\u5b50\u95ee\u9898 \\(dp[7]\\) \u3002

\u4ee5\u6b64\u7c7b\u63a8\uff0c\u5b50\u95ee\u9898\u4e2d\u5305\u542b\u66f4\u5c0f\u7684\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u5b50\u5b50\u5b59\u5b59\u65e0\u7a77\u5c3d\u4e5f\u3002\u7edd\u5927\u90e8\u5206\u8ba1\u7b97\u8d44\u6e90\u90fd\u6d6a\u8d39\u5728\u8fd9\u4e9b\u91cd\u53e0\u7684\u95ee\u9898\u4e0a\u3002

"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1412","title":"14.1.2 \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22","text":"

\u4e3a\u4e86\u63d0\u5347\u7b97\u6cd5\u6548\u7387\uff0c\u6211\u4eec\u5e0c\u671b\u6240\u6709\u7684\u91cd\u53e0\u5b50\u95ee\u9898\u90fd\u53ea\u88ab\u8ba1\u7b97\u4e00\u6b21\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u6570\u7ec4 mem \u6765\u8bb0\u5f55\u6bcf\u4e2a\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5e76\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u5c06\u91cd\u53e0\u5b50\u95ee\u9898\u526a\u679d\u3002

  1. \u5f53\u9996\u6b21\u8ba1\u7b97 \\(dp[i]\\) \u65f6\uff0c\u6211\u4eec\u5c06\u5176\u8bb0\u5f55\u81f3 mem[i] \uff0c\u4ee5\u4fbf\u4e4b\u540e\u4f7f\u7528\u3002
  2. \u5f53\u518d\u6b21\u9700\u8981\u8ba1\u7b97 \\(dp[i]\\) \u65f6\uff0c\u6211\u4eec\u4fbf\u53ef\u76f4\u63a5\u4ece mem[i] \u4e2d\u83b7\u53d6\u7ed3\u679c\uff0c\u4ece\u800c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u8be5\u5b50\u95ee\u9898\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_dfs_mem.py
def dfs(i: int, mem: list[int]) -> int:\n\"\"\"\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n# \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif i == 1 or i == 2:\nreturn i\n# \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif mem[i] != -1:\nreturn mem[i]\n# dp[i] = dp[i-1] + dp[i-2]\ncount = dfs(i - 1, mem) + dfs(i - 2, mem)\n# \u8bb0\u5f55 dp[i]\nmem[i] = count\nreturn count\ndef climbing_stairs_dfs_mem(n: int) -> int:\n\"\"\"\u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n# mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nmem = [-1] * (n + 1)\nreturn dfs(n, mem)\n
climbing_stairs_dfs_mem.cpp
/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, vector<int> &mem) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2)\nreturn i;\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif (mem[i] != -1)\nreturn mem[i];\n// dp[i] = dp[i-1] + dp[i-2]\nint count = dfs(i - 1, mem) + dfs(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nvector<int> mem(n + 1, -1);\nreturn dfs(n, mem);\n}\n
climbing_stairs_dfs_mem.java
/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, int[] mem) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2)\nreturn i;\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif (mem[i] != -1)\nreturn mem[i];\n// dp[i] = dp[i-1] + dp[i-2]\nint count = dfs(i - 1, mem) + dfs(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nint[] mem = new int[n + 1];\nArrays.fill(mem, -1);\nreturn dfs(n, mem);\n}\n
climbing_stairs_dfs_mem.cs
/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint DFS(int i, int[] mem) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2)\nreturn i;\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif (mem[i] != -1)\nreturn mem[i];\n// dp[i] = dp[i-1] + dp[i-2]\nint count = DFS(i - 1, mem) + DFS(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint ClimbingStairsDFSMem(int n) {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nint[] mem = new int[n + 1];\nArray.Fill(mem, -1);\nreturn DFS(n, mem);\n}\n
climbing_stairs_dfs_mem.go
/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc dfsMem(i int, mem []int) int {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif i == 1 || i == 2 {\nreturn i\n}\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif mem[i] != -1 {\nreturn mem[i]\n}\n// dp[i] = dp[i-1] + dp[i-2]\ncount := dfsMem(i-1, mem) + dfsMem(i-2, mem)\n// \u8bb0\u5f55 dp[i]\nmem[i] = count\nreturn count\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc climbingStairsDFSMem(n int) int {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nmem := make([]int, n+1)\nfor i := range mem {\nmem[i] = -1\n}\nreturn dfsMem(n, mem)\n}\n
climbing_stairs_dfs_mem.swift
/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc dfs(i: Int, mem: inout [Int]) -> Int {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif i == 1 || i == 2 {\nreturn i\n}\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif mem[i] != -1 {\nreturn mem[i]\n}\n// dp[i] = dp[i-1] + dp[i-2]\nlet count = dfs(i: i - 1, mem: &mem) + dfs(i: i - 2, mem: &mem)\n// \u8bb0\u5f55 dp[i]\nmem[i] = count\nreturn count\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc climbingStairsDFSMem(n: Int) -> Int {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nvar mem = Array(repeating: -1, count: n + 1)\nreturn dfs(i: n, mem: &mem)\n}\n
climbing_stairs_dfs_mem.js
/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction dfs(i, mem) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i === 1 || i === 2) return i;\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif (mem[i] != -1) return mem[i];\n// dp[i] = dp[i-1] + dp[i-2]\nconst count = dfs(i - 1, mem) + dfs(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction climbingStairsDFSMem(n) {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nconst mem = new Array(n + 1).fill(-1);\nreturn dfs(n, mem);\n}\n
climbing_stairs_dfs_mem.ts
/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction dfs(i: number, mem: number[]): number {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i === 1 || i === 2) return i;\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif (mem[i] != -1) return mem[i];\n// dp[i] = dp[i-1] + dp[i-2]\nconst count = dfs(i - 1, mem) + dfs(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction climbingStairsDFSMem(n: number): number {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nconst mem = new Array(n + 1).fill(-1);\nreturn dfs(n, mem);\n}\n
climbing_stairs_dfs_mem.dart
/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, List<int> mem) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2) return i;\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif (mem[i] != -1) return mem[i];\n// dp[i] = dp[i-1] + dp[i-2]\nint count = dfs(i - 1, mem) + dfs(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nList<int> mem = List.filled(n + 1, -1);\nreturn dfs(n, mem);\n}\n
climbing_stairs_dfs_mem.rs
/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn dfs(i: usize, mem: &mut [i32]) -> i32 {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif i == 1 || i == 2 { return i as i32; }\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif mem[i] != -1 { return mem[i]; }\n// dp[i] = dp[i-1] + dp[i-2]\nlet count = dfs(i - 1, mem) + dfs(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\ncount\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn climbing_stairs_dfs_mem(n: usize) -> i32 {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nlet mut mem = vec![-1; n + 1];\ndfs(n, &mut mem)\n}\n
climbing_stairs_dfs_mem.c
/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, int *mem) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2)\nreturn i;\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif (mem[i] != -1)\nreturn mem[i];\n// dp[i] = dp[i-1] + dp[i-2]\nint count = dfs(i - 1, mem) + dfs(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nint *mem = (int *)malloc((n + 1) * sizeof(int));\nfor (int i = 0; i <= n; i++) {\nmem[i] = -1;\n}\nint result = dfs(n, mem);\nfree(mem);\nreturn result;\n}\n
climbing_stairs_dfs_mem.zig
// \u8bb0\u5fc6\u5316\u641c\u7d22\nfn dfs(i: usize, mem: []i32) i32 {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 or i == 2) {\nreturn @intCast(i);\n}\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif (mem[i] != -1) {\nreturn mem[i];\n}\n// dp[i] = dp[i-1] + dp[i-2]\nvar count = dfs(i - 1, mem) + dfs(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\nreturn count;\n}\n// \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\nfn climbingStairsDFSMem(comptime n: usize) i32 {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nvar mem = [_]i32{ -1 } ** (n + 1);\nreturn dfs(n, &mem);\n}\n

\u89c2\u5bdf\u56fe 14-4 \uff0c\u7ecf\u8fc7\u8bb0\u5fc6\u5316\u5904\u7406\u540e\uff0c\u6240\u6709\u91cd\u53e0\u5b50\u95ee\u9898\u90fd\u53ea\u9700\u88ab\u8ba1\u7b97\u4e00\u6b21\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u88ab\u4f18\u5316\u81f3 \\(O(n)\\) \uff0c\u8fd9\u662f\u4e00\u4e2a\u5de8\u5927\u7684\u98de\u8dc3\u3002

\u56fe 14-4 \u00a0 \u8bb0\u5fc6\u5316\u641c\u7d22\u5bf9\u5e94\u9012\u5f52\u6811

"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1413","title":"14.1.3 \u00a0 \u65b9\u6cd5\u4e09\uff1a\u52a8\u6001\u89c4\u5212","text":"

\u8bb0\u5fc6\u5316\u641c\u7d22\u662f\u4e00\u79cd\u201c\u4ece\u9876\u81f3\u5e95\u201d\u7684\u65b9\u6cd5\uff1a\u6211\u4eec\u4ece\u539f\u95ee\u9898\uff08\u6839\u8282\u70b9\uff09\u5f00\u59cb\uff0c\u9012\u5f52\u5730\u5c06\u8f83\u5927\u5b50\u95ee\u9898\u5206\u89e3\u4e3a\u8f83\u5c0f\u5b50\u95ee\u9898\uff0c\u76f4\u81f3\u89e3\u5df2\u77e5\u7684\u6700\u5c0f\u5b50\u95ee\u9898\uff08\u53f6\u8282\u70b9\uff09\u3002\u4e4b\u540e\uff0c\u901a\u8fc7\u56de\u6eaf\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u9010\u5c42\u6536\u96c6\uff0c\u6784\u5efa\u51fa\u539f\u95ee\u9898\u7684\u89e3\u3002

\u4e0e\u4e4b\u76f8\u53cd\uff0c\u52a8\u6001\u89c4\u5212\u662f\u4e00\u79cd\u201c\u4ece\u5e95\u81f3\u9876\u201d\u7684\u65b9\u6cd5\uff1a\u4ece\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\u5f00\u59cb\uff0c\u8fed\u4ee3\u5730\u6784\u5efa\u66f4\u5927\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u76f4\u81f3\u5f97\u5230\u539f\u95ee\u9898\u7684\u89e3\u3002

\u7531\u4e8e\u52a8\u6001\u89c4\u5212\u4e0d\u5305\u542b\u56de\u6eaf\u8fc7\u7a0b\uff0c\u56e0\u6b64\u53ea\u9700\u4f7f\u7528\u5faa\u73af\u8fed\u4ee3\u5b9e\u73b0\uff0c\u65e0\u987b\u4f7f\u7528\u9012\u5f52\u3002\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521d\u59cb\u5316\u4e00\u4e2a\u6570\u7ec4 dp \u6765\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5b83\u8d77\u5230\u4e86\u8bb0\u5fc6\u5316\u641c\u7d22\u4e2d\u6570\u7ec4 mem \u76f8\u540c\u7684\u8bb0\u5f55\u4f5c\u7528\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_dp.py
def climbing_stairs_dp(n: int) -> int:\n\"\"\"\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nif n == 1 or n == 2:\nreturn n\n# \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\ndp = [0] * (n + 1)\n# \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1], dp[2] = 1, 2\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in range(3, n + 1):\ndp[i] = dp[i - 1] + dp[i - 2]\nreturn dp[n]\n
climbing_stairs_dp.cpp
/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\nif (n == 1 || n == 2)\nreturn n;\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvector<int> dp(n + 1);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\nreturn dp[n];\n}\n
climbing_stairs_dp.java
/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\nif (n == 1 || n == 2)\nreturn n;\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nint[] dp = new int[n + 1];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\nreturn dp[n];\n}\n
climbing_stairs_dp.cs
/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint ClimbingStairsDP(int n) {\nif (n == 1 || n == 2)\nreturn n;\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nint[] dp = new int[n + 1];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\nreturn dp[n];\n}\n
climbing_stairs_dp.go
/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDP(n int) int {\nif n == 1 || n == 2 {\nreturn n\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\ndp := make([]int, n+1)\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1\ndp[2] = 2\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i := 3; i <= n; i++ {\ndp[i] = dp[i-1] + dp[i-2]\n}\nreturn dp[n]\n}\n
climbing_stairs_dp.swift
/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDP(n: Int) -> Int {\nif n == 1 || n == 2 {\nreturn n\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvar dp = Array(repeating: 0, count: n + 1)\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1\ndp[2] = 2\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in stride(from: 3, through: n, by: 1) {\ndp[i] = dp[i - 1] + dp[i - 2]\n}\nreturn dp[n]\n}\n
climbing_stairs_dp.js
/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDP(n) {\nif (n === 1 || n === 2) return n;\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nconst dp = new Array(n + 1).fill(-1);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (let i = 3; i <= n; i++) {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\nreturn dp[n];\n}\n
climbing_stairs_dp.ts
/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDP(n: number): number {\nif (n === 1 || n === 2) return n;\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nconst dp = new Array(n + 1).fill(-1);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (let i = 3; i <= n; i++) {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\nreturn dp[n];\n}\n
climbing_stairs_dp.dart
/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\nif (n == 1 || n == 2) return n;\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nList<int> dp = List.filled(n + 1, 0);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\nreturn dp[n];\n}\n
climbing_stairs_dp.rs
/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfn climbing_stairs_dp(n: usize) -> i32 {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif n == 1 || n == 2 { return n as i32; }\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nlet mut dp = vec![-1; n + 1];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in 3..=n {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\ndp[n]\n}\n
climbing_stairs_dp.c
/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\nif (n == 1 || n == 2)\nreturn n;\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nint *dp = (int *)malloc((n + 1) * sizeof(int));\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i <= n; i++) {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\nint result = dp[n];\nfree(dp);\nreturn result;\n}\n
climbing_stairs_dp.zig
// \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212\nfn climbingStairsDP(comptime n: usize) i32 {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (n == 1 or n == 2) {\nreturn @intCast(n);\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvar dp = [_]i32{-1} ** (n + 1);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (3..n + 1) |i| {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\nreturn dp[n];\n}\n

\u56fe 14-5 \u6a21\u62df\u4e86\u4ee5\u4e0a\u4ee3\u7801\u7684\u6267\u884c\u8fc7\u7a0b\u3002

\u56fe 14-5 \u00a0 \u722c\u697c\u68af\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

\u4e0e\u56de\u6eaf\u7b97\u6cd5\u4e00\u6837\uff0c\u52a8\u6001\u89c4\u5212\u4e5f\u4f7f\u7528\u201c\u72b6\u6001\u201d\u6982\u5ff5\u6765\u8868\u793a\u95ee\u9898\u6c42\u89e3\u7684\u67d0\u4e2a\u7279\u5b9a\u9636\u6bb5\uff0c\u6bcf\u4e2a\u72b6\u6001\u90fd\u5bf9\u5e94\u4e00\u4e2a\u5b50\u95ee\u9898\u4ee5\u53ca\u76f8\u5e94\u7684\u5c40\u90e8\u6700\u4f18\u89e3\u3002\u4f8b\u5982\uff0c\u722c\u697c\u68af\u95ee\u9898\u7684\u72b6\u6001\u5b9a\u4e49\u4e3a\u5f53\u524d\u6240\u5728\u697c\u68af\u9636\u6570 \\(i\\) \u3002

\u6839\u636e\u4ee5\u4e0a\u5185\u5bb9\uff0c\u6211\u4eec\u53ef\u4ee5\u603b\u7ed3\u51fa\u52a8\u6001\u89c4\u5212\u7684\u5e38\u7528\u672f\u8bed\u3002

  • \u5c06\u6570\u7ec4 dp \u79f0\u4e3a\u300c\\(dp\\) \u8868\u300d\uff0c\\(dp[i]\\) \u8868\u793a\u72b6\u6001 \\(i\\) \u5bf9\u5e94\u5b50\u95ee\u9898\u7684\u89e3\u3002
  • \u5c06\u6700\u5c0f\u5b50\u95ee\u9898\u5bf9\u5e94\u7684\u72b6\u6001\uff08\u5373\u7b2c \\(1\\) \u548c \\(2\\) \u9636\u697c\u68af\uff09\u79f0\u4e3a\u300c\u521d\u59cb\u72b6\u6001\u300d\u3002
  • \u5c06\u9012\u63a8\u516c\u5f0f \\(dp[i] = dp[i-1] + dp[i-2]\\) \u79f0\u4e3a\u300c\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u300d\u3002
"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1414","title":"14.1.4 \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

\u7ec6\u5fc3\u7684\u4f60\u53ef\u80fd\u53d1\u73b0\uff0c\u7531\u4e8e \\(dp[i]\\) \u53ea\u4e0e \\(dp[i-1]\\) \u548c \\(dp[i-2]\\) \u6709\u5173\uff0c\u56e0\u6b64\u6211\u4eec\u65e0\u987b\u4f7f\u7528\u4e00\u4e2a\u6570\u7ec4 dp \u6765\u5b58\u50a8\u6240\u6709\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u800c\u53ea\u9700\u4e24\u4e2a\u53d8\u91cf\u6eda\u52a8\u524d\u8fdb\u5373\u53ef\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_dp.py
def climbing_stairs_dp_comp(n: int) -> int:\n\"\"\"\u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\nif n == 1 or n == 2:\nreturn n\na, b = 1, 2\nfor _ in range(3, n + 1):\na, b = b, a + b\nreturn b\n
climbing_stairs_dp.cpp
/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\nif (n == 1 || n == 2)\nreturn n;\nint a = 1, b = 2;\nfor (int i = 3; i <= n; i++) {\nint tmp = b;\nb = a + b;\na = tmp;\n}\nreturn b;\n}\n
climbing_stairs_dp.java
/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\nif (n == 1 || n == 2)\nreturn n;\nint a = 1, b = 2;\nfor (int i = 3; i <= n; i++) {\nint tmp = b;\nb = a + b;\na = tmp;\n}\nreturn b;\n}\n
climbing_stairs_dp.cs
/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint ClimbingStairsDPComp(int n) {\nif (n == 1 || n == 2)\nreturn n;\nint a = 1, b = 2;\nfor (int i = 3; i <= n; i++) {\nint tmp = b;\nb = a + b;\na = tmp;\n}\nreturn b;\n}\n
climbing_stairs_dp.go
/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDPComp(n int) int {\nif n == 1 || n == 2 {\nreturn n\n}\na, b := 1, 2\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i := 3; i <= n; i++ {\na, b = b, a+b\n}\nreturn b\n}\n
climbing_stairs_dp.swift
/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDPComp(n: Int) -> Int {\nif n == 1 || n == 2 {\nreturn n\n}\nvar a = 1\nvar b = 2\nfor _ in stride(from: 3, through: n, by: 1) {\n(a, b) = (b, a + b)\n}\nreturn b\n}\n
climbing_stairs_dp.js
/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDPComp(n) {\nif (n === 1 || n === 2) return n;\nlet a = 1,\nb = 2;\nfor (let i = 3; i <= n; i++) {\nconst tmp = b;\nb = a + b;\na = tmp;\n}\nreturn b;\n}\n
climbing_stairs_dp.ts
/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDPComp(n: number): number {\nif (n === 1 || n === 2) return n;\nlet a = 1,\nb = 2;\nfor (let i = 3; i <= n; i++) {\nconst tmp = b;\nb = a + b;\na = tmp;\n}\nreturn b;\n}\n
climbing_stairs_dp.dart
/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\nif (n == 1 || n == 2) return n;\nint a = 1, b = 2;\nfor (int i = 3; i <= n; i++) {\nint tmp = b;\nb = a + b;\na = tmp;\n}\nreturn b;\n}\n
climbing_stairs_dp.rs
/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn climbing_stairs_dp_comp(n: usize) -> i32 {\nif n == 1 || n == 2 { return n as i32; }\nlet (mut a, mut b) = (1, 2);\nfor _ in 3..=n {\nlet tmp = b;\nb = a + b;\na = tmp;\n}\nb\n}\n
climbing_stairs_dp.c
/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\nif (n == 1 || n == 2)\nreturn n;\nint a = 1, b = 2;\nfor (int i = 3; i <= n; i++) {\nint tmp = b;\nb = a + b;\na = tmp;\n}\nreturn b;\n}\n
climbing_stairs_dp.zig
// \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn climbingStairsDPComp(comptime n: usize) i32 {\nif (n == 1 or n == 2) {\nreturn @intCast(n);\n}\nvar a: i32 = 1;\nvar b: i32 = 2;\nfor (3..n + 1) |_| {\nvar tmp = b;\nb = a + b;\na = tmp;\n}\nreturn b;\n}\n

\u89c2\u5bdf\u4ee5\u4e0a\u4ee3\u7801\uff0c\u7531\u4e8e\u7701\u53bb\u4e86\u6570\u7ec4 dp \u5360\u7528\u7684\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n)\\) \u964d\u4f4e\u81f3 \\(O(1)\\) \u3002

\u5728\u52a8\u6001\u89c4\u5212\u95ee\u9898\u4e2d\uff0c\u5f53\u524d\u72b6\u6001\u5f80\u5f80\u4ec5\u4e0e\u524d\u9762\u6709\u9650\u4e2a\u72b6\u6001\u6709\u5173\uff0c\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u53ea\u4fdd\u7559\u5fc5\u8981\u7684\u72b6\u6001\uff0c\u901a\u8fc7\u201c\u964d\u7ef4\u201d\u6765\u8282\u7701\u5185\u5b58\u7a7a\u95f4\u3002\u8fd9\u79cd\u7a7a\u95f4\u4f18\u5316\u6280\u5de7\u88ab\u79f0\u4e3a\u201c\u6eda\u52a8\u53d8\u91cf\u201d\u6216\u201c\u6eda\u52a8\u6570\u7ec4\u201d\u3002

"},{"location":"chapter_dynamic_programming/knapsack_problem/","title":"14.4 \u00a0 0-1 \u80cc\u5305\u95ee\u9898","text":"

\u80cc\u5305\u95ee\u9898\u662f\u4e00\u4e2a\u975e\u5e38\u597d\u7684\u52a8\u6001\u89c4\u5212\u5165\u95e8\u9898\u76ee\uff0c\u662f\u52a8\u6001\u89c4\u5212\u4e2d\u6700\u5e38\u89c1\u7684\u95ee\u9898\u5f62\u5f0f\u3002\u5176\u5177\u6709\u5f88\u591a\u53d8\u79cd\uff0c\u4f8b\u5982 0-1 \u80cc\u5305\u95ee\u9898\u3001\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u3001\u591a\u91cd\u80cc\u5305\u95ee\u9898\u7b49\u3002

\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5148\u6765\u6c42\u89e3\u6700\u5e38\u89c1\u7684 0-1 \u80cc\u5305\u95ee\u9898\u3002

Question

\u7ed9\u5b9a \\(n\\) \u4e2a\u7269\u54c1\uff0c\u7b2c \\(i\\) \u4e2a\u7269\u54c1\u7684\u91cd\u91cf\u4e3a \\(wgt[i-1]\\)\u3001\u4ef7\u503c\u4e3a \\(val[i-1]\\) \uff0c\u548c\u4e00\u4e2a\u5bb9\u91cf\u4e3a \\(cap\\) \u7684\u80cc\u5305\u3002\u6bcf\u4e2a\u7269\u54c1\u53ea\u80fd\u9009\u62e9\u4e00\u6b21\uff0c\u95ee\u5728\u4e0d\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\u4e0b\u80fd\u653e\u5165\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u3002

\u89c2\u5bdf\u56fe 14-17 \uff0c\u7531\u4e8e\u7269\u54c1\u7f16\u53f7 \\(i\\) \u4ece \\(1\\) \u5f00\u59cb\u8ba1\u6570\uff0c\u6570\u7ec4\u7d22\u5f15\u4ece \\(0\\) \u5f00\u59cb\u8ba1\u6570\uff0c\u56e0\u6b64\u7269\u54c1 \\(i\\) \u5bf9\u5e94\u91cd\u91cf \\(wgt[i-1]\\) \u548c\u4ef7\u503c \\(val[i-1]\\) \u3002

\u56fe 14-17 \u00a0 0-1 \u80cc\u5305\u7684\u793a\u4f8b\u6570\u636e

\u6211\u4eec\u53ef\u4ee5\u5c06 0-1 \u80cc\u5305\u95ee\u9898\u770b\u4f5c\u662f\u4e00\u4e2a\u7531 \\(n\\) \u8f6e\u51b3\u7b56\u7ec4\u6210\u7684\u8fc7\u7a0b\uff0c\u6bcf\u4e2a\u7269\u4f53\u90fd\u6709\u4e0d\u653e\u5165\u548c\u653e\u5165\u4e24\u79cd\u51b3\u7b56\uff0c\u56e0\u6b64\u8be5\u95ee\u9898\u662f\u6ee1\u8db3\u51b3\u7b56\u6811\u6a21\u578b\u7684\u3002

\u8be5\u95ee\u9898\u7684\u76ee\u6807\u662f\u6c42\u89e3\u201c\u5728\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u7684\u6700\u5927\u4ef7\u503c\u201d\uff0c\u56e0\u6b64\u8f83\u5927\u6982\u7387\u662f\u4e2a\u52a8\u6001\u89c4\u5212\u95ee\u9898\u3002

\u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868

\u5bf9\u4e8e\u6bcf\u4e2a\u7269\u54c1\u6765\u8bf4\uff0c\u4e0d\u653e\u5165\u80cc\u5305\uff0c\u80cc\u5305\u5bb9\u91cf\u4e0d\u53d8\uff1b\u653e\u5165\u80cc\u5305\uff0c\u80cc\u5305\u5bb9\u91cf\u51cf\u5c0f\u3002\u7531\u6b64\u53ef\u5f97\u72b6\u6001\u5b9a\u4e49\uff1a\u5f53\u524d\u7269\u54c1\u7f16\u53f7 \\(i\\) \u548c\u5269\u4f59\u80cc\u5305\u5bb9\u91cf \\(c\\) \uff0c\u8bb0\u4e3a \\([i, c]\\) \u3002

\u72b6\u6001 \\([i, c]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u4e3a\uff1a\u524d \\(i\\) \u4e2a\u7269\u54c1\u5728\u5269\u4f59\u5bb9\u91cf\u4e3a \\(c\\) \u7684\u80cc\u5305\u4e2d\u7684\u6700\u5927\u4ef7\u503c\uff0c\u8bb0\u4e3a \\(dp[i, c]\\) \u3002

\u5f85\u6c42\u89e3\u7684\u662f \\(dp[n, cap]\\) \uff0c\u56e0\u6b64\u9700\u8981\u4e00\u4e2a\u5c3a\u5bf8\u4e3a \\((n+1) \\times (cap+1)\\) \u7684\u4e8c\u7ef4 \\(dp\\) \u8868\u3002

\u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b

\u5f53\u6211\u4eec\u505a\u51fa\u7269\u54c1 \\(i\\) \u7684\u51b3\u7b56\u540e\uff0c\u5269\u4f59\u7684\u662f\u524d \\(i-1\\) \u4e2a\u7269\u54c1\u7684\u51b3\u7b56\uff0c\u53ef\u5206\u4e3a\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\u3002

  • \u4e0d\u653e\u5165\u7269\u54c1 \\(i\\) \uff1a\u80cc\u5305\u5bb9\u91cf\u4e0d\u53d8\uff0c\u72b6\u6001\u53d8\u5316\u4e3a \\([i-1, c]\\) \u3002
  • \u653e\u5165\u7269\u54c1 \\(i\\) \uff1a\u80cc\u5305\u5bb9\u91cf\u51cf\u5c0f \\(wgt[i-1]\\) \uff0c\u4ef7\u503c\u589e\u52a0 \\(val[i-1]\\) \uff0c\u72b6\u6001\u53d8\u5316\u4e3a \\([i-1, c-wgt[i-1]]\\) \u3002

\u4e0a\u8ff0\u5206\u6790\u5411\u6211\u4eec\u63ed\u793a\u4e86\u672c\u9898\u7684\u6700\u4f18\u5b50\u7ed3\u6784\uff1a\u6700\u5927\u4ef7\u503c \\(dp[i, c]\\) \u7b49\u4e8e\u4e0d\u653e\u5165\u7269\u54c1 \\(i\\) \u548c\u653e\u5165\u7269\u54c1 \\(i\\) \u4e24\u79cd\u65b9\u6848\u4e2d\u7684\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\u3002\u7531\u6b64\u53ef\u63a8\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff1a

\\[ dp[i, c] = \\max(dp[i-1, c], dp[i-1, c - wgt[i-1]] + val[i-1]) \\]

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u82e5\u5f53\u524d\u7269\u54c1\u91cd\u91cf \\(wgt[i - 1]\\) \u8d85\u51fa\u5269\u4f59\u80cc\u5305\u5bb9\u91cf \\(c\\) \uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\u3002

\u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f

\u5f53\u65e0\u7269\u54c1\u6216\u65e0\u5269\u4f59\u80cc\u5305\u5bb9\u91cf\u65f6\u6700\u5927\u4ef7\u503c\u4e3a \\(0\\) \uff0c\u5373\u9996\u5217 \\(dp[i, 0]\\) \u548c\u9996\u884c \\(dp[0, c]\\) \u90fd\u7b49\u4e8e \\(0\\) \u3002

\u5f53\u524d\u72b6\u6001 \\([i, c]\\) \u4ece\u4e0a\u65b9\u7684\u72b6\u6001 \\([i-1, c]\\) \u548c\u5de6\u4e0a\u65b9\u7684\u72b6\u6001 \\([i-1, c-wgt[i-1]]\\) \u8f6c\u79fb\u800c\u6765\uff0c\u56e0\u6b64\u901a\u8fc7\u4e24\u5c42\u5faa\u73af\u6b63\u5e8f\u904d\u5386\u6574\u4e2a \\(dp\\) \u8868\u5373\u53ef\u3002

\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u6211\u4eec\u63a5\u4e0b\u6765\u6309\u987a\u5e8f\u5b9e\u73b0\u66b4\u529b\u641c\u7d22\u3001\u8bb0\u5fc6\u5316\u641c\u7d22\u3001\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\u3002

"},{"location":"chapter_dynamic_programming/knapsack_problem/#1","title":"1. \u00a0 \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u641c\u7d22","text":"

\u641c\u7d22\u4ee3\u7801\u5305\u542b\u4ee5\u4e0b\u8981\u7d20\u3002

  • \u9012\u5f52\u53c2\u6570\uff1a\u72b6\u6001 \\([i, c]\\) \u3002
  • \u8fd4\u56de\u503c\uff1a\u5b50\u95ee\u9898\u7684\u89e3 \\(dp[i, c]\\) \u3002
  • \u7ec8\u6b62\u6761\u4ef6\uff1a\u5f53\u7269\u54c1\u7f16\u53f7\u8d8a\u754c \\(i = 0\\) \u6216\u80cc\u5305\u5269\u4f59\u5bb9\u91cf\u4e3a \\(0\\) \u65f6\uff0c\u7ec8\u6b62\u9012\u5f52\u5e76\u8fd4\u56de\u4ef7\u503c \\(0\\) \u3002
  • \u526a\u679d\uff1a\u82e5\u5f53\u524d\u7269\u54c1\u91cd\u91cf\u8d85\u51fa\u80cc\u5305\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig knapsack.py
def knapsack_dfs(wgt: list[int], val: list[int], i: int, c: int) -> int:\n\"\"\"0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22\"\"\"\n# \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif i == 0 or c == 0:\nreturn 0\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif wgt[i - 1] > c:\nreturn knapsack_dfs(wgt, val, i - 1, c)\n# \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nno = knapsack_dfs(wgt, val, i - 1, c)\nyes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1]\n# \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn max(no, yes)\n
knapsack.cpp
/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(vector<int> &wgt, vector<int> &val, int i, int c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFS(wgt, val, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = knapsackDFS(wgt, val, i - 1, c);\nint yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn max(no, yes);\n}\n
knapsack.java
/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(int[] wgt, int[] val, int i, int c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFS(wgt, val, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = knapsackDFS(wgt, val, i - 1, c);\nint yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn Math.max(no, yes);\n}\n
knapsack.cs
/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint KnapsackDFS(int[] weight, int[] val, int i, int c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (weight[i - 1] > c) {\nreturn KnapsackDFS(weight, val, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = KnapsackDFS(weight, val, i - 1, c);\nint yes = KnapsackDFS(weight, val, i - 1, c - weight[i - 1]) + val[i - 1];\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn Math.Max(no, yes);\n}\n
knapsack.go
/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc knapsackDFS(wgt, val []int, i, c int) int {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif i == 0 || c == 0 {\nreturn 0\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif wgt[i-1] > c {\nreturn knapsackDFS(wgt, val, i-1, c)\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nno := knapsackDFS(wgt, val, i-1, c)\nyes := knapsackDFS(wgt, val, i-1, c-wgt[i-1]) + val[i-1]\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn int(math.Max(float64(no), float64(yes)))\n}\n
knapsack.swift
/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc knapsackDFS(wgt: [Int], val: [Int], i: Int, c: Int) -> Int {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif i == 0 || c == 0 {\nreturn 0\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif wgt[i - 1] > c {\nreturn knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c)\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nlet no = knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c)\nlet yes = knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c - wgt[i - 1]) + val[i - 1]\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn max(no, yes)\n}\n
knapsack.js
/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunction knapsackDFS(wgt, val, i, c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i === 0 || c === 0) {\nreturn 0;\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFS(wgt, val, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nconst no = knapsackDFS(wgt, val, i - 1, c);\nconst yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn Math.max(no, yes);\n}\n
knapsack.ts
/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunction knapsackDFS(\nwgt: Array<number>,\nval: Array<number>,\ni: number,\nc: number\n): number {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i === 0 || c === 0) {\nreturn 0;\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFS(wgt, val, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nconst no = knapsackDFS(wgt, val, i - 1, c);\nconst yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn Math.max(no, yes);\n}\n
knapsack.dart
/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(List<int> wgt, List<int> val, int i, int c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFS(wgt, val, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = knapsackDFS(wgt, val, i - 1, c);\nint yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn max(no, yes);\n}\n
knapsack.rs
/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfn knapsack_dfs(wgt: &[i32], val: &[i32], i: usize, c: usize) -> i32 {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif i == 0 || c == 0 {\nreturn 0;\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif wgt[i - 1] > c as i32 {\nreturn knapsack_dfs(wgt, val, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nlet no = knapsack_dfs(wgt, val, i - 1, c);\nlet yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1] as usize) + val[i - 1];\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nstd::cmp::max(no, yes)\n}\n
knapsack.c
/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(int wgt[], int val[], int i, int c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFS(wgt, val, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = knapsackDFS(wgt, val, i - 1, c);\nint yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn max(no, yes);\n}\n
knapsack.zig
// 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22\nfn knapsackDFS(wgt: []i32, val: []i32, i: usize, c: usize) i32 {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 or c == 0) {\nreturn 0;\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFS(wgt, val, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nvar no = knapsackDFS(wgt, val, i - 1, c);\nvar yes = knapsackDFS(wgt, val, i - 1, c - @as(usize, @intCast(wgt[i - 1]))) + val[i - 1];\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn @max(no, yes);\n}\n

\u5982\u56fe 14-18 \u6240\u793a\uff0c\u7531\u4e8e\u6bcf\u4e2a\u7269\u54c1\u90fd\u4f1a\u4ea7\u751f\u4e0d\u9009\u548c\u9009\u4e24\u6761\u641c\u7d22\u5206\u652f\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \u3002

\u89c2\u5bdf\u9012\u5f52\u6811\uff0c\u5bb9\u6613\u53d1\u73b0\u5176\u4e2d\u5b58\u5728\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u4f8b\u5982 \\(dp[1, 10]\\) \u7b49\u3002\u800c\u5f53\u7269\u54c1\u8f83\u591a\u3001\u80cc\u5305\u5bb9\u91cf\u8f83\u5927\uff0c\u5c24\u5176\u662f\u76f8\u540c\u91cd\u91cf\u7684\u7269\u54c1\u8f83\u591a\u65f6\uff0c\u91cd\u53e0\u5b50\u95ee\u9898\u7684\u6570\u91cf\u5c06\u4f1a\u5927\u5e45\u589e\u591a\u3002

\u56fe 14-18 \u00a0 0-1 \u80cc\u5305\u7684\u66b4\u529b\u641c\u7d22\u9012\u5f52\u6811

"},{"location":"chapter_dynamic_programming/knapsack_problem/#2","title":"2. \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22","text":"

\u4e3a\u4e86\u4fdd\u8bc1\u91cd\u53e0\u5b50\u95ee\u9898\u53ea\u88ab\u8ba1\u7b97\u4e00\u6b21\uff0c\u6211\u4eec\u501f\u52a9\u8bb0\u5fc6\u5217\u8868 mem \u6765\u8bb0\u5f55\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5176\u4e2d mem[i][c] \u5bf9\u5e94 \\(dp[i, c]\\) \u3002

\u5f15\u5165\u8bb0\u5fc6\u5316\u4e4b\u540e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u5b50\u95ee\u9898\u6570\u91cf\uff0c\u4e5f\u5c31\u662f \\(O(n \\times cap)\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig knapsack.py
def knapsack_dfs_mem(\nwgt: list[int], val: list[int], mem: list[list[int]], i: int, c: int\n) -> int:\n\"\"\"0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n# \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif i == 0 or c == 0:\nreturn 0\n# \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif mem[i][c] != -1:\nreturn mem[i][c]\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif wgt[i - 1] > c:\nreturn knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n# \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nno = knapsack_dfs_mem(wgt, val, mem, i - 1, c)\nyes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1]\n# \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = max(no, yes)\nreturn mem[i][c]\n
knapsack.cpp
/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(vector<int> &wgt, vector<int> &val, vector<vector<int>> &mem, int i, int c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][c] != -1) {\nreturn mem[i][c];\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFSMem(wgt, val, mem, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = knapsackDFSMem(wgt, val, mem, i - 1, c);\nint yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = max(no, yes);\nreturn mem[i][c];\n}\n
knapsack.java
/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(int[] wgt, int[] val, int[][] mem, int i, int c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][c] != -1) {\nreturn mem[i][c];\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFSMem(wgt, val, mem, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = knapsackDFSMem(wgt, val, mem, i - 1, c);\nint yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = Math.max(no, yes);\nreturn mem[i][c];\n}\n
knapsack.cs
/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint KnapsackDFSMem(int[] weight, int[] val, int[][] mem, int i, int c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][c] != -1) {\nreturn mem[i][c];\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (weight[i - 1] > c) {\nreturn KnapsackDFSMem(weight, val, mem, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = KnapsackDFSMem(weight, val, mem, i - 1, c);\nint yes = KnapsackDFSMem(weight, val, mem, i - 1, c - weight[i - 1]) + val[i - 1];\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = Math.Max(no, yes);\nreturn mem[i][c];\n}\n
knapsack.go
/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc knapsackDFSMem(wgt, val []int, mem [][]int, i, c int) int {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif i == 0 || c == 0 {\nreturn 0\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif mem[i][c] != -1 {\nreturn mem[i][c]\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif wgt[i-1] > c {\nreturn knapsackDFSMem(wgt, val, mem, i-1, c)\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nno := knapsackDFSMem(wgt, val, mem, i-1, c)\nyes := knapsackDFSMem(wgt, val, mem, i-1, c-wgt[i-1]) + val[i-1]\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = int(math.Max(float64(no), float64(yes)))\nreturn mem[i][c]\n}\n
knapsack.swift
/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc knapsackDFSMem(wgt: [Int], val: [Int], mem: inout [[Int]], i: Int, c: Int) -> Int {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif i == 0 || c == 0 {\nreturn 0\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif mem[i][c] != -1 {\nreturn mem[i][c]\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif wgt[i - 1] > c {\nreturn knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c)\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nlet no = knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c)\nlet yes = knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c - wgt[i - 1]) + val[i - 1]\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = max(no, yes)\nreturn mem[i][c]\n}\n
knapsack.js
/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction knapsackDFSMem(wgt, val, mem, i, c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i === 0 || c === 0) {\nreturn 0;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][c] !== -1) {\nreturn mem[i][c];\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFSMem(wgt, val, mem, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nconst no = knapsackDFSMem(wgt, val, mem, i - 1, c);\nconst yes =\nknapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = Math.max(no, yes);\nreturn mem[i][c];\n}\n
knapsack.ts
/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction knapsackDFSMem(\nwgt: Array<number>,\nval: Array<number>,\nmem: Array<Array<number>>,\ni: number,\nc: number\n): number {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i === 0 || c === 0) {\nreturn 0;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][c] !== -1) {\nreturn mem[i][c];\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFSMem(wgt, val, mem, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nconst no = knapsackDFSMem(wgt, val, mem, i - 1, c);\nconst yes =\nknapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = Math.max(no, yes);\nreturn mem[i][c];\n}\n
knapsack.dart
/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(\nList<int> wgt,\nList<int> val,\nList<List<int>> mem,\nint i,\nint c,\n) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][c] != -1) {\nreturn mem[i][c];\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFSMem(wgt, val, mem, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = knapsackDFSMem(wgt, val, mem, i - 1, c);\nint yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = max(no, yes);\nreturn mem[i][c];\n}\n
knapsack.rs
/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn knapsack_dfs_mem(wgt: &[i32], val: &[i32], mem: &mut Vec<Vec<i32>>, i: usize, c: usize) -> i32 {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif i == 0 || c == 0 {\nreturn 0;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif mem[i][c] != -1 {\nreturn mem[i][c];\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif wgt[i - 1] > c as i32 {\nreturn knapsack_dfs_mem(wgt, val, mem, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nlet no = knapsack_dfs_mem(wgt, val, mem, i - 1, c);\nlet yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1] as usize) + val[i - 1];\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = std::cmp::max(no, yes);\nmem[i][c]\n}\n
knapsack.c
/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(int wgt[], int val[], int memCols, int mem[][memCols], int i, int c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][c] != -1) {\nreturn mem[i][c];\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFSMem(wgt, val, memCols, mem, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = knapsackDFSMem(wgt, val, memCols, mem, i - 1, c);\nint yes = knapsackDFSMem(wgt, val, memCols, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = max(no, yes);\nreturn mem[i][c];\n}\n
knapsack.zig
// 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\nfn knapsackDFSMem(wgt: []i32, val: []i32, mem: anytype, i: usize, c: usize) i32 {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 or c == 0) {\nreturn 0;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][c] != -1) {\nreturn mem[i][c];\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] > c) {\nreturn knapsackDFSMem(wgt, val, mem, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nvar no = knapsackDFSMem(wgt, val, mem, i - 1, c);\nvar yes = knapsackDFSMem(wgt, val, mem, i - 1, c - @as(usize, @intCast(wgt[i - 1]))) + val[i - 1];\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = @max(no, yes);\nreturn mem[i][c];\n}\n

\u56fe 14-19 \u5c55\u793a\u4e86\u5728\u8bb0\u5fc6\u5316\u9012\u5f52\u4e2d\u88ab\u526a\u6389\u7684\u641c\u7d22\u5206\u652f\u3002

\u56fe 14-19 \u00a0 0-1 \u80cc\u5305\u7684\u8bb0\u5fc6\u5316\u641c\u7d22\u9012\u5f52\u6811

"},{"location":"chapter_dynamic_programming/knapsack_problem/#3","title":"3. \u00a0 \u65b9\u6cd5\u4e09\uff1a\u52a8\u6001\u89c4\u5212","text":"

\u52a8\u6001\u89c4\u5212\u5b9e\u8d28\u4e0a\u5c31\u662f\u5728\u72b6\u6001\u8f6c\u79fb\u4e2d\u586b\u5145 \\(dp\\) \u8868\u7684\u8fc7\u7a0b\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig knapsack.py
def knapsack_dp(wgt: list[int], val: list[int], cap: int) -> int:\n\"\"\"0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(wgt)\n# \u521d\u59cb\u5316 dp \u8868\ndp = [[0] * (cap + 1) for _ in range(n + 1)]\n# \u72b6\u6001\u8f6c\u79fb\nfor i in range(1, n + 1):\nfor c in range(1, cap + 1):\nif wgt[i - 1] > c:\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c]\nelse:\n# \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1])\nreturn dp[n][cap]\n
knapsack.cpp
/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(vector<int> &wgt, vector<int> &val, int cap) {\nint n = wgt.size();\n// \u521d\u59cb\u5316 dp \u8868\nvector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n
knapsack.java
/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(int[] wgt, int[] val, int cap) {\nint n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nint[][] dp = new int[n + 1][cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = Math.max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n
knapsack.cs
/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint KnapsackDP(int[] weight, int[] val, int cap) {\nint n = weight.Length;\n// \u521d\u59cb\u5316 dp \u8868\nint[,] dp = new int[n + 1, cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (weight[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i, c] = dp[i - 1, c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i, c] = Math.Max(dp[i - 1, c - weight[i - 1]] + val[i - 1], dp[i - 1, c]);\n}\n}\n}\nreturn dp[n, cap];\n}\n
knapsack.go
/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDP(wgt, val []int, cap int) int {\nn := len(wgt)\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([][]int, n+1)\nfor i := 0; i <= n; i++ {\ndp[i] = make([]int, cap+1)\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor i := 1; i <= n; i++ {\nfor c := 1; c <= cap; c++ {\nif wgt[i-1] > c {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i-1][c]\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = int(math.Max(float64(dp[i-1][c]), float64(dp[i-1][c-wgt[i-1]]+val[i-1])))\n}\n}\n}\nreturn dp[n][cap]\n}\n
knapsack.swift
/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDP(wgt: [Int], val: [Int], cap: Int) -> Int {\nlet n = wgt.count\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)\n// \u72b6\u6001\u8f6c\u79fb\nfor i in stride(from: 1, through: n, by: 1) {\nfor c in stride(from: 1, through: cap, by: 1) {\nif wgt[i - 1] > c {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c]\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1])\n}\n}\n}\nreturn dp[n][cap]\n}\n
knapsack.js
/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction knapsackDP(wgt, val, cap) {\nconst n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array(n + 1)\n.fill(0)\n.map(() => Array(cap + 1).fill(0));\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\nfor (let c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = Math.max(\ndp[i - 1][c],\ndp[i - 1][c - wgt[i - 1]] + val[i - 1]\n);\n}\n}\n}\nreturn dp[n][cap];\n}\n
knapsack.ts
/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction knapsackDP(\nwgt: Array<number>,\nval: Array<number>,\ncap: number\n): number {\nconst n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: n + 1 }, () =>\nArray.from({ length: cap + 1 }, () => 0)\n);\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\nfor (let c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = Math.max(\ndp[i - 1][c],\ndp[i - 1][c - wgt[i - 1]] + val[i - 1]\n);\n}\n}\n}\nreturn dp[n][cap];\n}\n
knapsack.dart
/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(List<int> wgt, List<int> val, int cap) {\nint n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nList<List<int>> dp = List.generate(n + 1, (index) => List.filled(cap + 1, 0));\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n
knapsack.rs
/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfn knapsack_dp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\nlet n = wgt.len();\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![vec![0; cap + 1]; n + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor i in 1..=n {\nfor c in 1..=cap {\nif wgt[i - 1] > c as i32 {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = std::cmp::max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1] as usize] + val[i - 1]);\n}\n}\n}\ndp[n][cap]\n}\n
knapsack.c
/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(int wgt[], int val[], int cap, int wgtSize) {\nint n = wgtSize;\n// \u521d\u59cb\u5316 dp \u8868\nint dp[n + 1][cap + 1];\nmemset(dp, 0, sizeof(dp));\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n
knapsack.zig
// 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212\nfn knapsackDP(comptime wgt: []i32, val: []i32, comptime cap: usize) i32 {\ncomptime var n = wgt.len;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_][cap + 1]i32{[_]i32{0} ** (cap + 1)} ** (n + 1);\n// \u72b6\u6001\u8f6c\u79fb\nfor (1..n + 1) |i| {\nfor (1..cap + 1) |c| {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = @max(dp[i - 1][c], dp[i - 1][c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n

\u5982\u56fe 14-20 \u6240\u793a\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u90fd\u7531\u6570\u7ec4 dp \u5927\u5c0f\u51b3\u5b9a\uff0c\u5373 \\(O(n \\times cap)\\) \u3002

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

\u56fe 14-20 \u00a0 0-1 \u80cc\u5305\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

"},{"location":"chapter_dynamic_programming/knapsack_problem/#4","title":"4. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

\u7531\u4e8e\u6bcf\u4e2a\u72b6\u6001\u90fd\u53ea\u4e0e\u5176\u4e0a\u4e00\u884c\u7684\u72b6\u6001\u6709\u5173\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e24\u4e2a\u6570\u7ec4\u6eda\u52a8\u524d\u8fdb\uff0c\u5c06\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n^2)\\) \u5c06\u4f4e\u81f3 \\(O(n)\\) \u3002

\u8fdb\u4e00\u6b65\u601d\u8003\uff0c\u6211\u4eec\u662f\u5426\u53ef\u4ee5\u4ec5\u7528\u4e00\u4e2a\u6570\u7ec4\u5b9e\u73b0\u7a7a\u95f4\u4f18\u5316\u5462\uff1f\u89c2\u5bdf\u53ef\u77e5\uff0c\u6bcf\u4e2a\u72b6\u6001\u90fd\u662f\u7531\u6b63\u4e0a\u65b9\u6216\u5de6\u4e0a\u65b9\u7684\u683c\u5b50\u8f6c\u79fb\u8fc7\u6765\u7684\u3002\u5047\u8bbe\u53ea\u6709\u4e00\u4e2a\u6570\u7ec4\uff0c\u5f53\u5f00\u59cb\u904d\u5386\u7b2c \\(i\\) \u884c\u65f6\uff0c\u8be5\u6570\u7ec4\u5b58\u50a8\u7684\u4ecd\u7136\u662f\u7b2c \\(i-1\\) \u884c\u7684\u72b6\u6001\u3002

  • \u5982\u679c\u91c7\u53d6\u6b63\u5e8f\u904d\u5386\uff0c\u90a3\u4e48\u904d\u5386\u5230 \\(dp[i, j]\\) \u65f6\uff0c\u5de6\u4e0a\u65b9 \\(dp[i-1, 1]\\) ~ \\(dp[i-1, j-1]\\) \u503c\u53ef\u80fd\u5df2\u7ecf\u88ab\u8986\u76d6\uff0c\u6b64\u65f6\u5c31\u65e0\u6cd5\u5f97\u5230\u6b63\u786e\u7684\u72b6\u6001\u8f6c\u79fb\u7ed3\u679c\u3002
  • \u5982\u679c\u91c7\u53d6\u5012\u5e8f\u904d\u5386\uff0c\u5219\u4e0d\u4f1a\u53d1\u751f\u8986\u76d6\u95ee\u9898\uff0c\u72b6\u6001\u8f6c\u79fb\u53ef\u4ee5\u6b63\u786e\u8fdb\u884c\u3002

\u56fe 14-21 \u5c55\u793a\u4e86\u5728\u5355\u4e2a\u6570\u7ec4\u4e0b\u4ece\u7b2c \\(i = 1\\) \u884c\u8f6c\u6362\u81f3\u7b2c \\(i = 2\\) \u884c\u7684\u8fc7\u7a0b\u3002\u8bf7\u601d\u8003\u6b63\u5e8f\u904d\u5386\u548c\u5012\u5e8f\u904d\u5386\u7684\u533a\u522b\u3002

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

\u56fe 14-21 \u00a0 0-1 \u80cc\u5305\u7684\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

\u5728\u4ee3\u7801\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u4ec5\u9700\u5c06\u6570\u7ec4 dp \u7684\u7b2c\u4e00\u7ef4 \\(i\\) \u76f4\u63a5\u5220\u9664\uff0c\u5e76\u4e14\u628a\u5185\u5faa\u73af\u66f4\u6539\u4e3a\u5012\u5e8f\u904d\u5386\u5373\u53ef\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig knapsack.py
def knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -> int:\n\"\"\"0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(wgt)\n# \u521d\u59cb\u5316 dp \u8868\ndp = [0] * (cap + 1)\n# \u72b6\u6001\u8f6c\u79fb\nfor i in range(1, n + 1):\n# \u5012\u5e8f\u904d\u5386\nfor c in range(cap, 0, -1):\nif wgt[i - 1] > c:\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c]\nelse:\n# \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\nreturn dp[cap]\n
knapsack.cpp
/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {\nint n = wgt.size();\n// \u521d\u59cb\u5316 dp \u8868\nvector<int> dp(cap + 1, 0);\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\n// \u5012\u5e8f\u904d\u5386\nfor (int c = cap; c >= 1; c--) {\nif (wgt[i - 1] <= c) {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
knapsack.java
/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(int[] wgt, int[] val, int cap) {\nint n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\n// \u5012\u5e8f\u904d\u5386\nfor (int c = cap; c >= 1; c--) {\nif (wgt[i - 1] <= c) {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
knapsack.cs
/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint KnapsackDPComp(int[] weight, int[] val, int cap) {\nint n = weight.Length;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\n// \u5012\u5e8f\u904d\u5386\nfor (int c = cap; c > 0; c--) {\nif (weight[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = Math.Max(dp[c], dp[c - weight[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
knapsack.go
/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDPComp(wgt, val []int, cap int) int {\nn := len(wgt)\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([]int, cap+1)\n// \u72b6\u6001\u8f6c\u79fb\nfor i := 1; i <= n; i++ {\n// \u5012\u5e8f\u904d\u5386\nfor c := cap; c >= 1; c-- {\nif wgt[i-1] <= c {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = int(math.Max(float64(dp[c]), float64(dp[c-wgt[i-1]]+val[i-1])))\n}\n}\n}\nreturn dp[cap]\n}\n
knapsack.swift
/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDPComp(wgt: [Int], val: [Int], cap: Int) -> Int {\nlet n = wgt.count\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: 0, count: cap + 1)\n// \u72b6\u6001\u8f6c\u79fb\nfor i in stride(from: 1, through: n, by: 1) {\n// \u5012\u5e8f\u904d\u5386\nfor c in stride(from: cap, through: 1, by: -1) {\nif wgt[i - 1] <= c {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n}\n}\n}\nreturn dp[cap]\n}\n
knapsack.js
/* 0-1 \u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction knapsackDPComp(wgt, val, cap) {\nconst n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array(cap + 1).fill(0);\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\n// \u5012\u5e8f\u904d\u5386\nfor (let c = cap; c >= 1; c--) {\nif (wgt[i - 1] <= c) {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
knapsack.ts
/* 0-1 \u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction knapsackDPComp(\nwgt: Array<number>,\nval: Array<number>,\ncap: number\n): number {\nconst n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array(cap + 1).fill(0);\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\n// \u5012\u5e8f\u904d\u5386\nfor (let c = cap; c >= 1; c--) {\nif (wgt[i - 1] <= c) {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
knapsack.dart
/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(List<int> wgt, List<int> val, int cap) {\nint n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nList<int> dp = List.filled(cap + 1, 0);\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\n// \u5012\u5e8f\u904d\u5386\nfor (int c = cap; c >= 1; c--) {\nif (wgt[i - 1] <= c) {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
knapsack.rs
/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn knapsack_dp_comp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\nlet n = wgt.len();\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![0; cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor i in 1..=n {\n// \u5012\u5e8f\u904d\u5386\nfor c in (1..=cap).rev() {\nif wgt[i - 1] <= c as i32 {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = std::cmp::max(dp[c], dp[c - wgt[i - 1] as usize] + val[i - 1]);\n}\n}\n}\ndp[cap]\n}\n
knapsack.c
/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(int wgt[], int val[], int cap, int wgtSize) {\nint n = wgtSize;\n// \u521d\u59cb\u5316 dp \u8868\nint dp[cap + 1];\nmemset(dp, 0, sizeof(dp));\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\n// \u5012\u5e8f\u904d\u5386\nfor (int c = cap; c >= 1; c--) {\nif (wgt[i - 1] <= c) {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
knapsack.zig
// 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn knapsackDPComp(wgt: []i32, val: []i32, comptime cap: usize) i32 {\nvar n = wgt.len;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_]i32{0} ** (cap + 1);\n// \u72b6\u6001\u8f6c\u79fb\nfor (1..n + 1) |i| {\n// \u5012\u5e8f\u904d\u5386\nvar c = cap;\nwhile (c > 0) : (c -= 1) {\nif (wgt[i - 1] < c) {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = @max(dp[c], dp[c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
"},{"location":"chapter_dynamic_programming/summary/","title":"14.7 \u00a0 \u5c0f\u7ed3","text":"
  • \u52a8\u6001\u89c4\u5212\u5bf9\u95ee\u9898\u8fdb\u884c\u5206\u89e3\uff0c\u5e76\u901a\u8fc7\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\u6765\u89c4\u907f\u91cd\u590d\u8ba1\u7b97\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684\u8ba1\u7b97\u6548\u7387\u3002
  • \u4e0d\u8003\u8651\u65f6\u95f4\u7684\u524d\u63d0\u4e0b\uff0c\u6240\u6709\u52a8\u6001\u89c4\u5212\u95ee\u9898\u90fd\u53ef\u4ee5\u7528\u56de\u6eaf\uff08\u66b4\u529b\u641c\u7d22\uff09\u8fdb\u884c\u6c42\u89e3\uff0c\u4f46\u9012\u5f52\u6811\u4e2d\u5b58\u5728\u5927\u91cf\u7684\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u6548\u7387\u6781\u4f4e\u3002\u901a\u8fc7\u5f15\u5165\u8bb0\u5fc6\u5316\u5217\u8868\uff0c\u53ef\u4ee5\u5b58\u50a8\u6240\u6709\u8ba1\u7b97\u8fc7\u7684\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u4ece\u800c\u4fdd\u8bc1\u91cd\u53e0\u5b50\u95ee\u9898\u53ea\u88ab\u8ba1\u7b97\u4e00\u6b21\u3002
  • \u8bb0\u5fc6\u5316\u9012\u5f52\u662f\u4e00\u79cd\u4ece\u9876\u81f3\u5e95\u7684\u9012\u5f52\u5f0f\u89e3\u6cd5\uff0c\u800c\u4e0e\u4e4b\u5bf9\u5e94\u7684\u52a8\u6001\u89c4\u5212\u662f\u4e00\u79cd\u4ece\u5e95\u81f3\u9876\u7684\u9012\u63a8\u5f0f\u89e3\u6cd5\uff0c\u5176\u5982\u540c\u201c\u586b\u5199\u8868\u683c\u201d\u4e00\u6837\u3002\u7531\u4e8e\u5f53\u524d\u72b6\u6001\u4ec5\u4f9d\u8d56\u4e8e\u67d0\u4e9b\u5c40\u90e8\u72b6\u6001\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u6d88\u9664 \\(dp\\) \u8868\u7684\u4e00\u4e2a\u7ef4\u5ea6\uff0c\u4ece\u800c\u964d\u4f4e\u7a7a\u95f4\u590d\u6742\u5ea6\u3002
  • \u5b50\u95ee\u9898\u5206\u89e3\u662f\u4e00\u79cd\u901a\u7528\u7684\u7b97\u6cd5\u601d\u8def\uff0c\u5728\u5206\u6cbb\u3001\u52a8\u6001\u89c4\u5212\u3001\u56de\u6eaf\u4e2d\u5177\u6709\u4e0d\u540c\u7684\u6027\u8d28\u3002
  • \u52a8\u6001\u89c4\u5212\u95ee\u9898\u7684\u4e09\u5927\u7279\u6027\uff1a\u91cd\u53e0\u5b50\u95ee\u9898\u3001\u6700\u4f18\u5b50\u7ed3\u6784\u3001\u65e0\u540e\u6548\u6027\u3002
  • \u5982\u679c\u539f\u95ee\u9898\u7684\u6700\u4f18\u89e3\u53ef\u4ee5\u4ece\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u6784\u5efa\u5f97\u6765\uff0c\u5219\u5b83\u5c31\u5177\u6709\u6700\u4f18\u5b50\u7ed3\u6784\u3002
  • \u65e0\u540e\u6548\u6027\u6307\u5bf9\u4e8e\u4e00\u4e2a\u72b6\u6001\uff0c\u5176\u672a\u6765\u53d1\u5c55\u53ea\u4e0e\u8be5\u72b6\u6001\u6709\u5173\uff0c\u4e0e\u5176\u6240\u7ecf\u5386\u7684\u8fc7\u53bb\u7684\u6240\u6709\u72b6\u6001\u65e0\u5173\u3002\u8bb8\u591a\u7ec4\u5408\u4f18\u5316\u95ee\u9898\u90fd\u4e0d\u5177\u6709\u65e0\u540e\u6548\u6027\uff0c\u65e0\u6cd5\u4f7f\u7528\u52a8\u6001\u89c4\u5212\u5feb\u901f\u6c42\u89e3\u3002

\u80cc\u5305\u95ee\u9898

  • \u80cc\u5305\u95ee\u9898\u662f\u6700\u5178\u578b\u7684\u52a8\u6001\u89c4\u5212\u9898\u76ee\uff0c\u5177\u6709 0-1 \u80cc\u5305\u3001\u5b8c\u5168\u80cc\u5305\u3001\u591a\u91cd\u80cc\u5305\u7b49\u53d8\u79cd\u95ee\u9898\u3002
  • 0-1 \u80cc\u5305\u7684\u72b6\u6001\u5b9a\u4e49\u4e3a\u524d \\(i\\) \u4e2a\u7269\u54c1\u5728\u5269\u4f59\u5bb9\u91cf\u4e3a \\(c\\) \u7684\u80cc\u5305\u4e2d\u7684\u6700\u5927\u4ef7\u503c\u3002\u6839\u636e\u4e0d\u653e\u5165\u80cc\u5305\u548c\u653e\u5165\u80cc\u5305\u4e24\u79cd\u51b3\u7b56\uff0c\u53ef\u5f97\u5230\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u5e76\u6784\u5efa\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u3002\u5728\u7a7a\u95f4\u4f18\u5316\u4e2d\uff0c\u7531\u4e8e\u6bcf\u4e2a\u72b6\u6001\u4f9d\u8d56\u6b63\u4e0a\u65b9\u548c\u5de6\u4e0a\u65b9\u7684\u72b6\u6001\uff0c\u56e0\u6b64\u9700\u8981\u5012\u5e8f\u904d\u5386\u5217\u8868\uff0c\u907f\u514d\u5de6\u4e0a\u65b9\u72b6\u6001\u88ab\u8986\u76d6\u3002
  • \u5b8c\u5168\u80cc\u5305\u7684\u6bcf\u79cd\u7269\u54c1\u7684\u9009\u53d6\u6570\u91cf\u65e0\u9650\u5236\uff0c\u56e0\u6b64\u9009\u62e9\u653e\u5165\u7269\u54c1\u7684\u72b6\u6001\u8f6c\u79fb\u4e0e 0-1 \u80cc\u5305\u4e0d\u540c\u3002\u7531\u4e8e\u72b6\u6001\u4f9d\u8d56\u4e8e\u6b63\u4e0a\u65b9\u548c\u6b63\u5de6\u65b9\u7684\u72b6\u6001\uff0c\u56e0\u6b64\u5728\u7a7a\u95f4\u4f18\u5316\u4e2d\u5e94\u5f53\u6b63\u5e8f\u904d\u5386\u3002
  • \u96f6\u94b1\u5151\u6362\u95ee\u9898\u662f\u5b8c\u5168\u80cc\u5305\u7684\u4e00\u4e2a\u53d8\u79cd\u3002\u5b83\u4ece\u6c42\u201c\u6700\u5927\u201d\u4ef7\u503c\u53d8\u4e3a\u6c42\u201c\u6700\u5c0f\u201d\u786c\u5e01\u6570\u91cf\uff0c\u56e0\u6b64\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e2d\u7684 \\(\\max()\\) \u5e94\u6539\u4e3a \\(\\min()\\) \u3002\u4ece\u6c42\u201c\u4e0d\u8d85\u8fc7\u201d\u80cc\u5305\u5bb9\u91cf\u5230\u6c42\u201c\u6070\u597d\u201d\u51d1\u51fa\u76ee\u6807\u91d1\u989d\uff0c\u56e0\u6b64\u4f7f\u7528 \\(amt + 1\\) \u6765\u8868\u793a\u201c\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u201d\u7684\u65e0\u6548\u89e3\u3002
  • \u96f6\u94b1\u5151\u6362 II \u95ee\u9898\u4ece\u6c42\u201c\u6700\u5c11\u786c\u5e01\u6570\u91cf\u201d\u6539\u4e3a\u6c42\u201c\u786c\u5e01\u7ec4\u5408\u6570\u91cf\u201d\uff0c\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u76f8\u5e94\u5730\u4ece \\(\\min()\\) \u6539\u4e3a\u6c42\u548c\u8fd0\u7b97\u7b26\u3002

\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898

  • \u7f16\u8f91\u8ddd\u79bb\uff08Levenshtein \u8ddd\u79bb\uff09\u7528\u4e8e\u8861\u91cf\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e4b\u95f4\u7684\u76f8\u4f3c\u5ea6\uff0c\u5176\u5b9a\u4e49\u4e3a\u4ece\u4e00\u4e2a\u5b57\u7b26\u4e32\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u6700\u5c0f\u7f16\u8f91\u6b65\u6570\uff0c\u7f16\u8f91\u64cd\u4f5c\u5305\u62ec\u6dfb\u52a0\u3001\u5220\u9664\u3001\u66ff\u6362\u3002
  • \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898\u7684\u72b6\u6001\u5b9a\u4e49\u4e3a\u5c06 \\(s\\) \u7684\u524d \\(i\\) \u4e2a\u5b57\u7b26\u66f4\u6539\u4e3a \\(t\\) \u7684\u524d \\(j\\) \u4e2a\u5b57\u7b26\u6240\u9700\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u3002\u5f53 \\(s[i] \\ne t[j]\\) \u65f6\uff0c\u5177\u6709\u4e09\u79cd\u51b3\u7b56\uff1a\u6dfb\u52a0\u3001\u5220\u9664\u3001\u66ff\u6362\uff0c\u5b83\u4eec\u90fd\u6709\u76f8\u5e94\u7684\u5269\u4f59\u5b50\u95ee\u9898\u3002\u636e\u6b64\u4fbf\u53ef\u4ee5\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\u4e0e\u6784\u5efa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u3002\u800c\u5f53 \\(s[i] = t[j]\\) \u65f6\uff0c\u65e0\u987b\u7f16\u8f91\u5f53\u524d\u5b57\u7b26\u3002
  • \u5728\u7f16\u8f91\u8ddd\u79bb\u4e2d\uff0c\u72b6\u6001\u4f9d\u8d56\u4e8e\u5176\u6b63\u4e0a\u65b9\u3001\u6b63\u5de6\u65b9\u3001\u5de6\u4e0a\u65b9\u7684\u72b6\u6001\uff0c\u56e0\u6b64\u7a7a\u95f4\u4f18\u5316\u540e\u6b63\u5e8f\u6216\u5012\u5e8f\u904d\u5386\u90fd\u65e0\u6cd5\u6b63\u786e\u5730\u8fdb\u884c\u72b6\u6001\u8f6c\u79fb\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5229\u7528\u4e00\u4e2a\u53d8\u91cf\u6682\u5b58\u5de6\u4e0a\u65b9\u72b6\u6001\uff0c\u4ece\u800c\u8f6c\u5316\u5230\u4e0e\u5b8c\u5168\u80cc\u5305\u7b49\u4ef7\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u5728\u7a7a\u95f4\u4f18\u5316\u540e\u8fdb\u884c\u6b63\u5e8f\u904d\u5386\u3002
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/","title":"14.5 \u00a0 \u5b8c\u5168\u80cc\u5305\u95ee\u9898","text":"

\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5148\u6c42\u89e3\u53e6\u4e00\u4e2a\u5e38\u89c1\u7684\u80cc\u5305\u95ee\u9898\uff1a\u5b8c\u5168\u80cc\u5305\uff0c\u518d\u4e86\u89e3\u5b83\u7684\u4e00\u79cd\u7279\u4f8b\uff1a\u96f6\u94b1\u5151\u6362\u3002

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1451","title":"14.5.1 \u00a0 \u5b8c\u5168\u80cc\u5305","text":"

Question

\u7ed9\u5b9a \\(n\\) \u4e2a\u7269\u54c1\uff0c\u7b2c \\(i\\) \u4e2a\u7269\u54c1\u7684\u91cd\u91cf\u4e3a \\(wgt[i-1]\\)\u3001\u4ef7\u503c\u4e3a \\(val[i-1]\\) \uff0c\u548c\u4e00\u4e2a\u5bb9\u91cf\u4e3a \\(cap\\) \u7684\u80cc\u5305\u3002\u6bcf\u4e2a\u7269\u54c1\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u5728\u4e0d\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\u4e0b\u80fd\u653e\u5165\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u3002

\u56fe 14-22 \u00a0 \u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"

\u5b8c\u5168\u80cc\u5305\u548c 0-1 \u80cc\u5305\u95ee\u9898\u975e\u5e38\u76f8\u4f3c\uff0c\u533a\u522b\u4ec5\u5728\u4e8e\u4e0d\u9650\u5236\u7269\u54c1\u7684\u9009\u62e9\u6b21\u6570\u3002

  • \u5728 0-1 \u80cc\u5305\u4e2d\uff0c\u6bcf\u4e2a\u7269\u54c1\u53ea\u6709\u4e00\u4e2a\uff0c\u56e0\u6b64\u5c06\u7269\u54c1 \\(i\\) \u653e\u5165\u80cc\u5305\u540e\uff0c\u53ea\u80fd\u4ece\u524d \\(i-1\\) \u4e2a\u7269\u54c1\u4e2d\u9009\u62e9\u3002
  • \u5728\u5b8c\u5168\u80cc\u5305\u4e2d\uff0c\u6bcf\u4e2a\u7269\u54c1\u6709\u65e0\u6570\u4e2a\uff0c\u56e0\u6b64\u5c06\u7269\u54c1 \\(i\\) \u653e\u5165\u80cc\u5305\u540e\uff0c\u4ecd\u53ef\u4ee5\u4ece\u524d \\(i\\) \u4e2a\u7269\u54c1\u4e2d\u9009\u62e9\u3002

\u5728\u5b8c\u5168\u80cc\u5305\u7684\u89c4\u5b9a\u4e0b\uff0c\u72b6\u6001 \\([i, c]\\) \u7684\u53d8\u5316\u5206\u4e3a\u4e24\u79cd\u60c5\u51b5\u3002

  • \u4e0d\u653e\u5165\u7269\u54c1 \\(i\\) \uff1a\u4e0e 0-1 \u80cc\u5305\u76f8\u540c\uff0c\u8f6c\u79fb\u81f3 \\([i-1, c]\\) \u3002
  • \u653e\u5165\u7269\u54c1 \\(i\\) \uff1a\u4e0e 0-1 \u80cc\u5305\u4e0d\u540c\uff0c\u8f6c\u79fb\u81f3 \\([i, c-wgt[i-1]]\\) \u3002

\u4ece\u800c\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u53d8\u4e3a\uff1a

\\[ dp[i, c] = \\max(dp[i-1, c], dp[i, c - wgt[i-1]] + val[i-1]) \\]"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u5bf9\u6bd4\u4e24\u9053\u9898\u76ee\u7684\u4ee3\u7801\uff0c\u72b6\u6001\u8f6c\u79fb\u4e2d\u6709\u4e00\u5904\u4ece \\(i-1\\) \u53d8\u4e3a \\(i\\) \uff0c\u5176\u4f59\u5b8c\u5168\u4e00\u81f4\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig unbounded_knapsack.py
def unbounded_knapsack_dp(wgt: list[int], val: list[int], cap: int) -> int:\n\"\"\"\u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(wgt)\n# \u521d\u59cb\u5316 dp \u8868\ndp = [[0] * (cap + 1) for _ in range(n + 1)]\n# \u72b6\u6001\u8f6c\u79fb\nfor i in range(1, n + 1):\nfor c in range(1, cap + 1):\nif wgt[i - 1] > c:\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c]\nelse:\n# \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1])\nreturn dp[n][cap]\n
unbounded_knapsack.cpp
/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(vector<int> &wgt, vector<int> &val, int cap) {\nint n = wgt.size();\n// \u521d\u59cb\u5316 dp \u8868\nvector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n
unbounded_knapsack.java
/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(int[] wgt, int[] val, int cap) {\nint n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nint[][] dp = new int[n + 1][cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = Math.max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n
unbounded_knapsack.cs
/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint UnboundedKnapsackDP(int[] wgt, int[] val, int cap) {\nint n = wgt.Length;\n// \u521d\u59cb\u5316 dp \u8868\nint[,] dp = new int[n + 1, cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i, c] = dp[i - 1, c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i, c] = Math.Max(dp[i - 1, c], dp[i, c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[n, cap];\n}\n
unbounded_knapsack.go
/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDP(wgt, val []int, cap int) int {\nn := len(wgt)\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([][]int, n+1)\nfor i := 0; i <= n; i++ {\ndp[i] = make([]int, cap+1)\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor i := 1; i <= n; i++ {\nfor c := 1; c <= cap; c++ {\nif wgt[i-1] > c {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i-1][c]\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = int(math.Max(float64(dp[i-1][c]), float64(dp[i][c-wgt[i-1]]+val[i-1])))\n}\n}\n}\nreturn dp[n][cap]\n}\n
unbounded_knapsack.swift
/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDP(wgt: [Int], val: [Int], cap: Int) -> Int {\nlet n = wgt.count\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)\n// \u72b6\u6001\u8f6c\u79fb\nfor i in stride(from: 1, through: n, by: 1) {\nfor c in stride(from: 1, through: cap, by: 1) {\nif wgt[i - 1] > c {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c]\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1])\n}\n}\n}\nreturn dp[n][cap]\n}\n
unbounded_knapsack.js
/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction unboundedKnapsackDP(wgt, val, cap) {\nconst n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: n + 1 }, () =>\nArray.from({ length: cap + 1 }, () => 0)\n);\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\nfor (let c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = Math.max(\ndp[i - 1][c],\ndp[i][c - wgt[i - 1]] + val[i - 1]\n);\n}\n}\n}\nreturn dp[n][cap];\n}\n
unbounded_knapsack.ts
/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction unboundedKnapsackDP(\nwgt: Array<number>,\nval: Array<number>,\ncap: number\n): number {\nconst n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: n + 1 }, () =>\nArray.from({ length: cap + 1 }, () => 0)\n);\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\nfor (let c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = Math.max(\ndp[i - 1][c],\ndp[i][c - wgt[i - 1]] + val[i - 1]\n);\n}\n}\n}\nreturn dp[n][cap];\n}\n
unbounded_knapsack.dart
/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(List<int> wgt, List<int> val, int cap) {\nint n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nList<List<int>> dp = List.generate(n + 1, (index) => List.filled(cap + 1, 0));\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n
unbounded_knapsack.rs
/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfn unbounded_knapsack_dp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\nlet n = wgt.len();\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![vec![0; cap + 1]; n + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor i in 1..=n {\nfor c in 1..=cap {\nif wgt[i - 1] > c as i32 {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = std::cmp::max(dp[i - 1][c], dp[i][c - wgt[i - 1] as usize] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n
unbounded_knapsack.c
/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(int wgt[], int val[], int cap, int wgtSize) {\nint n = wgtSize;\n// \u521d\u59cb\u5316 dp \u8868\nint dp[n + 1][cap + 1];\nmemset(dp, 0, sizeof(dp));\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n
unbounded_knapsack.zig
// \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212\nfn unboundedKnapsackDP(comptime wgt: []i32, val: []i32, comptime cap: usize) i32 {\ncomptime var n = wgt.len;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_][cap + 1]i32{[_]i32{0} ** (cap + 1)} ** (n + 1);\n// \u72b6\u6001\u8f6c\u79fb\nfor (1..n + 1) |i| {\nfor (1..cap + 1) |c| {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = @max(dp[i - 1][c], dp[i][c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

\u7531\u4e8e\u5f53\u524d\u72b6\u6001\u662f\u4ece\u5de6\u8fb9\u548c\u4e0a\u8fb9\u7684\u72b6\u6001\u8f6c\u79fb\u800c\u6765\uff0c\u56e0\u6b64\u7a7a\u95f4\u4f18\u5316\u540e\u5e94\u8be5\u5bf9 \\(dp\\) \u8868\u4e2d\u7684\u6bcf\u4e00\u884c\u91c7\u53d6\u6b63\u5e8f\u904d\u5386\u3002

\u8fd9\u4e2a\u904d\u5386\u987a\u5e8f\u4e0e 0-1 \u80cc\u5305\u6b63\u597d\u76f8\u53cd\u3002\u8bf7\u501f\u52a9\u56fe 14-23 \u6765\u7406\u89e3\u4e24\u8005\u7684\u533a\u522b\u3002

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

\u56fe 14-23 \u00a0 \u5b8c\u5168\u80cc\u5305\u7684\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

\u4ee3\u7801\u5b9e\u73b0\u6bd4\u8f83\u7b80\u5355\uff0c\u4ec5\u9700\u5c06\u6570\u7ec4 dp \u7684\u7b2c\u4e00\u7ef4\u5220\u9664\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig unbounded_knapsack.py
def unbounded_knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -> int:\n\"\"\"\u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(wgt)\n# \u521d\u59cb\u5316 dp \u8868\ndp = [0] * (cap + 1)\n# \u72b6\u6001\u8f6c\u79fb\nfor i in range(1, n + 1):\n# \u6b63\u5e8f\u904d\u5386\nfor c in range(1, cap + 1):\nif wgt[i - 1] > c:\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c]\nelse:\n# \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\nreturn dp[cap]\n
unbounded_knapsack.cpp
/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {\nint n = wgt.size();\n// \u521d\u59cb\u5316 dp \u8868\nvector<int> dp(cap + 1, 0);\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
unbounded_knapsack.java
/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {\nint n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
unbounded_knapsack.cs
/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint UnboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {\nint n = wgt.Length;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = Math.Max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
unbounded_knapsack.go
/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDPComp(wgt, val []int, cap int) int {\nn := len(wgt)\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([]int, cap+1)\n// \u72b6\u6001\u8f6c\u79fb\nfor i := 1; i <= n; i++ {\nfor c := 1; c <= cap; c++ {\nif wgt[i-1] > c {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c]\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = int(math.Max(float64(dp[c]), float64(dp[c-wgt[i-1]]+val[i-1])))\n}\n}\n}\nreturn dp[cap]\n}\n
unbounded_knapsack.swift
/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDPComp(wgt: [Int], val: [Int], cap: Int) -> Int {\nlet n = wgt.count\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: 0, count: cap + 1)\n// \u72b6\u6001\u8f6c\u79fb\nfor i in stride(from: 1, through: n, by: 1) {\nfor c in stride(from: 1, through: cap, by: 1) {\nif wgt[i - 1] > c {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c]\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n}\n}\n}\nreturn dp[cap]\n}\n
unbounded_knapsack.js
/* \u5b8c\u5168\u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction unboundedKnapsackDPComp(wgt, val, cap) {\nconst n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: cap + 1 }, () => 0);\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\nfor (let c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
unbounded_knapsack.ts
/* \u5b8c\u5168\u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction unboundedKnapsackDPComp(\nwgt: Array<number>,\nval: Array<number>,\ncap: number\n): number {\nconst n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: cap + 1 }, () => 0);\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\nfor (let c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
unbounded_knapsack.dart
/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(List<int> wgt, List<int> val, int cap) {\nint n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nList<int> dp = List.filled(cap + 1, 0);\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
unbounded_knapsack.rs
/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn unbounded_knapsack_dp_comp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\nlet n = wgt.len();\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![0; cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor i in 1..=n {\nfor c in 1..=cap {\nif wgt[i - 1] > c as i32 {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = std::cmp::max(dp[c], dp[c - wgt[i - 1] as usize] + val[i - 1]);\n}\n}\n}\ndp[cap]\n}\n
unbounded_knapsack.c
/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(int wgt[], int val[], int cap, int wgtSize) {\nint n = wgtSize;\n// \u521d\u59cb\u5316 dp \u8868\nint dp[cap + 1];\nmemset(dp, 0, sizeof(dp));\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int c = 1; c <= cap; c++) {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
unbounded_knapsack.zig
// \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn unboundedKnapsackDPComp(comptime wgt: []i32, val: []i32, comptime cap: usize) i32 {\ncomptime var n = wgt.len;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_]i32{0} ** (cap + 1);\n// \u72b6\u6001\u8f6c\u79fb\nfor (1..n + 1) |i| {\nfor (1..cap + 1) |c| {\nif (wgt[i - 1] > c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = @max(dp[c], dp[c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1452","title":"14.5.2 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898","text":"

\u80cc\u5305\u95ee\u9898\u662f\u4e00\u5927\u7c7b\u52a8\u6001\u89c4\u5212\u95ee\u9898\u7684\u4ee3\u8868\uff0c\u5176\u62e5\u6709\u5f88\u591a\u7684\u53d8\u79cd\uff0c\u4f8b\u5982\u96f6\u94b1\u5151\u6362\u95ee\u9898\u3002

Question

\u7ed9\u5b9a \\(n\\) \u79cd\u786c\u5e01\uff0c\u7b2c \\(i\\) \u79cd\u786c\u5e01\u7684\u9762\u503c\u4e3a \\(coins[i - 1]\\) \uff0c\u76ee\u6807\u91d1\u989d\u4e3a \\(amt\\) \uff0c\u6bcf\u79cd\u786c\u5e01\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u80fd\u591f\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u7684\u6700\u5c11\u786c\u5e01\u4e2a\u6570\u3002\u5982\u679c\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u5219\u8fd4\u56de \\(-1\\) \u3002

\u56fe 14-24 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1_1","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"

\u96f6\u94b1\u5151\u6362\u53ef\u4ee5\u770b\u4f5c\u662f\u5b8c\u5168\u80cc\u5305\u7684\u4e00\u79cd\u7279\u6b8a\u60c5\u51b5\uff0c\u4e24\u8005\u5177\u6709\u4ee5\u4e0b\u8054\u7cfb\u4e0e\u4e0d\u540c\u70b9\u3002

  • \u4e24\u9053\u9898\u53ef\u4ee5\u76f8\u4e92\u8f6c\u6362\uff0c\u201c\u7269\u54c1\u201d\u5bf9\u5e94\u4e8e\u201c\u786c\u5e01\u201d\u3001\u201c\u7269\u54c1\u91cd\u91cf\u201d\u5bf9\u5e94\u4e8e\u201c\u786c\u5e01\u9762\u503c\u201d\u3001\u201c\u80cc\u5305\u5bb9\u91cf\u201d\u5bf9\u5e94\u4e8e\u201c\u76ee\u6807\u91d1\u989d\u201d\u3002
  • \u4f18\u5316\u76ee\u6807\u76f8\u53cd\uff0c\u80cc\u5305\u95ee\u9898\u662f\u8981\u6700\u5927\u5316\u7269\u54c1\u4ef7\u503c\uff0c\u96f6\u94b1\u5151\u6362\u95ee\u9898\u662f\u8981\u6700\u5c0f\u5316\u786c\u5e01\u6570\u91cf\u3002
  • \u80cc\u5305\u95ee\u9898\u662f\u6c42\u201c\u4e0d\u8d85\u8fc7\u201d\u80cc\u5305\u5bb9\u91cf\u4e0b\u7684\u89e3\uff0c\u96f6\u94b1\u5151\u6362\u662f\u6c42\u201c\u6070\u597d\u201d\u51d1\u5230\u76ee\u6807\u91d1\u989d\u7684\u89e3\u3002

\u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868

\u72b6\u6001 \\([i, a]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u4e3a\uff1a\u524d \\(i\\) \u79cd\u786c\u5e01\u80fd\u591f\u51d1\u51fa\u91d1\u989d \\(a\\) \u7684\u6700\u5c11\u786c\u5e01\u4e2a\u6570\uff0c\u8bb0\u4e3a \\(dp[i, a]\\) \u3002

\u4e8c\u7ef4 \\(dp\\) \u8868\u7684\u5c3a\u5bf8\u4e3a \\((n+1) \\times (amt+1)\\) \u3002

\u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b

\u672c\u9898\u4e0e\u5b8c\u5168\u80cc\u5305\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u5b58\u5728\u4ee5\u4e0b\u4e24\u4e2a\u5dee\u5f02\u3002

  • \u672c\u9898\u8981\u6c42\u6700\u5c0f\u503c\uff0c\u56e0\u6b64\u9700\u5c06\u8fd0\u7b97\u7b26 \\(\\max()\\) \u66f4\u6539\u4e3a \\(\\min()\\) \u3002
  • \u4f18\u5316\u4e3b\u4f53\u662f\u786c\u5e01\u6570\u91cf\u800c\u975e\u5546\u54c1\u4ef7\u503c\uff0c\u56e0\u6b64\u5728\u9009\u4e2d\u786c\u5e01\u65f6\u6267\u884c \\(+1\\) \u5373\u53ef\u3002
\\[ dp[i, a] = \\min(dp[i-1, a], dp[i, a - coins[i-1]] + 1) \\]

\u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f

\u5f53\u76ee\u6807\u91d1\u989d\u4e3a \\(0\\) \u65f6\uff0c\u51d1\u51fa\u5b83\u7684\u6700\u5c11\u786c\u5e01\u4e2a\u6570\u4e3a \\(0\\) \uff0c\u5373\u9996\u5217\u6240\u6709 \\(dp[i, 0]\\) \u90fd\u7b49\u4e8e \\(0\\) \u3002

\u5f53\u65e0\u786c\u5e01\u65f6\uff0c\u65e0\u6cd5\u51d1\u51fa\u4efb\u610f \\(> 0\\) \u7684\u76ee\u6807\u91d1\u989d\uff0c\u5373\u662f\u65e0\u6548\u89e3\u3002\u4e3a\u4f7f\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e2d\u7684 \\(\\min()\\) \u51fd\u6570\u80fd\u591f\u8bc6\u522b\u5e76\u8fc7\u6ee4\u65e0\u6548\u89e3\uff0c\u6211\u4eec\u8003\u8651\u4f7f\u7528 \\(+ \\infty\\) \u6765\u8868\u793a\u5b83\u4eec\uff0c\u5373\u4ee4\u9996\u884c\u6240\u6709 \\(dp[0, a]\\) \u90fd\u7b49\u4e8e \\(+ \\infty\\) \u3002

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2_1","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u5e76\u672a\u63d0\u4f9b \\(+ \\infty\\) \u53d8\u91cf\uff0c\u53ea\u80fd\u4f7f\u7528\u6574\u578b int \u7684\u6700\u5927\u503c\u6765\u4ee3\u66ff\u3002\u800c\u8fd9\u53c8\u4f1a\u5bfc\u81f4\u5927\u6570\u8d8a\u754c\uff1a\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e2d\u7684 \\(+ 1\\) \u64cd\u4f5c\u53ef\u80fd\u53d1\u751f\u6ea2\u51fa\u3002

\u4e3a\u6b64\uff0c\u6211\u4eec\u91c7\u7528\u6570\u5b57 \\(amt + 1\\) \u6765\u8868\u793a\u65e0\u6548\u89e3\uff0c\u56e0\u4e3a\u51d1\u51fa \\(amt\\) \u7684\u786c\u5e01\u4e2a\u6570\u6700\u591a\u4e3a \\(amt\\) \u4e2a\u3002

\u6700\u540e\u8fd4\u56de\u524d\uff0c\u5224\u65ad \\(dp[n, amt]\\) \u662f\u5426\u7b49\u4e8e \\(amt + 1\\) \uff0c\u82e5\u662f\u5219\u8fd4\u56de \\(-1\\) \uff0c\u4ee3\u8868\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig coin_change.py
def coin_change_dp(coins: list[int], amt: int) -> int:\n\"\"\"\u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(coins)\nMAX = amt + 1\n# \u521d\u59cb\u5316 dp \u8868\ndp = [[0] * (amt + 1) for _ in range(n + 1)]\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor a in range(1, amt + 1):\ndp[0][a] = MAX\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in range(1, n + 1):\nfor a in range(1, amt + 1):\nif coins[i - 1] > a:\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a]\nelse:\n# \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\nreturn dp[n][amt] if dp[n][amt] != MAX else -1\n
coin_change.cpp
/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(vector<int> &coins, int amt) {\nint n = coins.size();\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nvector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int a = 1; a <= amt; a++) {\ndp[0][a] = MAX;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
coin_change.java
/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(int[] coins, int amt) {\nint n = coins.length;\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nint[][] dp = new int[n + 1][amt + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int a = 1; a <= amt; a++) {\ndp[0][a] = MAX;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
coin_change.cs
/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint CoinChangeDP(int[] coins, int amt) {\nint n = coins.Length;\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nint[,] dp = new int[n + 1, amt + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int a = 1; a <= amt; a++) {\ndp[0, a] = MAX;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i, a] = dp[i - 1, a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i, a] = Math.Min(dp[i - 1, a], dp[i, a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[n, amt] != MAX ? dp[n, amt] : -1;\n}\n
coin_change.go
/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDP(coins []int, amt int) int {\nn := len(coins)\nmax := amt + 1\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([][]int, n+1)\nfor i := 0; i <= n; i++ {\ndp[i] = make([]int, amt+1)\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor a := 1; a <= amt; a++ {\ndp[0][a] = max\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i := 1; i <= n; i++ {\nfor a := 1; a <= amt; a++ {\nif coins[i-1] > a {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i-1][a]\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = int(math.Min(float64(dp[i-1][a]), float64(dp[i][a-coins[i-1]]+1)))\n}\n}\n}\nif dp[n][amt] != max {\nreturn dp[n][amt]\n}\nreturn -1\n}\n
coin_change.swift
/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDP(coins: [Int], amt: Int) -> Int {\nlet n = coins.count\nlet MAX = amt + 1\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor a in stride(from: 1, through: amt, by: 1) {\ndp[0][a] = MAX\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in stride(from: 1, through: n, by: 1) {\nfor a in stride(from: 1, through: amt, by: 1) {\nif coins[i - 1] > a {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a]\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n}\n}\n}\nreturn dp[n][amt] != MAX ? dp[n][amt] : -1\n}\n
coin_change.js
/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeDP(coins, amt) {\nconst n = coins.length;\nconst MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: n + 1 }, () =>\nArray.from({ length: amt + 1 }, () => 0)\n);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (let a = 1; a <= amt; a++) {\ndp[0][a] = MAX;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (let i = 1; i <= n; i++) {\nfor (let a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[n][amt] !== MAX ? dp[n][amt] : -1;\n}\n
coin_change.ts
/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeDP(coins: Array<number>, amt: number): number {\nconst n = coins.length;\nconst MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: n + 1 }, () =>\nArray.from({ length: amt + 1 }, () => 0)\n);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (let a = 1; a <= amt; a++) {\ndp[0][a] = MAX;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (let i = 1; i <= n; i++) {\nfor (let a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[n][amt] !== MAX ? dp[n][amt] : -1;\n}\n
coin_change.dart
/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(List<int> coins, int amt) {\nint n = coins.length;\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nList<List<int>> dp = List.generate(n + 1, (index) => List.filled(amt + 1, 0));\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int a = 1; a <= amt; a++) {\ndp[0][a] = MAX;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
coin_change.rs
/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfn coin_change_dp(coins: &[i32], amt: usize) -> i32 {\nlet n = coins.len();\nlet max = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![vec![0; amt + 1]; n + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor a in 1..= amt {\ndp[0][a] = max;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in 1..=n {\nfor a in 1..=amt {\nif coins[i - 1] > a as i32 {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = std::cmp::min(dp[i - 1][a], dp[i][a - coins[i - 1] as usize] + 1);\n}\n}\n}\nif dp[n][amt] != max { return dp[n][amt] as i32; } else { -1 }\n}\n
coin_change.c
/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(int coins[], int amt, int coinsSize) {\nint n = coinsSize;\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nint dp[n + 1][amt + 1];\nmemset(dp, 0, sizeof(dp));\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int a = 1; a <= amt; a++) {\ndp[0][a] = MAX;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
coin_change.zig
// \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212\nfn coinChangeDP(comptime coins: []i32, comptime amt: usize) i32 {\ncomptime var n = coins.len;\ncomptime var max = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_][amt + 1]i32{[_]i32{0} ** (amt + 1)} ** (n + 1);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (1..amt + 1) |a| {\ndp[0][a] = max;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (1..n + 1) |i| {\nfor (1..amt + 1) |a| {\nif (coins[i - 1] > @as(i32, @intCast(a))) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = @min(dp[i - 1][a], dp[i][a - @as(usize, @intCast(coins[i - 1]))] + 1);\n}\n}\n}\nif (dp[n][amt] != max) {\nreturn @intCast(dp[n][amt]);\n} else {\nreturn -1;\n}\n}\n

\u56fe 14-25 \u5c55\u793a\u4e86\u96f6\u94b1\u5151\u6362\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b\uff0c\u548c\u5b8c\u5168\u80cc\u5305\u975e\u5e38\u76f8\u4f3c\u3002

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

\u56fe 14-25 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3_1","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

\u96f6\u94b1\u5151\u6362\u7684\u7a7a\u95f4\u4f18\u5316\u7684\u5904\u7406\u65b9\u5f0f\u548c\u5b8c\u5168\u80cc\u5305\u4e00\u81f4\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig coin_change.py
def coin_change_dp_comp(coins: list[int], amt: int) -> int:\n\"\"\"\u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(coins)\nMAX = amt + 1\n# \u521d\u59cb\u5316 dp \u8868\ndp = [MAX] * (amt + 1)\ndp[0] = 0\n# \u72b6\u6001\u8f6c\u79fb\nfor i in range(1, n + 1):\n# \u6b63\u5e8f\u904d\u5386\nfor a in range(1, amt + 1):\nif coins[i - 1] > a:\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a]\nelse:\n# \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\nreturn dp[amt] if dp[amt] != MAX else -1\n
coin_change.cpp
/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(vector<int> &coins, int amt) {\nint n = coins.size();\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nvector<int> dp(amt + 1, MAX);\ndp[0] = 0;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[amt] != MAX ? dp[amt] : -1;\n}\n
coin_change.java
/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(int[] coins, int amt) {\nint n = coins.length;\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[amt + 1];\nArrays.fill(dp, MAX);\ndp[0] = 0;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[amt] != MAX ? dp[amt] : -1;\n}\n
coin_change.cs
/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint CoinChangeDPComp(int[] coins, int amt) {\nint n = coins.Length;\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[amt + 1];\nArray.Fill(dp, MAX);\ndp[0] = 0;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = Math.Min(dp[a], dp[a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[amt] != MAX ? dp[amt] : -1;\n}\n
coin_change.go
/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDPComp(coins []int, amt int) int {\nn := len(coins)\nmax := amt + 1\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([]int, amt+1)\nfor i := 1; i <= amt; i++ {\ndp[i] = max\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor i := 1; i <= n; i++ {\n// \u5012\u5e8f\u904d\u5386\nfor a := 1; a <= amt; a++ {\nif coins[i-1] > a {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a]\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = int(math.Min(float64(dp[a]), float64(dp[a-coins[i-1]]+1)))\n}\n}\n}\nif dp[amt] != max {\nreturn dp[amt]\n}\nreturn -1\n}\n
coin_change.swift
/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDPComp(coins: [Int], amt: Int) -> Int {\nlet n = coins.count\nlet MAX = amt + 1\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: MAX, count: amt + 1)\ndp[0] = 0\n// \u72b6\u6001\u8f6c\u79fb\nfor i in stride(from: 1, through: n, by: 1) {\nfor a in stride(from: 1, through: amt, by: 1) {\nif coins[i - 1] > a {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a]\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n}\n}\n}\nreturn dp[amt] != MAX ? dp[amt] : -1\n}\n
coin_change.js
/* \u96f6\u94b1\u5151\u6362\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeDPComp(coins, amt) {\nconst n = coins.length;\nconst MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: amt + 1 }, () => MAX);\ndp[0] = 0;\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\nfor (let a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[amt] !== MAX ? dp[amt] : -1;\n}\n
coin_change.ts
/* \u96f6\u94b1\u5151\u6362\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeDPComp(coins: Array<number>, amt: number): number {\nconst n = coins.length;\nconst MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: amt + 1 }, () => MAX);\ndp[0] = 0;\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\nfor (let a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[amt] !== MAX ? dp[amt] : -1;\n}\n
coin_change.dart
/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(List<int> coins, int amt) {\nint n = coins.length;\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nList<int> dp = List.filled(amt + 1, MAX);\ndp[0] = 0;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[amt] != MAX ? dp[amt] : -1;\n}\n
coin_change.rs
/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn coin_change_dp_comp(coins: &[i32], amt: usize) -> i32 {\nlet n = coins.len();\nlet max = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![0; amt + 1];\ndp.fill(max);\ndp[0] = 0;\n// \u72b6\u6001\u8f6c\u79fb\nfor i in 1..=n {\nfor a in 1..=amt {\nif coins[i - 1] > a as i32 {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = std::cmp::min(dp[a], dp[a - coins[i - 1] as usize] + 1);\n}\n}\n}\nif dp[amt] != max { return dp[amt] as i32; } else { -1 }\n}\n
coin_change.c
/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(int coins[], int amt, int coinsSize) {\nint n = coinsSize;\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nint dp[amt + 1];\nmemset(dp, MAX, sizeof(dp));\ndp[0] = 0;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[amt] != MAX ? dp[amt] : -1;\n}\n
coin_change.zig
// \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn coinChangeDPComp(comptime coins: []i32, comptime amt: usize) i32 {\ncomptime var n = coins.len;\ncomptime var max = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_]i32{0} ** (amt + 1);\n@memset(&dp, max);\ndp[0] = 0;\n// \u72b6\u6001\u8f6c\u79fb\nfor (1..n + 1) |i| {\nfor (1..amt + 1) |a| {\nif (coins[i - 1] > @as(i32, @intCast(a))) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = @min(dp[a], dp[a - @as(usize, @intCast(coins[i - 1]))] + 1);\n}\n}\n}\nif (dp[amt] != max) {\nreturn @intCast(dp[amt]);\n} else {\nreturn -1;\n}\n}\n
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1453-ii","title":"14.5.3 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898 II","text":"

Question

\u7ed9\u5b9a \\(n\\) \u79cd\u786c\u5e01\uff0c\u7b2c \\(i\\) \u79cd\u786c\u5e01\u7684\u9762\u503c\u4e3a \\(coins[i - 1]\\) \uff0c\u76ee\u6807\u91d1\u989d\u4e3a \\(amt\\) \uff0c\u6bcf\u79cd\u786c\u5e01\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u5728\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u7684\u786c\u5e01\u7ec4\u5408\u6570\u91cf\u3002

\u56fe 14-26 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898 II \u7684\u793a\u4f8b\u6570\u636e

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1_2","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"

\u76f8\u6bd4\u4e8e\u4e0a\u4e00\u9898\uff0c\u672c\u9898\u76ee\u6807\u662f\u7ec4\u5408\u6570\u91cf\uff0c\u56e0\u6b64\u5b50\u95ee\u9898\u53d8\u4e3a\uff1a\u524d \\(i\\) \u79cd\u786c\u5e01\u80fd\u591f\u51d1\u51fa\u91d1\u989d \\(a\\) \u7684\u7ec4\u5408\u6570\u91cf\u3002\u800c \\(dp\\) \u8868\u4ecd\u7136\u662f\u5c3a\u5bf8\u4e3a \\((n+1) \\times (amt + 1)\\) \u7684\u4e8c\u7ef4\u77e9\u9635\u3002

\u5f53\u524d\u72b6\u6001\u7684\u7ec4\u5408\u6570\u91cf\u7b49\u4e8e\u4e0d\u9009\u5f53\u524d\u786c\u5e01\u4e0e\u9009\u5f53\u524d\u786c\u5e01\u8fd9\u4e24\u79cd\u51b3\u7b56\u7684\u7ec4\u5408\u6570\u91cf\u4e4b\u548c\u3002\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a

\\[ dp[i, a] = dp[i-1, a] + dp[i, a - coins[i-1]] \\]

\u5f53\u76ee\u6807\u91d1\u989d\u4e3a \\(0\\) \u65f6\uff0c\u65e0\u987b\u9009\u62e9\u4efb\u4f55\u786c\u5e01\u5373\u53ef\u51d1\u51fa\u76ee\u6807\u91d1\u989d\uff0c\u56e0\u6b64\u5e94\u5c06\u9996\u5217\u6240\u6709 \\(dp[i, 0]\\) \u90fd\u521d\u59cb\u5316\u4e3a \\(1\\) \u3002\u5f53\u65e0\u786c\u5e01\u65f6\uff0c\u65e0\u6cd5\u51d1\u51fa\u4efb\u4f55 \\(>0\\) \u7684\u76ee\u6807\u91d1\u989d\uff0c\u56e0\u6b64\u9996\u884c\u6240\u6709 \\(dp[0, a]\\) \u90fd\u7b49\u4e8e \\(0\\) \u3002

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2_2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"PythonC++JavaC#GoSwiftJSTSDartRustCZig coin_change_ii.py
def coin_change_ii_dp(coins: list[int], amt: int) -> int:\n\"\"\"\u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(coins)\n# \u521d\u59cb\u5316 dp \u8868\ndp = [[0] * (amt + 1) for _ in range(n + 1)]\n# \u521d\u59cb\u5316\u9996\u5217\nfor i in range(n + 1):\ndp[i][0] = 1\n# \u72b6\u6001\u8f6c\u79fb\nfor i in range(1, n + 1):\nfor a in range(1, amt + 1):\nif coins[i - 1] > a:\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a]\nelse:\n# \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\nreturn dp[n][amt]\n
coin_change_ii.cpp
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(vector<int> &coins, int amt) {\nint n = coins.size();\n// \u521d\u59cb\u5316 dp \u8868\nvector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));\n// \u521d\u59cb\u5316\u9996\u5217\nfor (int i = 0; i <= n; i++) {\ndp[i][0] = 1;\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n}\n}\n}\nreturn dp[n][amt];\n}\n
coin_change_ii.java
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(int[] coins, int amt) {\nint n = coins.length;\n// \u521d\u59cb\u5316 dp \u8868\nint[][] dp = new int[n + 1][amt + 1];\n// \u521d\u59cb\u5316\u9996\u5217\nfor (int i = 0; i <= n; i++) {\ndp[i][0] = 1;\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n}\n}\n}\nreturn dp[n][amt];\n}\n
coin_change_ii.cs
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint CoinChangeIIDP(int[] coins, int amt) {\nint n = coins.Length;\n// \u521d\u59cb\u5316 dp \u8868\nint[,] dp = new int[n + 1, amt + 1];\n// \u521d\u59cb\u5316\u9996\u5217\nfor (int i = 0; i <= n; i++) {\ndp[i, 0] = 1;\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i, a] = dp[i - 1, a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i, a] = dp[i - 1, a] + dp[i, a - coins[i - 1]];\n}\n}\n}\nreturn dp[n, amt];\n}\n
coin_change_ii.go
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDP(coins []int, amt int) int {\nn := len(coins)\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([][]int, n+1)\nfor i := 0; i <= n; i++ {\ndp[i] = make([]int, amt+1)\n}\n// \u521d\u59cb\u5316\u9996\u5217\nfor i := 0; i <= n; i++ {\ndp[i][0] = 1\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i := 1; i <= n; i++ {\nfor a := 1; a <= amt; a++ {\nif coins[i-1] > a {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i-1][a]\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i][a] = dp[i-1][a] + dp[i][a-coins[i-1]]\n}\n}\n}\nreturn dp[n][amt]\n}\n
coin_change_ii.swift
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDP(coins: [Int], amt: Int) -> Int {\nlet n = coins.count\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)\n// \u521d\u59cb\u5316\u9996\u5217\nfor i in stride(from: 0, through: n, by: 1) {\ndp[i][0] = 1\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor i in stride(from: 1, through: n, by: 1) {\nfor a in stride(from: 1, through: amt, by: 1) {\nif coins[i - 1] > a {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a]\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n}\n}\n}\nreturn dp[n][amt]\n}\n
coin_change_ii.js
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeIIDP(coins, amt) {\nconst n = coins.length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: n + 1 }, () =>\nArray.from({ length: amt + 1 }, () => 0)\n);\n// \u521d\u59cb\u5316\u9996\u5217\nfor (let i = 0; i <= n; i++) {\ndp[i][0] = 1;\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\nfor (let a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n}\n}\n}\nreturn dp[n][amt];\n}\n
coin_change_ii.ts
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeIIDP(coins: Array<number>, amt: number): number {\nconst n = coins.length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: n + 1 }, () =>\nArray.from({ length: amt + 1 }, () => 0)\n);\n// \u521d\u59cb\u5316\u9996\u5217\nfor (let i = 0; i <= n; i++) {\ndp[i][0] = 1;\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\nfor (let a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n}\n}\n}\nreturn dp[n][amt];\n}\n
coin_change_ii.dart
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(List<int> coins, int amt) {\nint n = coins.length;\n// \u521d\u59cb\u5316 dp \u8868\nList<List<int>> dp = List.generate(n + 1, (index) => List.filled(amt + 1, 0));\n// \u521d\u59cb\u5316\u9996\u5217\nfor (int i = 0; i <= n; i++) {\ndp[i][0] = 1;\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n}\n}\n}\nreturn dp[n][amt];\n}\n
coin_change_ii.rs
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfn coin_change_ii_dp(coins: &[i32], amt: usize) -> i32 {\nlet n = coins.len();\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![vec![0; amt + 1]; n + 1];\n// \u521d\u59cb\u5316\u9996\u5217\nfor i in 0..= n {\ndp[i][0] = 1;\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor i in 1..=n {\nfor a in 1..=amt {\nif coins[i - 1] > a as i32 {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1] as usize];\n}\n}\n}\ndp[n][amt]\n}\n
coin_change_ii.c
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(int coins[], int amt, int coinsSize) {\nint n = coinsSize;\n// \u521d\u59cb\u5316 dp \u8868\nint dp[n + 1][amt + 1];\nmemset(dp, 0, sizeof(dp));\n// \u521d\u59cb\u5316\u9996\u5217\nfor (int i = 0; i <= n; i++) {\ndp[i][0] = 1;\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n}\n}\n}\nreturn dp[n][amt];\n}\n
coin_change_ii.zig
// \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212\nfn coinChangeIIDP(comptime coins: []i32, comptime amt: usize) i32 {\ncomptime var n = coins.len;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_][amt + 1]i32{[_]i32{0} ** (amt + 1)} ** (n + 1);\n// \u521d\u59cb\u5316\u9996\u5217\nfor (0..n + 1) |i| {\ndp[i][0] = 1;\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor (1..n + 1) |i| {\nfor (1..amt + 1) |a| {\nif (coins[i - 1] > @as(i32, @intCast(a))) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = dp[i - 1][a] + dp[i][a - @as(usize, @intCast(coins[i - 1]))];\n}\n}\n}\nreturn dp[n][amt];\n}\n
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3_2","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"

\u7a7a\u95f4\u4f18\u5316\u5904\u7406\u65b9\u5f0f\u76f8\u540c\uff0c\u5220\u9664\u786c\u5e01\u7ef4\u5ea6\u5373\u53ef\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig coin_change_ii.py
def coin_change_ii_dp_comp(coins: list[int], amt: int) -> int:\n\"\"\"\u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(coins)\n# \u521d\u59cb\u5316 dp \u8868\ndp = [0] * (amt + 1)\ndp[0] = 1\n# \u72b6\u6001\u8f6c\u79fb\nfor i in range(1, n + 1):\n# \u6b63\u5e8f\u904d\u5386\nfor a in range(1, amt + 1):\nif coins[i - 1] > a:\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a]\nelse:\n# \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a - coins[i - 1]]\nreturn dp[amt]\n
coin_change_ii.cpp
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(vector<int> &coins, int amt) {\nint n = coins.size();\n// \u521d\u59cb\u5316 dp \u8868\nvector<int> dp(amt + 1, 0);\ndp[0] = 1;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a - coins[i - 1]];\n}\n}\n}\nreturn dp[amt];\n}\n
coin_change_ii.java
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(int[] coins, int amt) {\nint n = coins.length;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[amt + 1];\ndp[0] = 1;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a - coins[i - 1]];\n}\n}\n}\nreturn dp[amt];\n}\n
coin_change_ii.cs
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint CoinChangeIIDPComp(int[] coins, int amt) {\nint n = coins.Length;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[amt + 1];\ndp[0] = 1;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a - coins[i - 1]];\n}\n}\n}\nreturn dp[amt];\n}\n
coin_change_ii.go
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDPComp(coins []int, amt int) int {\nn := len(coins)\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([]int, amt+1)\ndp[0] = 1\n// \u72b6\u6001\u8f6c\u79fb\nfor i := 1; i <= n; i++ {\n// \u5012\u5e8f\u904d\u5386\nfor a := 1; a <= amt; a++ {\nif coins[i-1] > a {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a]\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a-coins[i-1]]\n}\n}\n}\nreturn dp[amt]\n}\n
coin_change_ii.swift
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDPComp(coins: [Int], amt: Int) -> Int {\nlet n = coins.count\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: 0, count: amt + 1)\ndp[0] = 1\n// \u72b6\u6001\u8f6c\u79fb\nfor i in stride(from: 1, through: n, by: 1) {\nfor a in stride(from: 1, through: amt, by: 1) {\nif coins[i - 1] > a {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a]\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a - coins[i - 1]]\n}\n}\n}\nreturn dp[amt]\n}\n
coin_change_ii.js
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeIIDPComp(coins, amt) {\nconst n = coins.length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: amt + 1 }, () => 0);\ndp[0] = 1;\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\nfor (let a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a - coins[i - 1]];\n}\n}\n}\nreturn dp[amt];\n}\n
coin_change_ii.ts
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeIIDPComp(coins: Array<number>, amt: number): number {\nconst n = coins.length;\n// \u521d\u59cb\u5316 dp \u8868\nconst dp = Array.from({ length: amt + 1 }, () => 0);\ndp[0] = 1;\n// \u72b6\u6001\u8f6c\u79fb\nfor (let i = 1; i <= n; i++) {\nfor (let a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a - coins[i - 1]];\n}\n}\n}\nreturn dp[amt];\n}\n
coin_change_ii.dart
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(List<int> coins, int amt) {\nint n = coins.length;\n// \u521d\u59cb\u5316 dp \u8868\nList<int> dp = List.filled(amt + 1, 0);\ndp[0] = 1;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a - coins[i - 1]];\n}\n}\n}\nreturn dp[amt];\n}\n
coin_change_ii.rs
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn coin_change_ii_dp_comp(coins: &[i32], amt: usize) -> i32 {\nlet n = coins.len();\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![0; amt + 1];\ndp[0] = 1;\n// \u72b6\u6001\u8f6c\u79fb\nfor i in 1..=n {\nfor a in 1..=amt {\nif coins[i - 1] > a as i32 {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = dp[a] + dp[a - coins[i - 1] as usize];\n}\n}\n}\ndp[amt]\n}\n
coin_change_ii.c
/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(int coins[], int amt, int coinsSize) {\nint n = coinsSize;\n// \u521d\u59cb\u5316 dp \u8868\nint dp[amt + 1];\nmemset(dp, 0, sizeof(dp));\ndp[0] = 1;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i <= n; i++) {\nfor (int a = 1; a <= amt; a++) {\nif (coins[i - 1] > a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a - coins[i - 1]];\n}\n}\n}\nreturn dp[amt];\n}\n
coin_change_ii.zig
// \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn coinChangeIIDPComp(comptime coins: []i32, comptime amt: usize) i32 {\ncomptime var n = coins.len;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_]i32{0} ** (amt + 1);\ndp[0] = 1;\n// \u72b6\u6001\u8f6c\u79fb\nfor (1..n + 1) |i| {\nfor (1..amt + 1) |a| {\nif (coins[i - 1] > @as(i32, @intCast(a))) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = dp[a] + dp[a - @as(usize, @intCast(coins[i - 1]))];\n}\n}\n}\nreturn dp[amt];\n}\n
"},{"location":"chapter_graph/","title":"\u7b2c 9 \u7ae0 \u00a0 \u56fe","text":"

Abstract

\u5728\u751f\u547d\u65c5\u9014\u4e2d\uff0c\u6211\u4eec\u5c31\u50cf\u662f\u6bcf\u4e2a\u8282\u70b9\uff0c\u88ab\u65e0\u6570\u770b\u4e0d\u89c1\u7684\u8fb9\u76f8\u8fde\u3002

\u6bcf\u4e00\u6b21\u7684\u76f8\u8bc6\u4e0e\u76f8\u79bb\uff0c\u90fd\u5728\u8fd9\u5f20\u5de8\u5927\u7684\u7f51\u7edc\u56fe\u4e2d\u7559\u4e0b\u72ec\u7279\u7684\u5370\u8bb0\u3002

"},{"location":"chapter_graph/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 9.1 \u00a0 \u56fe
  • 9.2 \u00a0 \u56fe\u57fa\u7840\u64cd\u4f5c
  • 9.3 \u00a0 \u56fe\u7684\u904d\u5386
  • 9.4 \u00a0 \u5c0f\u7ed3
"},{"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} \\]

\u5982\u679c\u5c06\u9876\u70b9\u770b\u4f5c\u8282\u70b9\uff0c\u5c06\u8fb9\u770b\u4f5c\u8fde\u63a5\u5404\u4e2a\u8282\u70b9\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u56fe\u770b\u4f5c\u662f\u4e00\u79cd\u4ece\u94fe\u8868\u62d3\u5c55\u800c\u6765\u7684\u6570\u636e\u7ed3\u6784\u3002\u5982\u56fe 9-1 \u6240\u793a\uff0c\u76f8\u8f83\u4e8e\u7ebf\u6027\u5173\u7cfb\uff08\u94fe\u8868\uff09\u548c\u5206\u6cbb\u5173\u7cfb\uff08\u6811\uff09\uff0c\u7f51\u7edc\u5173\u7cfb\uff08\u56fe\uff09\u7684\u81ea\u7531\u5ea6\u66f4\u9ad8\uff0c\u4ece\u800c\u66f4\u4e3a\u590d\u6742\u3002

\u56fe 9-1 \u00a0 \u94fe\u8868\u3001\u6811\u3001\u56fe\u4e4b\u95f4\u7684\u5173\u7cfb

"},{"location":"chapter_graph/graph/#911","title":"9.1.1 \u00a0 \u56fe\u5e38\u89c1\u7c7b\u578b\u4e0e\u672f\u8bed","text":"

\u6839\u636e\u8fb9\u662f\u5426\u5177\u6709\u65b9\u5411\uff0c\u53ef\u5206\u4e3a\u56fe 9-2 \u6240\u793a\u7684\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\u3002
  • \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\u3002

\u56fe 9-2 \u00a0 \u6709\u5411\u56fe\u4e0e\u65e0\u5411\u56fe

\u6839\u636e\u6240\u6709\u9876\u70b9\u662f\u5426\u8fde\u901a\uff0c\u53ef\u5206\u4e3a\u56fe 9-3 \u6240\u793a\u7684\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\u3002
  • \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\u3002

\u56fe 9-3 \u00a0 \u8fde\u901a\u56fe\u4e0e\u975e\u8fde\u901a\u56fe

\u6211\u4eec\u8fd8\u53ef\u4ee5\u4e3a\u8fb9\u6dfb\u52a0\u201c\u6743\u91cd\u201d\u53d8\u91cf\uff0c\u4ece\u800c\u5f97\u5230\u56fe 9-4 \u6240\u793a\u7684\u300c\u6709\u6743\u56fe weighted graph\u300d\u3002\u4f8b\u5982\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

\u56fe 9-4 \u00a0 \u6709\u6743\u56fe\u4e0e\u65e0\u6743\u56fe

\u56fe\u6570\u636e\u7ed3\u6784\u5305\u542b\u4ee5\u4e0b\u5e38\u7528\u672f\u8bed\u3002

  • \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\u56fe 9-4 \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\u56fe 9-4 \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\uff1a\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/#912","title":"9.1.2 \u00a0 \u56fe\u7684\u8868\u793a","text":"

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

"},{"location":"chapter_graph/graph/#1","title":"1. \u00a0 \u90bb\u63a5\u77e9\u9635","text":"

\u8bbe\u56fe\u7684\u9876\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u300c\u90bb\u63a5\u77e9\u9635 adjacency matrix\u300d\u4f7f\u7528\u4e00\u4e2a \\(n \\times n\\) \u5927\u5c0f\u7684\u77e9\u9635\u6765\u8868\u793a\u56fe\uff0c\u6bcf\u4e00\u884c\uff08\u5217\uff09\u4ee3\u8868\u4e00\u4e2a\u9876\u70b9\uff0c\u77e9\u9635\u5143\u7d20\u4ee3\u8868\u8fb9\uff0c\u7528 \\(1\\) \u6216 \\(0\\) \u8868\u793a\u4e24\u4e2a\u9876\u70b9\u4e4b\u95f4\u662f\u5426\u5b58\u5728\u8fb9\u3002

\u5982\u56fe 9-5 \u6240\u793a\uff0c\u8bbe\u90bb\u63a5\u77e9\u9635\u4e3a \\(M\\)\u3001\u9876\u70b9\u5217\u8868\u4e3a \\(V\\) \uff0c\u90a3\u4e48\u77e9\u9635\u5143\u7d20 \\(M[i, j] = 1\\) \u8868\u793a\u9876\u70b9 \\(V[i]\\) \u5230\u9876\u70b9 \\(V[j]\\) \u4e4b\u95f4\u5b58\u5728\u8fb9\uff0c\u53cd\u4e4b \\(M[i, j] = 0\\) \u8868\u793a\u4e24\u9876\u70b9\u4e4b\u95f4\u65e0\u8fb9\u3002

\u56fe 9-5 \u00a0 \u56fe\u7684\u90bb\u63a5\u77e9\u9635\u8868\u793a

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

  • \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\\) \u548c \\(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":"2. \u00a0 \u90bb\u63a5\u8868","text":"

\u300c\u90bb\u63a5\u8868 adjacency list\u300d\u4f7f\u7528 \\(n\\) \u4e2a\u94fe\u8868\u6765\u8868\u793a\u56fe\uff0c\u94fe\u8868\u8282\u70b9\u8868\u793a\u9876\u70b9\u3002\u7b2c \\(i\\) \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\u56fe 9-6 \u5c55\u793a\u4e86\u4e00\u4e2a\u4f7f\u7528\u90bb\u63a5\u8868\u5b58\u50a8\u7684\u56fe\u7684\u793a\u4f8b\u3002

\u56fe 9-6 \u00a0 \u56fe\u7684\u90bb\u63a5\u8868\u8868\u793a

\u90bb\u63a5\u8868\u4ec5\u5b58\u50a8\u5b9e\u9645\u5b58\u5728\u7684\u8fb9\uff0c\u800c\u8fb9\u7684\u603b\u6570\u901a\u5e38\u8fdc\u5c0f\u4e8e \\(n^2\\) \uff0c\u56e0\u6b64\u5b83\u66f4\u52a0\u8282\u7701\u7a7a\u95f4\u3002\u7136\u800c\uff0c\u5728\u90bb\u63a5\u8868\u4e2d\u9700\u8981\u901a\u8fc7\u904d\u5386\u94fe\u8868\u6765\u67e5\u627e\u8fb9\uff0c\u56e0\u6b64\u5176\u65f6\u95f4\u6548\u7387\u4e0d\u5982\u90bb\u63a5\u77e9\u9635\u3002

\u89c2\u5bdf\u56fe 9-6 \uff0c\u90bb\u63a5\u8868\u7ed3\u6784\u4e0e\u54c8\u5e0c\u8868\u4e2d\u7684\u201c\u94fe\u5f0f\u5730\u5740\u201d\u975e\u5e38\u76f8\u4f3c\uff0c\u56e0\u6b64\u6211\u4eec\u4e5f\u53ef\u4ee5\u91c7\u7528\u7c7b\u4f3c\u65b9\u6cd5\u6765\u4f18\u5316\u6548\u7387\u3002\u6bd4\u5982\u5f53\u94fe\u8868\u8f83\u957f\u65f6\uff0c\u53ef\u4ee5\u5c06\u94fe\u8868\u8f6c\u5316\u4e3a AVL \u6811\u6216\u7ea2\u9ed1\u6811\uff0c\u4ece\u800c\u5c06\u65f6\u95f4\u6548\u7387\u4ece \\(O(n)\\) \u4f18\u5316\u81f3 \\(O(\\log n)\\) \uff1b\u8fd8\u53ef\u4ee5\u628a\u94fe\u8868\u8f6c\u6362\u4e3a\u54c8\u5e0c\u8868\uff0c\u4ece\u800c\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u964d\u4f4e\u81f3 \\(O(1)\\) \u3002

"},{"location":"chapter_graph/graph/#913","title":"9.1.3 \u00a0 \u56fe\u5e38\u89c1\u5e94\u7528","text":"

\u5982\u8868 9-1 \u6240\u793a\uff0c\u8bb8\u591a\u73b0\u5b9e\u7cfb\u7edf\u90fd\u53ef\u4ee5\u7528\u56fe\u6765\u5efa\u6a21\uff0c\u76f8\u5e94\u7684\u95ee\u9898\u4e5f\u53ef\u4ee5\u7ea6\u5316\u4e3a\u56fe\u8ba1\u7b97\u95ee\u9898\u3002

\u8868 9-1 \u00a0 \u73b0\u5b9e\u751f\u6d3b\u4e2d\u5e38\u89c1\u7684\u56fe

\u9876\u70b9 \u8fb9 \u56fe\u8ba1\u7b97\u95ee\u9898 \u793e\u4ea4\u7f51\u7edc \u7528\u6237 \u597d\u53cb\u5173\u7cfb \u6f5c\u5728\u597d\u53cb\u63a8\u8350 \u5730\u94c1\u7ebf\u8def \u7ad9\u70b9 \u7ad9\u70b9\u95f4\u7684\u8fde\u901a\u6027 \u6700\u77ed\u8def\u7ebf\u63a8\u8350 \u592a\u9633\u7cfb \u661f\u4f53 \u661f\u4f53\u95f4\u7684\u4e07\u6709\u5f15\u529b\u4f5c\u7528 \u884c\u661f\u8f68\u9053\u8ba1\u7b97"},{"location":"chapter_graph/graph_operations/","title":"9.2 \u00a0 \u56fe\u57fa\u7840\u64cd\u4f5c","text":"

\u56fe\u7684\u57fa\u7840\u64cd\u4f5c\u53ef\u5206\u4e3a\u5bf9\u201c\u8fb9\u201d\u7684\u64cd\u4f5c\u548c\u5bf9\u201c\u9876\u70b9\u201d\u7684\u64cd\u4f5c\u3002\u5728\u201c\u90bb\u63a5\u77e9\u9635\u201d\u548c\u201c\u90bb\u63a5\u8868\u201d\u4e24\u79cd\u8868\u793a\u65b9\u6cd5\u4e0b\uff0c\u5b9e\u73b0\u65b9\u5f0f\u6709\u6240\u4e0d\u540c\u3002

"},{"location":"chapter_graph/graph_operations/#921","title":"9.2.1 \u00a0 \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u7684\u5b9e\u73b0","text":"

\u7ed9\u5b9a\u4e00\u4e2a\u9876\u70b9\u6570\u91cf\u4e3a \\(n\\) \u7684\u65e0\u5411\u56fe\uff0c\u5219\u5404\u79cd\u64cd\u4f5c\u7684\u5b9e\u73b0\u65b9\u5f0f\u5982\u56fe 9-7 \u6240\u793a\u3002

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

\u56fe 9-7 \u00a0 \u90bb\u63a5\u77e9\u9635\u7684\u521d\u59cb\u5316\u3001\u589e\u5220\u8fb9\u3001\u589e\u5220\u9876\u70b9

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

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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]]):\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):\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):\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):\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):\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):\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.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(vector<int>(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.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.cs
/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nreadonly List<int> vertices;     // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\nreadonly List<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(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.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\u51fd\u6570 */\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.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.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.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
graph_adjacency_matrix.rs
/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\u578b */\npub struct GraphAdjMat {\n// \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\npub vertices: Vec<i32>,\n// \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\npub adj_mat: Vec<Vec<i32>>,\n}\nimpl GraphAdjMat {\n/* \u6784\u9020\u65b9\u6cd5 */\npub fn new(vertices: Vec<i32>, edges: Vec<[usize; 2]>) -> Self {\nlet mut graph = GraphAdjMat {\nvertices: vec![],\nadj_mat: vec![],\n};\n// \u6dfb\u52a0\u9876\u70b9\nfor val in vertices {\ngraph.add_vertex(val);\n}\n// \u6dfb\u52a0\u8fb9\n// \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfor edge in edges {\ngraph.add_edge(edge[0], edge[1])\n}\ngraph\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\npub fn size(&self) -> usize {\nself.vertices.len()\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npub fn add_vertex(&mut self, val: i32) {\nlet n = self.size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nself.vertices.push(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nself.adj_mat.push(vec![0; n]);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor row in &mut self.adj_mat {\nrow.push(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\npub fn remove_vertex(&mut self, index: usize) {\nif index >= self.size() {\npanic!(\"index error\")\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nself.vertices.remove(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nself.adj_mat.remove(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor row in &mut self.adj_mat {\nrow.remove(index);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\npub fn add_edge(&mut self, i: usize, j: usize) {\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif i >= self.size() || j >= self.size() || i == j {\npanic!(\"index error\")\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)\nself.adj_mat[i][j] = 1;\nself.adj_mat[j][i] = 1;\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\npub fn remove_edge(&mut self, i: usize, j: usize) {\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif i >= self.size() || j >= self.size() || i == j {\npanic!(\"index error\")\n}\nself.adj_mat[i][j] = 0;\nself.adj_mat[j][i] = 0;\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\npub fn print(&self) {\nprintln!(\"\u9876\u70b9\u5217\u8868 = {:?}\", self.vertices);\nprintln!(\"\u90bb\u63a5\u77e9\u9635 =\");\nprintln!(\"[\");\nfor row in &self.adj_mat {\nprintln!(\"  {:?},\", row);\n}\nprintln!(\"]\")\n}\n}\n
graph_adjacency_matrix.c
/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\u7ed3\u6784 */\nstruct graphAdjMat {\nint *vertices;         // \u9876\u70b9\u5217\u8868\nunsigned int **adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u8fb9\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\nunsigned int size;     // \u9876\u70b9\u6570\u91cf\nunsigned int capacity; // \u56fe\u5bb9\u91cf\n};\ntypedef struct graphAdjMat graphAdjMat;\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid addEdge(graphAdjMat *t, int i, int j) {\n// \u8d8a\u754c\u68c0\u67e5\nif (i < 0 || j < 0 || i >= t->size || j >= t->size || i == j) {\nprintf(\"Out of range in %s:%d\\n\", __FILE__, __LINE__);\nexit(1);\n}\n// \u6dfb\u52a0\u8fb9\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nt->adjMat[i][j] = 1;\nt->adjMat[j][i] = 1;\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid removeEdge(graphAdjMat *t, int i, int j) {\n// \u8d8a\u754c\u68c0\u67e5\nif (i < 0 || j < 0 || i >= t->size || j >= t->size || i == j) {\nprintf(\"Out of range in %s:%d\\n\", __FILE__, __LINE__);\nexit(1);\n}\n// \u5220\u9664\u8fb9\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nt->adjMat[i][j] = 0;\nt->adjMat[j][i] = 0;\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(graphAdjMat *t, int val) {\n// \u5982\u679c\u5b9e\u9645\u4f7f\u7528\u4e0d\u5927\u4e8e\u9884\u8bbe\u7a7a\u95f4\uff0c\u5219\u76f4\u63a5\u521d\u59cb\u5316\u65b0\u7a7a\u95f4\nif (t->size < t->capacity) {\nt->vertices[t->size] = val; // \u521d\u59cb\u5316\u65b0\u9876\u70b9\u503c\nfor (int i = 0; i < t->size; i++) {\nt->adjMat[i][t->size] = 0; // \u90bb\u63a5\u77e9\u65b0\u5217\u9635\u7f6e0\n}\nmemset(t->adjMat[t->size], 0, sizeof(unsigned int) * (t->size + 1)); // \u5c06\u65b0\u589e\u884c\u7f6e 0\nt->size++;\nreturn;\n}\n// \u6269\u5bb9\uff0c\u7533\u8bf7\u65b0\u7684\u9876\u70b9\u6570\u7ec4\nint *temp = (int *)malloc(sizeof(int) * (t->size * 2));\nmemcpy(temp, t->vertices, sizeof(int) * t->size);\ntemp[t->size] = val;\n// \u91ca\u653e\u539f\u6570\u7ec4\nfree(t->vertices);\nt->vertices = temp;\n// \u6269\u5bb9\uff0c\u7533\u8bf7\u65b0\u7684\u4e8c\u7ef4\u6570\u7ec4\nunsigned int **tempMat = (unsigned int **)malloc(sizeof(unsigned int *) * t->size * 2);\nunsigned int *tempMatLine = (unsigned int *)malloc(sizeof(unsigned int) * (t->size * 2) * (t->size * 2));\nmemset(tempMatLine, 0, sizeof(unsigned int) * (t->size * 2) * (t->size * 2));\nfor (int k = 0; k < t->size * 2; k++) {\ntempMat[k] = tempMatLine + k * (t->size * 2);\n}\nfor (int i = 0; i < t->size; i++) {\nmemcpy(tempMat[i], t->adjMat[i], sizeof(unsigned int) * t->size); // \u539f\u6570\u636e\u590d\u5236\u5230\u65b0\u6570\u7ec4\n}\nfor (int i = 0; i < t->size; i++) {\ntempMat[i][t->size] = 0; // \u5c06\u65b0\u589e\u5217\u7f6e 0\n}\nmemset(tempMat[t->size], 0, sizeof(unsigned int) * (t->size + 1)); // \u5c06\u65b0\u589e\u884c\u7f6e 0\n// \u91ca\u653e\u539f\u6570\u7ec4\nfree(t->adjMat[0]);\nfree(t->adjMat);\n// \u6269\u5bb9\u540e\uff0c\u6307\u5411\u65b0\u5730\u5740\nt->adjMat = tempMat; // \u6307\u5411\u65b0\u7684\u90bb\u63a5\u77e9\u9635\u5730\u5740\nt->capacity = t->size * 2;\nt->size++;\n}\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(graphAdjMat *t, unsigned int index) {\n// \u8d8a\u754c\u68c0\u67e5\nif (index < 0 || index >= t->size) {\nprintf(\"Out of range in %s:%d\\n\", __FILE__, __LINE__);\nexit(1);\n}\nfor (int i = index; i < t->size - 1; i++) {\nt->vertices[i] = t->vertices[i + 1]; // \u6e05\u9664\u5220\u9664\u7684\u9876\u70b9\uff0c\u5e76\u5c06\u5176\u540e\u6240\u6709\u9876\u70b9\u524d\u79fb\n}\nt->vertices[t->size - 1] = 0; // \u5c06\u88ab\u524d\u79fb\u7684\u6700\u540e\u4e00\u4e2a\u9876\u70b9\u7f6e 0\n// \u6e05\u9664\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7684\u5217\nfor (int i = 0; i < t->size - 1; i++) {\nif (i < index) {\nfor (int j = index; j < t->size - 1; j++) {\nt->adjMat[i][j] = t->adjMat[i][j + 1]; // \u88ab\u5220\u9664\u5217\u540e\u7684\u6240\u6709\u5217\u524d\u79fb\n}\n} else {\nmemcpy(t->adjMat[i], t->adjMat[i + 1], sizeof(unsigned int) * t->size); // \u88ab\u5220\u9664\u884c\u7684\u4e0b\u65b9\u6240\u6709\u884c\u4e0a\u79fb\nfor (int j = index; j < t->size; j++) {\nt->adjMat[i][j] = t->adjMat[i][j + 1]; // \u88ab\u5220\u9664\u5217\u540e\u7684\u6240\u6709\u5217\u524d\u79fb\n}\n}\n}\nt->size--;\n}\n/* \u6253\u5370\u9876\u70b9\u4e0e\u90bb\u63a5\u77e9\u9635 */\nvoid printGraph(graphAdjMat *t) {\nif (t->size == 0) {\nprintf(\"graph is empty\\n\");\nreturn;\n}\nprintf(\"\u9876\u70b9\u5217\u8868 = [\");\nfor (int i = 0; i < t->size; i++) {\nif (i != t->size - 1) {\nprintf(\"%d, \", t->vertices[i]);\n} else {\nprintf(\"%d\", t->vertices[i]);\n}\n}\nprintf(\"]\\n\");\nprintf(\"\u90bb\u63a5\u77e9\u9635 =\\n[\\n\");\nfor (int i = 0; i < t->size; i++) {\nprintf(\"  [\");\nfor (int j = 0; j < t->size; j++) {\nif (j != t->size - 1) {\nprintf(\"%u, \", t->adjMat[i][j]);\n} else {\nprintf(\"%u\", t->adjMat[i][j]);\n}\n}\nprintf(\"],\\n\");\n}\nprintf(\"]\\n\");\n}\n/* \u6784\u9020\u51fd\u6570 */\ngraphAdjMat *newGraphAjdMat(unsigned int numberVertices, int *vertices, unsigned int **adjMat) {\n// \u7533\u8bf7\u5185\u5b58\ngraphAdjMat *newGraph = (graphAdjMat *)malloc(sizeof(graphAdjMat));   // \u4e3a\u56fe\u5206\u914d\u5185\u5b58\nnewGraph->vertices = (int *)malloc(sizeof(int) * numberVertices * 2); // \u4e3a\u9876\u70b9\u5217\u8868\u5206\u914d\u5185\u5b58\nnewGraph->adjMat = (unsigned int **)malloc(sizeof(unsigned int *) * numberVertices * 2); // \u4e3a\u90bb\u63a5\u77e9\u9635\u5206\u914d\u4e8c\u7ef4\u5185\u5b58\nunsigned int *temp = (unsigned int *)malloc(sizeof(unsigned int) * numberVertices * 2 * numberVertices * 2); // \u4e3a\u90bb\u63a5\u77e9\u9635\u5206\u914d\u4e00\u7ef4\u5185\u5b58\nnewGraph->size = numberVertices;                // \u521d\u59cb\u5316\u9876\u70b9\u6570\u91cf\nnewGraph->capacity = numberVertices * 2;        // \u521d\u59cb\u5316\u56fe\u5bb9\u91cf\n// \u914d\u7f6e\u4e8c\u7ef4\u6570\u7ec4\nfor (int i = 0; i < numberVertices * 2; i++) {\nnewGraph->adjMat[i] = temp + i * numberVertices * 2; // \u5c06\u4e8c\u7ef4\u6307\u9488\u6307\u5411\u4e00\u7ef4\u6570\u7ec4\n}\n// \u8d4b\u503c\nmemcpy(newGraph->vertices, vertices, sizeof(int) * numberVertices);\nfor (int i = 0; i < numberVertices; i++) {\nmemcpy(newGraph->adjMat[i], adjMat[i], sizeof(unsigned int) * numberVertices); // \u5c06\u4f20\u5165\u7684\u90bb\u63a5\u77e9\u9635\u8d4b\u503c\u7ed9\u7ed3\u6784\u4f53\u5185\u90bb\u63a5\u77e9\u9635\n}\n// \u8fd4\u56de\u7ed3\u6784\u4f53\u6307\u9488\nreturn newGraph;\n}\n
graph_adjacency_matrix.zig
[class]{GraphAdjMat}-[func]{}\n
"},{"location":"chapter_graph/graph_operations/#922","title":"9.2.2 \u00a0 \u57fa\u4e8e\u90bb\u63a5\u8868\u7684\u5b9e\u73b0","text":"

\u8bbe\u65e0\u5411\u56fe\u7684\u9876\u70b9\u603b\u6570\u4e3a \\(n\\)\u3001\u8fb9\u603b\u6570\u4e3a \\(m\\) \uff0c\u5219\u53ef\u6839\u636e\u56fe 9-8 \u6240\u793a\u7684\u65b9\u6cd5\u5b9e\u73b0\u5404\u79cd\u64cd\u4f5c\u3002

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

\u56fe 9-8 \u00a0 \u90bb\u63a5\u8868\u7684\u521d\u59cb\u5316\u3001\u589e\u5220\u8fb9\u3001\u589e\u5220\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\u800c\u8fd9\u6837\u505a\u662f\u6709\u539f\u56e0\u7684\u3002

  1. \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
  2. \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
  3. \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\u987b\u6539\u52a8\u5176\u4f59\u9876\u70b9\u4e86\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig 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]]):\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, list[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):\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):\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):\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):\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):\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.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.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.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();\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.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\u51fd\u6570 */\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\ng.adjList[vet1] = DeleteSliceElms(g.adjList[vet1], vet2)\ng.adjList[vet2] = DeleteSliceElms(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 v, list := range g.adjList {\ng.adjList[v] = DeleteSliceElms(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.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.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 (const 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 (const 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.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
graph_adjacency_list.rs
/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\u578b */\npub struct GraphAdjList {\n// \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\npub adj_list: HashMap<Vertex, Vec<Vertex>>,\n}\nimpl GraphAdjList {\n/* \u6784\u9020\u65b9\u6cd5 */\npub fn new(edges: Vec<[Vertex; 2]>) -> Self {\nlet mut graph = GraphAdjList {\nadj_list: HashMap::new(),\n};\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor edge in edges {\ngraph.add_vertex(edge[0]);\ngraph.add_vertex(edge[1]);\ngraph.add_edge(edge[0], edge[1]);\n}\ngraph\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n#[allow(unused)]\npub fn size(&self) -> usize {\nself.adj_list.len()\n}\n/* \u6dfb\u52a0\u8fb9 */\npub fn add_edge(&mut self, vet1: Vertex, vet2: Vertex) {\nif !self.adj_list.contains_key(&vet1) || !self.adj_list.contains_key(&vet2) || vet1 == vet2\n{\npanic!(\"value error\");\n}\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nself.adj_list.get_mut(&vet1).unwrap().push(vet2);\nself.adj_list.get_mut(&vet2).unwrap().push(vet1);\n}\n/* \u5220\u9664\u8fb9 */\n#[allow(unused)]\npub fn remove_edge(&mut self, vet1: Vertex, vet2: Vertex) {\nif !self.adj_list.contains_key(&vet1) || !self.adj_list.contains_key(&vet2) || vet1 == vet2\n{\npanic!(\"value error\");\n}\n// \u5220\u9664\u8fb9 vet1 - vet2\nself.adj_list\n.get_mut(&vet1)\n.unwrap()\n.retain(|&vet| vet != vet2);\nself.adj_list\n.get_mut(&vet2)\n.unwrap()\n.retain(|&vet| vet != vet1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npub fn add_vertex(&mut self, vet: Vertex) {\nif self.adj_list.contains_key(&vet) {\nreturn;\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nself.adj_list.insert(vet, vec![]);\n}\n/* \u5220\u9664\u9876\u70b9 */\n#[allow(unused)]\npub fn remove_vertex(&mut self, vet: Vertex) {\nif !self.adj_list.contains_key(&vet) {\npanic!(\"value error\");\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nself.adj_list.remove(&vet);\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor list in self.adj_list.values_mut() {\nlist.retain(|&v| v != vet);\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\npub fn print(&self) {\nprintln!(\"\u90bb\u63a5\u8868 =\");\nfor (vertex, list) in &self.adj_list {\nlet list = list.iter().map(|vertex| vertex.val).collect::<Vec<i32>>();\nprintln!(\"{}: {:?},\", vertex.val, list);\n}\n}\n}\n
graph_adjacency_list.c
/* \u57fa\u4e8e\u90bb\u63a5\u94fe\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\u7ed3\u6784 */\nstruct graphAdjList {\nVertex **verticesList; // \u90bb\u63a5\u8868\nunsigned int size;     // \u9876\u70b9\u6570\u91cf\nunsigned int capacity; // \u9876\u70b9\u5bb9\u91cf\n};\ntypedef struct graphAdjList graphAdjList;\n/* \u6dfb\u52a0\u8fb9 */\nvoid addEdge(graphAdjList *t, int i, int j) {\n// \u8d8a\u754c\u68c0\u67e5\nif (i < 0 || j < 0 || i == j || i >= t->size || j >= t->size) {\nprintf(\"Out of range in %s:%d\\n\", __FILE__, __LINE__);\nreturn;\n}\n// \u67e5\u627e\u6b32\u6dfb\u52a0\u8fb9\u7684\u9876\u70b9 vet1 - vet2\nVertex *vet1 = t->verticesList[i];\nVertex *vet2 = t->verticesList[j];\n// \u8fde\u63a5\u9876\u70b9 vet1 - vet2\npushBack(vet1->linked, vet2);\npushBack(vet2->linked, vet1);\n}\n/* \u5220\u9664\u8fb9 */\nvoid removeEdge(graphAdjList *t, int i, int j) {\n// \u8d8a\u754c\u68c0\u67e5\nif (i < 0 || j < 0 || i == j || i >= t->size || j >= t->size) {\nprintf(\"Out of range in %s:%d\\n\", __FILE__, __LINE__);\nreturn;\n}\n// \u67e5\u627e\u6b32\u5220\u9664\u8fb9\u7684\u9876\u70b9 vet1 - vet2\nVertex *vet1 = t->verticesList[i];\nVertex *vet2 = t->verticesList[j];\n// \u79fb\u9664\u5f85\u5220\u9664\u8fb9 vet1 - vet2\nremoveLink(vet1->linked, vet2);\nremoveLink(vet2->linked, vet1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(graphAdjList *t, int val) {\n// \u82e5\u5927\u5c0f\u8d85\u8fc7\u5bb9\u91cf\uff0c\u5219\u6269\u5bb9\nif (t->size >= t->capacity) {\nVertex **tempList = (Vertex **)malloc(sizeof(Vertex *) * 2 * t->capacity);\nmemcpy(tempList, t->verticesList, sizeof(Vertex *) * t->size);\nfree(t->verticesList);         // \u91ca\u653e\u539f\u90bb\u63a5\u8868\u5185\u5b58\nt->verticesList = tempList;    // \u6307\u5411\u65b0\u90bb\u63a5\u8868\nt->capacity = t->capacity * 2; // \u5bb9\u91cf\u6269\u5927\u81f32\u500d\n}\n// \u7533\u8bf7\u65b0\u9876\u70b9\u5185\u5b58\u5e76\u5c06\u65b0\u9876\u70b9\u5730\u5740\u5b58\u5165\u9876\u70b9\u5217\u8868\nVertex *newV = newVertex(val);    // \u5efa\u7acb\u65b0\u9876\u70b9\nnewV->pos = t->size;              // \u4e3a\u65b0\u9876\u70b9\u6807\u8bb0\u4e0b\u6807\nnewV->linked = newLinklist(newV); // \u4e3a\u65b0\u9876\u70b9\u5efa\u7acb\u94fe\u8868\nt->verticesList[t->size] = newV;  // \u5c06\u65b0\u9876\u70b9\u52a0\u5165\u90bb\u63a5\u8868\nt->size++;\n}\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(graphAdjList *t, unsigned int index) {\n// \u8d8a\u754c\u68c0\u67e5\nif (index < 0 || index >= t->size) {\nprintf(\"Out of range in %s:%d\\n\", __FILE__, __LINE__);\nexit(1);\n}\nVertex *vet = t->verticesList[index]; // \u67e5\u627e\u5f85\u5220\u8282\u70b9\nif (vet == 0) {                       // \u82e5\u4e0d\u5b58\u5728\u8be5\u8282\u70b9\uff0c\u5219\u8fd4\u56de\nprintf(\"index is:%d\\n\", index);\nprintf(\"Out of range in %s:%d\\n\", __FILE__, __LINE__);\nreturn;\n}\n// \u904d\u5386\u5f85\u5220\u9664\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5c06\u6240\u6709\u4e0e\u5f85\u5220\u9664\u7ed3\u70b9\u6709\u5173\u7684\u8fb9\u5220\u9664\nNode *temp = vet->linked->head->next;\nwhile (temp != 0) {\nremoveLink(temp->val->linked, vet); // \u5220\u9664\u4e0e\u8be5\u9876\u70b9\u6709\u5173\u7684\u8fb9\ntemp = temp->next;\n}\n// \u5c06\u9876\u70b9\u524d\u79fb\nfor (int i = index; i < t->size - 1; i++) {\nt->verticesList[i] = t->verticesList[i + 1]; // \u9876\u70b9\u524d\u79fb\nt->verticesList[i]->pos--;                   // \u6240\u6709\u524d\u79fb\u7684\u9876\u70b9\u7d22\u5f15\u503c\u51cf1\n}\nt->verticesList[t->size - 1] = 0; // \u5c06\u88ab\u5220\u9664\u9876\u70b9\u7684\u4f4d\u7f6e\u7f6e 0\nt->size--;\n// \u91ca\u653e\u5185\u5b58\nfreeVertex(vet);\n}\n/* \u6253\u5370\u9876\u70b9\u4e0e\u90bb\u63a5\u77e9\u9635 */\nvoid printGraph(graphAdjList *t) {\nprintf(\"\u90bb\u63a5\u8868  =\\n\");\nfor (int i = 0; i < t->size; i++) {\nNode *n = t->verticesList[i]->linked->head->next;\nprintf(\"%d: [\", t->verticesList[i]->val);\nwhile (n != 0) {\nif (n->next != 0) {\nprintf(\"%d, \", n->val->val);\n} else {\nprintf(\"%d\", n->val->val);\n}\nn = n->next;\n}\nprintf(\"]\\n\");\n}\n}\n/* \u6784\u9020\u51fd\u6570 */\ngraphAdjList *newGraphAdjList(unsigned int verticesCapacity) {\n// \u7533\u8bf7\u5185\u5b58\ngraphAdjList *newGraph = (graphAdjList *)malloc(sizeof(graphAdjList));\n// \u5efa\u7acb\u9876\u70b9\u8868\u5e76\u5206\u914d\u5185\u5b58\nnewGraph->verticesList = (Vertex **)malloc(sizeof(Vertex *) * verticesCapacity); // \u4e3a\u9876\u70b9\u5217\u8868\u5206\u914d\u5185\u5b58\nmemset(newGraph->verticesList, 0, sizeof(Vertex *) * verticesCapacity);          // \u9876\u70b9\u5217\u8868\u7f6e 0\nnewGraph->size = 0;                                                              // \u521d\u59cb\u5316\u9876\u70b9\u6570\u91cf\nnewGraph->capacity = verticesCapacity;                                           // \u521d\u59cb\u5316\u9876\u70b9\u5bb9\u91cf\n// \u8fd4\u56de\u56fe\u6307\u9488\nreturn newGraph;\n}\n
graph_adjacency_list.zig
[class]{GraphAdjList}-[func]{}\n
"},{"location":"chapter_graph/graph_operations/#923","title":"9.2.3 \u00a0 \u6548\u7387\u5bf9\u6bd4","text":"

\u8bbe\u56fe\u4e2d\u5171\u6709 \\(n\\) \u4e2a\u9876\u70b9\u548c \\(m\\) \u6761\u8fb9\uff0c\u8868 9-2 \u5bf9\u6bd4\u4e86\u90bb\u63a5\u77e9\u9635\u548c\u90bb\u63a5\u8868\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u6548\u7387\u3002

\u8868 9-2 \u00a0 \u90bb\u63a5\u77e9\u9635\u4e0e\u90bb\u63a5\u8868\u5bf9\u6bd4

\u90bb\u63a5\u77e9\u9635 \u90bb\u63a5\u8868\uff08\u94fe\u8868\uff09 \u90bb\u63a5\u8868\uff08\u54c8\u5e0c\u8868\uff09 \u5224\u65ad\u662f\u5426\u90bb\u63a5 \\(O(1)\\) \\(O(m)\\) \\(O(1)\\) \u6dfb\u52a0\u8fb9 \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) \u5220\u9664\u8fb9 \\(O(1)\\) \\(O(m)\\) \\(O(1)\\) \u6dfb\u52a0\u9876\u70b9 \\(O(n)\\) \\(O(1)\\) \\(O(1)\\) \u5220\u9664\u9876\u70b9 \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n)\\) \u5185\u5b58\u7a7a\u95f4\u5360\u7528 \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n + m)\\)

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

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

\u56fe\u548c\u6811\u90fd\u9700\u8981\u5e94\u7528\u641c\u7d22\u7b97\u6cd5\u6765\u5b9e\u73b0\u904d\u5386\u64cd\u4f5c\u3002\u56fe\u7684\u904d\u5386\u65b9\u5f0f\u53ef\u5206\u4e3a\u4e24\u79cd\uff1a\u300c\u5e7f\u5ea6\u4f18\u5148\u904d\u5386 breadth-first traversal\u300d\u548c\u300c\u6df1\u5ea6\u4f18\u5148\u904d\u5386 depth-first traversal\u300d\u3002\u5b83\u4eec\u4e5f\u5e38\u88ab\u79f0\u4e3a\u300c\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22 breadth-first search\u300d\u548c\u300c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22 depth-first search\u300d\uff0c\u7b80\u79f0 BFS \u548c DFS \u3002

"},{"location":"chapter_graph/graph_traversal/#931","title":"9.3.1 \u00a0 \u5e7f\u5ea6\u4f18\u5148\u904d\u5386","text":"

\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u662f\u4e00\u79cd\u7531\u8fd1\u53ca\u8fdc\u7684\u904d\u5386\u65b9\u5f0f\uff0c\u4ece\u67d0\u4e2a\u8282\u70b9\u51fa\u53d1\uff0c\u59cb\u7ec8\u4f18\u5148\u8bbf\u95ee\u8ddd\u79bb\u6700\u8fd1\u7684\u9876\u70b9\uff0c\u5e76\u4e00\u5c42\u5c42\u5411\u5916\u6269\u5f20\u3002\u5982\u56fe 9-9 \u6240\u793a\uff0c\u4ece\u5de6\u4e0a\u89d2\u9876\u70b9\u51fa\u53d1\uff0c\u5148\u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff0c\u7136\u540e\u904d\u5386\u4e0b\u4e00\u4e2a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u6240\u6709\u9876\u70b9\u8bbf\u95ee\u5b8c\u6bd5\u3002

\u56fe 9-9 \u00a0 \u56fe\u7684\u5e7f\u5ea6\u4f18\u5148\u904d\u5386

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

BFS \u901a\u5e38\u501f\u52a9\u961f\u5217\u6765\u5b9e\u73b0\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\u3002
  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\u3002
  3. \u5faa\u73af\u6b65\u9aa4 2. \uff0c\u76f4\u5230\u6240\u6709\u9876\u70b9\u88ab\u8bbf\u95ee\u5b8c\u6210\u540e\u7ed3\u675f\u3002

\u4e3a\u4e86\u9632\u6b62\u91cd\u590d\u904d\u5386\u9876\u70b9\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868 visited \u6765\u8bb0\u5f55\u54ea\u4e9b\u8282\u70b9\u5df2\u88ab\u8bbf\u95ee\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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<>();\nvisited.add(startVet);\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nQueue<Vertex> que = new LinkedList<>();\nque.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.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();\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nHashSet<Vertex> visited = new() { startVet };\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nQueue<Vertex> que = new();\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.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.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.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.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
graph_bfs.rs
/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfn graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nlet mut res = vec![];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nlet mut visited = HashSet::new();\nvisited.insert(start_vet);\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nlet mut que = VecDeque::new();\nque.push_back(start_vet);\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile !que.is_empty() {\nlet vet = que.pop_front().unwrap(); // \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\nif let Some(adj_vets) = graph.adj_list.get(&vet) {\nfor &adj_vet in adj_vets {\nif visited.contains(&adj_vet) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nque.push_back(adj_vet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.insert(adj_vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nres\n}\n
graph_bfs.c
/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\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\nVertex **graphBFS(graphAdjList *t, Vertex *startVet) {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nVertex **res = (Vertex **)malloc(sizeof(Vertex *) * t->size);\nmemset(res, 0, sizeof(Vertex *) * t->size);\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nqueue *que = newQueue(t->size);\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nhashTable *visited = newHash(t->size);\nint resIndex = 0;\nqueuePush(que, startVet);         // \u5c06\u7b2c\u4e00\u4e2a\u5143\u7d20\u5165\u961f\nhashMark(visited, startVet->pos); // \u6807\u8bb0\u7b2c\u4e00\u4e2a\u5165\u961f\u7684\u9876\u70b9\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile (que->head < que->tail) {\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u8fb9\u94fe\u8868\uff0c\u5c06\u6240\u6709\u4e0e\u8be5\u9876\u70b9\u6709\u8fde\u63a5\u7684\uff0c\u5e76\u4e14\u672a\u88ab\u6807\u8bb0\u7684\u9876\u70b9\u5165\u961f\nNode *n = queueTop(que)->linked->head->next;\nwhile (n != 0) {\n// \u67e5\u8be2\u54c8\u5e0c\u8868\uff0c\u82e5\u8be5\u7d22\u5f15\u7684\u9876\u70b9\u5df2\u5165\u961f\uff0c\u5219\u8df3\u8fc7\uff0c\u5426\u5219\u5165\u961f\u5e76\u6807\u8bb0\nif (hashQuery(visited, n->val->pos) == 1) {\nn = n->next;\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nqueuePush(que, n->val);         // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nhashMark(visited, n->val->pos); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n// \u961f\u9996\u5143\u7d20\u5b58\u5165\u6570\u7ec4\nres[resIndex] = queueTop(que); // \u961f\u9996\u9876\u70b9\u52a0\u5165\u9876\u70b9\u904d\u5386\u5e8f\u5217\nresIndex++;\nqueuePop(que); // \u961f\u9996\u5143\u7d20\u51fa\u961f\n}\n// \u91ca\u653e\u5185\u5b58\nfreeQueue(que);\nfreeHash(visited);\nresIndex = 0;\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res;\n}\n
graph_bfs.zig
[class]{}-[func]{graphBFS}\n

\u4ee3\u7801\u76f8\u5bf9\u62bd\u8c61\uff0c\u5efa\u8bae\u5bf9\u7167\u56fe 9-10 \u6765\u52a0\u6df1\u7406\u89e3\u3002

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

\u56fe 9-10 \u00a0 \u56fe\u7684\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u6b65\u9aa4

\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u7684\u5e8f\u5217\u662f\u5426\u552f\u4e00\uff1f

\u4e0d\u552f\u4e00\u3002\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u53ea\u8981\u6c42\u6309\u201c\u7531\u8fd1\u53ca\u8fdc\u201d\u7684\u987a\u5e8f\u904d\u5386\uff0c\u800c\u591a\u4e2a\u76f8\u540c\u8ddd\u79bb\u7684\u9876\u70b9\u7684\u904d\u5386\u987a\u5e8f\u662f\u5141\u8bb8\u88ab\u4efb\u610f\u6253\u4e71\u7684\u3002\u4ee5\u56fe 9-10 \u4e3a\u4f8b\uff0c\u9876\u70b9 \\(1\\)\u3001\\(3\\) \u7684\u8bbf\u95ee\u987a\u5e8f\u53ef\u4ee5\u4ea4\u6362\u3001\u9876\u70b9 \\(2\\)\u3001\\(4\\)\u3001\\(6\\) \u7684\u8bbf\u95ee\u987a\u5e8f\u4e5f\u53ef\u4ee5\u4efb\u610f\u4ea4\u6362\u3002

"},{"location":"chapter_graph/graph_traversal/#2","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"

\u65f6\u95f4\u590d\u6742\u5ea6\uff1a \u6240\u6709\u9876\u70b9\u90fd\u4f1a\u5165\u961f\u5e76\u51fa\u961f\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(|V|)\\) \u65f6\u95f4\uff1b\u5728\u904d\u5386\u90bb\u63a5\u9876\u70b9\u7684\u8fc7\u7a0b\u4e2d\uff0c\u7531\u4e8e\u662f\u65e0\u5411\u56fe\uff0c\u56e0\u6b64\u6240\u6709\u8fb9\u90fd\u4f1a\u88ab\u8bbf\u95ee \\(2\\) \u6b21\uff0c\u4f7f\u7528 \\(O(2|E|)\\) \u65f6\u95f4\uff1b\u603b\u4f53\u4f7f\u7528 \\(O(|V| + |E|)\\) \u65f6\u95f4\u3002

\u7a7a\u95f4\u590d\u6742\u5ea6\uff1a \u5217\u8868 res \uff0c\u54c8\u5e0c\u8868 visited \uff0c\u961f\u5217 que \u4e2d\u7684\u9876\u70b9\u6570\u91cf\u6700\u591a\u4e3a \\(|V|\\) \uff0c\u4f7f\u7528 \\(O(|V|)\\) \u7a7a\u95f4\u3002

"},{"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\u5982\u56fe 9-11 \u6240\u793a\uff0c\u4ece\u5de6\u4e0a\u89d2\u9876\u70b9\u51fa\u53d1\uff0c\u8bbf\u95ee\u5f53\u524d\u9876\u70b9\u7684\u67d0\u4e2a\u90bb\u63a5\u9876\u70b9\uff0c\u76f4\u5230\u8d70\u5230\u5c3d\u5934\u65f6\u8fd4\u56de\uff0c\u518d\u7ee7\u7eed\u8d70\u5230\u5c3d\u5934\u5e76\u8fd4\u56de\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u6240\u6709\u9876\u70b9\u904d\u5386\u5b8c\u6210\u3002

\u56fe 9-11 \u00a0 \u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u904d\u5386

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

\u8fd9\u79cd\u201c\u8d70\u5230\u5c3d\u5934\u518d\u8fd4\u56de\u201d\u7684\u7b97\u6cd5\u8303\u5f0f\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u6765\u5b9e\u73b0\u3002\u4e0e\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u7c7b\u4f3c\uff0c\u5728\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u4e2d\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

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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# \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]()\ndfs(graph, visited, res, start_vet)\nreturn res\n
graph_dfs.cpp
/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nvoid dfs(GraphAdjList &graph, unordered_set<Vertex *> &visited, vector<Vertex *> &res, Vertex *vet) {\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.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.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();\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nHashSet<Vertex> visited = new();\nDFS(graph, visited, res, startVet);\nreturn res;\n}\n
graph_dfs.go
/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nfunc dfs(g *graphAdjList, visited map[Vertex]struct{}, res *[]Vertex, vet Vertex) {\n// append \u64cd\u4f5c\u4f1a\u8fd4\u56de\u65b0\u7684\u7684\u5f15\u7528\uff0c\u5fc5\u987b\u8ba9\u539f\u5f15\u7528\u91cd\u65b0\u8d4b\u503c\u4e3a\u65b0slice\u7684\u5f15\u7528\n*res = append(*res, vet)\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.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.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.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
graph_dfs.rs
/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nfn dfs(graph: &GraphAdjList, visited: &mut HashSet<Vertex>, res: &mut Vec<Vertex>, vet: Vertex) {\nres.push(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\nif let Some(adj_vets) = graph.adj_list.get(&vet) {\nfor &adj_vet in adj_vets {\nif visited.contains(&adj_vet) {\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, adj_vet);\n}\n}\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfn graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nlet mut res = vec![];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nlet mut visited = HashSet::new();\ndfs(&graph, &mut visited, &mut res, start_vet);\nres\n}\n
graph_dfs.c
/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nint resIndex = 0;\nvoid dfs(graphAdjList *graph, hashTable *visited, Vertex *vet, Vertex **res) {\nif (hashQuery(visited, vet->pos) == 1) {\nreturn; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nhashMark(visited, vet->pos); // \u6807\u8bb0\u9876\u70b9\u5e76\u5c06\u9876\u70b9\u5b58\u5165\u6570\u7ec4\nres[resIndex] = vet;         // \u5c06\u9876\u70b9\u5b58\u5165\u6570\u7ec4\nresIndex++;\n// \u904d\u5386\u8be5\u9876\u70b9\u94fe\u8868\nNode *n = vet->linked->head->next;\nwhile (n != 0) {\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, n->val, res);\nn = n->next;\n}\nreturn;\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\nVertex **graphDFS(graphAdjList *graph, Vertex *startVet) {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nVertex **res = (Vertex **)malloc(sizeof(Vertex *) * graph->size);\nmemset(res, 0, sizeof(Vertex *) * graph->size);\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nhashTable *visited = newHash(graph->size);\ndfs(graph, visited, startVet, res);\n// \u91ca\u653e\u54c8\u5e0c\u8868\u5185\u5b58\u5e76\u5c06\u6570\u7ec4\u7d22\u5f15\u5f52\u96f6\nfreeHash(visited);\nresIndex = 0;\n// \u8fd4\u56de\u904d\u5386\u6570\u7ec4\nreturn res;\n}\n
graph_dfs.zig
[class]{}-[func]{dfs}\n[class]{}-[func]{graphDFS}\n

\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u7b97\u6cd5\u6d41\u7a0b\u5982\u56fe 9-12 \u6240\u793a\u3002

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

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

\u56fe 9-12 \u00a0 \u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u6b65\u9aa4

\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u5e8f\u5217\u662f\u5426\u552f\u4e00\uff1f

\u4e0e\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u7c7b\u4f3c\uff0c\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u5e8f\u5217\u7684\u987a\u5e8f\u4e5f\u4e0d\u662f\u552f\u4e00\u7684\u3002\u7ed9\u5b9a\u67d0\u9876\u70b9\uff0c\u5148\u5f80\u54ea\u4e2a\u65b9\u5411\u63a2\u7d22\u90fd\u53ef\u4ee5\uff0c\u5373\u90bb\u63a5\u9876\u70b9\u7684\u987a\u5e8f\u53ef\u4ee5\u4efb\u610f\u6253\u4e71\uff0c\u90fd\u662f\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u3002

\u4ee5\u6811\u7684\u904d\u5386\u4e3a\u4f8b\uff0c\u201c\u6839 \\(\\rightarrow\\) \u5de6 \\(\\rightarrow\\) \u53f3\u201d\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/#2_1","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"

\u65f6\u95f4\u590d\u6742\u5ea6\uff1a \u6240\u6709\u9876\u70b9\u90fd\u4f1a\u88ab\u8bbf\u95ee \\(1\\) \u6b21\uff0c\u4f7f\u7528 \\(O(|V|)\\) \u65f6\u95f4\uff1b\u6240\u6709\u8fb9\u90fd\u4f1a\u88ab\u8bbf\u95ee \\(2\\) \u6b21\uff0c\u4f7f\u7528 \\(O(2|E|)\\) \u65f6\u95f4\uff1b\u603b\u4f53\u4f7f\u7528 \\(O(|V| + |E|)\\) \u65f6\u95f4\u3002

\u7a7a\u95f4\u590d\u6742\u5ea6\uff1a \u5217\u8868 res \uff0c\u54c8\u5e0c\u8868 visited \u9876\u70b9\u6570\u91cf\u6700\u591a\u4e3a \\(|V|\\) \uff0c\u9012\u5f52\u6df1\u5ea6\u6700\u5927\u4e3a \\(|V|\\) \uff0c\u56e0\u6b64\u4f7f\u7528 \\(O(|V|)\\) \u7a7a\u95f4\u3002

"},{"location":"chapter_graph/summary/","title":"9.4 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_graph/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","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/#2-q-a","title":"2. \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_greedy/","title":"\u7b2c 15 \u7ae0 \u00a0 \u8d2a\u5fc3","text":"

Abstract

\u5411\u65e5\u8475\u671d\u7740\u592a\u9633\u8f6c\u52a8\uff0c\u65f6\u523b\u90fd\u5728\u8ffd\u6c42\u81ea\u8eab\u6210\u957f\u7684\u6700\u5927\u53ef\u80fd\u3002

\u8d2a\u5fc3\u7b56\u7565\u5728\u4e00\u8f6e\u8f6e\u7684\u7b80\u5355\u9009\u62e9\u4e2d\uff0c\u9010\u6b65\u5bfc\u5411\u6700\u4f73\u7684\u7b54\u6848\u3002

"},{"location":"chapter_greedy/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 15.1 \u00a0 \u8d2a\u5fc3\u7b97\u6cd5
  • 15.2 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898
  • 15.3 \u00a0 \u6700\u5927\u5bb9\u91cf\u95ee\u9898
  • 15.4 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u79ef\u95ee\u9898
  • 15.5 \u00a0 \u5c0f\u7ed3
"},{"location":"chapter_greedy/fractional_knapsack_problem/","title":"15.2 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898","text":"

Question

\u7ed9\u5b9a \\(n\\) \u4e2a\u7269\u54c1\uff0c\u7b2c \\(i\\) \u4e2a\u7269\u54c1\u7684\u91cd\u91cf\u4e3a \\(wgt[i-1]\\)\u3001\u4ef7\u503c\u4e3a \\(val[i-1]\\) \uff0c\u548c\u4e00\u4e2a\u5bb9\u91cf\u4e3a \\(cap\\) \u7684\u80cc\u5305\u3002\u6bcf\u4e2a\u7269\u54c1\u53ea\u80fd\u9009\u62e9\u4e00\u6b21\uff0c\u4f46\u53ef\u4ee5\u9009\u62e9\u7269\u54c1\u7684\u4e00\u90e8\u5206\uff0c\u4ef7\u503c\u6839\u636e\u9009\u62e9\u7684\u91cd\u91cf\u6bd4\u4f8b\u8ba1\u7b97\uff0c\u95ee\u5728\u4e0d\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\u4e0b\u80cc\u5305\u4e2d\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u3002

\u56fe 15-3 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e

\u5206\u6570\u80cc\u5305\u548c 0-1 \u80cc\u5305\u6574\u4f53\u4e0a\u975e\u5e38\u76f8\u4f3c\uff0c\u72b6\u6001\u5305\u542b\u5f53\u524d\u7269\u54c1 \\(i\\) \u548c\u5bb9\u91cf \\(c\\) \uff0c\u76ee\u6807\u662f\u6c42\u4e0d\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\u4e0b\u7684\u6700\u5927\u4ef7\u503c\u3002

\u4e0d\u540c\u70b9\u5728\u4e8e\uff0c\u672c\u9898\u5141\u8bb8\u53ea\u9009\u62e9\u7269\u54c1\u7684\u4e00\u90e8\u5206\u3002\u5982\u56fe 15-4 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u7269\u54c1\u4efb\u610f\u5730\u8fdb\u884c\u5207\u5206\uff0c\u5e76\u6309\u7167\u91cd\u91cf\u6bd4\u4f8b\u6765\u8ba1\u7b97\u7269\u54c1\u4ef7\u503c\u3002

  1. \u5bf9\u4e8e\u7269\u54c1 \\(i\\) \uff0c\u5b83\u5728\u5355\u4f4d\u91cd\u91cf\u4e0b\u7684\u4ef7\u503c\u4e3a \\(val[i-1] / wgt[i-1]\\) \uff0c\u7b80\u79f0\u4e3a\u5355\u4f4d\u4ef7\u503c\u3002
  2. \u5047\u8bbe\u653e\u5165\u4e00\u90e8\u5206\u7269\u54c1 \\(i\\) \uff0c\u91cd\u91cf\u4e3a \\(w\\) \uff0c\u5219\u80cc\u5305\u589e\u52a0\u7684\u4ef7\u503c\u4e3a \\(w \\times val[i-1] / wgt[i-1]\\) \u3002

\u56fe 15-4 \u00a0 \u7269\u54c1\u5728\u5355\u4f4d\u91cd\u91cf\u4e0b\u7684\u4ef7\u503c

"},{"location":"chapter_greedy/fractional_knapsack_problem/#1","title":"1. \u00a0 \u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a","text":"

\u6700\u5927\u5316\u80cc\u5305\u5185\u7269\u54c1\u603b\u4ef7\u503c\uff0c\u672c\u8d28\u4e0a\u662f\u8981\u6700\u5927\u5316\u5355\u4f4d\u91cd\u91cf\u4e0b\u7684\u7269\u54c1\u4ef7\u503c\u3002\u7531\u6b64\u4fbf\u53ef\u63a8\u51fa\u56fe 15-5 \u6240\u793a\u7684\u8d2a\u5fc3\u7b56\u7565\u3002

  1. \u5c06\u7269\u54c1\u6309\u7167\u5355\u4f4d\u4ef7\u503c\u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\u3002
  2. \u904d\u5386\u6240\u6709\u7269\u54c1\uff0c\u6bcf\u8f6e\u8d2a\u5fc3\u5730\u9009\u62e9\u5355\u4f4d\u4ef7\u503c\u6700\u9ad8\u7684\u7269\u54c1\u3002
  3. \u82e5\u5269\u4f59\u80cc\u5305\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u4f7f\u7528\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u586b\u6ee1\u80cc\u5305\u5373\u53ef\u3002

\u56fe 15-5 \u00a0 \u5206\u6570\u80cc\u5305\u7684\u8d2a\u5fc3\u7b56\u7565

"},{"location":"chapter_greedy/fractional_knapsack_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u6211\u4eec\u5efa\u7acb\u4e86\u4e00\u4e2a\u7269\u54c1\u7c7b Item \uff0c\u4ee5\u4fbf\u5c06\u7269\u54c1\u6309\u7167\u5355\u4f4d\u4ef7\u503c\u8fdb\u884c\u6392\u5e8f\u3002\u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u5f53\u80cc\u5305\u5df2\u6ee1\u65f6\u8df3\u51fa\u5e76\u8fd4\u56de\u89e3\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig fractional_knapsack.py
class Item:\n\"\"\"\u7269\u54c1\"\"\"\ndef __init__(self, w: int, v: int):\nself.w = w  # \u7269\u54c1\u91cd\u91cf\nself.v = v  # \u7269\u54c1\u4ef7\u503c\ndef fractional_knapsack(wgt: list[int], val: list[int], cap: int) -> int:\n\"\"\"\u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3\"\"\"\n# \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nitems = [Item(w, v) for w, v in zip(wgt, val)]\n# \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nitems.sort(key=lambda item: item.v / item.w, reverse=True)\n# \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\nres = 0\nfor item in items:\nif item.w <= cap:\n# \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += item.v\ncap -= item.w\nelse:\n# \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += (item.v / item.w) * cap\n# \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak\nreturn res\n
fractional_knapsack.cpp
/* \u7269\u54c1 */\nclass Item {\npublic:\nint w; // \u7269\u54c1\u91cd\u91cf\nint v; // \u7269\u54c1\u4ef7\u503c\nItem(int w, int v) : w(w), v(v) {\n}\n};\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble fractionalKnapsack(vector<int> &wgt, vector<int> &val, int cap) {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nvector<Item> items;\nfor (int i = 0; i < wgt.size(); i++) {\nitems.push_back(Item(wgt[i], val[i]));\n}\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nsort(items.begin(), items.end(), [](Item &a, Item &b) { return (double)a.v / a.w > (double)b.v / b.w; });\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\ndouble res = 0;\nfor (auto &item : items) {\nif (item.w <= cap) {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += item.v;\ncap -= item.w;\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += (double)item.v / item.w * cap;\n// \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak;\n}\n}\nreturn res;\n}\n
fractional_knapsack.java
/* \u7269\u54c1 */\nclass Item {\nint w; // \u7269\u54c1\u91cd\u91cf\nint v; // \u7269\u54c1\u4ef7\u503c\npublic Item(int w, int v) {\nthis.w = w;\nthis.v = v;\n}\n}\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble fractionalKnapsack(int[] wgt, int[] val, int cap) {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nItem[] items = new Item[wgt.length];\nfor (int i = 0; i < wgt.length; i++) {\nitems[i] = new Item(wgt[i], val[i]);\n}\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nArrays.sort(items, Comparator.comparingDouble(item -> -((double) item.v / item.w)));\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\ndouble res = 0;\nfor (Item item : items) {\nif (item.w <= cap) {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += item.v;\ncap -= item.w;\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += (double) item.v / item.w * cap;\n// \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak;\n}\n}\nreturn res;\n}\n
fractional_knapsack.cs
/* \u7269\u54c1 */\nclass Item {\npublic int w; // \u7269\u54c1\u91cd\u91cf\npublic int v; // \u7269\u54c1\u4ef7\u503c\npublic Item(int w, int v) {\nthis.w = w;\nthis.v = v;\n}\n}\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble FractionalKnapsack(int[] wgt, int[] val, int cap) {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nItem[] items = new Item[wgt.Length];\nfor (int i = 0; i < wgt.Length; i++) {\nitems[i] = new Item(wgt[i], val[i]);\n}\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nArray.Sort(items, (x, y) => (y.v / y.w).CompareTo(x.v / x.w));\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\ndouble res = 0;\nforeach (Item item in items) {\nif (item.w <= cap) {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += item.v;\ncap -= item.w;\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += (double)item.v / item.w * cap;\n// \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak;\n}\n}\nreturn res;\n}\n
fractional_knapsack.go
/* \u7269\u54c1 */\ntype Item struct {\nw int // \u7269\u54c1\u91cd\u91cf\nv int // \u7269\u54c1\u4ef7\u503c\n}\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfunc fractionalKnapsack(wgt []int, val []int, cap int) float64 {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nitems := make([]Item, len(wgt))\nfor i := 0; i < len(wgt); i++ {\nitems[i] = Item{wgt[i], val[i]}\n}\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nsort.Slice(items, func(i, j int) bool {\nreturn float64(items[i].v)/float64(items[i].w) > float64(items[j].v)/float64(items[j].w)\n})\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\nres := 0.0\nfor _, item := range items {\nif item.w <= cap {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += float64(item.v)\ncap -= item.w\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += float64(item.v) / float64(item.w) * float64(cap)\n// \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak\n}\n}\nreturn res\n}\n
fractional_knapsack.swift
/* \u7269\u54c1 */\nclass Item {\nvar w: Int // \u7269\u54c1\u91cd\u91cf\nvar v: Int // \u7269\u54c1\u4ef7\u503c\ninit(w: Int, v: Int) {\nself.w = w\nself.v = v\n}\n}\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfunc fractionalKnapsack(wgt: [Int], val: [Int], cap: Int) -> Double {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nvar items = zip(wgt, val).map { Item(w: $0, v: $1) }\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nitems.sort(by: { -(Double($0.v) / Double($0.w)) < -(Double($1.v) / Double($1.w)) })\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\nvar res = 0.0\nvar cap = cap\nfor item in items {\nif item.w <= cap {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += Double(item.v)\ncap -= item.w\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += Double(item.v) / Double(item.w) * Double(cap)\n// \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak\n}\n}\nreturn res\n}\n
fractional_knapsack.js
/* \u7269\u54c1 */\nclass Item {\nconstructor(w, v) {\nthis.w = w; // \u7269\u54c1\u91cd\u91cf\nthis.v = v; // \u7269\u54c1\u4ef7\u503c\n}\n}\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfunction fractionalKnapsack(wgt, val, cap) {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nconst items = wgt.map((w, i) => new Item(w, val[i]));\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nitems.sort((a, b) => b.v / b.w - a.v / a.w);\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\nlet res = 0;\nfor (const item of items) {\nif (item.w <= cap) {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += item.v;\ncap -= item.w;\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += (item.v / item.w) * cap;\n// \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak;\n}\n}\nreturn res;\n}\n
fractional_knapsack.ts
/* \u7269\u54c1 */\nclass Item {\nw: number; // \u7269\u54c1\u91cd\u91cf\nv: number; // \u7269\u54c1\u4ef7\u503c\nconstructor(w: number, v: number) {\nthis.w = w;\nthis.v = v;\n}\n}\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfunction fractionalKnapsack(wgt: number[], val: number[], cap: number): number {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nconst items: Item[] = wgt.map((w, i) => new Item(w, val[i]));\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nitems.sort((a, b) => b.v / b.w - a.v / a.w);\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\nlet res = 0;\nfor (const item of items) {\nif (item.w <= cap) {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += item.v;\ncap -= item.w;\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += (item.v / item.w) * cap;\n// \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak;\n}\n}\nreturn res;\n}\n
fractional_knapsack.dart
/* \u7269\u54c1 */\nclass Item {\nint w; // \u7269\u54c1\u91cd\u91cf\nint v; // \u7269\u54c1\u4ef7\u503c\nItem(this.w, this.v);\n}\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble fractionalKnapsack(List<int> wgt, List<int> val, int cap) {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nList<Item> items = List.generate(wgt.length, (i) => Item(wgt[i], val[i]));\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nitems.sort((a, b) => (b.v / b.w).compareTo(a.v / a.w));\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\ndouble res = 0;\nfor (Item item in items) {\nif (item.w <= cap) {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += item.v;\ncap -= item.w;\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += item.v / item.w * cap;\n// \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak;\n}\n}\nreturn res;\n}\n
fractional_knapsack.rs
/* \u7269\u54c1 */\nstruct Item {\nw: i32, // \u7269\u54c1\u91cd\u91cf\nv: i32, // \u7269\u54c1\u4ef7\u503c\n}\nimpl Item {\nfn new(w: i32, v: i32) -> Self {\nSelf { w, v }\n}\n}\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfn fractional_knapsack(wgt: &[i32], val: &[i32], mut cap: i32) -> f64 {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nlet mut items = wgt\n.iter()\n.zip(val.iter())\n.map(|(&w, &v)| Item::new(w, v))\n.collect::<Vec<Item>>();\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nitems.sort_by(|a, b| {\n(b.v as f64 / b.w as f64)\n.partial_cmp(&(a.v as f64 / a.w as f64))\n.unwrap()\n});\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\nlet mut res = 0.0;\nfor item in &items {\nif item.w <= cap {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += item.v as f64;\ncap -= item.w;\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += item.v as f64 / item.w as f64 * cap as f64;\n// \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak;\n}\n}\nres\n}\n
fractional_knapsack.c
/* \u7269\u54c1 */\nstruct Item {\nint w; // \u7269\u54c1\u91cd\u91cf\nint v; // \u7269\u54c1\u4ef7\u503c\n};\ntypedef struct Item Item;\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfloat fractionalKnapsack(int wgt[], int val[], int itemCount, int cap) {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nItem *items = malloc(sizeof(Item) * itemCount);\nfor (int i = 0; i < itemCount; i++) {\nitems[i] = (Item){.w = wgt[i], .v = val[i]};\n}\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nqsort(items, (size_t)itemCount, sizeof(Item), sortByValueDensity);\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\nfloat res = 0.0;\nfor (int i = 0; i < itemCount; i++) {\nif (items[i].w <= cap) {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += items[i].v;\ncap -= items[i].w;\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += (float)cap / items[i].w * items[i].v;\ncap = 0;\nbreak;\n}\n}\nfree(items);\nreturn res;\n}\n
fractional_knapsack.zig
[class]{Item}-[func]{}\n[class]{}-[func]{fractionalKnapsack}\n

\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u904d\u5386\u6574\u4e2a\u7269\u54c1\u5217\u8868\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u7269\u54c1\u6570\u91cf\u3002

\u7531\u4e8e\u521d\u59cb\u5316\u4e86\u4e00\u4e2a Item \u5bf9\u8c61\u5217\u8868\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

"},{"location":"chapter_greedy/fractional_knapsack_problem/#3","title":"3. \u00a0 \u6b63\u786e\u6027\u8bc1\u660e","text":"

\u91c7\u7528\u53cd\u8bc1\u6cd5\u3002\u5047\u8bbe\u7269\u54c1 \\(x\\) \u662f\u5355\u4f4d\u4ef7\u503c\u6700\u9ad8\u7684\u7269\u54c1\uff0c\u4f7f\u7528\u67d0\u7b97\u6cd5\u6c42\u5f97\u6700\u5927\u4ef7\u503c\u4e3a res \uff0c\u4f46\u8be5\u89e3\u4e2d\u4e0d\u5305\u542b\u7269\u54c1 \\(x\\) \u3002

\u73b0\u5728\u4ece\u80cc\u5305\u4e2d\u62ff\u51fa\u5355\u4f4d\u91cd\u91cf\u7684\u4efb\u610f\u7269\u54c1\uff0c\u5e76\u66ff\u6362\u4e3a\u5355\u4f4d\u91cd\u91cf\u7684\u7269\u54c1 \\(x\\) \u3002\u7531\u4e8e\u7269\u54c1 \\(x\\) \u7684\u5355\u4f4d\u4ef7\u503c\u6700\u9ad8\uff0c\u56e0\u6b64\u66ff\u6362\u540e\u7684\u603b\u4ef7\u503c\u4e00\u5b9a\u5927\u4e8e res \u3002\u8fd9\u4e0e res \u662f\u6700\u4f18\u89e3\u77db\u76fe\uff0c\u8bf4\u660e\u6700\u4f18\u89e3\u4e2d\u5fc5\u987b\u5305\u542b\u7269\u54c1 \\(x\\) \u3002

\u5bf9\u4e8e\u8be5\u89e3\u4e2d\u7684\u5176\u4ed6\u7269\u54c1\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u6784\u5efa\u51fa\u4e0a\u8ff0\u77db\u76fe\u3002\u603b\u800c\u8a00\u4e4b\uff0c\u5355\u4f4d\u4ef7\u503c\u66f4\u5927\u7684\u7269\u54c1\u603b\u662f\u66f4\u4f18\u9009\u62e9\uff0c\u8fd9\u8bf4\u660e\u8d2a\u5fc3\u7b56\u7565\u662f\u6709\u6548\u7684\u3002

\u5982\u56fe 15-6 \u6240\u793a\uff0c\u5982\u679c\u5c06\u7269\u54c1\u91cd\u91cf\u548c\u7269\u54c1\u5355\u4f4d\u4ef7\u503c\u5206\u522b\u770b\u4f5c\u4e00\u4e2a 2D \u56fe\u8868\u7684\u6a2a\u8f74\u548c\u7eb5\u8f74\uff0c\u5219\u5206\u6570\u80cc\u5305\u95ee\u9898\u53ef\u88ab\u8f6c\u5316\u4e3a\u201c\u6c42\u5728\u6709\u9650\u6a2a\u8f74\u533a\u95f4\u4e0b\u7684\u6700\u5927\u56f4\u6210\u9762\u79ef\u201d\u3002\u8fd9\u4e2a\u7c7b\u6bd4\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4ece\u51e0\u4f55\u89d2\u5ea6\u7406\u89e3\u8d2a\u5fc3\u7b56\u7565\u7684\u6709\u6548\u6027\u3002

\u56fe 15-6 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898\u7684\u51e0\u4f55\u8868\u793a

"},{"location":"chapter_greedy/greedy_algorithm/","title":"15.1 \u00a0 \u8d2a\u5fc3\u7b97\u6cd5","text":"

\u300c\u8d2a\u5fc3\u7b97\u6cd5 greedy algorithm\u300d\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u89e3\u51b3\u4f18\u5316\u95ee\u9898\u7684\u7b97\u6cd5\uff0c\u5176\u57fa\u672c\u601d\u60f3\u662f\u5728\u95ee\u9898\u7684\u6bcf\u4e2a\u51b3\u7b56\u9636\u6bb5\uff0c\u90fd\u9009\u62e9\u5f53\u524d\u770b\u8d77\u6765\u6700\u4f18\u7684\u9009\u62e9\uff0c\u5373\u8d2a\u5fc3\u5730\u505a\u51fa\u5c40\u90e8\u6700\u4f18\u7684\u51b3\u7b56\uff0c\u4ee5\u671f\u671b\u83b7\u5f97\u5168\u5c40\u6700\u4f18\u89e3\u3002\u8d2a\u5fc3\u7b97\u6cd5\u7b80\u6d01\u4e14\u9ad8\u6548\uff0c\u5728\u8bb8\u591a\u5b9e\u9645\u95ee\u9898\u4e2d\u90fd\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002

\u8d2a\u5fc3\u7b97\u6cd5\u548c\u52a8\u6001\u89c4\u5212\u90fd\u5e38\u7528\u4e8e\u89e3\u51b3\u4f18\u5316\u95ee\u9898\u3002\u5b83\u4eec\u4e4b\u95f4\u5b58\u5728\u4e00\u4e9b\u76f8\u4f3c\u4e4b\u5904\uff0c\u6bd4\u5982\u90fd\u4f9d\u8d56\u6700\u4f18\u5b50\u7ed3\u6784\u6027\u8d28\uff0c\u4f46\u5de5\u4f5c\u539f\u7406\u662f\u4e0d\u540c\u7684\u3002

  • \u52a8\u6001\u89c4\u5212\u4f1a\u6839\u636e\u4e4b\u524d\u9636\u6bb5\u7684\u6240\u6709\u51b3\u7b56\u6765\u8003\u8651\u5f53\u524d\u51b3\u7b56\uff0c\u5e76\u4f7f\u7528\u8fc7\u53bb\u5b50\u95ee\u9898\u7684\u89e3\u6765\u6784\u5efa\u5f53\u524d\u5b50\u95ee\u9898\u7684\u89e3\u3002
  • \u8d2a\u5fc3\u7b97\u6cd5\u4e0d\u4f1a\u91cd\u65b0\u8003\u8651\u8fc7\u53bb\u7684\u51b3\u7b56\uff0c\u800c\u662f\u4e00\u8def\u5411\u524d\u5730\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u4e0d\u65ad\u7f29\u5c0f\u95ee\u9898\u8303\u56f4\uff0c\u76f4\u81f3\u95ee\u9898\u88ab\u89e3\u51b3\u3002

\u6211\u4eec\u5148\u901a\u8fc7\u4f8b\u9898\u201c\u96f6\u94b1\u5151\u6362\u201d\u4e86\u89e3\u8d2a\u5fc3\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u3002\u8fd9\u9053\u9898\u5df2\u7ecf\u5728\u52a8\u6001\u89c4\u5212\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u8fc7\uff0c\u76f8\u4fe1\u4f60\u5bf9\u5b83\u5e76\u4e0d\u964c\u751f\u3002

Question

\u7ed9\u5b9a \\(n\\) \u79cd\u786c\u5e01\uff0c\u7b2c \\(i\\) \u79cd\u786c\u5e01\u7684\u9762\u503c\u4e3a \\(coins[i - 1]\\) \uff0c\u76ee\u6807\u91d1\u989d\u4e3a \\(amt\\) \uff0c\u6bcf\u79cd\u786c\u5e01\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u80fd\u591f\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u7684\u6700\u5c11\u786c\u5e01\u4e2a\u6570\u3002\u5982\u679c\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u5219\u8fd4\u56de \\(-1\\) \u3002

\u672c\u9898\u7684\u8d2a\u5fc3\u7b56\u7565\u5982\u56fe 15-1 \u6240\u793a\u3002\u7ed9\u5b9a\u76ee\u6807\u91d1\u989d\uff0c\u6211\u4eec\u8d2a\u5fc3\u5730\u9009\u62e9\u4e0d\u5927\u4e8e\u4e14\u6700\u63a5\u8fd1\u5b83\u7684\u786c\u5e01\uff0c\u4e0d\u65ad\u5faa\u73af\u8be5\u6b65\u9aa4\uff0c\u76f4\u81f3\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u4e3a\u6b62\u3002

\u56fe 15-1 \u00a0 \u96f6\u94b1\u5151\u6362\u7684\u8d2a\u5fc3\u7b56\u7565

\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002\u4f60\u53ef\u80fd\u4f1a\u4e0d\u7531\u5730\u53d1\u51fa\u611f\u53f9\uff1aSo Clean \uff01\u8d2a\u5fc3\u7b97\u6cd5\u4ec5\u7528\u5341\u884c\u4ee3\u7801\u5c31\u89e3\u51b3\u4e86\u96f6\u94b1\u5151\u6362\u95ee\u9898\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig coin_change_greedy.py
def coin_change_greedy(coins: list[int], amt: int) -> int:\n\"\"\"\u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3\"\"\"\n# \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\ni = len(coins) - 1\ncount = 0\n# \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile amt > 0:\n# \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile i > 0 and coins[i] > amt:\ni -= 1\n# \u9009\u62e9 coins[i]\namt -= coins[i]\ncount += 1\n# \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nreturn count if amt == 0 else -1\n
coin_change_greedy.cpp
/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(vector<int> &coins, int amt) {\n// \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\nint i = coins.size() - 1;\nint count = 0;\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile (amt > 0) {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile (i > 0 && coins[i] > amt) {\ni--;\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i];\ncount++;\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nreturn amt == 0 ? count : -1;\n}\n
coin_change_greedy.java
/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(int[] coins, int amt) {\n// \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\nint i = coins.length - 1;\nint count = 0;\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile (amt > 0) {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile (i > 0 && coins[i] > amt) {\ni--;\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i];\ncount++;\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nreturn amt == 0 ? count : -1;\n}\n
coin_change_greedy.cs
/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint CoinChangeGreedy(int[] coins, int amt) {\n// \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\nint i = coins.Length - 1;\nint count = 0;\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile (amt > 0) {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile (i > 0 && coins[i] > amt) {\ni--;\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i];\ncount++;\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nreturn amt == 0 ? count : -1;\n}\n
coin_change_greedy.go
/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfunc coinChangeGreedy(coins []int, amt int) int {\n// \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\ni := len(coins) - 1\ncount := 0\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nfor amt > 0 {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nfor i > 0 && coins[i] > amt {\ni--\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i]\ncount++\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nif amt != 0 {\nreturn -1\n}\nreturn count\n}\n
coin_change_greedy.swift
/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfunc coinChangeGreedy(coins: [Int], amt: Int) -> Int {\n// \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\nvar i = coins.count - 1\nvar count = 0\nvar amt = amt\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile amt > 0 {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile i > 0 && coins[i] > amt {\ni -= 1\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i]\ncount += 1\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nreturn amt == 0 ? count : -1\n}\n
coin_change_greedy.js
/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfunction coinChangeGreedy(coins, amt) {\n// \u5047\u8bbe coins \u6570\u7ec4\u6709\u5e8f\nlet i = coins.length - 1;\nlet count = 0;\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile (amt > 0) {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile (i > 0 && coins[i] > amt) {\ni--;\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i];\ncount++;\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nreturn amt === 0 ? count : -1;\n}\n
coin_change_greedy.ts
/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfunction coinChangeGreedy(coins: number[], amt: number): number {\n// \u5047\u8bbe coins \u6570\u7ec4\u6709\u5e8f\nlet i = coins.length - 1;\nlet count = 0;\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile (amt > 0) {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile (i > 0 && coins[i] > amt) {\ni--;\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i];\ncount++;\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nreturn amt === 0 ? count : -1;\n}\n
coin_change_greedy.dart
/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(List<int> coins, int amt) {\n// \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\nint i = coins.length - 1;\nint count = 0;\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile (amt > 0) {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile (i > 0 && coins[i] > amt) {\ni--;\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i];\ncount++;\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nreturn amt == 0 ? count : -1;\n}\n
coin_change_greedy.rs
/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfn coin_change_greedy(coins: &[i32], mut amt: i32) -> i32 {\n// \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\nlet mut i = coins.len() - 1;\nlet mut count = 0;\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile amt > 0 {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile i > 0 && coins[i] > amt {\ni -= 1;\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i];\ncount += 1;\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nif amt == 0 {\ncount\n} else {\n-1\n}\n}\n
coin_change_greedy.c
/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(int* coins, int size, int amt) {\n// \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\nint i = size - 1;\nint count = 0;\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile (amt > 0) {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile (i > 0 && coins[i] > amt) {\ni--;\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i];\ncount++;\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nreturn amt == 0 ? count : -1;\n}\n
coin_change_greedy.zig
[class]{}-[func]{coinChangeGreedy}\n
"},{"location":"chapter_greedy/greedy_algorithm/#1511","title":"15.1.1 \u00a0 \u8d2a\u5fc3\u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"

\u8d2a\u5fc3\u7b97\u6cd5\u4e0d\u4ec5\u64cd\u4f5c\u76f4\u63a5\u3001\u5b9e\u73b0\u7b80\u5355\uff0c\u800c\u4e14\u901a\u5e38\u6548\u7387\u4e5f\u5f88\u9ad8\u3002\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u8bb0\u786c\u5e01\u6700\u5c0f\u9762\u503c\u4e3a \\(\\min(coins)\\) \uff0c\u5219\u8d2a\u5fc3\u9009\u62e9\u6700\u591a\u5faa\u73af \\(amt / \\min(coins)\\) \u6b21\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(amt / \\min(coins))\\) \u3002\u8fd9\u6bd4\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n \\times amt)\\) \u63d0\u5347\u4e86\u4e00\u4e2a\u6570\u91cf\u7ea7\u3002

\u7136\u800c\uff0c\u5bf9\u4e8e\u67d0\u4e9b\u786c\u5e01\u9762\u503c\u7ec4\u5408\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u5e76\u4e0d\u80fd\u627e\u5230\u6700\u4f18\u89e3\u3002\u56fe 15-2 \u7ed9\u51fa\u4e86\u4e24\u4e2a\u793a\u4f8b\u3002

  • \u6b63\u4f8b \\(coins = [1, 5, 10, 20, 50, 100]\\)\uff1a\u5728\u8be5\u786c\u5e01\u7ec4\u5408\u4e0b\uff0c\u7ed9\u5b9a\u4efb\u610f \\(amt\\) \uff0c\u8d2a\u5fc3\u7b97\u6cd5\u90fd\u53ef\u4ee5\u627e\u51fa\u6700\u4f18\u89e3\u3002
  • \u53cd\u4f8b \\(coins = [1, 20, 50]\\)\uff1a\u5047\u8bbe \\(amt = 60\\) \uff0c\u8d2a\u5fc3\u7b97\u6cd5\u53ea\u80fd\u627e\u5230 \\(50 + 1 \\times 10\\) \u7684\u5151\u6362\u7ec4\u5408\uff0c\u5171\u8ba1 \\(11\\) \u679a\u786c\u5e01\uff0c\u4f46\u52a8\u6001\u89c4\u5212\u53ef\u4ee5\u627e\u5230\u6700\u4f18\u89e3 \\(20 + 20 + 20\\) \uff0c\u4ec5\u9700 \\(3\\) \u679a\u786c\u5e01\u3002
  • \u53cd\u4f8b \\(coins = [1, 49, 50]\\)\uff1a\u5047\u8bbe \\(amt = 98\\) \uff0c\u8d2a\u5fc3\u7b97\u6cd5\u53ea\u80fd\u627e\u5230 \\(50 + 1 \\times 48\\) \u7684\u5151\u6362\u7ec4\u5408\uff0c\u5171\u8ba1 \\(49\\) \u679a\u786c\u5e01\uff0c\u4f46\u52a8\u6001\u89c4\u5212\u53ef\u4ee5\u627e\u5230\u6700\u4f18\u89e3 \\(49 + 49\\) \uff0c\u4ec5\u9700 \\(2\\) \u679a\u786c\u5e01\u3002

\u56fe 15-2 \u00a0 \u8d2a\u5fc3\u65e0\u6cd5\u627e\u51fa\u6700\u4f18\u89e3\u7684\u793a\u4f8b

\u4e5f\u5c31\u662f\u8bf4\uff0c\u5bf9\u4e8e\u96f6\u94b1\u5151\u6362\u95ee\u9898\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u65e0\u6cd5\u4fdd\u8bc1\u627e\u5230\u5168\u5c40\u6700\u4f18\u89e3\uff0c\u5e76\u4e14\u6709\u53ef\u80fd\u627e\u5230\u975e\u5e38\u5dee\u7684\u89e3\u3002\u5b83\u66f4\u9002\u5408\u7528\u52a8\u6001\u89c4\u5212\u89e3\u51b3\u3002

\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u9002\u7528\u4e8e\u4ee5\u4e0b\u4e24\u7c7b\u95ee\u9898\u3002

  1. \u53ef\u4ee5\u4fdd\u8bc1\u627e\u5230\u6700\u4f18\u89e3\uff1a\u8d2a\u5fc3\u7b97\u6cd5\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5f80\u5f80\u662f\u6700\u4f18\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u5f80\u5f80\u6bd4\u56de\u6eaf\u3001\u52a8\u6001\u89c4\u5212\u66f4\u9ad8\u6548\u3002
  2. \u53ef\u4ee5\u627e\u5230\u8fd1\u4f3c\u6700\u4f18\u89e3\uff1a\u8d2a\u5fc3\u7b97\u6cd5\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4e5f\u662f\u53ef\u7528\u7684\u3002\u5bf9\u4e8e\u5f88\u591a\u590d\u6742\u95ee\u9898\u6765\u8bf4\uff0c\u5bfb\u627e\u5168\u5c40\u6700\u4f18\u89e3\u662f\u975e\u5e38\u56f0\u96be\u7684\uff0c\u80fd\u4ee5\u8f83\u9ad8\u6548\u7387\u627e\u5230\u6b21\u4f18\u89e3\u4e5f\u662f\u975e\u5e38\u4e0d\u9519\u7684\u3002
"},{"location":"chapter_greedy/greedy_algorithm/#1512","title":"15.1.2 \u00a0 \u8d2a\u5fc3\u7b97\u6cd5\u7279\u6027","text":"

\u90a3\u4e48\u95ee\u9898\u6765\u4e86\uff0c\u4ec0\u4e48\u6837\u7684\u95ee\u9898\u9002\u5408\u7528\u8d2a\u5fc3\u7b97\u6cd5\u6c42\u89e3\u5462\uff1f\u6216\u8005\u8bf4\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4fdd\u8bc1\u627e\u5230\u6700\u4f18\u89e3\uff1f

\u76f8\u8f83\u4e8e\u52a8\u6001\u89c4\u5212\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u7684\u4f7f\u7528\u6761\u4ef6\u66f4\u52a0\u82db\u523b\uff0c\u5176\u4e3b\u8981\u5173\u6ce8\u95ee\u9898\u7684\u4e24\u4e2a\u6027\u8d28\u3002

  • \u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\uff1a\u53ea\u6709\u5f53\u5c40\u90e8\u6700\u4f18\u9009\u62e9\u59cb\u7ec8\u53ef\u4ee5\u5bfc\u81f4\u5168\u5c40\u6700\u4f18\u89e3\u65f6\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u624d\u80fd\u4fdd\u8bc1\u5f97\u5230\u6700\u4f18\u89e3\u3002
  • \u6700\u4f18\u5b50\u7ed3\u6784\uff1a\u539f\u95ee\u9898\u7684\u6700\u4f18\u89e3\u5305\u542b\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u3002

\u6700\u4f18\u5b50\u7ed3\u6784\u5df2\u7ecf\u5728\u52a8\u6001\u89c4\u5212\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u8fc7\uff0c\u4e0d\u518d\u8d58\u8ff0\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u4e00\u4e9b\u95ee\u9898\u7684\u6700\u4f18\u5b50\u7ed3\u6784\u5e76\u4e0d\u660e\u663e\uff0c\u4f46\u4ecd\u7136\u53ef\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u89e3\u51b3\u3002

\u6211\u4eec\u4e3b\u8981\u63a2\u7a76\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u7684\u5224\u65ad\u65b9\u6cd5\u3002\u867d\u7136\u5b83\u7684\u63cf\u8ff0\u770b\u4e0a\u53bb\u6bd4\u8f83\u7b80\u5355\uff0c\u4f46\u5b9e\u9645\u4e0a\u5bf9\u4e8e\u8bb8\u591a\u95ee\u9898\uff0c\u8bc1\u660e\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u4e0d\u662f\u4e00\u4ef6\u6613\u4e8b\u3002

\u4f8b\u5982\u96f6\u94b1\u5151\u6362\u95ee\u9898\uff0c\u6211\u4eec\u867d\u7136\u80fd\u591f\u5bb9\u6613\u5730\u4e3e\u51fa\u53cd\u4f8b\uff0c\u5bf9\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u8fdb\u884c\u8bc1\u4f2a\uff0c\u4f46\u8bc1\u5b9e\u7684\u96be\u5ea6\u8f83\u5927\u3002\u5982\u679c\u95ee\uff1a\u6ee1\u8db3\u4ec0\u4e48\u6761\u4ef6\u7684\u786c\u5e01\u7ec4\u5408\u53ef\u4ee5\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u6c42\u89e3\uff1f\u6211\u4eec\u5f80\u5f80\u53ea\u80fd\u51ed\u501f\u76f4\u89c9\u6216\u4e3e\u4f8b\u5b50\u6765\u7ed9\u51fa\u4e00\u4e2a\u6a21\u68f1\u4e24\u53ef\u7684\u7b54\u6848\uff0c\u800c\u96be\u4ee5\u7ed9\u51fa\u4e25\u8c28\u7684\u6570\u5b66\u8bc1\u660e\u3002

Quote

\u6709\u4e00\u7bc7\u8bba\u6587\u7ed9\u51fa\u4e86\u4e00\u4e2a \\(O(n^3)\\) \u65f6\u95f4\u590d\u6742\u5ea6\u7684\u7b97\u6cd5\uff0c\u7528\u4e8e\u5224\u65ad\u4e00\u4e2a\u786c\u5e01\u7ec4\u5408\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u627e\u51fa\u4efb\u4f55\u91d1\u989d\u7684\u6700\u4f18\u89e3\u3002

Pearson, David. A polynomial-time algorithm for the change-making problem. Operations Research Letters 33.3 (2005): 231-234.

"},{"location":"chapter_greedy/greedy_algorithm/#1513","title":"15.1.3 \u00a0 \u8d2a\u5fc3\u89e3\u9898\u6b65\u9aa4","text":"

\u8d2a\u5fc3\u95ee\u9898\u7684\u89e3\u51b3\u6d41\u7a0b\u5927\u4f53\u53ef\u5206\u4e3a\u4ee5\u4e0b\u4e09\u6b65\u3002

  1. \u95ee\u9898\u5206\u6790\uff1a\u68b3\u7406\u4e0e\u7406\u89e3\u95ee\u9898\u7279\u6027\uff0c\u5305\u62ec\u72b6\u6001\u5b9a\u4e49\u3001\u4f18\u5316\u76ee\u6807\u548c\u7ea6\u675f\u6761\u4ef6\u7b49\u3002\u8fd9\u4e00\u6b65\u5728\u56de\u6eaf\u548c\u52a8\u6001\u89c4\u5212\u4e2d\u90fd\u6709\u6d89\u53ca\u3002
  2. \u786e\u5b9a\u8d2a\u5fc3\u7b56\u7565\uff1a\u786e\u5b9a\u5982\u4f55\u5728\u6bcf\u4e00\u6b65\u4e2d\u505a\u51fa\u8d2a\u5fc3\u9009\u62e9\u3002\u8fd9\u4e2a\u7b56\u7565\u80fd\u591f\u5728\u6bcf\u4e00\u6b65\u51cf\u5c0f\u95ee\u9898\u7684\u89c4\u6a21\uff0c\u5e76\u6700\u7ec8\u80fd\u89e3\u51b3\u6574\u4e2a\u95ee\u9898\u3002
  3. \u6b63\u786e\u6027\u8bc1\u660e\uff1a\u901a\u5e38\u9700\u8981\u8bc1\u660e\u95ee\u9898\u5177\u6709\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u548c\u6700\u4f18\u5b50\u7ed3\u6784\u3002\u8fd9\u4e2a\u6b65\u9aa4\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u5230\u6570\u5b66\u8bc1\u660e\uff0c\u4f8b\u5982\u5f52\u7eb3\u6cd5\u6216\u53cd\u8bc1\u6cd5\u7b49\u3002

\u786e\u5b9a\u8d2a\u5fc3\u7b56\u7565\u662f\u6c42\u89e3\u95ee\u9898\u7684\u6838\u5fc3\u6b65\u9aa4\uff0c\u4f46\u5b9e\u65bd\u8d77\u6765\u53ef\u80fd\u5e76\u4e0d\u5bb9\u6613\uff0c\u4e3b\u8981\u5305\u542b\u4ee5\u4e0b\u539f\u56e0\u3002

  • \u4e0d\u540c\u95ee\u9898\u7684\u8d2a\u5fc3\u7b56\u7565\u7684\u5dee\u5f02\u8f83\u5927\u3002\u5bf9\u4e8e\u8bb8\u591a\u95ee\u9898\u6765\u8bf4\uff0c\u8d2a\u5fc3\u7b56\u7565\u90fd\u6bd4\u8f83\u6d45\u663e\uff0c\u6211\u4eec\u901a\u8fc7\u4e00\u4e9b\u5927\u6982\u7684\u601d\u8003\u4e0e\u5c1d\u8bd5\u5c31\u80fd\u5f97\u51fa\u3002\u800c\u5bf9\u4e8e\u4e00\u4e9b\u590d\u6742\u95ee\u9898\uff0c\u8d2a\u5fc3\u7b56\u7565\u53ef\u80fd\u975e\u5e38\u9690\u853d\uff0c\u8fd9\u79cd\u60c5\u51b5\u5c31\u975e\u5e38\u8003\u9a8c\u4e2a\u4eba\u7684\u89e3\u9898\u7ecf\u9a8c\u4e0e\u7b97\u6cd5\u80fd\u529b\u4e86\u3002
  • \u67d0\u4e9b\u8d2a\u5fc3\u7b56\u7565\u5177\u6709\u8f83\u5f3a\u7684\u8ff7\u60d1\u6027\u3002\u5f53\u6211\u4eec\u6ee1\u6000\u4fe1\u5fc3\u8bbe\u8ba1\u597d\u8d2a\u5fc3\u7b56\u7565\uff0c\u5199\u51fa\u89e3\u9898\u4ee3\u7801\u5e76\u63d0\u4ea4\u8fd0\u884c\uff0c\u5f88\u53ef\u80fd\u53d1\u73b0\u90e8\u5206\u6d4b\u8bd5\u6837\u4f8b\u65e0\u6cd5\u901a\u8fc7\u3002\u8fd9\u662f\u56e0\u4e3a\u8bbe\u8ba1\u7684\u8d2a\u5fc3\u7b56\u7565\u53ea\u662f\u201c\u90e8\u5206\u6b63\u786e\u201d\u7684\uff0c\u4e0a\u6587\u4ecb\u7ecd\u7684\u96f6\u94b1\u5151\u6362\u5c31\u662f\u4e2a\u5178\u578b\u6848\u4f8b\u3002

\u4e3a\u4e86\u4fdd\u8bc1\u6b63\u786e\u6027\uff0c\u6211\u4eec\u5e94\u8be5\u5bf9\u8d2a\u5fc3\u7b56\u7565\u8fdb\u884c\u4e25\u8c28\u7684\u6570\u5b66\u8bc1\u660e\uff0c\u901a\u5e38\u9700\u8981\u7528\u5230\u53cd\u8bc1\u6cd5\u6216\u6570\u5b66\u5f52\u7eb3\u6cd5\u3002

\u7136\u800c\uff0c\u6b63\u786e\u6027\u8bc1\u660e\u4e5f\u5f88\u53ef\u80fd\u4e0d\u662f\u4e00\u4ef6\u6613\u4e8b\u3002\u5982\u82e5\u6ca1\u6709\u5934\u7eea\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u9009\u62e9\u9762\u5411\u6d4b\u8bd5\u7528\u4f8b\u8fdb\u884c Debug \uff0c\u4e00\u6b65\u6b65\u4fee\u6539\u4e0e\u9a8c\u8bc1\u8d2a\u5fc3\u7b56\u7565\u3002

"},{"location":"chapter_greedy/greedy_algorithm/#1514","title":"15.1.4 \u00a0 \u8d2a\u5fc3\u5178\u578b\u4f8b\u9898","text":"

\u8d2a\u5fc3\u7b97\u6cd5\u5e38\u5e38\u5e94\u7528\u5728\u6ee1\u8db3\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u548c\u6700\u4f18\u5b50\u7ed3\u6784\u7684\u4f18\u5316\u95ee\u9898\u4e2d\uff0c\u4ee5\u4e0b\u5217\u4e3e\u4e86\u4e00\u4e9b\u5178\u578b\u7684\u8d2a\u5fc3\u7b97\u6cd5\u95ee\u9898\u3002

  • \u786c\u5e01\u627e\u96f6\u95ee\u9898\uff1a\u5728\u67d0\u4e9b\u786c\u5e01\u7ec4\u5408\u4e0b\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u603b\u662f\u53ef\u4ee5\u5f97\u5230\u6700\u4f18\u89e3\u3002
  • \u533a\u95f4\u8c03\u5ea6\u95ee\u9898\uff1a\u5047\u8bbe\u4f60\u6709\u4e00\u4e9b\u4efb\u52a1\uff0c\u6bcf\u4e2a\u4efb\u52a1\u5728\u4e00\u6bb5\u65f6\u95f4\u5185\u8fdb\u884c\uff0c\u4f60\u7684\u76ee\u6807\u662f\u5b8c\u6210\u5c3d\u53ef\u80fd\u591a\u7684\u4efb\u52a1\u3002\u5982\u679c\u6bcf\u6b21\u90fd\u9009\u62e9\u7ed3\u675f\u65f6\u95f4\u6700\u65e9\u7684\u4efb\u52a1\uff0c\u90a3\u4e48\u8d2a\u5fc3\u7b97\u6cd5\u5c31\u53ef\u4ee5\u5f97\u5230\u6700\u4f18\u89e3\u3002
  • \u5206\u6570\u80cc\u5305\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u7ec4\u7269\u54c1\u548c\u4e00\u4e2a\u8f7d\u91cd\u91cf\uff0c\u4f60\u7684\u76ee\u6807\u662f\u9009\u62e9\u4e00\u7ec4\u7269\u54c1\uff0c\u4f7f\u5f97\u603b\u91cd\u91cf\u4e0d\u8d85\u8fc7\u8f7d\u91cd\u91cf\uff0c\u4e14\u603b\u4ef7\u503c\u6700\u5927\u3002\u5982\u679c\u6bcf\u6b21\u90fd\u9009\u62e9\u6027\u4ef7\u6bd4\u6700\u9ad8\uff08\u4ef7\u503c / \u91cd\u91cf\uff09\u7684\u7269\u54c1\uff0c\u90a3\u4e48\u8d2a\u5fc3\u7b97\u6cd5\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\u53ef\u4ee5\u5f97\u5230\u6700\u4f18\u89e3\u3002
  • \u80a1\u7968\u4e70\u5356\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u7ec4\u80a1\u7968\u7684\u5386\u53f2\u4ef7\u683c\uff0c\u4f60\u53ef\u4ee5\u8fdb\u884c\u591a\u6b21\u4e70\u5356\uff0c\u4f46\u5982\u679c\u4f60\u5df2\u7ecf\u6301\u6709\u80a1\u7968\uff0c\u90a3\u4e48\u5728\u5356\u51fa\u4e4b\u524d\u4e0d\u80fd\u518d\u4e70\uff0c\u76ee\u6807\u662f\u83b7\u53d6\u6700\u5927\u5229\u6da6\u3002
  • \u970d\u592b\u66fc\u7f16\u7801\uff1a\u970d\u592b\u66fc\u7f16\u7801\u662f\u4e00\u79cd\u7528\u4e8e\u65e0\u635f\u6570\u636e\u538b\u7f29\u7684\u8d2a\u5fc3\u7b97\u6cd5\u3002\u901a\u8fc7\u6784\u5efa\u970d\u592b\u66fc\u6811\uff0c\u6bcf\u6b21\u9009\u62e9\u51fa\u73b0\u9891\u7387\u6700\u5c0f\u7684\u4e24\u4e2a\u8282\u70b9\u5408\u5e76\uff0c\u6700\u540e\u5f97\u5230\u7684\u970d\u592b\u66fc\u6811\u7684\u5e26\u6743\u8def\u5f84\u957f\u5ea6\uff08\u5373\u7f16\u7801\u957f\u5ea6\uff09\u6700\u5c0f\u3002
  • Dijkstra \u7b97\u6cd5\uff1a\u5b83\u662f\u4e00\u79cd\u89e3\u51b3\u7ed9\u5b9a\u6e90\u9876\u70b9\u5230\u5176\u4f59\u5404\u9876\u70b9\u7684\u6700\u77ed\u8def\u5f84\u95ee\u9898\u7684\u8d2a\u5fc3\u7b97\u6cd5\u3002
"},{"location":"chapter_greedy/max_capacity_problem/","title":"15.3 \u00a0 \u6700\u5927\u5bb9\u91cf\u95ee\u9898","text":"

Question

\u8f93\u5165\u4e00\u4e2a\u6570\u7ec4 \\(ht\\) \uff0c\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u4ee3\u8868\u4e00\u4e2a\u5782\u76f4\u9694\u677f\u7684\u9ad8\u5ea6\u3002\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u4e24\u4e2a\u9694\u677f\uff0c\u4ee5\u53ca\u5b83\u4eec\u4e4b\u95f4\u7684\u7a7a\u95f4\u53ef\u4ee5\u7ec4\u6210\u4e00\u4e2a\u5bb9\u5668\u3002

\u5bb9\u5668\u7684\u5bb9\u91cf\u7b49\u4e8e\u9ad8\u5ea6\u548c\u5bbd\u5ea6\u7684\u4e58\u79ef\uff08\u5373\u9762\u79ef\uff09\uff0c\u5176\u4e2d\u9ad8\u5ea6\u7531\u8f83\u77ed\u7684\u9694\u677f\u51b3\u5b9a\uff0c\u5bbd\u5ea6\u662f\u4e24\u4e2a\u9694\u677f\u7684\u6570\u7ec4\u7d22\u5f15\u4e4b\u5dee\u3002

\u8bf7\u5728\u6570\u7ec4\u4e2d\u9009\u62e9\u4e24\u4e2a\u9694\u677f\uff0c\u4f7f\u5f97\u7ec4\u6210\u7684\u5bb9\u5668\u7684\u5bb9\u91cf\u6700\u5927\uff0c\u8fd4\u56de\u6700\u5927\u5bb9\u91cf\u3002

\u56fe 15-7 \u00a0 \u6700\u5927\u5bb9\u91cf\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e

\u5bb9\u5668\u7531\u4efb\u610f\u4e24\u4e2a\u9694\u677f\u56f4\u6210\uff0c\u56e0\u6b64\u672c\u9898\u7684\u72b6\u6001\u4e3a\u4e24\u4e2a\u9694\u677f\u7684\u7d22\u5f15\uff0c\u8bb0\u4e3a \\([i, j]\\) \u3002

\u6839\u636e\u9898\u610f\uff0c\u5bb9\u91cf\u7b49\u4e8e\u9ad8\u5ea6\u4e58\u4ee5\u5bbd\u5ea6\uff0c\u5176\u4e2d\u9ad8\u5ea6\u7531\u77ed\u677f\u51b3\u5b9a\uff0c\u5bbd\u5ea6\u662f\u4e24\u9694\u677f\u7684\u7d22\u5f15\u4e4b\u5dee\u3002\u8bbe\u5bb9\u91cf\u4e3a \\(cap[i, j]\\) \uff0c\u5219\u53ef\u5f97\u8ba1\u7b97\u516c\u5f0f\uff1a

\\[ cap[i, j] = \\min(ht[i], ht[j]) \\times (j - i) \\]

\u8bbe\u6570\u7ec4\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u4e24\u4e2a\u9694\u677f\u7684\u7ec4\u5408\u6570\u91cf\uff08\u5373\u72b6\u6001\u603b\u6570\uff09\u4e3a \\(C_n^2 = \\frac{n(n - 1)}{2}\\) \u4e2a\u3002\u6700\u76f4\u63a5\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u7a77\u4e3e\u6240\u6709\u72b6\u6001\uff0c\u4ece\u800c\u6c42\u5f97\u6700\u5927\u5bb9\u91cf\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002

"},{"location":"chapter_greedy/max_capacity_problem/#1","title":"1. \u00a0 \u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a","text":"

\u8fd9\u9053\u9898\u8fd8\u6709\u66f4\u9ad8\u6548\u7387\u7684\u89e3\u6cd5\u3002\u5982\u56fe 15-8 \u6240\u793a\uff0c\u73b0\u9009\u53d6\u4e00\u4e2a\u72b6\u6001 \\([i, j]\\) \uff0c\u5176\u6ee1\u8db3\u7d22\u5f15 \\(i < j\\) \u4e14\u9ad8\u5ea6 \\(ht[i] < ht[j]\\) \uff0c\u5373 \\(i\\) \u4e3a\u77ed\u677f\u3001\\(j\\) \u4e3a\u957f\u677f\u3002

\u56fe 15-8 \u00a0 \u521d\u59cb\u72b6\u6001

\u5982\u56fe 15-9 \u6240\u793a\uff0c\u82e5\u6b64\u65f6\u5c06\u957f\u677f \\(j\\) \u5411\u77ed\u677f \\(i\\) \u9760\u8fd1\uff0c\u5219\u5bb9\u91cf\u4e00\u5b9a\u53d8\u5c0f\u3002

\u8fd9\u662f\u56e0\u4e3a\u5728\u79fb\u52a8\u957f\u677f \\(j\\) \u540e\uff0c\u5bbd\u5ea6 \\(j-i\\) \u80af\u5b9a\u53d8\u5c0f\uff1b\u800c\u9ad8\u5ea6\u7531\u77ed\u677f\u51b3\u5b9a\uff0c\u56e0\u6b64\u9ad8\u5ea6\u53ea\u53ef\u80fd\u4e0d\u53d8\uff08 \\(i\\) \u4ecd\u4e3a\u77ed\u677f\uff09\u6216\u53d8\u5c0f\uff08\u79fb\u52a8\u540e\u7684 \\(j\\) \u6210\u4e3a\u77ed\u677f\uff09\u3002

\u56fe 15-9 \u00a0 \u5411\u5185\u79fb\u52a8\u957f\u677f\u540e\u7684\u72b6\u6001

\u53cd\u5411\u601d\u8003\uff0c\u6211\u4eec\u53ea\u6709\u5411\u5185\u6536\u7f29\u77ed\u677f \\(i\\) \uff0c\u624d\u6709\u53ef\u80fd\u4f7f\u5bb9\u91cf\u53d8\u5927\u3002\u56e0\u4e3a\u867d\u7136\u5bbd\u5ea6\u4e00\u5b9a\u53d8\u5c0f\uff0c\u4f46\u9ad8\u5ea6\u53ef\u80fd\u4f1a\u53d8\u5927\uff08\u79fb\u52a8\u540e\u7684\u77ed\u677f \\(i\\) \u53ef\u80fd\u4f1a\u53d8\u957f\uff09\u3002\u4f8b\u5982\u5728\u56fe 15-10 \u4e2d\uff0c\u79fb\u52a8\u77ed\u677f\u540e\u9762\u79ef\u53d8\u5927\u3002

\u56fe 15-10 \u00a0 \u5411\u5185\u79fb\u52a8\u77ed\u677f\u540e\u7684\u72b6\u6001

\u7531\u6b64\u4fbf\u53ef\u63a8\u51fa\u672c\u9898\u7684\u8d2a\u5fc3\u7b56\u7565\uff1a\u521d\u59cb\u5316\u4e24\u6307\u9488\u5206\u88c2\u5bb9\u5668\u4e24\u7aef\uff0c\u6bcf\u8f6e\u5411\u5185\u6536\u7f29\u77ed\u677f\u5bf9\u5e94\u7684\u6307\u9488\uff0c\u76f4\u81f3\u4e24\u6307\u9488\u76f8\u9047\u3002

\u56fe 15-11 \u5c55\u793a\u4e86\u8d2a\u5fc3\u7b56\u7565\u7684\u6267\u884c\u8fc7\u7a0b\u3002

  1. \u521d\u59cb\u72b6\u6001\u4e0b\uff0c\u6307\u9488 \\(i\\) \u548c \\(j\\) \u5206\u5217\u4e0e\u6570\u7ec4\u4e24\u7aef\u3002
  2. \u8ba1\u7b97\u5f53\u524d\u72b6\u6001\u7684\u5bb9\u91cf \\(cap[i, j]\\) \uff0c\u5e76\u66f4\u65b0\u6700\u5927\u5bb9\u91cf\u3002
  3. \u6bd4\u8f83\u677f \\(i\\) \u548c \u677f \\(j\\) \u7684\u9ad8\u5ea6\uff0c\u5e76\u5c06\u77ed\u677f\u5411\u5185\u79fb\u52a8\u4e00\u683c\u3002
  4. \u5faa\u73af\u6267\u884c\u7b2c 2. \u548c 3. \u6b65\uff0c\u76f4\u81f3 \\(i\\) \u548c \\(j\\) \u76f8\u9047\u65f6\u7ed3\u675f\u3002
<1><2><3><4><5><6><7><8><9>

\u56fe 15-11 \u00a0 \u6700\u5927\u5bb9\u91cf\u95ee\u9898\u7684\u8d2a\u5fc3\u8fc7\u7a0b

"},{"location":"chapter_greedy/max_capacity_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u4ee3\u7801\u5faa\u73af\u6700\u591a \\(n\\) \u8f6e\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002

\u53d8\u91cf \\(i\\)\u3001\\(j\\)\u3001\\(res\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u989d\u5916\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig max_capacity.py
def max_capacity(ht: list[int]) -> int:\n\"\"\"\u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3\"\"\"\n# \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\ni, j = 0, len(ht) - 1\n# \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nres = 0\n# \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile i < j:\n# \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\ncap = min(ht[i], ht[j]) * (j - i)\nres = max(res, cap)\n# \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif ht[i] < ht[j]:\ni += 1\nelse:\nj -= 1\nreturn res\n
max_capacity.cpp
/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(vector<int> &ht) {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\nint i = 0, j = ht.size() - 1;\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nint res = 0;\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile (i < j) {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\nint cap = min(ht[i], ht[j]) * (j - i);\nres = max(res, cap);\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif (ht[i] < ht[j]) {\ni++;\n} else {\nj--;\n}\n}\nreturn res;\n}\n
max_capacity.java
/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(int[] ht) {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\nint i = 0, j = ht.length - 1;\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nint res = 0;\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile (i < j) {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\nint cap = Math.min(ht[i], ht[j]) * (j - i);\nres = Math.max(res, cap);\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif (ht[i] < ht[j]) {\ni++;\n} else {\nj--;\n}\n}\nreturn res;\n}\n
max_capacity.cs
/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint MaxCapacity(int[] ht) {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\nint i = 0, j = ht.Length - 1;\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nint res = 0;\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile (i < j) {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\nint cap = Math.Min(ht[i], ht[j]) * (j - i);\nres = Math.Max(res, cap);\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif (ht[i] < ht[j]) {\ni++;\n} else {\nj--;\n}\n}\nreturn res;\n}\n
max_capacity.go
/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfunc maxCapacity(ht []int) int {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\ni, j := 0, len(ht)-1\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nres := 0\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nfor i < j {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\ncapacity := int(math.Min(float64(ht[i]), float64(ht[j]))) * (j - i)\nres = int(math.Max(float64(res), float64(capacity)))\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif ht[i] < ht[j] {\ni++\n} else {\nj--\n}\n}\nreturn res\n}\n
max_capacity.swift
/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfunc maxCapacity(ht: [Int]) -> Int {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\nvar i = 0, j = ht.count - 1\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nvar res = 0\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile i < j {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\nlet cap = min(ht[i], ht[j]) * (j - i)\nres = max(res, cap)\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif ht[i] < ht[j] {\ni += 1\n} else {\nj -= 1\n}\n}\nreturn res\n}\n
max_capacity.js
/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfunction maxCapacity(ht) {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\nlet i = 0,\nj = ht.length - 1;\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nlet res = 0;\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile (i < j) {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\nconst cap = Math.min(ht[i], ht[j]) * (j - i);\nres = Math.max(res, cap);\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif (ht[i] < ht[j]) {\ni += 1;\n} else {\nj -= 1;\n}\n}\nreturn res;\n}\n
max_capacity.ts
/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfunction maxCapacity(ht: number[]): number {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\nlet i = 0,\nj = ht.length - 1;\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nlet res = 0;\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile (i < j) {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\nconst cap: number = Math.min(ht[i], ht[j]) * (j - i);\nres = Math.max(res, cap);\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif (ht[i] < ht[j]) {\ni += 1;\n} else {\nj -= 1;\n}\n}\nreturn res;\n}\n
max_capacity.dart
/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(List<int> ht) {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\nint i = 0, j = ht.length - 1;\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nint res = 0;\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile (i < j) {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\nint cap = min(ht[i], ht[j]) * (j - i);\nres = max(res, cap);\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif (ht[i] < ht[j]) {\ni++;\n} else {\nj--;\n}\n}\nreturn res;\n}\n
max_capacity.rs
/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfn max_capacity(ht: &[i32]) -> i32 {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\nlet mut i = 0;\nlet mut j = ht.len() - 1;\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nlet mut res = 0;\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile i < j {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\nlet cap = std::cmp::min(ht[i], ht[j]) * (j - i) as i32;\nres = std::cmp::max(res, cap);\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif ht[i] < ht[j] {\ni += 1;\n} else {\nj -= 1;\n}\n}\nres\n}\n
max_capacity.c
/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(int ht[], int htLength) {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\nint i = 0;\nint j = htLength - 1;\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nint res = 0;\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile (i < j) {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\nint capacity = MIN(ht[i], ht[j]) * (j - i);\nres = MAX(res, capacity);\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif (ht[i] < ht[j]) {\ni++;\n} else {\nj--;\n}\n}\nreturn res;\n}\n
max_capacity.zig
[class]{}-[func]{maxCapacity}\n
"},{"location":"chapter_greedy/max_capacity_problem/#3","title":"3. \u00a0 \u6b63\u786e\u6027\u8bc1\u660e","text":"

\u4e4b\u6240\u4ee5\u8d2a\u5fc3\u6bd4\u7a77\u4e3e\u66f4\u5feb\uff0c\u662f\u56e0\u4e3a\u6bcf\u8f6e\u7684\u8d2a\u5fc3\u9009\u62e9\u90fd\u4f1a\u201c\u8df3\u8fc7\u201d\u4e00\u4e9b\u72b6\u6001\u3002

\u6bd4\u5982\u5728\u72b6\u6001 \\(cap[i, j]\\) \u4e0b\uff0c\\(i\\) \u4e3a\u77ed\u677f\u3001\\(j\\) \u4e3a\u957f\u677f\u3002\u82e5\u8d2a\u5fc3\u5730\u5c06\u77ed\u677f \\(i\\) \u5411\u5185\u79fb\u52a8\u4e00\u683c\uff0c\u4f1a\u5bfc\u81f4\u56fe 15-12 \u6240\u793a\u7684\u72b6\u6001\u88ab\u201c\u8df3\u8fc7\u201d\u3002\u8fd9\u610f\u5473\u7740\u4e4b\u540e\u65e0\u6cd5\u9a8c\u8bc1\u8fd9\u4e9b\u72b6\u6001\u7684\u5bb9\u91cf\u5927\u5c0f\u3002

\\[ cap[i, i+1], cap[i, i+2], \\dots, cap[i, j-2], cap[i, j-1] \\]

\u56fe 15-12 \u00a0 \u79fb\u52a8\u77ed\u677f\u5bfc\u81f4\u88ab\u8df3\u8fc7\u7684\u72b6\u6001

\u89c2\u5bdf\u53d1\u73b0\uff0c\u8fd9\u4e9b\u88ab\u8df3\u8fc7\u7684\u72b6\u6001\u5b9e\u9645\u4e0a\u5c31\u662f\u5c06\u957f\u677f \\(j\\) \u5411\u5185\u79fb\u52a8\u7684\u6240\u6709\u72b6\u6001\u3002\u800c\u5728\u7b2c\u4e8c\u6b65\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u8bc1\u660e\u5185\u79fb\u957f\u677f\u4e00\u5b9a\u4f1a\u5bfc\u81f4\u5bb9\u91cf\u53d8\u5c0f\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u88ab\u8df3\u8fc7\u7684\u72b6\u6001\u90fd\u4e0d\u53ef\u80fd\u662f\u6700\u4f18\u89e3\uff0c\u8df3\u8fc7\u5b83\u4eec\u4e0d\u4f1a\u5bfc\u81f4\u9519\u8fc7\u6700\u4f18\u89e3\u3002

\u4ee5\u4e0a\u7684\u5206\u6790\u8bf4\u660e\uff0c\u79fb\u52a8\u77ed\u677f\u7684\u64cd\u4f5c\u662f\u201c\u5b89\u5168\u201d\u7684\uff0c\u8d2a\u5fc3\u7b56\u7565\u662f\u6709\u6548\u7684\u3002

"},{"location":"chapter_greedy/max_product_cutting_problem/","title":"15.4 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u79ef\u95ee\u9898","text":"

Question

\u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570 \\(n\\) \uff0c\u5c06\u5176\u5207\u5206\u4e3a\u81f3\u5c11\u4e24\u4e2a\u6b63\u6574\u6570\u7684\u548c\uff0c\u6c42\u5207\u5206\u540e\u6240\u6709\u6574\u6570\u7684\u4e58\u79ef\u6700\u5927\u662f\u591a\u5c11\u3002

\u56fe 15-13 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u79ef\u7684\u95ee\u9898\u5b9a\u4e49

\u5047\u8bbe\u6211\u4eec\u5c06 \\(n\\) \u5207\u5206\u4e3a \\(m\\) \u4e2a\u6574\u6570\u56e0\u5b50\uff0c\u5176\u4e2d\u7b2c \\(i\\) \u4e2a\u56e0\u5b50\u8bb0\u4e3a \\(n_i\\) \uff0c\u5373

\\[ n = \\sum_{i=1}^{m}n_i \\]

\u672c\u9898\u76ee\u6807\u662f\u6c42\u5f97\u6240\u6709\u6574\u6570\u56e0\u5b50\u7684\u6700\u5927\u4e58\u79ef\uff0c\u5373

\\[ \\max(\\prod_{i=1}^{m}n_i) \\]

\u6211\u4eec\u9700\u8981\u601d\u8003\u7684\u662f\uff1a\u5207\u5206\u6570\u91cf \\(m\\) \u5e94\u8be5\u591a\u5927\uff0c\u6bcf\u4e2a \\(n_i\\) \u5e94\u8be5\u662f\u591a\u5c11\uff1f

"},{"location":"chapter_greedy/max_product_cutting_problem/#1","title":"1. \u00a0 \u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a","text":"

\u6839\u636e\u7ecf\u9a8c\uff0c\u4e24\u4e2a\u6574\u6570\u7684\u4e58\u79ef\u5f80\u5f80\u6bd4\u5b83\u4eec\u7684\u52a0\u548c\u66f4\u5927\u3002\u5047\u8bbe\u4ece \\(n\\) \u4e2d\u5206\u51fa\u4e00\u4e2a\u56e0\u5b50 \\(2\\) \uff0c\u5219\u5b83\u4eec\u7684\u4e58\u79ef\u4e3a \\(2(n-2)\\) \u3002\u6211\u4eec\u5c06\u8be5\u4e58\u79ef\u4e0e \\(n\\) \u4f5c\u6bd4\u8f83\uff1a

\\[ \\begin{aligned} 2(n-2) & \\geq n \\newline 2n - n - 4 & \\geq 0 \\newline n & \\geq 4 \\end{aligned} \\]

\u5982\u56fe 15-14 \u6240\u793a\uff0c\u5f53 \\(n \\geq 4\\) \u65f6\uff0c\u5207\u5206\u51fa\u4e00\u4e2a \\(2\\) \u540e\u4e58\u79ef\u4f1a\u53d8\u5927\uff0c\u8fd9\u8bf4\u660e\u5927\u4e8e\u7b49\u4e8e \\(4\\) \u7684\u6574\u6570\u90fd\u5e94\u8be5\u88ab\u5207\u5206\u3002

\u8d2a\u5fc3\u7b56\u7565\u4e00\uff1a\u5982\u679c\u5207\u5206\u65b9\u6848\u4e2d\u5305\u542b \\(\\geq 4\\) \u7684\u56e0\u5b50\uff0c\u90a3\u4e48\u5b83\u5c31\u5e94\u8be5\u88ab\u7ee7\u7eed\u5207\u5206\u3002\u6700\u7ec8\u7684\u5207\u5206\u65b9\u6848\u53ea\u5e94\u51fa\u73b0 \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u8fd9\u4e09\u79cd\u56e0\u5b50\u3002

\u56fe 15-14 \u00a0 \u5207\u5206\u5bfc\u81f4\u4e58\u79ef\u53d8\u5927

\u63a5\u4e0b\u6765\u601d\u8003\u54ea\u4e2a\u56e0\u5b50\u662f\u6700\u4f18\u7684\u3002\u5728 \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u8fd9\u4e09\u4e2a\u56e0\u5b50\u4e2d\uff0c\u663e\u7136 \\(1\\) \u662f\u6700\u5dee\u7684\uff0c\u56e0\u4e3a \\(1 \\times (n-1) < n\\) \u6052\u6210\u7acb\uff0c\u5373\u5207\u5206\u51fa \\(1\\) \u53cd\u800c\u4f1a\u5bfc\u81f4\u4e58\u79ef\u51cf\u5c0f\u3002

\u5982\u56fe 15-15 \u6240\u793a\uff0c\u5f53 \\(n = 6\\) \u65f6\uff0c\u6709 \\(3 \\times 3 > 2 \\times 2 \\times 2\\) \u3002\u8fd9\u610f\u5473\u7740\u5207\u5206\u51fa \\(3\\) \u6bd4\u5207\u5206\u51fa \\(2\\) \u66f4\u4f18\u3002

\u8d2a\u5fc3\u7b56\u7565\u4e8c\uff1a\u5728\u5207\u5206\u65b9\u6848\u4e2d\uff0c\u6700\u591a\u53ea\u5e94\u5b58\u5728\u4e24\u4e2a \\(2\\) \u3002\u56e0\u4e3a\u4e09\u4e2a \\(2\\) \u603b\u662f\u53ef\u4ee5\u88ab\u66ff\u6362\u4e3a\u4e24\u4e2a \\(3\\) \uff0c\u4ece\u800c\u83b7\u5f97\u66f4\u5927\u4e58\u79ef\u3002

\u56fe 15-15 \u00a0 \u6700\u4f18\u5207\u5206\u56e0\u5b50

\u603b\u7ed3\u4ee5\u4e0a\uff0c\u53ef\u63a8\u51fa\u4ee5\u4e0b\u8d2a\u5fc3\u7b56\u7565\u3002

  1. \u8f93\u5165\u6574\u6570 \\(n\\) \uff0c\u4ece\u5176\u4e0d\u65ad\u5730\u5207\u5206\u51fa\u56e0\u5b50 \\(3\\) \uff0c\u76f4\u81f3\u4f59\u6570\u4e3a \\(0\\)\u3001\\(1\\)\u3001\\(2\\) \u3002
  2. \u5f53\u4f59\u6570\u4e3a \\(0\\) \u65f6\uff0c\u4ee3\u8868 \\(n\\) \u662f \\(3\\) \u7684\u500d\u6570\uff0c\u56e0\u6b64\u4e0d\u505a\u4efb\u4f55\u5904\u7406\u3002
  3. \u5f53\u4f59\u6570\u4e3a \\(2\\) \u65f6\uff0c\u4e0d\u7ee7\u7eed\u5212\u5206\uff0c\u4fdd\u7559\u4e4b\u3002
  4. \u5f53\u4f59\u6570\u4e3a \\(1\\) \u65f6\uff0c\u7531\u4e8e \\(2 \\times 2 > 1 \\times 3\\) \uff0c\u56e0\u6b64\u5e94\u5c06\u6700\u540e\u4e00\u4e2a \\(3\\) \u66ff\u6362\u4e3a \\(2\\) \u3002
"},{"location":"chapter_greedy/max_product_cutting_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u5982\u56fe 15-16 \u6240\u793a\uff0c\u6211\u4eec\u65e0\u987b\u901a\u8fc7\u5faa\u73af\u6765\u5207\u5206\u6574\u6570\uff0c\u800c\u53ef\u4ee5\u5229\u7528\u5411\u4e0b\u6574\u9664\u8fd0\u7b97\u5f97\u5230 \\(3\\) \u7684\u4e2a\u6570 \\(a\\) \uff0c\u7528\u53d6\u6a21\u8fd0\u7b97\u5f97\u5230\u4f59\u6570 \\(b\\) \uff0c\u6b64\u65f6\u6709\uff1a

\\[ n = 3 a + b \\]

\u8bf7\u6ce8\u610f\uff0c\u5bf9\u4e8e \\(n \\leq 3\\) \u7684\u8fb9\u754c\u60c5\u51b5\uff0c\u5fc5\u987b\u62c6\u5206\u51fa\u4e00\u4e2a \\(1\\) \uff0c\u4e58\u79ef\u4e3a \\(1 \\times (n - 1)\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig max_product_cutting.py
def max_product_cutting(n: int) -> int:\n\"\"\"\u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3\"\"\"\n# \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif n <= 3:\nreturn 1 * (n - 1)\n# \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\na, b = n // 3, n % 3\nif b == 1:\n# \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn int(math.pow(3, a - 1)) * 2 * 2\nif b == 2:\n# \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn int(math.pow(3, a)) * 2\n# \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn int(math.pow(3, a))\n
max_product_cutting.cpp
/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n// \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif (n <= 3) {\nreturn 1 * (n - 1);\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\nint a = n / 3;\nint b = n % 3;\nif (b == 1) {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn (int)pow(3, a - 1) * 2 * 2;\n}\nif (b == 2) {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn (int)pow(3, a) * 2;\n}\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn (int)pow(3, a);\n}\n
max_product_cutting.java
/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n// \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif (n <= 3) {\nreturn 1 * (n - 1);\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\nint a = n / 3;\nint b = n % 3;\nif (b == 1) {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn (int) Math.pow(3, a - 1) * 2 * 2;\n}\nif (b == 2) {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn (int) Math.pow(3, a) * 2;\n}\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn (int) Math.pow(3, a);\n}\n
max_product_cutting.cs
/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint MaxProductCutting(int n) {\n// \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif (n <= 3) {\nreturn 1 * (n - 1);\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\nint a = n / 3;\nint b = n % 3;\nif (b == 1) {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn (int)Math.Pow(3, a - 1) * 2 * 2;\n}\nif (b == 2) {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn (int)Math.Pow(3, a) * 2;\n}\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn (int)Math.Pow(3, a);\n}\n
max_product_cutting.go
/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfunc maxProductCutting(n int) int {\n// \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif n <= 3 {\nreturn 1 * (n - 1)\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\na := n / 3\nb := n % 3\nif b == 1 {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn int(math.Pow(3, float64(a-1))) * 2 * 2\n}\nif b == 2 {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn int(math.Pow(3, float64(a))) * 2\n}\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn int(math.Pow(3, float64(a)))\n}\n
max_product_cutting.swift
/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfunc maxProductCutting(n: Int) -> Int {\n// \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif n <= 3 {\nreturn 1 * (n - 1)\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\nlet a = n / 3\nlet b = n % 3\nif b == 1 {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn pow(3, a - 1) * 2 * 2\n}\nif b == 2 {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn pow(3, a) * 2\n}\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn pow(3, a)\n}\n
max_product_cutting.js
/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfunction maxProductCutting(n) {\n// \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif (n <= 3) {\nreturn 1 * (n - 1);\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\nlet a = Math.floor(n / 3);\nlet b = n % 3;\nif (b === 1) {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn Math.pow(3, a - 1) * 2 * 2;\n}\nif (b === 2) {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn Math.pow(3, a) * 2;\n}\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn Math.pow(3, a);\n}\n
max_product_cutting.ts
/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfunction maxProductCutting(n: number): number {\n// \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif (n <= 3) {\nreturn 1 * (n - 1);\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\nlet a: number = Math.floor(n / 3);\nlet b: number = n % 3;\nif (b === 1) {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn Math.pow(3, a - 1) * 2 * 2;\n}\nif (b === 2) {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn Math.pow(3, a) * 2;\n}\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn Math.pow(3, a);\n}\n
max_product_cutting.dart
/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n// \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif (n <= 3) {\nreturn 1 * (n - 1);\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\nint a = n ~/ 3;\nint b = n % 3;\nif (b == 1) {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn (pow(3, a - 1) * 2 * 2).toInt();\n}\nif (b == 2) {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn (pow(3, a) * 2).toInt();\n}\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn pow(3, a).toInt();\n}\n
max_product_cutting.rs
/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfn max_product_cutting(n: i32) -> i32 {\n// \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif n <= 3 {\nreturn 1 * (n - 1);\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\nlet a = n / 3;\nlet b = n % 3;\nif b == 1 {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n3_i32.pow(a as u32 - 1) * 2 * 2\n} else if b == 2 {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n3_i32.pow(a as u32) * 2\n} else {\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n3_i32.pow(a as u32)\n}\n}\n
max_product_cutting.c
/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n// \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif (n <= 3) {\nreturn 1 * (n - 1);\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\nint a = n / 3;\nint b = n % 3;\nif (b == 1) {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn pow(3, a - 1) * 2 * 2;\n}\nif (b == 2) {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn pow(3, a) * 2;\n}\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn pow(3, a);\n}\n
max_product_cutting.zig
[class]{}-[func]{maxProductCutting}\n

\u56fe 15-16 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u79ef\u7684\u8ba1\u7b97\u65b9\u6cd5

\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u7f16\u7a0b\u8bed\u8a00\u7684\u5e42\u8fd0\u7b97\u7684\u5b9e\u73b0\u65b9\u6cd5\u3002\u4ee5 Python \u4e3a\u4f8b\uff0c\u5e38\u7528\u7684\u5e42\u8ba1\u7b97\u51fd\u6570\u6709\u4e09\u79cd\u3002

  • \u8fd0\u7b97\u7b26 ** \u548c\u51fd\u6570 pow() \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(\\log\u2061 a)\\) \u3002
  • \u51fd\u6570 math.pow() \u5185\u90e8\u8c03\u7528 C \u8bed\u8a00\u5e93\u7684 pow() \u51fd\u6570\uff0c\u5176\u6267\u884c\u6d6e\u70b9\u53d6\u5e42\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002

\u53d8\u91cf \\(a\\) \u548c \\(b\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002

"},{"location":"chapter_greedy/max_product_cutting_problem/#3","title":"3. \u00a0 \u6b63\u786e\u6027\u8bc1\u660e","text":"

\u4f7f\u7528\u53cd\u8bc1\u6cd5\uff0c\u53ea\u5206\u6790 \\(n \\geq 3\\) \u7684\u60c5\u51b5\u3002

  1. \u6240\u6709\u56e0\u5b50 \\(\\leq 3\\) \uff1a\u5047\u8bbe\u6700\u4f18\u5207\u5206\u65b9\u6848\u4e2d\u5b58\u5728 \\(\\geq 4\\) \u7684\u56e0\u5b50 \\(x\\) \uff0c\u90a3\u4e48\u4e00\u5b9a\u53ef\u4ee5\u5c06\u5176\u7ee7\u7eed\u5212\u5206\u4e3a \\(2(x-2)\\) \uff0c\u4ece\u800c\u83b7\u5f97\u66f4\u5927\u7684\u4e58\u79ef\u3002\u8fd9\u4e0e\u5047\u8bbe\u77db\u76fe\u3002
  2. \u5207\u5206\u65b9\u6848\u4e0d\u5305\u542b \\(1\\) \uff1a\u5047\u8bbe\u6700\u4f18\u5207\u5206\u65b9\u6848\u4e2d\u5b58\u5728\u4e00\u4e2a\u56e0\u5b50 \\(1\\) \uff0c\u90a3\u4e48\u5b83\u4e00\u5b9a\u53ef\u4ee5\u5408\u5e76\u5165\u53e6\u5916\u4e00\u4e2a\u56e0\u5b50\u4e2d\uff0c\u4ee5\u83b7\u53d6\u66f4\u5927\u4e58\u79ef\u3002\u8fd9\u4e0e\u5047\u8bbe\u77db\u76fe\u3002
  3. \u5207\u5206\u65b9\u6848\u6700\u591a\u5305\u542b\u4e24\u4e2a \\(2\\) \uff1a\u5047\u8bbe\u6700\u4f18\u5207\u5206\u65b9\u6848\u4e2d\u5305\u542b\u4e09\u4e2a \\(2\\) \uff0c\u90a3\u4e48\u4e00\u5b9a\u53ef\u4ee5\u66ff\u6362\u4e3a\u4e24\u4e2a \\(3\\) \uff0c\u4e58\u79ef\u66f4\u5927\u3002\u8fd9\u4e0e\u5047\u8bbe\u77db\u76fe\u3002
"},{"location":"chapter_greedy/summary/","title":"15.5 \u00a0 \u5c0f\u7ed3","text":"
  • \u8d2a\u5fc3\u7b97\u6cd5\u901a\u5e38\u7528\u4e8e\u89e3\u51b3\u6700\u4f18\u5316\u95ee\u9898\uff0c\u5176\u539f\u7406\u662f\u5728\u6bcf\u4e2a\u51b3\u7b56\u9636\u6bb5\u90fd\u505a\u51fa\u5c40\u90e8\u6700\u4f18\u7684\u51b3\u7b56\uff0c\u4ee5\u671f\u671b\u83b7\u5f97\u5168\u5c40\u6700\u4f18\u89e3\u3002
  • \u8d2a\u5fc3\u7b97\u6cd5\u4f1a\u8fed\u4ee3\u5730\u505a\u51fa\u4e00\u4e2a\u53c8\u4e00\u4e2a\u7684\u8d2a\u5fc3\u9009\u62e9\uff0c\u6bcf\u8f6e\u90fd\u5c06\u95ee\u9898\u8f6c\u5316\u6210\u4e00\u4e2a\u89c4\u6a21\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u76f4\u5230\u95ee\u9898\u88ab\u89e3\u51b3\u3002
  • \u8d2a\u5fc3\u7b97\u6cd5\u4e0d\u4ec5\u5b9e\u73b0\u7b80\u5355\uff0c\u8fd8\u5177\u6709\u5f88\u9ad8\u7684\u89e3\u9898\u6548\u7387\u3002\u76f8\u6bd4\u4e8e\u52a8\u6001\u89c4\u5212\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u66f4\u4f4e\u3002
  • \u5728\u96f6\u94b1\u5151\u6362\u95ee\u9898\u4e2d\uff0c\u5bf9\u4e8e\u67d0\u4e9b\u786c\u5e01\u7ec4\u5408\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u53ef\u4ee5\u4fdd\u8bc1\u627e\u5230\u6700\u4f18\u89e3\uff1b\u5bf9\u4e8e\u53e6\u5916\u4e00\u4e9b\u786c\u5e01\u7ec4\u5408\u5219\u4e0d\u7136\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u53ef\u80fd\u627e\u5230\u5f88\u5dee\u7684\u89e3\u3002
  • \u9002\u5408\u7528\u8d2a\u5fc3\u7b97\u6cd5\u6c42\u89e3\u7684\u95ee\u9898\u5177\u6709\u4e24\u5927\u6027\u8d28\uff1a\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u548c\u6700\u4f18\u5b50\u7ed3\u6784\u3002\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u4ee3\u8868\u8d2a\u5fc3\u7b56\u7565\u7684\u6709\u6548\u6027\u3002
  • \u5bf9\u4e8e\u67d0\u4e9b\u590d\u6742\u95ee\u9898\uff0c\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u7684\u8bc1\u660e\u5e76\u4e0d\u7b80\u5355\u3002\u76f8\u5bf9\u6765\u8bf4\uff0c\u8bc1\u4f2a\u66f4\u52a0\u5bb9\u6613\uff0c\u4f8b\u5982\u96f6\u94b1\u5151\u6362\u95ee\u9898\u3002
  • \u6c42\u89e3\u8d2a\u5fc3\u95ee\u9898\u4e3b\u8981\u5206\u4e3a\u4e09\u6b65\uff1a\u95ee\u9898\u5206\u6790\u3001\u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a\u3001\u6b63\u786e\u6027\u8bc1\u660e\u3002\u5176\u4e2d\uff0c\u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a\u662f\u6838\u5fc3\u6b65\u9aa4\uff0c\u6b63\u786e\u6027\u8bc1\u660e\u5f80\u5f80\u662f\u96be\u70b9\u3002
  • \u5206\u6570\u80cc\u5305\u95ee\u9898\u5728 0-1 \u80cc\u5305\u7684\u57fa\u7840\u4e0a\uff0c\u5141\u8bb8\u9009\u62e9\u7269\u54c1\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u53ef\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u6c42\u89e3\u3002\u8d2a\u5fc3\u7b56\u7565\u7684\u6b63\u786e\u6027\u53ef\u4ee5\u4f7f\u7528\u53cd\u8bc1\u6cd5\u6765\u8bc1\u660e\u3002
  • \u6700\u5927\u5bb9\u91cf\u95ee\u9898\u53ef\u4f7f\u7528\u7a77\u4e3e\u6cd5\u6c42\u89e3\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002\u901a\u8fc7\u8bbe\u8ba1\u8d2a\u5fc3\u7b56\u7565\uff0c\u6bcf\u8f6e\u5411\u5185\u79fb\u52a8\u77ed\u677f\uff0c\u53ef\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(n)\\) \u3002
  • \u5728\u6700\u5927\u5207\u5206\u4e58\u79ef\u95ee\u9898\u4e2d\uff0c\u6211\u4eec\u5148\u540e\u63a8\u7406\u51fa\u4e24\u4e2a\u8d2a\u5fc3\u7b56\u7565\uff1a\\(\\geq 4\\) \u7684\u6574\u6570\u90fd\u5e94\u8be5\u7ee7\u7eed\u5207\u5206\u3001\u6700\u4f18\u5207\u5206\u56e0\u5b50\u4e3a \\(3\\) \u3002\u4ee3\u7801\u4e2d\u5305\u542b\u5e42\u8fd0\u7b97\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u5e42\u8fd0\u7b97\u5b9e\u73b0\u65b9\u6cd5\uff0c\u901a\u5e38\u4e3a \\(O(1)\\) \u6216 \\(O(\\log n)\\) \u3002
"},{"location":"chapter_hashing/","title":"\u7b2c 6 \u7ae0 \u00a0 \u54c8\u5e0c\u8868","text":"

Abstract

\u5728\u8ba1\u7b97\u673a\u4e16\u754c\u4e2d\uff0c\u54c8\u5e0c\u8868\u5982\u540c\u4e00\u4f4d\u667a\u80fd\u7684\u56fe\u4e66\u7ba1\u7406\u5458\u3002

\u4ed6\u77e5\u9053\u5982\u4f55\u8ba1\u7b97\u7d22\u4e66\u53f7\uff0c\u4ece\u800c\u53ef\u4ee5\u5feb\u901f\u627e\u5230\u76ee\u6807\u4e66\u7c4d\u3002

"},{"location":"chapter_hashing/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 6.1 \u00a0 \u54c8\u5e0c\u8868
  • 6.2 \u00a0 \u54c8\u5e0c\u51b2\u7a81
  • 6.3 \u00a0 \u54c8\u5e0c\u7b97\u6cd5
  • 6.4 \u00a0 \u5c0f\u7ed3
"},{"location":"chapter_hashing/hash_algorithm/","title":"6.3 \u00a0 \u54c8\u5e0c\u7b97\u6cd5","text":"

\u5728\u4e0a\u4e24\u8282\u4e2d\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u54c8\u5e0c\u8868\u7684\u5de5\u4f5c\u539f\u7406\u548c\u54c8\u5e0c\u51b2\u7a81\u7684\u5904\u7406\u65b9\u6cd5\u3002\u7136\u800c\u65e0\u8bba\u662f\u5f00\u653e\u5bfb\u5740\u8fd8\u662f\u94fe\u5730\u5740\u6cd5\uff0c\u5b83\u4eec\u53ea\u80fd\u4fdd\u8bc1\u54c8\u5e0c\u8868\u53ef\u4ee5\u5728\u53d1\u751f\u51b2\u7a81\u65f6\u6b63\u5e38\u5de5\u4f5c\uff0c\u4f46\u65e0\u6cd5\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\u7684\u53d1\u751f\u3002

\u5982\u679c\u54c8\u5e0c\u51b2\u7a81\u8fc7\u4e8e\u9891\u7e41\uff0c\u54c8\u5e0c\u8868\u7684\u6027\u80fd\u5219\u4f1a\u6025\u5267\u52a3\u5316\u3002\u5982\u56fe 6-8 \u6240\u793a\uff0c\u5bf9\u4e8e\u94fe\u5730\u5740\u54c8\u5e0c\u8868\uff0c\u7406\u60f3\u60c5\u51b5\u4e0b\u952e\u503c\u5bf9\u5e73\u5747\u5206\u5e03\u5728\u5404\u4e2a\u6876\u4e2d\uff0c\u8fbe\u5230\u6700\u4f73\u67e5\u8be2\u6548\u7387\uff1b\u6700\u5dee\u60c5\u51b5\u4e0b\u6240\u6709\u952e\u503c\u5bf9\u90fd\u88ab\u5b58\u50a8\u5230\u540c\u4e00\u4e2a\u6876\u4e2d\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u9000\u5316\u81f3 \\(O(n)\\) \u3002

\u56fe 6-8 \u00a0 \u54c8\u5e0c\u51b2\u7a81\u7684\u6700\u4f73\u4e0e\u6700\u5dee\u60c5\u51b5

\u952e\u503c\u5bf9\u7684\u5206\u5e03\u60c5\u51b5\u7531\u54c8\u5e0c\u51fd\u6570\u51b3\u5b9a\u3002\u56de\u5fc6\u54c8\u5e0c\u51fd\u6570\u7684\u8ba1\u7b97\u6b65\u9aa4\uff0c\u5148\u8ba1\u7b97\u54c8\u5e0c\u503c\uff0c\u518d\u5bf9\u6570\u7ec4\u957f\u5ea6\u53d6\u6a21\uff1a

index = hash(key) % capacity\n

\u89c2\u5bdf\u4ee5\u4e0a\u516c\u5f0f\uff0c\u5f53\u54c8\u5e0c\u8868\u5bb9\u91cf capacity \u56fa\u5b9a\u65f6\uff0c\u54c8\u5e0c\u7b97\u6cd5 hash() \u51b3\u5b9a\u4e86\u8f93\u51fa\u503c\uff0c\u8fdb\u800c\u51b3\u5b9a\u4e86\u952e\u503c\u5bf9\u5728\u54c8\u5e0c\u8868\u4e2d\u7684\u5206\u5e03\u60c5\u51b5\u3002

\u8fd9\u610f\u5473\u7740\uff0c\u4e3a\u4e86\u51cf\u5c0f\u54c8\u5e0c\u51b2\u7a81\u7684\u53d1\u751f\u6982\u7387\uff0c\u6211\u4eec\u5e94\u5f53\u5c06\u6ce8\u610f\u529b\u96c6\u4e2d\u5728\u54c8\u5e0c\u7b97\u6cd5 hash() \u7684\u8bbe\u8ba1\u4e0a\u3002

"},{"location":"chapter_hashing/hash_algorithm/#631","title":"6.3.1 \u00a0 \u54c8\u5e0c\u7b97\u6cd5\u7684\u76ee\u6807","text":"

\u4e3a\u4e86\u5b9e\u73b0\u201c\u65e2\u5feb\u53c8\u7a33\u201d\u7684\u54c8\u5e0c\u8868\u6570\u636e\u7ed3\u6784\uff0c\u54c8\u5e0c\u7b97\u6cd5\u5e94\u5305\u542b\u4ee5\u4e0b\u7279\u70b9\u3002

  • \u786e\u5b9a\u6027\uff1a\u5bf9\u4e8e\u76f8\u540c\u7684\u8f93\u5165\uff0c\u54c8\u5e0c\u7b97\u6cd5\u5e94\u59cb\u7ec8\u4ea7\u751f\u76f8\u540c\u7684\u8f93\u51fa\u3002\u8fd9\u6837\u624d\u80fd\u786e\u4fdd\u54c8\u5e0c\u8868\u662f\u53ef\u9760\u7684\u3002
  • \u6548\u7387\u9ad8\uff1a\u8ba1\u7b97\u54c8\u5e0c\u503c\u7684\u8fc7\u7a0b\u5e94\u8be5\u8db3\u591f\u5feb\u3002\u8ba1\u7b97\u5f00\u9500\u8d8a\u5c0f\uff0c\u54c8\u5e0c\u8868\u7684\u5b9e\u7528\u6027\u8d8a\u9ad8\u3002
  • \u5747\u5300\u5206\u5e03\uff1a\u54c8\u5e0c\u7b97\u6cd5\u5e94\u4f7f\u5f97\u952e\u503c\u5bf9\u5e73\u5747\u5206\u5e03\u5728\u54c8\u5e0c\u8868\u4e2d\u3002\u5206\u5e03\u8d8a\u5e73\u5747\uff0c\u54c8\u5e0c\u51b2\u7a81\u7684\u6982\u7387\u5c31\u8d8a\u4f4e\u3002

\u5b9e\u9645\u4e0a\uff0c\u54c8\u5e0c\u7b97\u6cd5\u9664\u4e86\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u54c8\u5e0c\u8868\uff0c\u8fd8\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5176\u4ed6\u9886\u57df\u4e2d\u3002

  • \u5bc6\u7801\u5b58\u50a8\uff1a\u4e3a\u4e86\u4fdd\u62a4\u7528\u6237\u5bc6\u7801\u7684\u5b89\u5168\uff0c\u7cfb\u7edf\u901a\u5e38\u4e0d\u4f1a\u76f4\u63a5\u5b58\u50a8\u7528\u6237\u7684\u660e\u6587\u5bc6\u7801\uff0c\u800c\u662f\u5b58\u50a8\u5bc6\u7801\u7684\u54c8\u5e0c\u503c\u3002\u5f53\u7528\u6237\u8f93\u5165\u5bc6\u7801\u65f6\uff0c\u7cfb\u7edf\u4f1a\u5bf9\u8f93\u5165\u7684\u5bc6\u7801\u8ba1\u7b97\u54c8\u5e0c\u503c\uff0c\u7136\u540e\u4e0e\u5b58\u50a8\u7684\u54c8\u5e0c\u503c\u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679c\u4e24\u8005\u5339\u914d\uff0c\u90a3\u4e48\u5bc6\u7801\u5c31\u88ab\u89c6\u4e3a\u6b63\u786e\u3002
  • \u6570\u636e\u5b8c\u6574\u6027\u68c0\u67e5\uff1a\u6570\u636e\u53d1\u9001\u65b9\u53ef\u4ee5\u8ba1\u7b97\u6570\u636e\u7684\u54c8\u5e0c\u503c\u5e76\u5c06\u5176\u4e00\u540c\u53d1\u9001\uff1b\u63a5\u6536\u65b9\u53ef\u4ee5\u91cd\u65b0\u8ba1\u7b97\u63a5\u6536\u5230\u7684\u6570\u636e\u7684\u54c8\u5e0c\u503c\uff0c\u5e76\u4e0e\u63a5\u6536\u5230\u7684\u54c8\u5e0c\u503c\u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679c\u4e24\u8005\u5339\u914d\uff0c\u90a3\u4e48\u6570\u636e\u5c31\u88ab\u89c6\u4e3a\u5b8c\u6574\u7684\u3002

\u5bf9\u4e8e\u5bc6\u7801\u5b66\u7684\u76f8\u5173\u5e94\u7528\uff0c\u4e3a\u4e86\u9632\u6b62\u4ece\u54c8\u5e0c\u503c\u63a8\u5bfc\u51fa\u539f\u59cb\u5bc6\u7801\u7b49\u9006\u5411\u5de5\u7a0b\uff0c\u54c8\u5e0c\u7b97\u6cd5\u9700\u8981\u5177\u5907\u66f4\u9ad8\u7b49\u7ea7\u7684\u5b89\u5168\u7279\u6027\u3002

  • \u5355\u5411\u6027\uff1a\u65e0\u6cd5\u901a\u8fc7\u54c8\u5e0c\u503c\u53cd\u63a8\u51fa\u5173\u4e8e\u8f93\u5165\u6570\u636e\u7684\u4efb\u4f55\u4fe1\u606f\u3002
  • \u6297\u78b0\u649e\u6027\uff1a\u5e94\u5f53\u6781\u5176\u56f0\u96be\u627e\u5230\u4e24\u4e2a\u4e0d\u540c\u7684\u8f93\u5165\uff0c\u4f7f\u5f97\u5b83\u4eec\u7684\u54c8\u5e0c\u503c\u76f8\u540c\u3002
  • \u96ea\u5d29\u6548\u5e94\uff1a\u8f93\u5165\u7684\u5fae\u5c0f\u53d8\u5316\u5e94\u5f53\u5bfc\u81f4\u8f93\u51fa\u7684\u663e\u8457\u4e14\u4e0d\u53ef\u9884\u6d4b\u7684\u53d8\u5316\u3002

\u8bf7\u6ce8\u610f\uff0c\u201c\u5747\u5300\u5206\u5e03\u201d\u4e0e\u201c\u6297\u78b0\u649e\u6027\u201d\u662f\u4e24\u4e2a\u72ec\u7acb\u7684\u6982\u5ff5\uff0c\u6ee1\u8db3\u5747\u5300\u5206\u5e03\u4e0d\u4e00\u5b9a\u6ee1\u8db3\u6297\u78b0\u649e\u6027\u3002\u4f8b\u5982\uff0c\u5728\u968f\u673a\u8f93\u5165 key \u4e0b\uff0c\u54c8\u5e0c\u51fd\u6570 key % 100 \u53ef\u4ee5\u4ea7\u751f\u5747\u5300\u5206\u5e03\u7684\u8f93\u51fa\u3002\u7136\u800c\u8be5\u54c8\u5e0c\u7b97\u6cd5\u8fc7\u4e8e\u7b80\u5355\uff0c\u6240\u6709\u540e\u4e24\u4f4d\u76f8\u7b49\u7684 key \u7684\u8f93\u51fa\u90fd\u76f8\u540c\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u4ece\u54c8\u5e0c\u503c\u53cd\u63a8\u51fa\u53ef\u7528\u7684 key \uff0c\u4ece\u800c\u7834\u89e3\u5bc6\u7801\u3002

"},{"location":"chapter_hashing/hash_algorithm/#632","title":"6.3.2 \u00a0 \u54c8\u5e0c\u7b97\u6cd5\u7684\u8bbe\u8ba1","text":"

\u54c8\u5e0c\u7b97\u6cd5\u7684\u8bbe\u8ba1\u662f\u4e00\u4e2a\u9700\u8981\u8003\u8651\u8bb8\u591a\u56e0\u7d20\u7684\u590d\u6742\u95ee\u9898\u3002\u7136\u800c\u5bf9\u4e8e\u67d0\u4e9b\u8981\u6c42\u4e0d\u9ad8\u7684\u573a\u666f\uff0c\u6211\u4eec\u4e5f\u80fd\u8bbe\u8ba1\u4e00\u4e9b\u7b80\u5355\u7684\u54c8\u5e0c\u7b97\u6cd5\u3002

  • \u52a0\u6cd5\u54c8\u5e0c\uff1a\u5bf9\u8f93\u5165\u7684\u6bcf\u4e2a\u5b57\u7b26\u7684 ASCII \u7801\u8fdb\u884c\u76f8\u52a0\uff0c\u5c06\u5f97\u5230\u7684\u603b\u548c\u4f5c\u4e3a\u54c8\u5e0c\u503c\u3002
  • \u4e58\u6cd5\u54c8\u5e0c\uff1a\u5229\u7528\u4e86\u4e58\u6cd5\u7684\u4e0d\u76f8\u5173\u6027\uff0c\u6bcf\u8f6e\u4e58\u4ee5\u4e00\u4e2a\u5e38\u6570\uff0c\u5c06\u5404\u4e2a\u5b57\u7b26\u7684 ASCII \u7801\u7d2f\u79ef\u5230\u54c8\u5e0c\u503c\u4e2d\u3002
  • \u5f02\u6216\u54c8\u5e0c\uff1a\u5c06\u8f93\u5165\u6570\u636e\u7684\u6bcf\u4e2a\u5143\u7d20\u901a\u8fc7\u5f02\u6216\u64cd\u4f5c\u7d2f\u79ef\u5230\u4e00\u4e2a\u54c8\u5e0c\u503c\u4e2d\u3002
  • \u65cb\u8f6c\u54c8\u5e0c\uff1a\u5c06\u6bcf\u4e2a\u5b57\u7b26\u7684 ASCII \u7801\u7d2f\u79ef\u5230\u4e00\u4e2a\u54c8\u5e0c\u503c\u4e2d\uff0c\u6bcf\u6b21\u7d2f\u79ef\u4e4b\u524d\u90fd\u4f1a\u5bf9\u54c8\u5e0c\u503c\u8fdb\u884c\u65cb\u8f6c\u64cd\u4f5c\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig simple_hash.py
def add_hash(key: str) -> int:\n\"\"\"\u52a0\u6cd5\u54c8\u5e0c\"\"\"\nhash = 0\nmodulus = 1000000007\nfor c in key:\nhash += ord(c)\nreturn hash % modulus\ndef mul_hash(key: str) -> int:\n\"\"\"\u4e58\u6cd5\u54c8\u5e0c\"\"\"\nhash = 0\nmodulus = 1000000007\nfor c in key:\nhash = 31 * hash + ord(c)\nreturn hash % modulus\ndef xor_hash(key: str) -> int:\n\"\"\"\u5f02\u6216\u54c8\u5e0c\"\"\"\nhash = 0\nmodulus = 1000000007\nfor c in key:\nhash ^= ord(c)\nreturn hash % modulus\ndef rot_hash(key: str) -> int:\n\"\"\"\u65cb\u8f6c\u54c8\u5e0c\"\"\"\nhash = 0\nmodulus = 1000000007\nfor c in key:\nhash = (hash << 4) ^ (hash >> 28) ^ ord(c)\nreturn hash % modulus\n
simple_hash.cpp
/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(string key) {\nlong long hash = 0;\nconst int MODULUS = 1000000007;\nfor (unsigned char c : key) {\nhash = (hash + (int)c) % MODULUS;\n}\nreturn (int)hash;\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(string key) {\nlong long hash = 0;\nconst int MODULUS = 1000000007;\nfor (unsigned char c : key) {\nhash = (31 * hash + (int)c) % MODULUS;\n}\nreturn (int)hash;\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(string key) {\nint hash = 0;\nconst int MODULUS = 1000000007;\nfor (unsigned char c : key) {\nhash ^= (int)c;\n}\nreturn hash & MODULUS;\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(string key) {\nlong long hash = 0;\nconst int MODULUS = 1000000007;\nfor (unsigned char c : key) {\nhash = ((hash << 4) ^ (hash >> 28) ^ (int)c) % MODULUS;\n}\nreturn (int)hash;\n}\n
simple_hash.java
/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(String key) {\nlong hash = 0;\nfinal int MODULUS = 1000000007;\nfor (char c : key.toCharArray()) {\nhash = (hash + (int) c) % MODULUS;\n}\nreturn (int) hash;\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(String key) {\nlong hash = 0;\nfinal int MODULUS = 1000000007;\nfor (char c : key.toCharArray()) {\nhash = (31 * hash + (int) c) % MODULUS;\n}\nreturn (int) hash;\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(String key) {\nint hash = 0;\nfinal int MODULUS = 1000000007;\nfor (char c : key.toCharArray()) {\nhash ^= (int) c;\n}\nreturn hash & MODULUS;\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(String key) {\nlong hash = 0;\nfinal int MODULUS = 1000000007;\nfor (char c : key.toCharArray()) {\nhash = ((hash << 4) ^ (hash >> 28) ^ (int) c) % MODULUS;\n}\nreturn (int) hash;\n}\n
simple_hash.cs
/* \u52a0\u6cd5\u54c8\u5e0c */\nint AddHash(string key) {\nlong hash = 0;\nconst int MODULUS = 1000000007;\nforeach (char c in key) {\nhash = (hash + c) % MODULUS;\n}\nreturn (int)hash;\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint MulHash(string key) {\nlong hash = 0;\nconst int MODULUS = 1000000007;\nforeach (char c in key) {\nhash = (31 * hash + c) % MODULUS;\n}\nreturn (int)hash;\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nint XorHash(string key) {\nint hash = 0;\nconst int MODULUS = 1000000007;\nforeach (char c in key) {\nhash ^= c;\n}\nreturn hash & MODULUS;\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint RotHash(string key) {\nlong hash = 0;\nconst int MODULUS = 1000000007;\nforeach (char c in key) {\nhash = ((hash << 4) ^ (hash >> 28) ^ c) % MODULUS;\n}\nreturn (int)hash;\n}\n
simple_hash.go
/* \u52a0\u6cd5\u54c8\u5e0c */\nfunc addHash(key string) int {\nvar hash int64\nvar modulus int64\nmodulus = 1000000007\nfor _, b := range []byte(key) {\nhash = (hash + int64(b)) % modulus\n}\nreturn int(hash)\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunc mulHash(key string) int {\nvar hash int64\nvar modulus int64\nmodulus = 1000000007\nfor _, b := range []byte(key) {\nhash = (31*hash + int64(b)) % modulus\n}\nreturn int(hash)\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nfunc xorHash(key string) int {\nhash := 0\nmodulus := 1000000007\nfor _, b := range []byte(key) {\nfmt.Println(int(b))\nhash ^= int(b)\nhash = (31*hash + int(b)) % modulus\n}\nreturn hash & modulus\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunc rotHash(key string) int {\nvar hash int64\nvar modulus int64\nmodulus = 1000000007\nfor _, b := range []byte(key) {\nhash = ((hash << 4) ^ (hash >> 28) ^ int64(b)) % modulus\n}\nreturn int(hash)\n}\n
simple_hash.swift
/* \u52a0\u6cd5\u54c8\u5e0c */\nfunc addHash(key: String) -> Int {\nvar hash = 0\nlet MODULUS = 1_000_000_007\nfor c in key {\nfor scalar in c.unicodeScalars {\nhash = (hash + Int(scalar.value)) % MODULUS\n}\n}\nreturn hash\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunc mulHash(key: String) -> Int {\nvar hash = 0\nlet MODULUS = 1_000_000_007\nfor c in key {\nfor scalar in c.unicodeScalars {\nhash = (31 * hash + Int(scalar.value)) % MODULUS\n}\n}\nreturn hash\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nfunc xorHash(key: String) -> Int {\nvar hash = 0\nlet MODULUS = 1_000_000_007\nfor c in key {\nfor scalar in c.unicodeScalars {\nhash ^= Int(scalar.value)\n}\n}\nreturn hash & MODULUS\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunc rotHash(key: String) -> Int {\nvar hash = 0\nlet MODULUS = 1_000_000_007\nfor c in key {\nfor scalar in c.unicodeScalars {\nhash = ((hash << 4) ^ (hash >> 28) ^ Int(scalar.value)) % MODULUS\n}\n}\nreturn hash\n}\n
simple_hash.js
/* \u52a0\u6cd5\u54c8\u5e0c */\nfunction addHash(key) {\nlet hash = 0;\nconst MODULUS = 1000000007;\nfor (const c of key) {\nhash = (hash + c.charCodeAt(0)) % MODULUS;\n}\nreturn hash;\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunction mulHash(key) {\nlet hash = 0;\nconst MODULUS = 1000000007;\nfor (const c of key) {\nhash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n}\nreturn hash;\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nfunction xorHash(key) {\nlet hash = 0;\nconst MODULUS = 1000000007;\nfor (const c of key) {\nhash ^= c.charCodeAt(0);\n}\nreturn hash & MODULUS;\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunction rotHash(key) {\nlet hash = 0;\nconst MODULUS = 1000000007;\nfor (const c of key) {\nhash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;\n}\nreturn hash;\n}\n
simple_hash.ts
/* \u52a0\u6cd5\u54c8\u5e0c */\nfunction addHash(key: string): number {\nlet hash = 0;\nconst MODULUS = 1000000007;\nfor (const c of key) {\nhash = (hash + c.charCodeAt(0)) % MODULUS;\n}\nreturn hash;\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunction mulHash(key: string): number {\nlet hash = 0;\nconst MODULUS = 1000000007;\nfor (const c of key) {\nhash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n}\nreturn hash;\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nfunction xorHash(key: string): number {\nlet hash = 0;\nconst MODULUS = 1000000007;\nfor (const c of key) {\nhash ^= c.charCodeAt(0);\n}\nreturn hash & MODULUS;\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunction rotHash(key: string): number {\nlet hash = 0;\nconst MODULUS = 1000000007;\nfor (const c of key) {\nhash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;\n}\nreturn hash;\n}\n
simple_hash.dart
/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(String key) {\nint hash = 0;\nfinal int MODULUS = 1000000007;\nfor (int i = 0; i < key.length; i++) {\nhash = (hash + key.codeUnitAt(i)) % MODULUS;\n}\nreturn hash;\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(String key) {\nint hash = 0;\nfinal int MODULUS = 1000000007;\nfor (int i = 0; i < key.length; i++) {\nhash = (31 * hash + key.codeUnitAt(i)) % MODULUS;\n}\nreturn hash;\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(String key) {\nint hash = 0;\nfinal int MODULUS = 1000000007;\nfor (int i = 0; i < key.length; i++) {\nhash ^= key.codeUnitAt(i);\n}\nreturn hash & MODULUS;\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(String key) {\nint hash = 0;\nfinal int MODULUS = 1000000007;\nfor (int i = 0; i < key.length; i++) {\nhash = ((hash << 4) ^ (hash >> 28) ^ key.codeUnitAt(i)) % MODULUS;\n}\nreturn hash;\n}\n
simple_hash.rs
/* \u52a0\u6cd5\u54c8\u5e0c */\nfn add_hash(key: &str) -> i32 {\nlet mut hash = 0_i64;\nconst MODULUS: i64 = 1000000007;\nfor c in key.chars() {\nhash = (hash + c as i64) % MODULUS;\n}\nhash as i32\n} /* \u4e58\u6cd5\u54c8\u5e0c */\nfn mul_hash(key: &str) -> i32 {\nlet mut hash = 0_i64;\nconst MODULUS: i64 = 1000000007;\nfor c in key.chars() {\nhash = (31 * hash + c as i64) % MODULUS;\n}\nhash as i32\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nfn xor_hash(key: &str) -> i32 {\nlet mut hash = 0_i64;\nconst MODULUS: i64 = 1000000007;\nfor c in key.chars() {\nhash ^= c as i64;\n}\n(hash & MODULUS) as i32\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfn rot_hash(key: &str) -> i32 {\nlet mut hash = 0_i64;\nconst MODULUS: i64 = 1000000007;\nfor c in key.chars() {\nhash = ((hash << 4) ^ (hash >> 28) ^ c as i64) % MODULUS;\n}\nhash as i32\n}\n
simple_hash.c
/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(char *key) {\nlong long hash = 0;\nconst int MODULUS = 1000000007;\nfor (int i = 0; i < strlen(key); i++) {\nhash = (hash + (unsigned char)key[i]) % MODULUS;\n}\nreturn (int)hash;\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(char *key) {\nlong long hash = 0;\nconst int MODULUS = 1000000007;\nfor (int i = 0; i < strlen(key); i++) {\nhash = (31 * hash + (unsigned char)key[i]) % MODULUS;\n}\nreturn (int)hash;\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(char *key) {\nint hash = 0;\nconst int MODULUS = 1000000007;\nfor (int i = 0; i < strlen(key); i++) {\nhash ^= (unsigned char)key[i];\n}\nreturn hash & MODULUS;\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(char *key) {\nlong long hash = 0;\nconst int MODULUS = 1000000007;\nfor (int i = 0; i < strlen(key); i++) {\nhash = ((hash << 4) ^ (hash >> 28) ^ (unsigned char)key[i]) % MODULUS;\n}\nreturn (int)hash;\n}\n
simple_hash.zig
[class]{}-[func]{addHash}\n[class]{}-[func]{mulHash}\n[class]{}-[func]{xorHash}\n[class]{}-[func]{rotHash}\n

\u89c2\u5bdf\u53d1\u73b0\uff0c\u6bcf\u79cd\u54c8\u5e0c\u7b97\u6cd5\u7684\u6700\u540e\u4e00\u6b65\u90fd\u662f\u5bf9\u5927\u8d28\u6570 \\(1000000007\\) \u53d6\u6a21\uff0c\u4ee5\u786e\u4fdd\u54c8\u5e0c\u503c\u5728\u5408\u9002\u7684\u8303\u56f4\u5185\u3002\u503c\u5f97\u601d\u8003\u7684\u662f\uff0c\u4e3a\u4ec0\u4e48\u8981\u5f3a\u8c03\u5bf9\u8d28\u6570\u53d6\u6a21\uff0c\u6216\u8005\u8bf4\u5bf9\u5408\u6570\u53d6\u6a21\u7684\u5f0a\u7aef\u662f\u4ec0\u4e48\uff1f\u8fd9\u662f\u4e00\u4e2a\u6709\u8da3\u7684\u95ee\u9898\u3002

\u5148\u629b\u51fa\u7ed3\u8bba\uff1a\u5f53\u6211\u4eec\u4f7f\u7528\u5927\u8d28\u6570\u4f5c\u4e3a\u6a21\u6570\u65f6\uff0c\u53ef\u4ee5\u6700\u5927\u5316\u5730\u4fdd\u8bc1\u54c8\u5e0c\u503c\u7684\u5747\u5300\u5206\u5e03\u3002\u56e0\u4e3a\u8d28\u6570\u4e0d\u4f1a\u4e0e\u5176\u4ed6\u6570\u5b57\u5b58\u5728\u516c\u7ea6\u6570\uff0c\u53ef\u4ee5\u51cf\u5c11\u56e0\u53d6\u6a21\u64cd\u4f5c\u800c\u4ea7\u751f\u7684\u5468\u671f\u6027\u6a21\u5f0f\uff0c\u4ece\u800c\u907f\u514d\u54c8\u5e0c\u51b2\u7a81\u3002

\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u6211\u4eec\u9009\u62e9\u5408\u6570 \\(9\\) \u4f5c\u4e3a\u6a21\u6570\uff0c\u5b83\u53ef\u4ee5\u88ab \\(3\\) \u6574\u9664\u3002\u90a3\u4e48\u6240\u6709\u53ef\u4ee5\u88ab \\(3\\) \u6574\u9664\u7684 key \u90fd\u4f1a\u88ab\u6620\u5c04\u5230 \\(0\\)\u3001\\(3\\)\u3001\\(6\\) \u8fd9\u4e09\u4e2a\u54c8\u5e0c\u503c\u3002

\\[ \\begin{aligned} \\text{modulus} & = 9 \\newline \\text{key} & = \\{ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, \\dots \\} \\newline \\text{hash} & = \\{ 0, 3, 6, 0, 3, 6, 0, 3, 6, 0, 3, 6,\\dots \\} \\end{aligned} \\]

\u5982\u679c\u8f93\u5165 key \u6070\u597d\u6ee1\u8db3\u8fd9\u79cd\u7b49\u5dee\u6570\u5217\u7684\u6570\u636e\u5206\u5e03\uff0c\u90a3\u4e48\u54c8\u5e0c\u503c\u5c31\u4f1a\u51fa\u73b0\u805a\u5806\uff0c\u4ece\u800c\u52a0\u91cd\u54c8\u5e0c\u51b2\u7a81\u3002\u73b0\u5728\uff0c\u5047\u8bbe\u5c06 modulus \u66ff\u6362\u4e3a\u8d28\u6570 \\(13\\) \uff0c\u7531\u4e8e key \u548c modulus \u4e4b\u95f4\u4e0d\u5b58\u5728\u516c\u7ea6\u6570\uff0c\u8f93\u51fa\u7684\u54c8\u5e0c\u503c\u7684\u5747\u5300\u6027\u4f1a\u660e\u663e\u63d0\u5347\u3002

\\[ \\begin{aligned} \\text{modulus} & = 13 \\newline \\text{key} & = \\{ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, \\dots \\} \\newline \\text{hash} & = \\{ 0, 3, 6, 9, 12, 2, 5, 8, 11, 1, 4, 7, \\dots \\} \\end{aligned} \\]

\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u5982\u679c\u80fd\u591f\u4fdd\u8bc1 key \u662f\u968f\u673a\u5747\u5300\u5206\u5e03\u7684\uff0c\u90a3\u4e48\u9009\u62e9\u8d28\u6570\u6216\u8005\u5408\u6570\u4f5c\u4e3a\u6a21\u6570\u90fd\u662f\u53ef\u4ee5\u7684\uff0c\u5b83\u4eec\u90fd\u80fd\u8f93\u51fa\u5747\u5300\u5206\u5e03\u7684\u54c8\u5e0c\u503c\u3002\u800c\u5f53 key \u7684\u5206\u5e03\u5b58\u5728\u67d0\u79cd\u5468\u671f\u6027\u65f6\uff0c\u5bf9\u5408\u6570\u53d6\u6a21\u66f4\u5bb9\u6613\u51fa\u73b0\u805a\u96c6\u73b0\u8c61\u3002

\u603b\u800c\u8a00\u4e4b\uff0c\u6211\u4eec\u901a\u5e38\u9009\u53d6\u8d28\u6570\u4f5c\u4e3a\u6a21\u6570\uff0c\u5e76\u4e14\u8fd9\u4e2a\u8d28\u6570\u6700\u597d\u8db3\u591f\u5927\uff0c\u4ee5\u5c3d\u53ef\u80fd\u6d88\u9664\u5468\u671f\u6027\u6a21\u5f0f\uff0c\u63d0\u5347\u54c8\u5e0c\u7b97\u6cd5\u7684\u7a33\u5065\u6027\u3002

"},{"location":"chapter_hashing/hash_algorithm/#633","title":"6.3.3 \u00a0 \u5e38\u89c1\u54c8\u5e0c\u7b97\u6cd5","text":"

\u4e0d\u96be\u53d1\u73b0\uff0c\u4ee5\u4e0a\u4ecb\u7ecd\u7684\u7b80\u5355\u54c8\u5e0c\u7b97\u6cd5\u90fd\u6bd4\u8f83\u201c\u8106\u5f31\u201d\uff0c\u8fdc\u8fdc\u6ca1\u6709\u8fbe\u5230\u54c8\u5e0c\u7b97\u6cd5\u7684\u8bbe\u8ba1\u76ee\u6807\u3002\u4f8b\u5982\uff0c\u7531\u4e8e\u52a0\u6cd5\u548c\u5f02\u6216\u6ee1\u8db3\u4ea4\u6362\u5f8b\uff0c\u56e0\u6b64\u52a0\u6cd5\u54c8\u5e0c\u548c\u5f02\u6216\u54c8\u5e0c\u65e0\u6cd5\u533a\u5206\u5185\u5bb9\u76f8\u540c\u4f46\u987a\u5e8f\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\uff0c\u8fd9\u53ef\u80fd\u4f1a\u52a0\u5267\u54c8\u5e0c\u51b2\u7a81\uff0c\u5e76\u5f15\u8d77\u4e00\u4e9b\u5b89\u5168\u95ee\u9898\u3002

\u5728\u5b9e\u9645\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u7528\u4e00\u4e9b\u6807\u51c6\u54c8\u5e0c\u7b97\u6cd5\uff0c\u4f8b\u5982 MD5\u3001SHA-1\u3001SHA-2\u3001SHA3 \u7b49\u3002\u5b83\u4eec\u53ef\u4ee5\u5c06\u4efb\u610f\u957f\u5ea6\u7684\u8f93\u5165\u6570\u636e\u6620\u5c04\u5230\u6052\u5b9a\u957f\u5ea6\u7684\u54c8\u5e0c\u503c\u3002

\u8fd1\u4e00\u4e2a\u4e16\u7eaa\u4ee5\u6765\uff0c\u54c8\u5e0c\u7b97\u6cd5\u5904\u5728\u4e0d\u65ad\u5347\u7ea7\u4e0e\u4f18\u5316\u7684\u8fc7\u7a0b\u4e2d\u3002\u4e00\u90e8\u5206\u7814\u7a76\u4eba\u5458\u52aa\u529b\u63d0\u5347\u54c8\u5e0c\u7b97\u6cd5\u7684\u6027\u80fd\uff0c\u53e6\u4e00\u90e8\u5206\u7814\u7a76\u4eba\u5458\u548c\u9ed1\u5ba2\u5219\u81f4\u529b\u4e8e\u5bfb\u627e\u54c8\u5e0c\u7b97\u6cd5\u7684\u5b89\u5168\u6027\u95ee\u9898\u3002\u8868 6-2 \u5c55\u793a\u4e86\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u5e38\u89c1\u7684\u54c8\u5e0c\u7b97\u6cd5\u3002

  • MD5 \u548c SHA-1 \u5df2\u591a\u6b21\u88ab\u6210\u529f\u653b\u51fb\uff0c\u56e0\u6b64\u5b83\u4eec\u88ab\u5404\u7c7b\u5b89\u5168\u5e94\u7528\u5f03\u7528\u3002
  • SHA-2 \u7cfb\u5217\u4e2d\u7684 SHA-256 \u662f\u6700\u5b89\u5168\u7684\u54c8\u5e0c\u7b97\u6cd5\u4e4b\u4e00\uff0c\u4ecd\u672a\u51fa\u73b0\u6210\u529f\u7684\u653b\u51fb\u6848\u4f8b\uff0c\u56e0\u6b64\u5e38\u88ab\u7528\u5728\u5404\u7c7b\u5b89\u5168\u5e94\u7528\u4e0e\u534f\u8bae\u4e2d\u3002
  • SHA-3 \u76f8\u8f83 SHA-2 \u7684\u5b9e\u73b0\u5f00\u9500\u66f4\u4f4e\u3001\u8ba1\u7b97\u6548\u7387\u66f4\u9ad8\uff0c\u4f46\u76ee\u524d\u4f7f\u7528\u8986\u76d6\u5ea6\u4e0d\u5982 SHA-2 \u7cfb\u5217\u3002

\u8868 6-2 \u00a0 \u5e38\u89c1\u7684\u54c8\u5e0c\u7b97\u6cd5

MD5 SHA-1 SHA-2 SHA-3 \u63a8\u51fa\u65f6\u95f4 1992 1995 2002 2008 \u8f93\u51fa\u957f\u5ea6 128 bits 160 bits 256 / 512 bits 224/256/384/512 bits \u54c8\u5e0c\u51b2\u7a81 \u8f83\u591a \u8f83\u591a \u5f88\u5c11 \u5f88\u5c11 \u5b89\u5168\u7b49\u7ea7 \u4f4e\uff0c\u5df2\u88ab\u6210\u529f\u653b\u51fb \u4f4e\uff0c\u5df2\u88ab\u6210\u529f\u653b\u51fb \u9ad8 \u9ad8 \u5e94\u7528 \u5df2\u88ab\u5f03\u7528\uff0c\u4ecd\u7528\u4e8e\u6570\u636e\u5b8c\u6574\u6027\u68c0\u67e5 \u5df2\u88ab\u5f03\u7528 \u52a0\u5bc6\u8d27\u5e01\u4ea4\u6613\u9a8c\u8bc1\u3001\u6570\u5b57\u7b7e\u540d\u7b49 \u53ef\u7528\u4e8e\u66ff\u4ee3 SHA-2"},{"location":"chapter_hashing/hash_algorithm/#634","title":"6.3.4 \u00a0 \u6570\u636e\u7ed3\u6784\u7684\u54c8\u5e0c\u503c","text":"

\u6211\u4eec\u77e5\u9053\uff0c\u54c8\u5e0c\u8868\u7684 key \u53ef\u4ee5\u662f\u6574\u6570\u3001\u5c0f\u6570\u6216\u5b57\u7b26\u4e32\u7b49\u6570\u636e\u7c7b\u578b\u3002\u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u4f1a\u4e3a\u8fd9\u4e9b\u6570\u636e\u7c7b\u578b\u63d0\u4f9b\u5185\u7f6e\u7684\u54c8\u5e0c\u7b97\u6cd5\uff0c\u7528\u4e8e\u8ba1\u7b97\u54c8\u5e0c\u8868\u4e2d\u7684\u6876\u7d22\u5f15\u3002\u4ee5 Python \u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528 hash() \u51fd\u6570\u6765\u8ba1\u7b97\u5404\u79cd\u6570\u636e\u7c7b\u578b\u7684\u54c8\u5e0c\u503c\u3002

  • \u6574\u6570\u548c\u5e03\u5c14\u91cf\u7684\u54c8\u5e0c\u503c\u5c31\u662f\u5176\u672c\u8eab\u3002
  • \u6d6e\u70b9\u6570\u548c\u5b57\u7b26\u4e32\u7684\u54c8\u5e0c\u503c\u8ba1\u7b97\u8f83\u4e3a\u590d\u6742\uff0c\u6709\u5174\u8da3\u7684\u540c\u5b66\u8bf7\u81ea\u884c\u5b66\u4e60\u3002
  • \u5143\u7ec4\u7684\u54c8\u5e0c\u503c\u662f\u5bf9\u5176\u4e2d\u6bcf\u4e00\u4e2a\u5143\u7d20\u8fdb\u884c\u54c8\u5e0c\uff0c\u7136\u540e\u5c06\u8fd9\u4e9b\u54c8\u5e0c\u503c\u7ec4\u5408\u8d77\u6765\uff0c\u5f97\u5230\u5355\u4e00\u7684\u54c8\u5e0c\u503c\u3002
  • \u5bf9\u8c61\u7684\u54c8\u5e0c\u503c\u57fa\u4e8e\u5176\u5185\u5b58\u5730\u5740\u751f\u6210\u3002\u901a\u8fc7\u91cd\u5199\u5bf9\u8c61\u7684\u54c8\u5e0c\u65b9\u6cd5\uff0c\u53ef\u5b9e\u73b0\u57fa\u4e8e\u5185\u5bb9\u751f\u6210\u54c8\u5e0c\u503c\u3002

Tip

\u8bf7\u6ce8\u610f\uff0c\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u7684\u5185\u7f6e\u54c8\u5e0c\u503c\u8ba1\u7b97\u51fd\u6570\u7684\u5b9a\u4e49\u548c\u65b9\u6cd5\u4e0d\u540c\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig built_in_hash.py
num = 3\nhash_num = hash(num)\n# \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3\nbol = True\nhash_bol = hash(bol)\n# \u5e03\u5c14\u91cf True \u7684\u54c8\u5e0c\u503c\u4e3a 1\ndec = 3.14159\nhash_dec = hash(dec)\n# \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 326484311674566659\nstr = \"Hello \u7b97\u6cd5\"\nhash_str = hash(str)\n# \u5b57\u7b26\u4e32 Hello \u7b97\u6cd5 \u7684\u54c8\u5e0c\u503c\u4e3a 4617003410720528961\ntup = (12836, \"\u5c0f\u54c8\")\nhash_tup = hash(tup)\n# \u5143\u7ec4 (12836, '\u5c0f\u54c8') \u7684\u54c8\u5e0c\u503c\u4e3a 1029005403108185979\nobj = ListNode(0)\nhash_obj = hash(obj)\n# \u8282\u70b9\u5bf9\u8c61 <ListNode object at 0x1058fd810> \u7684\u54c8\u5e0c\u503c\u4e3a 274267521\n
built_in_hash.cpp
int num = 3;\nsize_t hashNum = hash<int>()(num);\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3\nbool bol = true;\nsize_t hashBol = hash<bool>()(bol);\n// \u5e03\u5c14\u91cf 1 \u7684\u54c8\u5e0c\u503c\u4e3a 1\ndouble dec = 3.14159;\nsize_t hashDec = hash<double>()(dec);\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 4614256650576692846\nstring str = \"Hello \u7b97\u6cd5\";\nsize_t hashStr = hash<string>()(str);\n// \u5b57\u7b26\u4e32 Hello \u7b97\u6cd5 \u7684\u54c8\u5e0c\u503c\u4e3a 15466937326284535026\n// \u5728 C++ \u4e2d\uff0c\u5185\u7f6e std:hash() \u4ec5\u63d0\u4f9b\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u54c8\u5e0c\u503c\u8ba1\u7b97\n// \u6570\u7ec4\u3001\u5bf9\u8c61\u7684\u54c8\u5e0c\u503c\u8ba1\u7b97\u9700\u8981\u81ea\u884c\u5b9e\u73b0\n
built_in_hash.java
int num = 3;\nint hashNum = Integer.hashCode(num);\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3\nboolean bol = true;\nint hashBol = Boolean.hashCode(bol);\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a 1231\ndouble dec = 3.14159;\nint hashDec = Double.hashCode(dec);\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a -1340954729\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode();\n// \u5b57\u7b26\u4e32 Hello \u7b97\u6cd5 \u7684\u54c8\u5e0c\u503c\u4e3a -727081396\nObject[] arr = { 12836, \"\u5c0f\u54c8\" };\nint hashTup = Arrays.hashCode(arr);\n// \u6570\u7ec4 [12836, \u5c0f\u54c8] \u7684\u54c8\u5e0c\u503c\u4e3a 1151158\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode();\n// \u8282\u70b9\u5bf9\u8c61 utils.ListNode@7dc5e7b4 \u7684\u54c8\u5e0c\u503c\u4e3a 2110121908\n
built_in_hash.cs
int num = 3;\nint hashNum = num.GetHashCode();\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3;\nbool bol = true;\nint hashBol = bol.GetHashCode();\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a 1;\ndouble dec = 3.14159;\nint hashDec = dec.GetHashCode();\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a -1340954729;\nstring str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.GetHashCode();\n// \u5b57\u7b26\u4e32 Hello \u7b97\u6cd5 \u7684\u54c8\u5e0c\u503c\u4e3a -586107568;\nobject[] arr = { 12836, \"\u5c0f\u54c8\" };\nint hashTup = arr.GetHashCode();\n// \u6570\u7ec4 [12836, \u5c0f\u54c8] \u7684\u54c8\u5e0c\u503c\u4e3a 42931033;\nListNode obj = new(0);\nint hashObj = obj.GetHashCode();\n// \u8282\u70b9\u5bf9\u8c61 0 \u7684\u54c8\u5e0c\u503c\u4e3a 39053774;\n
built_in_hash.go
\n
built_in_hash.swift
let num = 3\nlet hashNum = num.hashValue\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 9047044699613009734\nlet bol = true\nlet hashBol = bol.hashValue\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a -4431640247352757451\nlet dec = 3.14159\nlet hashDec = dec.hashValue\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a -2465384235396674631\nlet str = \"Hello \u7b97\u6cd5\"\nlet hashStr = str.hashValue\n// \u5b57\u7b26\u4e32 Hello \u7b97\u6cd5 \u7684\u54c8\u5e0c\u503c\u4e3a -7850626797806988787\nlet arr = [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")]\nlet hashTup = arr.hashValue\n// \u6570\u7ec4 [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")] \u7684\u54c8\u5e0c\u503c\u4e3a -2308633508154532996\nlet obj = ListNode(x: 0)\nlet hashObj = obj.hashValue\n// \u8282\u70b9\u5bf9\u8c61 utils.ListNode \u7684\u54c8\u5e0c\u503c\u4e3a -2434780518035996159\n
built_in_hash.js
// JavaScript \u672a\u63d0\u4f9b\u5185\u7f6e hash code \u51fd\u6570\n
built_in_hash.ts
// TypeScript \u672a\u63d0\u4f9b\u5185\u7f6e hash code \u51fd\u6570\n
built_in_hash.dart
int num = 3;\nint hashNum = num.hashCode;\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 34803\nbool bol = true;\nint hashBol = bol.hashCode;\n// \u5e03\u5c14\u503c true \u7684\u54c8\u5e0c\u503c\u4e3a 1231\ndouble dec = 3.14159;\nint hashDec = dec.hashCode;\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 2570631074981783\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode;\n// \u5b57\u7b26\u4e32 Hello \u7b97\u6cd5 \u7684\u54c8\u5e0c\u503c\u4e3a 468167534\nList arr = [12836, \"\u5c0f\u54c8\"];\nint hashArr = arr.hashCode;\n// \u6570\u7ec4 [12836, \u5c0f\u54c8] \u7684\u54c8\u5e0c\u503c\u4e3a 976512528\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode;\n// \u8282\u70b9\u5bf9\u8c61 Instance of 'ListNode' \u7684\u54c8\u5e0c\u503c\u4e3a 1033450432\n
built_in_hash.rs
use std::collections::hash_map::DefaultHasher;\nuse std::hash::{Hash, Hasher};\nlet num = 3;\nlet mut num_hasher = DefaultHasher::new();\nnum.hash(&mut num_hasher);\nlet hash_num = num_hasher.finish();\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 568126464209439262\nlet bol = true;\nlet mut bol_hasher = DefaultHasher::new();\nbol.hash(&mut bol_hasher);\nlet hash_bol = bol_hasher.finish();\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a 4952851536318644461\nlet dec: f32 = 3.14159;\nlet mut dec_hasher = DefaultHasher::new();\ndec.to_bits().hash(&mut dec_hasher);\nlet hash_dec = dec_hasher.finish();\nprintln!(\"\u5c0f\u6570 {} \u7684\u54c8\u5e0c\u503c\u4e3a {}\", dec, hash_dec);\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 2566941990314602357\nlet str = \"Hello \u7b97\u6cd5\";\nlet mut str_hasher = DefaultHasher::new();\nstr.hash(&mut str_hasher);\nlet hash_str = str_hasher.finish();\n// \u5b57\u7b26\u4e32 Hello \u7b97\u6cd5 \u7684\u54c8\u5e0c\u503c\u4e3a 16092673739211250988\nlet arr = (&12836, &\"\u5c0f\u54c8\");\nlet mut tup_hasher = DefaultHasher::new();\narr.hash(&mut tup_hasher);\nlet hash_tup = tup_hasher.finish();\n// \u5143\u7ec4 (12836, \"\u5c0f\u54c8\") \u7684\u54c8\u5e0c\u503c\u4e3a 1885128010422702749\nlet node = ListNode::new(42);\nlet mut hasher = DefaultHasher::new();\nnode.borrow().val.hash(&mut hasher);\nlet hash = hasher.finish();\n// \u8282\u70b9\u5bf9\u8c61 RefCell { value: ListNode { val: 42, next: None } } \u7684\u54c8\u5e0c\u503c\u4e3a15387811073369036852\n
built_in_hash.c
// C \u672a\u63d0\u4f9b\u5185\u7f6e hash code \u51fd\u6570\n
built_in_hash.zig
\n

\u5728\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u53ea\u6709\u4e0d\u53ef\u53d8\u5bf9\u8c61\u624d\u53ef\u4f5c\u4e3a\u54c8\u5e0c\u8868\u7684 key \u3002\u5047\u5982\u6211\u4eec\u5c06\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\u4f5c\u4e3a key \uff0c\u5f53\u5217\u8868\u7684\u5185\u5bb9\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u5b83\u7684\u54c8\u5e0c\u503c\u4e5f\u968f\u4e4b\u6539\u53d8\uff0c\u6211\u4eec\u5c31\u65e0\u6cd5\u5728\u54c8\u5e0c\u8868\u4e2d\u67e5\u8be2\u5230\u539f\u5148\u7684 value \u4e86\u3002

\u867d\u7136\u81ea\u5b9a\u4e49\u5bf9\u8c61\uff08\u6bd4\u5982\u94fe\u8868\u8282\u70b9\uff09\u7684\u6210\u5458\u53d8\u91cf\u662f\u53ef\u53d8\u7684\uff0c\u4f46\u5b83\u662f\u53ef\u54c8\u5e0c\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u5bf9\u8c61\u7684\u54c8\u5e0c\u503c\u901a\u5e38\u662f\u57fa\u4e8e\u5185\u5b58\u5730\u5740\u751f\u6210\u7684\uff0c\u5373\u4f7f\u5bf9\u8c61\u7684\u5185\u5bb9\u53d1\u751f\u4e86\u53d8\u5316\uff0c\u4f46\u5b83\u7684\u5185\u5b58\u5730\u5740\u4e0d\u53d8\uff0c\u54c8\u5e0c\u503c\u4ecd\u7136\u662f\u4e0d\u53d8\u7684\u3002

\u7ec6\u5fc3\u7684\u4f60\u53ef\u80fd\u53d1\u73b0\u5728\u4e0d\u540c\u63a7\u5236\u53f0\u4e2d\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u8f93\u51fa\u7684\u54c8\u5e0c\u503c\u662f\u4e0d\u540c\u7684\u3002\u8fd9\u662f\u56e0\u4e3a Python \u89e3\u91ca\u5668\u5728\u6bcf\u6b21\u542f\u52a8\u65f6\uff0c\u90fd\u4f1a\u4e3a\u5b57\u7b26\u4e32\u54c8\u5e0c\u51fd\u6570\u52a0\u5165\u4e00\u4e2a\u968f\u673a\u7684\u76d0\uff08Salt\uff09\u503c\u3002\u8fd9\u79cd\u505a\u6cd5\u53ef\u4ee5\u6709\u6548\u9632\u6b62 HashDoS \u653b\u51fb\uff0c\u63d0\u5347\u54c8\u5e0c\u7b97\u6cd5\u7684\u5b89\u5168\u6027\u3002

"},{"location":"chapter_hashing/hash_collision/","title":"6.2 \u00a0 \u54c8\u5e0c\u51b2\u7a81","text":"

\u4e0a\u8282\u63d0\u5230\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u54c8\u5e0c\u51fd\u6570\u7684\u8f93\u5165\u7a7a\u95f4\u8fdc\u5927\u4e8e\u8f93\u51fa\u7a7a\u95f4\uff0c\u56e0\u6b64\u7406\u8bba\u4e0a\u54c8\u5e0c\u51b2\u7a81\u662f\u4e0d\u53ef\u907f\u514d\u7684\u3002\u6bd4\u5982\uff0c\u8f93\u5165\u7a7a\u95f4\u4e3a\u5168\u4f53\u6574\u6570\uff0c\u8f93\u51fa\u7a7a\u95f4\u4e3a\u6570\u7ec4\u5bb9\u91cf\u5927\u5c0f\uff0c\u5219\u5fc5\u7136\u6709\u591a\u4e2a\u6574\u6570\u6620\u5c04\u81f3\u540c\u4e00\u6876\u7d22\u5f15\u3002

\u54c8\u5e0c\u51b2\u7a81\u4f1a\u5bfc\u81f4\u67e5\u8be2\u7ed3\u679c\u9519\u8bef\uff0c\u4e25\u91cd\u5f71\u54cd\u54c8\u5e0c\u8868\u7684\u53ef\u7528\u6027\u3002\u4e3a\u89e3\u51b3\u8be5\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u6bcf\u5f53\u9047\u5230\u54c8\u5e0c\u51b2\u7a81\u65f6\u5c31\u8fdb\u884c\u54c8\u5e0c\u8868\u6269\u5bb9\uff0c\u76f4\u81f3\u51b2\u7a81\u6d88\u5931\u4e3a\u6b62\u3002\u6b64\u65b9\u6cd5\u7b80\u5355\u7c97\u66b4\u4e14\u6709\u6548\uff0c\u4f46\u6548\u7387\u592a\u4f4e\uff0c\u56e0\u4e3a\u54c8\u5e0c\u8868\u6269\u5bb9\u9700\u8981\u8fdb\u884c\u5927\u91cf\u7684\u6570\u636e\u642c\u8fd0\u4e0e\u54c8\u5e0c\u503c\u8ba1\u7b97\u3002\u4e3a\u4e86\u63d0\u5347\u6548\u7387\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u7b56\u7565\u3002

  1. \u6539\u826f\u54c8\u5e0c\u8868\u6570\u636e\u7ed3\u6784\uff0c\u4f7f\u5f97\u54c8\u5e0c\u8868\u53ef\u4ee5\u5728\u5b58\u5728\u54c8\u5e0c\u51b2\u7a81\u65f6\u6b63\u5e38\u5de5\u4f5c\u3002
  2. \u4ec5\u5728\u5fc5\u8981\u65f6\uff0c\u5373\u5f53\u54c8\u5e0c\u51b2\u7a81\u6bd4\u8f83\u4e25\u91cd\u65f6\uff0c\u624d\u6267\u884c\u6269\u5bb9\u64cd\u4f5c\u3002

\u54c8\u5e0c\u8868\u7684\u7ed3\u6784\u6539\u826f\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u201c\u94fe\u5f0f\u5730\u5740\u201d\u548c\u201c\u5f00\u653e\u5bfb\u5740\u201d\u3002

"},{"location":"chapter_hashing/hash_collision/#621","title":"6.2.1 \u00a0 \u94fe\u5f0f\u5730\u5740","text":"

\u5728\u539f\u59cb\u54c8\u5e0c\u8868\u4e2d\uff0c\u6bcf\u4e2a\u6876\u4ec5\u80fd\u5b58\u50a8\u4e00\u4e2a\u952e\u503c\u5bf9\u3002\u300c\u94fe\u5f0f\u5730\u5740 separate chaining\u300d\u5c06\u5355\u4e2a\u5143\u7d20\u8f6c\u6362\u4e3a\u94fe\u8868\uff0c\u5c06\u952e\u503c\u5bf9\u4f5c\u4e3a\u94fe\u8868\u8282\u70b9\uff0c\u5c06\u6240\u6709\u53d1\u751f\u51b2\u7a81\u7684\u952e\u503c\u5bf9\u90fd\u5b58\u50a8\u5728\u540c\u4e00\u94fe\u8868\u4e2d\u3002\u56fe 6-5 \u5c55\u793a\u4e86\u4e00\u4e2a\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\u7684\u4f8b\u5b50\u3002

\u56fe 6-5 \u00a0 \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868

\u57fa\u4e8e\u94fe\u5f0f\u5730\u5740\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868\u7684\u64cd\u4f5c\u65b9\u6cd5\u53d1\u751f\u4e86\u4ee5\u4e0b\u53d8\u5316\u3002

  • \u67e5\u8be2\u5143\u7d20\uff1a\u8f93\u5165 key \uff0c\u7ecf\u8fc7\u54c8\u5e0c\u51fd\u6570\u5f97\u5230\u6876\u7d22\u5f15\uff0c\u5373\u53ef\u8bbf\u95ee\u94fe\u8868\u5934\u8282\u70b9\uff0c\u7136\u540e\u904d\u5386\u94fe\u8868\u5e76\u5bf9\u6bd4 key \u4ee5\u67e5\u627e\u76ee\u6807\u952e\u503c\u5bf9\u3002
  • \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

\u94fe\u5f0f\u5730\u5740\u5b58\u5728\u4ee5\u4e0b\u5c40\u9650\u6027\u3002

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

\u4ee5\u4e0b\u4ee3\u7801\u7ed9\u51fa\u4e86\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\u7684\u7b80\u5355\u5b9e\u73b0\uff0c\u9700\u8981\u6ce8\u610f\u4e24\u70b9\u3002

  • \u4f7f\u7528\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\u4ee3\u66ff\u94fe\u8868\uff0c\u4ece\u800c\u7b80\u5316\u4ee3\u7801\u3002\u5728\u8fd9\u79cd\u8bbe\u5b9a\u4e0b\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\u5b9e\u73b0\u5305\u542b\u54c8\u5e0c\u8868\u6269\u5bb9\u65b9\u6cd5\u3002\u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7 \\(\\frac{2}{3}\\) \u65f6\uff0c\u6211\u4eec\u5c06\u54c8\u5e0c\u8868\u6269\u5bb9\u81f3 \\(2\\) \u500d\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig hash_map_chaining.py
class 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.0 / 3.0  # \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 | None:\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\nbreak\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.cpp
/* \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.0), extendRatio(2) {\nbuckets.resize(capacity);\n}\n/* \u6790\u6784\u65b9\u6cd5 */\n~HashMapChaining() {\nfor (auto &bucket : buckets) {\nfor (Pair *pair : bucket) {\n// \u91ca\u653e\u5185\u5b58\ndelete pair;\n}\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 / (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\u7a7a\u5b57\u7b26\u4e32\nreturn \"\";\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// \u91ca\u653e\u5185\u5b58\ndelete pair;\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.java
/* \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.0 / 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);\nsize--;\nbreak;\n}\n}\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.cs
/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\nint size; // \u952e\u503c\u5bf9\u6570\u91cf\nint capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\nreadonly double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nreadonly int 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.0 / 3.0;\nextendRatio = 2;\nbuckets = new List<List<Pair>>(capacity);\nfor (int i = 0; i < capacity; i++) {\nbuckets.Add(new List<Pair>());\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nprivate int HashFunc(int key) {\nreturn key % capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nprivate double LoadFactor() {\nreturn (double)size / capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npublic string? Get(int key) {\nint index = HashFunc(key);\n// \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nforeach (Pair pair in buckets[index]) {\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 */\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// \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nforeach (Pair pair in 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].Add(new Pair(key, val));\nsize++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\npublic void Remove(int key) {\nint index = HashFunc(key);\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nforeach (Pair pair in buckets[index].ToList()) {\nif (pair.key == key) {\nbuckets[index].Remove(pair);\nsize--;\nbreak;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nprivate void 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 List<List<Pair>>(capacity);\nfor (int i = 0; i < capacity; i++) {\nbuckets.Add(new List<Pair>());\n}\nsize = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nforeach (List<Pair> bucket in bucketsTmp) {\nforeach (Pair pair in bucket) {\nPut(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\npublic void Print() {\nforeach (List<Pair> bucket in buckets) {\nList<string> res = new();\nforeach (Pair pair in bucket) {\nres.Add(pair.key + \" -> \" + pair.val);\n}\nforeach (string kv in res) {\nConsole.WriteLine(kv);\n}\n}\n}\n}\n
hash_map_chaining.go
/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\ntype hashMapChaining struct {\nsize        int      // \u952e\u503c\u5bf9\u6570\u91cf\ncapacity    int      // \u54c8\u5e0c\u8868\u5bb9\u91cf\nloadThres   float64  // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nextendRatio int      // \u6269\u5bb9\u500d\u6570\nbuckets     [][]pair // \u6876\u6570\u7ec4\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newHashMapChaining() *hashMapChaining {\nbuckets := make([][]pair, 4)\nfor i := 0; i < 4; i++ {\nbuckets[i] = make([]pair, 0)\n}\nreturn &hashMapChaining{\nsize:        0,\ncapacity:    4,\nloadThres:   2.0 / 3.0,\nextendRatio: 2,\nbuckets:     buckets,\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (m *hashMapChaining) hashFunc(key int) int {\nreturn key % m.capacity\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfunc (m *hashMapChaining) loadFactor() float64 {\nreturn float64(m.size / m.capacity)\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (m *hashMapChaining) get(key int) string {\nidx := m.hashFunc(key)\nbucket := m.buckets[idx]\n// \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nfor _, p := range bucket {\nif p.key == key {\nreturn p.val\n}\n}\n// \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\nreturn \"\"\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (m *hashMapChaining) put(key int, val string) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif m.loadFactor() > m.loadThres {\nm.extend()\n}\nidx := m.hashFunc(key)\n// \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nfor _, p := range m.buckets[idx] {\nif p.key == key {\np.val = val\nreturn\n}\n}\n// \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\np := pair{\nkey: key,\nval: val,\n}\nm.buckets[idx] = append(m.buckets[idx], p)\nm.size += 1\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (m *hashMapChaining) remove(key int) {\nidx := m.hashFunc(key)\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor i, p := range m.buckets[idx] {\nif p.key == key {\n// \u5207\u7247\u5220\u9664\nm.buckets[idx] = append(m.buckets[idx][:i], m.buckets[idx][i+1:]...)\nm.size -= 1\nbreak\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfunc (m *hashMapChaining) extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\ntmpBuckets := make([][]pair, len(m.buckets))\nfor i := 0; i < len(m.buckets); i++ {\ntmpBuckets[i] = make([]pair, len(m.buckets[i]))\ncopy(tmpBuckets[i], m.buckets[i])\n}\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nm.capacity *= m.extendRatio\nm.buckets = make([][]pair, m.capacity)\nfor i := 0; i < m.capacity; i++ {\nm.buckets[i] = make([]pair, 0)\n}\nm.size = 0\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor _, bucket := range tmpBuckets {\nfor _, p := range bucket {\nm.put(p.key, p.val)\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (m *hashMapChaining) print() {\nvar builder strings.Builder\nfor _, bucket := range m.buckets {\nbuilder.WriteString(\"[\")\nfor _, p := range bucket {\nbuilder.WriteString(strconv.Itoa(p.key) + \" -> \" + p.val + \" \")\n}\nbuilder.WriteString(\"]\")\nfmt.Println(builder.String())\nbuilder.Reset()\n}\n}\n
hash_map_chaining.swift
/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\nvar size: Int // \u952e\u503c\u5bf9\u6570\u91cf\nvar capacity: Int // \u54c8\u5e0c\u8868\u5bb9\u91cf\nvar loadThres: Double // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nvar extendRatio: Int // \u6269\u5bb9\u500d\u6570\nvar buckets: [[Pair]] // \u6876\u6570\u7ec4\n/* \u6784\u9020\u65b9\u6cd5 */\ninit() {\nsize = 0\ncapacity = 4\nloadThres = 2.0 / 3.0\nextendRatio = 2\nbuckets = Array(repeating: [], count: capacity)\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc hashFunc(key: Int) -> Int {\nkey % capacity\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfunc loadFactor() -> Double {\nDouble(size / capacity)\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc get(key: Int) -> String? {\nlet index = hashFunc(key: key)\nlet bucket = 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}\n}\n// \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de nil\nreturn nil\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc put(key: Int, val: String) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif loadFactor() > loadThres {\nextend()\n}\nlet index = hashFunc(key: key)\nlet bucket = 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}\n}\n// \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\nlet pair = Pair(key: key, val: val)\nbuckets[index].append(pair)\nsize += 1\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfunc remove(key: Int) {\nlet index = hashFunc(key: key)\nlet bucket = buckets[index]\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor (pairIndex, pair) in bucket.enumerated() {\nif pair.key == key {\nbuckets[index].remove(at: pairIndex)\n}\n}\nsize -= 1\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfunc extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nlet bucketsTmp = buckets\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\ncapacity *= extendRatio\nbuckets = Array(repeating: [], count: capacity)\nsize = 0\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor bucket in bucketsTmp {\nfor pair in bucket {\nput(key: pair.key, val: pair.val)\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc print() {\nfor bucket in buckets {\nlet res = bucket.map { \"\\($0.key) -> \\($0.val)\" }\nSwift.print(res)\n}\n}\n}\n
hash_map_chaining.js
/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n#size; // \u952e\u503c\u5bf9\u6570\u91cf\n#capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n#loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n#extendRatio; // \u6269\u5bb9\u500d\u6570\n#buckets; // \u6876\u6570\u7ec4\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor() {\nthis.#size = 0;\nthis.#capacity = 4;\nthis.#loadThres = 2.0 / 3.0;\nthis.#extendRatio = 2;\nthis.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\n#hashFunc(key) {\nreturn key % this.#capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\n#loadFactor() {\nreturn this.#size / this.#capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nget(key) {\nconst index = this.#hashFunc(key);\nconst bucket = this.#buckets[index];\n// \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nfor (const pair of 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 */\nput(key, val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (this.#loadFactor() > this.#loadThres) {\nthis.#extend();\n}\nconst index = this.#hashFunc(key);\nconst bucket = this.#buckets[index];\n// \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nfor (const pair of 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\nconst pair = new Pair(key, val);\nbucket.push(pair);\nthis.#size++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nremove(key) {\nconst index = this.#hashFunc(key);\nlet bucket = this.#buckets[index];\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor (let i = 0; i < bucket.length; i++) {\nif (bucket[i].key === key) {\nbucket.splice(i, 1);\nthis.#size--;\nbreak;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n#extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nconst bucketsTmp = this.#buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nthis.#capacity *= this.#extendRatio;\nthis.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\nthis.#size = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (const bucket of bucketsTmp) {\nfor (const pair of bucket) {\nthis.put(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nprint() {\nfor (const bucket of this.#buckets) {\nlet res = [];\nfor (const pair of bucket) {\nres.push(pair.key + ' -> ' + pair.val);\n}\nconsole.log(res);\n}\n}\n}\n
hash_map_chaining.ts
/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n#size: number; // \u952e\u503c\u5bf9\u6570\u91cf\n#capacity: number; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n#loadThres: number; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n#extendRatio: number; // \u6269\u5bb9\u500d\u6570\n#buckets: Pair[][]; // \u6876\u6570\u7ec4\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor() {\nthis.#size = 0;\nthis.#capacity = 4;\nthis.#loadThres = 2.0 / 3.0;\nthis.#extendRatio = 2;\nthis.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\n#hashFunc(key: number): number {\nreturn key % this.#capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\n#loadFactor(): number {\nreturn this.#size / this.#capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nget(key: number): string | null {\nconst index = this.#hashFunc(key);\nconst bucket = this.#buckets[index];\n// \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nfor (const pair of 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 */\nput(key: number, val: string): void {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (this.#loadFactor() > this.#loadThres) {\nthis.#extend();\n}\nconst index = this.#hashFunc(key);\nconst bucket = this.#buckets[index];\n// \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nfor (const pair of 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\nconst pair = new Pair(key, val);\nbucket.push(pair);\nthis.#size++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nremove(key: number): void {\nconst index = this.#hashFunc(key);\nlet bucket = this.#buckets[index];\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor (let i = 0; i < bucket.length; i++) {\nif (bucket[i].key === key) {\nbucket.splice(i, 1);\nthis.#size--;\nbreak;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n#extend(): void {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nconst bucketsTmp = this.#buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nthis.#capacity *= this.#extendRatio;\nthis.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\nthis.#size = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (const bucket of bucketsTmp) {\nfor (const pair of bucket) {\nthis.put(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nprint(): void {\nfor (const bucket of this.#buckets) {\nlet res = [];\nfor (const pair of bucket) {\nres.push(pair.key + ' -> ' + pair.val);\n}\nconsole.log(res);\n}\n}\n}\n
hash_map_chaining.dart
/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\nlate int size; // \u952e\u503c\u5bf9\u6570\u91cf\nlate int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\nlate double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nlate int extendRatio; // \u6269\u5bb9\u500d\u6570\nlate List<List<Pair>> buckets; // \u6876\u6570\u7ec4\n/* \u6784\u9020\u65b9\u6cd5 */\nHashMapChaining() {\nsize = 0;\ncapacity = 4;\nloadThres = 2.0 / 3.0;\nextendRatio = 2;\nbuckets = List.generate(capacity, (_) => []);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(int key) {\nreturn key % capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor() {\nreturn size / capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nString? get(int key) {\nint index = hashFunc(key);\nList<Pair> bucket = buckets[index];\n// \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nfor (Pair pair in 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[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 in 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 = 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[index];\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor (Pair pair in bucket) {\nif (pair.key == key) {\nbucket.remove(pair);\nsize--;\nbreak;\n}\n}\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 = List.generate(capacity, (_) => []);\nsize = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (List<Pair> bucket in bucketsTmp) {\nfor (Pair pair in bucket) {\nput(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid printHashMap() {\nfor (List<Pair> bucket in buckets) {\nList<String> res = [];\nfor (Pair pair in bucket) {\nres.add(\"${pair.key} -> ${pair.val}\");\n}\nprint(res);\n}\n}\n}\n
hash_map_chaining.rs
/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nstruct HashMapChaining {\nsize: i32,\ncapacity: i32,\nload_thres: f32,\nextend_ratio: i32,\nbuckets: Vec<Vec<Pair>>,\n}\nimpl HashMapChaining {\n/* \u6784\u9020\u65b9\u6cd5 */\nfn new() -> Self {\nSelf {\nsize: 0,\ncapacity: 4,\nload_thres: 2.0 / 3.0,\nextend_ratio: 2,\nbuckets: vec![vec![]; 4],\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nfn hash_func(&self, key: i32) -> usize {\nkey as usize % self.capacity as usize\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfn load_factor(&self) -> f32 {\nself.size as f32 / self.capacity as f32\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfn remove(&mut self, key: i32) -> Option<String> {\nlet index = self.hash_func(key);\nlet bucket = &mut self.buckets[index];\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor i in 0..bucket.len() {\nif bucket[i].key == key {\nlet pair = bucket.remove(i);\nself.size -= 1;\nreturn Some(pair.val);\n}\n}\n// \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de None\nNone\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfn extend(&mut self) {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nlet buckets_tmp = std::mem::replace(&mut self.buckets, vec![]);\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nself.capacity *= self.extend_ratio;\nself.buckets = vec![Vec::new(); self.capacity as usize];\nself.size = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor bucket in buckets_tmp {\nfor pair in bucket {\nself.put(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfn print(&self) {\nfor bucket in &self.buckets {\nlet mut res = Vec::new();\nfor pair in bucket {\nres.push(format!(\"{} -> {}\", pair.key, pair.val));\n}\nprintln!(\"{:?}\", res);\n}\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfn put(&mut self, key: i32, val: String) {\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();\n}\nlet index = self.hash_func(key);\nlet bucket = &mut 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.clone();\nreturn;\n}\n}\nlet bucket = &mut self.buckets[index];\n// \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\nlet pair = Pair {\nkey,\nval: val.clone(),\n};\nbucket.push(pair);\nself.size += 1;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfn get(&self, key: i32) -> Option<&str> {\nlet index = self.hash_func(key);\nlet bucket = &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 Some(&pair.val);\n}\n}\n// \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de None\nNone\n}\n}\n
hash_map_chaining.c
/* \u94fe\u8868\u8282\u70b9 */\nstruct node {\nPair *pair;\nstruct node *next;\n};\ntypedef struct node Node;\n/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nstruct 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\nNode **buckets;   // \u6876\u6570\u7ec4\n};\ntypedef struct hashMapChaining hashMapChaining;\n/* \u6784\u9020\u65b9\u6cd5 */\nhashMapChaining *initHashMapChaining() {\nhashMapChaining *hashMap = (hashMapChaining *)malloc(sizeof(hashMapChaining));\nhashMap->size = 0;\nhashMap->capacity = 4;\nhashMap->loadThres = 2.0 / 3.0;\nhashMap->extendRatio = 2;\nhashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));\nfor (int i = 0; i < hashMap->capacity; i++) {\nhashMap->buckets[i] = NULL;\n}\nreturn hashMap;\n}\n/* \u6790\u6784\u65b9\u6cd5 */\nvoid freeHashMapChaining(hashMapChaining *hashMap) {\nfor (int i = 0; i < hashMap->capacity; i++) {\nNode *cur = hashMap->buckets[i];\nwhile (cur) {\nNode *temp = cur;\ncur = cur->next;\nfree(temp->pair);\nfree(temp);\n}\n}\nfree(hashMap->buckets);\nfree(hashMap);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(hashMapChaining *hashMap, int key) {\nreturn key % hashMap->capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor(hashMapChaining *hashMap) {\nreturn (double)hashMap->size / (double)hashMap->capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nchar *get(hashMapChaining *hashMap, int key) {\nint index = hashFunc(hashMap, key);\n// \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nNode *cur = hashMap->buckets[index];\nwhile (cur) {\nif (cur->pair->key == key) {\nreturn cur->pair->val;\n}\ncur = cur->next;\n}\nreturn \"\"; // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(hashMapChaining *hashMap, int key, const char *val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (loadFactor(hashMap) > hashMap->loadThres) {\nextend(hashMap);\n}\nint index = hashFunc(hashMap, key);\n// \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nNode *cur = hashMap->buckets[index];\nwhile (cur) {\nif (cur->pair->key == key) {\nstrcpy(cur->pair->val, val); // \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nreturn;\n}\ncur = cur->next;\n}\n// \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\nPair *newPair = (Pair *)malloc(sizeof(Pair));\nnewPair->key = key;\nstrcpy(newPair->val, val);\nNode *newNode = (Node *)malloc(sizeof(Node));\nnewNode->pair = newPair;\nnewNode->next = hashMap->buckets[index];\nhashMap->buckets[index] = newNode;\nhashMap->size++;\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend(hashMapChaining *hashMap) {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nint oldCapacity = hashMap->capacity;\nNode **oldBuckets = hashMap->buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nhashMap->capacity *= hashMap->extendRatio;\nhashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));\nfor (int i = 0; i < hashMap->capacity; i++) {\nhashMap->buckets[i] = NULL;\n}\nhashMap->size = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (int i = 0; i < oldCapacity; i++) {\nNode *cur = oldBuckets[i];\nwhile (cur) {\nput(hashMap, cur->pair->key, cur->pair->val);\nNode *temp = cur;\ncur = cur->next;\n// \u91ca\u653e\u5185\u5b58\nfree(temp->pair);\nfree(temp);\n}\n}\nfree(oldBuckets);\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid removeKey(hashMapChaining *hashMap, int key) {\nint index = hashFunc(hashMap, key);\nNode *cur = hashMap->buckets[index];\nNode *pre = NULL;\nwhile (cur) {\nif (cur->pair->key == key) {\n// \u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nif (pre) {\npre->next = cur->next;\n} else {\nhashMap->buckets[index] = cur->next;\n}\n// \u91ca\u653e\u5185\u5b58\nfree(cur->pair);\nfree(cur);\nhashMap->size--;\nreturn;\n}\npre = cur;\ncur = cur->next;\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print(hashMapChaining *hashMap) {\nfor (int i = 0; i < hashMap->capacity; i++) {\nNode *cur = hashMap->buckets[i];\nprintf(\"[\");\nwhile (cur) {\nprintf(\"%d -> %s, \", cur->pair->key, cur->pair->val);\ncur = cur->next;\n}\nprintf(\"]\\n\");\n}\n}\n
hash_map_chaining.zig
[class]{HashMapChaining}-[func]{}\n

\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u94fe\u8868\u5f88\u957f\u65f6\uff0c\u67e5\u8be2\u6548\u7387 \\(O(n)\\) \u5f88\u5dee\u3002\u6b64\u65f6\u53ef\u4ee5\u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u201cAVL \u6811\u201d\u6216\u201c\u7ea2\u9ed1\u6811\u201d\uff0c\u4ece\u800c\u5c06\u67e5\u8be2\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(\\log n)\\) \u3002

"},{"location":"chapter_hashing/hash_collision/#622","title":"6.2.2 \u00a0 \u5f00\u653e\u5bfb\u5740","text":"

\u300c\u5f00\u653e\u5bfb\u5740 open addressing\u300d\u4e0d\u5f15\u5165\u989d\u5916\u7684\u6570\u636e\u7ed3\u6784\uff0c\u800c\u662f\u901a\u8fc7\u201c\u591a\u6b21\u63a2\u6d4b\u201d\u6765\u5904\u7406\u54c8\u5e0c\u51b2\u7a81\uff0c\u63a2\u6d4b\u65b9\u5f0f\u4e3b\u8981\u5305\u62ec\u7ebf\u6027\u63a2\u6d4b\u3001\u5e73\u65b9\u63a2\u6d4b\u3001\u591a\u6b21\u54c8\u5e0c\u7b49\u3002

\u4e0b\u9762\u5c06\u4e3b\u8981\u4ee5\u7ebf\u6027\u63a2\u6d4b\u4e3a\u4f8b\uff0c\u4ecb\u7ecd\u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868\u7684\u5de5\u4f5c\u673a\u5236\u4e0e\u4ee3\u7801\u5b9e\u73b0\u3002

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

\u7ebf\u6027\u63a2\u6d4b\u91c7\u7528\u56fa\u5b9a\u6b65\u957f\u7684\u7ebf\u6027\u641c\u7d22\u6765\u8fdb\u884c\u63a2\u6d4b\uff0c\u5176\u64cd\u4f5c\u65b9\u6cd5\u4e0e\u666e\u901a\u54c8\u5e0c\u8868\u6709\u6240\u4e0d\u540c\u3002

  • \u63d2\u5165\u5143\u7d20\uff1a\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u8ba1\u7b97\u6876\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\u6876\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\u5982\u679c\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u76ee\u6807\u5143\u7d20\u4e0d\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u8fd4\u56de \\(\\text{None}\\) \u3002

\u56fe 6-6 \u5c55\u793a\u4e86\u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\uff09\u54c8\u5e0c\u8868\u7684\u952e\u503c\u5bf9\u5206\u5e03\u3002\u6839\u636e\u6b64\u54c8\u5e0c\u51fd\u6570\uff0c\u6700\u540e\u4e24\u4f4d\u76f8\u540c\u7684 key \u90fd\u4f1a\u88ab\u6620\u5c04\u5230\u76f8\u540c\u7684\u6876\u3002\u800c\u901a\u8fc7\u7ebf\u6027\u63a2\u6d4b\uff0c\u5b83\u4eec\u88ab\u4f9d\u6b21\u5b58\u50a8\u5728\u8be5\u6876\u4ee5\u53ca\u4e4b\u4e0b\u7684\u6876\u4e2d\u3002

\u56fe 6-6 \u00a0 \u5f00\u653e\u5bfb\u5740\u548c\u7ebf\u6027\u63a2\u6d4b

\u7136\u800c\uff0c\u7ebf\u6027\u63a2\u6d4b\u5bb9\u6613\u4ea7\u751f\u201c\u805a\u96c6\u73b0\u8c61\u201d\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u6570\u7ec4\u4e2d\u8fde\u7eed\u88ab\u5360\u7528\u7684\u4f4d\u7f6e\u8d8a\u957f\uff0c\u8fd9\u4e9b\u8fde\u7eed\u4f4d\u7f6e\u53d1\u751f\u54c8\u5e0c\u51b2\u7a81\u7684\u53ef\u80fd\u6027\u8d8a\u5927\uff0c\u4ece\u800c\u8fdb\u4e00\u6b65\u4fc3\u4f7f\u8be5\u4f4d\u7f6e\u7684\u805a\u5806\u751f\u957f\uff0c\u5f62\u6210\u6076\u6027\u5faa\u73af\uff0c\u6700\u7ec8\u5bfc\u81f4\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u6548\u7387\u52a3\u5316\u3002

\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6211\u4eec\u4e0d\u80fd\u5728\u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868\u4e2d\u76f4\u63a5\u5220\u9664\u5143\u7d20\u3002\u8fd9\u662f\u56e0\u4e3a\u5220\u9664\u5143\u7d20\u4f1a\u5728\u6570\u7ec4\u5185\u4ea7\u751f\u4e00\u4e2a\u7a7a\u6876 \\(\\text{None}\\) \uff0c\u800c\u5f53\u67e5\u8be2\u5143\u7d20\u65f6\uff0c\u7ebf\u6027\u63a2\u6d4b\u5230\u8be5\u7a7a\u6876\u5c31\u4f1a\u8fd4\u56de\uff0c\u56e0\u6b64\u5728\u8be5\u7a7a\u6876\u4e4b\u4e0b\u7684\u5143\u7d20\u90fd\u65e0\u6cd5\u518d\u88ab\u8bbf\u95ee\u5230\uff0c\u7a0b\u5e8f\u53ef\u80fd\u8bef\u5224\u8fd9\u4e9b\u5143\u7d20\u4e0d\u5b58\u5728\u3002

\u56fe 6-7 \u00a0 \u5728\u5f00\u653e\u5bfb\u5740\u4e2d\u5220\u9664\u5143\u7d20\u5bfc\u81f4\u7684\u67e5\u8be2\u95ee\u9898

\u4e3a\u4e86\u89e3\u51b3\u8be5\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u7528\u300c\u61d2\u5220\u9664 lazy deletion\u300d\u673a\u5236\uff1a\u5b83\u4e0d\u76f4\u63a5\u4ece\u54c8\u5e0c\u8868\u4e2d\u79fb\u9664\u5143\u7d20\uff0c\u800c\u662f\u5229\u7528\u4e00\u4e2a\u5e38\u91cf TOMBSTONE \u6765\u6807\u8bb0\u8fd9\u4e2a\u6876\u3002\u5728\u8be5\u673a\u5236\u4e0b\uff0c\\(\\text{None}\\) \u548c TOMBSTONE \u90fd\u4ee3\u8868\u7a7a\u6876\uff0c\u90fd\u53ef\u4ee5\u653e\u7f6e\u952e\u503c\u5bf9\u3002\u4f46\u4e0d\u540c\u7684\u662f\uff0c\u7ebf\u6027\u63a2\u6d4b\u5230 TOMBSTONE \u65f6\u5e94\u8be5\u7ee7\u7eed\u904d\u5386\uff0c\u56e0\u4e3a\u5176\u4e4b\u4e0b\u53ef\u80fd\u8fd8\u5b58\u5728\u952e\u503c\u5bf9\u3002

\u7136\u800c\uff0c\u61d2\u5220\u9664\u53ef\u80fd\u4f1a\u52a0\u901f\u54c8\u5e0c\u8868\u7684\u6027\u80fd\u9000\u5316\u3002\u8fd9\u662f\u56e0\u4e3a\u6bcf\u6b21\u5220\u9664\u64cd\u4f5c\u90fd\u4f1a\u4ea7\u751f\u4e00\u4e2a\u5220\u9664\u6807\u8bb0\uff0c\u968f\u7740 TOMBSTONE \u7684\u589e\u52a0\uff0c\u641c\u7d22\u65f6\u95f4\u4e5f\u4f1a\u589e\u52a0\uff0c\u56e0\u4e3a\u7ebf\u6027\u63a2\u6d4b\u53ef\u80fd\u9700\u8981\u8df3\u8fc7\u591a\u4e2a TOMBSTONE \u624d\u80fd\u627e\u5230\u76ee\u6807\u5143\u7d20\u3002

\u4e3a\u6b64\uff0c\u8003\u8651\u5728\u7ebf\u6027\u63a2\u6d4b\u4e2d\u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a TOMBSTONE \u7684\u7d22\u5f15\uff0c\u5e76\u5c06\u641c\u7d22\u5230\u7684\u76ee\u6807\u5143\u7d20\u4e0e\u8be5 TOMBSTONE \u4ea4\u6362\u4f4d\u7f6e\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\u5f53\u6bcf\u6b21\u67e5\u8be2\u6216\u6dfb\u52a0\u5143\u7d20\u65f6\uff0c\u5143\u7d20\u4f1a\u88ab\u79fb\u52a8\u81f3\u8ddd\u79bb\u7406\u60f3\u4f4d\u7f6e\uff08\u63a2\u6d4b\u8d77\u59cb\u70b9\uff09\u66f4\u8fd1\u7684\u6876\uff0c\u4ece\u800c\u4f18\u5316\u67e5\u8be2\u6548\u7387\u3002

\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u4e2a\u5305\u542b\u61d2\u5220\u9664\u7684\u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\uff09\u54c8\u5e0c\u8868\u3002\u4e3a\u4e86\u66f4\u52a0\u5145\u5206\u5730\u4f7f\u7528\u54c8\u5e0c\u8868\u7684\u7a7a\u95f4\uff0c\u6211\u4eec\u5c06\u54c8\u5e0c\u8868\u770b\u4f5c\u662f\u4e00\u4e2a\u201c\u73af\u5f62\u6570\u7ec4\u201d\uff0c\u5f53\u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u56de\u5230\u5934\u90e8\u7ee7\u7eed\u904d\u5386\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig hash_map_open_addressing.py
class 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.0 / 3.0  # \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.TOMBSTONE = 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 find_bucket(self, key: int) -> int:\n\"\"\"\u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\"\"\"\nindex = self.hash_func(key)\nfirst_tombstone = -1\n# \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\nwhile self.buckets[index] is not None:\n# \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\nif self.buckets[index].key == key:\n# \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\nif first_tombstone != -1:\nself.buckets[first_tombstone] = self.buckets[index]\nself.buckets[index] = self.TOMBSTONE\nreturn first_tombstone  # \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\nreturn index  # \u8fd4\u56de\u6876\u7d22\u5f15\n# \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\nif first_tombstone == -1 and self.buckets[index] is self.TOMBSTONE:\nfirst_tombstone = index\n# \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nindex = (index + 1) % self.capacity\n# \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\nreturn index if first_tombstone == -1 else first_tombstone\ndef get(self, key: int) -> str:\n\"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\n# \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nindex = self.find_bucket(key)\n# \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif self.buckets[index] not in [None, self.TOMBSTONE]:\nreturn self.buckets[index].val\n# \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\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()\n# \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nindex = self.find_bucket(key)\n# \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\nif self.buckets[index] not in [None, self.TOMBSTONE]:\nself.buckets[index].val = val\nreturn\n# \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\nself.buckets[index] = Pair(key, val)\nself.size += 1\ndef remove(self, key: int):\n\"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\n# \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nindex = self.find_bucket(key)\n# \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\nif self.buckets[index] not in [None, self.TOMBSTONE]:\nself.buckets[index] = self.TOMBSTONE\nself.size -= 1\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.TOMBSTONE]:\nself.put(pair.key, pair.val)\ndef print(self):\n\"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\nfor pair in self.buckets:\nif pair is None:\nprint(\"None\")\nelif pair is self.TOMBSTONE:\nprint(\"TOMBSTONE\")\nelse:\nprint(pair.key, \"->\", pair.val)\n
hash_map_open_addressing.cpp
/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\nprivate:\nint size;                             // \u952e\u503c\u5bf9\u6570\u91cf\nint capacity = 4;                     // \u54c8\u5e0c\u8868\u5bb9\u91cf\nconst double loadThres = 2.0 / 3.0;     // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nconst int extendRatio = 2;            // \u6269\u5bb9\u500d\u6570\nvector<Pair *> buckets;               // \u6876\u6570\u7ec4\nPair *TOMBSTONE = new Pair(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nHashMapOpenAddressing() : size(0), buckets(capacity, nullptr) {\n}\n/* \u6790\u6784\u65b9\u6cd5 */\n~HashMapOpenAddressing() {\nfor (Pair *pair : buckets) {\nif (pair != nullptr && pair != TOMBSTONE) {\ndelete pair;\n}\n}\ndelete TOMBSTONE;\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/* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\nint findBucket(int key) {\nint index = hashFunc(key);\nint firstTombstone = -1;\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\nwhile (buckets[index] != nullptr) {\n// \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\nif (buckets[index]->key == key) {\n// \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\nif (firstTombstone != -1) {\nbuckets[firstTombstone] = buckets[index];\nbuckets[index] = TOMBSTONE;\nreturn firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n}\nreturn index; // \u8fd4\u56de\u6876\u7d22\u5f15\n}\n// \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\nif (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\nfirstTombstone = index;\n}\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nindex = (index + 1) % capacity;\n}\n// \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\nreturn firstTombstone == -1 ? index : firstTombstone;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nstring get(int key) {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = findBucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\nreturn buckets[index]->val;\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\nreturn \"\";\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}\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = findBucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\nif (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\nbuckets[index]->val = val;\nreturn;\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\nbuckets[index] = new Pair(key, val);\nsize++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid remove(int key) {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = findBucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\nif (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\ndelete buckets[index];\nbuckets[index] = TOMBSTONE;\nsize--;\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 != TOMBSTONE) {\nput(pair->key, pair->val);\ndelete pair;\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print() {\nfor (Pair *pair : buckets) {\nif (pair == nullptr) {\ncout << \"nullptr\" << endl;\n} else if (pair == TOMBSTONE) {\ncout << \"TOMBSTONE\" << endl;\n} else {\ncout << pair->key << \" -> \" << pair->val << endl;\n}\n}\n}\n};\n
hash_map_open_addressing.java
/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\nprivate int size; // \u952e\u503c\u5bf9\u6570\u91cf\nprivate int capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\nprivate final double loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nprivate final int extendRatio = 2; // \u6269\u5bb9\u500d\u6570\nprivate Pair[] buckets; // \u6876\u6570\u7ec4\nprivate final Pair TOMBSTONE = new Pair(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n/* \u6784\u9020\u65b9\u6cd5 */\npublic HashMapOpenAddressing() {\nsize = 0;\nbuckets = new Pair[capacity];\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nprivate int hashFunc(int key) {\nreturn key % capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nprivate double loadFactor() {\nreturn (double) size / capacity;\n}\n/* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\nprivate int findBucket(int key) {\nint index = hashFunc(key);\nint firstTombstone = -1;\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\nwhile (buckets[index] != null) {\n// \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\nif (buckets[index].key == key) {\n// \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\nif (firstTombstone != -1) {\nbuckets[firstTombstone] = buckets[index];\nbuckets[index] = TOMBSTONE;\nreturn firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n}\nreturn index; // \u8fd4\u56de\u6876\u7d22\u5f15\n}\n// \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\nif (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\nfirstTombstone = index;\n}\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nindex = (index + 1) % capacity;\n}\n// \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\nreturn firstTombstone == -1 ? index : firstTombstone;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npublic String get(int key) {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = findBucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif (buckets[index] != null && buckets[index] != TOMBSTONE) {\nreturn buckets[index].val;\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\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}\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = findBucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\nif (buckets[index] != null && buckets[index] != TOMBSTONE) {\nbuckets[index].val = val;\nreturn;\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\nbuckets[index] = new Pair(key, val);\nsize++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\npublic void remove(int key) {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = findBucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\nif (buckets[index] != null && buckets[index] != TOMBSTONE) {\nbuckets[index] = TOMBSTONE;\nsize--;\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nprivate 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 != TOMBSTONE) {\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(\"null\");\n} else if (pair == TOMBSTONE) {\nSystem.out.println(\"TOMBSTONE\");\n} else {\nSystem.out.println(pair.key + \" -> \" + pair.val);\n}\n}\n}\n}\n
hash_map_open_addressing.cs
/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\nprivate int size; // \u952e\u503c\u5bf9\u6570\u91cf\nprivate int capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\nprivate readonly double loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nprivate readonly int extendRatio = 2; // \u6269\u5bb9\u500d\u6570\nprivate Pair[] buckets; // \u6876\u6570\u7ec4\nprivate readonly Pair TOMBSTONE = new(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n/* \u6784\u9020\u65b9\u6cd5 */\npublic HashMapOpenAddressing() {\nsize = 0;\nbuckets = new Pair[capacity];\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nprivate int HashFunc(int key) {\nreturn key % capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nprivate double LoadFactor() {\nreturn (double)size / capacity;\n}\n/* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\nprivate int FindBucket(int key) {\nint index = HashFunc(key);\nint firstTombstone = -1;\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\nwhile (buckets[index] != null) {\n// \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\nif (buckets[index].key == key) {\n// \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\nif (firstTombstone != -1) {\nbuckets[firstTombstone] = buckets[index];\nbuckets[index] = TOMBSTONE;\nreturn firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n}\nreturn index; // \u8fd4\u56de\u6876\u7d22\u5f15\n}\n// \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\nif (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\nfirstTombstone = index;\n}\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nindex = (index + 1) % capacity;\n}\n// \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\nreturn firstTombstone == -1 ? index : firstTombstone;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npublic string? Get(int key) {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = FindBucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif (buckets[index] != null && buckets[index] != TOMBSTONE) {\nreturn buckets[index].val;\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\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}\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = FindBucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\nif (buckets[index] != null && buckets[index] != TOMBSTONE) {\nbuckets[index].val = val;\nreturn;\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\nbuckets[index] = new Pair(key, val);\nsize++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\npublic void Remove(int key) {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = FindBucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\nif (buckets[index] != null && buckets[index] != TOMBSTONE) {\nbuckets[index] = TOMBSTONE;\nsize--;\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nprivate 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\nforeach (Pair pair in bucketsTmp) {\nif (pair != null && pair != TOMBSTONE) {\nPut(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\npublic void Print() {\nforeach (Pair pair in buckets) {\nif (pair == null) {\nConsole.WriteLine(\"null\");\n} else if (pair == TOMBSTONE) {\nConsole.WriteLine(\"TOMBSTONE\");\n} else {\nConsole.WriteLine(pair.key + \" -> \" + pair.val);\n}\n}\n}\n}\n
hash_map_open_addressing.go
/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\ntype hashMapOpenAddressing struct {\nsize        int     // \u952e\u503c\u5bf9\u6570\u91cf\ncapacity    int     // \u54c8\u5e0c\u8868\u5bb9\u91cf\nloadThres   float64 // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nextendRatio int     // \u6269\u5bb9\u500d\u6570\nbuckets     []pair  // \u6876\u6570\u7ec4\nremoved     pair    // \u5220\u9664\u6807\u8bb0\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newHashMapOpenAddressing() *hashMapOpenAddressing {\nbuckets := make([]pair, 4)\nreturn &hashMapOpenAddressing{\nsize:        0,\ncapacity:    4,\nloadThres:   2.0 / 3.0,\nextendRatio: 2,\nbuckets:     buckets,\nremoved: pair{\nkey: -1,\nval: \"-1\",\n},\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (m *hashMapOpenAddressing) hashFunc(key int) int {\nreturn key % m.capacity\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfunc (m *hashMapOpenAddressing) loadFactor() float64 {\nreturn float64(m.size) / float64(m.capacity)\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (m *hashMapOpenAddressing) get(key int) string {\nidx := m.hashFunc(key)\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor i := 0; i < m.capacity; i++ {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nj := (idx + 1) % m.capacity\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u8fd4\u56de null\nif m.buckets[j] == (pair{}) {\nreturn \"\"\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif m.buckets[j].key == key && m.buckets[j] != m.removed {\nreturn m.buckets[j].val\n}\n}\n// \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\nreturn \"\"\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (m *hashMapOpenAddressing) put(key int, val string) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif m.loadFactor() > m.loadThres {\nm.extend()\n}\nidx := m.hashFunc(key)\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor i := 0; i < m.capacity; i++ {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nj := (idx + i) % m.capacity\n// \u82e5\u9047\u5230\u7a7a\u6876\u3001\u6216\u5e26\u6709\u5220\u9664\u6807\u8bb0\u7684\u6876\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u653e\u5165\u8be5\u6876\nif m.buckets[j] == (pair{}) || m.buckets[j] == m.removed {\nm.buckets[j] = pair{\nkey: key,\nval: val,\n}\nm.size += 1\nreturn\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val\nif m.buckets[j].key == key {\nm.buckets[j].val = val\n}\n}\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (m *hashMapOpenAddressing) remove(key int) {\nidx := m.hashFunc(key)\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor i := 0; i < m.capacity; i++ {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nj := (idx + 1) % m.capacity\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif m.buckets[j] == (pair{}) {\nreturn\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u6807\u8bb0\u5220\u9664\u5e76\u8fd4\u56de\nif m.buckets[j].key == key {\nm.buckets[j] = m.removed\nm.size -= 1\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfunc (m *hashMapOpenAddressing) extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\ntmpBuckets := make([]pair, len(m.buckets))\ncopy(tmpBuckets, m.buckets)\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nm.capacity *= m.extendRatio\nm.buckets = make([]pair, m.capacity)\nm.size = 0\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor _, p := range tmpBuckets {\nif p != (pair{}) && p != m.removed {\nm.put(p.key, p.val)\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (m *hashMapOpenAddressing) print() {\nfor _, p := range m.buckets {\nif p != (pair{}) {\nfmt.Println(strconv.Itoa(p.key) + \" -> \" + p.val)\n} else {\nfmt.Println(\"nil\")\n}\n}\n}\n
hash_map_open_addressing.swift
/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\nvar size: Int // \u952e\u503c\u5bf9\u6570\u91cf\nvar capacity: Int // \u54c8\u5e0c\u8868\u5bb9\u91cf\nvar loadThres: Double // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nvar extendRatio: Int // \u6269\u5bb9\u500d\u6570\nvar buckets: [Pair?] // \u6876\u6570\u7ec4\nvar TOMBSTONE: Pair // \u5220\u9664\u6807\u8bb0\n/* \u6784\u9020\u65b9\u6cd5 */\ninit() {\nsize = 0\ncapacity = 4\nloadThres = 2.0 / 3.0\nextendRatio = 2\nbuckets = Array(repeating: nil, count: capacity)\nTOMBSTONE = Pair(key: -1, val: \"-1\")\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc hashFunc(key: Int) -> Int {\nkey % capacity\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfunc loadFactor() -> Double {\nDouble(size / capacity)\n}\n/* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\nfunc findBucket(key: Int) -> Int {\nvar index = hashFunc(key: key)\nvar firstTombstone = -1\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\nwhile buckets[index] != nil {\n// \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\nif buckets[index]!.key == key {\n// \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\nif firstTombstone != -1 {\nbuckets[firstTombstone] = buckets[index]\nbuckets[index] = TOMBSTONE\nreturn firstTombstone // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n}\nreturn index // \u8fd4\u56de\u6876\u7d22\u5f15\n}\n// \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\nif firstTombstone == -1 && buckets[index] == TOMBSTONE {\nfirstTombstone = index\n}\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nindex = (index + 1) % capacity\n}\n// \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\nreturn firstTombstone == -1 ? index : firstTombstone\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc get(key: Int) -> String? {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nlet index = findBucket(key: key)\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif buckets[index] != nil, buckets[index] != TOMBSTONE {\nreturn buckets[index]!.val\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\nreturn nil\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc put(key: Int, val: String) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif loadFactor() > loadThres {\nextend()\n}\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nlet index = findBucket(key: key)\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\nif buckets[index] != nil, buckets[index] != TOMBSTONE {\nbuckets[index]!.val = val\nreturn\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\nbuckets[index] = Pair(key: key, val: val)\nsize += 1\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfunc remove(key: Int) {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nlet index = findBucket(key: key)\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\nif buckets[index] != nil, buckets[index] != TOMBSTONE {\nbuckets[index] = TOMBSTONE\nsize -= 1\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfunc extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nlet bucketsTmp = buckets\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\ncapacity *= extendRatio\nbuckets = Array(repeating: nil, count: capacity)\nsize = 0\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor pair in bucketsTmp {\nif let pair, pair != TOMBSTONE {\nput(key: pair.key, val: pair.val)\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc print() {\nfor pair in buckets {\nif pair == nil {\nSwift.print(\"null\")\n} else if pair == TOMBSTONE {\nSwift.print(\"TOMBSTONE\")\n} else {\nSwift.print(\"\\(pair!.key) -> \\(pair!.val)\")\n}\n}\n}\n}\n
hash_map_open_addressing.js
/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n#size; // \u952e\u503c\u5bf9\u6570\u91cf\n#capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n#loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n#extendRatio; // \u6269\u5bb9\u500d\u6570\n#buckets; // \u6876\u6570\u7ec4\n#removed; // \u5220\u9664\u6807\u8bb0\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor() {\nthis.#size = 0;\nthis.#capacity = 4;\nthis.#loadThres = 2.0 / 3.0;\nthis.#extendRatio = 2;\nthis.#buckets = new Array(this.#capacity).fill(null);\nthis.#removed = new Pair(-1, '-1');\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\n#hashFunc(key) {\nreturn key % this.#capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\n#loadFactor() {\nreturn this.#size / this.#capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nget(key) {\nconst index = this.#hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (let i = 0; i < this.#capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nconst j = (index + i) % this.#capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u8fd4\u56de null\nif (this.#buckets[j] === null) return null;\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif (\nthis.#buckets[j].key === key &&\nthis.#buckets[j].key !== this.#removed.key\n)\nreturn this.#buckets[j].val;\n}\nreturn null;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nput(key, val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (this.#loadFactor() > this.#loadThres) {\nthis.#extend();\n}\nconst index = this.#hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (let i = 0; i < this.#capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nlet j = (index + i) % this.#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 (\nthis.#buckets[j] === null ||\nthis.#buckets[j].key === this.#removed.key\n) {\nthis.#buckets[j] = new Pair(key, val);\nthis.#size += 1;\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val\nif (this.#buckets[j].key === key) {\nthis.#buckets[j].val = val;\nreturn;\n}\n}\n}\n/* \u5220\u9664\u64cd\u4f5c */\nremove(key) {\nconst index = this.#hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (let i = 0; i < this.#capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nconst j = (index + i) % this.#capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (this.#buckets[j] === null) {\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u6807\u8bb0\u5220\u9664\u5e76\u8fd4\u56de\nif (this.#buckets[j].key === key) {\nthis.#buckets[j] = this.#removed;\nthis.#size -= 1;\nreturn;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n#extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nconst bucketsTmp = this.#buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nthis.#capacity *= this.#extendRatio;\nthis.#buckets = new Array(this.#capacity).fill(null);\nthis.#size = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (const pair of bucketsTmp) {\nif (pair !== null && pair.key !== this.#removed.key) {\nthis.put(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nprint() {\nfor (const pair of this.#buckets) {\nif (pair !== null) {\nconsole.log(pair.key + ' -> ' + pair.val);\n} else {\nconsole.log('null');\n}\n}\n}\n}\n
hash_map_open_addressing.ts
/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n#size: number; // \u952e\u503c\u5bf9\u6570\u91cf\n#capacity: number; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n#loadThres: number; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n#extendRatio: number; // \u6269\u5bb9\u500d\u6570\n#buckets: Pair[]; // \u6876\u6570\u7ec4\n#removed: Pair; // \u5220\u9664\u6807\u8bb0\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor() {\nthis.#size = 0;\nthis.#capacity = 4;\nthis.#loadThres = 2.0 / 3.0;\nthis.#extendRatio = 2;\nthis.#buckets = new Array(this.#capacity).fill(null);\nthis.#removed = new Pair(-1, '-1');\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\n#hashFunc(key: number): number {\nreturn key % this.#capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\n#loadFactor(): number {\nreturn this.#size / this.#capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nget(key: number): string | null {\nconst index = this.#hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (let i = 0; i < this.#capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nconst j = (index + i) % this.#capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u8fd4\u56de null\nif (this.#buckets[j] === null) return null;\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif (\nthis.#buckets[j].key === key &&\nthis.#buckets[j].key !== this.#removed.key\n)\nreturn this.#buckets[j].val;\n}\nreturn null;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nput(key: number, val: string): void {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (this.#loadFactor() > this.#loadThres) {\nthis.#extend();\n}\nconst index = this.#hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (let i = 0; i < this.#capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nlet j = (index + i) % this.#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 (\nthis.#buckets[j] === null ||\nthis.#buckets[j].key === this.#removed.key\n) {\nthis.#buckets[j] = new Pair(key, val);\nthis.#size += 1;\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val\nif (this.#buckets[j].key === key) {\nthis.#buckets[j].val = val;\nreturn;\n}\n}\n}\n/* \u5220\u9664\u64cd\u4f5c */\nremove(key: number): void {\nconst index = this.#hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (let i = 0; i < this.#capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nconst j = (index + i) % this.#capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (this.#buckets[j] === null) {\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u6807\u8bb0\u5220\u9664\u5e76\u8fd4\u56de\nif (this.#buckets[j].key === key) {\nthis.#buckets[j] = this.#removed;\nthis.#size -= 1;\nreturn;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n#extend(): void {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nconst bucketsTmp = this.#buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nthis.#capacity *= this.#extendRatio;\nthis.#buckets = new Array(this.#capacity).fill(null);\nthis.#size = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (const pair of bucketsTmp) {\nif (pair !== null && pair.key !== this.#removed.key) {\nthis.put(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nprint(): void {\nfor (const pair of this.#buckets) {\nif (pair !== null) {\nconsole.log(pair.key + ' -> ' + pair.val);\n} else {\nconsole.log('null');\n}\n}\n}\n}\n
hash_map_open_addressing.dart
/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\nlate int _size; // \u952e\u503c\u5bf9\u6570\u91cf\nint _capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\ndouble _loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nint _extendRatio = 2; // \u6269\u5bb9\u500d\u6570\nlate List<Pair?> _buckets; // \u6876\u6570\u7ec4\nPair _TOMBSTONE = Pair(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n/* \u6784\u9020\u65b9\u6cd5 */\nHashMapOpenAddressing() {\n_size = 0;\n_buckets = List.generate(_capacity, (index) => null);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(int key) {\nreturn key % _capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor() {\nreturn _size / _capacity;\n}\n/* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\nint findBucket(int key) {\nint index = hashFunc(key);\nint firstTombstone = -1;\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\nwhile (_buckets[index] != null) {\n// \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\nif (_buckets[index]!.key == key) {\n// \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\nif (firstTombstone != -1) {\n_buckets[firstTombstone] = _buckets[index];\n_buckets[index] = _TOMBSTONE;\nreturn firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n}\nreturn index; // \u8fd4\u56de\u6876\u7d22\u5f15\n}\n// \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\nif (firstTombstone == -1 && _buckets[index] == _TOMBSTONE) {\nfirstTombstone = index;\n}\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nindex = (index + 1) % _capacity;\n}\n// \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\nreturn firstTombstone == -1 ? index : firstTombstone;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nString? get(int key) {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = findBucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\nreturn _buckets[index]!.val;\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\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}\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = findBucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\nif (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n_buckets[index]!.val = val;\nreturn;\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n_buckets[index] = new Pair(key, val);\n_size++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid remove(int key) {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = findBucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\nif (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n_buckets[index] = _TOMBSTONE;\n_size--;\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nList<Pair?> bucketsTmp = _buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n_capacity *= _extendRatio;\n_buckets = List.generate(_capacity, (index) => null);\n_size = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (Pair? pair in bucketsTmp) {\nif (pair != null && pair != _TOMBSTONE) {\nput(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid printHashMap() {\nfor (Pair? pair in _buckets) {\nif (pair == null) {\nprint(\"null\");\n} else if (pair == _TOMBSTONE) {\nprint(\"TOMBSTONE\");\n} else {\nprint(\"${pair.key} -> ${pair.val}\");\n}\n}\n}\n}\n
hash_map_open_addressing.rs
/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nstruct HashMapOpenAddressing {\nsize: usize,                    // \u952e\u503c\u5bf9\u6570\u91cf\ncapacity: usize,                // \u54c8\u5e0c\u8868\u5bb9\u91cf\nload_thres: f64,                // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nextend_ratio: usize,            // \u6269\u5bb9\u500d\u6570\nbuckets: Vec<Option<Pair>>,     // \u6876\u6570\u7ec4\nTOMBSTONE: Option<Pair>,        // \u5220\u9664\u6807\u8bb0\n}\nimpl HashMapOpenAddressing {\n/* \u6784\u9020\u65b9\u6cd5 */\nfn new() -> Self {\nSelf {\nsize: 0,\ncapacity: 4,\nload_thres: 2.0 / 3.0,\nextend_ratio: 2,\nbuckets: vec![None; 4],\nTOMBSTONE: Some(Pair {key: -1, val: \"-1\".to_string()}),\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nfn hash_func(&self, key: i32) -> usize {\n(key % self.capacity as i32) as usize\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfn load_factor(&self) -> f64 {\nself.size as f64 / self.capacity as f64\n}\n/* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\nfn find_bucket(&mut self, key: i32) -> usize {\nlet mut index = self.hash_func(key);\nlet mut first_tombstone = -1;\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\nwhile self.buckets[index].is_some() {\n// \u82e5\u9047\u5230 key\uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nif self.buckets[index].as_ref().unwrap().key == key {\n// \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u5efa\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\nif first_tombstone != -1 {\nself.buckets[first_tombstone as usize] = self.buckets[index].take();\nself.buckets[index] = self.TOMBSTONE.clone();\nreturn first_tombstone as usize;    // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n}\nreturn index;   // \u8fd4\u56de\u6876\u7d22\u5f15\n}\n// \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\nif first_tombstone == -1 && self.buckets[index] == self.TOMBSTONE {\nfirst_tombstone = index as i32;\n}\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nindex = (index + 1) % self.capacity;\n}\n// \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\nif first_tombstone == -1 { index } else { first_tombstone as usize }\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfn get(&mut self, key: i32) -> Option<&str> {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nlet index = self.find_bucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\nreturn self.buckets[index].as_ref().map(|pair| &pair.val as &str);\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\nNone\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfn put(&mut self, key: i32, val: String) {\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();\n}\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nlet index = self.find_bucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\nif self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\nself.buckets[index].as_mut().unwrap().val = val;\nreturn;\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\nself.buckets[index] = Some(Pair { key, val });\nself.size += 1;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfn remove(&mut self, key: i32) {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nlet index = self.find_bucket(key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\nif self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\nself.buckets[index] = self.TOMBSTONE.clone();\nself.size -= 1;\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfn extend(&mut self) {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nlet buckets_tmp = self.buckets.clone();\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nself.capacity *= self.extend_ratio;\nself.buckets = vec![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.is_none() || pair == self.TOMBSTONE {\ncontinue;\n}\nlet pair = pair.unwrap();\nself.put(pair.key, pair.val);\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfn print(&self) {\nfor pair in &self.buckets {\nif pair.is_none() {\nprintln!(\"null\");\n} else if pair == &self.TOMBSTONE {\nprintln!(\"TOMBSTONE\");\n} else {\nlet pair = pair.as_ref().unwrap();\nprintln!(\"{} -> {}\", pair.key, pair.val);\n}\n}\n}\n}\n
hash_map_open_addressing.c
/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nstruct hashMapOpenAddressing {\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\nPair **buckets;   // \u6876\u6570\u7ec4\nPair *TOMBSTONE;  // \u5220\u9664\u6807\u8bb0\n};\ntypedef struct hashMapOpenAddressing hashMapOpenAddressing;\n/* \u6784\u9020\u65b9\u6cd5 */\nhashMapOpenAddressing *newHashMapOpenAddressing() {\nhashMapOpenAddressing *hashMap = (hashMapOpenAddressing *)malloc(sizeof(hashMapOpenAddressing));\nhashMap->size = 0;\nhashMap->capacity = 4;\nhashMap->loadThres = 2.0 / 3.0;\nhashMap->extendRatio = 2;\nhashMap->buckets = (Pair **)malloc(sizeof(Pair *) * hashMap->capacity);\nhashMap->TOMBSTONE = (Pair *)malloc(sizeof(Pair));\nhashMap->TOMBSTONE->key = -1;\nhashMap->TOMBSTONE->val = \"-1\";\nreturn hashMap;\n}\n/* \u6790\u6784\u65b9\u6cd5 */\nvoid delHashMapOpenAddressing(hashMapOpenAddressing *hashMap) {\nfor (int i = 0; i < hashMap->capacity; i++) {\nPair *pair = hashMap->buckets[i];\nif (pair != NULL && pair != hashMap->TOMBSTONE) {\nfree(pair->val);\nfree(pair);\n}\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(hashMapOpenAddressing *hashMap, int key) {\nreturn key % hashMap->capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor(hashMapOpenAddressing *hashMap) {\nreturn (double)hashMap->size / (double)hashMap->capacity;\n}\n/* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\nint findBucket(hashMapOpenAddressing *hashMap, int key) {\nint index = hashFunc(hashMap, key);\nint firstTombstone = -1;\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\nwhile (hashMap->buckets[index] != NULL) {\n// \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\nif (hashMap->buckets[index]->key == key) {\n// \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\nif (firstTombstone != -1) {\nhashMap->buckets[firstTombstone] = hashMap->buckets[index];\nhashMap->buckets[index] = hashMap->TOMBSTONE;\nreturn firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n}\nreturn index; // \u8fd4\u56de\u6876\u7d22\u5f15\n}\n// \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\nif (firstTombstone == -1 && hashMap->buckets[index] == hashMap->TOMBSTONE) {\nfirstTombstone = index;\n}\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nindex = (index + 1) % hashMap->capacity;\n}\n// \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\nreturn firstTombstone == -1 ? index : firstTombstone;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nchar *get(hashMapOpenAddressing *hashMap, int key) {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = findBucket(hashMap, key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\nreturn hashMap->buckets[index]->val;\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\nreturn \"\";\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(hashMapOpenAddressing *hashMap, int key, char *val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (loadFactor(hashMap) > hashMap->loadThres) {\nextend(hashMap);\n}\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = findBucket(hashMap, key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\nif (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\nfree(hashMap->buckets[index]->val);\nhashMap->buckets[index]->val = (char *)malloc(sizeof(strlen(val + 1)));\nstrcpy(hashMap->buckets[index]->val, val);\nhashMap->buckets[index]->val[strlen(val)] = '\\0';\nreturn;\n}\n// \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\nPair *pair = (Pair *)malloc(sizeof(Pair));\npair->key = key;\npair->val = (char *)malloc(sizeof(strlen(val + 1)));\nstrcpy(pair->val, val);\npair->val[strlen(val)] = '\\0';\nhashMap->buckets[index] = pair;\nhashMap->size++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid removeItem(hashMapOpenAddressing *hashMap, int key) {\n// \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\nint index = findBucket(hashMap, key);\n// \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\nif (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\nPair *pair = hashMap->buckets[index];\nfree(pair->val);\nfree(pair);\nhashMap->buckets[index] = hashMap->TOMBSTONE;\nhashMap->size--;\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend(hashMapOpenAddressing *hashMap) {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nPair **bucketsTmp = hashMap->buckets;\nint oldCapacity = hashMap->capacity;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nhashMap->capacity *= hashMap->extendRatio;\nhashMap->buckets = (Pair **)malloc(sizeof(Pair *) * hashMap->capacity);\nhashMap->size = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (int i = 0; i < oldCapacity; i++) {\nPair *pair = bucketsTmp[i];\nif (pair != NULL && pair != hashMap->TOMBSTONE) {\nput(hashMap, pair->key, pair->val);\nfree(pair->val);\nfree(pair);\n}\n}\nfree(bucketsTmp);\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print(hashMapOpenAddressing *hashMap) {\nfor (int i = 0; i < hashMap->capacity; i++) {\nPair *pair = hashMap->buckets[i];\nif (pair == NULL) {\nprintf(\"NULL\\n\");\n} else if (pair == hashMap->TOMBSTONE) {\nprintf(\"TOMBSTONE\\n\");\n} else {\nprintf(\"%d -> %s\\n\", pair->key, pair->val);\n}\n}\n}\n
hash_map_open_addressing.zig
[class]{HashMapOpenAddressing}-[func]{}\n
"},{"location":"chapter_hashing/hash_collision/#2","title":"2. \u00a0 \u5e73\u65b9\u63a2\u6d4b","text":"

\u5e73\u65b9\u63a2\u6d4b\u4e0e\u7ebf\u6027\u63a2\u6d4b\u7c7b\u4f3c\uff0c\u90fd\u662f\u5f00\u653e\u5bfb\u5740\u7684\u5e38\u89c1\u7b56\u7565\u4e4b\u4e00\u3002\u5f53\u53d1\u751f\u51b2\u7a81\u65f6\uff0c\u5e73\u65b9\u63a2\u6d4b\u4e0d\u662f\u7b80\u5355\u5730\u8df3\u8fc7\u4e00\u4e2a\u56fa\u5b9a\u7684\u6b65\u6570\uff0c\u800c\u662f\u8df3\u8fc7\u201c\u63a2\u6d4b\u6b21\u6570\u7684\u5e73\u65b9\u201d\u7684\u6b65\u6570\uff0c\u5373 \\(1, 4, 9, \\dots\\) \u6b65\u3002

\u5e73\u65b9\u63a2\u6d4b\u901a\u4e3b\u8981\u5177\u6709\u4ee5\u4e0b\u4f18\u52bf\u3002

  • \u5e73\u65b9\u63a2\u6d4b\u901a\u8fc7\u8df3\u8fc7\u5e73\u65b9\u7684\u8ddd\u79bb\uff0c\u8bd5\u56fe\u7f13\u89e3\u7ebf\u6027\u63a2\u6d4b\u7684\u805a\u96c6\u6548\u5e94\u3002
  • \u5e73\u65b9\u63a2\u6d4b\u4f1a\u8df3\u8fc7\u66f4\u5927\u7684\u8ddd\u79bb\u6765\u5bfb\u627e\u7a7a\u4f4d\u7f6e\uff0c\u6709\u52a9\u4e8e\u6570\u636e\u5206\u5e03\u5f97\u66f4\u52a0\u5747\u5300\u3002

\u7136\u800c\uff0c\u5e73\u65b9\u63a2\u6d4b\u4e5f\u5e76\u4e0d\u662f\u5b8c\u7f8e\u7684\u3002

  • \u4ecd\u7136\u5b58\u5728\u805a\u96c6\u73b0\u8c61\uff0c\u5373\u67d0\u4e9b\u4f4d\u7f6e\u6bd4\u5176\u4ed6\u4f4d\u7f6e\u66f4\u5bb9\u6613\u88ab\u5360\u7528\u3002
  • \u7531\u4e8e\u5e73\u65b9\u7684\u589e\u957f\uff0c\u5e73\u65b9\u63a2\u6d4b\u53ef\u80fd\u4e0d\u4f1a\u63a2\u6d4b\u6574\u4e2a\u54c8\u5e0c\u8868\uff0c\u8fd9\u610f\u5473\u7740\u5373\u4f7f\u54c8\u5e0c\u8868\u4e2d\u6709\u7a7a\u6876\uff0c\u5e73\u65b9\u63a2\u6d4b\u4e5f\u53ef\u80fd\u65e0\u6cd5\u8bbf\u95ee\u5230\u5b83\u3002
"},{"location":"chapter_hashing/hash_collision/#3","title":"3. \u00a0 \u591a\u6b21\u54c8\u5e0c","text":"

\u591a\u6b21\u54c8\u5e0c\u4f7f\u7528\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570 \\(f_1(x)\\)\u3001\\(f_2(x)\\)\u3001\\(f_3(x)\\)\u3001\\(\\dots\\) \u8fdb\u884c\u63a2\u6d4b\u3002

  • \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\u6876\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\u5f53\u9047\u5230\u7a7a\u6876\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

Tip

\u8bf7\u6ce8\u610f\uff0c\u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\u3001\u5e73\u65b9\u63a2\u6d4b\u548c\u591a\u6b21\u54c8\u5e0c\uff09\u54c8\u5e0c\u8868\u90fd\u5b58\u5728\u201c\u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5143\u7d20\u201d\u7684\u95ee\u9898\u3002

"},{"location":"chapter_hashing/hash_collision/#623","title":"6.2.3 \u00a0 \u7f16\u7a0b\u8bed\u8a00\u7684\u9009\u62e9","text":"

\u5404\u4e2a\u7f16\u7a0b\u8bed\u8a00\u91c7\u53d6\u4e86\u4e0d\u540c\u7684\u54c8\u5e0c\u8868\u5b9e\u73b0\u7b56\u7565\uff0c\u4ee5\u4e0b\u4e3e\u51e0\u4e2a\u4f8b\u5b50\u3002

  • 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\u3002\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\uff0c\u53c8\u79f0\u300c\u6563\u5217\u8868\u300d\uff0c\u5176\u901a\u8fc7\u5efa\u7acb\u952e key \u4e0e\u503c value \u4e4b\u95f4\u7684\u6620\u5c04\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684\u5143\u7d20\u67e5\u8be2\u3002\u5177\u4f53\u800c\u8a00\uff0c\u6211\u4eec\u5411\u54c8\u5e0c\u8868\u8f93\u5165\u4e00\u4e2a\u952e key \uff0c\u5219\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u83b7\u53d6\u5bf9\u5e94\u7684\u503c value \u3002

\u5982\u56fe 6-1 \u6240\u793a\uff0c\u7ed9\u5b9a \\(n\\) \u4e2a\u5b66\u751f\uff0c\u6bcf\u4e2a\u5b66\u751f\u90fd\u6709\u201c\u59d3\u540d\u201d\u548c\u201c\u5b66\u53f7\u201d\u4e24\u9879\u6570\u636e\u3002\u5047\u5982\u6211\u4eec\u5e0c\u671b\u5b9e\u73b0\u201c\u8f93\u5165\u4e00\u4e2a\u5b66\u53f7\uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u59d3\u540d\u201d\u7684\u67e5\u8be2\u529f\u80fd\uff0c\u5219\u53ef\u4ee5\u91c7\u7528\u56fe 6-1 \u6240\u793a\u7684\u54c8\u5e0c\u8868\u6765\u5b9e\u73b0\u3002

\u56fe 6-1 \u00a0 \u54c8\u5e0c\u8868\u7684\u62bd\u8c61\u8868\u793a

\u9664\u54c8\u5e0c\u8868\u5916\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u67e5\u8be2\u529f\u80fd\uff0c\u5b83\u4eec\u7684\u6548\u7387\u5bf9\u6bd4\u5982\u8868 6-1 \u6240\u793a\u3002

  • \u6dfb\u52a0\u5143\u7d20\uff1a\u4ec5\u9700\u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u6570\u7ec4\uff08\u94fe\u8868\uff09\u7684\u5c3e\u90e8\u5373\u53ef\uff0c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002
  • \u67e5\u8be2\u5143\u7d20\uff1a\u7531\u4e8e\u6570\u7ec4\uff08\u94fe\u8868\uff09\u662f\u4e71\u5e8f\u7684\uff0c\u56e0\u6b64\u9700\u8981\u904d\u5386\u5176\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002
  • \u5220\u9664\u5143\u7d20\uff1a\u9700\u8981\u5148\u67e5\u8be2\u5230\u5143\u7d20\uff0c\u518d\u4ece\u6570\u7ec4\uff08\u94fe\u8868\uff09\u4e2d\u5220\u9664\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002

\u8868 6-1 \u00a0 \u5143\u7d20\u67e5\u8be2\u6548\u7387\u5bf9\u6bd4

\u6570\u7ec4 \u94fe\u8868 \u54c8\u5e0c\u8868 \u67e5\u627e\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(1)\\) \u6dfb\u52a0\u5143\u7d20 \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(1)\\)

\u89c2\u5bdf\u53d1\u73b0\uff0c\u5728\u54c8\u5e0c\u8868\u4e2d\u8fdb\u884c\u589e\u5220\u67e5\u6539\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662f \\(O(1)\\) \uff0c\u975e\u5e38\u9ad8\u6548\u3002

"},{"location":"chapter_hashing/hash_map/#611","title":"6.1.1 \u00a0 \u54c8\u5e0c\u8868\u5e38\u7528\u64cd\u4f5c","text":"

\u54c8\u5e0c\u8868\u7684\u5e38\u89c1\u64cd\u4f5c\u5305\u62ec\uff1a\u521d\u59cb\u5316\u3001\u67e5\u8be2\u64cd\u4f5c\u3001\u6dfb\u52a0\u952e\u503c\u5bf9\u548c\u5220\u9664\u952e\u503c\u5bf9\u7b49\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig hash_map.py
# \u521d\u59cb\u5316\u54c8\u5e0c\u8868\nhmap: dict = {}\n# \u6dfb\u52a0\u64cd\u4f5c\n# \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nhmap[12836] = \"\u5c0f\u54c8\"\nhmap[15937] = \"\u5c0f\u5570\"\nhmap[16750] = \"\u5c0f\u7b97\"\nhmap[13276] = \"\u5c0f\u6cd5\"\nhmap[10583] = \"\u5c0f\u9e2d\"\n# \u67e5\u8be2\u64cd\u4f5c\n# \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nname: str = hmap[15937]\n# \u5220\u9664\u64cd\u4f5c\n# \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nhmap.pop(10583)\n
hash_map.cpp
/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nunordered_map<int, string> map;\n/* \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.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.cs
/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nDictionary<int, string> map = new() {\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\n{ 12836, \"\u5c0f\u54c8\" },\n{ 15937, \"\u5c0f\u5570\" },\n{ 16750, \"\u5c0f\u7b97\" },\n{ 13276, \"\u5c0f\u6cd5\" },\n{ 10583, \"\u5c0f\u9e2d\" }\n};\n/* \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_test.go
/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nhmap := make(map[int]string)\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nhmap[12836] = \"\u5c0f\u54c8\"\nhmap[15937] = \"\u5c0f\u5570\"\nhmap[16750] = \"\u5c0f\u7b97\"\nhmap[13276] = \"\u5c0f\u6cd5\"\nhmap[10583] = \"\u5c0f\u9e2d\"\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nname := hmap[15937]\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\ndelete(hmap, 10583)\n
hash_map.swift
/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nvar map: [Int: String] = [:]\n/* \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.js
/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nconst map = new Map();\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.set(12836, '\u5c0f\u54c8');\nmap.set(15937, '\u5c0f\u5570');\nmap.set(16750, '\u5c0f\u7b97');\nmap.set(13276, '\u5c0f\u6cd5');\nmap.set(10583, '\u5c0f\u9e2d');\n/* \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.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
hash_map.rs
use std::collections::HashMap;\n/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nlet mut map: HashMap<i32, String> = HashMap::new();\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.insert(12836, \"\u5c0f\u54c8\".to_string());\nmap.insert(15937, \"\u5c0f\u5570\".to_string());\nmap.insert(16750, \"\u5c0f\u7b97\".to_string());\nmap.insert(13279, \"\u5c0f\u6cd5\".to_string());\nmap.insert(10583, \"\u5c0f\u9e2d\".to_string());\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet _name: Option<&String> = map.get(&15937);\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nlet _removed_value: Option<String> = map.remove(&10583);\n
hash_map.c
// C \u672a\u63d0\u4f9b\u5185\u7f6e\u54c8\u5e0c\u8868\n
hash_map.zig
\n

\u54c8\u5e0c\u8868\u6709\u4e09\u79cd\u5e38\u7528\u904d\u5386\u65b9\u5f0f\uff1a\u904d\u5386\u952e\u503c\u5bf9\u3001\u904d\u5386\u952e\u548c\u904d\u5386\u503c\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig hash_map.py
# \u904d\u5386\u54c8\u5e0c\u8868\n# \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor key, value in hmap.items():\nprint(key, \"->\", value)\n# \u5355\u72ec\u904d\u5386\u952e key\nfor key in hmap.keys():\nprint(key)\n# \u5355\u72ec\u904d\u5386\u503c value\nfor value in hmap.values():\nprint(value)\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 kv: map) {\ncout << kv.first << endl;\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor (auto kv: map) {\ncout << kv.second << endl;\n}\n
hash_map.java
/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor (Map.Entry <Integer, String> kv: map.entrySet()) {\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.cs
/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nforeach (var kv in map) {\nConsole.WriteLine(kv.Key + \" -> \" + kv.Value);\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nforeach (int key in map.Keys) {\nConsole.WriteLine(key);\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nforeach (string val in map.Values) {\nConsole.WriteLine(val);\n}\n
hash_map_test.go
/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor key, value := range hmap {\nfmt.Println(key, \"->\", value)\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nfor key := range hmap {\nfmt.Println(key)\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor _, value := range hmap {\nfmt.Println(value)\n}\n
hash_map.swift
/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nfor (key, value) in map {\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.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.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
hash_map.rs
/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nfor (key, value) in &map {\nprintln!(\"{key} -> {value}\");\n}\n// \u5355\u72ec\u904d\u5386\u952e Key\nfor key in map.keys() {\nprintln!(\"{key}\"); }\n// \u5355\u72ec\u904d\u5386\u503c Value\nfor value in map.values() {\nprintln!(\"{value}\");\n}\n
hash_map.c
// C \u672a\u63d0\u4f9b\u5185\u7f6e\u54c8\u5e0c\u8868\n
hash_map.zig
\n
"},{"location":"chapter_hashing/hash_map/#612","title":"6.1.2 \u00a0 \u54c8\u5e0c\u8868\u7b80\u5355\u5b9e\u73b0","text":"

\u6211\u4eec\u5148\u8003\u8651\u6700\u7b80\u5355\u7684\u60c5\u51b5\uff0c\u4ec5\u7528\u4e00\u4e2a\u6570\u7ec4\u6765\u5b9e\u73b0\u54c8\u5e0c\u8868\u3002\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u6211\u4eec\u5c06\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u7a7a\u4f4d\u79f0\u4e3a\u300c\u6876 bucket\u300d\uff0c\u6bcf\u4e2a\u6876\u53ef\u5b58\u50a8\u4e00\u4e2a\u952e\u503c\u5bf9\u3002\u56e0\u6b64\uff0c\u67e5\u8be2\u64cd\u4f5c\u5c31\u662f\u627e\u5230 key \u5bf9\u5e94\u7684\u6876\uff0c\u5e76\u5728\u6876\u4e2d\u83b7\u53d6 value \u3002

\u90a3\u4e48\uff0c\u5982\u4f55\u57fa\u4e8e key \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\u5728\u6570\u7ec4\u4e2d\u7684\u5b58\u50a8\u4f4d\u7f6e\u3002

\u8f93\u5165\u4e00\u4e2a key \uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u8ba1\u7b97\u8fc7\u7a0b\u5206\u4e3a\u4ee5\u4e0b\u4e24\u6b65\u3002

  1. \u901a\u8fc7\u67d0\u79cd\u54c8\u5e0c\u7b97\u6cd5 hash() \u8ba1\u7b97\u5f97\u5230\u54c8\u5e0c\u503c\u3002
  2. \u5c06\u54c8\u5e0c\u503c\u5bf9\u6876\u6570\u91cf\uff08\u6570\u7ec4\u957f\u5ea6\uff09capacity \u53d6\u6a21\uff0c\u4ece\u800c\u83b7\u53d6\u8be5 key \u5bf9\u5e94\u7684\u6570\u7ec4\u7d22\u5f15 index \u3002
index = hash(key) % capacity\n

\u968f\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5229\u7528 index \u5728\u54c8\u5e0c\u8868\u4e2d\u8bbf\u95ee\u5bf9\u5e94\u7684\u6876\uff0c\u4ece\u800c\u83b7\u53d6 value \u3002

\u8bbe\u6570\u7ec4\u957f\u5ea6 capacity = 100\u3001\u54c8\u5e0c\u7b97\u6cd5 hash(key) = key \uff0c\u6613\u5f97\u54c8\u5e0c\u51fd\u6570\u4e3a key % 100 \u3002\u56fe 6-2 \u4ee5 key \u5b66\u53f7\u548c value \u59d3\u540d\u4e3a\u4f8b\uff0c\u5c55\u793a\u4e86\u54c8\u5e0c\u51fd\u6570\u7684\u5de5\u4f5c\u539f\u7406\u3002

\u56fe 6-2 \u00a0 \u54c8\u5e0c\u51fd\u6570\u5de5\u4f5c\u539f\u7406

\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u5355\u54c8\u5e0c\u8868\u3002\u5176\u4e2d\uff0c\u6211\u4eec\u5c06 key \u548c value \u5c01\u88c5\u6210\u4e00\u4e2a\u7c7b Pair \uff0c\u4ee5\u8868\u793a\u952e\u503c\u5bf9\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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 \"\";\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.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.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 readonly 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(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.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.swift
/* \u952e\u503c\u5bf9 */\nclass Pair: Equatable {\npublic var key: Int\npublic var val: String\npublic init(key: Int, val: String) {\nself.key = key\nself.val = val\n}\npublic static func == (lhs: Pair, rhs: Pair) -> Bool {\nlhs.key == rhs.key && lhs.val == rhs.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.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) {\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) {\nconsole.info(`${pair.key} -> ${pair.val}`);\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
array_hash_map.rs
/* \u952e\u503c\u5bf9 */\n#[derive(Debug, Clone, PartialEq)]\npub struct Pair {\npub key: i32,\npub val: String,\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\npub struct ArrayHashMap {\nbuckets: Vec<Option<Pair>>\n}\nimpl ArrayHashMap {\npub fn new() -> ArrayHashMap {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nSelf { buckets: vec![None; 100] }\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nfn hash_func(&self, key: i32) -> usize {\nkey as usize % 100\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npub fn get(&self, key: i32) -> Option<&String> {\nlet index = self.hash_func(key);\nself.buckets[index].as_ref().map(|pair| &pair.val)\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\npub fn put(&mut self, key: i32, val: &str) {\nlet index = self.hash_func(key);\nself.buckets[index] = Some(Pair {\nkey,\nval: val.to_string(),\n});\n}\n/* \u5220\u9664\u64cd\u4f5c */\npub fn remove(&mut self, key: i32) {\nlet index = self.hash_func(key);\n// \u7f6e\u4e3a None \uff0c\u4ee3\u8868\u5220\u9664\nself.buckets[index] = None;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\npub fn entry_set(&self) -> Vec<&Pair> {\nself.buckets.iter().filter_map(|pair| pair.as_ref()).collect()\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\npub fn key_set(&self) -> Vec<&i32> {\nself.buckets.iter().filter_map(|pair| pair.as_ref().map(|pair| &pair.key)).collect()\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\npub fn value_set(&self) -> Vec<&String> {\nself.buckets.iter().filter_map(|pair| pair.as_ref().map(|pair| &pair.val)).collect()\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\npub fn print(&self) {\nfor pair in self.entry_set() {\nprintln!(\"{} -> {}\", 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/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nstruct arrayHashMap {\npair *buckets[HASH_MAP_DEFAULT_SIZE];\n};\ntypedef struct arrayHashMap arrayHashMap;\n/* \u54c8\u5e0c\u8868\u521d\u59cb\u5316\u51fd\u6570 */\narrayHashMap *newArrayHashMap() {\narrayHashMap *map = malloc(sizeof(arrayHashMap));\nreturn map;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(arrayHashMap *d, const int key, const char *val) {\npair *pair = malloc(sizeof(pair));\npair->key = key;\npair->val = malloc(strlen(val) + 1);\nstrcpy(pair->val, val);\nint index = hashFunc(key);\nd->buckets[index] = pair;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid removeItem(arrayHashMap *d, const int key) {\nint index = hashFunc(key);\nfree(d->buckets[index]->val);\nfree(d->buckets[index]);\nd->buckets[index] = NULL;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\nvoid pairSet(arrayHashMap *d, mapSet *set) {\npair *entries;\nint i = 0, index = 0;\nint total = 0;\n/* \u7edf\u8ba1\u6709\u6548\u952e\u503c\u5bf9\u6570\u91cf */\nfor (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {\nif (d->buckets[i] != NULL) {\ntotal++;\n}\n}\nentries = malloc(sizeof(pair) * total);\nfor (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {\nif (d->buckets[i] != NULL) {\nentries[index].key = d->buckets[i]->key;\nentries[index].val = malloc(strlen(d->buckets[i]->val + 1));\nstrcpy(entries[index].val, d->buckets[i]->val);\nindex++;\n}\n}\nset->set = entries;\nset->len = total;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\nvoid keySet(arrayHashMap *d, mapSet *set) {\nint *keys;\nint i = 0, index = 0;\nint total = 0;\n/* \u7edf\u8ba1\u6709\u6548\u952e\u503c\u5bf9\u6570\u91cf */\nfor (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {\nif (d->buckets[i] != NULL) {\ntotal++;\n}\n}\nkeys = malloc(total * sizeof(int));\nfor (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {\nif (d->buckets[i] != NULL) {\nkeys[index] = d->buckets[i]->key;\nindex++;\n}\n}\nset->set = keys;\nset->len = total;\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\nvoid valueSet(arrayHashMap *d, mapSet *set) {\nchar **vals;\nint i = 0, index = 0;\nint total = 0;\n/* \u7edf\u8ba1\u6709\u6548\u952e\u503c\u5bf9\u6570\u91cf */\nfor (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {\nif (d->buckets[i] != NULL) {\ntotal++;\n}\n}\nvals = malloc(total * sizeof(char *));\nfor (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {\nif (d->buckets[i] != NULL) {\nvals[index] = d->buckets[i]->val;\nindex++;\n}\n}\nset->set = vals;\nset->len = total;\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print(arrayHashMap *d) {\nint i;\nmapSet set;\npairSet(d, &set);\npair *entries = (pair *)set.set;\nfor (i = 0; i < set.len; i++) {\nprintf(\"%d -> %s\\n\", entries[i].key, entries[i].val);\n}\nfree(set.set);\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 {\nbucket: ?std.ArrayList(?T) = null,\nmem_allocator: std.mem.Allocator = undefined,\nconst Self = @This();\n// \u6784\u9020\u51fd\u6570\npub fn init(self: *Self, allocator: std.mem.Allocator) !void {\nself.mem_allocator = allocator;\n// \u521d\u59cb\u5316\u4e00\u4e2a\u957f\u5ea6\u4e3a 100 \u7684\u6876\uff08\u6570\u7ec4\uff09\nself.bucket = std.ArrayList(?T).init(self.mem_allocator);\nvar i: i32 = 0;\nwhile (i < 100) : (i += 1) {\ntry self.bucket.?.append(null);\n}\n}\n// \u6790\u6784\u51fd\u6570\npub fn deinit(self: *Self) void {\nif (self.bucket != null) self.bucket.?.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.bucket.?.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.bucket.?.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.bucket.?.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.bucket.?.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.bucket.?.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.bucket.?.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
"},{"location":"chapter_hashing/hash_map/#613","title":"6.1.3 \u00a0 \u54c8\u5e0c\u51b2\u7a81\u4e0e\u6269\u5bb9","text":"

\u672c\u8d28\u4e0a\u770b\uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5c06\u6240\u6709 key \u6784\u6210\u7684\u8f93\u5165\u7a7a\u95f4\u6620\u5c04\u5230\u6570\u7ec4\u6240\u6709\u7d22\u5f15\u6784\u6210\u7684\u8f93\u51fa\u7a7a\u95f4\uff0c\u800c\u8f93\u5165\u7a7a\u95f4\u5f80\u5f80\u8fdc\u5927\u4e8e\u8f93\u51fa\u7a7a\u95f4\u3002\u56e0\u6b64\uff0c\u7406\u8bba\u4e0a\u4e00\u5b9a\u5b58\u5728\u201c\u591a\u4e2a\u8f93\u5165\u5bf9\u5e94\u76f8\u540c\u8f93\u51fa\u201d\u7684\u60c5\u51b5\u3002

\u5bf9\u4e8e\u4e0a\u8ff0\u793a\u4f8b\u4e2d\u7684\u54c8\u5e0c\u51fd\u6570\uff0c\u5f53\u8f93\u5165\u7684 key \u540e\u4e24\u4f4d\u76f8\u540c\u65f6\uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u8f93\u51fa\u7ed3\u679c\u4e5f\u76f8\u540c\u3002\u4f8b\u5982\uff0c\u67e5\u8be2\u5b66\u53f7\u4e3a 12836 \u548c 20336 \u7684\u4e24\u4e2a\u5b66\u751f\u65f6\uff0c\u6211\u4eec\u5f97\u5230\uff1a

12836 % 100 = 36\n20336 % 100 = 36\n

\u5982\u56fe 6-3 \u6240\u793a\uff0c\u4e24\u4e2a\u5b66\u53f7\u6307\u5411\u4e86\u540c\u4e00\u4e2a\u59d3\u540d\uff0c\u8fd9\u663e\u7136\u662f\u4e0d\u5bf9\u7684\u3002\u6211\u4eec\u5c06\u8fd9\u79cd\u591a\u4e2a\u8f93\u5165\u5bf9\u5e94\u540c\u4e00\u8f93\u51fa\u7684\u60c5\u51b5\u79f0\u4e3a\u300c\u54c8\u5e0c\u51b2\u7a81 hash collision\u300d\u3002

\u56fe 6-3 \u00a0 \u54c8\u5e0c\u51b2\u7a81\u793a\u4f8b

\u5bb9\u6613\u60f3\u5230\uff0c\u54c8\u5e0c\u8868\u5bb9\u91cf \\(n\\) \u8d8a\u5927\uff0c\u591a\u4e2a key \u88ab\u5206\u914d\u5230\u540c\u4e00\u4e2a\u6876\u4e2d\u7684\u6982\u7387\u5c31\u8d8a\u4f4e\uff0c\u51b2\u7a81\u5c31\u8d8a\u5c11\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6269\u5bb9\u54c8\u5e0c\u8868\u6765\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\u3002

\u5982\u56fe 6-4 \u6240\u793a\uff0c\u6269\u5bb9\u524d\u952e\u503c\u5bf9 (136, A) \u548c (236, D) \u53d1\u751f\u51b2\u7a81\uff0c\u6269\u5bb9\u540e\u51b2\u7a81\u6d88\u5931\u3002

\u56fe 6-4 \u00a0 \u54c8\u5e0c\u8868\u6269\u5bb9

\u7c7b\u4f3c\u4e8e\u6570\u7ec4\u6269\u5bb9\uff0c\u54c8\u5e0c\u8868\u6269\u5bb9\u9700\u5c06\u6240\u6709\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u8fc1\u79fb\u81f3\u65b0\u54c8\u5e0c\u8868\uff0c\u975e\u5e38\u8017\u65f6\u3002\u5e76\u4e14\u7531\u4e8e\u54c8\u5e0c\u8868\u5bb9\u91cf capacity \u6539\u53d8\uff0c\u6211\u4eec\u9700\u8981\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u6765\u91cd\u65b0\u8ba1\u7b97\u6240\u6709\u952e\u503c\u5bf9\u7684\u5b58\u50a8\u4f4d\u7f6e\uff0c\u8fd9\u8fdb\u4e00\u6b65\u63d0\u9ad8\u4e86\u6269\u5bb9\u8fc7\u7a0b\u7684\u8ba1\u7b97\u5f00\u9500\u3002\u4e3a\u6b64\uff0c\u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u4f1a\u9884\u7559\u8db3\u591f\u5927\u7684\u54c8\u5e0c\u8868\u5bb9\u91cf\uff0c\u9632\u6b62\u9891\u7e41\u6269\u5bb9\u3002

\u300c\u8d1f\u8f7d\u56e0\u5b50 load factor\u300d\u662f\u54c8\u5e0c\u8868\u7684\u4e00\u4e2a\u91cd\u8981\u6982\u5ff5\uff0c\u5176\u5b9a\u4e49\u4e3a\u54c8\u5e0c\u8868\u7684\u5143\u7d20\u6570\u91cf\u9664\u4ee5\u6876\u6570\u91cf\uff0c\u7528\u4e8e\u8861\u91cf\u54c8\u5e0c\u51b2\u7a81\u7684\u4e25\u91cd\u7a0b\u5ea6\uff0c\u4e5f\u5e38\u88ab\u4f5c\u4e3a\u54c8\u5e0c\u8868\u6269\u5bb9\u7684\u89e6\u53d1\u6761\u4ef6\u3002\u4f8b\u5982\u5728 Java \u4e2d\uff0c\u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7 \\(0.75\\) \u65f6\uff0c\u7cfb\u7edf\u4f1a\u5c06\u54c8\u5e0c\u8868\u5bb9\u91cf\u6269\u5c55\u4e3a\u539f\u5148\u7684 \\(2\\) \u500d\u3002

"},{"location":"chapter_hashing/summary/","title":"6.4 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_hashing/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
  • \u8f93\u5165 key \uff0c\u54c8\u5e0c\u8868\u80fd\u591f\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u67e5\u8be2\u5230 value \uff0c\u6548\u7387\u975e\u5e38\u9ad8\u3002
  • \u5e38\u89c1\u7684\u54c8\u5e0c\u8868\u64cd\u4f5c\u5305\u62ec\u67e5\u8be2\u3001\u6dfb\u52a0\u952e\u503c\u5bf9\u3001\u5220\u9664\u952e\u503c\u5bf9\u548c\u904d\u5386\u54c8\u5e0c\u8868\u7b49\u3002
  • \u54c8\u5e0c\u51fd\u6570\u5c06 key \u6620\u5c04\u4e3a\u6570\u7ec4\u7d22\u5f15\uff0c\u4ece\u800c\u8bbf\u95ee\u5bf9\u5e94\u6876\u5e76\u83b7\u53d6 value \u3002
  • \u4e24\u4e2a\u4e0d\u540c\u7684 key \u53ef\u80fd\u5728\u7ecf\u8fc7\u54c8\u5e0c\u51fd\u6570\u540e\u5f97\u5230\u76f8\u540c\u7684\u6570\u7ec4\u7d22\u5f15\uff0c\u5bfc\u81f4\u67e5\u8be2\u7ed3\u679c\u51fa\u9519\uff0c\u8fd9\u79cd\u73b0\u8c61\u88ab\u79f0\u4e3a\u54c8\u5e0c\u51b2\u7a81\u3002
  • \u54c8\u5e0c\u8868\u5bb9\u91cf\u8d8a\u5927\uff0c\u54c8\u5e0c\u51b2\u7a81\u7684\u6982\u7387\u5c31\u8d8a\u4f4e\u3002\u56e0\u6b64\u53ef\u4ee5\u901a\u8fc7\u6269\u5bb9\u54c8\u5e0c\u8868\u6765\u7f13\u89e3\u54c8\u5e0c\u51b2\u7a81\u3002\u4e0e\u6570\u7ec4\u6269\u5bb9\u7c7b\u4f3c\uff0c\u54c8\u5e0c\u8868\u6269\u5bb9\u64cd\u4f5c\u7684\u5f00\u9500\u5f88\u5927\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
  • \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\u3002\u7136\u800c\uff0c\u94fe\u8868\u8fc7\u957f\u4f1a\u964d\u4f4e\u67e5\u8be2\u6548\u7387\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811\u6765\u63d0\u9ad8\u6548\u7387\u3002
  • \u5f00\u653e\u5bfb\u5740\u901a\u8fc7\u591a\u6b21\u63a2\u6d4b\u6765\u5904\u7406\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\uff0c\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\u8f83\u7ebf\u6027\u63a2\u6d4b\u66f4\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\u3002\u4f8b\u5982\uff0cJava \u7684 HashMap \u4f7f\u7528\u94fe\u5f0f\u5730\u5740\uff0c\u800c Python \u7684 Dict \u91c7\u7528\u5f00\u653e\u5bfb\u5740\u3002
  • \u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u6211\u4eec\u5e0c\u671b\u54c8\u5e0c\u7b97\u6cd5\u5177\u6709\u786e\u5b9a\u6027\u3001\u9ad8\u6548\u7387\u548c\u5747\u5300\u5206\u5e03\u7684\u7279\u70b9\u3002\u5728\u5bc6\u7801\u5b66\u4e2d\uff0c\u54c8\u5e0c\u7b97\u6cd5\u8fd8\u5e94\u8be5\u5177\u5907\u6297\u78b0\u649e\u6027\u548c\u96ea\u5d29\u6548\u5e94\u3002
  • \u54c8\u5e0c\u7b97\u6cd5\u901a\u5e38\u91c7\u7528\u5927\u8d28\u6570\u4f5c\u4e3a\u6a21\u6570\uff0c\u4ee5\u6700\u5927\u5316\u5730\u4fdd\u8bc1\u54c8\u5e0c\u503c\u7684\u5747\u5300\u5206\u5e03\uff0c\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\u3002
  • \u5e38\u89c1\u7684\u54c8\u5e0c\u7b97\u6cd5\u5305\u62ec MD5\u3001SHA-1\u3001SHA-2 \u548c SHA3 \u7b49\u3002MD5 \u5e38\u7528\u4e8e\u6821\u9a8c\u6587\u4ef6\u5b8c\u6574\u6027\uff0cSHA-2 \u5e38\u7528\u4e8e\u5b89\u5168\u5e94\u7528\u4e0e\u534f\u8bae\u3002
  • \u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u4f1a\u4e3a\u6570\u636e\u7c7b\u578b\u63d0\u4f9b\u5185\u7f6e\u54c8\u5e0c\u7b97\u6cd5\uff0c\u7528\u4e8e\u8ba1\u7b97\u54c8\u5e0c\u8868\u4e2d\u7684\u6876\u7d22\u5f15\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u53ea\u6709\u4e0d\u53ef\u53d8\u5bf9\u8c61\u662f\u53ef\u54c8\u5e0c\u7684\u3002
"},{"location":"chapter_hashing/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\u4ec0\u4e48\u4e0d\u662f \\(O(n)\\) \uff1f

\u5f53\u54c8\u5e0c\u51b2\u7a81\u6bd4\u8f83\u4e25\u91cd\u65f6\uff0c\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f1a\u9000\u5316\u81f3 \\(O(n)\\) \u3002\u5f53\u54c8\u5e0c\u51fd\u6570\u8bbe\u8ba1\u7684\u6bd4\u8f83\u597d\u3001\u5bb9\u91cf\u8bbe\u7f6e\u6bd4\u8f83\u5408\u7406\u3001\u51b2\u7a81\u6bd4\u8f83\u5e73\u5747\u65f6\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u3002\u6211\u4eec\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u5185\u7f6e\u7684\u54c8\u5e0c\u8868\u65f6\uff0c\u901a\u5e38\u8ba4\u4e3a\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u3002

\u4e3a\u4ec0\u4e48\u4e0d\u4f7f\u7528\u54c8\u5e0c\u51fd\u6570 \\(f(x) = x\\) \u5462\uff1f\u8fd9\u6837\u5c31\u4e0d\u4f1a\u6709\u51b2\u7a81\u4e86

\u5728 \\(f(x) = x\\) \u54c8\u5e0c\u51fd\u6570\u4e0b\uff0c\u6bcf\u4e2a\u5143\u7d20\u5bf9\u5e94\u552f\u4e00\u7684\u6876\u7d22\u5f15\uff0c\u8fd9\u4e0e\u6570\u7ec4\u7b49\u4ef7\u3002\u7136\u800c\uff0c\u8f93\u5165\u7a7a\u95f4\u901a\u5e38\u8fdc\u5927\u4e8e\u8f93\u51fa\u7a7a\u95f4\uff08\u6570\u7ec4\u957f\u5ea6\uff09\uff0c\u56e0\u6b64\u54c8\u5e0c\u51fd\u6570\u7684\u6700\u540e\u4e00\u6b65\u5f80\u5f80\u662f\u5bf9\u6570\u7ec4\u957f\u5ea6\u53d6\u6a21\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u54c8\u5e0c\u8868\u7684\u76ee\u6807\u662f\u5c06\u4e00\u4e2a\u8f83\u5927\u7684\u72b6\u6001\u7a7a\u95f4\u6620\u5c04\u5230\u4e00\u4e2a\u8f83\u5c0f\u7684\u7a7a\u95f4\uff0c\u5e76\u63d0\u4f9b \\(O(1)\\) \u7684\u67e5\u8be2\u6548\u7387\u3002

\u54c8\u5e0c\u8868\u5e95\u5c42\u5b9e\u73b0\u662f\u6570\u7ec4\u3001\u94fe\u8868\u3001\u4e8c\u53c9\u6811\uff0c\u4f46\u4e3a\u4ec0\u4e48\u6548\u7387\u53ef\u4ee5\u6bd4\u4ed6\u4eec\u66f4\u9ad8\u5462\uff1f

\u9996\u5148\uff0c\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u6548\u7387\u53d8\u9ad8\uff0c\u4f46\u7a7a\u95f4\u6548\u7387\u53d8\u4f4e\u4e86\u3002\u54c8\u5e0c\u8868\u6709\u76f8\u5f53\u4e00\u90e8\u5206\u7684\u5185\u5b58\u662f\u672a\u4f7f\u7528\u7684\uff0c

\u5176\u6b21\uff0c\u53ea\u662f\u5728\u7279\u5b9a\u4f7f\u7528\u573a\u666f\u4e0b\u65f6\u95f4\u6548\u7387\u53d8\u9ad8\u4e86\u3002\u5982\u679c\u4e00\u4e2a\u529f\u80fd\u80fd\u591f\u5728\u76f8\u540c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e0b\u4f7f\u7528\u6570\u7ec4\u6216\u94fe\u8868\u5b9e\u73b0\uff0c\u90a3\u4e48\u901a\u5e38\u6bd4\u54c8\u5e0c\u8868\u66f4\u5feb\u3002\u8fd9\u662f\u56e0\u4e3a\u54c8\u5e0c\u51fd\u6570\u8ba1\u7b97\u9700\u8981\u5f00\u9500\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u7684\u5e38\u6570\u9879\u66f4\u5927\u3002

\u6700\u540e\uff0c\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u80fd\u53d1\u751f\u52a3\u5316\u3002\u4f8b\u5982\u5728\u94fe\u5f0f\u5730\u5740\u4e2d\uff0c\u6211\u4eec\u91c7\u53d6\u5728\u94fe\u8868\u6216\u7ea2\u9ed1\u6811\u4e2d\u6267\u884c\u67e5\u627e\u64cd\u4f5c\uff0c\u4ecd\u7136\u6709\u9000\u5316\u81f3 \\(O(n)\\) \u65f6\u95f4\u7684\u98ce\u9669\u3002

\u591a\u6b21\u54c8\u5e0c\u6709\u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5143\u7d20\u7684\u7f3a\u9677\u5417\uff1f\u5bf9\u4e8e\u6807\u8bb0\u5df2\u5220\u9664\u7684\u7a7a\u95f4\uff0c\u8fd9\u4e2a\u7a7a\u95f4\u8fd8\u80fd\u518d\u6b21\u4f7f\u7528\u5417\uff1f

\u591a\u6b21\u54c8\u5e0c\u662f\u5f00\u653e\u5bfb\u5740\u7684\u4e00\u79cd\uff0c\u5f00\u653e\u5bfb\u5740\u6cd5\u90fd\u6709\u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5143\u7d20\u7684\u7f3a\u9677\uff0c\u9700\u8981\u901a\u8fc7\u6807\u8bb0\u5220\u9664\u3002\u88ab\u6807\u8bb0\u4e3a\u5df2\u5220\u9664\u7684\u7a7a\u95f4\u662f\u53ef\u4ee5\u518d\u6b21\u88ab\u4f7f\u7528\u7684\u3002\u5f53\u5c06\u65b0\u5143\u7d20\u63d2\u5165\u54c8\u5e0c\u8868\uff0c\u5e76\u4e14\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u627e\u5230\u4e86\u88ab\u6807\u8bb0\u4e3a\u5df2\u5220\u9664\u7684\u4f4d\u7f6e\u65f6\uff0c\u8be5\u4f4d\u7f6e\u53ef\u4ee5\u88ab\u65b0\u7684\u5143\u7d20\u4f7f\u7528\u3002\u8fd9\u6837\u505a\u65e2\u80fd\u4fdd\u6301\u54c8\u5e0c\u8868\u7684\u63a2\u6d4b\u5e8f\u5217\u4e0d\u53d8\uff0c\u53c8\u80fd\u4fdd\u8bc1\u54c8\u5e0c\u8868\u7684\u7a7a\u95f4\u4f7f\u7528\u7387\u3002

\u4e3a\u4ec0\u4e48\u5728\u7ebf\u6027\u63a2\u6d4b\u4e2d\uff0c\u67e5\u627e\u5143\u7d20\u7684\u65f6\u5019\u4f1a\u51fa\u73b0\u54c8\u5e0c\u51b2\u7a81\u5462\uff1f

\u67e5\u627e\u7684\u65f6\u5019\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u627e\u5230\u5bf9\u5e94\u7684\u6876\u548c\u952e\u503c\u5bf9\uff0c\u53d1\u73b0 key \u4e0d\u5339\u914d\uff0c\u8fd9\u5c31\u4ee3\u8868\u6709\u54c8\u5e0c\u51b2\u7a81\u3002\u56e0\u6b64\uff0c\u7ebf\u6027\u63a2\u6d4b\u6cd5\u4f1a\u6839\u636e\u9884\u5148\u8bbe\u5b9a\u7684\u6b65\u957f\u4f9d\u6b21\u5411\u4e0b\u67e5\u627e\uff0c\u76f4\u81f3\u627e\u5230\u6b63\u786e\u7684\u952e\u503c\u5bf9\u6216\u65e0\u6cd5\u627e\u5230\u8df3\u51fa\u4e3a\u6b62\u3002

\u4e3a\u4ec0\u4e48\u54c8\u5e0c\u8868\u6269\u5bb9\u80fd\u591f\u7f13\u89e3\u54c8\u5e0c\u51b2\u7a81\uff1f

\u54c8\u5e0c\u51fd\u6570\u7684\u6700\u540e\u4e00\u6b65\u5f80\u5f80\u662f\u5bf9\u6570\u7ec4\u957f\u5ea6 \\(n\\) \u53d6\u4f59\uff0c\u8ba9\u8f93\u51fa\u503c\u843d\u5165\u5728\u6570\u7ec4\u7d22\u5f15\u8303\u56f4\uff1b\u5728\u6269\u5bb9\u540e\uff0c\u6570\u7ec4\u957f\u5ea6 \\(n\\) \u53d1\u751f\u53d8\u5316\uff0c\u800c key \u5bf9\u5e94\u7684\u7d22\u5f15\u4e5f\u53ef\u80fd\u53d1\u751f\u53d8\u5316\u3002\u539f\u5148\u843d\u5728\u540c\u4e00\u4e2a\u6876\u7684\u591a\u4e2a key \uff0c\u5728\u6269\u5bb9\u540e\u53ef\u80fd\u4f1a\u88ab\u5206\u914d\u5230\u591a\u4e2a\u6876\u4e2d\uff0c\u4ece\u800c\u5b9e\u73b0\u54c8\u5e0c\u51b2\u7a81\u7684\u7f13\u89e3\u3002

"},{"location":"chapter_heap/","title":"\u7b2c 8 \u7ae0 \u00a0 \u5806","text":"

Abstract

\u5806\u5c31\u50cf\u662f\u5c71\u5ddd\u7684\u5cf0\u5ce6\uff0c\u5b83\u4eec\u5c42\u53e0\u8d77\u4f0f\u3001\u5f62\u6001\u5404\u5f02\u3002

\u6bcf\u4e00\u5ea7\u5c71\u5cf0\u90fd\u6709\u5176\u9ad8\u4f4e\u4e4b\u5206\uff0c\u800c\u6700\u9ad8\u7684\u5c71\u5cf0\u603b\u662f\u6700\u5148\u6620\u5165\u773c\u5e18\u3002

"},{"location":"chapter_heap/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 8.1 \u00a0 \u5806
  • 8.2 \u00a0 \u5efa\u5806\u64cd\u4f5c
  • 8.3 \u00a0 Top-K \u95ee\u9898
  • 8.4 \u00a0 \u5c0f\u7ed3
"},{"location":"chapter_heap/build_heap/","title":"8.2 \u00a0 \u5efa\u5806\u64cd\u4f5c","text":"

\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u4f7f\u7528\u4e00\u4e2a\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\u6765\u6784\u5efa\u4e00\u4e2a\u5806\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a\u201c\u5efa\u5806\u64cd\u4f5c\u201d\u3002

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

\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u7a7a\u5806\uff0c\u7136\u540e\u904d\u5386\u5217\u8868\uff0c\u4f9d\u6b21\u5bf9\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u201c\u5165\u5806\u64cd\u4f5c\u201d\uff0c\u5373\u5148\u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u5806\u7684\u5c3e\u90e8\uff0c\u518d\u5bf9\u8be5\u5143\u7d20\u6267\u884c\u201c\u4ece\u5e95\u81f3\u9876\u201d\u5806\u5316\u3002

\u6bcf\u5f53\u4e00\u4e2a\u5143\u7d20\u5165\u5806\uff0c\u5806\u7684\u957f\u5ea6\u5c31\u52a0\u4e00\u3002\u7531\u4e8e\u8282\u70b9\u662f\u4ece\u9876\u5230\u5e95\u4f9d\u6b21\u88ab\u6dfb\u52a0\u8fdb\u4e8c\u53c9\u6811\u7684\uff0c\u56e0\u6b64\u5806\u662f\u201c\u81ea\u4e0a\u800c\u4e0b\u201d\u5730\u6784\u5efa\u7684\u3002

\u8bbe\u5143\u7d20\u6570\u91cf\u4e3a \\(n\\) \uff0c\u6bcf\u4e2a\u5143\u7d20\u7684\u5165\u5806\u64cd\u4f5c\u4f7f\u7528 \\(O(\\log{n})\\) \u65f6\u95f4\uff0c\u56e0\u6b64\u8be5\u5efa\u5806\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002

"},{"location":"chapter_heap/build_heap/#822","title":"8.2.2 \u00a0 \u901a\u8fc7\u904d\u5386\u5806\u5316\u5b9e\u73b0","text":"

\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u4e00\u79cd\u66f4\u4e3a\u9ad8\u6548\u7684\u5efa\u5806\u65b9\u6cd5\uff0c\u5171\u5206\u4e3a\u4e24\u6b65\u3002

  1. \u5c06\u5217\u8868\u6240\u6709\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u5230\u5806\u4e2d\uff0c\u6b64\u65f6\u5806\u7684\u6027\u8d28\u5c1a\u672a\u5f97\u5230\u6ee1\u8db3\u3002
  2. \u5012\u5e8f\u904d\u5386\u5806\uff08\u5373\u5c42\u5e8f\u904d\u5386\u7684\u5012\u5e8f\uff09\uff0c\u4f9d\u6b21\u5bf9\u6bcf\u4e2a\u975e\u53f6\u8282\u70b9\u6267\u884c\u201c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u201d\u3002

\u6bcf\u5f53\u5806\u5316\u4e00\u4e2a\u8282\u70b9\u540e\uff0c\u4ee5\u8be5\u8282\u70b9\u4e3a\u6839\u8282\u70b9\u7684\u5b50\u6811\u5c31\u5f62\u6210\u4e00\u4e2a\u5408\u6cd5\u7684\u5b50\u5806\u3002\u800c\u7531\u4e8e\u662f\u5012\u5e8f\u904d\u5386\uff0c\u56e0\u6b64\u5806\u662f\u201c\u81ea\u4e0b\u800c\u4e0a\u201d\u5730\u88ab\u6784\u5efa\u7684\u3002

\u4e4b\u6240\u4ee5\u9009\u62e9\u5012\u5e8f\u904d\u5386\uff0c\u662f\u56e0\u4e3a\u8fd9\u6837\u80fd\u591f\u4fdd\u8bc1\u5f53\u524d\u8282\u70b9\u4e4b\u4e0b\u7684\u5b50\u6811\u5df2\u7ecf\u662f\u5408\u6cd5\u7684\u5b50\u5806\uff0c\u8fd9\u6837\u5806\u5316\u5f53\u524d\u8282\u70b9\u624d\u662f\u6709\u6548\u7684\u3002

\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u53f6\u8282\u70b9\u6ca1\u6709\u5b50\u8282\u70b9\uff0c\u5929\u7136\u5c31\u662f\u5408\u6cd5\u7684\u5b50\u5806\uff0c\u56e0\u6b64\u65e0\u9700\u5806\u5316\u3002\u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u6700\u540e\u4e00\u4e2a\u975e\u53f6\u8282\u70b9\u662f\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u7684\u7236\u8282\u70b9\uff0c\u6211\u4eec\u4ece\u5b83\u5f00\u59cb\u5012\u5e8f\u904d\u5386\u5e76\u6267\u884c\u5806\u5316\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig my_heap.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.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.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.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.go
/* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u5207\u7247\u5efa\u5806 */\nfunc newMaxHeap(nums []any) *maxHeap {\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\nh := &maxHeap{data: nums}\nfor i := h.parent(len(h.data) - 1); i >= 0; i-- {\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nh.siftDown(i)\n}\nreturn h\n}\n
my_heap.swift
/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\ninit(nums: [Int]) {\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\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.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.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--) {\nsiftDown(i);\n}\n}\n
my_heap.rs
/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nfn new(nums: Vec<i32>) -> Self {\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\nlet mut heap = MaxHeap { max_heap: nums };\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor i in (0..=Self::parent(heap.size() - 1)).rev() {\nheap.sift_down(i);\n}\nheap\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 = parent(h, 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.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
"},{"location":"chapter_heap/build_heap/#823","title":"8.2.3 \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"

\u4e0b\u9762\uff0c\u6211\u4eec\u6765\u5c1d\u8bd5\u63a8\u7b97\u7b2c\u4e8c\u79cd\u5efa\u5806\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002

  • \u5047\u8bbe\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u5219\u53f6\u8282\u70b9\u6570\u91cf\u4e3a \\((n + 1) / 2\\) \uff0c\u5176\u4e2d \\(/\\) \u4e3a\u5411\u4e0b\u6574\u9664\u3002\u56e0\u6b64\u9700\u8981\u5806\u5316\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\((n - 1) / 2\\) \u3002
  • \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 \\(\\log n\\) \u3002

\u5c06\u4e0a\u8ff0\u4e24\u8005\u76f8\u4e58\uff0c\u53ef\u5f97\u5230\u5efa\u5806\u8fc7\u7a0b\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002\u4f46\u8fd9\u4e2a\u4f30\u7b97\u7ed3\u679c\u5e76\u4e0d\u51c6\u786e\uff0c\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u8003\u8651\u5230\u4e8c\u53c9\u6811\u5e95\u5c42\u8282\u70b9\u6570\u91cf\u8fdc\u591a\u4e8e\u9876\u5c42\u8282\u70b9\u7684\u6027\u8d28\u3002

\u63a5\u4e0b\u6765\u6211\u4eec\u6765\u8fdb\u884c\u66f4\u4e3a\u51c6\u786e\u7684\u8ba1\u7b97\u3002\u4e3a\u4e86\u51cf\u5c0f\u8ba1\u7b97\u96be\u5ea6\uff0c\u5047\u8bbe\u7ed9\u5b9a\u4e00\u4e2a\u8282\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u9ad8\u5ea6\u4e3a \\(h\\) \u7684\u201c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u201d\uff0c\u8be5\u5047\u8bbe\u4e0d\u4f1a\u5f71\u54cd\u8ba1\u7b97\u7ed3\u679c\u7684\u6b63\u786e\u6027\u3002

\u56fe 8-5 \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u5404\u5c42\u8282\u70b9\u6570\u91cf

\u5982\u56fe 8-5 \u6240\u793a\uff0c\u8282\u70b9\u201c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u201d\u7684\u6700\u5927\u8fed\u4ee3\u6b21\u6570\u7b49\u4e8e\u8be5\u8282\u70b9\u5230\u53f6\u8282\u70b9\u7684\u8ddd\u79bb\uff0c\u800c\u8be5\u8ddd\u79bb\u6b63\u662f\u201c\u8282\u70b9\u9ad8\u5ea6\u201d\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\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) + \\dots + 2^{(h-1)}\\times1 \\]

\u5316\u7b80\u4e0a\u5f0f\u9700\u8981\u501f\u52a9\u4e2d\u5b66\u7684\u6570\u5217\u77e5\u8bc6\uff0c\u5148\u5bf9 \\(T(h)\\) \u4e58\u4ee5 \\(2\\) \uff0c\u5f97\u5230\uff1a

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

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

\\[ 2T(h) - T(h) = T(h) = -2^0h + 2^1 + 2^2 + \\dots + 2^{h-1} + 2^h \\]

\u89c2\u5bdf\u4e0a\u5f0f\uff0c\u53d1\u73b0 \\(T(h)\\) \u662f\u4e00\u4e2a\u7b49\u6bd4\u6570\u5217\uff0c\u53ef\u76f4\u63a5\u4f7f\u7528\u6c42\u548c\u516c\u5f0f\uff0c\u5f97\u5230\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\uff1a

\\[ \\begin{aligned} T(h) & = 2 \\frac{1 - 2^h}{1 - 2} - h \\newline & = 2^{h+1} - h - 2 \\newline & = O(2^h) \\end{aligned} \\]

\u8fdb\u4e00\u6b65\u5730\uff0c\u9ad8\u5ea6\u4e3a \\(h\\) \u7684\u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(n = 2^{h+1} - 1\\) \uff0c\u6613\u5f97\u590d\u6742\u5ea6\u4e3a \\(O(2^h) = O(n)\\) \u3002\u4ee5\u4e0a\u63a8\u7b97\u8868\u660e\uff0c\u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u975e\u5e38\u9ad8\u6548\u3002

"},{"location":"chapter_heap/heap/","title":"8.1 \u00a0 \u5806","text":"

\u300c\u5806 heap\u300d\u662f\u4e00\u79cd\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u4e3b\u8981\u53ef\u5206\u4e3a\u56fe 8-1 \u6240\u793a\u7684\u4e24\u79cd\u7c7b\u578b\u3002

  • \u300c\u5927\u9876\u5806 max heap\u300d\uff1a\u4efb\u610f\u8282\u70b9\u7684\u503c \\(\\geq\\) \u5176\u5b50\u8282\u70b9\u7684\u503c\u3002
  • \u300c\u5c0f\u9876\u5806 min heap\u300d\uff1a\u4efb\u610f\u8282\u70b9\u7684\u503c \\(\\leq\\) \u5176\u5b50\u8282\u70b9\u7684\u503c\u3002

\u56fe 8-1 \u00a0 \u5c0f\u9876\u5806\u4e0e\u5927\u9876\u5806

\u5806\u4f5c\u4e3a\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u4e00\u4e2a\u7279\u4f8b\uff0c\u5177\u6709\u4ee5\u4e0b\u7279\u6027\u3002

  • \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\u201c\u5806\u9876\u201d\uff0c\u5c06\u5e95\u5c42\u6700\u9760\u53f3\u7684\u8282\u70b9\u79f0\u4e3a\u201c\u5806\u5e95\u201d\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\u201c\u4f18\u5148\u961f\u5217\u201d\u548c\u201c\u5806\u201d\u770b\u4f5c\u7b49\u4ef7\u7684\u6570\u636e\u7ed3\u6784\u3002\u56e0\u6b64\uff0c\u672c\u4e66\u5bf9\u4e24\u8005\u4e0d\u505a\u7279\u522b\u533a\u5206\uff0c\u7edf\u4e00\u4f7f\u7528\u201c\u5806\u201c\u6765\u547d\u540d\u3002

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

\u8868 8-1 \u00a0 \u5806\u7684\u64cd\u4f5c\u6548\u7387

\u65b9\u6cd5\u540d \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 push() \u5143\u7d20\u5165\u5806 \\(O(\\log n)\\) pop() \u5806\u9876\u5143\u7d20\u51fa\u5806 \\(O(\\log n)\\) peek() \u8bbf\u95ee\u5806\u9876\u5143\u7d20\uff08\u5927 / \u5c0f\u9876\u5806\u5206\u522b\u4e3a\u6700\u5927 / \u5c0f\u503c\uff09 \\(O(1)\\) size() \u83b7\u53d6\u5806\u7684\u5143\u7d20\u6570\u91cf \\(O(1)\\) isEmpty() \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a \\(O(1)\\)

\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b\u7684\u5806\u7c7b\uff08\u6216\u4f18\u5148\u961f\u5217\u7c7b\uff09\u3002

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

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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 = maxHeap.poll(); // 5\npeek = maxHeap.poll(); // 4\npeek = maxHeap.poll(); // 3\npeek = maxHeap.poll(); // 2\npeek = maxHeap.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.cs
/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nPriorityQueue<int, int> minHeap = new();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\uff08\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u4fee\u6539 Comparator \u5373\u53ef\uff09\nPriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => y - x));\n/* \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.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.swift
// Swift \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.js
// JavaScript \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.ts
// TypeScript \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.dart
// Dart \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.rs
use std::collections::BinaryHeap;\nuse std::cmp::Reverse;\n/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nlet mut min_heap = BinaryHeap::<Reverse<i32>>::new();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\nlet mut max_heap = BinaryHeap::new();\n/* \u5143\u7d20\u5165\u5806 */\nmax_heap.push(1);\nmax_heap.push(3);\nmax_heap.push(2);\nmax_heap.push(5);\nmax_heap.push(4);\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nlet peek = max_heap.peek().unwrap();  // 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\nlet peek = max_heap.pop().unwrap();   // 5\nlet peek = max_heap.pop().unwrap();   // 4\nlet peek = max_heap.pop().unwrap();   // 3\nlet peek = max_heap.pop().unwrap();   // 2\nlet peek = max_heap.pop().unwrap();   // 1\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nlet size = max_heap.len();\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = max_heap.is_empty();\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nlet min_heap = BinaryHeap::from(vec![Reverse(1), Reverse(3), Reverse(2), Reverse(5), Reverse(4)]);\n
heap.c
// C \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.zig
\n
"},{"location":"chapter_heap/heap/#812","title":"8.1.2 \u00a0 \u5806\u7684\u5b9e\u73b0","text":"

\u4e0b\u6587\u5b9e\u73b0\u7684\u662f\u5927\u9876\u5806\u3002\u82e5\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u5c0f\u9876\u5806\uff0c\u53ea\u9700\u5c06\u6240\u6709\u5927\u5c0f\u903b\u8f91\u5224\u65ad\u53d6\u9006\uff08\u4f8b\u5982\uff0c\u5c06 \\(\\geq\\) \u66ff\u6362\u4e3a \\(\\leq\\) \uff09\u3002\u611f\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u5b9e\u73b0\u3002

"},{"location":"chapter_heap/heap/#1","title":"1. \u00a0 \u5806\u7684\u5b58\u50a8\u4e0e\u8868\u793a","text":"

\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

\u5982\u56fe 8-2 \u6240\u793a\uff0c\u7ed9\u5b9a\u7d22\u5f15 \\(i\\) \uff0c\u5176\u5de6\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 1\\) \uff0c\u53f3\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 2\\) \uff0c\u7236\u8282\u70b9\u7d22\u5f15\u4e3a \\((i - 1) / 2\\)\uff08\u5411\u4e0b\u53d6\u6574\uff09\u3002\u5f53\u7d22\u5f15\u8d8a\u754c\u65f6\uff0c\u8868\u793a\u7a7a\u8282\u70b9\u6216\u8282\u70b9\u4e0d\u5b58\u5728\u3002

\u56fe 8-2 \u00a0 \u5806\u7684\u8868\u793a\u4e0e\u5b58\u50a8

\u6211\u4eec\u53ef\u4ee5\u5c06\u7d22\u5f15\u6620\u5c04\u516c\u5f0f\u5c01\u88c5\u6210\u51fd\u6570\uff0c\u65b9\u4fbf\u540e\u7eed\u4f7f\u7528\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
my_heap.rs
/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nfn left(i: usize) -> usize {\n2 * i + 1\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nfn right(i: usize) -> usize {\n2 * i + 2\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nfn parent(i: usize) -> usize {\n(i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.c
/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint left(maxHeap *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.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
"},{"location":"chapter_heap/heap/#2","title":"2. \u00a0 \u8bbf\u95ee\u5806\u9876\u5143\u7d20","text":"

\u5806\u9876\u5143\u7d20\u5373\u4e3a\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\uff0c\u4e5f\u5c31\u662f\u5217\u8868\u7684\u9996\u4e2a\u5143\u7d20\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig my_heap.py
def peek(self) -> int:\n\"\"\"\u8bbf\u95ee\u5806\u9876\u5143\u7d20\"\"\"\nreturn self.max_heap[0]\n
my_heap.cpp
/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\nreturn maxHeap[0];\n}\n
my_heap.java
/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\nreturn maxHeap.get(0);\n}\n
my_heap.cs
/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint Peek() {\nreturn maxHeap[0];\n}\n
my_heap.go
/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunc (h *maxHeap) peek() any {\nreturn h.data[0]\n}\n
my_heap.swift
/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunc peek() -> Int {\nmaxHeap[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.dart
/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\nreturn _maxHeap[0];\n}\n
my_heap.rs
/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfn peek(&self) -> Option<i32> {\nself.max_heap.first().copied()\n}\n
my_heap.c
/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek(maxHeap *h) {\nreturn h->data[0];\n}\n
my_heap.zig
// \u8bbf\u95ee\u5806\u9876\u5143\u7d20\nfn peek(self: *Self) T {\nreturn self.max_heap.?.items[0];\n}  
"},{"location":"chapter_heap/heap/#3","title":"3. \u00a0 \u5143\u7d20\u5165\u5806","text":"

\u7ed9\u5b9a\u5143\u7d20 val \uff0c\u6211\u4eec\u9996\u5148\u5c06\u5176\u6dfb\u52a0\u5230\u5806\u5e95\u3002\u6dfb\u52a0\u4e4b\u540e\uff0c\u7531\u4e8e val \u53ef\u80fd\u5927\u4e8e\u5806\u4e2d\u5176\u4ed6\u5143\u7d20\uff0c\u5806\u7684\u6210\u7acb\u6761\u4ef6\u53ef\u80fd\u5df2\u88ab\u7834\u574f\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\u5982\u56fe 8-3 \u6240\u793a\uff0c\u6211\u4eec\u6bd4\u8f83\u63d2\u5165\u8282\u70b9\u4e0e\u5176\u7236\u8282\u70b9\u7684\u503c\uff0c\u5982\u679c\u63d2\u5165\u8282\u70b9\u66f4\u5927\uff0c\u5219\u5c06\u5b83\u4eec\u4ea4\u6362\u3002\u7136\u540e\u7ee7\u7eed\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u4ece\u5e95\u81f3\u9876\u4fee\u590d\u5806\u4e2d\u7684\u5404\u4e2a\u8282\u70b9\uff0c\u76f4\u81f3\u8d8a\u8fc7\u6839\u8282\u70b9\u6216\u9047\u5230\u65e0\u987b\u4ea4\u6362\u7684\u8282\u70b9\u65f6\u7ed3\u675f\u3002

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

\u56fe 8-3 \u00a0 \u5143\u7d20\u5165\u5806\u6b65\u9aa4

\u8bbe\u8282\u70b9\u603b\u6570\u4e3a \\(n\\) \uff0c\u5219\u6811\u7684\u9ad8\u5ea6\u4e3a \\(O(\\log n)\\) \u3002\u7531\u6b64\u53ef\u77e5\uff0c\u5806\u5316\u64cd\u4f5c\u7684\u5faa\u73af\u8f6e\u6570\u6700\u591a\u4e3a \\(O(\\log n)\\) \uff0c\u5143\u7d20\u5165\u5806\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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\u987b\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.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\u987b\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.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\u987b\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.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\u987b\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.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\u987b\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.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\u987b\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.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\u987b\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\u987b\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.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\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\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p < 0 || _maxHeap[i] <= _maxHeap[p]) {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\n_swap(i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n
my_heap.rs
/* \u5143\u7d20\u5165\u5806 */\nfn push(&mut self, val: i32) {\n// \u6dfb\u52a0\u8282\u70b9\nself.max_heap.push(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nself.sift_up(self.size() - 1);\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfn sift_up(&mut self, mut i: usize) {\nloop {\n// \u8282\u70b9 i \u5df2\u7ecf\u662f\u5806\u9876\u8282\u70b9\u4e86\uff0c\u7ed3\u675f\u5806\u5316\nif i == 0 {\nbreak;\n}\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nlet p = Self::parent(i);\n// \u5f53\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif self.max_heap[i] <= self.max_heap[p] {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nself.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\u987b\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.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\u987b\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
"},{"location":"chapter_heap/heap/#4","title":"4. \u00a0 \u5806\u9876\u5143\u7d20\u51fa\u5806","text":"

\u5806\u9876\u5143\u7d20\u662f\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\uff0c\u5373\u5217\u8868\u9996\u5143\u7d20\u3002\u5982\u679c\u6211\u4eec\u76f4\u63a5\u4ece\u5217\u8868\u4e2d\u5220\u9664\u9996\u5143\u7d20\uff0c\u90a3\u4e48\u4e8c\u53c9\u6811\u4e2d\u6240\u6709\u8282\u70b9\u7684\u7d22\u5f15\u90fd\u4f1a\u53d1\u751f\u53d8\u5316\uff0c\u8fd9\u5c06\u4f7f\u5f97\u540e\u7eed\u4f7f\u7528\u5806\u5316\u4fee\u590d\u53d8\u5f97\u56f0\u96be\u3002\u4e3a\u4e86\u5c3d\u91cf\u51cf\u5c11\u5143\u7d20\u7d22\u5f15\u7684\u53d8\u52a8\uff0c\u6211\u4eec\u91c7\u7528\u4ee5\u4e0b\u64cd\u4f5c\u6b65\u9aa4\u3002

  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\u3002
  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\u3002
  3. \u4ece\u6839\u8282\u70b9\u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u6267\u884c\u5806\u5316\u3002

\u5982\u56fe 8-4 \u6240\u793a\uff0c\u201c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u201d\u7684\u64cd\u4f5c\u65b9\u5411\u4e0e\u201c\u4ece\u5e95\u81f3\u9876\u5806\u5316\u201d\u76f8\u53cd\uff0c\u6211\u4eec\u5c06\u6839\u8282\u70b9\u7684\u503c\u4e0e\u5176\u4e24\u4e2a\u5b50\u8282\u70b9\u7684\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u5c06\u6700\u5927\u7684\u5b50\u8282\u70b9\u4e0e\u6839\u8282\u70b9\u4ea4\u6362\u3002\u7136\u540e\u5faa\u73af\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u76f4\u5230\u8d8a\u8fc7\u53f6\u8282\u70b9\u6216\u9047\u5230\u65e0\u987b\u4ea4\u6362\u7684\u8282\u70b9\u65f6\u7ed3\u675f\u3002

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

\u56fe 8-4 \u00a0 \u5806\u9876\u5143\u7d20\u51fa\u5806\u6b65\u9aa4

\u4e0e\u5143\u7d20\u5165\u5806\u64cd\u4f5c\u76f8\u4f3c\uff0c\u5806\u9876\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u4e3a \\(O(\\log n)\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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\u987b\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.cpp
/* \u5143\u7d20\u51fa\u5806 */\nvoid pop() {\n// \u5224\u7a7a\u5904\u7406\nif (isEmpty()) {\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;\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\u987b\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.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\u987b\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.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.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\u987b\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.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\u987b\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.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\u987b\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\u987b\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.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\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);\nint r = _right(i);\nint ma = i;\nif (l < size() && _maxHeap[l] > _maxHeap[ma]) ma = l;\nif (r < size() && _maxHeap[r] > _maxHeap[ma]) ma = r;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\n_swap(i, ma);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n
my_heap.rs
/* \u5143\u7d20\u51fa\u5806 */\nfn pop(&mut self) -> i32 {\n// \u5224\u7a7a\u5904\u7406\nif self.is_empty() {\npanic!(\"index out of bounds\");\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\nself.swap(0, self.size() - 1);\n// \u5220\u9664\u8282\u70b9\nlet val = self.max_heap.remove(self.size() - 1);\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nself.sift_down(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nval\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfn sift_down(&mut self, mut i: usize) {\nloop {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nlet (l, r, mut ma) = (Self::left(i), Self::right(i), i);\nif l < self.size() && self.max_heap[l] > self.max_heap[ma] {\nma = l;\n}\nif r < self.size() && self.max_heap[r] > self.max_heap[ma] {\nma = r;\n}\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif ma == i {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nself.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\u987b\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.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\u987b\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
"},{"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":""},{"location":"chapter_heap/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","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
  • Top-K \u662f\u4e00\u4e2a\u7ecf\u5178\u7b97\u6cd5\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u5806\u6570\u636e\u7ed3\u6784\u9ad8\u6548\u89e3\u51b3\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log k)\\) \u3002
"},{"location":"chapter_heap/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

\u6570\u636e\u7ed3\u6784\u7684\u201c\u5806\u201d\u4e0e\u5185\u5b58\u7ba1\u7406\u7684\u201c\u5806\u201d\u662f\u540c\u4e00\u4e2a\u6982\u5ff5\u5417\uff1f

\u4e24\u8005\u4e0d\u662f\u540c\u4e00\u4e2a\u6982\u5ff5\uff0c\u53ea\u662f\u78b0\u5de7\u90fd\u53eb\u5806\u3002\u8ba1\u7b97\u673a\u7cfb\u7edf\u5185\u5b58\u4e2d\u7684\u5806\u662f\u52a8\u6001\u5185\u5b58\u5206\u914d\u7684\u4e00\u90e8\u5206\uff0c\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5b58\u50a8\u6570\u636e\u3002\u7a0b\u5e8f\u53ef\u4ee5\u8bf7\u6c42\u4e00\u5b9a\u91cf\u7684\u5806\u5185\u5b58\uff0c\u7528\u4e8e\u5b58\u50a8\u5982\u5bf9\u8c61\u548c\u6570\u7ec4\u7b49\u590d\u6742\u7ed3\u6784\u3002\u5f53\u8fd9\u4e9b\u6570\u636e\u4e0d\u518d\u9700\u8981\u65f6\uff0c\u7a0b\u5e8f\u9700\u8981\u91ca\u653e\u8fd9\u4e9b\u5185\u5b58\uff0c\u4ee5\u9632\u6b62\u5185\u5b58\u6cc4\u9732\u3002\u76f8\u8f83\u4e8e\u6808\u5185\u5b58\uff0c\u5806\u5185\u5b58\u7684\u7ba1\u7406\u548c\u4f7f\u7528\u9700\u8981\u66f4\u8c28\u614e\uff0c\u4e0d\u6070\u5f53\u7684\u4f7f\u7528\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u9732\u548c\u91ce\u6307\u9488\u7b49\u95ee\u9898\u3002

"},{"location":"chapter_heap/top_k/","title":"8.3 \u00a0 Top-K \u95ee\u9898","text":"

Question

\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u65e0\u5e8f\u6570\u7ec4 nums \uff0c\u8bf7\u8fd4\u56de\u6570\u7ec4\u4e2d\u524d \\(k\\) \u5927\u7684\u5143\u7d20\u3002

\u5bf9\u4e8e\u8be5\u95ee\u9898\uff0c\u6211\u4eec\u5148\u4ecb\u7ecd\u4e24\u79cd\u601d\u8def\u6bd4\u8f83\u76f4\u63a5\u7684\u89e3\u6cd5\uff0c\u518d\u4ecb\u7ecd\u6548\u7387\u66f4\u9ad8\u7684\u5806\u89e3\u6cd5\u3002

"},{"location":"chapter_heap/top_k/#831","title":"8.3.1 \u00a0 \u65b9\u6cd5\u4e00\uff1a\u904d\u5386\u9009\u62e9","text":"

\u6211\u4eec\u53ef\u4ee5\u8fdb\u884c\u56fe 8-6 \u6240\u793a\u7684 \\(k\\) \u8f6e\u904d\u5386\uff0c\u5206\u522b\u5728\u6bcf\u8f6e\u4e2d\u63d0\u53d6\u7b2c \\(1\\)\u3001\\(2\\)\u3001\\(\\dots\\)\u3001\\(k\\) \u5927\u7684\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nk)\\) \u3002

\u6b64\u65b9\u6cd5\u53ea\u9002\u7528\u4e8e \\(k \\ll n\\) \u7684\u60c5\u51b5\uff0c\u56e0\u4e3a\u5f53 \\(k\\) \u4e0e \\(n\\) \u6bd4\u8f83\u63a5\u8fd1\u65f6\uff0c\u5176\u65f6\u95f4\u590d\u6742\u5ea6\u8d8b\u5411\u4e8e \\(O(n^2)\\) \uff0c\u975e\u5e38\u8017\u65f6\u3002

\u56fe 8-6 \u00a0 \u904d\u5386\u5bfb\u627e\u6700\u5927\u7684 k \u4e2a\u5143\u7d20

Tip

\u5f53 \\(k = n\\) \u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u5b8c\u6574\u7684\u6709\u5e8f\u5e8f\u5217\uff0c\u6b64\u65f6\u7b49\u4ef7\u4e8e\u201c\u9009\u62e9\u6392\u5e8f\u201d\u7b97\u6cd5\u3002

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

\u5982\u56fe 8-7 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u5bf9\u6570\u7ec4 nums \u8fdb\u884c\u6392\u5e8f\uff0c\u518d\u8fd4\u56de\u6700\u53f3\u8fb9\u7684 \\(k\\) \u4e2a\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002

\u663e\u7136\uff0c\u8be5\u65b9\u6cd5\u201c\u8d85\u989d\u201d\u5b8c\u6210\u4efb\u52a1\u4e86\uff0c\u56e0\u4e3a\u6211\u4eec\u53ea\u9700\u8981\u627e\u51fa\u6700\u5927\u7684 \\(k\\) \u4e2a\u5143\u7d20\u5373\u53ef\uff0c\u800c\u4e0d\u9700\u8981\u6392\u5e8f\u5176\u4ed6\u5143\u7d20\u3002

\u56fe 8-7 \u00a0 \u6392\u5e8f\u5bfb\u627e\u6700\u5927\u7684 k \u4e2a\u5143\u7d20

"},{"location":"chapter_heap/top_k/#833","title":"8.3.3 \u00a0 \u65b9\u6cd5\u4e09\uff1a\u5806","text":"

\u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e\u5806\u66f4\u52a0\u9ad8\u6548\u5730\u89e3\u51b3 Top-K \u95ee\u9898\uff0c\u6d41\u7a0b\u5982\u56fe 8-8 \u6240\u793a\u3002

  1. \u521d\u59cb\u5316\u4e00\u4e2a\u5c0f\u9876\u5806\uff0c\u5176\u5806\u9876\u5143\u7d20\u6700\u5c0f\u3002
  2. \u5148\u5c06\u6570\u7ec4\u7684\u524d \\(k\\) \u4e2a\u5143\u7d20\u4f9d\u6b21\u5165\u5806\u3002
  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\u3002
  4. \u904d\u5386\u5b8c\u6210\u540e\uff0c\u5806\u4e2d\u4fdd\u5b58\u7684\u5c31\u662f\u6700\u5927\u7684 \\(k\\) \u4e2a\u5143\u7d20\u3002
<1><2><3><4><5><6><7><8><9>

\u56fe 8-8 \u00a0 \u57fa\u4e8e\u5806\u5bfb\u627e\u6700\u5927\u7684 k \u4e2a\u5143\u7d20

\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

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.offer(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.offer(nums[i]);\n}\n}\nreturn heap;\n}\n
top_k.cs
/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nPriorityQueue<int, int> TopKHeap(int[] nums, int k) {\nPriorityQueue<int, int> heap = new();\n// \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nfor (int i = 0; i < k; i++) {\nheap.Enqueue(nums[i], nums[i]);\n}\n// \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\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.Dequeue();\nheap.Enqueue(nums[i], nums[i]);\n}\n}\nreturn heap;\n}\n
top_k.go
/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunc topKHeap(nums []int, k int) *minHeap {\nh := &minHeap{}\nheap.Init(h)\n// \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nfor i := 0; i < k; i++ {\nheap.Push(h, nums[i])\n}\n// \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\nfor i := k; i < len(nums); i++ {\n// \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\nif nums[i] > h.Top().(int) {\nheap.Pop(h)\nheap.Push(h, nums[i])\n}\n}\nreturn h\n}\n
top_k.swift
/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunc topKHeap(nums: [Int], k: Int) -> [Int] {\n// \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nvar heap = Array(nums.prefix(k))\n// \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\nfor i in stride(from: k, to: nums.count, by: 1) {\n// \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\nif nums[i] > heap.first! {\nheap.removeFirst()\nheap.insert(nums[i], at: 0)\n}\n}\nreturn heap\n}\n
top_k.js
/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunction topKHeap(nums, k) {\n// \u4f7f\u7528\u5927\u9876\u5806 MaxHeap \uff0c\u5bf9\u6570\u7ec4 nums \u53d6\u76f8\u53cd\u6570\nconst invertedNums = nums.map((num) => -num);\n// \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nconst heap = new MaxHeap(invertedNums.slice(0, k));\n// \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\nfor (let i = k; i < invertedNums.length; i++) {\n// \u82e5\u5f53\u524d\u5143\u7d20\u5c0f\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\nif (invertedNums[i] < heap.peek()) {\nheap.pop();\nheap.push(invertedNums[i]);\n}\n}\n// \u53d6\u51fa\u5806\u4e2d\u5143\u7d20\nconst maxHeap = heap.getMaxHeap();\n// \u5bf9\u5806\u4e2d\u5143\u7d20\u53d6\u76f8\u53cd\u6570\nconst invertedMaxHeap = maxHeap.map((num) => -num);\nreturn invertedMaxHeap;\n}\n
top_k.ts
/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunction topKHeap(nums: number[], k: number): number[] {\n// \u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u7528\u5927\u9876\u5806\u6765\u6a21\u62df\u5c0f\u9876\u5806\nconst invertedNums = nums.map((num) => -num);\n// \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nconst heap = new MaxHeap(invertedNums.slice(0, k));\n// \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\nfor (let i = k; i < invertedNums.length; i++) {\n// \u82e5\u5f53\u524d\u5143\u7d20\u5c0f\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\nif (invertedNums[i] < heap.peek()) {\nheap.pop();\nheap.push(invertedNums[i]);\n}\n}\n// \u53d6\u51fa\u5806\u4e2d\u5143\u7d20\nconst maxHeap = heap.getMaxHeap();\n// \u5bf9\u5806\u4e2d\u5143\u7d20\u53d6\u76f8\u53cd\u6570\nconst invertedMaxHeap = maxHeap.map((num) => -num);\nreturn invertedMaxHeap;\n}\n
top_k.dart
/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nMinHeap topKHeap(List<int> nums, int k) {\n// \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nMinHeap heap = MinHeap(nums.sublist(0, k));\n// \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\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.pop();\nheap.push(nums[i]);\n}\n}\nreturn heap;\n}\n
top_k.rs
/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfn top_k_heap(nums: Vec<i32>, k: usize) -> BinaryHeap<Reverse<i32>> {\n// Rust \u7684 BinaryHeap \u662f\u5927\u9876\u5806\uff0c\u4f7f\u7528 Reverse \u5c06\u5143\u7d20\u5927\u5c0f\u53cd\u8f6c\nlet mut heap = BinaryHeap::<Reverse<i32>>::new();\n// \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nfor &num in nums.iter().take(k) {\nheap.push(Reverse(num));\n}\n// \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\nfor &num in nums.iter().skip(k) {\n// \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\nif num > heap.peek().unwrap().0 {\nheap.pop();\nheap.push(Reverse(num));\n}\n}\nheap\n}\n
top_k.c
[class]{}-[func]{topKHeap}\n
top_k.zig
[class]{}-[func]{topKHeap}\n
"},{"location":"chapter_introduction/","title":"\u7b2c 1 \u7ae0 \u00a0 \u521d\u8bc6\u7b97\u6cd5","text":"

Abstract

\u4e00\u4f4d\u5c11\u5973\u7fe9\u7fe9\u8d77\u821e\uff0c\u4e0e\u6570\u636e\u4ea4\u7ec7\u5728\u4e00\u8d77\uff0c\u88d9\u6446\u4e0a\u98d8\u626c\u7740\u7b97\u6cd5\u7684\u65cb\u5f8b\u3002

\u5979\u9080\u8bf7\u4f60\u5171\u821e\uff0c\u8bf7\u7d27\u8ddf\u5979\u7684\u6b65\u4f10\uff0c\u8e0f\u5165\u5145\u6ee1\u903b\u8f91\u4e0e\u7f8e\u611f\u7684\u7b97\u6cd5\u4e16\u754c\u3002

"},{"location":"chapter_introduction/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 1.1 \u00a0 \u7b97\u6cd5\u65e0\u5904\u4e0d\u5728
  • 1.2 \u00a0 \u7b97\u6cd5\u662f\u4ec0\u4e48
  • 1.3 \u00a0 \u5c0f\u7ed3
"},{"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\u4f60\u5df2\u7ecf\u5728\u4e0d\u77e5\u4e0d\u89c9\u4e2d\u5b66\u4f1a\u4e86\u8bb8\u591a\u7b97\u6cd5\uff0c\u5e76\u4e60\u60ef\u5c06\u5b83\u4eec\u5e94\u7528\u5230\u65e5\u5e38\u751f\u6d3b\u4e2d\u4e86\u3002\u4e0b\u9762\uff0c\u6211\u5c06\u4e3e\u51e0\u4e2a\u5177\u4f53\u4f8b\u5b50\u6765\u8bc1\u5b9e\u8fd9\u4e00\u70b9\u3002

\u4f8b\u4e00\uff1a\u67e5\u9605\u5b57\u5178\u3002\u5728\u5b57\u5178\u91cc\uff0c\u6bcf\u4e2a\u6c49\u5b57\u90fd\u5bf9\u5e94\u4e00\u4e2a\u62fc\u97f3\uff0c\u800c\u5b57\u5178\u662f\u6309\u7167\u62fc\u97f3\u5b57\u6bcd\u987a\u5e8f\u6392\u5217\u7684\u3002\u5047\u8bbe\u6211\u4eec\u9700\u8981\u67e5\u627e\u4e00\u4e2a\u62fc\u97f3\u9996\u5b57\u6bcd\u4e3a \\(r\\) \u7684\u5b57\uff0c\u901a\u5e38\u4f1a\u6309\u7167\u56fe 1-1 \u6240\u793a\u7684\u65b9\u5f0f\u5b9e\u73b0\u3002

  1. \u7ffb\u5f00\u5b57\u5178\u7ea6\u4e00\u534a\u7684\u9875\u6570\uff0c\u67e5\u770b\u8be5\u9875\u7684\u9996\u5b57\u6bcd\u662f\u4ec0\u4e48\uff0c\u5047\u8bbe\u9996\u5b57\u6bcd\u4e3a \\(m\\) \u3002
  2. \u7531\u4e8e\u5728\u62fc\u97f3\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\u3002
  3. \u4e0d\u65ad\u91cd\u590d\u6b65\u9aa4 1. \u548c \u6b65\u9aa4 2. \uff0c\u76f4\u81f3\u627e\u5230\u62fc\u97f3\u9996\u5b57\u6bcd\u4e3a \\(r\\) \u7684\u9875\u7801\u4e3a\u6b62\u3002
<1><2><3><4><5>

\u56fe 1-1 \u00a0 \u67e5\u5b57\u5178\u6b65\u9aa4

\u67e5\u9605\u5b57\u5178\u8fd9\u4e2a\u5c0f\u5b66\u751f\u5fc5\u5907\u6280\u80fd\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u8457\u540d\u7684\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u3002\u4ece\u6570\u636e\u7ed3\u6784\u7684\u89d2\u5ea6\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u5b57\u5178\u89c6\u4e3a\u4e00\u4e2a\u5df2\u6392\u5e8f\u7684\u201c\u6570\u7ec4\u201d\uff1b\u4ece\u7b97\u6cd5\u7684\u89d2\u5ea6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e0a\u8ff0\u67e5\u5b57\u5178\u7684\u4e00\u7cfb\u5217\u64cd\u4f5c\u770b\u4f5c\u662f\u201c\u4e8c\u5206\u67e5\u627e\u201d\u3002

\u4f8b\u4e8c\uff1a\u6574\u7406\u6251\u514b\u3002\u6211\u4eec\u5728\u6253\u724c\u65f6\uff0c\u6bcf\u5c40\u90fd\u9700\u8981\u6574\u7406\u6251\u514b\u724c\uff0c\u4f7f\u5176\u4ece\u5c0f\u5230\u5927\u6392\u5217\uff0c\u5b9e\u73b0\u6d41\u7a0b\u5982\u56fe 1-2 \u6240\u793a\u3002

  1. \u5c06\u6251\u514b\u724c\u5212\u5206\u4e3a\u201c\u6709\u5e8f\u201d\u548c\u201c\u65e0\u5e8f\u201d\u4e24\u90e8\u5206\uff0c\u5e76\u5047\u8bbe\u521d\u59cb\u72b6\u6001\u4e0b\u6700\u5de6 1 \u5f20\u6251\u514b\u724c\u5df2\u7ecf\u6709\u5e8f\u3002
  2. \u5728\u65e0\u5e8f\u90e8\u5206\u62bd\u51fa\u4e00\u5f20\u6251\u514b\u724c\uff0c\u63d2\u5165\u81f3\u6709\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\uff1b\u5b8c\u6210\u540e\u6700\u5de6 2 \u5f20\u6251\u514b\u5df2\u7ecf\u6709\u5e8f\u3002
  3. \u4e0d\u65ad\u5faa\u73af\u6b65\u9aa4 2. \uff0c\u6bcf\u4e00\u8f6e\u5c06\u4e00\u5f20\u6251\u514b\u724c\u4ece\u65e0\u5e8f\u90e8\u5206\u63d2\u5165\u81f3\u6709\u5e8f\u90e8\u5206\uff0c\u76f4\u81f3\u6240\u6709\u6251\u514b\u724c\u90fd\u6709\u5e8f\u3002

\u56fe 1-2 \u00a0 \u6251\u514b\u6392\u5e8f\u6b65\u9aa4

\u4e0a\u8ff0\u6574\u7406\u6251\u514b\u724c\u7684\u65b9\u6cd5\u672c\u8d28\u4e0a\u662f\u201c\u63d2\u5165\u6392\u5e8f\u201d\u7b97\u6cd5\uff0c\u5b83\u5728\u5904\u7406\u5c0f\u578b\u6570\u636e\u96c6\u65f6\u975e\u5e38\u9ad8\u6548\u3002\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u7684\u6392\u5e8f\u5e93\u51fd\u6570\u4e2d\u90fd\u5b58\u5728\u63d2\u5165\u6392\u5e8f\u7684\u8eab\u5f71\u3002

\u4f8b\u4e09\uff1a\u8d27\u5e01\u627e\u96f6\u3002\u5047\u8bbe\u6211\u4eec\u5728\u8d85\u5e02\u8d2d\u4e70\u4e86 \\(69\\) \u5143\u7684\u5546\u54c1\uff0c\u7ed9\u4e86\u6536\u94f6\u5458 \\(100\\) \u5143\uff0c\u5219\u6536\u94f6\u5458\u9700\u8981\u627e\u6211\u4eec \\(31\\) \u5143\u3002\u4ed6\u4f1a\u5f88\u81ea\u7136\u5730\u5b8c\u6210\u5982\u56fe 1-3 \u6240\u793a\u7684\u601d\u8003\u3002

  1. \u53ef\u9009\u9879\u662f\u6bd4 \\(31\\) \u5143\u9762\u503c\u66f4\u5c0f\u7684\u8d27\u5e01\uff0c\u5305\u62ec \\(1\\) \u5143\u3001\\(5\\) \u5143\u3001\\(10\\) \u5143\u3001\\(20\\) \u5143\u3002
  2. \u4ece\u53ef\u9009\u9879\u4e2d\u62ff\u51fa\u6700\u5927\u7684 \\(20\\) \u5143\uff0c\u5269\u4f59 \\(31 - 20 = 11\\) \u5143\u3002
  3. \u4ece\u5269\u4f59\u53ef\u9009\u9879\u4e2d\u62ff\u51fa\u6700\u5927\u7684 \\(10\\) \u5143\uff0c\u5269\u4f59 \\(11 - 10 = 1\\) \u5143\u3002
  4. \u4ece\u5269\u4f59\u53ef\u9009\u9879\u4e2d\u62ff\u51fa\u6700\u5927\u7684 \\(1\\) \u5143\uff0c\u5269\u4f59 \\(1 - 1 = 0\\) \u5143\u3002
  5. \u5b8c\u6210\u627e\u96f6\uff0c\u65b9\u6848\u4e3a \\(20 + 10 + 1 = 31\\) \u5143\u3002

\u56fe 1-3 \u00a0 \u8d27\u5e01\u627e\u96f6\u8fc7\u7a0b

\u5728\u4ee5\u4e0a\u6b65\u9aa4\u4e2d\uff0c\u6211\u4eec\u6bcf\u4e00\u6b65\u90fd\u91c7\u53d6\u5f53\u524d\u770b\u6765\u6700\u597d\u7684\u9009\u62e9\uff08\u5c3d\u53ef\u80fd\u7528\u5927\u9762\u989d\u7684\u8d27\u5e01\uff09\uff0c\u6700\u7ec8\u5f97\u5230\u4e86\u53ef\u884c\u7684\u627e\u96f6\u65b9\u6848\u3002\u4ece\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u89d2\u5ea6\u770b\uff0c\u8fd9\u79cd\u65b9\u6cd5\u672c\u8d28\u4e0a\u662f\u201c\u8d2a\u5fc3\u201d\u7b97\u6cd5\u3002

\u5c0f\u5230\u70f9\u996a\u4e00\u9053\u83dc\uff0c\u5927\u5230\u661f\u9645\u822a\u884c\uff0c\u51e0\u4e4e\u6240\u6709\u95ee\u9898\u7684\u89e3\u51b3\u90fd\u79bb\u4e0d\u5f00\u7b97\u6cd5\u3002\u8ba1\u7b97\u673a\u7684\u51fa\u73b0\u4f7f\u6211\u4eec\u80fd\u591f\u901a\u8fc7\u7f16\u7a0b\u5c06\u6570\u636e\u7ed3\u6784\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\uff0c\u540c\u65f6\u7f16\u5199\u4ee3\u7801\u8c03\u7528 CPU \u548c GPU \u6267\u884c\u7b97\u6cd5\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u80fd\u628a\u751f\u6d3b\u4e2d\u7684\u95ee\u9898\u8f6c\u79fb\u5230\u8ba1\u7b97\u673a\u4e0a\uff0c\u4ee5\u66f4\u9ad8\u6548\u7684\u65b9\u5f0f\u89e3\u51b3\u5404\u79cd\u590d\u6742\u95ee\u9898\u3002

Tip

\u5982\u679c\u4f60\u5bf9\u6570\u636e\u7ed3\u6784\u3001\u7b97\u6cd5\u3001\u6570\u7ec4\u548c\u4e8c\u5206\u67e5\u627e\u7b49\u6982\u5ff5\u4ecd\u611f\u5230\u4e00\u77e5\u534a\u89e3\uff0c\u8bf7\u7ee7\u7eed\u5f80\u4e0b\u9605\u8bfb\uff0c\u8fd9\u672c\u4e66\u5c06\u5f15\u5bfc\u4f60\u8fc8\u5165\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u77e5\u8bc6\u6bbf\u5802\u3002

"},{"location":"chapter_introduction/summary/","title":"1.3 \u00a0 \u5c0f\u7ed3","text":"
  • \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\u4f1a\u4e86\u8bb8\u591a\u7b97\u6cd5\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\u7b97\u6cd5\u4f53\u73b0\u4e86\u5206\u800c\u6cbb\u4e4b\u7684\u91cd\u8981\u7b97\u6cd5\u601d\u60f3\u3002
  • \u6574\u7406\u6251\u514b\u7684\u8fc7\u7a0b\u4e0e\u63d2\u5165\u6392\u5e8f\u7b97\u6cd5\u975e\u5e38\u7c7b\u4f3c\u3002\u63d2\u5165\u6392\u5e8f\u7b97\u6cd5\u9002\u5408\u6392\u5e8f\u5c0f\u578b\u6570\u636e\u96c6\u3002
  • \u8d27\u5e01\u627e\u96f6\u7684\u6b65\u9aa4\u672c\u8d28\u4e0a\u662f\u8d2a\u5fc3\u7b97\u6cd5\uff0c\u6bcf\u4e00\u6b65\u90fd\u91c7\u53d6\u5f53\u524d\u770b\u6765\u7684\u6700\u597d\u7684\u9009\u62e9\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
  • \u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7c7b\u6bd4\u4e3a\u62fc\u88c5\u79ef\u6728\uff0c\u79ef\u6728\u4ee3\u8868\u6570\u636e\uff0c\u79ef\u6728\u7684\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\uff0c\u5b83\u5177\u6709\u4ee5\u4e0b\u7279\u6027\u3002

  • \u95ee\u9898\u662f\u660e\u786e\u7684\uff0c\u5305\u542b\u6e05\u6670\u7684\u8f93\u5165\u548c\u8f93\u51fa\u5b9a\u4e49\u3002
  • \u5177\u6709\u53ef\u884c\u6027\uff0c\u80fd\u591f\u5728\u6709\u9650\u6b65\u9aa4\u3001\u65f6\u95f4\u548c\u5185\u5b58\u7a7a\u95f4\u4e0b\u5b8c\u6210\u3002
  • \u5404\u6b65\u9aa4\u90fd\u6709\u786e\u5b9a\u7684\u542b\u4e49\uff0c\u76f8\u540c\u7684\u8f93\u5165\u548c\u8fd0\u884c\u6761\u4ef6\u4e0b\uff0c\u8f93\u51fa\u59cb\u7ec8\u76f8\u540c\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\uff0c\u5177\u6709\u4ee5\u4e0b\u8bbe\u8ba1\u76ee\u6807\u3002

  • \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\u4fbf\u4f7f\u5f97\u7b97\u6cd5\u9ad8\u6548\u8fd0\u884c\u3002

\u6570\u636e\u7ed3\u6784\u8bbe\u8ba1\u662f\u4e00\u4e2a\u5145\u6ee1\u6743\u8861\u7684\u8fc7\u7a0b\u3002\u5982\u679c\u60f3\u8981\u5728\u67d0\u65b9\u9762\u53d6\u5f97\u63d0\u5347\uff0c\u5f80\u5f80\u9700\u8981\u5728\u53e6\u4e00\u65b9\u9762\u4f5c\u51fa\u59a5\u534f\u3002\u4e0b\u9762\u4e3e\u4e24\u4e2a\u4f8b\u5b50\u3002

  • \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\u3002
  • \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":"

\u5982\u56fe 1-4 \u6240\u793a\uff0c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u9ad8\u5ea6\u76f8\u5173\u3001\u7d27\u5bc6\u7ed3\u5408\uff0c\u5177\u4f53\u8868\u73b0\u4ee5\u4e0b\u4e09\u4e2a\u65b9\u9762\u3002

  • \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\u4f5c\u7528\u7684\u821e\u53f0\u3002\u6570\u636e\u7ed3\u6784\u672c\u8eab\u4ec5\u5b58\u50a8\u6570\u636e\u4fe1\u606f\uff0c\u7ed3\u5408\u7b97\u6cd5\u624d\u80fd\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\u3002
  • \u7b97\u6cd5\u901a\u5e38\u53ef\u4ee5\u57fa\u4e8e\u4e0d\u540c\u7684\u6570\u636e\u7ed3\u6784\u8fdb\u884c\u5b9e\u73b0\uff0c\u4f46\u6267\u884c\u6548\u7387\u53ef\u80fd\u76f8\u5dee\u5f88\u5927\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u662f\u5173\u952e\u3002

\u56fe 1-4 \u00a0 \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u5173\u7cfb

\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u72b9\u5982\u56fe 1-5 \u6240\u793a\u7684\u62fc\u88c5\u79ef\u6728\u3002\u4e00\u5957\u79ef\u6728\uff0c\u9664\u4e86\u5305\u542b\u8bb8\u591a\u96f6\u4ef6\u4e4b\u5916\uff0c\u8fd8\u9644\u6709\u8be6\u7ec6\u7684\u7ec4\u88c5\u8bf4\u660e\u4e66\u3002\u6211\u4eec\u6309\u7167\u8bf4\u660e\u4e66\u4e00\u6b65\u6b65\u64cd\u4f5c\uff0c\u5c31\u80fd\u7ec4\u88c5\u51fa\u7cbe\u7f8e\u7684\u79ef\u6728\u6a21\u578b\u3002

\u56fe 1-5 \u00a0 \u62fc\u88c5\u79ef\u6728

\u4e24\u8005\u7684\u8be6\u7ec6\u5bf9\u5e94\u5173\u7cfb\u5982\u8868 1-1 \u6240\u793a\u3002

\u8868 1-1 \u00a0 \u5c06\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7c7b\u6bd4\u4e3a\u79ef\u6728

\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5 \u62fc\u88c5\u79ef\u6728 \u8f93\u5165\u6570\u636e \u672a\u62fc\u88c5\u7684\u79ef\u6728 \u6570\u636e\u7ed3\u6784 \u79ef\u6728\u7ec4\u7ec7\u5f62\u5f0f\uff0c\u5305\u62ec\u5f62\u72b6\u3001\u5927\u5c0f\u3001\u8fde\u63a5\u65b9\u5f0f\u7b49 \u7b97\u6cd5 \u628a\u79ef\u6728\u62fc\u6210\u76ee\u6807\u5f62\u6001\u7684\u4e00\u7cfb\u5217\u64cd\u4f5c\u6b65\u9aa4 \u8f93\u51fa\u6570\u636e \u79ef\u6728\u6a21\u578b

\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u662f\u72ec\u7acb\u4e8e\u7f16\u7a0b\u8bed\u8a00\u7684\u3002\u6b63\u56e0\u5982\u6b64\uff0c\u672c\u4e66\u5f97\u4ee5\u63d0\u4f9b\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\u7684\u5b9e\u73b0\u3002

\u7ea6\u5b9a\u4fd7\u6210\u7684\u7b80\u79f0

\u5728\u5b9e\u9645\u8ba8\u8bba\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u201c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u201d\u7b80\u79f0\u4e3a\u201c\u7b97\u6cd5\u201d\u3002\u6bd4\u5982\u4f17\u6240\u5468\u77e5\u7684 LeetCode \u7b97\u6cd5\u9898\u76ee\uff0c\u5b9e\u9645\u4e0a\u540c\u65f6\u8003\u5bdf\u4e86\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u4e24\u65b9\u9762\u7684\u77e5\u8bc6\u3002

"},{"location":"chapter_preface/","title":"\u7b2c 0 \u7ae0 \u00a0 \u524d\u8a00","text":"

Abstract

\u7b97\u6cd5\u72b9\u5982\u7f8e\u5999\u7684\u4ea4\u54cd\u4e50\uff0c\u6bcf\u4e00\u884c\u4ee3\u7801\u90fd\u50cf\u97f5\u5f8b\u822c\u6d41\u6dcc\u3002

\u613f\u8fd9\u672c\u4e66\u5728\u4f60\u7684\u8111\u6d77\u4e2d\u8f7b\u8f7b\u54cd\u8d77\uff0c\u7559\u4e0b\u72ec\u7279\u800c\u6df1\u523b\u7684\u65cb\u5f8b\u3002

"},{"location":"chapter_preface/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 0.1 \u00a0 \u5173\u4e8e\u672c\u4e66
  • 0.2 \u00a0 \u5982\u4f55\u4f7f\u7528\u672c\u4e66
  • 0.3 \u00a0 \u5c0f\u7ed3
"},{"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\u3002
  • \u7b97\u6cd5\u6e90\u4ee3\u7801\u7686\u53ef\u4e00\u952e\u8fd0\u884c\uff0c\u652f\u6301 Java\u3001C++\u3001Python\u3001Go\u3001JS\u3001TS\u3001C#\u3001Swift\u3001Rust\u3001Dart\u3001Zig \u7b49\u8bed\u8a00\u3002
  • \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\u3002
"},{"location":"chapter_preface/about_the_book/#011","title":"0.1.1 \u00a0 \u8bfb\u8005\u5bf9\u8c61","text":"

\u82e5\u60a8\u662f\u7b97\u6cd5\u521d\u5b66\u8005\uff0c\u4ece\u672a\u63a5\u89e6\u8fc7\u7b97\u6cd5\uff0c\u6216\u8005\u5df2\u7ecf\u6709\u4e00\u4e9b\u5237\u9898\u7ecf\u9a8c\uff0c\u5bf9\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u6709\u6a21\u7cca\u7684\u8ba4\u8bc6\uff0c\u5728\u4f1a\u4e0e\u4e0d\u4f1a\u4e4b\u95f4\u53cd\u590d\u6a2a\u8df3\uff0c\u90a3\u4e48\u8fd9\u672c\u4e66\u6b63\u662f\u4e3a\u60a8\u91cf\u8eab\u5b9a\u5236\uff01

\u5982\u679c\u60a8\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\u7b97\u6cd5\u5927\u795e\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\u5982\u56fe 0-1 \u6240\u793a\u3002

  • \u590d\u6742\u5ea6\u5206\u6790\uff1a\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u7684\u8bc4\u4ef7\u7ef4\u5ea6\u4e0e\u65b9\u6cd5\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u3001\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u63a8\u7b97\u65b9\u6cd5\u3001\u5e38\u89c1\u7c7b\u578b\u3001\u793a\u4f8b\u7b49\u3002
  • \u6570\u636e\u7ed3\u6784\uff1a\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u6570\u636e\u7ed3\u6784\u7684\u5206\u7c7b\u65b9\u6cd5\u3002\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\u7b49\u6570\u636e\u7ed3\u6784\u7684\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\u641c\u7d22\u3001\u6392\u5e8f\u3001\u5206\u6cbb\u3001\u56de\u6eaf\u3001\u52a8\u6001\u89c4\u5212\u3001\u8d2a\u5fc3\u7b49\u7b97\u6cd5\u7684\u5b9a\u4e49\u3001\u4f18\u7f3a\u70b9\u3001\u6548\u7387\u3001\u5e94\u7528\u573a\u666f\u3001\u89e3\u9898\u6b65\u9aa4\u3001\u793a\u4f8b\u9898\u76ee\u7b49\u3002

\u56fe 0-1 \u00a0 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\u4e00\u6b21\u7545\u8c08\u4e2d\u60a8\u9f13\u52b1\u6211\u201c\u5feb\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\u4ed6\u5f00\u53d1\u7684\u5f00\u6e90\u6587\u6863\u4e3b\u9898 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\u4ef6\u5bcc\u6709\u8da3\u5473\u7684\u4e8b\u3002

"},{"location":"chapter_preface/suggestions/","title":"0.2 \u00a0 \u5982\u4f55\u4f7f\u7528\u672c\u4e66","text":"

Tip

\u4e3a\u4e86\u83b7\u5f97\u6700\u4f73\u7684\u9605\u8bfb\u4f53\u9a8c\uff0c\u5efa\u8bae\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
  • \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
  • \u91cd\u8981\u4e13\u6709\u540d\u8bcd\u53ca\u5176\u82f1\u6587\u7ffb\u8bd1\u4f1a\u7528 \u300c \u300d \u62ec\u53f7\u6807\u6ce8\uff0c\u4f8b\u5982 \u300c\u6570\u7ec4 array\u300d \u3002\u5efa\u8bae\u8bb0\u4f4f\u5b83\u4eec\uff0c\u4ee5\u4fbf\u9605\u8bfb\u6587\u732e\u3002
  • \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
  • \u5f53\u6d89\u53ca\u5230\u7f16\u7a0b\u8bed\u8a00\u4e4b\u95f4\u4e0d\u4e00\u81f4\u7684\u540d\u8bcd\u65f6\uff0c\u672c\u4e66\u5747\u4ee5 Python \u4e3a\u51c6\uff0c\u4f8b\u5982\u4f7f\u7528 \\(\\text{None}\\) \u6765\u8868\u793a\u201c\u7a7a\u201d\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
PythonC++JavaC#GoSwiftJSTSDartRustCZig
\"\"\"\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/**\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
"},{"location":"chapter_preface/suggestions/#022","title":"0.2.2 \u00a0 \u5728\u52a8\u753b\u56fe\u89e3\u4e2d\u9ad8\u6548\u5b66\u4e60","text":"

\u76f8\u8f83\u4e8e\u6587\u5b57\uff0c\u89c6\u9891\u548c\u56fe\u7247\u5177\u6709\u66f4\u9ad8\u7684\u4fe1\u606f\u5bc6\u5ea6\u548c\u7ed3\u6784\u5316\u7a0b\u5ea6\uff0c\u66f4\u6613\u4e8e\u7406\u89e3\u3002\u5728\u672c\u4e66\u4e2d\uff0c\u91cd\u70b9\u548c\u96be\u70b9\u77e5\u8bc6\u5c06\u4e3b\u8981\u901a\u8fc7\u52a8\u753b\u548c\u56fe\u89e3\u5f62\u5f0f\u5c55\u793a\uff0c\u800c\u6587\u5b57\u5219\u4f5c\u4e3a\u52a8\u753b\u548c\u56fe\u7247\u7684\u89e3\u91ca\u4e0e\u8865\u5145\u3002

\u5982\u679c\u4f60\u5728\u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u53d1\u73b0\u67d0\u6bb5\u5185\u5bb9\u63d0\u4f9b\u4e86\u56fe 0-2 \u6240\u793a\u7684\u52a8\u753b\u6216\u56fe\u89e3\uff0c\u8bf7\u4ee5\u56fe\u4e3a\u4e3b\u3001\u4ee5\u6587\u5b57\u4e3a\u8f85\uff0c\u7efc\u5408\u4e24\u8005\u6765\u7406\u89e3\u5185\u5bb9\u3002

\u56fe 0-2 \u00a0 \u52a8\u753b\u56fe\u89e3\u793a\u4f8b

"},{"location":"chapter_preface/suggestions/#023","title":"0.2.3 \u00a0 \u5728\u4ee3\u7801\u5b9e\u8df5\u4e2d\u52a0\u6df1\u7406\u89e3","text":"

\u672c\u4e66\u7684\u914d\u5957\u4ee3\u7801\u88ab\u6258\u7ba1\u5728 GitHub \u4ed3\u5e93\u3002\u5982\u56fe 0-3 \u6240\u793a\uff0c\u6e90\u4ee3\u7801\u9644\u6709\u6d4b\u8bd5\u6837\u4f8b\uff0c\u53ef\u4e00\u952e\u8fd0\u884c\u3002

\u5982\u679c\u65f6\u95f4\u5141\u8bb8\uff0c\u5efa\u8bae\u4f60\u53c2\u7167\u4ee3\u7801\u81ea\u884c\u6572\u4e00\u904d\u3002\u5982\u679c\u5b66\u4e60\u65f6\u95f4\u6709\u9650\uff0c\u8bf7\u81f3\u5c11\u901a\u8bfb\u5e76\u8fd0\u884c\u6240\u6709\u4ee3\u7801\u3002

\u4e0e\u9605\u8bfb\u4ee3\u7801\u76f8\u6bd4\uff0c\u7f16\u5199\u4ee3\u7801\u7684\u8fc7\u7a0b\u5f80\u5f80\u80fd\u5e26\u6765\u66f4\u591a\u6536\u83b7\u3002\u52a8\u624b\u5b66\uff0c\u624d\u662f\u771f\u7684\u5b66\u3002

\u56fe 0-3 \u00a0 \u8fd0\u884c\u4ee3\u7801\u793a\u4f8b

\u8fd0\u884c\u4ee3\u7801\u7684\u524d\u7f6e\u5de5\u4f5c\u4e3b\u8981\u5206\u4e3a\u4e09\u6b65\u3002

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

\u7b2c\u4e8c\u6b65\uff1a\u514b\u9686\u6216\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\u5728\u56fe 0-4 \u6240\u793a\u7684\u4f4d\u7f6e\uff0c\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

\u56fe 0-4 \u00a0 \u514b\u9686\u4ed3\u5e93\u4e0e\u4e0b\u8f7d\u4ee3\u7801

\u7b2c\u4e09\u6b65\uff1a\u8fd0\u884c\u6e90\u4ee3\u7801\u3002\u5982\u56fe 0-5 \u6240\u793a\uff0c\u5bf9\u4e8e\u9876\u90e8\u6807\u6709\u6587\u4ef6\u540d\u79f0\u7684\u4ee3\u7801\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4ed3\u5e93\u7684 codes \u6587\u4ef6\u5939\u5185\u627e\u5230\u5bf9\u5e94\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u3002\u6e90\u4ee3\u7801\u6587\u4ef6\u53ef\u4e00\u952e\u8fd0\u884c\uff0c\u5c06\u5e2e\u52a9\u4f60\u8282\u7701\u4e0d\u5fc5\u8981\u7684\u8c03\u8bd5\u65f6\u95f4\uff0c\u8ba9\u4f60\u80fd\u591f\u4e13\u6ce8\u4e8e\u5b66\u4e60\u5185\u5bb9\u3002

\u56fe 0-5 \u00a0 \u4ee3\u7801\u5757\u4e0e\u5bf9\u5e94\u7684\u6e90\u4ee3\u7801\u6587\u4ef6

"},{"location":"chapter_preface/suggestions/#024","title":"0.2.4 \u00a0 \u5728\u63d0\u95ee\u8ba8\u8bba\u4e2d\u5171\u540c\u6210\u957f","text":"

\u5728\u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u8bf7\u4e0d\u8981\u8f7b\u6613\u8df3\u8fc7\u90a3\u4e9b\u6ca1\u5b66\u660e\u767d\u7684\u77e5\u8bc6\u70b9\u3002\u6b22\u8fce\u5728\u8bc4\u8bba\u533a\u63d0\u51fa\u4f60\u7684\u95ee\u9898\uff0c\u6211\u548c\u5c0f\u4f19\u4f34\u4eec\u5c06\u7aed\u8bda\u4e3a\u4f60\u89e3\u7b54\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u53ef\u5728\u4e24\u5929\u5185\u56de\u590d\u3002

\u5982\u56fe 0-6 \u6240\u793a\uff0c\u6bcf\u7bc7\u6587\u7ae0\u7684\u5e95\u90e8\u90fd\u914d\u6709\u8bc4\u8bba\u533a\u3002\u5e0c\u671b\u4f60\u80fd\u591a\u5173\u6ce8\u8bc4\u8bba\u533a\u7684\u5185\u5bb9\u3002\u4e00\u65b9\u9762\uff0c\u4f60\u53ef\u4ee5\u4e86\u89e3\u5927\u5bb6\u9047\u5230\u7684\u95ee\u9898\uff0c\u4ece\u800c\u67e5\u6f0f\u8865\u7f3a\uff0c\u6fc0\u53d1\u66f4\u6df1\u5165\u7684\u601d\u8003\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u671f\u5f85\u4f60\u80fd\u6177\u6168\u5730\u56de\u7b54\u5176\u4ed6\u5c0f\u4f19\u4f34\u7684\u95ee\u9898\uff0c\u5206\u4eab\u60a8\u7684\u89c1\u89e3\uff0c\u5e2e\u52a9\u4ed6\u4eba\u8fdb\u6b65\u3002

\u56fe 0-6 \u00a0 \u8bc4\u8bba\u533a\u793a\u4f8b

"},{"location":"chapter_preface/suggestions/#025","title":"0.2.5 \u00a0 \u7b97\u6cd5\u5b66\u4e60\u8def\u7ebf","text":"

\u4ece\u603b\u4f53\u4e0a\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b66\u4e60\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u8fc7\u7a0b\u5212\u5206\u4e3a\u4e09\u4e2a\u9636\u6bb5\u3002

  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

\u5982\u56fe 0-7 \u6240\u793a\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

\u56fe 0-7 \u00a0 \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":"\u7b2c 10 \u7ae0 \u00a0 \u641c\u7d22","text":"

Abstract

\u641c\u7d22\u662f\u4e00\u573a\u672a\u77e5\u7684\u5192\u9669\uff0c\u6211\u4eec\u6216\u8bb8\u9700\u8981\u8d70\u904d\u795e\u79d8\u7a7a\u95f4\u7684\u6bcf\u4e2a\u89d2\u843d\uff0c\u53c8\u6216\u8bb8\u53ef\u4ee5\u5feb\u901f\u9501\u5b9a\u76ee\u6807\u3002

\u5728\u8fd9\u573a\u5bfb\u89c5\u4e4b\u65c5\u4e2d\uff0c\u6bcf\u4e00\u6b21\u63a2\u7d22\u90fd\u53ef\u80fd\u5f97\u5230\u4e00\u4e2a\u672a\u66fe\u6599\u60f3\u7684\u7b54\u6848\u3002

"},{"location":"chapter_searching/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 10.1 \u00a0 \u4e8c\u5206\u67e5\u627e
  • 10.2 \u00a0 \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9
  • 10.3 \u00a0 \u4e8c\u5206\u67e5\u627e\u8fb9\u754c
  • 10.4 \u00a0 \u54c8\u5e0c\u4f18\u5316\u7b56\u7565
  • 10.5 \u00a0 \u91cd\u8bc6\u641c\u7d22\u7b97\u6cd5
  • 10.6 \u00a0 \u5c0f\u7ed3
"},{"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\u7b56\u7565\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

\u56fe 10-1 \u00a0 \u4e8c\u5206\u67e5\u627e\u793a\u4f8b\u6570\u636e

\u5982\u56fe 10-2 \u6240\u793a\uff0c\u6211\u4eec\u5148\u521d\u59cb\u5316\u6307\u9488 \\(i = 0\\) \u548c \\(j = n - 1\\) \uff0c\u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u548c\u5c3e\u5143\u7d20\uff0c\u4ee3\u8868\u641c\u7d22\u533a\u95f4 \\([0, n - 1]\\) \u3002\u8bf7\u6ce8\u610f\uff0c\u4e2d\u62ec\u53f7\u8868\u793a\u95ed\u533a\u95f4\uff0c\u5176\u5305\u542b\u8fb9\u754c\u503c\u672c\u8eab\u3002

\u63a5\u4e0b\u6765\uff0c\u5faa\u73af\u6267\u884c\u4ee5\u4e0b\u4e24\u6b65\u3002

  1. \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 \\(m = \\lfloor {(i + j) / 2} \\rfloor\\) \uff0c\u5176\u4e2d \\(\\lfloor \\: \\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\u4ee5\u4e0b\u4e09\u79cd\u60c5\u51b5\u3002
    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\\) \u3002
    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\\) \u3002
    3. \u5f53 nums[m] = target \u65f6\uff0c\u8bf4\u660e\u627e\u5230 target \uff0c\u56e0\u6b64\u8fd4\u56de\u7d22\u5f15 \\(m\\) \u3002

\u82e5\u6570\u7ec4\u4e0d\u5305\u542b\u76ee\u6807\u5143\u7d20\uff0c\u641c\u7d22\u533a\u95f4\u6700\u7ec8\u4f1a\u7f29\u5c0f\u4e3a\u7a7a\u3002\u6b64\u65f6\u8fd4\u56de \\(-1\\) \u3002

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

\u56fe 10-2 \u00a0 \u4e8c\u5206\u67e5\u627e\u6d41\u7a0b

\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e \\(i\\) \u548c \\(j\\) \u90fd\u662f int \u7c7b\u578b\uff0c\u56e0\u6b64 \\(i + j\\) \u53ef\u80fd\u4f1a\u8d85\u51fa int \u7c7b\u578b\u7684\u53d6\u503c\u8303\u56f4\u3002\u4e3a\u4e86\u907f\u514d\u5927\u6570\u8d8a\u754c\uff0c\u6211\u4eec\u901a\u5e38\u91c7\u7528\u516c\u5f0f \\(m = \\lfloor {i + (j - i) / 2} \\rfloor\\) \u6765\u8ba1\u7b97\u4e2d\u70b9\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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\u987b\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.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.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.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.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.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.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.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
binary_search.rs
/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfn binary_search(nums: &[i32], target: i32) -> i32 {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\nlet mut i = 0;\nlet mut j = nums.len() as i32 - 1;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\nwhile i <= j {\nlet m = i + (j - i) / 2;      // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m as usize] < target {         // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\n} else if nums[m as usize] > 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// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -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.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(m);\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n

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

\u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(1)\\) \uff1a\u6307\u9488 \\(i\\) \u548c \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7a7a\u95f4\u3002

"},{"location":"chapter_searching/binary_search/#1011","title":"10.1.1 \u00a0 \u533a\u95f4\u8868\u793a\u65b9\u6cd5","text":"

\u9664\u4e86\u4e0a\u8ff0\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

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
binary_search.rs
/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nfn binary_search_lcro(nums: &[i32], target: i32) -> i32 {\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 mut i = 0;\nlet mut j = nums.len() as i32;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nwhile i < j {\nlet m = i + (j - i) / 2;      // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m as usize] < target {         // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\n} else if nums[m as usize] > target {  // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m - 1;\n} else {                      // \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.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.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(m);\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n

\u5982\u56fe 10-3 \u6240\u793a\uff0c\u5728\u4e24\u79cd\u533a\u95f4\u8868\u793a\u4e0b\uff0c\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u7684\u521d\u59cb\u5316\u3001\u5faa\u73af\u6761\u4ef6\u548c\u7f29\u5c0f\u533a\u95f4\u64cd\u4f5c\u7686\u6709\u6240\u4e0d\u540c\u3002

\u7531\u4e8e\u201c\u53cc\u95ed\u533a\u95f4\u201d\u8868\u793a\u4e2d\u7684\u5de6\u53f3\u8fb9\u754c\u90fd\u88ab\u5b9a\u4e49\u4e3a\u95ed\u533a\u95f4\uff0c\u56e0\u6b64\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\uff0c\u56e0\u6b64\u4e00\u822c\u5efa\u8bae\u91c7\u7528\u201c\u53cc\u95ed\u533a\u95f4\u201d\u7684\u5199\u6cd5\u3002

\u56fe 10-3 \u00a0 \u4e24\u79cd\u533a\u95f4\u5b9a\u4e49

"},{"location":"chapter_searching/binary_search/#1012","title":"10.1.2 \u00a0 \u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"

\u4e8c\u5206\u67e5\u627e\u5728\u65f6\u95f4\u548c\u7a7a\u95f4\u65b9\u9762\u90fd\u6709\u8f83\u597d\u7684\u6027\u80fd\u3002

  • \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\u987b\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\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002

  • \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.3 \u00a0 \u4e8c\u5206\u67e5\u627e\u8fb9\u754c","text":""},{"location":"chapter_searching/binary_search_edge/#1031","title":"10.3.1 \u00a0 \u67e5\u627e\u5de6\u8fb9\u754c","text":"

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\u8fd4\u56de\u6570\u7ec4\u4e2d\u6700\u5de6\u4e00\u4e2a\u5143\u7d20 target \u7684\u7d22\u5f15\u3002\u82e5\u6570\u7ec4\u4e2d\u4e0d\u5305\u542b\u8be5\u5143\u7d20\uff0c\u5219\u8fd4\u56de \\(-1\\) \u3002

\u56de\u5fc6\u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\u7684\u65b9\u6cd5\uff0c\u641c\u7d22\u5b8c\u6210\u540e \\(i\\) \u6307\u5411\u6700\u5de6\u4e00\u4e2a target \uff0c\u56e0\u6b64\u67e5\u627e\u63d2\u5165\u70b9\u672c\u8d28\u4e0a\u662f\u5728\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target \u7684\u7d22\u5f15\u3002

\u8003\u8651\u901a\u8fc7\u67e5\u627e\u63d2\u5165\u70b9\u7684\u51fd\u6570\u5b9e\u73b0\u67e5\u627e\u5de6\u8fb9\u754c\u3002\u8bf7\u6ce8\u610f\uff0c\u6570\u7ec4\u4e2d\u53ef\u80fd\u4e0d\u5305\u542b target \uff0c\u8fd9\u79cd\u60c5\u51b5\u53ef\u80fd\u5bfc\u81f4\u4ee5\u4e0b\u4e24\u79cd\u7ed3\u679c\u3002

  • \u63d2\u5165\u70b9\u7684\u7d22\u5f15 \\(i\\) \u8d8a\u754c\u3002
  • \u5143\u7d20 nums[i] \u4e0e target \u4e0d\u76f8\u7b49\u3002

\u5f53\u9047\u5230\u4ee5\u4e0a\u4e24\u79cd\u60c5\u51b5\u65f6\uff0c\u76f4\u63a5\u8fd4\u56de \\(-1\\) \u5373\u53ef\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_edge.py
def binary_search_left_edge(nums: list[int], target: int) -> int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target\"\"\"\n# \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\ni = binary_search_insertion(nums, target)\n# \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif i == len(nums) or nums[i] != target:\nreturn -1\n# \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i\n
binary_search_edge.cpp
/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(vector<int> &nums, int target) {\n// \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\nint i = binarySearchInsertion(nums, target);\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (i == nums.size() || nums[i] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i;\n}\n
binary_search_edge.java
/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(int[] nums, int target) {\n// \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\nint i = binary_search_insertion.binarySearchInsertion(nums, target);\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (i == nums.length || nums[i] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i;\n}\n
binary_search_edge.cs
/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint BinarySearchLeftEdge(int[] nums, int target) {\n// \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\nint i = binary_search_insertion.BinarySearchInsertion(nums, target);\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (i == nums.Length || nums[i] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i;\n}\n
binary_search_edge.go
/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfunc binarySearchLeftEdge(nums []int, target int) int {\n// \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\ni := binarySearchInsertion(nums, target)\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif i == len(nums) || nums[i] != target {\nreturn -1\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i\n}\n
binary_search_edge.swift
/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfunc binarySearchLeftEdge(nums: [Int], target: Int) -> Int {\n// \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\nlet i = binarySearchInsertion(nums: nums, target: target)\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif i == nums.count || nums[i] != target {\nreturn -1\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i\n}\n
binary_search_edge.js
/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfunction binarySearchLeftEdge(nums, target) {\n// \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\nconst i = binarySearchInsertion(nums, target);\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (i === nums.length || nums[i] !== target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i;\n}\n
binary_search_edge.ts
/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfunction binarySearchLeftEdge(nums: Array<number>, target: number): number {\n// \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\nconst i = binarySearchInsertion(nums, target);\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (i === nums.length || nums[i] !== target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i;\n}\n
binary_search_edge.dart
/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(List<int> nums, int target) {\n// \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\nint i = binarySearchInsertion(nums, target);\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (i == nums.length || nums[i] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i;\n}\n
binary_search_edge.rs
/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfn binary_search_left_edge(nums: &[i32], target: i32) -> i32 {\n// \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\nlet i = binary_search_insertion(nums, target);\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif i == nums.len() as i32 || nums[i as usize] != target {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\ni\n}\n
binary_search_edge.c
/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(int *nums, int numSize, int target) {\n// \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\nint i = binarySearchInsertion(nums, numSize, target);\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (i == numSize || nums[i] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i;\n}\n
binary_search_edge.zig
[class]{}-[func]{binarySearchLeftEdge}\n
"},{"location":"chapter_searching/binary_search_edge/#1032","title":"10.3.2 \u00a0 \u67e5\u627e\u53f3\u8fb9\u754c","text":"

\u90a3\u4e48\u5982\u4f55\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target \u5462\uff1f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\u662f\u4fee\u6539\u4ee3\u7801\uff0c\u66ff\u6362\u5728 nums[m] == target \u60c5\u51b5\u4e0b\u7684\u6307\u9488\u6536\u7f29\u64cd\u4f5c\u3002\u4ee3\u7801\u5728\u6b64\u7701\u7565\uff0c\u6709\u5174\u8da3\u7684\u540c\u5b66\u53ef\u4ee5\u81ea\u884c\u5b9e\u73b0\u3002

\u4e0b\u9762\u6211\u4eec\u4ecb\u7ecd\u4e24\u79cd\u66f4\u52a0\u53d6\u5de7\u7684\u65b9\u6cd5\u3002

"},{"location":"chapter_searching/binary_search_edge/#1","title":"1. \u00a0 \u590d\u7528\u67e5\u627e\u5de6\u8fb9\u754c","text":"

\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u67e5\u627e\u6700\u5de6\u5143\u7d20\u7684\u51fd\u6570\u6765\u67e5\u627e\u6700\u53f3\u5143\u7d20\uff0c\u5177\u4f53\u65b9\u6cd5\u4e3a\uff1a\u5c06\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\u3002

\u5982\u56fe 10-7 \u6240\u793a\uff0c\u67e5\u627e\u5b8c\u6210\u540e\uff0c\u6307\u9488 \\(i\\) \u6307\u5411\u6700\u5de6\u4e00\u4e2a target + 1\uff08\u5982\u679c\u5b58\u5728\uff09\uff0c\u800c \\(j\\) \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0c\u56e0\u6b64\u8fd4\u56de \\(j\\) \u5373\u53ef\u3002

\u56fe 10-7 \u00a0 \u5c06\u67e5\u627e\u53f3\u8fb9\u754c\u8f6c\u5316\u4e3a\u67e5\u627e\u5de6\u8fb9\u754c

\u8bf7\u6ce8\u610f\uff0c\u8fd4\u56de\u7684\u63d2\u5165\u70b9\u662f \\(i\\) \uff0c\u56e0\u6b64\u9700\u8981\u5c06\u5176\u51cf \\(1\\) \uff0c\u4ece\u800c\u83b7\u5f97 \\(j\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_edge.py
def binary_search_right_edge(nums: list[int], target: int) -> int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target\"\"\"\n# \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\ni = binary_search_insertion(nums, target + 1)\n# j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nj = i - 1\n# \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif j == -1 or nums[j] != target:\nreturn -1\n# \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j\n
binary_search_edge.cpp
/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(vector<int> &nums, int target) {\n// \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\nint i = binarySearchInsertion(nums, target + 1);\n// j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nint j = i - 1;\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (j == -1 || nums[j] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j;\n}\n
binary_search_edge.java
/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(int[] nums, int target) {\n// \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\nint i = binary_search_insertion.binarySearchInsertion(nums, target + 1);\n// j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nint j = i - 1;\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (j == -1 || nums[j] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j;\n}\n
binary_search_edge.cs
/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint BinarySearchRightEdge(int[] nums, int target) {\n// \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\nint i = binary_search_insertion.BinarySearchInsertion(nums, target + 1);\n// j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nint j = i - 1;\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (j == -1 || nums[j] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j;\n}\n
binary_search_edge.go
/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfunc binarySearchRightEdge(nums []int, target int) int {\n// \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\ni := binarySearchInsertion(nums, target+1)\n// j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nj := i - 1\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif j == -1 || nums[j] != target {\nreturn -1\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j\n}\n
binary_search_edge.swift
/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfunc binarySearchRightEdge(nums: [Int], target: Int) -> Int {\n// \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\nlet i = binarySearchInsertion(nums: nums, target: target + 1)\n// j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nlet j = i - 1\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif j == -1 || nums[j] != target {\nreturn -1\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j\n}\n
binary_search_edge.js
/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfunction binarySearchRightEdge(nums, target) {\n// \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\nconst i = binarySearchInsertion(nums, target + 1);\n// j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nconst j = i - 1;\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (j === -1 || nums[j] !== target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j;\n}\n
binary_search_edge.ts
/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfunction binarySearchRightEdge(nums: Array<number>, target: number): number {\n// \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\nconst i = binarySearchInsertion(nums, target + 1);\n// j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nconst j = i - 1;\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (j === -1 || nums[j] !== target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j;\n}\n
binary_search_edge.dart
/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(List<int> nums, int target) {\n// \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\nint i = binarySearchInsertion(nums, target + 1);\n// j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nint j = i - 1;\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (j == -1 || nums[j] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j;\n}\n
binary_search_edge.rs
/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfn binary_search_right_edge(nums: &[i32], target: i32) -> i32 {\n// \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\nlet i = binary_search_insertion(nums, target + 1);\n// j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nlet j = i - 1;\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif j == -1 || nums[j as usize] != target {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nj\n}\n
binary_search_edge.c
/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(int *nums, int numSize, int target) {\n// \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\nint i = binarySearchInsertion(nums, numSize, target + 1);\n// j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nint j = i - 1;\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (j == -1 || nums[j] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j;\n}\n
binary_search_edge.zig
[class]{}-[func]{binarySearchRightEdge}\n
"},{"location":"chapter_searching/binary_search_edge/#2","title":"2. \u00a0 \u8f6c\u5316\u4e3a\u67e5\u627e\u5143\u7d20","text":"

\u6211\u4eec\u77e5\u9053\uff0c\u5f53\u6570\u7ec4\u4e0d\u5305\u542b target \u65f6\uff0c\u6700\u7ec8 \\(i\\) \u548c \\(j\\) \u4f1a\u5206\u522b\u6307\u5411\u9996\u4e2a\u5927\u4e8e\u3001\u5c0f\u4e8e target \u7684\u5143\u7d20\u3002

\u56e0\u6b64\uff0c\u5982\u56fe 10-8 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u6784\u9020\u4e00\u4e2a\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728\u7684\u5143\u7d20\uff0c\u7528\u4e8e\u67e5\u627e\u5de6\u53f3\u8fb9\u754c\u3002

  • \u67e5\u627e\u6700\u5de6\u4e00\u4e2a target \uff1a\u53ef\u4ee5\u8f6c\u5316\u4e3a\u67e5\u627e target - 0.5 \uff0c\u5e76\u8fd4\u56de\u6307\u9488 \\(i\\) \u3002
  • \u67e5\u627e\u6700\u53f3\u4e00\u4e2a target \uff1a\u53ef\u4ee5\u8f6c\u5316\u4e3a\u67e5\u627e target + 0.5 \uff0c\u5e76\u8fd4\u56de\u6307\u9488 \\(j\\) \u3002

\u56fe 10-8 \u00a0 \u5c06\u67e5\u627e\u8fb9\u754c\u8f6c\u5316\u4e3a\u67e5\u627e\u5143\u7d20

\u4ee3\u7801\u5728\u6b64\u7701\u7565\uff0c\u503c\u5f97\u6ce8\u610f\u4ee5\u4e0b\u4e24\u70b9\u3002

  • \u7ed9\u5b9a\u6570\u7ec4\u4e0d\u5305\u542b\u5c0f\u6570\uff0c\u8fd9\u610f\u5473\u7740\u6211\u4eec\u65e0\u987b\u5173\u5fc3\u5982\u4f55\u5904\u7406\u76f8\u7b49\u7684\u60c5\u51b5\u3002
  • \u56e0\u4e3a\u8be5\u65b9\u6cd5\u5f15\u5165\u4e86\u5c0f\u6570\uff0c\u6240\u4ee5\u9700\u8981\u5c06\u51fd\u6570\u4e2d\u7684\u53d8\u91cf target \u6539\u4e3a\u6d6e\u70b9\u6570\u7c7b\u578b\u3002
"},{"location":"chapter_searching/binary_search_insertion/","title":"10.2 \u00a0 \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9","text":"

\u4e8c\u5206\u67e5\u627e\u4e0d\u4ec5\u53ef\u7528\u4e8e\u641c\u7d22\u76ee\u6807\u5143\u7d20\uff0c\u8fd8\u5177\u6709\u8bb8\u591a\u53d8\u79cd\u95ee\u9898\uff0c\u6bd4\u5982\u641c\u7d22\u76ee\u6807\u5143\u7d20\u7684\u63d2\u5165\u4f4d\u7f6e\u3002

"},{"location":"chapter_searching/binary_search_insertion/#1021","title":"10.2.1 \u00a0 \u65e0\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"

Question

\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6709\u5e8f\u6570\u7ec4 nums \u548c\u4e00\u4e2a\u5143\u7d20 target \uff0c\u6570\u7ec4\u4e0d\u5b58\u5728\u91cd\u590d\u5143\u7d20\u3002\u73b0\u5c06 target \u63d2\u5165\u5230\u6570\u7ec4 nums \u4e2d\uff0c\u5e76\u4fdd\u6301\u5176\u6709\u5e8f\u6027\u3002\u82e5\u6570\u7ec4\u4e2d\u5df2\u5b58\u5728\u5143\u7d20 target \uff0c\u5219\u63d2\u5165\u5230\u5176\u5de6\u65b9\u3002\u8bf7\u8fd4\u56de\u63d2\u5165\u540e target \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\u3002

\u56fe 10-4 \u00a0 \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\u793a\u4f8b\u6570\u636e

\u5982\u679c\u60f3\u8981\u590d\u7528\u4e0a\u8282\u7684\u4e8c\u5206\u67e5\u627e\u4ee3\u7801\uff0c\u5219\u9700\u8981\u56de\u7b54\u4ee5\u4e0b\u4e24\u4e2a\u95ee\u9898\u3002

\u95ee\u9898\u4e00\uff1a\u5f53\u6570\u7ec4\u4e2d\u5305\u542b target \u65f6\uff0c\u63d2\u5165\u70b9\u7684\u7d22\u5f15\u662f\u5426\u662f\u8be5\u5143\u7d20\u7684\u7d22\u5f15\uff1f

\u9898\u76ee\u8981\u6c42\u5c06 target \u63d2\u5165\u5230\u76f8\u7b49\u5143\u7d20\u7684\u5de6\u8fb9\uff0c\u8fd9\u610f\u5473\u7740\u65b0\u63d2\u5165\u7684 target \u66ff\u6362\u4e86\u539f\u6765 target \u7684\u4f4d\u7f6e\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u6570\u7ec4\u5305\u542b target \u65f6\uff0c\u63d2\u5165\u70b9\u7684\u7d22\u5f15\u5c31\u662f\u8be5 target \u7684\u7d22\u5f15\u3002

\u95ee\u9898\u4e8c\uff1a\u5f53\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728 target \u65f6\uff0c\u63d2\u5165\u70b9\u662f\u54ea\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\uff1f

\u8fdb\u4e00\u6b65\u601d\u8003\u4e8c\u5206\u67e5\u627e\u8fc7\u7a0b\uff1a\u5f53 nums[m] < target \u65f6 \\(i\\) \u79fb\u52a8\uff0c\u8fd9\u610f\u5473\u7740\u6307\u9488 \\(i\\) \u5728\u5411\u5927\u4e8e\u7b49\u4e8e target \u7684\u5143\u7d20\u9760\u8fd1\u3002\u540c\u7406\uff0c\u6307\u9488 \\(j\\) \u59cb\u7ec8\u5728\u5411\u5c0f\u4e8e\u7b49\u4e8e target \u7684\u5143\u7d20\u9760\u8fd1\u3002

\u56e0\u6b64\u4e8c\u5206\u7ed3\u675f\u65f6\u4e00\u5b9a\u6709\uff1a\\(i\\) \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\uff0c\\(j\\) \u6307\u5411\u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u3002\u6613\u5f97\u5f53\u6570\u7ec4\u4e0d\u5305\u542b target \u65f6\uff0c\u63d2\u5165\u7d22\u5f15\u4e3a \\(i\\) \u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_insertion.py
def binary_search_insertion_simple(nums: list[int], target: int) -> int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09\"\"\"\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:\nreturn m  # \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n# \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i\n
binary_search_insertion.cpp
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(vector<int> &nums, int target) {\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\n} else if (nums[m] > target) {\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nreturn m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n}\n}\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.java
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(int[] nums, int target) {\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\n} else if (nums[m] > target) {\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nreturn m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n}\n}\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.cs
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint BinarySearchInsertionSimple(int[] nums, int target) {\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\n} else if (nums[m] > target) {\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nreturn m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n}\n}\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.go
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertionSimple(nums []int, target int) int {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\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// \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\nreturn m\n}\n}\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i\n}\n
binary_search_insertion.swift
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertionSimple(nums: [Int], target: Int) -> Int {\nvar i = 0, j = nums.count - 1 // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-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 {\nreturn m // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n}\n}\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i\n}\n
binary_search_insertion.js
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfunction binarySearchInsertionSimple(nums, target) {\nlet i = 0,\nj = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nconst m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\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 {\nreturn m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n}\n}\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.ts
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfunction binarySearchInsertionSimple(\nnums: Array<number>,\ntarget: number\n): number {\nlet i = 0,\nj = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nconst m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\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 {\nreturn m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n}\n}\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.dart
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(List<int> nums, int target) {\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\n} else if (nums[m] > target) {\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nreturn m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n}\n}\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.rs
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\npub fn binary_search_insertion(nums: &[i32], target: i32) -> i32 {\nlet (mut i, mut j) = (0, nums.len() as i32 - 1);    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile i <= j {\nlet m = i + (j - i) / 2;    // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m as usize] < target {\ni = m + 1;  // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if nums[m as usize] > 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}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\ni\n}\n
binary_search_insertion.c
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(int *nums, int numSize, int target) {\nint i = 0, j = numSize - 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\n} else if (nums[m] > target) {\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nreturn m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n}\n}\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.zig
[class]{}-[func]{binarySearchInsertionSimple}\n
"},{"location":"chapter_searching/binary_search_insertion/#1022","title":"10.2.2 \u00a0 \u5b58\u5728\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"

Question

\u5728\u4e0a\u4e00\u9898\u7684\u57fa\u7840\u4e0a\uff0c\u89c4\u5b9a\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u5176\u4f59\u4e0d\u53d8\u3002

\u5047\u8bbe\u6570\u7ec4\u4e2d\u5b58\u5728\u591a\u4e2a target \uff0c\u5219\u666e\u901a\u4e8c\u5206\u67e5\u627e\u53ea\u80fd\u8fd4\u56de\u5176\u4e2d\u4e00\u4e2a target \u7684\u7d22\u5f15\uff0c\u800c\u65e0\u6cd5\u786e\u5b9a\u8be5\u5143\u7d20\u7684\u5de6\u8fb9\u548c\u53f3\u8fb9\u8fd8\u6709\u591a\u5c11 target\u3002

\u9898\u76ee\u8981\u6c42\u5c06\u76ee\u6807\u5143\u7d20\u63d2\u5165\u5230\u6700\u5de6\u8fb9\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5de6\u4e00\u4e2a target \u7684\u7d22\u5f15\u3002\u521d\u6b65\u8003\u8651\u901a\u8fc7\u56fe 10-5 \u6240\u793a\u7684\u6b65\u9aa4\u5b9e\u73b0\u3002

  1. \u6267\u884c\u4e8c\u5206\u67e5\u627e\uff0c\u5f97\u5230\u4efb\u610f\u4e00\u4e2a target \u7684\u7d22\u5f15\uff0c\u8bb0\u4e3a \\(k\\) \u3002
  2. \u4ece\u7d22\u5f15 \\(k\\) \u5f00\u59cb\uff0c\u5411\u5de6\u8fdb\u884c\u7ebf\u6027\u904d\u5386\uff0c\u5f53\u627e\u5230\u6700\u5de6\u8fb9\u7684 target \u65f6\u8fd4\u56de\u3002

\u56fe 10-5 \u00a0 \u7ebf\u6027\u67e5\u627e\u91cd\u590d\u5143\u7d20\u7684\u63d2\u5165\u70b9

\u6b64\u65b9\u6cd5\u867d\u7136\u53ef\u7528\uff0c\u4f46\u5176\u5305\u542b\u7ebf\u6027\u67e5\u627e\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002\u5f53\u6570\u7ec4\u4e2d\u5b58\u5728\u5f88\u591a\u91cd\u590d\u7684 target \u65f6\uff0c\u8be5\u65b9\u6cd5\u6548\u7387\u5f88\u4f4e\u3002

\u73b0\u8003\u8651\u62d3\u5c55\u4e8c\u5206\u67e5\u627e\u4ee3\u7801\u3002\u5982\u56fe 10-6 \u6240\u793a\uff0c\u6574\u4f53\u6d41\u7a0b\u4fdd\u6301\u4e0d\u53d8\uff0c\u6bcf\u8f6e\u5148\u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 \\(m\\) \uff0c\u518d\u5224\u65ad target \u548c nums[m] \u5927\u5c0f\u5173\u7cfb\uff0c\u5206\u4e3a\u4ee5\u4e0b\u51e0\u79cd\u60c5\u51b5\u3002

  • \u5f53 nums[m] < target \u6216 nums[m] > target \u65f6\uff0c\u8bf4\u660e\u8fd8\u6ca1\u6709\u627e\u5230 target \uff0c\u56e0\u6b64\u91c7\u7528\u666e\u901a\u4e8c\u5206\u67e5\u627e\u7684\u7f29\u5c0f\u533a\u95f4\u64cd\u4f5c\uff0c\u4ece\u800c\u4f7f\u6307\u9488 \\(i\\) \u548c \\(j\\) \u5411 target \u9760\u8fd1\u3002
  • \u5f53 nums[m] == target \u65f6\uff0c\u8bf4\u660e\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 \\([i, m - 1]\\) \u4e2d\uff0c\u56e0\u6b64\u91c7\u7528 \\(j = m - 1\\) \u6765\u7f29\u5c0f\u533a\u95f4\uff0c\u4ece\u800c\u4f7f\u6307\u9488 \\(j\\) \u5411\u5c0f\u4e8e target \u7684\u5143\u7d20\u9760\u8fd1\u3002

\u5faa\u73af\u5b8c\u6210\u540e\uff0c\\(i\\) \u6307\u5411\u6700\u5de6\u8fb9\u7684 target \uff0c\\(j\\) \u6307\u5411\u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\uff0c\u56e0\u6b64\u7d22\u5f15 \\(i\\) \u5c31\u662f\u63d2\u5165\u70b9\u3002

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

\u56fe 10-6 \u00a0 \u4e8c\u5206\u67e5\u627e\u91cd\u590d\u5143\u7d20\u7684\u63d2\u5165\u70b9\u7684\u6b65\u9aa4

\u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u5224\u65ad\u5206\u652f nums[m] > target \u548c nums[m] == target \u7684\u64cd\u4f5c\u76f8\u540c\uff0c\u56e0\u6b64\u4e24\u8005\u53ef\u4ee5\u5408\u5e76\u3002

\u5373\u4fbf\u5982\u6b64\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u5c06\u5224\u65ad\u6761\u4ef6\u4fdd\u6301\u5c55\u5f00\uff0c\u56e0\u4e3a\u5176\u903b\u8f91\u66f4\u52a0\u6e05\u6670\u3001\u53ef\u8bfb\u6027\u66f4\u597d\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_insertion.py
def binary_search_insertion(nums: list[int], target: int) -> int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09\"\"\"\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\n# \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i\n
binary_search_insertion.cpp
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(vector<int> &nums, int target) {\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\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}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.java
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(int[] nums, int target) {\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\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}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.cs
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint BinarySearchInsertion(int[] nums, int target) {\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\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}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.go
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertion(nums []int, target int) int {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\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}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i\n}\n
binary_search_insertion.swift
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertion(nums: [Int], target: Int) -> Int {\nvar i = 0, j = nums.count - 1 // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-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 {\nj = m - 1 // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n}\n}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i\n}\n
binary_search_insertion.js
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfunction binarySearchInsertion(nums, target) {\nlet i = 0,\nj = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nconst m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\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}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.ts
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfunction binarySearchInsertion(nums: Array<number>, target: number): number {\nlet i = 0,\nj = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i <= j) {\nconst m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\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}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.dart
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(List<int> nums, int target) {\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\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}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.rs
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\npub fn binary_search_insertion(nums: &[i32], target: i32) -> i32 {\nlet (mut i, mut j) = (0, nums.len() as i32 - 1);    // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile i <= j {\nlet m = i + (j - i) / 2;    // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m as usize] < target {\ni = m + 1;  // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if nums[m as usize] > 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}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\ni\n}\n
binary_search_insertion.c
/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(int *nums, int numSize, int target) {\nint i = 0, j = numSize - 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\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}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n
binary_search_insertion.zig
[class]{}-[func]{binarySearchInsertion}\n

Tip

\u672c\u8282\u7684\u4ee3\u7801\u90fd\u662f\u201c\u53cc\u95ed\u533a\u95f4\u201d\u5199\u6cd5\u3002\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u5b9e\u73b0\u201c\u5de6\u95ed\u53f3\u5f00\u201d\u5199\u6cd5\u3002

\u603b\u7684\u6765\u770b\uff0c\u4e8c\u5206\u67e5\u627e\u65e0\u975e\u5c31\u662f\u7ed9\u6307\u9488 \\(i\\) \u548c \\(j\\) \u5206\u522b\u8bbe\u5b9a\u641c\u7d22\u76ee\u6807\uff0c\u76ee\u6807\u53ef\u80fd\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u5143\u7d20\uff08\u4f8b\u5982 target \uff09\uff0c\u4e5f\u53ef\u80fd\u662f\u4e00\u4e2a\u5143\u7d20\u8303\u56f4\uff08\u4f8b\u5982\u5c0f\u4e8e target \u7684\u5143\u7d20\uff09\u3002

\u5728\u4e0d\u65ad\u7684\u5faa\u73af\u4e8c\u5206\u4e2d\uff0c\u6307\u9488 \\(i\\) \u548c \\(j\\) \u90fd\u9010\u6e10\u903c\u8fd1\u9884\u5148\u8bbe\u5b9a\u7684\u76ee\u6807\u3002\u6700\u7ec8\uff0c\u5b83\u4eec\u6216\u662f\u6210\u529f\u627e\u5230\u7b54\u6848\uff0c\u6216\u662f\u8d8a\u8fc7\u8fb9\u754c\u540e\u505c\u6b62\u3002

"},{"location":"chapter_searching/replace_linear_by_hashing/","title":"10.4 \u00a0 \u54c8\u5e0c\u4f18\u5316\u7b56\u7565","text":"

\u5728\u7b97\u6cd5\u9898\u4e2d\uff0c\u6211\u4eec\u5e38\u901a\u8fc7\u5c06\u7ebf\u6027\u67e5\u627e\u66ff\u6362\u4e3a\u54c8\u5e0c\u67e5\u627e\u6765\u964d\u4f4e\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002\u6211\u4eec\u501f\u52a9\u4e00\u4e2a\u7b97\u6cd5\u9898\u6765\u52a0\u6df1\u7406\u89e3\u3002

Question

\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums \u548c\u4e00\u4e2a\u76ee\u6807\u5143\u7d20 target \uff0c\u8bf7\u5728\u6570\u7ec4\u4e2d\u641c\u7d22\u201c\u548c\u201d\u4e3a target \u7684\u4e24\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u6570\u7ec4\u7d22\u5f15\u3002\u8fd4\u56de\u4efb\u610f\u4e00\u4e2a\u89e3\u5373\u53ef\u3002

"},{"location":"chapter_searching/replace_linear_by_hashing/#1041","title":"10.4.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\u5982\u56fe 10-9 \u6240\u793a\uff0c\u6211\u4eec\u5f00\u542f\u4e00\u4e2a\u4e24\u5c42\u5faa\u73af\uff0c\u5728\u6bcf\u8f6e\u4e2d\u5224\u65ad\u4e24\u4e2a\u6574\u6570\u7684\u548c\u662f\u5426\u4e3a target \uff0c\u82e5\u662f\u5219\u8fd4\u56de\u5b83\u4eec\u7684\u7d22\u5f15\u3002

\u56fe 10-9 \u00a0 \u7ebf\u6027\u67e5\u627e\u6c42\u89e3\u4e24\u6570\u4e4b\u548c

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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 Array.Empty<int>();\n}\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.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.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.dart
/* \u65b9\u6cd5\u4e00\uff1a \u66b4\u529b\u679a\u4e3e */\nList<int> twoSumBruteForce(List<int> nums, int target) {\nint size = nums.length;\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\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
two_sum.rs
/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\npub fn two_sum_brute_force(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {\nlet size = nums.len();\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor i in 0..size - 1 {\nfor j in i + 1..size {\nif nums[i] + nums[j] == target {\nreturn Some(vec![i as i32, j as i32]);\n}\n}\n}\nNone\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.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(i), @intCast(j)};\n}\n}\n}\nreturn null;\n}\n

\u6b64\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff0c\u5728\u5927\u6570\u636e\u91cf\u4e0b\u975e\u5e38\u8017\u65f6\u3002

"},{"location":"chapter_searching/replace_linear_by_hashing/#1042","title":"10.4.2 \u00a0 \u54c8\u5e0c\u67e5\u627e\uff1a\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4","text":"

\u8003\u8651\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868\uff0c\u952e\u503c\u5bf9\u5206\u522b\u4e3a\u6570\u7ec4\u5143\u7d20\u548c\u5143\u7d20\u7d22\u5f15\u3002\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u6bcf\u8f6e\u6267\u884c\u56fe 10-10 \u6240\u793a\u7684\u6b65\u9aa4\u3002

  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\u3002
  2. \u5c06\u952e\u503c\u5bf9 nums[i] \u548c\u7d22\u5f15 i \u6dfb\u52a0\u8fdb\u54c8\u5e0c\u8868\u3002
<1><2><3>

\u56fe 10-10 \u00a0 \u8f85\u52a9\u54c8\u5e0c\u8868\u6c42\u89e3\u4e24\u6570\u4e4b\u548c

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

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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 Array.Empty<int>();\n}\n
two_sum.go
/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunc twoSumHashTable(nums []int, target int) []int {\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 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.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.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[target - nums[i]] !== undefined) {\nreturn [m[target - nums[i]], i];\n} else {\nm[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(target - nums[i]);\nif (index !== undefined) {\nreturn [index, i];\n} else {\nm.set(nums[i], i);\n}\n}\nreturn [];\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;\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nMap<int, int> dic = HashMap();\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\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
two_sum.rs
/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\npub fn two_sum_hash_table(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nlet mut dic = HashMap::new();\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor (i, num) in nums.iter().enumerate() {\nmatch dic.get(&(target - num)) {\nSome(v) => return Some(vec![*v as i32, i as i32]),\nNone => dic.insert(num, i as i32)\n};\n}\nNone\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.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(i)};\n}\ntry dic.put(nums[i], @intCast(i));\n}\nreturn null;\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.5 \u00a0 \u91cd\u8bc6\u641c\u7d22\u7b97\u6cd5","text":"

\u300c\u641c\u7d22\u7b97\u6cd5 searching algorithm\u300d\u7528\u4e8e\u5728\u6570\u636e\u7ed3\u6784\uff08\u4f8b\u5982\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6811\u6216\u56fe\uff09\u4e2d\u641c\u7d22\u4e00\u4e2a\u6216\u4e00\u7ec4\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002

\u641c\u7d22\u7b97\u6cd5\u53ef\u6839\u636e\u5b9e\u73b0\u601d\u8def\u5206\u4e3a\u4ee5\u4e0b\u4e24\u7c7b\u3002

  • \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/#1051","title":"10.5.1 \u00a0 \u66b4\u529b\u641c\u7d22","text":"

\u66b4\u529b\u641c\u7d22\u901a\u8fc7\u904d\u5386\u6570\u636e\u7ed3\u6784\u7684\u6bcf\u4e2a\u5143\u7d20\u6765\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\u3002

  • \u201c\u7ebf\u6027\u641c\u7d22\u201d\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
  • \u201c\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u201d\u548c\u201c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u201d\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\u987b\u5bf9\u6570\u636e\u505a\u9884\u5904\u7406\u548c\u501f\u52a9\u989d\u5916\u7684\u6570\u636e\u7ed3\u6784\u3002

\u7136\u800c\uff0c\u6b64\u7c7b\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u5143\u7d20\u6570\u91cf\uff0c\u56e0\u6b64\u5728\u6570\u636e\u91cf\u8f83\u5927\u7684\u60c5\u51b5\u4e0b\u6027\u80fd\u8f83\u5dee\u3002

"},{"location":"chapter_searching/searching_algorithm_revisited/#1052","title":"10.5.2 \u00a0 \u81ea\u9002\u5e94\u641c\u7d22","text":"

\u81ea\u9002\u5e94\u641c\u7d22\u5229\u7528\u6570\u636e\u7684\u7279\u6709\u5c5e\u6027\uff08\u4f8b\u5982\u6709\u5e8f\u6027\uff09\u6765\u4f18\u5316\u641c\u7d22\u8fc7\u7a0b\uff0c\u4ece\u800c\u66f4\u9ad8\u6548\u5730\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\u3002

  • \u201c\u4e8c\u5206\u67e5\u627e\u201d\u5229\u7528\u6570\u636e\u7684\u6709\u5e8f\u6027\u5b9e\u73b0\u9ad8\u6548\u67e5\u627e\uff0c\u4ec5\u9002\u7528\u4e8e\u6570\u7ec4\u3002
  • \u201c\u54c8\u5e0c\u67e5\u627e\u201d\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
  • \u201c\u6811\u67e5\u627e\u201d\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/#1053","title":"10.5.3 \u00a0 \u641c\u7d22\u65b9\u6cd5\u9009\u53d6","text":"

\u7ed9\u5b9a\u5927\u5c0f\u4e3a \\(n\\) \u7684\u4e00\u7ec4\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7ebf\u6027\u641c\u7d22\u3001\u4e8c\u5206\u67e5\u627e\u3001\u6811\u67e5\u627e\u3001\u54c8\u5e0c\u67e5\u627e\u7b49\u591a\u79cd\u65b9\u6cd5\u5728\u8be5\u6570\u636e\u4e2d\u641c\u7d22\u76ee\u6807\u5143\u7d20\u3002\u5404\u4e2a\u65b9\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u5982\u56fe 10-11 \u6240\u793a\u3002

\u56fe 10-11 \u00a0 \u591a\u79cd\u641c\u7d22\u7b56\u7565

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

\u8868 10-1 \u00a0 \u67e5\u627e\u7b97\u6cd5\u6548\u7387\u5bf9\u6bd4

\u7ebf\u6027\u641c\u7d22 \u4e8c\u5206\u67e5\u627e \u6811\u67e5\u627e \u54c8\u5e0c\u67e5\u627e \u67e5\u627e\u5143\u7d20 \\(O(n)\\) \\(O(\\log n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u63d2\u5165\u5143\u7d20 \\(O(1)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u989d\u5916\u7a7a\u95f4 \\(O(1)\\) \\(O(1)\\) \\(O(n)\\) \\(O(n)\\) \u6570\u636e\u9884\u5904\u7406 / \u6392\u5e8f \\(O(n \\log n)\\) \u5efa\u6811 \\(O(n \\log n)\\) \u5efa\u54c8\u5e0c\u8868 \\(O(n)\\) \u6570\u636e\u662f\u5426\u6709\u5e8f \u65e0\u5e8f \u6709\u5e8f \u6709\u5e8f \u65e0\u5e8f

\u641c\u7d22\u7b97\u6cd5\u7684\u9009\u62e9\u8fd8\u53d6\u51b3\u4e8e\u6570\u636e\u4f53\u91cf\u3001\u641c\u7d22\u6027\u80fd\u8981\u6c42\u3001\u6570\u636e\u67e5\u8be2\u4e0e\u66f4\u65b0\u9891\u7387\u7b49\u3002

\u7ebf\u6027\u641c\u7d22

  • \u901a\u7528\u6027\u8f83\u597d\uff0c\u65e0\u987b\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\u5206\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.6 \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\u987b\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\u987b\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":"\u7b2c 11 \u7ae0 \u00a0 \u6392\u5e8f","text":"

Abstract

\u6392\u5e8f\u72b9\u5982\u4e00\u628a\u5c06\u6df7\u4e71\u53d8\u4e3a\u79e9\u5e8f\u7684\u9b54\u6cd5\u94a5\u5319\uff0c\u4f7f\u6211\u4eec\u80fd\u4ee5\u66f4\u9ad8\u6548\u7684\u65b9\u5f0f\u7406\u89e3\u4e0e\u5904\u7406\u6570\u636e\u3002

\u65e0\u8bba\u662f\u7b80\u5355\u7684\u5347\u5e8f\uff0c\u8fd8\u662f\u590d\u6742\u7684\u5206\u7c7b\u6392\u5217\uff0c\u6392\u5e8f\u90fd\u5411\u6211\u4eec\u5c55\u793a\u4e86\u6570\u636e\u7684\u548c\u8c10\u7f8e\u611f\u3002

"},{"location":"chapter_sorting/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 11.1 \u00a0 \u6392\u5e8f\u7b97\u6cd5
  • 11.2 \u00a0 \u9009\u62e9\u6392\u5e8f
  • 11.3 \u00a0 \u5192\u6ce1\u6392\u5e8f
  • 11.4 \u00a0 \u63d2\u5165\u6392\u5e8f
  • 11.5 \u00a0 \u5feb\u901f\u6392\u5e8f
  • 11.6 \u00a0 \u5f52\u5e76\u6392\u5e8f
  • 11.7 \u00a0 \u5806\u6392\u5e8f
  • 11.8 \u00a0 \u6876\u6392\u5e8f
  • 11.9 \u00a0 \u8ba1\u6570\u6392\u5e8f
  • 11.10 \u00a0 \u57fa\u6570\u6392\u5e8f
  • 11.11 \u00a0 \u5c0f\u7ed3
"},{"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

\u5982\u56fe 11-4 \u6240\u793a\uff0c\u5192\u6ce1\u8fc7\u7a0b\u53ef\u4ee5\u5229\u7528\u5143\u7d20\u4ea4\u6362\u64cd\u4f5c\u6765\u6a21\u62df\uff1a\u4ece\u6570\u7ec4\u6700\u5de6\u7aef\u5f00\u59cb\u5411\u53f3\u904d\u5386\uff0c\u4f9d\u6b21\u6bd4\u8f83\u76f8\u90bb\u5143\u7d20\u5927\u5c0f\uff0c\u5982\u679c\u201c\u5de6\u5143\u7d20 > \u53f3\u5143\u7d20\u201d\u5c31\u4ea4\u6362\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>

\u56fe 11-4 \u00a0 \u5229\u7528\u5143\u7d20\u4ea4\u6362\u64cd\u4f5c\u6a21\u62df\u5192\u6ce1

"},{"location":"chapter_sorting/bubble_sort/#1131","title":"11.3.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"

\u8bbe\u6570\u7ec4\u7684\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u5192\u6ce1\u6392\u5e8f\u7684\u6b65\u9aa4\u5982\u56fe 11-5 \u6240\u793a\u3002

  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\u987b\u6392\u5e8f\uff0c\u56e0\u6b64\u6570\u7ec4\u6392\u5e8f\u5b8c\u6210\u3002

\u56fe 11-5 \u00a0 \u5192\u6ce1\u6392\u5e8f\u6d41\u7a0b

PythonC++JavaC#GoSwiftJSTSDartRustCZig bubble_sort.py
def bubble_sort(nums: list[int]):\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.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.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.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]\n(nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n}\n}\n}\n}\n
bubble_sort.go
/* \u5192\u6ce1\u6392\u5e8f */\nfunc bubbleSort(nums []int) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\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.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.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.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
bubble_sort.rs
/* \u5192\u6ce1\u6392\u5e8f */\nfn bubble_sort(nums: &mut [i32]) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor i in (1..nums.len()).rev() {\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \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;\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.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
"},{"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

PythonC++JavaC#GoSwiftJSTSDartRustCZig bubble_sort.py
def bubble_sort_with_flag(nums: list[int]):\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.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.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.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]\n(nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\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.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.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.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.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
bubble_sort.rs
/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09 */\nfn bubble_sort_with_flag(nums: &mut [i32]) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor i in (1..nums.len()).rev() {\nlet mut flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \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;\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.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
"},{"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\\)\u3001\\(n - 2\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \uff0c\u603b\u548c\u4e3a \\((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\\) \u548c \\(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\u9636\u3002

\u300c\u6876\u6392\u5e8f bucket sort\u300d\u662f\u5206\u6cbb\u7b56\u7565\u7684\u4e00\u4e2a\u5178\u578b\u5e94\u7528\u3002\u5b83\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e9b\u5177\u6709\u5927\u5c0f\u987a\u5e8f\u7684\u6876\uff0c\u6bcf\u4e2a\u6876\u5bf9\u5e94\u4e00\u4e2a\u6570\u636e\u8303\u56f4\uff0c\u5c06\u6570\u636e\u5e73\u5747\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\uff1b\u7136\u540e\uff0c\u5728\u6bcf\u4e2a\u6876\u5185\u90e8\u5206\u522b\u6267\u884c\u6392\u5e8f\uff1b\u6700\u7ec8\u6309\u7167\u6876\u7684\u987a\u5e8f\u5c06\u6240\u6709\u6570\u636e\u5408\u5e76\u3002

"},{"location":"chapter_sorting/bucket_sort/#1181","title":"11.8.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"

\u8003\u8651\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\uff0c\u5143\u7d20\u662f\u8303\u56f4 \\([0, 1)\\) \u7684\u6d6e\u70b9\u6570\u3002\u6876\u6392\u5e8f\u7684\u6d41\u7a0b\u5982\u56fe 11-13 \u6240\u793a\u3002

  1. \u521d\u59cb\u5316 \\(k\\) \u4e2a\u6876\uff0c\u5c06 \\(n\\) \u4e2a\u5143\u7d20\u5206\u914d\u5230 \\(k\\) \u4e2a\u6876\u4e2d\u3002
  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\u3002
  3. \u6309\u7167\u6876\u7684\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\uff0c\u5408\u5e76\u7ed3\u679c\u3002

\u56fe 11-13 \u00a0 \u6876\u6392\u5e8f\u7b97\u6cd5\u6d41\u7a0b

PythonC++JavaC#GoSwiftJSTSDartRustCZig bucket_sort.py
def bucket_sort(nums: list[float]):\n\"\"\"\u6876\u6392\u5e8f\"\"\"\n# \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\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\u5e8f\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.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.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.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();\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.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.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 * Double(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.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.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
bucket_sort.rs
/* \u6876\u6392\u5e8f */\nfn bucket_sort(nums: &mut [f64]) {\n// \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\nlet k = nums.len() / 2;\nlet mut buckets = vec![vec![]; k];\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor &mut num in &mut *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 = (num * k as f64) as usize;\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\nbuckets[i].push(num);\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor bucket in &mut buckets {\n// \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\nbucket.sort_by(|a, b| a.partial_cmp(b).unwrap());\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nlet mut i = 0;\nfor bucket in &mut buckets {\nfor &mut num in bucket {\nnums[i] = num;\ni += 1;\n}\n}\n}\n
bucket_sort.c
/* \u6876\u6392\u5e8f */\nvoid bucketSort(float nums[], int size) {\n// \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\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.zig
[class]{}-[func]{bucketSort}\n
"},{"location":"chapter_sorting/bucket_sort/#1182","title":"11.8.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"

\u6876\u6392\u5e8f\u9002\u7528\u4e8e\u5904\u7406\u4f53\u91cf\u5f88\u5927\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u8f93\u5165\u6570\u636e\u5305\u542b 100 \u4e07\u4e2a\u5143\u7d20\uff0c\u7531\u4e8e\u7a7a\u95f4\u9650\u5236\uff0c\u7cfb\u7edf\u5185\u5b58\u65e0\u6cd5\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u6570\u636e\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u5206\u6210 1000 \u4e2a\u6876\uff0c\u7136\u540e\u5206\u522b\u5bf9\u6bcf\u4e2a\u6876\u8fdb\u884c\u6392\u5e8f\uff0c\u6700\u540e\u5c06\u7ed3\u679c\u5408\u5e76\u3002

  • \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\u6240\u6709\u6876\u548c\u5143\u7d20\uff0c\u82b1\u8d39 \\(O(n + 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

\u5982\u56fe 11-14 \u6240\u793a\uff0c\u8fd9\u79cd\u65b9\u6cd5\u672c\u8d28\u4e0a\u662f\u521b\u5efa\u4e00\u4e2a\u9012\u5f52\u6811\uff0c\u76ee\u6807\u662f\u8ba9\u53f6\u8282\u70b9\u7684\u503c\u5c3d\u53ef\u80fd\u5e73\u5747\u3002\u5f53\u7136\uff0c\u4e0d\u4e00\u5b9a\u8981\u6bcf\u8f6e\u5c06\u6570\u636e\u5212\u5206\u4e3a 3 \u4e2a\u6876\uff0c\u5177\u4f53\u5212\u5206\u65b9\u5f0f\u53ef\u6839\u636e\u6570\u636e\u7279\u70b9\u7075\u6d3b\u9009\u62e9\u3002

\u56fe 11-14 \u00a0 \u9012\u5f52\u5212\u5206\u6876

\u5982\u679c\u6211\u4eec\u63d0\u524d\u77e5\u9053\u5546\u54c1\u4ef7\u683c\u7684\u6982\u7387\u5206\u5e03\uff0c\u5219\u53ef\u4ee5\u6839\u636e\u6570\u636e\u6982\u7387\u5206\u5e03\u8bbe\u7f6e\u6bcf\u4e2a\u6876\u7684\u4ef7\u683c\u5206\u754c\u7ebf\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6570\u636e\u5206\u5e03\u5e76\u4e0d\u4e00\u5b9a\u9700\u8981\u7279\u610f\u7edf\u8ba1\uff0c\u4e5f\u53ef\u4ee5\u6839\u636e\u6570\u636e\u7279\u70b9\u91c7\u7528\u67d0\u79cd\u6982\u7387\u6a21\u578b\u8fdb\u884c\u8fd1\u4f3c\u3002

\u5982\u56fe 11-15 \u6240\u793a\uff0c\u6211\u4eec\u5047\u8bbe\u5546\u54c1\u4ef7\u683c\u670d\u4ece\u6b63\u6001\u5206\u5e03\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5408\u7406\u5730\u8bbe\u5b9a\u4ef7\u683c\u533a\u95f4\uff0c\u4ece\u800c\u5c06\u5546\u54c1\u5e73\u5747\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\u3002

\u56fe 11-15 \u00a0 \u6839\u636e\u6982\u7387\u5206\u5e03\u5212\u5206\u6876

"},{"location":"chapter_sorting/counting_sort/","title":"11.9 \u00a0 \u8ba1\u6570\u6392\u5e8f","text":"

\u300c\u8ba1\u6570\u6392\u5e8f counting sort\u300d\u901a\u8fc7\u7edf\u8ba1\u5143\u7d20\u6570\u91cf\u6765\u5b9e\u73b0\u6392\u5e8f\uff0c\u901a\u5e38\u5e94\u7528\u4e8e\u6574\u6570\u6570\u7ec4\u3002

"},{"location":"chapter_sorting/counting_sort/#1191","title":"11.9.1 \u00a0 \u7b80\u5355\u5b9e\u73b0","text":"

\u5148\u6765\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u3002\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4 nums \uff0c\u5176\u4e2d\u7684\u5143\u7d20\u90fd\u662f\u201c\u975e\u8d1f\u6574\u6570\u201d\uff0c\u8ba1\u6570\u6392\u5e8f\u7684\u6574\u4f53\u6d41\u7a0b\u5982\u56fe 11-16 \u6240\u793a\u3002

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

\u56fe 11-16 \u00a0 \u8ba1\u6570\u6392\u5e8f\u6d41\u7a0b

PythonC++JavaC#GoSwiftJSTSDartRustCZig counting_sort.py
def counting_sort_naive(nums: list[int]):\n\"\"\"\u8ba1\u6570\u6392\u5e8f\"\"\"\n# \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\n# 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\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.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.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.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.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.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.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.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
counting_sort.rs
/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfn counting_sort_naive(nums: &mut [i32]) {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nlet m = *nums.into_iter().max().unwrap();\n// 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n// counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\nlet mut counter = vec![0; m as usize + 1];\nfor &num in &*nums {\ncounter[num as usize] += 1;\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nlet mut i = 0;\nfor num in 0..m + 1 {\nfor _ in 0..counter[num as usize] {\nnums[i] = num;\ni += 1;\n}\n}\n}\n
counting_sort.c
/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid countingSortNaive(int nums[], int size) {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\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.zig
[class]{}-[func]{countingSortNaive}\n

\u8ba1\u6570\u6392\u5e8f\u4e0e\u6876\u6392\u5e8f\u7684\u8054\u7cfb

\u4ece\u6876\u6392\u5e8f\u7684\u89d2\u5ea6\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8ba1\u6570\u6392\u5e8f\u4e2d\u7684\u8ba1\u6570\u6570\u7ec4 counter \u7684\u6bcf\u4e2a\u7d22\u5f15\u89c6\u4e3a\u4e00\u4e2a\u6876\uff0c\u5c06\u7edf\u8ba1\u6570\u91cf\u7684\u8fc7\u7a0b\u770b\u4f5c\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\u5047\u8bbe\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\u201c\u524d\u7f00\u548c\u201d\u3002\u987e\u540d\u601d\u4e49\uff0c\u7d22\u5f15 i \u5904\u7684\u524d\u7f00\u548c prefix[i] \u7b49\u4e8e\u6570\u7ec4\u524d i \u4e2a\u5143\u7d20\u4e4b\u548c\uff1a

\\[ \\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\u4ee5\u4e0b\u4e24\u6b65\u3002

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

\u904d\u5386\u5b8c\u6210\u540e\uff0c\u6570\u7ec4 res \u4e2d\u5c31\u662f\u6392\u5e8f\u597d\u7684\u7ed3\u679c\uff0c\u6700\u540e\u4f7f\u7528 res \u8986\u76d6\u539f\u6570\u7ec4 nums \u5373\u53ef\u3002\u56fe 11-17 \u5c55\u793a\u4e86\u5b8c\u6574\u7684\u8ba1\u6570\u6392\u5e8f\u6d41\u7a0b\u3002

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

\u56fe 11-17 \u00a0 \u8ba1\u6570\u6392\u5e8f\u6b65\u9aa4

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

PythonC++JavaC#GoSwiftJSTSDartRustCZig counting_sort.py
def counting_sort(nums: list[int]):\n\"\"\"\u8ba1\u6570\u6392\u5e8f\"\"\"\n# \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\n# 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\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.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.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.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.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.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.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.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
counting_sort.rs
/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfn counting_sort(nums: &mut [i32]) {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nlet m = *nums.into_iter().max().unwrap();\n// 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n// counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\nlet mut counter = vec![0; m as usize + 1];\nfor &num in &*nums {\ncounter[num as usize] += 1;\n}\n// 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n// \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\nfor i in 0..m as usize {\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\nlet n = nums.len();\nlet mut res = vec![0; n];\nfor i in (0..n).rev() {\nlet num = nums[i];\nres[counter[num as usize] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[num as usize] -= 1; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n}\n// \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\nfor i in 0..n {\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.zig
[class]{}-[func]{countingSort}\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\u201c\u5806\u201c\u7ae0\u8282\u3002

\u300c\u5806\u6392\u5e8f heap sort\u300d\u662f\u4e00\u79cd\u57fa\u4e8e\u5806\u6570\u636e\u7ed3\u6784\u5b9e\u73b0\u7684\u9ad8\u6548\u6392\u5e8f\u7b97\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5df2\u7ecf\u5b66\u8fc7\u7684\u201c\u5efa\u5806\u64cd\u4f5c\u201d\u548c\u201c\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\u201d\u5b9e\u73b0\u5806\u6392\u5e8f\u3002

  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\u56fe 11-12 \u6240\u793a\u3002

  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

Tip

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

\u56fe 11-12 \u00a0 \u5806\u6392\u5e8f\u6b65\u9aa4

\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 sift_down() \u51fd\u6570\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e\u5806\u7684\u957f\u5ea6\u4f1a\u968f\u7740\u63d0\u53d6\u6700\u5927\u5143\u7d20\u800c\u51cf\u5c0f\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u7ed9 sift_down() \u51fd\u6570\u6dfb\u52a0\u4e00\u4e2a\u957f\u5ea6\u53c2\u6570 \\(n\\) \uff0c\u7528\u4e8e\u6307\u5b9a\u5806\u7684\u5f53\u524d\u6709\u6548\u957f\u5ea6\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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\u987b\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.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\u987b\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.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\u987b\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.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\u987b\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.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\u987b\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.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\u987b\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.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\u987b\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\u987b\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.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\u987b\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
heap_sort.rs
/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfn sift_down(nums: &mut [i32], n: usize, mut i: usize) {\nloop {\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 mut 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\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif ma == i {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nlet 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 */\nfn heap_sort(nums: &mut [i32]) {\n// \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor i in (0..=nums.len() / 2 - 1).rev() {\nsift_down(nums, nums.len(), i);\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor i in (1..=nums.len() - 1).rev() {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nlet 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\nsift_down(nums, i, 0);\n}\n}\n
heap_sort.c
/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int nums[], int n, int i) {\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\u987b\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.zig
[class]{}-[func]{siftDown}\n[class]{}-[func]{heapSort}\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

\u56fe 11-6 \u5c55\u793a\u4e86\u6570\u7ec4\u63d2\u5165\u5143\u7d20\u7684\u64cd\u4f5c\u6d41\u7a0b\u3002\u8bbe\u57fa\u51c6\u5143\u7d20\u4e3a base \uff0c\u6211\u4eec\u9700\u8981\u5c06\u4ece\u76ee\u6807\u7d22\u5f15\u5230 base \u4e4b\u95f4\u7684\u6240\u6709\u5143\u7d20\u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\uff0c\u7136\u540e\u518d\u5c06 base \u8d4b\u503c\u7ed9\u76ee\u6807\u7d22\u5f15\u3002

\u56fe 11-6 \u00a0 \u5355\u6b21\u63d2\u5165\u64cd\u4f5c

"},{"location":"chapter_sorting/insertion_sort/#1141","title":"11.4.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"

\u63d2\u5165\u6392\u5e8f\u7684\u6574\u4f53\u6d41\u7a0b\u5982\u56fe 11-7 \u6240\u793a\u3002

  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

\u56fe 11-7 \u00a0 \u63d2\u5165\u6392\u5e8f\u6d41\u7a0b

PythonC++JavaC#GoSwiftJSTSDartRustCZig insertion_sort.py
def insertion_sort(nums: list[int]):\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.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.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.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.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.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.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.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
insertion_sort.rs
/* \u63d2\u5165\u6392\u5e8f */\nfn insertion_sort(nums: &mut [i32]) {\n// \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\nfor i in 1..nums.len() {\nlet (base, mut j) = (nums[i],  (i - 1) as i32);\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile j >= 0 && nums[j as usize] > base {\nnums[(j + 1) as usize] = nums[j as usize]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj -= 1;\n}\nnums[(j + 1) as usize] = base;  // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n}\n}\n
insertion_sort.c
/* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(int nums[], int size) {\n// \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\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.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
"},{"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\\)\u3001\\(n-2\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \u6b21\uff0c\u6c42\u548c\u5f97\u5230 \\((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\\) \u548c \\(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\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\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002

  • \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\u662f\u4e00\u79cd\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5305\u542b\u56fe 11-10 \u6240\u793a\u7684\u201c\u5212\u5206\u201d\u548c\u201c\u5408\u5e76\u201d\u9636\u6bb5\u3002

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

\u56fe 11-10 \u00a0 \u5f52\u5e76\u6392\u5e8f\u7684\u5212\u5206\u4e0e\u5408\u5e76\u9636\u6bb5

"},{"location":"chapter_sorting/merge_sort/#1161","title":"11.6.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"

\u5982\u56fe 11-11 \u6240\u793a\uff0c\u201c\u5212\u5206\u9636\u6bb5\u201d\u4ece\u9876\u81f3\u5e95\u9012\u5f52\u5730\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5207\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\u3002

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

\u201c\u5408\u5e76\u9636\u6bb5\u201d\u4ece\u5e95\u81f3\u9876\u5730\u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u5408\u5e76\u4e3a\u4e00\u4e2a\u6709\u5e8f\u6570\u7ec4\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4ece\u957f\u5ea6\u4e3a 1 \u7684\u5b50\u6570\u7ec4\u5f00\u59cb\u5408\u5e76\uff0c\u5408\u5e76\u9636\u6bb5\u4e2d\u7684\u6bcf\u4e2a\u5b50\u6570\u7ec4\u90fd\u662f\u6709\u5e8f\u7684\u3002

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

\u56fe 11-11 \u00a0 \u5f52\u5e76\u6392\u5e8f\u6b65\u9aa4

\u89c2\u5bdf\u53d1\u73b0\uff0c\u5f52\u5e76\u6392\u5e8f\u4e0e\u4e8c\u53c9\u6811\u540e\u5e8f\u904d\u5386\u7684\u9012\u5f52\u987a\u5e8f\u662f\u4e00\u81f4\u7684\u3002

  • \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
PythonC++JavaC#GoSwiftJSTSDartRustCZig merge_sort.py
def merge(nums: list[int], left: int, mid: int, right: int):\n\"\"\"\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\"\"\"\n# \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid]\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):\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.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.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.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.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.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.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.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
merge_sort.rs
/* \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: &mut [i32], left: usize, mid: usize, right: usize) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nlet tmp: Vec<i32> = nums[left..right + 1].to_vec();\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet (left_start, left_end) = (left - left, mid - left);\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet (right_start, right_end) = (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\nlet (mut l_corrent, mut r_corrent) = (left_start, 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 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 l_corrent > left_end {\nnums[k] = tmp[r_corrent];\nr_corrent += 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 r_corrent > right_end || tmp[l_corrent] <= tmp[r_corrent] {\nnums[k] = tmp[l_corrent];\nl_corrent += 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[r_corrent];\nr_corrent += 1;\n}\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nfn merge_sort(left: usize, right: usize, nums: &mut [i32]) {\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 = (left + right) / 2;     // \u8ba1\u7b97\u4e2d\u70b9\nmerge_sort(left, mid, nums);      // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmerge_sort(mid + 1, right, nums);  // \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.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

\u5b9e\u73b0\u5408\u5e76\u51fd\u6570 merge() \u5b58\u5728\u4ee5\u4e0b\u96be\u70b9\u3002

  • \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":"

\u5bf9\u4e8e\u94fe\u8868\uff0c\u5f52\u5e76\u6392\u5e8f\u76f8\u8f83\u4e8e\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\u5177\u6709\u663e\u8457\u4f18\u52bf\uff0c\u53ef\u4ee5\u5c06\u94fe\u8868\u6392\u5e8f\u4efb\u52a1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(1)\\) \u3002

  • \u5212\u5206\u9636\u6bb5\uff1a\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u201c\u8fed\u4ee3\u201d\u66ff\u4ee3\u201c\u9012\u5f52\u201d\u6765\u5b9e\u73b0\u94fe\u8868\u5212\u5206\u5de5\u4f5c\uff0c\u4ece\u800c\u7701\u53bb\u9012\u5f52\u4f7f\u7528\u7684\u6808\u5e27\u7a7a\u95f4\u3002
  • \u5408\u5e76\u9636\u6bb5\uff1a\u5728\u94fe\u8868\u4e2d\uff0c\u8282\u70b9\u589e\u5220\u64cd\u4f5c\u4ec5\u9700\u6539\u53d8\u5f15\u7528\uff08\u6307\u9488\uff09\u5373\u53ef\u5b9e\u73b0\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\u987b\u521b\u5efa\u989d\u5916\u94fe\u8868\u3002

\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\u7b56\u7565\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u8fd0\u884c\u9ad8\u6548\uff0c\u5e94\u7528\u5e7f\u6cdb\u3002

\u5feb\u901f\u6392\u5e8f\u7684\u6838\u5fc3\u64cd\u4f5c\u662f\u201c\u54e8\u5175\u5212\u5206\u201d\uff0c\u5176\u76ee\u6807\u662f\uff1a\u9009\u62e9\u6570\u7ec4\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\u4f5c\u4e3a\u201c\u57fa\u51c6\u6570\u201d\uff0c\u5c06\u6240\u6709\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\u79fb\u5230\u5176\u5de6\u4fa7\uff0c\u800c\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\u79fb\u5230\u5176\u53f3\u4fa7\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u54e8\u5175\u5212\u5206\u7684\u6d41\u7a0b\u5982\u56fe 11-8 \u6240\u793a\u3002

  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\u3002
  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\u3002
  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\u3002
<1><2><3><4><5><6><7><8><9>

\u56fe 11-8 \u00a0 \u54e8\u5175\u5212\u5206\u6b65\u9aa4

\u54e8\u5175\u5212\u5206\u5b8c\u6210\u540e\uff0c\u539f\u6570\u7ec4\u88ab\u5212\u5206\u6210\u4e09\u90e8\u5206\uff1a\u5de6\u5b50\u6570\u7ec4\u3001\u57fa\u51c6\u6570\u3001\u53f3\u5b50\u6570\u7ec4\uff0c\u4e14\u6ee1\u8db3\u201c\u5de6\u5b50\u6570\u7ec4\u4efb\u610f\u5143\u7d20 \\(\\leq\\) \u57fa\u51c6\u6570 \\(\\leq\\) \u53f3\u5b50\u6570\u7ec4\u4efb\u610f\u5143\u7d20\u201d\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u63a5\u4e0b\u6765\u53ea\u9700\u5bf9\u8fd9\u4e24\u4e2a\u5b50\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002

\u5feb\u901f\u6392\u5e8f\u7684\u5206\u6cbb\u7b56\u7565

\u54e8\u5175\u5212\u5206\u7684\u5b9e\u8d28\u662f\u5c06\u4e00\u4e2a\u8f83\u957f\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\u7b80\u5316\u4e3a\u4e24\u4e2a\u8f83\u77ed\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig quick_sort.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.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.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.cs
/* \u5143\u7d20\u4ea4\u6362 */\nvoid Swap(int[] nums, int i, int j) {\n(nums[j], nums[i]) = (nums[i], nums[j]);\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.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.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.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.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
quick_sort.rs
/* \u54e8\u5175\u5212\u5206 */\nfn partition(nums: &mut [i32], left: usize, right: usize) -> usize {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nlet (mut i, mut j) = (left, 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}\nnums.swap(i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nnums.swap(i, left);  // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\ni                    // \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.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
"},{"location":"chapter_sorting/quick_sort/#1151","title":"11.5.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"

\u5feb\u901f\u6392\u5e8f\u7684\u6574\u4f53\u6d41\u7a0b\u5982\u56fe 11-9 \u6240\u793a\u3002

  1. \u9996\u5148\uff0c\u5bf9\u539f\u6570\u7ec4\u6267\u884c\u4e00\u6b21\u201c\u54e8\u5175\u5212\u5206\u201d\uff0c\u5f97\u5230\u672a\u6392\u5e8f\u7684\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u3002
  2. \u7136\u540e\uff0c\u5bf9\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u5206\u522b\u9012\u5f52\u6267\u884c\u201c\u54e8\u5175\u5212\u5206\u201d\u3002
  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\u3002

\u56fe 11-9 \u00a0 \u5feb\u901f\u6392\u5e8f\u6d41\u7a0b

PythonC++JavaC#GoSwiftJSTSDartRustCZig quick_sort.py
def quick_sort(self, nums: list[int], left: int, right: int):\n\"\"\"\u5feb\u901f\u6392\u5e8f\"\"\"\n# \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\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.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.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.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.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.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.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.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
quick_sort.rs
/* \u5feb\u901f\u6392\u5e8f */\npub fn quick_sort(left: i32, right: i32, nums: &mut [i32]) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nif left >= right {\nreturn;\n}\n// \u54e8\u5175\u5212\u5206\nlet pivot = Self::partition(nums, left as usize, right as usize) as i32;\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nSelf::quick_sort(left, pivot - 1, nums);\nSelf::quick_sort(pivot + 1, right, nums);\n}\n
quick_sort.c
/* \u5feb\u901f\u6392\u5e8f\u7c7b */\n// \u5feb\u901f\u6392\u5e8f\u7c7b-\u54e8\u5175\u5212\u5206\nint partition(int nums[], int left, int right) {\n// \u4ee5 nums[left] \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.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
"},{"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\u201c\u5f52\u5e76\u6392\u5e8f\u201d\u548c\u201c\u5806\u6392\u5e8f\u201d\u76f8\u540c\uff0c\u4f46\u901a\u5e38\u5feb\u901f\u6392\u5e8f\u7684\u6548\u7387\u66f4\u9ad8\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002

  • \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\u201c\u5806\u6392\u5e8f\u201d\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\u201c\u63d2\u5165\u6392\u5e8f\u201d\u6bd4\u201c\u5192\u6ce1\u6392\u5e8f\u201d\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\u201c\u5192\u6ce1\u6392\u5e8f\u201d\u3002

\u4e3a\u4e86\u5c3d\u91cf\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f18\u5316\u54e8\u5175\u5212\u5206\u4e2d\u7684\u57fa\u51c6\u6570\u7684\u9009\u53d6\u7b56\u7565\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u968f\u673a\u9009\u53d6\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6\u6570\u3002\u7136\u800c\uff0c\u5982\u679c\u8fd0\u6c14\u4e0d\u4f73\uff0c\u6bcf\u6b21\u90fd\u9009\u5230\u4e0d\u7406\u60f3\u7684\u57fa\u51c6\u6570\uff0c\u6548\u7387\u4ecd\u7136\u4e0d\u5c3d\u5982\u4eba\u610f\u3002

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u751f\u6210\u7684\u662f\u201c\u4f2a\u968f\u673a\u6570\u201d\u3002\u5982\u679c\u6211\u4eec\u9488\u5bf9\u4f2a\u968f\u673a\u6570\u5e8f\u5217\u6784\u5efa\u4e00\u4e2a\u7279\u5b9a\u7684\u6d4b\u8bd5\u6837\u4f8b\uff0c\u90a3\u4e48\u5feb\u901f\u6392\u5e8f\u7684\u6548\u7387\u4ecd\u7136\u53ef\u80fd\u52a3\u5316\u3002

\u4e3a\u4e86\u8fdb\u4e00\u6b65\u6539\u8fdb\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6570\u7ec4\u4e2d\u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\uff08\u901a\u5e38\u4e3a\u6570\u7ec4\u7684\u9996\u3001\u5c3e\u3001\u4e2d\u70b9\u5143\u7d20\uff09\uff0c\u5e76\u5c06\u8fd9\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\u4f5c\u4e3a\u57fa\u51c6\u6570\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u57fa\u51c6\u6570\u201c\u65e2\u4e0d\u592a\u5c0f\u4e5f\u4e0d\u592a\u5927\u201d\u7684\u6982\u7387\u5c06\u5927\u5e45\u63d0\u5347\u3002\u5f53\u7136\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u9009\u53d6\u66f4\u591a\u5019\u9009\u5143\u7d20\uff0c\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u7b97\u6cd5\u7684\u7a33\u5065\u6027\u3002\u91c7\u7528\u8fd9\u79cd\u65b9\u6cd5\u540e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u52a3\u5316\u81f3 \\(O(n^2)\\) \u7684\u6982\u7387\u5927\u5927\u964d\u4f4e\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
quick_sort.rs
/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nfn median_three(nums: &mut [i32], left: usize, mid: usize, right: usize) -> usize {\n// \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n// \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\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} right\n}\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nfn partition(nums: &mut [i32], left: usize, right: usize) -> usize {\n// \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nlet med = Self::median_three(nums, left, (left + right) / 2, right);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nnums.swap(left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nlet (mut i, mut j) = (left, 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}\nnums.swap(i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nnums.swap(i, left);  // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\ni                    // \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.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
"},{"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 \\(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

PythonC++JavaC#GoSwiftJSTSDartRustCZig quick_sort.py
def quick_sort(self, nums: list[int], left: int, right: int):\n\"\"\"\u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09\"\"\"\n# \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\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.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.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.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.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.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.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.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
quick_sort.rs
/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\npub fn quick_sort(mut left: i32, mut right: i32, nums: &mut [i32]) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\nwhile left < right {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nlet pivot = Self::partition(nums, left as usize, right as usize) as i32;\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif  pivot - left < right - pivot {\nSelf::quick_sort(left, pivot - 1, nums);  // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1;  // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nSelf::quick_sort(pivot + 1, right, nums); // \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.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
"},{"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\u6d41\u7a0b\u5982\u56fe 11-18 \u6240\u793a\u3002

  1. \u521d\u59cb\u5316\u4f4d\u6570 \\(k = 1\\) \u3002
  2. \u5bf9\u5b66\u53f7\u7684\u7b2c \\(k\\) \u4f4d\u6267\u884c\u201c\u8ba1\u6570\u6392\u5e8f\u201d\u3002\u5b8c\u6210\u540e\uff0c\u6570\u636e\u4f1a\u6839\u636e\u7b2c \\(k\\) \u4f4d\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\u3002
  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\u3002

\u56fe 11-18 \u00a0 \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 \\: 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

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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):\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]):\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.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.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.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.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.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.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.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;\nfor (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
radix_sort.rs
/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfn digit(num: i32, exp: i32) -> usize {\n// \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\nreturn ((num / exp) % 10) as usize;\n}\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfn counting_sort_digit(nums: &mut [i32], exp: i32) {\n// \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\nlet mut counter = [0; 10];\nlet n = nums.len();\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor i in 0..n {\nlet d = 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}\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\nlet mut res = vec![0; n];\nfor i in (0..n).rev() {\nlet d = digit(nums[i], 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 0..n {\nnums[i] = res[i];\n}\n}\n/* \u57fa\u6570\u6392\u5e8f */\nfn radix_sort(nums: &mut [i32]) {\n// \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\nlet m = *nums.into_iter().max().unwrap();\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nlet mut exp = 1;\nwhile exp <= m {\ncounting_sort_digit(nums, exp);\nexp *= 10;\n}\n}\n
radix_sort.c
/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int num, int exp) {\n// \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\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.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);\n@memset(counter, 0);\nvar n = nums.len;\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (nums) |num| {\nvar d: u32 = @bitCast(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: u32 = @bitCast(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} 

\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\u56fe 11-2 \u6240\u793a\u3002

  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\u987b\u6392\u5e8f\uff0c\u56e0\u6b64\u6570\u7ec4\u6392\u5e8f\u5b8c\u6210\u3002
<1><2><3><4><5><6><7><8><9><10><11>

\u56fe 11-2 \u00a0 \u9009\u62e9\u6392\u5e8f\u6b65\u9aa4

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

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
selection_sort.rs
/* \u9009\u62e9\u6392\u5e8f */\nfn selection_sort(nums: &mut [i32]) {\nlet n = nums.len();\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor i in 0..n-1 {\n// \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\nlet mut k = i;\nfor j in i+1..n {\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.swap(i, k);\n}\n}\n
selection_sort.c
/* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(int nums[], int n) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\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.zig
[class]{}-[func]{selectionSort}\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\\)\u3001\\(n - 1\\)\u3001\\(\\dots\\)\u3001\\(3\\)\u3001\\(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\\) \u548c \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002
  • \u975e\u7a33\u5b9a\u6392\u5e8f\uff1a\u5982\u56fe 11-3 \u6240\u793a\uff0c\u5143\u7d20 nums[i] \u6709\u53ef\u80fd\u88ab\u4ea4\u6362\u81f3\u4e0e\u5176\u76f8\u7b49\u7684\u5143\u7d20\u7684\u53f3\u8fb9\uff0c\u5bfc\u81f4\u4e24\u8005\u76f8\u5bf9\u987a\u5e8f\u53d1\u751f\u6539\u53d8\u3002

\u56fe 11-3 \u00a0 \u9009\u62e9\u6392\u5e8f\u975e\u7a33\u5b9a\u793a\u4f8b

"},{"location":"chapter_sorting/sorting_algorithm/","title":"11.1 \u00a0 \u6392\u5e8f\u7b97\u6cd5","text":"

\u300c\u6392\u5e8f\u7b97\u6cd5 sorting algorithm\u300d\u7528\u4e8e\u5bf9\u4e00\u7ec4\u6570\u636e\u6309\u7167\u7279\u5b9a\u987a\u5e8f\u8fdb\u884c\u6392\u5217\u3002\u6392\u5e8f\u7b97\u6cd5\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u56e0\u4e3a\u6709\u5e8f\u6570\u636e\u901a\u5e38\u80fd\u591f\u88ab\u66f4\u6709\u6548\u5730\u67e5\u627e\u3001\u5206\u6790\u548c\u5904\u7406\u3002

\u5982\u56fe 11-1 \u6240\u793a\uff0c\u6392\u5e8f\u7b97\u6cd5\u4e2d\u7684\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u662f\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\u7b49\u3002\u6392\u5e8f\u7684\u5224\u65ad\u89c4\u5219\u53ef\u6839\u636e\u9700\u6c42\u8bbe\u5b9a\uff0c\u5982\u6570\u5b57\u5927\u5c0f\u3001\u5b57\u7b26 ASCII \u7801\u987a\u5e8f\u6216\u81ea\u5b9a\u4e49\u89c4\u5219\u3002

\u56fe 11-1 \u00a0 \u6570\u636e\u7c7b\u578b\u548c\u5224\u65ad\u89c4\u5219\u793a\u4f8b

"},{"location":"chapter_sorting/sorting_algorithm/#1111","title":"11.1.1 \u00a0 \u8bc4\u4ef7\u7ef4\u5ea6","text":"

\u8fd0\u884c\u6548\u7387\uff1a\u6211\u4eec\u671f\u671b\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5c3d\u91cf\u4f4e\uff0c\u4e14\u603b\u4f53\u64cd\u4f5c\u6570\u91cf\u8f83\u5c11\uff08\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\u987b\u501f\u52a9\u989d\u5916\u7684\u8f85\u52a9\u6570\u7ec4\uff0c\u4ece\u800c\u8282\u7701\u5185\u5b58\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u539f\u5730\u6392\u5e8f\u7684\u6570\u636e\u642c\u8fd0\u64cd\u4f5c\u8f83\u5c11\uff0c\u8fd0\u884c\u901f\u5ea6\u4e5f\u66f4\u5feb\u3002

\u7a33\u5b9a\u6027\uff1a\u300c\u7a33\u5b9a\u6392\u5e8f\u300d\u5728\u5b8c\u6210\u6392\u5e8f\u540e\uff0c\u76f8\u7b49\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u76f8\u5bf9\u987a\u5e8f\u4e0d\u53d1\u751f\u6539\u53d8\u3002

\u7a33\u5b9a\u6392\u5e8f\u662f\u591a\u7ea7\u6392\u5e8f\u573a\u666f\u7684\u5fc5\u8981\u6761\u4ef6\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5b58\u50a8\u5b66\u751f\u4fe1\u606f\u7684\u8868\u683c\uff0c\u7b2c 1 \u5217\u548c\u7b2c 2 \u5217\u5206\u522b\u662f\u59d3\u540d\u548c\u5e74\u9f84\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u300c\u975e\u7a33\u5b9a\u6392\u5e8f\u300d\u53ef\u80fd\u5bfc\u81f4\u8f93\u5165\u6570\u636e\u7684\u6709\u5e8f\u6027\u4e27\u5931\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\\(<\\)\u3001\\(=\\)\u3001\\(>\\)\uff09\u6765\u5224\u65ad\u5143\u7d20\u7684\u76f8\u5bf9\u987a\u5e8f\uff0c\u4ece\u800c\u6392\u5e8f\u6574\u4e2a\u6570\u7ec4\uff0c\u7406\u8bba\u6700\u4f18\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002\u800c\u300c\u975e\u6bd4\u8f83\u6392\u5e8f\u300d\u4e0d\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe \\(O(n)\\) \uff0c\u4f46\u5176\u901a\u7528\u6027\u76f8\u5bf9\u8f83\u5dee\u3002

"},{"location":"chapter_sorting/sorting_algorithm/#1112","title":"11.1.2 \u00a0 \u7406\u60f3\u6392\u5e8f\u7b97\u6cd5","text":"

\u8fd0\u884c\u5feb\u3001\u539f\u5730\u3001\u7a33\u5b9a\u3001\u6b63\u5411\u81ea\u9002\u5e94\u3001\u901a\u7528\u6027\u597d\u3002\u663e\u7136\uff0c\u8fc4\u4eca\u4e3a\u6b62\u5c1a\u672a\u53d1\u73b0\u517c\u5177\u4ee5\u4e0a\u6240\u6709\u7279\u6027\u7684\u6392\u5e8f\u7b97\u6cd5\u3002\u56e0\u6b64\uff0c\u5728\u9009\u62e9\u6392\u5e8f\u7b97\u6cd5\u65f6\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u6570\u636e\u7279\u70b9\u548c\u95ee\u9898\u9700\u6c42\u6765\u51b3\u5b9a\u3002

\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u5171\u540c\u5b66\u4e60\u5404\u79cd\u6392\u5e8f\u7b97\u6cd5\uff0c\u5e76\u57fa\u4e8e\u4e0a\u8ff0\u8bc4\u4ef7\u7ef4\u5ea6\u5bf9\u5404\u4e2a\u6392\u5e8f\u7b97\u6cd5\u7684\u4f18\u7f3a\u70b9\u8fdb\u884c\u5206\u6790\u3002

"},{"location":"chapter_sorting/summary/","title":"11.11 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_sorting/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"
  • \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
  • \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
  • \u56fe 11-19 \u5bf9\u6bd4\u4e86\u4e3b\u6d41\u6392\u5e8f\u7b97\u6cd5\u7684\u6548\u7387\u3001\u7a33\u5b9a\u6027\u3001\u5c31\u5730\u6027\u548c\u81ea\u9002\u5e94\u6027\u7b49\u3002

\u56fe 11-19 \u00a0 \u6392\u5e8f\u7b97\u6cd5\u5bf9\u6bd4

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

\u6392\u5e8f\u7b97\u6cd5\u7a33\u5b9a\u6027\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u662f\u5fc5\u987b\u7684\uff1f

\u5728\u73b0\u5b9e\u4e2d\uff0c\u6211\u4eec\u6709\u53ef\u80fd\u662f\u5728\u5bf9\u8c61\u7684\u67d0\u4e2a\u5c5e\u6027\u4e0a\u8fdb\u884c\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u5b66\u751f\u6709\u59d3\u540d\u548c\u8eab\u9ad8\u4e24\u4e2a\u5c5e\u6027\uff0c\u6211\u4eec\u5e0c\u671b\u5b9e\u73b0\u4e00\u4e2a\u591a\u7ea7\u6392\u5e8f/

\u5148\u6309\u7167\u59d3\u540d\u8fdb\u884c\u6392\u5e8f\uff0c\u5f97\u5230 (A, 180) (B, 185) (C, 170) (D, 170) \uff1b\u63a5\u4e0b\u6765\u5bf9\u8eab\u9ad8\u8fdb\u884c\u6392\u5e8f\u3002\u7531\u4e8e\u6392\u5e8f\u7b97\u6cd5\u4e0d\u7a33\u5b9a\uff0c\u6211\u4eec\u53ef\u80fd\u5f97\u5230 (D, 170) (C, 170) (A, 180) (B, 185) \u3002

\u53ef\u4ee5\u53d1\u73b0\uff0c\u5b66\u751f D \u548c C \u7684\u4f4d\u7f6e\u53d1\u751f\u4e86\u4ea4\u6362\uff0c\u59d3\u540d\u7684\u6709\u5e8f\u6027\u88ab\u7834\u574f\u4e86\uff0c\u800c\u8fd9\u662f\u6211\u4eec\u4e0d\u5e0c\u671b\u770b\u5230\u7684\u3002

\u54e8\u5175\u5212\u5206\u4e2d\u201c\u4ece\u53f3\u5f80\u5de6\u67e5\u627e\u201d\u4e0e\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\u7684\u987a\u5e8f\u53ef\u4ee5\u4ea4\u6362\u5417\uff1f

\u4e0d\u884c\uff0c\u5f53\u6211\u4eec\u4ee5\u6700\u5de6\u7aef\u5143\u7d20\u4e3a\u57fa\u51c6\u6570\u65f6\uff0c\u5fc5\u987b\u5148\u201c\u4ece\u53f3\u5f80\u5de6\u67e5\u627e\u201d\u518d\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\u3002\u8fd9\u4e2a\u7ed3\u8bba\u6709\u4e9b\u53cd\u76f4\u89c9\uff0c\u6211\u4eec\u6765\u5256\u6790\u4e00\u4e0b\u539f\u56e0\u3002

\u54e8\u5175\u5212\u5206 partition() \u7684\u6700\u540e\u4e00\u6b65\u662f\u4ea4\u6362 nums[left] \u548c nums[i] \u3002\u5b8c\u6210\u4ea4\u6362\u540e\uff0c\u57fa\u51c6\u6570\u5de6\u8fb9\u7684\u5143\u7d20\u90fd <= \u57fa\u51c6\u6570\uff0c\u8fd9\u5c31\u8981\u6c42\u6700\u540e\u4e00\u6b65\u4ea4\u6362\u524d nums[left] >= nums[i] \u5fc5\u987b\u6210\u7acb\u3002\u5047\u8bbe\u6211\u4eec\u5148\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\uff0c\u90a3\u4e48\u5982\u679c\u627e\u4e0d\u5230\u6bd4\u57fa\u51c6\u6570\u66f4\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

\u5173\u4e8e\u5c3e\u9012\u5f52\u4f18\u5316\uff0c\u4e3a\u4ec0\u4e48\u9009\u77ed\u7684\u6570\u7ec4\u80fd\u4fdd\u8bc1\u9012\u5f52\u6df1\u5ea6\u4e0d\u8d85\u8fc7 \\(\\log n\\) \uff1f

\u9012\u5f52\u6df1\u5ea6\u5c31\u662f\u5f53\u524d\u672a\u8fd4\u56de\u7684\u9012\u5f52\u65b9\u6cd5\u7684\u6570\u91cf\u3002\u6bcf\u8f6e\u54e8\u5175\u5212\u5206\u6211\u4eec\u5c06\u539f\u6570\u7ec4\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\u3002\u5728\u5c3e\u9012\u5f52\u4f18\u5316\u540e\uff0c\u5411\u4e0b\u9012\u5f52\u7684\u5b50\u6570\u7ec4\u957f\u5ea6\u6700\u5927\u4e3a\u539f\u6570\u7ec4\u7684\u4e00\u534a\u957f\u5ea6\u3002\u5047\u8bbe\u6700\u5dee\u60c5\u51b5\uff0c\u4e00\u76f4\u4e3a\u4e00\u534a\u957f\u5ea6\uff0c\u90a3\u4e48\u6700\u7ec8\u7684\u9012\u5f52\u6df1\u5ea6\u5c31\u662f \\(\\log n\\) \u3002

\u56de\u987e\u539f\u59cb\u7684\u5feb\u901f\u6392\u5e8f\uff0c\u6211\u4eec\u6709\u53ef\u80fd\u4f1a\u8fde\u7eed\u5730\u9012\u5f52\u957f\u5ea6\u8f83\u5927\u7684\u6570\u7ec4\uff0c\u6700\u5dee\u60c5\u51b5\u4e0b\u4e3a \\(n\\)\u3001\\(n - 1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \uff0c\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \u3002\u5c3e\u9012\u5f52\u4f18\u5316\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u7684\u51fa\u73b0\u3002

\u5f53\u6570\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u90fd\u76f8\u7b49\u65f6\uff0c\u5feb\u901f\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(n^2)\\) \u5417\uff1f\u8be5\u5982\u4f55\u5904\u7406\u8fd9\u79cd\u9000\u5316\u60c5\u51b5\uff1f

\u662f\u7684\u3002\u8fd9\u79cd\u60c5\u51b5\u53ef\u4ee5\u8003\u8651\u901a\u8fc7\u54e8\u5175\u5212\u5206\u5c06\u6570\u7ec4\u5212\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\uff1a\u5c0f\u4e8e\u3001\u7b49\u4e8e\u3001\u5927\u4e8e\u57fa\u51c6\u6570\u3002\u4ec5\u5411\u4e0b\u9012\u5f52\u5c0f\u4e8e\u548c\u5927\u4e8e\u7684\u4e24\u90e8\u5206\u3002\u5728\u8be5\u65b9\u6cd5\u4e0b\uff0c\u8f93\u5165\u5143\u7d20\u5168\u90e8\u76f8\u7b49\u7684\u6570\u7ec4\uff0c\u4ec5\u4e00\u8f6e\u54e8\u5175\u5212\u5206\u5373\u53ef\u5b8c\u6210\u6392\u5e8f\u3002

\u6876\u6392\u5e8f\u7684\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\u4ec0\u4e48\u662f \\(O(n^2)\\) \uff1f

\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u5143\u7d20\u88ab\u5206\u81f3\u540c\u4e00\u4e2a\u6876\u4e2d\u3002\u5982\u679c\u6211\u4eec\u91c7\u7528\u4e00\u4e2a \\(O(n^2)\\) \u7b97\u6cd5\u6765\u6392\u5e8f\u8fd9\u4e9b\u5143\u7d20\uff0c\u5219\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002

"},{"location":"chapter_stack_and_queue/","title":"\u7b2c 5 \u7ae0 \u00a0 \u6808\u4e0e\u961f\u5217","text":"

Abstract

\u6808\u5982\u540c\u53e0\u732b\u732b\uff0c\u800c\u961f\u5217\u5c31\u50cf\u732b\u732b\u6392\u961f\u3002

\u4e24\u8005\u5206\u522b\u4ee3\u8868\u7740\u5148\u5165\u540e\u51fa\u548c\u5148\u5165\u5148\u51fa\u7684\u903b\u8f91\u5173\u7cfb\u3002

"},{"location":"chapter_stack_and_queue/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 5.1 \u00a0 \u6808
  • 5.2 \u00a0 \u961f\u5217
  • 5.3 \u00a0 \u53cc\u5411\u961f\u5217
  • 5.4 \u00a0 \u5c0f\u7ed3
"},{"location":"chapter_stack_and_queue/deque/","title":"5.3 \u00a0 \u53cc\u5411\u961f\u5217","text":"

\u5728\u961f\u5217\u4e2d\uff0c\u6211\u4eec\u4ec5\u80fd\u5728\u5934\u90e8\u5220\u9664\u6216\u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\u3002\u5982\u56fe 5-7 \u6240\u793a\uff0c\u300c\u53cc\u5411\u961f\u5217 double-ended queue\u300d\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\uff0c\u5141\u8bb8\u5728\u5934\u90e8\u548c\u5c3e\u90e8\u6267\u884c\u5143\u7d20\u7684\u6dfb\u52a0\u6216\u5220\u9664\u64cd\u4f5c\u3002

\u56fe 5-7 \u00a0 \u53cc\u5411\u961f\u5217\u7684\u64cd\u4f5c

"},{"location":"chapter_stack_and_queue/deque/#531","title":"5.3.1 \u00a0 \u53cc\u5411\u961f\u5217\u5e38\u7528\u64cd\u4f5c","text":"

\u53cc\u5411\u961f\u5217\u7684\u5e38\u7528\u64cd\u4f5c\u5982\u8868 5-3 \u6240\u793a\uff0c\u5177\u4f53\u7684\u65b9\u6cd5\u540d\u79f0\u9700\u8981\u6839\u636e\u6240\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\u6765\u786e\u5b9a\u3002

\u8868 5-3 \u00a0 \u53cc\u5411\u961f\u5217\u64cd\u4f5c\u6548\u7387

\u65b9\u6cd5\u540d \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 pushFirst() \u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u961f\u9996 \\(O(1)\\) pushLast() \u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u961f\u5c3e \\(O(1)\\) popFirst() \u5220\u9664\u961f\u9996\u5143\u7d20 \\(O(1)\\) popLast() \u5220\u9664\u961f\u5c3e\u5143\u7d20 \\(O(1)\\) peekFirst() \u8bbf\u95ee\u961f\u9996\u5143\u7d20 \\(O(1)\\) peekLast() \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 \\(O(1)\\)

\u540c\u6837\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u4e2d\u5df2\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\u7c7b\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.cs
/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 C# \u4e2d\uff0c\u5c06\u94fe\u8868 LinkedList \u770b\u4f5c\u53cc\u5411\u961f\u5217\u6765\u4f7f\u7528\nLinkedList<int> deque = new();\n/* \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_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.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.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.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
deque.rs
/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\nlet mut deque: VecDeque<u32> = VecDeque::new();\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 */\nif let Some(front) = deque.front() { // \u961f\u9996\u5143\u7d20\n}\nif let Some(rear) = deque.back() {   // \u961f\u5c3e\u5143\u7d20\n}\n/* \u5143\u7d20\u51fa\u961f */\nif let Some(pop_front) = deque.pop_front() { // \u961f\u9996\u5143\u7d20\u51fa\u961f\n}\nif let Some(pop_rear) = deque.pop_back() {   // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = deque.len();\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = deque.is_empty();\n
deque.c
// C \u672a\u63d0\u4f9b\u5185\u7f6e\u53cc\u5411\u961f\u5217\n
deque.zig
\n
"},{"location":"chapter_stack_and_queue/deque/#532","title":"5.3.2 \u00a0 \u53cc\u5411\u961f\u5217\u5b9e\u73b0 *","text":"

\u53cc\u5411\u961f\u5217\u7684\u5b9e\u73b0\u4e0e\u961f\u5217\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u9009\u62e9\u94fe\u8868\u6216\u6570\u7ec4\u4f5c\u4e3a\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002

"},{"location":"chapter_stack_and_queue/deque/#1","title":"1. \u00a0 \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u7684\u5b9e\u73b0","text":"

\u56de\u987e\u4e0a\u4e00\u8282\u5185\u5bb9\uff0c\u6211\u4eec\u4f7f\u7528\u666e\u901a\u5355\u5411\u94fe\u8868\u6765\u5b9e\u73b0\u961f\u5217\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u5220\u9664\u5934\u8282\u70b9\uff08\u5bf9\u5e94\u51fa\u961f\u64cd\u4f5c\uff09\u548c\u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0\u65b0\u8282\u70b9\uff08\u5bf9\u5e94\u5165\u961f\u64cd\u4f5c\uff09\u3002

\u5bf9\u4e8e\u53cc\u5411\u961f\u5217\u800c\u8a00\uff0c\u5934\u90e8\u548c\u5c3e\u90e8\u90fd\u53ef\u4ee5\u6267\u884c\u5165\u961f\u548c\u51fa\u961f\u64cd\u4f5c\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u53cc\u5411\u961f\u5217\u9700\u8981\u5b9e\u73b0\u53e6\u4e00\u4e2a\u5bf9\u79f0\u65b9\u5411\u7684\u64cd\u4f5c\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u91c7\u7528\u201c\u53cc\u5411\u94fe\u8868\u201d\u4f5c\u4e3a\u53cc\u5411\u961f\u5217\u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002

\u5982\u56fe 5-8 \u6240\u793a\uff0c\u6211\u4eec\u5c06\u53cc\u5411\u94fe\u8868\u7684\u5934\u8282\u70b9\u548c\u5c3e\u8282\u70b9\u89c6\u4e3a\u53cc\u5411\u961f\u5217\u7684\u961f\u9996\u548c\u961f\u5c3e\uff0c\u540c\u65f6\u5b9e\u73b0\u5728\u4e24\u7aef\u6dfb\u52a0\u548c\u5220\u9664\u8282\u70b9\u7684\u529f\u80fd\u3002

LinkedListDequepushLast()pushFirst()popLast()popFirst()

\u56fe 5-8 \u00a0 \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u53cc\u5411\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c

\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig linkedlist_deque.py
class ListNode:\n\"\"\"\u53cc\u5411\u94fe\u8868\u8282\u70b9\"\"\"\ndef __init__(self, val: int):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.val: int = val\nself.next: ListNode | None = None  # \u540e\u7ee7\u8282\u70b9\u5f15\u7528\nself.prev: ListNode | None = None  # \u524d\u9a71\u8282\u70b9\u5f15\u7528\nclass LinkedListDeque:\n\"\"\"\u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\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  # \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):\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):\n\"\"\"\u961f\u9996\u5165\u961f\"\"\"\nself.push(num, True)\ndef push_last(self, num: int):\n\"\"\"\u961f\u5c3e\u5165\u961f\"\"\"\nself.push(num, False)\ndef pop(self, is_front: bool) -> int:\n\"\"\"\u51fa\u961f\u64cd\u4f5c\"\"\"\nif self.is_empty():\nraise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\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\"\"\"\nif self.is_empty():\nraise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\nreturn self._front.val\ndef peek_last(self) -> int:\n\"\"\"\u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\"\"\"\nif self.is_empty():\nraise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\nreturn 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.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) {\nif (isEmpty())\nthrow out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\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() {\nif (isEmpty())\nthrow out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\nreturn front->val;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast() {\nif (isEmpty())\nthrow out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\nreturn 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.java
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\nint val; // \u8282\u70b9\u503c\nListNode next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\nListNode prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\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 int pop(boolean isFront) {\nif (isEmpty())\nthrow new IndexOutOfBoundsException();\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 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() {\nif (isEmpty())\nthrow new IndexOutOfBoundsException();\nreturn front.val;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npublic int peekLast() {\nif (isEmpty())\nthrow new IndexOutOfBoundsException();\nreturn 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.cs
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\npublic int val;       // \u8282\u70b9\u503c\npublic ListNode? next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\npublic ListNode? prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\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(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) {\nif (IsEmpty())\nthrow new Exception();\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() {\nif (IsEmpty())\nthrow new Exception();\nreturn front?.val;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npublic int? PeekLast() {\nif (IsEmpty())\nthrow new Exception();\nreturn 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.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.swift
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\nvar val: Int // \u8282\u70b9\u503c\nvar next: ListNode? // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\nweak var prev: ListNode? // \u524d\u9a71\u8282\u70b9\u5f15\u7528\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.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.dart
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\nint val; // \u8282\u70b9\u503c\nListNode? next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\nListNode? prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\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 \u548c _rear \u90fd\u6307\u5411 node\n_front = _rear = node;\n} else if (isFront) {\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n_front!.prev = node;\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
linkedlist_deque.rs
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\npub struct ListNode<T> {\npub val: T,                                 // \u8282\u70b9\u503c\npub next: Option<Rc<RefCell<ListNode<T>>>>, // \u540e\u7ee7\u8282\u70b9\u6307\u9488\npub prev: Option<Rc<RefCell<ListNode<T>>>>, // \u524d\u9a71\u8282\u70b9\u6307\u9488\n}\nimpl<T> ListNode<T> {\npub fn new(val: T) -> Rc<RefCell<ListNode<T>>> {\nRc::new(RefCell::new(ListNode {\nval,\nnext: None,\nprev: None,\n}))\n}\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\n#[allow(dead_code)]\npub struct LinkedListDeque<T> {\nfront: Option<Rc<RefCell<ListNode<T>>>>,    // \u5934\u8282\u70b9 front\nrear: Option<Rc<RefCell<ListNode<T>>>>,     // \u5c3e\u8282\u70b9 rear \nque_size: usize,                            // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n}\nimpl<T: Copy> LinkedListDeque<T> {\npub fn new() -> Self {\nSelf {\nfront: None,\nrear: None,\nque_size: 0, }\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\npub fn size(&self) -> usize {\nreturn self.que_size;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npub fn is_empty(&self) -> bool {\nreturn self.size() == 0;\n}\n/* \u5165\u961f\u64cd\u4f5c */\npub fn push(&mut self, num: T, is_front: bool) {\nlet node = ListNode::new(num);\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\nif is_front {\nmatch self.front.take() {\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nNone => {\nself.rear = Some(node.clone());\nself.front = Some(node);\n}\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nSome(old_front) => {\nold_front.borrow_mut().prev = Some(node.clone());\nnode.borrow_mut().next = Some(old_front);\nself.front = Some(node); // \u66f4\u65b0\u5934\u8282\u70b9\n}\n}\n} // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\nelse {\nmatch self.rear.take() {\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nNone => {\nself.front = Some(node.clone());\nself.rear = Some(node);\n}\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\nSome(old_rear) => {\nold_rear.borrow_mut().next = Some(node.clone());\nnode.borrow_mut().prev = Some(old_rear);\nself.rear = Some(node); // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\n}\n}\nself.que_size += 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n/* \u961f\u9996\u5165\u961f */\npub fn push_first(&mut self, num: T) {\nself.push(num, true);\n}\n/* \u961f\u5c3e\u5165\u961f */\npub fn push_last(&mut self, num: T) {\nself.push(num, false);\n}\n/* \u51fa\u961f\u64cd\u4f5c */\npub fn pop(&mut self, is_front: bool) -> Option<T> {\n// \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de None\nif self.is_empty() { return None };\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif is_front {\nself.front.take().map(|old_front| {\nmatch old_front.borrow_mut().next.take() {\nSome(new_front) => {\nnew_front.borrow_mut().prev.take();\nself.front = Some(new_front);   // \u66f4\u65b0\u5934\u8282\u70b9\n}\nNone => {\nself.rear.take();\n}\n}\nself.que_size -= 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nRc::try_unwrap(old_front).ok().unwrap().into_inner().val\n})\n} // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\nelse {\nself.rear.take().map(|old_rear| {\nmatch old_rear.borrow_mut().prev.take() {\nSome(new_rear) => {\nnew_rear.borrow_mut().next.take();\nself.rear = Some(new_rear);     // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nNone => {\nself.front.take();\n}\n}\nself.que_size -= 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nRc::try_unwrap(old_rear).ok().unwrap().into_inner().val\n})\n}\n}\n/* \u961f\u9996\u51fa\u961f */\npub fn pop_first(&mut self) -> Option<T> {\nreturn self.pop(true);\n}\n/* \u961f\u5c3e\u51fa\u961f */\npub fn pop_last(&mut self) -> Option<T> {\nreturn self.pop(false);\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npub fn peek_first(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\nself.front.as_ref()\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npub fn peek_last(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\nself.rear.as_ref()\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\npub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\nif let Some(node) = head {\nlet mut nums = self.to_array(node.borrow().next.as_ref());\nnums.insert(0, node.borrow().val);\nreturn nums;\n}\nreturn Vec::new();\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// \u961f\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.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\u6307\u9488\nprev: ?*Self = null,    // \u524d\u9a71\u8282\u70b9\u6307\u9488\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\u51fd\u6570\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\u51fd\u6570\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());\n@memset(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
"},{"location":"chapter_stack_and_queue/deque/#2","title":"2. \u00a0 \u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"

\u5982\u56fe 5-9 \u6240\u793a\uff0c\u4e0e\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u961f\u5217\u7c7b\u4f3c\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u73af\u5f62\u6570\u7ec4\u6765\u5b9e\u73b0\u53cc\u5411\u961f\u5217\u3002

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

\u56fe 5-9 \u00a0 \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u53cc\u5411\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c

\u5728\u961f\u5217\u7684\u5b9e\u73b0\u57fa\u7840\u4e0a\uff0c\u4ec5\u9700\u589e\u52a0\u201c\u961f\u9996\u5165\u961f\u201d\u548c\u201c\u961f\u5c3e\u51fa\u961f\u201d\u7684\u65b9\u6cd5\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig array_deque.py
class ArrayDeque:\n\"\"\"\u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\"\"\"\ndef __init__(self, capacity: int):\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):\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):\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.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.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.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.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.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.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.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
array_deque.rs
/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nstruct ArrayDeque {\nnums: Vec<i32>,     // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nfront: usize,       // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nque_size: usize,    // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n}\nimpl ArrayDeque {\n/* \u6784\u9020\u65b9\u6cd5 */\npub fn new(capacity: usize) -> Self {\nSelf {\nnums: vec![0; capacity],\nfront: 0,\nque_size: 0,\n}\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\npub fn capacity(&self) -> usize {\nself.nums.len()\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\npub fn size(&self) -> usize {\nself.que_size\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npub fn is_empty(&self) -> bool {\nself.que_size == 0\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nfn index(&self, i: i32) -> usize {\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\nreturn ((i + self.capacity() as i32) % self.capacity() as i32) as usize;\n}\n/* \u961f\u9996\u5165\u961f */\npub fn push_first(&mut self, num: i32) {\nif self.que_size == self.capacity() {\nprintln!(\"\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\nself.front = self.index(self.front as i32 - 1);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nself.nums[self.front] = num;\nself.que_size += 1;\n}\n/* \u961f\u5c3e\u5165\u961f */\npub fn push_last(&mut self, num: i32) {\nif self.que_size == self.capacity() {\nprintln!(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\nreturn\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nlet rear = self.index(self.front as i32 + self.que_size as i32);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nself.nums[rear] = num;\nself.que_size += 1;\n}\n/* \u961f\u9996\u51fa\u961f */\nfn pop_first(&mut self) -> i32 {\nlet num = self.peek_first();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nself.front = self.index(self.front as i32 + 1);\nself.que_size -= 1;\nnum\n}\n/* \u961f\u5c3e\u51fa\u961f */\nfn pop_last(&mut self) -> i32 {\nlet num = self.peek_last();\nself.que_size -= 1;\nnum\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfn peek_first(&self) -> i32 {\nif self.is_empty() { panic!(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\") };\nself.nums[self.front]\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfn peek_last(&self) -> i32 {\nif self.is_empty() { panic!(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\") };\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nlet last = self.index(self.front as i32 + self.que_size as i32 - 1);\nself.nums[last]\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nfn to_array(&self) -> Vec<i32> {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nlet mut res = vec![0; self.que_size];\nlet mut j = self.front;\nfor i in 0..self.que_size {\nres[i] = self.nums[self.index(j as i32)];\nj += 1;\n}\nres\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.zig
[class]{ArrayDeque}-[func]{}\n
"},{"location":"chapter_stack_and_queue/deque/#533","title":"5.3.3 \u00a0 \u53cc\u5411\u961f\u5217\u5e94\u7528","text":"

\u53cc\u5411\u961f\u5217\u517c\u5177\u6808\u4e0e\u961f\u5217\u7684\u903b\u8f91\uff0c\u56e0\u6b64\u5b83\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e24\u8005\u7684\u6240\u6709\u5e94\u7528\u573a\u666f\uff0c\u540c\u65f6\u63d0\u4f9b\u66f4\u9ad8\u7684\u81ea\u7531\u5ea6\u3002

\u6211\u4eec\u77e5\u9053\uff0c\u8f6f\u4ef6\u7684\u201c\u64a4\u9500\u201d\u529f\u80fd\u901a\u5e38\u4f7f\u7528\u6808\u6765\u5b9e\u73b0\uff1a\u7cfb\u7edf\u5c06\u6bcf\u6b21\u66f4\u6539\u64cd\u4f5c push \u5230\u6808\u4e2d\uff0c\u7136\u540e\u901a\u8fc7 pop \u5b9e\u73b0\u64a4\u9500\u3002\u7136\u800c\uff0c\u8003\u8651\u5230\u7cfb\u7edf\u8d44\u6e90\u7684\u9650\u5236\uff0c\u8f6f\u4ef6\u901a\u5e38\u4f1a\u9650\u5236\u64a4\u9500\u7684\u6b65\u6570\uff08\u4f8b\u5982\u4ec5\u5141\u8bb8\u4fdd\u5b58 \\(50\\) \u6b65\uff09\u3002\u5f53\u6808\u7684\u957f\u5ea6\u8d85\u8fc7 \\(50\\) \u65f6\uff0c\u8f6f\u4ef6\u9700\u8981\u5728\u6808\u5e95\uff08\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\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

\u5982\u56fe 5-4 \u6240\u793a\uff0c\u6211\u4eec\u5c06\u961f\u5217\u7684\u5934\u90e8\u79f0\u4e3a\u201c\u961f\u9996\u201d\uff0c\u5c3e\u90e8\u79f0\u4e3a\u201c\u961f\u5c3e\u201d\uff0c\u5c06\u628a\u5143\u7d20\u52a0\u5165\u961f\u5c3e\u7684\u64cd\u4f5c\u79f0\u4e3a\u201c\u5165\u961f\u201d\uff0c\u5220\u9664\u961f\u9996\u5143\u7d20\u7684\u64cd\u4f5c\u79f0\u4e3a\u201c\u51fa\u961f\u201d\u3002

\u56fe 5-4 \u00a0 \u961f\u5217\u7684\u5148\u5165\u5148\u51fa\u89c4\u5219

"},{"location":"chapter_stack_and_queue/queue/#521","title":"5.2.1 \u00a0 \u961f\u5217\u5e38\u7528\u64cd\u4f5c","text":"

\u961f\u5217\u7684\u5e38\u89c1\u64cd\u4f5c\u5982\u8868 5-2 \u6240\u793a\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u7684\u65b9\u6cd5\u540d\u79f0\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002\u6211\u4eec\u5728\u6b64\u91c7\u7528\u4e0e\u6808\u76f8\u540c\u7684\u65b9\u6cd5\u547d\u540d\u3002

\u8868 5-2 \u00a0 \u961f\u5217\u64cd\u4f5c\u6548\u7387

\u65b9\u6cd5\u540d \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 push() \u5143\u7d20\u5165\u961f\uff0c\u5373\u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u961f\u5c3e \\(O(1)\\) pop() \u961f\u9996\u5143\u7d20\u51fa\u961f \\(O(1)\\) peek() \u8bbf\u95ee\u961f\u9996\u5143\u7d20 \\(O(1)\\)

\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u4e2d\u73b0\u6210\u7684\u961f\u5217\u7c7b\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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_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.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.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.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
queue.rs
/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 Rust \u4e2d\u4f7f\u7528\u53cc\u5411\u961f\u5217\u4f5c\u4e3a\u666e\u901a\u961f\u5217\u6765\u4f7f\u7528\nlet mut deque: VecDeque<u32> = VecDeque::new();\n/* \u5143\u7d20\u5165\u961f */\ndeque.push_back(1);\ndeque.push_back(3);\ndeque.push_back(2);\ndeque.push_back(5);\ndeque.push_back(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nif let Some(front) = deque.front() {\n}\n/* \u5143\u7d20\u51fa\u961f */\nif let Some(pop) = deque.pop_front() {\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = deque.len();\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = deque.is_empty();\n
queue.c
// C \u672a\u63d0\u4f9b\u5185\u7f6e\u961f\u5217\n
queue.zig
\n
"},{"location":"chapter_stack_and_queue/queue/#522","title":"5.2.2 \u00a0 \u961f\u5217\u5b9e\u73b0","text":"

\u4e3a\u4e86\u5b9e\u73b0\u961f\u5217\uff0c\u6211\u4eec\u9700\u8981\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u5728\u4e00\u7aef\u6dfb\u52a0\u5143\u7d20\uff0c\u5e76\u5728\u53e6\u4e00\u7aef\u5220\u9664\u5143\u7d20\u3002\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":"1. \u00a0 \u57fa\u4e8e\u94fe\u8868\u7684\u5b9e\u73b0","text":"

\u5982\u56fe 5-5 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u94fe\u8868\u7684\u201c\u5934\u8282\u70b9\u201d\u548c\u201c\u5c3e\u8282\u70b9\u201d\u5206\u522b\u89c6\u4e3a\u201c\u961f\u9996\u201d\u548c\u201c\u961f\u5c3e\u201d\uff0c\u89c4\u5b9a\u961f\u5c3e\u4ec5\u53ef\u6dfb\u52a0\u8282\u70b9\uff0c\u961f\u9996\u4ec5\u53ef\u5220\u9664\u8282\u70b9\u3002

LinkedListQueuepush()pop()

\u56fe 5-5 \u00a0 \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c

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

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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):\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.is_empty():\nraise IndexError(\"\u961f\u5217\u4e3a\u7a7a\")\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.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 isEmpty() {\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.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 (isEmpty())\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.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(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 (IsEmpty())\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.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.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.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.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
linkedlist_queue.rs
/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\n#[allow(dead_code)]\npub struct LinkedListQueue<T> {\nfront: Option<Rc<RefCell<ListNode<T>>>>,    // \u5934\u8282\u70b9 front\nrear: Option<Rc<RefCell<ListNode<T>>>>,     // \u5c3e\u8282\u70b9 rear \nque_size: usize,                            // \u961f\u5217\u7684\u957f\u5ea6\n}\nimpl<T: Copy> LinkedListQueue<T> {\npub fn new() -> Self {\nSelf {\nfront: None,\nrear: None,\nque_size: 0, }\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\npub fn size(&self) -> usize {\nreturn self.que_size;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npub fn is_empty(&self) -> bool {\nreturn self.size() == 0;\n}\n/* \u5165\u961f */\npub fn push(&mut self, num: T) {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nlet new_rear = ListNode::new(num);\nmatch self.rear.take() {\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\nSome(old_rear) => {\nold_rear.borrow_mut().next = Some(new_rear.clone());\nself.rear = Some(new_rear);\n}\n// \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\nNone => {\nself.front = Some(new_rear.clone());\nself.rear = Some(new_rear);\n}\n}\nself.que_size += 1;\n}\n/* \u51fa\u961f */\npub fn pop(&mut self) -> Option<T> {\nself.front.take().map(|old_front| {\nmatch old_front.borrow_mut().next.take() {\nSome(new_front) => {\nself.front = Some(new_front);\n}\nNone => {\nself.rear.take();\n}\n}\nself.que_size -= 1;\nRc::try_unwrap(old_front).ok().unwrap().into_inner().val\n})\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\nself.front.as_ref()\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\nif let Some(node) = head {\nlet mut nums = self.to_array(node.borrow().next.as_ref());\nnums.insert(0, node.borrow().val);\nreturn nums;\n}\nreturn Vec::new();\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; i++) {\narr[i] = node->val;\nnode = node->next;\n}\nprintArray(arr, queue->queSize);\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\u51fd\u6570\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\u51fd\u6570\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());\n@memset(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
"},{"location":"chapter_stack_and_queue/queue/#2","title":"2. \u00a0 \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 size \u7528\u4e8e\u8bb0\u5f55\u961f\u5217\u957f\u5ea6\u3002\u5b9a\u4e49 rear = front + size \uff0c\u8fd9\u4e2a\u516c\u5f0f\u8ba1\u7b97\u51fa\u7684 rear \u6307\u5411\u961f\u5c3e\u5143\u7d20\u4e4b\u540e\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\u3002

\u57fa\u4e8e\u6b64\u8bbe\u8ba1\uff0c\u6570\u7ec4\u4e2d\u5305\u542b\u5143\u7d20\u7684\u6709\u6548\u533a\u95f4\u4e3a [front, rear - 1]\uff0c\u5404\u79cd\u64cd\u4f5c\u7684\u5b9e\u73b0\u65b9\u6cd5\u5982\u56fe 5-6 \u6240\u793a\u3002

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

\u53ef\u4ee5\u770b\u5230\uff0c\u5165\u961f\u548c\u51fa\u961f\u64cd\u4f5c\u90fd\u53ea\u9700\u8fdb\u884c\u4e00\u6b21\u64cd\u4f5c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(1)\\) \u3002

ArrayQueuepush()pop()

\u56fe 5-6 \u00a0 \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c

\u4f60\u53ef\u80fd\u4f1a\u53d1\u73b0\u4e00\u4e2a\u95ee\u9898\uff1a\u5728\u4e0d\u65ad\u8fdb\u884c\u5165\u961f\u548c\u51fa\u961f\u7684\u8fc7\u7a0b\u4e2d\uff0cfront \u548c rear \u90fd\u5728\u5411\u53f3\u79fb\u52a8\uff0c\u5f53\u5b83\u4eec\u5230\u8fbe\u6570\u7ec4\u5c3e\u90e8\u65f6\u5c31\u65e0\u6cd5\u7ee7\u7eed\u79fb\u52a8\u4e86\u3002\u4e3a\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u89c6\u4e3a\u9996\u5c3e\u76f8\u63a5\u7684\u201c\u73af\u5f62\u6570\u7ec4\u201d\u3002

\u5bf9\u4e8e\u73af\u5f62\u6570\u7ec4\uff0c\u6211\u4eec\u9700\u8981\u8ba9 front \u6216 rear \u5728\u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u76f4\u63a5\u56de\u5230\u6570\u7ec4\u5934\u90e8\u7ee7\u7eed\u904d\u5386\u3002\u8fd9\u79cd\u5468\u671f\u6027\u89c4\u5f8b\u53ef\u4ee5\u901a\u8fc7\u201c\u53d6\u4f59\u64cd\u4f5c\u201d\u6765\u5b9e\u73b0\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig array_queue.py
class ArrayQueue:\n\"\"\"\u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217\"\"\"\ndef __init__(self, size: int):\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):\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\u90e8\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.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 isEmpty() {\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 (isEmpty())\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.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.cs
/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\nprivate readonly 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.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.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.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 */\nisEmpty() {\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.isEmpty()) 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 */\nisEmpty(): 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.isEmpty()) 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.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
array_queue.rs
/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nstruct ArrayQueue {\nnums: Vec<i32>,     // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nfront: i32,         // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nque_size: i32,      // \u961f\u5217\u957f\u5ea6\nque_capacity: i32,  // \u961f\u5217\u5bb9\u91cf\n}\nimpl ArrayQueue {\n/* \u6784\u9020\u65b9\u6cd5 */\nfn new(capacity: i32) -> ArrayQueue {\nArrayQueue {\nnums: vec![0; capacity as usize],\nfront: 0,\nque_size: 0,\nque_capacity: capacity,\n}\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nfn capacity(&self) -> i32 {\nself.que_capacity\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfn size(&self) -> i32 {\nself.que_size\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfn is_empty(&self) -> bool {\nself.que_size == 0\n}\n/* \u5165\u961f */\nfn push(&mut self, num: i32) {\nif self.que_size == self.capacity() {\nprintln!(\"\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 = (self.front + self.que_size) % self.que_capacity;\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nself.nums[rear as usize] = num;\nself.que_size += 1;\n}\n/* \u51fa\u961f */\nfn pop(&mut self) -> i32 {\nlet 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.que_capacity;\nself.que_size -= 1;\nnum\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfn peek(&self) -> i32 {\nif self.is_empty() {\npanic!(\"index out of bounds\");\n}\nself.nums[self.front as usize]\n}\n/* \u8fd4\u56de\u6570\u7ec4 */\nfn to_vector(&self) -> Vec<i32> {\nlet cap = self.que_capacity;\nlet mut j = self.front;\nlet mut arr = vec![0; self.que_size as usize];\nfor i in 0..self.que_size {\narr[i as usize] = self.nums[(j % cap) as usize];\nj += 1;\n}\narr\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.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\nqueSize: 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\u51fd\u6570\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);\n@memset(self.nums, @as(T, 0));\n}\n// \u6790\u6784\u51fd\u6570\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.queSize;\n}\n// \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\npub fn isEmpty(self: *Self) bool {\nreturn self.queSize == 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.queSize) % self.capacity();\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nself.nums[rear] = num;\nself.queSize += 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.queSize -= 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());\n@memset(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

\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\u7684\u903b\u8f91\u7684\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u3002

\u6211\u4eec\u53ef\u4ee5\u5c06\u6808\u7c7b\u6bd4\u4e3a\u684c\u9762\u4e0a\u7684\u4e00\u645e\u76d8\u5b50\uff0c\u5982\u679c\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

\u5982\u56fe 5-1 \u6240\u793a\uff0c\u6211\u4eec\u628a\u5806\u53e0\u5143\u7d20\u7684\u9876\u90e8\u79f0\u4e3a\u201c\u6808\u9876\u201d\uff0c\u5e95\u90e8\u79f0\u4e3a\u201c\u6808\u5e95\u201d\u3002\u5c06\u628a\u5143\u7d20\u6dfb\u52a0\u5230\u6808\u9876\u7684\u64cd\u4f5c\u53eb\u505a\u201c\u5165\u6808\u201d\uff0c\u5220\u9664\u6808\u9876\u5143\u7d20\u7684\u64cd\u4f5c\u53eb\u505a\u201c\u51fa\u6808\u201d\u3002

\u56fe 5-1 \u00a0 \u6808\u7684\u5148\u5165\u540e\u51fa\u89c4\u5219

"},{"location":"chapter_stack_and_queue/stack/#511","title":"5.1.1 \u00a0 \u6808\u5e38\u7528\u64cd\u4f5c","text":"

\u6808\u7684\u5e38\u7528\u64cd\u4f5c\u5982\u8868 5-1 \u6240\u793a\uff0c\u5177\u4f53\u7684\u65b9\u6cd5\u540d\u9700\u8981\u6839\u636e\u6240\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\u6765\u786e\u5b9a\u3002\u5728\u6b64\uff0c\u6211\u4eec\u4ee5\u5e38\u89c1\u7684 push()\u3001pop()\u3001peek() \u547d\u540d\u4e3a\u4f8b\u3002

\u8868 5-1 \u00a0 \u6808\u7684\u64cd\u4f5c\u6548\u7387

\u65b9\u6cd5 \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 push() \u5143\u7d20\u5165\u6808\uff08\u6dfb\u52a0\u81f3\u6808\u9876\uff09 \\(O(1)\\) pop() \u6808\u9876\u5143\u7d20\u51fa\u6808 \\(O(1)\\) peek() \u8bbf\u95ee\u6808\u9876\u5143\u7d20 \\(O(1)\\)

\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u5185\u7f6e\u7684\u6808\u7c7b\u3002\u7136\u800c\uff0c\u67d0\u4e9b\u8bed\u8a00\u53ef\u80fd\u6ca1\u6709\u4e13\u95e8\u63d0\u4f9b\u6808\u7c7b\uff0c\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u5c06\u8be5\u8bed\u8a00\u7684\u201c\u6570\u7ec4\u201d\u6216\u201c\u94fe\u8868\u201d\u89c6\u4f5c\u6808\u6765\u4f7f\u7528\uff0c\u5e76\u5728\u7a0b\u5e8f\u903b\u8f91\u4e0a\u5ffd\u7565\u4e0e\u6808\u65e0\u5173\u7684\u64cd\u4f5c\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig stack.py
# \u521d\u59cb\u5316\u6808\n# Python \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a List \u5f53\u4f5c\u6808\u6765\u4f7f\u7528 \nstack: list[int] = []\n# \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.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.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.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_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.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.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.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
stack.rs
/* \u521d\u59cb\u5316\u6808 */\n// \u628a Vec \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nlet mut stack: Vec<i32> = Vec::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 */\nlet top = stack[stack.len() - 1];\n/* \u5143\u7d20\u51fa\u6808 */\nlet pop = stack.pop().unwrap();\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nlet size = stack.len();\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = stack.is_empty();\n
stack.c
// C \u672a\u63d0\u4f9b\u5185\u7f6e\u6808\n
stack.zig
\n
"},{"location":"chapter_stack_and_queue/stack/#512","title":"5.1.2 \u00a0 \u6808\u7684\u5b9e\u73b0","text":"

\u4e3a\u4e86\u6df1\u5165\u4e86\u89e3\u6808\u7684\u8fd0\u884c\u673a\u5236\uff0c\u6211\u4eec\u6765\u5c1d\u8bd5\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e2a\u6808\u7c7b\u3002

\u6808\u9075\u5faa\u5148\u5165\u540e\u51fa\u7684\u539f\u5219\uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u80fd\u5728\u6808\u9876\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\u3002\u7136\u800c\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u90fd\u53ef\u4ee5\u5728\u4efb\u610f\u4f4d\u7f6e\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\uff0c\u56e0\u6b64\u6808\u53ef\u4ee5\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":"1. \u00a0 \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

\u5982\u56fe 5-2 \u6240\u793a\uff0c\u5bf9\u4e8e\u5165\u6808\u64cd\u4f5c\uff0c\u6211\u4eec\u53ea\u9700\u5c06\u5143\u7d20\u63d2\u5165\u94fe\u8868\u5934\u90e8\uff0c\u8fd9\u79cd\u8282\u70b9\u63d2\u5165\u65b9\u6cd5\u88ab\u79f0\u4e3a\u201c\u5934\u63d2\u6cd5\u201d\u3002\u800c\u5bf9\u4e8e\u51fa\u6808\u64cd\u4f5c\uff0c\u53ea\u9700\u5c06\u5934\u8282\u70b9\u4ece\u94fe\u8868\u4e2d\u5220\u9664\u5373\u53ef\u3002

LinkedListStackpush()pop()

\u56fe 5-2 \u00a0 \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u6808\u7684\u5165\u6808\u51fa\u6808\u64cd\u4f5c

\u4ee5\u4e0b\u662f\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u6808\u7684\u793a\u4f8b\u4ee3\u7801\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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):\n\"\"\"\u5165\u6808\"\"\"\nnode = ListNode(val)\nnode.next = self._peek\nself._peek = node\nself._size += 1\ndef pop(self) -> int:\n\"\"\"\u51fa\u6808\"\"\"\nnum = self.peek()\nself._peek = self._peek.next\nself._size -= 1\nreturn num\ndef peek(self) -> int:\n\"\"\"\u8bbf\u95ee\u6808\u9876\u5143\u7d20\"\"\"\nif self.is_empty():\nraise IndexError(\"\u6808\u4e3a\u7a7a\")\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.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 isEmpty() {\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 (isEmpty())\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.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 (isEmpty())\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.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(num) {\nnext = stackPeek\n};\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 (IsEmpty())\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.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.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.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.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
linkedlist_stack.rs
/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\n#[allow(dead_code)]\npub struct LinkedListStack<T> {\nstack_peek: Option<Rc<RefCell<ListNode<T>>>>,   // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nstk_size: usize,                                // \u6808\u7684\u957f\u5ea6\n}\nimpl<T: Copy> LinkedListStack<T> {\npub fn new() -> Self {\nSelf {\nstack_peek: None,\nstk_size: 0,\n}\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\npub fn size(&self) -> usize {\nreturn self.stk_size;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\npub fn is_empty(&self) -> bool {\nreturn self.size() == 0;\n}\n/* \u5165\u6808 */\npub fn push(&mut self, num: T) {\nlet node = ListNode::new(num);\nnode.borrow_mut().next = self.stack_peek.take();\nself.stack_peek = Some(node);\nself.stk_size += 1;\n}\n/* \u51fa\u6808 */\npub fn pop(&mut self) -> Option<T> {\nself.stack_peek.take().map(|old_head| {\nmatch old_head.borrow_mut().next.take() {\nSome(new_head) => {\nself.stack_peek = Some(new_head);\n}\nNone => {\nself.stack_peek = None;\n}\n}\nself.stk_size -= 1;\nRc::try_unwrap(old_head).ok().unwrap().into_inner().val\n})\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\nself.stack_peek.as_ref()\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\nif let Some(node) = head {\nlet mut nums = self.to_array(node.borrow().next.as_ref());\nnums.push(node.borrow().val);\nreturn nums;\n}\nreturn Vec::new();\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.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\u51fd\u6570\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\u51fd\u6570\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());\n@memset(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
"},{"location":"chapter_stack_and_queue/stack/#2","title":"2. \u00a0 \u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"

\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u6808\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u7684\u5c3e\u90e8\u4f5c\u4e3a\u6808\u9876\u3002\u5982\u56fe 5-3 \u6240\u793a\uff0c\u5165\u6808\u4e0e\u51fa\u6808\u64cd\u4f5c\u5206\u522b\u5bf9\u5e94\u5728\u6570\u7ec4\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\u4e0e\u5220\u9664\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(1)\\) \u3002

ArrayStackpush()pop()

\u56fe 5-3 \u00a0 \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u6808\u7684\u5165\u6808\u51fa\u6808\u64cd\u4f5c

\u7531\u4e8e\u5165\u6808\u7684\u5143\u7d20\u53ef\u80fd\u4f1a\u6e90\u6e90\u4e0d\u65ad\u5730\u589e\u52a0\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u52a8\u6001\u6570\u7ec4\uff0c\u8fd9\u6837\u5c31\u65e0\u987b\u81ea\u884c\u5904\u7406\u6570\u7ec4\u6269\u5bb9\u95ee\u9898\u3002\u4ee5\u4e0b\u4e3a\u793a\u4f8b\u4ee3\u7801\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig array_stack.py
class ArrayStack:\n\"\"\"\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\"\"\"\ndef __init__(self):\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):\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.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 isEmpty() {\nreturn stack.size() == 0;\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 (isEmpty())\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.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.cs
/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\nprivate readonly 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.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.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.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 */\nisEmpty() {\nreturn this.#stack.length === 0;\n}\n/* \u5165\u6808 */\npush(num) {\nthis.#stack.push(num);\n}\n/* \u51fa\u6808 */\npop() {\nif (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\nreturn this.#stack.pop();\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\ntop() {\nif (this.isEmpty()) 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 */\nisEmpty(): 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.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\nreturn this.stack.pop();\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\ntop(): number | undefined {\nif (this.isEmpty()) 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.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
array_stack.rs
/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nstruct ArrayStack<T> {\nstack: Vec<T>,\n}\nimpl<T> ArrayStack<T> {\n/* \u521d\u59cb\u5316\u6808 */\nfn new() -> ArrayStack<T> {\nArrayStack::<T> { stack: Vec::<T>::new() }\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nfn size(&self) -> usize {\nself.stack.len()\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nfn is_empty(&self) -> bool {\nself.size() == 0\n}\n/* \u5165\u6808 */\nfn push(&mut self, num: T) {\nself.stack.push(num);\n}\n/* \u51fa\u6808 */\nfn pop(&mut self) -> Option<T> {\nmatch self.stack.pop() {\nSome(num) => Some(num),\nNone => None,\n}\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nfn peek(&self) -> Option<&T> {\nif self.is_empty() { panic!(\"\u6808\u4e3a\u7a7a\") };\nself.stack.last()\n}\n/* \u8fd4\u56de &Vec */\nfn to_array(&self) -> &Vec<T> {\n&self.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.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
"},{"location":"chapter_stack_and_queue/stack/#513","title":"5.1.3 \u00a0 \u4e24\u79cd\u5b9e\u73b0\u5bf9\u6bd4","text":"

\u652f\u6301\u64cd\u4f5c

\u4e24\u79cd\u5b9e\u73b0\u90fd\u652f\u6301\u6808\u5b9a\u4e49\u4e2d\u7684\u5404\u9879\u64cd\u4f5c\u3002\u6570\u7ec4\u5b9e\u73b0\u989d\u5916\u652f\u6301\u968f\u673a\u8bbf\u95ee\uff0c\u4f46\u8fd9\u5df2\u8d85\u51fa\u4e86\u6808\u7684\u5b9a\u4e49\u8303\u7574\uff0c\u56e0\u6b64\u4e00\u822c\u4e0d\u4f1a\u7528\u5230\u3002

\u65f6\u95f4\u6548\u7387

\u5728\u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0\u4e2d\uff0c\u5165\u6808\u548c\u51fa\u6808\u64cd\u4f5c\u90fd\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\u65f6\uff0c\u4f8b\u5982 int \u6216 double \uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\u3002

  • \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\u3002
  • \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\u53ef\u4ee5\u63d0\u4f9b\u66f4\u52a0\u7a33\u5b9a\u7684\u6548\u7387\u8868\u73b0\u3002

\u7a7a\u95f4\u6548\u7387

\u5728\u521d\u59cb\u5316\u5217\u8868\u65f6\uff0c\u7cfb\u7edf\u4f1a\u4e3a\u5217\u8868\u5206\u914d\u201c\u521d\u59cb\u5bb9\u91cf\u201d\uff0c\u8be5\u5bb9\u91cf\u53ef\u80fd\u8d85\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\u540e\u9000\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":""},{"location":"chapter_stack_and_queue/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","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\u52a3\u5316\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/#2-q-a","title":"2. \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

\u64a4\u9500\uff08undo\uff09\u548c\u53cd\u64a4\u9500\uff08redo\uff09\u5177\u4f53\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff1f

\u4f7f\u7528\u4e24\u4e2a\u5806\u6808\uff0c\u6808 A \u7528\u4e8e\u64a4\u9500\uff0c\u6808 B \u7528\u4e8e\u53cd\u64a4\u9500\u3002

  1. \u6bcf\u5f53\u7528\u6237\u6267\u884c\u4e00\u4e2a\u64cd\u4f5c\uff0c\u5c06\u8fd9\u4e2a\u64cd\u4f5c\u538b\u5165\u6808 A \uff0c\u5e76\u6e05\u7a7a\u6808 B \u3002
  2. \u5f53\u7528\u6237\u6267\u884c\u201c\u64a4\u9500\u201d\u65f6\uff0c\u4ece\u6808 A \u4e2d\u5f39\u51fa\u6700\u8fd1\u7684\u64cd\u4f5c\uff0c\u5e76\u5c06\u5176\u538b\u5165\u6808 B \u3002
  3. \u5f53\u7528\u6237\u6267\u884c\u201c\u53cd\u64a4\u9500\u201d\u65f6\uff0c\u4ece\u6808 B \u4e2d\u5f39\u51fa\u6700\u8fd1\u7684\u64cd\u4f5c\uff0c\u5e76\u5c06\u5176\u538b\u5165\u6808 A \u3002
"},{"location":"chapter_tree/","title":"\u7b2c 7 \u7ae0 \u00a0 \u6811","text":"

Abstract

\u53c2\u5929\u5927\u6811\u5145\u6ee1\u751f\u547d\u529b\uff0c\u5176\u6839\u6df1\u53f6\u8302\uff0c\u5206\u679d\u6276\u758f\u3002

\u5b83\u4e3a\u6211\u4eec\u5c55\u73b0\u4e86\u6570\u636e\u5206\u6cbb\u7684\u751f\u52a8\u5f62\u6001\u3002

"},{"location":"chapter_tree/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"
  • 7.1 \u00a0 \u4e8c\u53c9\u6811
  • 7.2 \u00a0 \u4e8c\u53c9\u6811\u904d\u5386
  • 7.3 \u00a0 \u4e8c\u53c9\u6811\u6570\u7ec4\u8868\u793a
  • 7.4 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811
  • 7.5 \u00a0 AVL \u6811 *
  • 7.6 \u00a0 \u5c0f\u7ed3
"},{"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\u6211\u4eec\u80fd\u5426\u7528\u6570\u7ec4\u6765\u8868\u793a\u4e8c\u53c9\u6811\u5462\uff1f\u7b54\u6848\u662f\u80af\u5b9a\u7684\u3002

"},{"location":"chapter_tree/array_representation_of_tree/#731","title":"7.3.1 \u00a0 \u8868\u793a\u5b8c\u7f8e\u4e8c\u53c9\u6811","text":"

\u5148\u5206\u6790\u4e00\u4e2a\u7b80\u5355\u6848\u4f8b\u3002\u7ed9\u5b9a\u4e00\u4e2a\u5b8c\u7f8e\u4e8c\u53c9\u6811\uff0c\u6211\u4eec\u5c06\u6240\u6709\u8282\u70b9\u6309\u7167\u5c42\u5e8f\u904d\u5386\u7684\u987a\u5e8f\u5b58\u50a8\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\uff0c\u5219\u6bcf\u4e2a\u8282\u70b9\u90fd\u5bf9\u5e94\u552f\u4e00\u7684\u6570\u7ec4\u7d22\u5f15\u3002

\u6839\u636e\u5c42\u5e8f\u904d\u5386\u7684\u7279\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u63a8\u5bfc\u51fa\u7236\u8282\u70b9\u7d22\u5f15\u4e0e\u5b50\u8282\u70b9\u7d22\u5f15\u4e4b\u95f4\u7684\u201c\u6620\u5c04\u516c\u5f0f\u201d\uff1a\u82e5\u8282\u70b9\u7684\u7d22\u5f15\u4e3a \\(i\\) \uff0c\u5219\u8be5\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 1\\) \uff0c\u53f3\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 2\\) \u3002\u56fe 7-12 \u5c55\u793a\u4e86\u5404\u4e2a\u8282\u70b9\u7d22\u5f15\u4e4b\u95f4\u7684\u6620\u5c04\u5173\u7cfb\u3002

\u56fe 7-12 \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a

\u6620\u5c04\u516c\u5f0f\u7684\u89d2\u8272\u76f8\u5f53\u4e8e\u94fe\u8868\u4e2d\u7684\u6307\u9488\u3002\u7ed9\u5b9a\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u8282\u70b9\uff0c\u6211\u4eec\u90fd\u53ef\u4ee5\u901a\u8fc7\u6620\u5c04\u516c\u5f0f\u6765\u8bbf\u95ee\u5b83\u7684\u5de6\uff08\u53f3\uff09\u5b50\u8282\u70b9\u3002

"},{"location":"chapter_tree/array_representation_of_tree/#732","title":"7.3.2 \u00a0 \u8868\u793a\u4efb\u610f\u4e8c\u53c9\u6811","text":"

\u5b8c\u7f8e\u4e8c\u53c9\u6811\u662f\u4e00\u4e2a\u7279\u4f8b\uff0c\u5728\u4e8c\u53c9\u6811\u7684\u4e2d\u95f4\u5c42\u901a\u5e38\u5b58\u5728\u8bb8\u591a \\(\\text{None}\\) \u3002\u7531\u4e8e\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5e76\u4e0d\u5305\u542b\u8fd9\u4e9b \\(\\text{None}\\) \uff0c\u56e0\u6b64\u6211\u4eec\u65e0\u6cd5\u4ec5\u51ed\u8be5\u5e8f\u5217\u6765\u63a8\u6d4b \\(\\text{None}\\) \u7684\u6570\u91cf\u548c\u5206\u5e03\u4f4d\u7f6e\u3002\u8fd9\u610f\u5473\u7740\u5b58\u5728\u591a\u79cd\u4e8c\u53c9\u6811\u7ed3\u6784\u90fd\u7b26\u5408\u8be5\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u3002

\u5982\u56fe 7-13 \u6240\u793a\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u975e\u5b8c\u7f8e\u4e8c\u53c9\u6811\uff0c\u4e0a\u8ff0\u7684\u6570\u7ec4\u8868\u793a\u65b9\u6cd5\u5df2\u7ecf\u5931\u6548\u3002

\u56fe 7-13 \u00a0 \u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5bf9\u5e94\u591a\u79cd\u4e8c\u53c9\u6811\u53ef\u80fd\u6027

\u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u5728\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u4e2d\u663e\u5f0f\u5730\u5199\u51fa\u6240\u6709 \\(\\text{None}\\) \u3002\u5982\u56fe 7-14 \u6240\u793a\uff0c\u8fd9\u6837\u5904\u7406\u540e\uff0c\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5c31\u53ef\u4ee5\u552f\u4e00\u8868\u793a\u4e8c\u53c9\u6811\u4e86\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig
# \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a\n# \u4f7f\u7528 None \u6765\u8868\u793a\u7a7a\u4f4d\ntree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int \u6700\u5927\u503c INT_MAX \u6807\u8bb0\u7a7a\u4f4d\nvector<int> tree = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int \u7684\u5305\u88c5\u7c7b Integer \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u6765\u6807\u8bb0\u7a7a\u4f4d\nInteger[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int? \u53ef\u7a7a\u7c7b\u578b \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u6765\u6807\u8bb0\u7a7a\u4f4d\nint?[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 any \u7c7b\u578b\u7684\u5207\u7247, \u5c31\u53ef\u4ee5\u4f7f\u7528 nil \u6765\u6807\u8bb0\u7a7a\u4f4d\ntree := []any{1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15}\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 Int? \u53ef\u7a7a\u7c7b\u578b \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 nil \u6765\u6807\u8bb0\u7a7a\u4f4d\nlet tree: [Int?] = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 null \u6765\u8868\u793a\u7a7a\u4f4d\nlet tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 null \u6765\u8868\u793a\u7a7a\u4f4d\nlet tree: (number | null)[] = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int? \u53ef\u7a7a\u7c7b\u578b \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u6765\u6807\u8bb0\u7a7a\u4f4d\nList<int?> tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 None \u6765\u6807\u8bb0\u7a7a\u4f4d\nlet tree = [Some(1), Some(2), Some(3), Some(4), None, Some(6), Some(7), Some(8), Some(9), None, None, Some(12), None, None, Some(15)];\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int \u6700\u5927\u503c\u6807\u8bb0\u7a7a\u4f4d\uff0c\u56e0\u6b64\u8981\u6c42\u8282\u70b9\u503c\u4e0d\u80fd\u4e3a INT_MAX\nint tree[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
\n

\u56fe 7-14 \u00a0 \u4efb\u610f\u7c7b\u578b\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a

\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u5b8c\u5168\u4e8c\u53c9\u6811\u975e\u5e38\u9002\u5408\u4f7f\u7528\u6570\u7ec4\u6765\u8868\u793a\u3002\u56de\u987e\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u5b9a\u4e49\uff0c\\(\\text{None}\\) \u53ea\u51fa\u73b0\u5728\u6700\u5e95\u5c42\u4e14\u9760\u53f3\u7684\u4f4d\u7f6e\uff0c\u56e0\u6b64\u6240\u6709 \\(\\text{None}\\) \u4e00\u5b9a\u51fa\u73b0\u5728\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u7684\u672b\u5c3e\u3002

\u8fd9\u610f\u5473\u7740\u4f7f\u7528\u6570\u7ec4\u8868\u793a\u5b8c\u5168\u4e8c\u53c9\u6811\u65f6\uff0c\u53ef\u4ee5\u7701\u7565\u5b58\u50a8\u6240\u6709 \\(\\text{None}\\) \uff0c\u975e\u5e38\u65b9\u4fbf\u3002\u56fe 7-15 \u7ed9\u51fa\u4e86\u4e00\u4e2a\u4f8b\u5b50\u3002

\u56fe 7-15 \u00a0 \u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a

\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u4e2a\u57fa\u4e8e\u6570\u7ec4\u8868\u793a\u7684\u4e8c\u53c9\u6811\uff0c\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\u64cd\u4f5c\u3002

  • \u7ed9\u5b9a\u67d0\u8282\u70b9\uff0c\u83b7\u53d6\u5b83\u7684\u503c\u3001\u5de6\uff08\u53f3\uff09\u5b50\u8282\u70b9\u3001\u7236\u8282\u70b9\u3002
  • \u83b7\u53d6\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u3001\u540e\u5e8f\u904d\u5386\u3001\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig array_binary_tree.py
class ArrayBinaryTree:\n\"\"\"\u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b\"\"\"\ndef __init__(self, arr: list[int | None]):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself._tree = list(arr)\ndef size(self):\n\"\"\"\u8282\u70b9\u6570\u91cf\"\"\"\nreturn len(self._tree)\ndef val(self, i: int) -> int:\n\"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c\"\"\"\n# \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de None \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif i < 0 or i >= self.size():\nreturn None\nreturn self._tree[i]\ndef left(self, i: int) -> int | None:\n\"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\nreturn 2 * i + 1\ndef right(self, i: int) -> int | None:\n\"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\nreturn 2 * i + 2\ndef parent(self, i: int) -> int | None:\n\"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\nreturn (i - 1) // 2\ndef level_order(self) -> list[int]:\n\"\"\"\u5c42\u5e8f\u904d\u5386\"\"\"\nself.res = []\n# \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor i in range(self.size()):\nif self.val(i) is not None:\nself.res.append(self.val(i))\nreturn self.res\ndef dfs(self, i: int, order: str):\n\"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386\"\"\"\nif self.val(i) is None:\nreturn\n# \u524d\u5e8f\u904d\u5386\nif order == \"pre\":\nself.res.append(self.val(i))\nself.dfs(self.left(i), order)\n# \u4e2d\u5e8f\u904d\u5386\nif order == \"in\":\nself.res.append(self.val(i))\nself.dfs(self.right(i), order)\n# \u540e\u5e8f\u904d\u5386\nif order == \"post\":\nself.res.append(self.val(i))\ndef pre_order(self) -> list[int]:\n\"\"\"\u524d\u5e8f\u904d\u5386\"\"\"\nself.res = []\nself.dfs(0, order=\"pre\")\nreturn self.res\ndef in_order(self) -> list[int]:\n\"\"\"\u4e2d\u5e8f\u904d\u5386\"\"\"\nself.res = []\nself.dfs(0, order=\"in\")\nreturn self.res\ndef post_order(self) -> list[int]:\n\"\"\"\u540e\u5e8f\u904d\u5386\"\"\"\nself.res = []\nself.dfs(0, order=\"post\")\nreturn self.res\n
array_binary_tree.cpp
/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nArrayBinaryTree(vector<int> arr) {\ntree = arr;\n}\n/* \u8282\u70b9\u6570\u91cf */\nint size() {\nreturn tree.size();\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nint val(int i) {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de INT_MAX \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif (i < 0 || i >= size())\nreturn INT_MAX;\nreturn tree[i];\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint right(int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint parent(int i) {\nreturn (i - 1) / 2;\n}\n/* \u5c42\u5e8f\u904d\u5386 */\nvector<int> levelOrder() {\nvector<int> res;\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i < size(); i++) {\nif (val(i) != INT_MAX)\nres.push_back(val(i));\n}\nreturn res;\n}\n/* \u524d\u5e8f\u904d\u5386 */\nvector<int> preOrder() {\nvector<int> res;\ndfs(0, \"pre\", res);\nreturn res;\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nvector<int> inOrder() {\nvector<int> res;\ndfs(0, \"in\", res);\nreturn res;\n}\n/* \u540e\u5e8f\u904d\u5386 */\nvector<int> postOrder() {\nvector<int> res;\ndfs(0, \"post\", res);\nreturn res;\n}\nprivate:\nvector<int> tree;\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nvoid dfs(int i, string order, vector<int> &res) {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nif (val(i) == INT_MAX)\nreturn;\n// \u524d\u5e8f\u904d\u5386\nif (order == \"pre\")\nres.push_back(val(i));\ndfs(left(i), order, res);\n// \u4e2d\u5e8f\u904d\u5386\nif (order == \"in\")\nres.push_back(val(i));\ndfs(right(i), order, res);\n// \u540e\u5e8f\u904d\u5386\nif (order == \"post\")\nres.push_back(val(i));\n}\n};\n
array_binary_tree.java
/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\nprivate List<Integer> tree;\n/* \u6784\u9020\u65b9\u6cd5 */\npublic ArrayBinaryTree(List<Integer> arr) {\ntree = new ArrayList<>(arr);\n}\n/* \u8282\u70b9\u6570\u91cf */\npublic int size() {\nreturn tree.size();\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\npublic Integer val(int i) {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif (i < 0 || i >= size())\nreturn null;\nreturn tree.get(i);\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\npublic Integer left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\npublic Integer right(int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\npublic Integer parent(int i) {\nreturn (i - 1) / 2;\n}\n/* \u5c42\u5e8f\u904d\u5386 */\npublic List<Integer> levelOrder() {\nList<Integer> res = new ArrayList<>();\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i < size(); i++) {\nif (val(i) != null)\nres.add(val(i));\n}\nreturn res;\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nprivate void dfs(Integer i, String order, List<Integer> res) {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nif (val(i) == null)\nreturn;\n// \u524d\u5e8f\u904d\u5386\nif (order == \"pre\")\nres.add(val(i));\ndfs(left(i), order, res);\n// \u4e2d\u5e8f\u904d\u5386\nif (order == \"in\")\nres.add(val(i));\ndfs(right(i), order, res);\n// \u540e\u5e8f\u904d\u5386\nif (order == \"post\")\nres.add(val(i));\n}\n/* \u524d\u5e8f\u904d\u5386 */\npublic List<Integer> preOrder() {\nList<Integer> res = new ArrayList<>();\ndfs(0, \"pre\", res);\nreturn res;\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\npublic List<Integer> inOrder() {\nList<Integer> res = new ArrayList<>();\ndfs(0, \"in\", res);\nreturn res;\n}\n/* \u540e\u5e8f\u904d\u5386 */\npublic List<Integer> postOrder() {\nList<Integer> res = new ArrayList<>();\ndfs(0, \"post\", res);\nreturn res;\n}\n}\n
array_binary_tree.cs
/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\nprivate readonly List<int?> tree;\n/* \u6784\u9020\u65b9\u6cd5 */\npublic ArrayBinaryTree(List<int?> arr) {\ntree = new List<int?>(arr);\n}\n/* \u8282\u70b9\u6570\u91cf */\npublic int Size() {\nreturn tree.Count;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\npublic int? Val(int i) {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif (i < 0 || i >= Size())\nreturn null;\nreturn tree[i];\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\npublic int Left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\npublic int Right(int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\npublic int Parent(int i) {\nreturn (i - 1) / 2;\n}\n/* \u5c42\u5e8f\u904d\u5386 */\npublic List<int> LevelOrder() {\nList<int> res = new();\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i < Size(); i++) {\nif (Val(i).HasValue)\nres.Add(Val(i).Value);\n}\nreturn res;\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nprivate void DFS(int i, string order, List<int> res) {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nif (!Val(i).HasValue)\nreturn;\n// \u524d\u5e8f\u904d\u5386\nif (order == \"pre\")\nres.Add(Val(i).Value);\nDFS(Left(i), order, res);\n// \u4e2d\u5e8f\u904d\u5386\nif (order == \"in\")\nres.Add(Val(i).Value);\nDFS(Right(i), order, res);\n// \u540e\u5e8f\u904d\u5386\nif (order == \"post\")\nres.Add(Val(i).Value);\n}\n/* \u524d\u5e8f\u904d\u5386 */\npublic List<int> PreOrder() {\nList<int> res = new();\nDFS(0, \"pre\", res);\nreturn res;\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\npublic List<int> InOrder() {\nList<int> res = new();\nDFS(0, \"in\", res);\nreturn res;\n}\n/* \u540e\u5e8f\u904d\u5386 */\npublic List<int> PostOrder() {\nList<int> res = new();\nDFS(0, \"post\", res);\nreturn res;\n}\n}\n
array_binary_tree.go
/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\ntype arrayBinaryTree struct {\ntree []any\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newArrayBinaryTree(arr []any) *arrayBinaryTree {\nreturn &arrayBinaryTree{\ntree: arr,\n}\n}\n/* \u8282\u70b9\u6570\u91cf */\nfunc (abt *arrayBinaryTree) size() int {\nreturn len(abt.tree)\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nfunc (abt *arrayBinaryTree) val(i int) any {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif i < 0 || i >= abt.size() {\nreturn nil\n}\nreturn abt.tree[i]\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) left(i int) int {\nreturn 2*i + 1\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) right(i int) int {\nreturn 2*i + 2\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) parent(i int) int {\nreturn (i - 1) / 2\n}\n/* \u5c42\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) levelOrder() []any {\nvar res []any\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor i := 0; i < abt.size(); i++ {\nif abt.val(i) != nil {\nres = append(res, abt.val(i))\n}\n}\nreturn res\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nfunc (abt *arrayBinaryTree) dfs(i int, order string, res *[]any) {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nif abt.val(i) == nil {\nreturn\n}\n// \u524d\u5e8f\u904d\u5386\nif order == \"pre\" {\n*res = append(*res, abt.val(i))\n}\nabt.dfs(abt.left(i), order, res)\n// \u4e2d\u5e8f\u904d\u5386\nif order == \"in\" {\n*res = append(*res, abt.val(i))\n}\nabt.dfs(abt.right(i), order, res)\n// \u540e\u5e8f\u904d\u5386\nif order == \"post\" {\n*res = append(*res, abt.val(i))\n}\n}\n/* \u524d\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) preOrder() []any {\nvar res []any\nabt.dfs(0, \"pre\", &res)\nreturn res\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) inOrder() []any {\nvar res []any\nabt.dfs(0, \"in\", &res)\nreturn res\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) postOrder() []any {\nvar res []any\nabt.dfs(0, \"post\", &res)\nreturn res\n}\n
array_binary_tree.swift
/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\nprivate var tree: [Int?]\n/* \u6784\u9020\u65b9\u6cd5 */\ninit(arr: [Int?]) {\ntree = arr\n}\n/* \u8282\u70b9\u6570\u91cf */\nfunc size() -> Int {\ntree.count\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nfunc val(i: Int) -> Int? {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif i < 0 || i >= size() {\nreturn nil\n}\nreturn tree[i]\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc left(i: Int) -> Int {\n2 * i + 1\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc right(i: Int) -> Int {\n2 * i + 2\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc parent(i: Int) -> Int {\n(i - 1) / 2\n}\n/* \u5c42\u5e8f\u904d\u5386 */\nfunc levelOrder() -> [Int] {\nvar res: [Int] = []\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor i in stride(from: 0, to: size(), by: 1) {\nif let val = val(i: i) {\nres.append(val)\n}\n}\nreturn res\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nprivate func dfs(i: Int, order: String, res: inout [Int]) {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nguard let val = val(i: i) else {\nreturn\n}\n// \u524d\u5e8f\u904d\u5386\nif order == \"pre\" {\nres.append(val)\n}\ndfs(i: left(i: i), order: order, res: &res)\n// \u4e2d\u5e8f\u904d\u5386\nif order == \"in\" {\nres.append(val)\n}\ndfs(i: right(i: i), order: order, res: &res)\n// \u540e\u5e8f\u904d\u5386\nif order == \"post\" {\nres.append(val)\n}\n}\n/* \u524d\u5e8f\u904d\u5386 */\nfunc preOrder() -> [Int] {\nvar res: [Int] = []\ndfs(i: 0, order: \"pre\", res: &res)\nreturn res\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc inOrder() -> [Int] {\nvar res: [Int] = []\ndfs(i: 0, order: \"in\", res: &res)\nreturn res\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfunc postOrder() -> [Int] {\nvar res: [Int] = []\ndfs(i: 0, order: \"post\", res: &res)\nreturn res\n}\n}\n
array_binary_tree.js
/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n#tree;\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor(arr) {\nthis.#tree = arr;\n}\n/* \u8282\u70b9\u6570\u91cf */\nsize() {\nreturn this.#tree.length;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nval(i) {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif (i < 0 || i >= this.size()) return null;\nreturn this.#tree[i];\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nleft(i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nright(i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nparent(i) {\nreturn (i - 1) / 2;\n}\n/* \u5c42\u5e8f\u904d\u5386 */\nlevelOrder() {\nlet res = [];\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (let i = 0; i < this.size(); i++) {\nif (this.val(i) !== null) res.push(this.val(i));\n}\nreturn res;\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n#dfs(i, order, res) {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nif (this.val(i) === null) return;\n// \u524d\u5e8f\u904d\u5386\nif (order === 'pre') res.push(this.val(i));\nthis.#dfs(this.left(i), order, res);\n// \u4e2d\u5e8f\u904d\u5386\nif (order === 'in') res.push(this.val(i));\nthis.#dfs(this.right(i), order, res);\n// \u540e\u5e8f\u904d\u5386\nif (order === 'post') res.push(this.val(i));\n}\n/* \u524d\u5e8f\u904d\u5386 */\npreOrder() {\nconst res = [];\nthis.#dfs(0, 'pre', res);\nreturn res;\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\ninOrder() {\nconst res = [];\nthis.#dfs(0, 'in', res);\nreturn res;\n}\n/* \u540e\u5e8f\u904d\u5386 */\npostOrder() {\nconst res = [];\nthis.#dfs(0, 'post', res);\nreturn res;\n}\n}\n
array_binary_tree.ts
/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n#tree: (number | null)[];\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor(arr: (number | null)[]) {\nthis.#tree = arr;\n}\n/* \u8282\u70b9\u6570\u91cf */\nsize(): number {\nreturn this.#tree.length;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nval(i: number): number | null {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif (i < 0 || i >= this.size()) return null;\nreturn this.#tree[i];\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nleft(i: number): number {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nright(i: number): number {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nparent(i: number): number {\nreturn (i - 1) / 2;\n}\n/* \u5c42\u5e8f\u904d\u5386 */\nlevelOrder(): number[] {\nlet res = [];\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (let i = 0; i < this.size(); i++) {\nif (this.val(i) !== null) res.push(this.val(i));\n}\nreturn res;\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n#dfs(i: number, order: Order, res: (number | null)[]): void {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nif (this.val(i) === null) return;\n// \u524d\u5e8f\u904d\u5386\nif (order === 'pre') res.push(this.val(i));\nthis.#dfs(this.left(i), order, res);\n// \u4e2d\u5e8f\u904d\u5386\nif (order === 'in') res.push(this.val(i));\nthis.#dfs(this.right(i), order, res);\n// \u540e\u5e8f\u904d\u5386\nif (order === 'post') res.push(this.val(i));\n}\n/* \u524d\u5e8f\u904d\u5386 */\npreOrder(): (number | null)[] {\nconst res = [];\nthis.#dfs(0, 'pre', res);\nreturn res;\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\ninOrder(): (number | null)[] {\nconst res = [];\nthis.#dfs(0, 'in', res);\nreturn res;\n}\n/* \u540e\u5e8f\u904d\u5386 */\npostOrder(): (number | null)[] {\nconst res = [];\nthis.#dfs(0, 'post', res);\nreturn res;\n}\n}\n
array_binary_tree.dart
/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\nlate List<int?> _tree;\n/* \u6784\u9020\u65b9\u6cd5 */\nArrayBinaryTree(this._tree);\n/* \u8282\u70b9\u6570\u91cf */\nint size() {\nreturn _tree.length;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nint? val(int i) {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif (i < 0 || i >= size()) {\nreturn null;\n}\nreturn _tree[i];\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint? left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint? right(int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint? parent(int i) {\nreturn (i - 1) ~/ 2;\n}\n/* \u5c42\u5e8f\u904d\u5386 */\nList<int> levelOrder() {\nList<int> res = [];\nfor (int i = 0; i < size(); i++) {\nif (val(i) != null) {\nres.add(val(i)!);\n}\n}\nreturn res;\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nvoid dfs(int i, String order, List<int?> res) {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nif (val(i) == null) {\nreturn;\n}\n// \u524d\u5e8f\u904d\u5386\nif (order == 'pre') {\nres.add(val(i));\n}\ndfs(left(i)!, order, res);\n// \u4e2d\u5e8f\u904d\u5386\nif (order == 'in') {\nres.add(val(i));\n}\ndfs(right(i)!, order, res);\n// \u540e\u5e8f\u904d\u5386\nif (order == 'post') {\nres.add(val(i));\n}\n}\n/* \u524d\u5e8f\u904d\u5386 */\nList<int?> preOrder() {\nList<int?> res = [];\ndfs(0, 'pre', res);\nreturn res;\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nList<int?> inOrder() {\nList<int?> res = [];\ndfs(0, 'in', res);\nreturn res;\n}\n/* \u540e\u5e8f\u904d\u5386 */\nList<int?> postOrder() {\nList<int?> res = [];\ndfs(0, 'post', res);\nreturn res;\n}\n}\n
array_binary_tree.rs
/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nstruct ArrayBinaryTree {\ntree: Vec<Option<i32>>,\n}\nimpl ArrayBinaryTree {\n/* \u6784\u9020\u65b9\u6cd5 */\nfn new(arr: Vec<Option<i32>>) -> Self {\nSelf { tree: arr }\n}\n/* \u8282\u70b9\u6570\u91cf */\nfn size(&self) -> i32 {\nself.tree.len() as i32\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nfn val(&self, i: i32) -> Option<i32> {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de None \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif i < 0 || i >= self.size() {\nNone\n} else {\nself.tree[i as usize]\n}\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfn left(&self, i: i32) -> i32 {\n2 * i + 1\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfn right(&self, i: i32) -> i32 {\n2 * i + 2\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfn parent(&self, i: i32) -> i32 {\n(i - 1) / 2\n}\n/* \u5c42\u5e8f\u904d\u5386 */\nfn level_order(&self) -> Vec<i32> {\nlet mut res = vec![];\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor i in 0..self.size() {\nif let Some(val) = self.val(i) {\nres.push(val)\n}\n}\nres\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nfn dfs(&self, i: i32, order: &str, res: &mut Vec<i32>) {\nif self.val(i).is_none() {\nreturn;\n}\nlet val = self.val(i).unwrap();\n// \u524d\u5e8f\u904d\u5386\nif order == \"pre\" {\nres.push(val);\n}\nself.dfs(self.left(i), order, res);\n// \u4e2d\u5e8f\u904d\u5386\nif order == \"in\" {\nres.push(val);\n}\nself.dfs(self.right(i), order, res);\n// \u540e\u5e8f\u904d\u5386\nif order == \"post\" {\nres.push(val);\n}\n}\n/* \u524d\u5e8f\u904d\u5386 */\nfn pre_order(&self) -> Vec<i32> {\nlet mut res = vec![];\nself.dfs(0, \"pre\", &mut res);\nres\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfn in_order(&self) -> Vec<i32> {\nlet mut res = vec![];\nself.dfs(0, \"in\", &mut res);\nres\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfn post_order(&self) -> Vec<i32> {\nlet mut res = vec![];\nself.dfs(0, \"post\", &mut res);\nres\n}\n}\n
array_binary_tree.c
/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nstruct arrayBinaryTree {\nvector *tree;\n};\ntypedef struct arrayBinaryTree arrayBinaryTree;\n/* \u6784\u9020\u51fd\u6570 */\narrayBinaryTree *newArrayBinaryTree(vector *arr) {\narrayBinaryTree *newABT = malloc(sizeof(arrayBinaryTree));\nnewABT->tree = arr;\nreturn newABT;\n}\n/* \u8282\u70b9\u6570\u91cf */\nint size(arrayBinaryTree *abt) {\nreturn abt->tree->size;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nint val(arrayBinaryTree *abt, int i) {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de INT_MAX \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif (i < 0 || i >= size(abt))\nreturn INT_MAX;\nreturn *(int *)abt->tree->data[i];\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nvoid dfs(arrayBinaryTree *abt, int i, const char *order, vector *res) {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nif (val(abt, i) == INT_MAX)\nreturn;\n// \u524d\u5e8f\u904d\u5386\nif (strcmp(order, \"pre\") == 0) {\nint tmp = val(abt, i);\nvectorPushback(res, &tmp, sizeof(tmp));\n}\ndfs(abt, left(i), order, res);\n// \u4e2d\u5e8f\u904d\u5386\nif (strcmp(order, \"in\") == 0) {\nint tmp = val(abt, i);\nvectorPushback(res, &tmp, sizeof(tmp));\n}\ndfs(abt, right(i), order, res);\n// \u540e\u5e8f\u904d\u5386\nif (strcmp(order, \"post\") == 0) {\nint tmp = val(abt, i);\nvectorPushback(res, &tmp, sizeof(tmp));\n}\n}\n/* \u5c42\u5e8f\u904d\u5386 */\nvector *levelOrder(arrayBinaryTree *abt) {\nvector *res = newVector();\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i < size(abt); i++) {\nif (val(abt, i) != INT_MAX) {\nint tmp = val(abt, i);\nvectorPushback(res, &tmp, sizeof(int));\n}\n}\nreturn res;\n}\n/* \u524d\u5e8f\u904d\u5386 */\nvector *preOrder(arrayBinaryTree *abt) {\nvector *res = newVector();\ndfs(abt, 0, \"pre\", res);\nreturn res;\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nvector *inOrder(arrayBinaryTree *abt) {\nvector *res = newVector();\ndfs(abt, 0, \"in\", res);\nreturn res;\n}\n/* \u540e\u5e8f\u904d\u5386 */\nvector *postOrder(arrayBinaryTree *abt) {\nvector *res = newVector();\ndfs(abt, 0, \"post\", res);\nreturn res;\n}\n
array_binary_tree.zig
[class]{ArrayBinaryTree}-[func]{}\n
"},{"location":"chapter_tree/array_representation_of_tree/#733","title":"7.3.3 \u00a0 \u4f18\u52bf\u4e0e\u5c40\u9650\u6027","text":"

\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a\u4e3b\u8981\u6709\u4ee5\u4e0b\u4f18\u70b9\u3002

  • \u6570\u7ec4\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\uff0c\u5bf9\u7f13\u5b58\u53cb\u597d\uff0c\u8bbf\u95ee\u4e0e\u904d\u5386\u901f\u5ea6\u8f83\u5feb\u3002
  • \u4e0d\u9700\u8981\u5b58\u50a8\u6307\u9488\uff0c\u6bd4\u8f83\u8282\u7701\u7a7a\u95f4\u3002
  • \u5141\u8bb8\u968f\u673a\u8bbf\u95ee\u8282\u70b9\u3002

\u7136\u800c\uff0c\u6570\u7ec4\u8868\u793a\u4e5f\u5b58\u5728\u4e00\u4e9b\u5c40\u9650\u6027\u3002

  • \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\u3002
  • \u5f53\u4e8c\u53c9\u6811\u4e2d\u5b58\u5728\u5927\u91cf \\(\\text{None}\\) \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
"},{"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\u56fe 7-24 \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

\u56fe 7-24 \u00a0 AVL \u6811\u5728\u5220\u9664\u8282\u70b9\u540e\u53d1\u751f\u9000\u5316

\u518d\u4f8b\u5982\uff0c\u5728\u56fe 7-25 \u7684\u5b8c\u7f8e\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e24\u4e2a\u8282\u70b9\u540e\uff0c\u6811\u5c06\u4e25\u91cd\u5411\u5de6\u503e\u659c\uff0c\u67e5\u627e\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u968f\u4e4b\u6076\u5316\u3002

\u56fe 7-25 \u00a0 AVL \u6811\u5728\u63d2\u5165\u8282\u70b9\u540e\u53d1\u751f\u9000\u5316

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":"

AVL \u6811\u65e2\u662f\u4e8c\u53c9\u641c\u7d22\u6811\u4e5f\u662f\u5e73\u8861\u4e8c\u53c9\u6811\uff0c\u540c\u65f6\u6ee1\u8db3\u8fd9\u4e24\u7c7b\u4e8c\u53c9\u6811\u7684\u6240\u6709\u6027\u8d28\uff0c\u56e0\u6b64\u4e5f\u88ab\u79f0\u4e3a\u300c\u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811 balanced binary search tree\u300d\u3002

"},{"location":"chapter_tree/avl_tree/#1","title":"1. \u00a0 \u8282\u70b9\u9ad8\u5ea6","text":"

\u7531\u4e8e AVL \u6811\u7684\u76f8\u5173\u64cd\u4f5c\u9700\u8981\u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u4e3a\u8282\u70b9\u7c7b\u6dfb\u52a0 height \u53d8\u91cf\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig
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: TreeNode | None = None   # \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nself.right: TreeNode | None = None  # \u53f3\u5b50\u8282\u70b9\u5f15\u7528\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
/* 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 {\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\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 {\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
/* 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\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
use std::rc::Rc;\nuse std::cell::RefCell;\n/* AVL \u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\nval: i32,                               // \u8282\u70b9\u503c\nheight: i32,                            // \u8282\u70b9\u9ad8\u5ea6\nleft: Option<Rc<RefCell<TreeNode>>>,    // \u5de6\u5b50\u8282\u70b9\nright: Option<Rc<RefCell<TreeNode>>>,   // \u53f3\u5b50\u8282\u70b9\n}\nimpl TreeNode {\n/* \u6784\u9020\u65b9\u6cd5 */\nfn new(val: i32) -> Rc<RefCell<Self>> {\nRc::new(RefCell::new(Self {\nval,\nheight: 0,\nleft: None,\nright: None\n}))\n}\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
\n

\u201c\u8282\u70b9\u9ad8\u5ea6\u201d\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

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.dart
/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode? node) {\n// \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\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
avl_tree.rs
/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfn height(node: OptionTreeNodeRc) -> i32 {\n// \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\nmatch node {\nSome(node) => node.borrow().height,\nNone => -1,\n}\n}\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfn update_height(node: OptionTreeNodeRc) {\nif let Some(node) = node {\nlet left = node.borrow().left.clone();\nlet right = node.borrow().right.clone();\n// \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nnode.borrow_mut().height = std::cmp::max(Self::height(left), Self::height(right)) + 1;\n}\n}\n
avl_tree.c
/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode *node) {\n// \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\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.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 = @max(self.height(node.?.left), self.height(node.?.right)) + 1;\n}\n
"},{"location":"chapter_tree/avl_tree/#2","title":"2. \u00a0 \u8282\u70b9\u5e73\u8861\u56e0\u5b50","text":"

\u8282\u70b9\u7684\u300c\u5e73\u8861\u56e0\u5b50 balance factor\u300d\u5b9a\u4e49\u4e3a\u8282\u70b9\u5de6\u5b50\u6811\u7684\u9ad8\u5ea6\u51cf\u53bb\u53f3\u5b50\u6811\u7684\u9ad8\u5ea6\uff0c\u540c\u65f6\u89c4\u5b9a\u7a7a\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u4e3a 0 \u3002\u6211\u4eec\u540c\u6837\u5c06\u83b7\u53d6\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u7684\u529f\u80fd\u5c01\u88c5\u6210\u51fd\u6570\uff0c\u65b9\u4fbf\u540e\u7eed\u4f7f\u7528\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
avl_tree.rs
/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfn balance_factor(node: OptionTreeNodeRc) -> i32 {\nmatch node {\n// \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nNone => 0,\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nSome(node) => {\nSelf::height(node.borrow().left.clone()) - Self::height(node.borrow().right.clone())\n}\n}\n}\n
avl_tree.c
/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode *node) {\n// \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\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.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

Note

\u8bbe\u5e73\u8861\u56e0\u5b50\u4e3a \\(f\\) \uff0c\u5219\u4e00\u68f5 AVL \u6811\u7684\u4efb\u610f\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u7686\u6ee1\u8db3 \\(-1 \\le f \\le 1\\) \u3002

"},{"location":"chapter_tree/avl_tree/#752-avl","title":"7.5.2 \u00a0 AVL \u6811\u65cb\u8f6c","text":"

AVL \u6811\u7684\u7279\u70b9\u5728\u4e8e\u201c\u65cb\u8f6c\u201d\u64cd\u4f5c\uff0c\u5b83\u80fd\u591f\u5728\u4e0d\u5f71\u54cd\u4e8c\u53c9\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u7684\u524d\u63d0\u4e0b\uff0c\u4f7f\u5931\u8861\u8282\u70b9\u91cd\u65b0\u6062\u590d\u5e73\u8861\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u65cb\u8f6c\u64cd\u4f5c\u65e2\u80fd\u4fdd\u6301\u201c\u4e8c\u53c9\u641c\u7d22\u6811\u201d\u7684\u6027\u8d28\uff0c\u4e5f\u80fd\u4f7f\u6811\u91cd\u65b0\u53d8\u4e3a\u201c\u5e73\u8861\u4e8c\u53c9\u6811\u201d\u3002

\u6211\u4eec\u5c06\u5e73\u8861\u56e0\u5b50\u7edd\u5bf9\u503c \\(> 1\\) \u7684\u8282\u70b9\u79f0\u4e3a\u201c\u5931\u8861\u8282\u70b9\u201d\u3002\u6839\u636e\u8282\u70b9\u5931\u8861\u60c5\u51b5\u7684\u4e0d\u540c\uff0c\u65cb\u8f6c\u64cd\u4f5c\u5206\u4e3a\u56db\u79cd\uff1a\u53f3\u65cb\u3001\u5de6\u65cb\u3001\u5148\u53f3\u65cb\u540e\u5de6\u65cb\u3001\u5148\u5de6\u65cb\u540e\u53f3\u65cb\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65cb\u8f6c\u64cd\u4f5c\u3002

"},{"location":"chapter_tree/avl_tree/#1_1","title":"1. \u00a0 \u53f3\u65cb","text":"

\u5982\u56fe 7-26 \u6240\u793a\uff0c\u8282\u70b9\u4e0b\u65b9\u4e3a\u5e73\u8861\u56e0\u5b50\u3002\u4ece\u5e95\u81f3\u9876\u770b\uff0c\u4e8c\u53c9\u6811\u4e2d\u9996\u4e2a\u5931\u8861\u8282\u70b9\u662f\u201c\u8282\u70b9 3\u201d\u3002\u6211\u4eec\u5173\u6ce8\u4ee5\u8be5\u5931\u8861\u8282\u70b9\u4e3a\u6839\u8282\u70b9\u7684\u5b50\u6811\uff0c\u5c06\u8be5\u8282\u70b9\u8bb0\u4e3a node \uff0c\u5176\u5de6\u5b50\u8282\u70b9\u8bb0\u4e3a child \uff0c\u6267\u884c\u201c\u53f3\u65cb\u201d\u64cd\u4f5c\u3002\u5b8c\u6210\u53f3\u65cb\u540e\uff0c\u5b50\u6811\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>

\u56fe 7-26 \u00a0 \u53f3\u65cb\u64cd\u4f5c\u6b65\u9aa4

\u5982\u56fe 7-27 \u6240\u793a\uff0c\u5f53\u8282\u70b9 child \u6709\u53f3\u5b50\u8282\u70b9\uff08\u8bb0\u4e3a grandChild \uff09\u65f6\uff0c\u9700\u8981\u5728\u53f3\u65cb\u4e2d\u6dfb\u52a0\u4e00\u6b65\uff1a\u5c06 grandChild \u4f5c\u4e3a node \u7684\u5de6\u5b50\u8282\u70b9\u3002

\u56fe 7-27 \u00a0 \u6709 grandChild \u7684\u53f3\u65cb\u64cd\u4f5c

\u201c\u5411\u53f3\u65cb\u8f6c\u201d\u662f\u4e00\u79cd\u5f62\u8c61\u5316\u7684\u8bf4\u6cd5\uff0c\u5b9e\u9645\u4e0a\u9700\u8981\u901a\u8fc7\u4fee\u6539\u8282\u70b9\u6307\u9488\u6765\u5b9e\u73b0\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
avl_tree.rs
/* \u53f3\u65cb\u64cd\u4f5c */\nfn right_rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\nmatch node {\nSome(node) => {\nlet child = node.borrow().left.clone().unwrap();\nlet grand_child = child.borrow().right.clone();\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.borrow_mut().right = Some(node.clone());\nnode.borrow_mut().left = grand_child;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nSelf::update_height(Some(node));\nSelf::update_height(Some(child.clone()));\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nSome(child)\n}\nNone => None,\n}\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.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
"},{"location":"chapter_tree/avl_tree/#2_1","title":"2. \u00a0 \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\u56fe 7-28 \u6240\u793a\u7684\u201c\u5de6\u65cb\u201d\u64cd\u4f5c\u3002

\u56fe 7-28 \u00a0 \u5de6\u65cb\u64cd\u4f5c

\u540c\u7406\uff0c\u5982\u56fe 7-29 \u6240\u793a\uff0c\u5f53\u8282\u70b9 child \u6709\u5de6\u5b50\u8282\u70b9\uff08\u8bb0\u4e3a grandChild \uff09\u65f6\uff0c\u9700\u8981\u5728\u5de6\u65cb\u4e2d\u6dfb\u52a0\u4e00\u6b65\uff1a\u5c06 grandChild \u4f5c\u4e3a node \u7684\u53f3\u5b50\u8282\u70b9\u3002

\u56fe 7-29 \u00a0 \u6709 grandChild \u7684\u5de6\u65cb\u64cd\u4f5c

\u53ef\u4ee5\u89c2\u5bdf\u5230\uff0c\u53f3\u65cb\u548c\u5de6\u65cb\u64cd\u4f5c\u5728\u903b\u8f91\u4e0a\u662f\u955c\u50cf\u5bf9\u79f0\u7684\uff0c\u5b83\u4eec\u5206\u522b\u89e3\u51b3\u7684\u4e24\u79cd\u5931\u8861\u60c5\u51b5\u4e5f\u662f\u5bf9\u79f0\u7684\u3002\u57fa\u4e8e\u5bf9\u79f0\u6027\uff0c\u6211\u4eec\u53ea\u9700\u5c06\u53f3\u65cb\u7684\u5b9e\u73b0\u4ee3\u7801\u4e2d\u7684\u6240\u6709\u7684 left \u66ff\u6362\u4e3a right \uff0c\u5c06\u6240\u6709\u7684 right \u66ff\u6362\u4e3a left \uff0c\u5373\u53ef\u5f97\u5230\u5de6\u65cb\u7684\u5b9e\u73b0\u4ee3\u7801\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.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
avl_tree.rs
/* \u5de6\u65cb\u64cd\u4f5c */\nfn left_rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\nmatch node {\nSome(node) => {\nlet child = node.borrow().right.clone().unwrap();\nlet grand_child = child.borrow().left.clone();\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.borrow_mut().left = Some(node.clone());\nnode.borrow_mut().right = grand_child;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nSelf::update_height(Some(node));\nSelf::update_height(Some(child.clone()));\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nSome(child)\n}\nNone => None,\n}\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.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
"},{"location":"chapter_tree/avl_tree/#3","title":"3. \u00a0 \u5148\u5de6\u65cb\u540e\u53f3\u65cb","text":"

\u5bf9\u4e8e\u56fe 7-30 \u4e2d\u7684\u5931\u8861\u8282\u70b9 3 \uff0c\u4ec5\u4f7f\u7528\u5de6\u65cb\u6216\u53f3\u65cb\u90fd\u65e0\u6cd5\u4f7f\u5b50\u6811\u6062\u590d\u5e73\u8861\u3002\u6b64\u65f6\u9700\u8981\u5148\u5bf9 child \u6267\u884c\u201c\u5de6\u65cb\u201d\uff0c\u518d\u5bf9 node \u6267\u884c\u201c\u53f3\u65cb\u201d\u3002

\u56fe 7-30 \u00a0 \u5148\u5de6\u65cb\u540e\u53f3\u65cb

"},{"location":"chapter_tree/avl_tree/#4","title":"4. \u00a0 \u5148\u53f3\u65cb\u540e\u5de6\u65cb","text":"

\u5982\u56fe 7-31 \u6240\u793a\uff0c\u5bf9\u4e8e\u4e0a\u8ff0\u5931\u8861\u4e8c\u53c9\u6811\u7684\u955c\u50cf\u60c5\u51b5\uff0c\u9700\u8981\u5148\u5bf9 child \u6267\u884c\u201c\u53f3\u65cb\u201d\uff0c\u7136\u540e\u5bf9 node \u6267\u884c\u201c\u5de6\u65cb\u201d\u3002

\u56fe 7-31 \u00a0 \u5148\u53f3\u65cb\u540e\u5de6\u65cb

"},{"location":"chapter_tree/avl_tree/#5","title":"5. \u00a0 \u65cb\u8f6c\u7684\u9009\u62e9","text":"

\u56fe 7-32 \u5c55\u793a\u7684\u56db\u79cd\u5931\u8861\u60c5\u51b5\u4e0e\u4e0a\u8ff0\u6848\u4f8b\u9010\u4e2a\u5bf9\u5e94\uff0c\u5206\u522b\u9700\u8981\u91c7\u7528\u53f3\u65cb\u3001\u5de6\u65cb\u3001\u5148\u53f3\u540e\u5de6\u3001\u5148\u5de6\u540e\u53f3\u7684\u65cb\u8f6c\u64cd\u4f5c\u3002

\u56fe 7-32 \u00a0 AVL \u6811\u7684\u56db\u79cd\u65cb\u8f6c\u60c5\u51b5

\u5982\u4e0b\u8868\u6240\u793a\uff0c\u6211\u4eec\u901a\u8fc7\u5224\u65ad\u5931\u8861\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u4ee5\u53ca\u8f83\u9ad8\u4e00\u4fa7\u5b50\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u7684\u6b63\u8d1f\u53f7\uff0c\u6765\u786e\u5b9a\u5931\u8861\u8282\u70b9\u5c5e\u4e8e\u56fe 7-32 \u4e2d\u7684\u54ea\u79cd\u60c5\u51b5\u3002

\u8868 7-3 \u00a0 \u56db\u79cd\u65cb\u8f6c\u60c5\u51b5\u7684\u9009\u62e9\u6761\u4ef6

\u5931\u8861\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50 \u5b50\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50 \u5e94\u91c7\u7528\u7684\u65cb\u8f6c\u65b9\u6cd5 \\(> 1\\) \uff08\u5373\u5de6\u504f\u6811\uff09 \\(\\geq 0\\) \u53f3\u65cb \\(> 1\\) \uff08\u5373\u5de6\u504f\u6811\uff09 \\(<0\\) \u5148\u5de6\u65cb\u540e\u53f3\u65cb \\(< -1\\) \uff08\u5373\u53f3\u504f\u6811\uff09 \\(\\leq 0\\) \u5de6\u65cb \\(< -1\\) \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

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node\n
avl_tree.cpp
/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode *rotate(TreeNode *node) {\n// \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\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\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\n
avl_tree.java
/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode rotate(TreeNode node) {\n// \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\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\u987b\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\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\n
avl_tree.go
/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfunc (t *aVLTree) rotate(node *TreeNode) *TreeNode {\n// \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n// Go \u63a8\u8350\u77ed\u53d8\u91cf\uff0c\u8fd9\u91cc bf \u6307\u4ee3 t.balanceFactor\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\u987b\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\u987b\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\u987b\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\u987b\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\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\n
avl_tree.rs
/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfn rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n// \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\nlet balance_factor = Self::balance_factor(node.clone());\n// \u5de6\u504f\u6811\nif balance_factor > 1 {\nlet node = node.unwrap();\nif Self::balance_factor(node.borrow().left.clone()) >= 0 {\n// \u53f3\u65cb\nSelf::right_rotate(Some(node))\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nlet left = node.borrow().left.clone();\nnode.borrow_mut().left = Self::left_rotate(left);\nSelf::right_rotate(Some(node))\n}\n}\n// \u53f3\u504f\u6811\nelse if balance_factor < -1 {\nlet node = node.unwrap();\nif Self::balance_factor(node.borrow().right.clone()) <= 0 {\n// \u5de6\u65cb\nSelf::left_rotate(Some(node))\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nlet right = node.borrow().right.clone();\nnode.borrow_mut().right = Self::right_rotate(right);\nSelf::left_rotate(Some(node))\n}\n} else {\n// \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nnode\n}\n}\n
avl_tree.c
/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode *rotate(TreeNode *node) {\n// \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\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\u987b\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\u987b\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/#1_2","title":"1. \u00a0 \u63d2\u5165\u8282\u70b9","text":"

AVL \u6811\u7684\u8282\u70b9\u63d2\u5165\u64cd\u4f5c\u4e0e\u4e8c\u53c9\u641c\u7d22\u6811\u5728\u4e3b\u4f53\u4e0a\u7c7b\u4f3c\u3002\u552f\u4e00\u7684\u533a\u522b\u5728\u4e8e\uff0c\u5728 AVL \u6811\u4e2d\u63d2\u5165\u8282\u70b9\u540e\uff0c\u4ece\u8be5\u8282\u70b9\u5230\u6839\u8282\u70b9\u7684\u8def\u5f84\u4e0a\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e00\u7cfb\u5217\u5931\u8861\u8282\u70b9\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u4ece\u8fd9\u4e2a\u8282\u70b9\u5f00\u59cb\uff0c\u81ea\u5e95\u5411\u4e0a\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u6240\u6709\u5931\u8861\u8282\u70b9\u6062\u590d\u5e73\u8861\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig avl_tree.py
def insert(self, val):\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.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.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.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.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\u51fd\u6570\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.(int) {\nnode.Left = t.insertHelper(node.Left, val)\n} else if val > node.Val.(int) {\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.swift
/* \u63d2\u5165\u8282\u70b9 */\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.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.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
avl_tree.rs
/* \u63d2\u5165\u8282\u70b9 */\nfn insert(&mut self, val: i32) {\nself.root = Self::insert_helper(self.root.clone(), val);\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfn insert_helper(node: OptionTreeNodeRc, val: i32) -> OptionTreeNodeRc {\nmatch node {\nSome(mut node) => {\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nmatch {\nlet node_val = node.borrow().val;\nnode_val\n}\n.cmp(&val)\n{\nOrdering::Greater => {\nlet left = node.borrow().left.clone();\nnode.borrow_mut().left = Self::insert_helper(left, val);\n}\nOrdering::Less => {\nlet right = node.borrow().right.clone();\nnode.borrow_mut().right = Self::insert_helper(right, val);\n}\nOrdering::Equal => {\nreturn Some(node); // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n}\n}\nSelf::update_height(Some(node.clone())); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = Self::rotate(Some(node)).unwrap();\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nSome(node)\n}\nNone => Some(TreeNode::new(val)),\n}\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\u51fd\u6570\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.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
"},{"location":"chapter_tree/avl_tree/#2_2","title":"2. \u00a0 \u5220\u9664\u8282\u70b9","text":"

\u7c7b\u4f3c\u5730\uff0c\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u5220\u9664\u8282\u70b9\u65b9\u6cd5\u7684\u57fa\u7840\u4e0a\uff0c\u9700\u8981\u4ece\u5e95\u81f3\u9876\u5730\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u6240\u6709\u5931\u8861\u8282\u70b9\u6062\u590d\u5e73\u8861\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig avl_tree.py
def remove(self, val: int):\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.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.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.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 ?? 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.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\u51fd\u6570\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.(int) {\nnode.Left = t.removeHelper(node.Left, val)\n} else if val > node.Val.(int) {\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.(int))\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.swift
/* \u5220\u9664\u8282\u70b9 */\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.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.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
avl_tree.rs
/* \u5220\u9664\u8282\u70b9 */\nfn remove(&self, val: i32) {\nSelf::remove_helper(self.root.clone(), val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfn remove_helper(node: OptionTreeNodeRc, val: i32) -> OptionTreeNodeRc {\nmatch node {\nSome(mut node) => {\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif val < node.borrow().val {\nlet left = node.borrow().left.clone();\nnode.borrow_mut().left = Self::remove_helper(left, val);\n} else if val > node.borrow().val {\nlet right = node.borrow().right.clone();\nnode.borrow_mut().right = Self::remove_helper(right, val);\n} else if node.borrow().left.is_none() || node.borrow().right.is_none() {\nlet child = if node.borrow().left.is_some() {\nnode.borrow().left.clone()\n} else {\nnode.borrow().right.clone()\n};\nmatch child {\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nNone => {\nreturn None;\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nSome(child) => node = child,\n}\n} else {\n// \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\nlet mut temp = node.borrow().right.clone().unwrap();\nloop {\nlet temp_left = temp.borrow().left.clone();\nif temp_left.is_none() {\nbreak;\n}\ntemp = temp_left.unwrap();\n}\nlet right = node.borrow().right.clone();\nnode.borrow_mut().right = Self::remove_helper(right, temp.borrow().val);\nnode.borrow_mut().val = temp.borrow().val;\n}\nSelf::update_height(Some(node.clone())); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = Self::rotate(Some(node)).unwrap();\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nSome(node)\n}\nNone => None,\n}\n}\n
avl_tree.c
/* \u5220\u9664\u8282\u70b9 */\n// \u7531\u4e8e\u5f15\u5165\u4e86 stdio.h \uff0c\u6b64\u5904\u65e0\u6cd5\u4f7f\u7528 remove \u5173\u952e\u8bcd\nvoid removeItem(aVLTree *tree, int val) {\nTreeNode *root = removeHelper(tree->root, val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\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.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
"},{"location":"chapter_tree/avl_tree/#3_1","title":"3. \u00a0 \u67e5\u627e\u8282\u70b9","text":"

AVL \u6811\u7684\u8282\u70b9\u67e5\u627e\u64cd\u4f5c\u4e0e\u4e8c\u53c9\u641c\u7d22\u6811\u4e00\u81f4\uff0c\u5728\u6b64\u4e0d\u518d\u8d58\u8ff0\u3002

"},{"location":"chapter_tree/avl_tree/#754-avl","title":"7.5.4 \u00a0 AVL \u6811\u5178\u578b\u5e94\u7528","text":"
  • \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\u3002
  • \u7528\u4e8e\u6784\u5efa\u6570\u636e\u5e93\u4e2d\u7684\u7d22\u5f15\u7cfb\u7edf\u3002
  • \u7ea2\u9ed1\u6811\u5728\u8bb8\u591a\u5e94\u7528\u4e2d\u6bd4 AVL \u6811\u66f4\u53d7\u6b22\u8fce\u3002\u8fd9\u662f\u56e0\u4e3a\u7ea2\u9ed1\u6811\u7684\u5e73\u8861\u6761\u4ef6\u76f8\u5bf9\u5bbd\u677e\uff0c\u5728\u7ea2\u9ed1\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\u6240\u9700\u7684\u65cb\u8f6c\u64cd\u4f5c\u76f8\u5bf9\u8f83\u5c11\uff0c\u5176\u8282\u70b9\u589e\u5220\u64cd\u4f5c\u7684\u5e73\u5747\u6548\u7387\u66f4\u9ad8\u3002
"},{"location":"chapter_tree/binary_search_tree/","title":"7.4 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811","text":"

\u5982\u56fe 7-16 \u6240\u793a\uff0c\u300c\u4e8c\u53c9\u641c\u7d22\u6811 binary search tree\u300d\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\u3002

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

\u56fe 7-16 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811

"},{"location":"chapter_tree/binary_search_tree/#741","title":"7.4.1 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u64cd\u4f5c","text":"

\u6211\u4eec\u5c06\u4e8c\u53c9\u641c\u7d22\u6811\u5c01\u88c5\u4e3a\u4e00\u4e2a\u7c7b ArrayBinaryTree \uff0c\u5e76\u58f0\u660e\u4e00\u4e2a\u6210\u5458\u53d8\u91cf root \uff0c\u6307\u5411\u6811\u7684\u6839\u8282\u70b9\u3002

"},{"location":"chapter_tree/binary_search_tree/#1","title":"1. \u00a0 \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\u5982\u56fe 7-17 \u6240\u793a\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u8282\u70b9 cur \uff0c\u4ece\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9 root \u51fa\u53d1\uff0c\u5faa\u73af\u6bd4\u8f83\u8282\u70b9\u503c cur.val \u548c num \u4e4b\u95f4\u7684\u5927\u5c0f\u5173\u7cfb\u3002

  • \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 \u3002
  • \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 \u3002
  • \u82e5 cur.val = num \uff0c\u8bf4\u660e\u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\u5e76\u8fd4\u56de\u8be5\u8282\u70b9\u3002
<1><2><3><4>

\u56fe 7-17 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u67e5\u627e\u8282\u70b9\u793a\u4f8b

\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u67e5\u627e\u64cd\u4f5c\u4e0e\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u4e00\u81f4\uff0c\u90fd\u662f\u6bcf\u8f6e\u6392\u9664\u4e00\u534a\u60c5\u51b5\u3002\u5faa\u73af\u6b21\u6570\u6700\u591a\u4e3a\u4e8c\u53c9\u6811\u7684\u9ad8\u5ea6\uff0c\u5f53\u4e8c\u53c9\u6811\u5e73\u8861\u65f6\uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_tree.py
def search(self, num: int) -> TreeNode | None:\n\"\"\"\u67e5\u627e\u8282\u70b9\"\"\"\ncur = 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.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.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.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.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.(int) < num {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nnode = node.Right\n} else if node.Val.(int) > 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.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.js
/* \u67e5\u627e\u8282\u70b9 */\nsearch(num) {\nlet cur = this.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 */\nsearch(num: number): TreeNode | null {\nlet cur = this.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.dart
/* \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.rs
/* \u67e5\u627e\u8282\u70b9 */\npub fn search(&self, num: i32) -> Option<TreeNodeRc> {\nlet mut cur = self.root.clone();\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile let Some(node) = cur.clone() {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif node.borrow().val < num {\ncur = node.borrow().right.clone();\n}\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse if node.borrow().val > num {\ncur = node.borrow().left.clone();\n}\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse {\nbreak;\n}\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\ncur\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.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
"},{"location":"chapter_tree/binary_search_tree/#2","title":"2. \u00a0 \u63d2\u5165\u8282\u70b9","text":"

\u7ed9\u5b9a\u4e00\u4e2a\u5f85\u63d2\u5165\u5143\u7d20 num \uff0c\u4e3a\u4e86\u4fdd\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u201c\u5de6\u5b50\u6811 < \u6839\u8282\u70b9 < \u53f3\u5b50\u6811\u201d\u7684\u6027\u8d28\uff0c\u63d2\u5165\u64cd\u4f5c\u6d41\u7a0b\u5982\u56fe 7-18 \u6240\u793a\u3002

  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{None}\\) \uff09\u65f6\u8df3\u51fa\u5faa\u73af\u3002
  2. \u5728\u8be5\u4f4d\u7f6e\u63d2\u5165\u8282\u70b9\uff1a\u521d\u59cb\u5316\u8282\u70b9 num \uff0c\u5c06\u8be5\u8282\u70b9\u7f6e\u4e8e \\(\\text{None}\\) \u7684\u4f4d\u7f6e\u3002

\u56fe 7-18 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u63d2\u5165\u8282\u70b9

\u5728\u4ee3\u7801\u5b9e\u73b0\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u4e24\u70b9\u3002

  • \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
  • \u4e3a\u4e86\u5b9e\u73b0\u63d2\u5165\u8282\u70b9\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u8282\u70b9 pre \u4fdd\u5b58\u4e0a\u4e00\u8f6e\u5faa\u73af\u7684\u8282\u70b9\u3002\u8fd9\u6837\u5728\u904d\u5386\u81f3 \\(\\text{None}\\) \u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u5230\u5176\u7236\u8282\u70b9\uff0c\u4ece\u800c\u5b8c\u6210\u8282\u70b9\u63d2\u5165\u64cd\u4f5c\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_tree.py
def insert(self, num: int):\n\"\"\"\u63d2\u5165\u8282\u70b9\"\"\"\n# \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\nif self._root is None:\nself._root = TreeNode(num)\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.cpp
/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\nif (root == nullptr) {\nroot = new TreeNode(num);\nreturn;\n}\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.java
/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\nif (root == null) {\nroot = new TreeNode(num);\nreturn;\n}\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.cs
/* \u63d2\u5165\u8282\u70b9 */\nvoid Insert(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\nif (root == null) {\nroot = new TreeNode(num);\nreturn;\n}\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(num);\nif (pre != null) {\nif (pre.val < num)\npre.right = node;\nelse\npre.left = node;\n}\n}\n
binary_search_tree.go
/* \u63d2\u5165\u8282\u70b9 */\nfunc (bst *binarySearchTree) insert(num int) {\ncur := bst.root\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\nif cur == nil {\nbst.root = NewTreeNode(num)\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.(int) < num {\ncur = cur.Right\n} else {\ncur = cur.Left\n}\n}\n// \u63d2\u5165\u8282\u70b9\nnode := NewTreeNode(num)\nif pre.Val.(int) < num {\npre.Right = node\n} else {\npre.Left = node\n}\n}\n
binary_search_tree.swift
/* \u63d2\u5165\u8282\u70b9 */\nfunc insert(num: Int) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\nif root == nil {\nroot = TreeNode(x: num)\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.js
/* \u63d2\u5165\u8282\u70b9 */\ninsert(num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\nif (this.root === null) {\nthis.root = new TreeNode(num);\nreturn;\n}\nlet cur = this.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\nconst 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 */\ninsert(num: number): void {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\nif (this.root === null) {\nthis.root = new TreeNode(num);\nreturn;\n}\nlet cur: TreeNode | null = this.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\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\nconst node = new TreeNode(num);\nif (pre!.val < num) pre!.right = node;\nelse pre!.left = node;\n}\n
binary_search_tree.dart
/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\nif (_root == null) {\n_root = TreeNode(num);\nreturn;\n}\nTreeNode? cur = _root;\nTreeNode? 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) 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\nelse\ncur = cur.left;\n}\n// \u63d2\u5165\u8282\u70b9\nTreeNode? node = TreeNode(num);\nif (pre!.val < num)\npre.right = node;\nelse\npre.left = node;\n}\n
binary_search_tree.rs
/* \u63d2\u5165\u8282\u70b9 */\npub fn insert(&mut self, num: i32) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\nif self.root.is_none() {\nself.root = Some(TreeNode::new(num));\nreturn;\n}\nlet mut cur = self.root.clone();\nlet mut pre = None;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile let Some(node) = cur.clone() {\n// \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif node.borrow().val == num {\nreturn;\n}\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\npre = cur.clone();\nif node.borrow().val < num {\ncur = node.borrow().right.clone();\n}\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse {\ncur = node.borrow().left.clone();\n}\n}\n// \u63d2\u5165\u8282\u70b9\nlet node = TreeNode::new(num);\nlet pre = pre.unwrap();\nif pre.borrow().val < num {\npre.borrow_mut().right = Some(Rc::clone(&node));\n} else {\npre.borrow_mut().left = Some(Rc::clone(&node));\n}\n}\n
binary_search_tree.c
/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(binarySearchTree *bst, int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\nif (bst->root == NULL) {\nbst->root = newTreeNode(num);\nreturn;\n}\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.zig
// \u63d2\u5165\u8282\u70b9\nfn insert(self: *Self, num: T) !void {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\nif (self.root == null) {\nself.root = try self.mem_allocator.create(inc.TreeNode(T));\nreturn;\n}\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

\u4e0e\u67e5\u627e\u8282\u70b9\u76f8\u540c\uff0c\u63d2\u5165\u8282\u70b9\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\u3002

"},{"location":"chapter_tree/binary_search_tree/#3","title":"3. \u00a0 \u5220\u9664\u8282\u70b9","text":"

\u5148\u5728\u4e8c\u53c9\u6811\u4e2d\u67e5\u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u518d\u5c06\u5176\u4ece\u4e8c\u53c9\u6811\u4e2d\u5220\u9664\u3002

\u4e0e\u63d2\u5165\u8282\u70b9\u7c7b\u4f3c\uff0c\u6211\u4eec\u9700\u8981\u4fdd\u8bc1\u5728\u5220\u9664\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u201c\u5de6\u5b50\u6811 < \u6839\u8282\u70b9 < \u53f3\u5b50\u6811\u201d\u7684\u6027\u8d28\u4ecd\u7136\u6ee1\u8db3\u3002

\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u76ee\u6807\u8282\u70b9\u7684\u5b50\u8282\u70b9\u6570\u91cf\uff0c\u5171\u5206\u4e3a 0\u30011 \u548c 2 \u8fd9\u4e09\u79cd\u60c5\u51b5\uff0c\u6267\u884c\u5bf9\u5e94\u7684\u5220\u9664\u8282\u70b9\u64cd\u4f5c\u3002

\u5982\u56fe 7-19 \u6240\u793a\uff0c\u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5ea6\u4e3a \\(0\\) \u65f6\uff0c\u8868\u793a\u8be5\u8282\u70b9\u662f\u53f6\u8282\u70b9\uff0c\u53ef\u4ee5\u76f4\u63a5\u5220\u9664\u3002

\u56fe 7-19 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 0 \uff09

\u5982\u56fe 7-20 \u6240\u793a\uff0c\u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5ea6\u4e3a \\(1\\) \u65f6\uff0c\u5c06\u5f85\u5220\u9664\u8282\u70b9\u66ff\u6362\u4e3a\u5176\u5b50\u8282\u70b9\u5373\u53ef\u3002

\u56fe 7-20 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 1 \uff09

\u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5ea6\u4e3a \\(2\\) \u65f6\uff0c\u6211\u4eec\u65e0\u6cd5\u76f4\u63a5\u5220\u9664\u5b83\uff0c\u800c\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u8282\u70b9\u66ff\u6362\u8be5\u8282\u70b9\u3002\u7531\u4e8e\u8981\u4fdd\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u201c\u5de6 \\(<\\) \u6839 \\(<\\) \u53f3\u201d\u7684\u6027\u8d28\uff0c\u56e0\u6b64\u8fd9\u4e2a\u8282\u70b9\u53ef\u4ee5\u662f\u53f3\u5b50\u6811\u7684\u6700\u5c0f\u8282\u70b9\u6216\u5de6\u5b50\u6811\u7684\u6700\u5927\u8282\u70b9\u3002

\u5047\u8bbe\u6211\u4eec\u9009\u62e9\u53f3\u5b50\u6811\u7684\u6700\u5c0f\u8282\u70b9\uff08\u5373\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\uff09\uff0c\u5219\u5220\u9664\u64cd\u4f5c\u6d41\u7a0b\u5982\u56fe 7-21 \u6240\u793a\u3002

  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 \u3002
  2. \u5c06 tmp \u7684\u503c\u8986\u76d6\u5f85\u5220\u9664\u8282\u70b9\u7684\u503c\uff0c\u5e76\u5728\u6811\u4e2d\u9012\u5f52\u5220\u9664\u8282\u70b9 tmp \u3002
<1><2><3><4>

\u56fe 7-21 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 2 \uff09

\u5220\u9664\u8282\u70b9\u64cd\u4f5c\u540c\u6837\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\uff0c\u5176\u4e2d\u67e5\u627e\u5f85\u5220\u9664\u8282\u70b9\u9700\u8981 \\(O(\\log n)\\) \u65f6\u95f4\uff0c\u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u540e\u7ee7\u8282\u70b9\u9700\u8981 \\(O(\\log n)\\) \u65f6\u95f4\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_tree.py
def remove(self, num: int):\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 = child\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.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.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.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)\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 ?? 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.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.(int) < 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.(int))\n// \u7528 tmp \u8986\u76d6 cur\ncur.Val = tmp.Val\n}\n}\n
binary_search_tree.swift
/* \u5220\u9664\u8282\u70b9 */\nfunc remove(num: Int) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\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 = 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\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.js
/* \u5220\u9664\u8282\u70b9 */\nremove(num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (this.root === null) return;\nlet cur = this.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\nconst child = cur.left !== null ? cur.left : cur.right;\n// \u5220\u9664\u8282\u70b9 cur\nif (cur !== this.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\nthis.root = 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\nthis.remove(tmp.val);\n// \u7528 tmp \u8986\u76d6 cur\ncur.val = tmp.val;\n}\n}\n
binary_search_tree.ts
/* \u5220\u9664\u8282\u70b9 */\nremove(num: number): void {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (this.root === null) return;\nlet cur: TreeNode | null = this.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) 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\nconst child: TreeNode | null =\ncur.left !== null ? cur.left : cur.right;\n// \u5220\u9664\u8282\u70b9 cur\nif (cur !== this.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\nthis.root = 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: TreeNode | null = cur.right;\nwhile (tmp!.left !== null) {\ntmp = tmp!.left;\n}\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nthis.remove(tmp!.val);\n// \u7528 tmp \u8986\u76d6 cur\ncur.val = tmp!.val;\n}\n}\n
binary_search_tree.dart
/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (_root == null) return;\nTreeNode? cur = _root;\nTreeNode? 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) 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\nelse\ncur = cur.left;\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\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\nTreeNode? child = 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\n_root = child;\n}\n} else {\n// \u5b50\u8282\u70b9\u6570\u91cf = 2\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\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.rs
/* \u5220\u9664\u8282\u70b9 */\npub fn remove(&mut self, num: i32) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif self.root.is_none() { return; }\nlet mut cur = self.root.clone();\nlet mut pre = None;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile let Some(node) = cur.clone() {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif node.borrow().val == num {\nbreak;\n}\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\npre = cur.clone();\nif node.borrow().val < num {\ncur = node.borrow().right.clone();\n}\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse {\ncur = node.borrow().left.clone();\n}\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif cur.is_none() {\nreturn;\n}\nlet cur = cur.unwrap();\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif cur.borrow().left.is_none() || cur.borrow().right.is_none() {\n// \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u8282\u70b9\nlet child = cur.borrow().left.clone().or_else(|| cur.borrow().right.clone());\nlet pre = pre.unwrap();\nlet left = pre.borrow().left.clone().unwrap();\n// \u5220\u9664\u8282\u70b9 cur\nif !Rc::ptr_eq(&cur, self.root.as_ref().unwrap()) {\nif Rc::ptr_eq(&left, &cur) {\npre.borrow_mut().left = child;\n} else {\npre.borrow_mut().right = child;\n}\n} else {\n// \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nself.root = 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 mut tmp = cur.borrow().right.clone();\nwhile let Some(node) = tmp.clone() {\nif node.borrow().left.is_some() {\ntmp = node.borrow().left.clone();\n} else {\nbreak;\n}\n}\nlet tmpval = tmp.unwrap().borrow().val;\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nself.remove(tmpval);\n// \u7528 tmp \u8986\u76d6 cur\ncur.borrow_mut().val = tmpval;\n}\n}\n
binary_search_tree.c
/* \u5220\u9664\u8282\u70b9 */\n// \u7531\u4e8e\u5f15\u5165\u4e86 stdio.h \uff0c\u6b64\u5904\u65e0\u6cd5\u4f7f\u7528 remove \u5173\u952e\u8bcd\nvoid removeItem(binarySearchTree *bst, int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\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\nremoveItem(bst, tmp->val);\n// \u7528 tmp \u8986\u76d6 cur\ncur->val = tmpVal;\n}\n}\n
binary_search_tree.zig
// \u5220\u9664\u8282\u70b9\nfn remove(self: *Self, num: T) void {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\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 tmp_val = tmp.?.val;\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nself.remove(tmp.?.val);\n// \u7528 tmp \u8986\u76d6 cur\ncur.?.val = tmp_val;\n}\n}\n
"},{"location":"chapter_tree/binary_search_tree/#4","title":"4. \u00a0 \u4e2d\u5e8f\u904d\u5386\u6709\u5e8f","text":"

\u5982\u56fe 7-22 \u6240\u793a\uff0c\u4e8c\u53c9\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u9075\u5faa\u201c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u201d\u7684\u904d\u5386\u987a\u5e8f\uff0c\u800c\u4e8c\u53c9\u641c\u7d22\u6811\u6ee1\u8db3\u201c\u5de6\u5b50\u8282\u70b9 \\(<\\) \u6839\u8282\u70b9 \\(<\\) \u53f3\u5b50\u8282\u70b9\u201d\u7684\u5927\u5c0f\u5173\u7cfb\u3002

\u8fd9\u610f\u5473\u7740\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u8fdb\u884c\u4e2d\u5e8f\u904d\u5386\u65f6\uff0c\u603b\u662f\u4f1a\u4f18\u5148\u904d\u5386\u4e0b\u4e00\u4e2a\u6700\u5c0f\u8282\u70b9\uff0c\u4ece\u800c\u5f97\u51fa\u4e00\u4e2a\u91cd\u8981\u6027\u8d28\uff1a\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u662f\u5347\u5e8f\u7684\u3002

\u5229\u7528\u4e2d\u5e8f\u904d\u5386\u5347\u5e8f\u7684\u6027\u8d28\uff0c\u6211\u4eec\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u83b7\u53d6\u6709\u5e8f\u6570\u636e\u4ec5\u9700 \\(O(n)\\) \u65f6\u95f4\uff0c\u65e0\u987b\u8fdb\u884c\u989d\u5916\u7684\u6392\u5e8f\u64cd\u4f5c\uff0c\u975e\u5e38\u9ad8\u6548\u3002

\u56fe 7-22 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217

"},{"location":"chapter_tree/binary_search_tree/#742","title":"7.4.2 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6548\u7387","text":"

\u7ed9\u5b9a\u4e00\u7ec4\u6570\u636e\uff0c\u6211\u4eec\u8003\u8651\u4f7f\u7528\u6570\u7ec4\u6216\u4e8c\u53c9\u641c\u7d22\u6811\u5b58\u50a8\u3002\u89c2\u5bdf\u8868 7-2 \uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u5404\u9879\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662f\u5bf9\u6570\u9636\uff0c\u5177\u6709\u7a33\u5b9a\u4e14\u9ad8\u6548\u7684\u6027\u80fd\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

\u8868 7-2 \u00a0 \u6570\u7ec4\u4e0e\u641c\u7d22\u6811\u7684\u6548\u7387\u5bf9\u6bd4

\u65e0\u5e8f\u6570\u7ec4 \u4e8c\u53c9\u641c\u7d22\u6811 \u67e5\u627e\u5143\u7d20 \\(O(n)\\) \\(O(\\log n)\\) \u63d2\u5165\u5143\u7d20 \\(O(1)\\) \\(O(\\log n)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(\\log n)\\)

\u5728\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u662f\u201c\u5e73\u8861\u201d\u7684\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728 \\(\\log n\\) \u8f6e\u5faa\u73af\u5185\u67e5\u627e\u4efb\u610f\u8282\u70b9\u3002

\u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u4e0d\u65ad\u5730\u63d2\u5165\u548c\u5220\u9664\u8282\u70b9\uff0c\u53ef\u80fd\u5bfc\u81f4\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u56fe 7-23 \u6240\u793a\u7684\u94fe\u8868\uff0c\u8fd9\u65f6\u5404\u79cd\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u4f1a\u9000\u5316\u4e3a \\(O(n)\\) \u3002

\u56fe 7-23 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\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\uff1a\u503c\u3001\u5de6\u5b50\u8282\u70b9\u5f15\u7528\u3001\u53f3\u5b50\u8282\u70b9\u5f15\u7528\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig
class TreeNode:\n\"\"\"\u4e8c\u53c9\u6811\u8282\u70b9\u7c7b\"\"\"\ndef __init__(self, val: int):\nself.val: int = val                # \u8282\u70b9\u503c\nself.left: TreeNode | None = None  # \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nself.right: TreeNode | None = None # \u53f3\u5b50\u8282\u70b9\u5f15\u7528\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
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nint val;         // \u8282\u70b9\u503c\nTreeNode left;   // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nTreeNode right;  // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\nTreeNode(int x) { val = x; }\n}\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nint val;          // \u8282\u70b9\u503c\nTreeNode? left;   // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nTreeNode? right;  // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\nTreeNode(int x) { val = x; }\n}\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype TreeNode struct {\nVal   int\nLeft  *TreeNode\nRight *TreeNode\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc NewTreeNode(v int) *TreeNode {\nreturn &TreeNode{\nLeft:  nil, // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nRight: nil, // \u53f3\u5b50\u8282\u70b9\u6307\u9488\nVal:   v,   // \u8282\u70b9\u503c\n}\n}\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nvar val: Int // \u8282\u70b9\u503c\nvar left: TreeNode? // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nvar right: TreeNode? // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\ninit(x: Int) {\nval = x\n}\n}\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nval; // \u8282\u70b9\u503c\nleft; // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nright; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\nconstructor(val, left, right) {\nthis.val = val === undefined ? 0 : val;\nthis.left = left === undefined ? null : left;\nthis.right = right === undefined ? null : right;\n}\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\u5f15\u7528\nthis.right = right === undefined ? null : right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\n}\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nint val;         // \u8282\u70b9\u503c\nTreeNode? left;  // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nTreeNode? right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\nTreeNode(this.val, [this.left, this.right]);\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n/* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\nval: i32,                               // \u8282\u70b9\u503c\nleft: Option<Rc<RefCell<TreeNode>>>,    // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nright: Option<Rc<RefCell<TreeNode>>>,   // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\nimpl TreeNode {\n/* \u6784\u9020\u65b9\u6cd5 */\nfn new(val: i32) -> Rc<RefCell<Self>> {\nRc::new(RefCell::new(Self {\nval,\nleft: None,\nright: None\n}))\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
\n

\u6bcf\u4e2a\u8282\u70b9\u90fd\u6709\u4e24\u4e2a\u5f15\u7528\uff08\u6307\u9488\uff09\uff0c\u5206\u522b\u6307\u5411\u300c\u5de6\u5b50\u8282\u70b9 left-child node\u300d\u548c\u300c\u53f3\u5b50\u8282\u70b9 right-child node\u300d\uff0c\u8be5\u8282\u70b9\u88ab\u79f0\u4e3a\u8fd9\u4e24\u4e2a\u5b50\u8282\u70b9\u7684\u300c\u7236\u8282\u70b9 parent node\u300d\u3002\u5f53\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u65f6\uff0c\u6211\u4eec\u5c06\u8be5\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u53ca\u5176\u4ee5\u4e0b\u8282\u70b9\u5f62\u6210\u7684\u6811\u79f0\u4e3a\u8be5\u8282\u70b9\u7684\u300c\u5de6\u5b50\u6811 left subtree\u300d\uff0c\u540c\u7406\u53ef\u5f97\u300c\u53f3\u5b50\u6811 right subtree\u300d\u3002

\u5728\u4e8c\u53c9\u6811\u4e2d\uff0c\u9664\u53f6\u8282\u70b9\u5916\uff0c\u5176\u4ed6\u6240\u6709\u8282\u70b9\u90fd\u5305\u542b\u5b50\u8282\u70b9\u548c\u975e\u7a7a\u5b50\u6811\u3002\u5982\u56fe 7-1 \u6240\u793a\uff0c\u5982\u679c\u5c06\u201c\u8282\u70b9 2\u201d\u89c6\u4e3a\u7236\u8282\u70b9\uff0c\u5219\u5176\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\u5206\u522b\u662f\u201c\u8282\u70b9 4\u201d\u548c\u201c\u8282\u70b9 5\u201d\uff0c\u5de6\u5b50\u6811\u662f\u201c\u8282\u70b9 4 \u53ca\u5176\u4ee5\u4e0b\u8282\u70b9\u5f62\u6210\u7684\u6811\u201d\uff0c\u53f3\u5b50\u6811\u662f\u201c\u8282\u70b9 5 \u53ca\u5176\u4ee5\u4e0b\u8282\u70b9\u5f62\u6210\u7684\u6811\u201d\u3002

\u56fe 7-1 \u00a0 \u7236\u8282\u70b9\u3001\u5b50\u8282\u70b9\u3001\u5b50\u6811

"},{"location":"chapter_tree/binary_tree/#711","title":"7.1.1 \u00a0 \u4e8c\u53c9\u6811\u5e38\u89c1\u672f\u8bed","text":"

\u4e8c\u53c9\u6811\u7684\u5e38\u7528\u672f\u8bed\u5982\u56fe 7-2 \u6240\u793a\u3002

  • \u300c\u6839\u8282\u70b9 root node\u300d\uff1a\u4f4d\u4e8e\u4e8c\u53c9\u6811\u9876\u5c42\u7684\u8282\u70b9\uff0c\u6ca1\u6709\u7236\u8282\u70b9\u3002
  • \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{None}\\) \u3002
  • \u300c\u8fb9 edge\u300d\uff1a\u8fde\u63a5\u4e24\u4e2a\u8282\u70b9\u7684\u7ebf\u6bb5\uff0c\u5373\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\u3002
  • \u8282\u70b9\u6240\u5728\u7684\u300c\u5c42 level\u300d\uff1a\u4ece\u9876\u81f3\u5e95\u9012\u589e\uff0c\u6839\u8282\u70b9\u6240\u5728\u5c42\u4e3a 1 \u3002
  • \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\u53d6\u503c\u8303\u56f4\u662f 0\u30011\u30012 \u3002
  • \u4e8c\u53c9\u6811\u7684\u300c\u9ad8\u5ea6 height\u300d\uff1a\u4ece\u6839\u8282\u70b9\u5230\u6700\u8fdc\u53f6\u8282\u70b9\u6240\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\u3002
  • \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\u3002
  • \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\u3002

\u56fe 7-2 \u00a0 \u4e8c\u53c9\u6811\u7684\u5e38\u7528\u672f\u8bed

Tip

\u8bf7\u6ce8\u610f\uff0c\u6211\u4eec\u901a\u5e38\u5c06\u201c\u9ad8\u5ea6\u201d\u548c\u201c\u6df1\u5ea6\u201d\u5b9a\u4e49\u4e3a\u201c\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":""},{"location":"chapter_tree/binary_tree/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u4e8c\u53c9\u6811","text":"

\u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u9996\u5148\u521d\u59cb\u5316\u8282\u70b9\uff0c\u7136\u540e\u6784\u5efa\u5f15\u7528\uff08\u6307\u9488\uff09\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_tree.py
# \u521d\u59cb\u5316\u4e8c\u53c9\u6811\n# \u521d\u59cb\u5316\u8282\u70b9\nn1 = TreeNode(val=1)\nn2 = TreeNode(val=2)\nn3 = TreeNode(val=3)\nn4 = TreeNode(val=4)\nn5 = TreeNode(val=5)\n# \u6784\u5efa\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.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.cs
/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode n1 = new(1);\nTreeNode n2 = new(2);\nTreeNode n3 = new(3);\nTreeNode n4 = new(4);\nTreeNode n5 = new(5);\n// \u6784\u5efa\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.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.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.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
binary_tree.rs
// \u521d\u59cb\u5316\u8282\u70b9\nlet n1 = TreeNode::new(1);\nlet n2 = TreeNode::new(2);\nlet n3 = TreeNode::new(3);\nlet n4 = TreeNode::new(4);\nlet n5 = TreeNode::new(5);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1.borrow_mut().left = Some(n2.clone());\nn1.borrow_mut().right = Some(n3);\nn2.borrow_mut().left = Some(n4);\nn2.borrow_mut().right = Some(n5);\n
binary_tree.c
/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode *n1 = newTreeNode(1);\nTreeNode *n2 = newTreeNode(2);\nTreeNode *n3 = newTreeNode(3);\nTreeNode *n4 = newTreeNode(4);\nTreeNode *n5 = newTreeNode(5);\n// \u6784\u5efa\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
"},{"location":"chapter_tree/binary_tree/#2","title":"2. \u00a0 \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9","text":"

\u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u5728\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u6307\u9488\u6765\u5b9e\u73b0\u3002\u56fe 7-3 \u7ed9\u51fa\u4e86\u4e00\u4e2a\u793a\u4f8b\u3002

\u56fe 7-3 \u00a0 \u5728\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9

PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_tree.py
# \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\np = TreeNode(0)\n# \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = p\np.left = n2\n# \u5220\u9664\u8282\u70b9 P\nn1.left = n2\n
binary_tree.cpp
/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode* P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1->left = P;\nP->left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1->left = n2;\n
binary_tree.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.cs
/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode P = new(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
binary_tree.go
/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\np := NewTreeNode(0)\nn1.Left = p\np.Left = n2\n// \u5220\u9664\u8282\u70b9 P\nn1.Left = n2\n
binary_tree.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.js
/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nlet P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
binary_tree.ts
/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nconst P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
binary_tree.dart
/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
binary_tree.rs
let p = TreeNode::new(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.borrow_mut().left = Some(p.clone());\np.borrow_mut().left = Some(n2.clone());\n// \u5220\u9664\u8282\u70b9 p\nn1.borrow_mut().left = Some(n2);\n
binary_tree.c
/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode *P = newTreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1->left = P;\nP->left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1->left = n2;\n
binary_tree.zig
\n

Note

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u63d2\u5165\u8282\u70b9\u53ef\u80fd\u4f1a\u6539\u53d8\u4e8c\u53c9\u6811\u7684\u539f\u6709\u903b\u8f91\u7ed3\u6784\uff0c\u800c\u5220\u9664\u8282\u70b9\u901a\u5e38\u610f\u5473\u7740\u5220\u9664\u8be5\u8282\u70b9\u53ca\u5176\u6240\u6709\u5b50\u6811\u3002\u56e0\u6b64\uff0c\u5728\u4e8c\u53c9\u6811\u4e2d\uff0c\u63d2\u5165\u4e0e\u5220\u9664\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_1","title":"1. \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811","text":"

\u300c\u5b8c\u7f8e\u4e8c\u53c9\u6811 perfect binary tree\u300d\u6240\u6709\u5c42\u7684\u8282\u70b9\u90fd\u88ab\u5b8c\u5168\u586b\u6ee1\u3002\u5728\u5b8c\u7f8e\u4e8c\u53c9\u6811\u4e2d\uff0c\u53f6\u8282\u70b9\u7684\u5ea6\u4e3a \\(0\\) \uff0c\u5176\u4f59\u6240\u6709\u8282\u70b9\u7684\u5ea6\u90fd\u4e3a \\(2\\) \uff1b\u82e5\u6811\u9ad8\u5ea6\u4e3a \\(h\\) \uff0c\u5219\u8282\u70b9\u603b\u6570\u4e3a \\(2^{h+1} - 1\\) \uff0c\u5448\u73b0\u6807\u51c6\u7684\u6307\u6570\u7ea7\u5173\u7cfb\uff0c\u53cd\u6620\u4e86\u81ea\u7136\u754c\u4e2d\u5e38\u89c1\u7684\u7ec6\u80de\u5206\u88c2\u73b0\u8c61\u3002

Tip

\u8bf7\u6ce8\u610f\uff0c\u5728\u4e2d\u6587\u793e\u533a\u4e2d\uff0c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u5e38\u88ab\u79f0\u4e3a\u300c\u6ee1\u4e8c\u53c9\u6811\u300d\u3002

\u56fe 7-4 \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811

"},{"location":"chapter_tree/binary_tree/#2_1","title":"2. \u00a0 \u5b8c\u5168\u4e8c\u53c9\u6811","text":"

\u5982\u56fe 7-5 \u6240\u793a\uff0c\u300c\u5b8c\u5168\u4e8c\u53c9\u6811 complete binary tree\u300d\u53ea\u6709\u6700\u5e95\u5c42\u7684\u8282\u70b9\u672a\u88ab\u586b\u6ee1\uff0c\u4e14\u6700\u5e95\u5c42\u8282\u70b9\u5c3d\u91cf\u9760\u5de6\u586b\u5145\u3002

\u56fe 7-5 \u00a0 \u5b8c\u5168\u4e8c\u53c9\u6811

"},{"location":"chapter_tree/binary_tree/#3","title":"3. \u00a0 \u5b8c\u6ee1\u4e8c\u53c9\u6811","text":"

\u5982\u56fe 7-6 \u6240\u793a\uff0c\u300c\u5b8c\u6ee1\u4e8c\u53c9\u6811 full binary tree\u300d\u9664\u4e86\u53f6\u8282\u70b9\u4e4b\u5916\uff0c\u5176\u4f59\u6240\u6709\u8282\u70b9\u90fd\u6709\u4e24\u4e2a\u5b50\u8282\u70b9\u3002

\u56fe 7-6 \u00a0 \u5b8c\u6ee1\u4e8c\u53c9\u6811

"},{"location":"chapter_tree/binary_tree/#4","title":"4. \u00a0 \u5e73\u8861\u4e8c\u53c9\u6811","text":"

\u5982\u56fe 7-7 \u6240\u793a\uff0c\u300c\u5e73\u8861\u4e8c\u53c9\u6811 balanced binary tree\u300d\u4e2d\u4efb\u610f\u8282\u70b9\u7684\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u7684\u9ad8\u5ea6\u4e4b\u5dee\u7684\u7edd\u5bf9\u503c\u4e0d\u8d85\u8fc7 1 \u3002

\u56fe 7-7 \u00a0 \u5e73\u8861\u4e8c\u53c9\u6811

"},{"location":"chapter_tree/binary_tree/#714","title":"7.1.4 \u00a0 \u4e8c\u53c9\u6811\u7684\u9000\u5316","text":"

\u56fe 7-8 \u5c55\u793a\u4e86\u4e8c\u53c9\u6811\u7684\u7406\u60f3\u4e0e\u9000\u5316\u72b6\u6001\u3002\u5f53\u4e8c\u53c9\u6811\u7684\u6bcf\u5c42\u8282\u70b9\u90fd\u88ab\u586b\u6ee1\u65f6\uff0c\u8fbe\u5230\u201c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u201d\uff1b\u800c\u5f53\u6240\u6709\u8282\u70b9\u90fd\u504f\u5411\u4e00\u4fa7\u65f6\uff0c\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u201c\u94fe\u8868\u201d\u3002

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

\u56fe 7-8 \u00a0 \u4e8c\u53c9\u6811\u7684\u6700\u4f73\u4e0e\u6700\u5dee\u7ed3\u6784

\u5982\u8868 7-1 \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

\u8868 7-1 \u00a0 \u4e8c\u53c9\u6811\u7684\u6700\u4f73\u4e0e\u6700\u5dee\u60c5\u51b5

\u5b8c\u7f8e\u4e8c\u53c9\u6811 \u94fe\u8868 \u7b2c \\(i\\) \u5c42\u7684\u8282\u70b9\u6570\u91cf \\(2^{i-1}\\) \\(1\\) \u9ad8\u5ea6 \\(h\\) \u6811\u7684\u53f6\u8282\u70b9\u6570\u91cf \\(2^h\\) \\(1\\) \u9ad8\u5ea6 \\(h\\) \u6811\u7684\u8282\u70b9\u603b\u6570 \\(2^{h+1} - 1\\) \\(h + 1\\) \u8282\u70b9\u603b\u6570 \\(n\\) \u6811\u7684\u9ad8\u5ea6 \\(\\log_2 (n+1) - 1\\) \\(n - 1\\)"},{"location":"chapter_tree/binary_tree_traversal/","title":"7.2 \u00a0 \u4e8c\u53c9\u6811\u904d\u5386","text":"

\u4ece\u7269\u7406\u7ed3\u6784\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u6811\u662f\u4e00\u79cd\u57fa\u4e8e\u94fe\u8868\u7684\u6570\u636e\u7ed3\u6784\uff0c\u56e0\u6b64\u5176\u904d\u5386\u65b9\u5f0f\u662f\u901a\u8fc7\u6307\u9488\u9010\u4e2a\u8bbf\u95ee\u8282\u70b9\u3002\u7136\u800c\uff0c\u6811\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u8fd9\u4f7f\u5f97\u904d\u5386\u6811\u6bd4\u904d\u5386\u94fe\u8868\u66f4\u52a0\u590d\u6742\uff0c\u9700\u8981\u501f\u52a9\u641c\u7d22\u7b97\u6cd5\u6765\u5b9e\u73b0\u3002

\u4e8c\u53c9\u6811\u5e38\u89c1\u7684\u904d\u5386\u65b9\u5f0f\u5305\u62ec\u5c42\u5e8f\u904d\u5386\u3001\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u548c\u540e\u5e8f\u904d\u5386\u7b49\u3002

"},{"location":"chapter_tree/binary_tree_traversal/#721","title":"7.2.1 \u00a0 \u5c42\u5e8f\u904d\u5386","text":"

\u5982\u56fe 7-9 \u6240\u793a\uff0c\u300c\u5c42\u5e8f\u904d\u5386 level-order traversal\u300d\u4ece\u9876\u90e8\u5230\u5e95\u90e8\u9010\u5c42\u904d\u5386\u4e8c\u53c9\u6811\uff0c\u5e76\u5728\u6bcf\u4e00\u5c42\u6309\u7167\u4ece\u5de6\u5230\u53f3\u7684\u987a\u5e8f\u8bbf\u95ee\u8282\u70b9\u3002

\u5c42\u5e8f\u904d\u5386\u672c\u8d28\u4e0a\u5c5e\u4e8e\u300c\u5e7f\u5ea6\u4f18\u5148\u904d\u5386 breadth-first traversal\u300d\uff0c\u5b83\u4f53\u73b0\u4e86\u4e00\u79cd\u201c\u4e00\u5708\u4e00\u5708\u5411\u5916\u6269\u5c55\u201d\u7684\u9010\u5c42\u904d\u5386\u65b9\u5f0f\u3002

\u56fe 7-9 \u00a0 \u4e8c\u53c9\u6811\u7684\u5c42\u5e8f\u904d\u5386

"},{"location":"chapter_tree/binary_tree_traversal/#1","title":"1. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u901a\u5e38\u501f\u52a9\u201c\u961f\u5217\u201d\u6765\u5b9e\u73b0\u3002\u961f\u5217\u9075\u5faa\u201c\u5148\u8fdb\u5148\u51fa\u201d\u7684\u89c4\u5219\uff0c\u800c\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u5219\u9075\u5faa\u201c\u9010\u5c42\u63a8\u8fdb\u201d\u7684\u89c4\u5219\uff0c\u4e24\u8005\u80cc\u540e\u7684\u601d\u60f3\u662f\u4e00\u81f4\u7684\u3002

PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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<>();\nqueue.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.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.go
/* \u5c42\u5e8f\u904d\u5386 */\nfunc levelOrder(root *TreeNode) []any {\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([]any, 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.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.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.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
binary_tree_bfs.rs
/* \u5c42\u5e8f\u904d\u5386 */\nfn level_order(root: &Rc<RefCell<TreeNode>>) -> Vec<i32> {\n// \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u7ed3\u70b9\nlet mut que = VecDeque::new();\nque.push_back(Rc::clone(&root));\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nlet mut vec = Vec::new();\nwhile let Some(node) = que.pop_front() {                 // \u961f\u5217\u51fa\u961f\nvec.push(node.borrow().val);                         // \u4fdd\u5b58\u7ed3\u70b9\u503c\nif let Some(left) = node.borrow().left.as_ref() {\nque.push_back(Rc::clone(left));                  // \u5de6\u5b50\u7ed3\u70b9\u5165\u961f\n}\nif let Some(right) = node.borrow().right.as_ref() {\nque.push_back(Rc::clone(right));                 // \u53f3\u5b50\u7ed3\u70b9\u5165\u961f\n};\n}\nvec\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.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
"},{"location":"chapter_tree/binary_tree_traversal/#2","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"
  • \u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \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 \\(O(n)\\) \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 \\((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

\u56fe 7-10 \u5c55\u793a\u4e86\u5bf9\u4e8c\u53c9\u6811\u8fdb\u884c\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u5de5\u4f5c\u539f\u7406\u3002\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u5c31\u50cf\u662f\u7ed5\u7740\u6574\u4e2a\u4e8c\u53c9\u6811\u7684\u5916\u56f4\u201c\u8d70\u201d\u4e00\u5708\uff0c\u5728\u6bcf\u4e2a\u8282\u70b9\u90fd\u4f1a\u9047\u5230\u4e09\u4e2a\u4f4d\u7f6e\uff0c\u5206\u522b\u5bf9\u5e94\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u548c\u540e\u5e8f\u904d\u5386\u3002

\u56fe 7-10 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u524d\u3001\u4e2d\u3001\u540e\u5e8f\u904d\u5386

"},{"location":"chapter_tree/binary_tree_traversal/#1_1","title":"1. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"

\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u5b9e\u73b0\uff1a

PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_tree_dfs.py
def pre_order(root: TreeNode | 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):\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):\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.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.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.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.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.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.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.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
binary_tree_dfs.rs
/* \u524d\u5e8f\u904d\u5386 */\nfn pre_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\nlet mut result = vec![];\nif let Some(node) = root {\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u7ed3\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nresult.push(node.borrow().val);\nresult.append(&mut pre_order(node.borrow().left.as_ref()));\nresult.append(&mut pre_order(node.borrow().right.as_ref()));\n}\nresult\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfn in_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\nlet mut result = vec![];\nif let Some(node) = root {\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u7ed3\u70b9 -> \u53f3\u5b50\u6811\nresult.append(&mut in_order(node.borrow().left.as_ref()));\nresult.push(node.borrow().val);\nresult.append(&mut in_order(node.borrow().right.as_ref()));\n}\nresult\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfn post_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\nlet mut result = vec![];\nif let Some(node) = root {\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u7ed3\u70b9\nresult.append(&mut post_order(node.borrow().left.as_ref()));\nresult.append(&mut post_order(node.borrow().right.as_ref()));\nresult.push(node.borrow().val);\n}\nresult\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.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

Note

\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u4e5f\u53ef\u4ee5\u57fa\u4e8e\u8fed\u4ee3\u5b9e\u73b0\uff0c\u6709\u5174\u8da3\u7684\u540c\u5b66\u53ef\u4ee5\u81ea\u884c\u7814\u7a76\u3002

\u56fe 7-11 \u5c55\u793a\u4e86\u524d\u5e8f\u904d\u5386\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b\uff0c\u5176\u53ef\u5206\u4e3a\u201c\u9012\u201d\u548c\u201c\u5f52\u201d\u4e24\u4e2a\u9006\u5411\u7684\u90e8\u5206\u3002

  1. \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\u3002
  2. \u201c\u5f52\u201d\u8868\u793a\u51fd\u6570\u8fd4\u56de\uff0c\u4ee3\u8868\u5f53\u524d\u8282\u70b9\u5df2\u7ecf\u8bbf\u95ee\u5b8c\u6bd5\u3002
<1><2><3><4><5><6><7><8><9><10><11>

\u56fe 7-11 \u00a0 \u524d\u5e8f\u904d\u5386\u7684\u9012\u5f52\u8fc7\u7a0b

"},{"location":"chapter_tree/binary_tree_traversal/#2_1","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"
  • \u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \uff1a\u6240\u6709\u8282\u70b9\u88ab\u8bbf\u95ee\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002
  • \u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \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":""},{"location":"chapter_tree/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","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\u5b8c\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
"},{"location":"chapter_tree/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

\u5bf9\u4e8e\u53ea\u6709\u4e00\u4e2a\u8282\u70b9\u7684\u4e8c\u53c9\u6811\uff0c\u6811\u7684\u9ad8\u5ea6\u548c\u6839\u8282\u70b9\u7684\u6df1\u5ea6\u90fd\u662f \\(0\\) \u5417\uff1f

\u662f\u7684\uff0c\u56e0\u4e3a\u9ad8\u5ea6\u548c\u6df1\u5ea6\u901a\u5e38\u5b9a\u4e49\u4e3a\u201c\u8d70\u8fc7\u8fb9\u7684\u6570\u91cf\u201d\u3002

\u4e8c\u53c9\u6811\u4e2d\u7684\u63d2\u5165\u4e0e\u5220\u9664\u4e00\u822c\u90fd\u662f\u7531\u4e00\u5957\u64cd\u4f5c\u914d\u5408\u5b8c\u6210\u7684\uff0c\u8fd9\u91cc\u7684\u201c\u4e00\u5957\u64cd\u4f5c\u201d\u6307\u4ec0\u4e48\u5462\uff1f\u53ef\u4ee5\u7406\u89e3\u4e3a\u8d44\u6e90\u7684\u5b50\u8282\u70b9\u7684\u8d44\u6e90\u91ca\u653e\u5417\uff1f

\u62ff\u4e8c\u53c9\u641c\u7d22\u6811\u6765\u4e3e\u4f8b\uff0c\u5220\u9664\u8282\u70b9\u64cd\u4f5c\u8981\u5206\u4e3a\u4e09\u79cd\u60c5\u51b5\u5904\u7406\uff0c\u5176\u4e2d\u6bcf\u79cd\u60c5\u51b5\u90fd\u9700\u8981\u8fdb\u884c\u591a\u4e2a\u6b65\u9aa4\u7684\u8282\u70b9\u64cd\u4f5c\u3002

\u4e3a\u4ec0\u4e48 DFS \u904d\u5386\u4e8c\u53c9\u6811\u6709\u524d\u3001\u4e2d\u3001\u540e\u4e09\u79cd\u987a\u5e8f\uff0c\u5206\u522b\u6709\u4ec0\u4e48\u7528\u5462\uff1f

DFS \u7684\u524d\u3001\u4e2d\u3001\u540e\u5e8f\u904d\u5386\u548c\u8bbf\u95ee\u6570\u7ec4\u7684\u987a\u5e8f\u7c7b\u4f3c\uff0c\u662f\u904d\u5386\u4e8c\u53c9\u6811\u7684\u57fa\u672c\u65b9\u6cd5\uff0c\u5229\u7528\u8fd9\u4e09\u79cd\u904d\u5386\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u7279\u5b9a\u987a\u5e8f\u7684\u904d\u5386\u7ed3\u679c\u3002\u4f8b\u5982\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\uff0c\u7531\u4e8e\u7ed3\u70b9\u5927\u5c0f\u6ee1\u8db3 \u5de6\u5b50\u7ed3\u70b9\u503c < \u6839\u7ed3\u70b9\u503c < \u53f3\u5b50\u7ed3\u70b9\u503c \uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u8981\u6309\u7167 \u5de6->\u6839->\u53f3 \u7684\u4f18\u5148\u7ea7\u904d\u5386\u6811\uff0c\u5c31\u53ef\u4ee5\u83b7\u5f97\u6709\u5e8f\u7684\u8282\u70b9\u5e8f\u5217\u3002

\u53f3\u65cb\u64cd\u4f5c\u662f\u5904\u7406\u5931\u8861\u8282\u70b9 node\u3001child\u3001grand_child \u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u90a3 node \u7684\u7236\u8282\u70b9\u548c node \u539f\u6765\u7684\u8fde\u63a5\u4e0d\u9700\u8981\u7ef4\u62a4\u5417\uff1f\u53f3\u65cb\u64cd\u4f5c\u540e\u5c82\u4e0d\u662f\u65ad\u6389\u4e86\uff1f

\u6211\u4eec\u9700\u8981\u4ece\u9012\u5f52\u7684\u89c6\u89d2\u6765\u770b\u8fd9\u4e2a\u95ee\u9898\u3002\u53f3\u65cb\u64cd\u4f5c right_rotate(root) \u4f20\u5165\u7684\u662f\u5b50\u6811\u7684\u6839\u8282\u70b9\uff0c\u6700\u7ec8 return child \u8fd4\u56de\u65cb\u8f6c\u4e4b\u540e\u7684\u5b50\u6811\u7684\u6839\u8282\u70b9\u3002\u5b50\u6811\u7684\u6839\u8282\u70b9\u548c\u5176\u7236\u8282\u70b9\u7684\u8fde\u63a5\u662f\u5728\u8be5\u51fd\u6570\u8fd4\u56de\u540e\u5b8c\u6210\u7684\uff0c\u4e0d\u5c5e\u4e8e\u53f3\u65cb\u64cd\u4f5c\u7684\u7ef4\u62a4\u8303\u56f4\u3002

\u5728 C++ \u4e2d\uff0c\u51fd\u6570\u88ab\u5212\u5206\u5230 private \u548c public \u4e2d\uff0c\u8fd9\u65b9\u9762\u6709\u4ec0\u4e48\u8003\u91cf\u5417\uff1f\u4e3a\u4ec0\u4e48\u8981\u5c06 height() \u51fd\u6570\u548c updateHeight() \u51fd\u6570\u5206\u522b\u653e\u5728 public \u548c private \u4e2d\u5462\uff1f

\u4e3b\u8981\u770b\u65b9\u6cd5\u7684\u4f7f\u7528\u8303\u56f4\uff0c\u5982\u679c\u65b9\u6cd5\u53ea\u5728\u7c7b\u5185\u90e8\u4f7f\u7528\uff0c\u90a3\u4e48\u5c31\u8bbe\u8ba1\u4e3a private \u3002\u4f8b\u5982\uff0c\u7528\u6237\u5355\u72ec\u8c03\u7528 updateHeight() \u662f\u6ca1\u6709\u610f\u4e49\u7684\uff0c\u5b83\u53ea\u662f\u63d2\u5165\u3001\u5220\u9664\u64cd\u4f5c\u4e2d\u7684\u4e00\u6b65\u3002\u800c height() \u662f\u8bbf\u95ee\u7ed3\u70b9\u9ad8\u5ea6\uff0c\u7c7b\u4f3c\u4e8e vector.size() \uff0c\u56e0\u6b64\u8bbe\u7f6e\u6210 public \u4ee5\u4fbf\u4f7f\u7528\u3002

\u8bf7\u95ee\u5982\u4f55\u4ece\u4e00\u7ec4\u8f93\u5165\u6570\u636e\u6784\u5efa\u4e00\u4e2a\u4e8c\u53c9\u641c\u7d22\u6811\uff1f\u6839\u8282\u70b9\u7684\u9009\u62e9\u662f\u4e0d\u662f\u5f88\u91cd\u8981\uff1f

\u662f\u7684\uff0c\u6784\u5efa\u6811\u7684\u65b9\u6cd5\u5df2\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4ee3\u7801\u4e2d\u7684 build_tree() \u65b9\u6cd5\u4e2d\u7ed9\u51fa\u3002\u81f3\u4e8e\u6839\u8282\u70b9\u7684\u9009\u62e9\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u8f93\u5165\u6570\u636e\u6392\u5e8f\uff0c\u7136\u540e\u7528\u4e2d\u70b9\u5143\u7d20\u4f5c\u4e3a\u6839\u8282\u70b9\uff0c\u518d\u9012\u5f52\u5730\u6784\u5efa\u5de6\u53f3\u5b50\u6811\u3002\u8fd9\u6837\u505a\u53ef\u4ee5\u6700\u5927\u7a0b\u5ea6\u4fdd\u8bc1\u6811\u7684\u5e73\u8861\u6027\u3002

\u5728 Java \u4e2d\uff0c\u5b57\u7b26\u4e32\u5bf9\u6bd4\u662f\u5426\u4e00\u5b9a\u8981\u7528 equals() \u65b9\u6cd5\uff1f

\u5728 Java \u4e2d\uff0c\u5bf9\u4e8e\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c== \u7528\u4e8e\u5bf9\u6bd4\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\u662f\u5426\u76f8\u7b49\u3002\u5bf9\u4e8e\u5f15\u7528\u7c7b\u578b\uff0c\u4e24\u79cd\u7b26\u53f7\u7684\u5de5\u4f5c\u539f\u7406\u662f\u4e0d\u540c\u7684\u3002

  • == \uff1a\u7528\u6765\u6bd4\u8f83\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5373\u5b83\u4eec\u5728\u5185\u5b58\u4e2d\u7684\u4f4d\u7f6e\u662f\u5426\u76f8\u540c\u3002
  • equals()\uff1a\u7528\u6765\u5bf9\u6bd4\u4e24\u4e2a\u5bf9\u8c61\u7684\u503c\u662f\u5426\u76f8\u7b49\u3002

\u56e0\u6b64\u5982\u679c\u8981\u5bf9\u6bd4\u503c\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u7528 equals() \u3002\u7136\u800c\uff0c\u901a\u8fc7 String a = \"hi\"; String b = \"hi\"; \u521d\u59cb\u5316\u7684\u5b57\u7b26\u4e32\u90fd\u5b58\u50a8\u5728\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u4e2d\uff0c\u5b83\u4eec\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u56e0\u6b64\u4e5f\u53ef\u4ee5\u7528 a == b \u6765\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u3002

\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u5230\u6700\u5e95\u5c42\u4e4b\u524d\uff0c\u961f\u5217\u4e2d\u7684\u8282\u70b9\u6570\u91cf\u662f \\(2^h\\) \u5417\uff1f

\u662f\u7684\uff0c\u4f8b\u5982\u9ad8\u5ea6 \\(h = 2\\) \u7684\u6ee1\u4e8c\u53c9\u6811\uff0c\u5176\u8282\u70b9\u603b\u6570 \\(n = 7\\) \uff0c\u5219\u5e95\u5c42\u8282\u70b9\u6570\u91cf \\(4 = 2^h = (n + 1) / 2\\) \u3002

"}]} \ No newline at end of file diff --git a/sitemap.xml b/sitemap.xml index 8666a5ac4..42410f942 100644 --- a/sitemap.xml +++ b/sitemap.xml @@ -2,512 +2,512 @@ https://www.hello-algo.com/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_appendix/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_appendix/contribution/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_appendix/installation/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_array_and_linkedlist/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_array_and_linkedlist/array/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_array_and_linkedlist/linked_list/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_array_and_linkedlist/list/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_array_and_linkedlist/summary/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_backtracking/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_backtracking/backtracking_algorithm/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_backtracking/n_queens_problem/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_backtracking/permutations_problem/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_backtracking/subset_sum_problem/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_backtracking/summary/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_computational_complexity/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_computational_complexity/iteration_and_recursion/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_computational_complexity/performance_evaluation/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_computational_complexity/space_complexity/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_computational_complexity/summary/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_computational_complexity/time_complexity/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_data_structure/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_data_structure/basic_data_types/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_data_structure/character_encoding/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_data_structure/classification_of_data_structure/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_data_structure/number_encoding/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_data_structure/summary/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_divide_and_conquer/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_divide_and_conquer/binary_search_recur/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_divide_and_conquer/build_binary_tree_problem/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_divide_and_conquer/divide_and_conquer/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_divide_and_conquer/hanota_problem/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_divide_and_conquer/summary/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_dynamic_programming/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_dynamic_programming/dp_problem_features/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_dynamic_programming/dp_solution_pipeline/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_dynamic_programming/edit_distance_problem/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_dynamic_programming/intro_to_dynamic_programming/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_dynamic_programming/knapsack_problem/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_dynamic_programming/summary/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_dynamic_programming/unbounded_knapsack_problem/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_graph/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_graph/graph/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_graph/graph_operations/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_graph/graph_traversal/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_graph/summary/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_greedy/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_greedy/fractional_knapsack_problem/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_greedy/greedy_algorithm/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_greedy/max_capacity_problem/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_greedy/max_product_cutting_problem/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_greedy/summary/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_hashing/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_hashing/hash_algorithm/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_hashing/hash_collision/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_hashing/hash_map/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_hashing/summary/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_heap/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_heap/build_heap/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_heap/heap/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_heap/summary/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_heap/top_k/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_introduction/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_introduction/algorithms_are_everywhere/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_introduction/summary/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_introduction/what_is_dsa/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_preface/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_preface/about_the_book/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_preface/suggestions/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_preface/summary/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_reference/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_searching/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_searching/binary_search/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_searching/binary_search_edge/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_searching/binary_search_insertion/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_searching/replace_linear_by_hashing/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_searching/searching_algorithm_revisited/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_searching/summary/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_sorting/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_sorting/bubble_sort/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_sorting/bucket_sort/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_sorting/counting_sort/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_sorting/heap_sort/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_sorting/insertion_sort/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_sorting/merge_sort/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_sorting/quick_sort/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_sorting/radix_sort/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_sorting/selection_sort/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_sorting/sorting_algorithm/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_sorting/summary/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_stack_and_queue/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_stack_and_queue/deque/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_stack_and_queue/queue/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_stack_and_queue/stack/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_stack_and_queue/summary/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_tree/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_tree/array_representation_of_tree/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_tree/avl_tree/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_tree/binary_search_tree/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_tree/binary_tree/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_tree/binary_tree_traversal/ - 2023-10-16 + 2023-10-17 daily https://www.hello-algo.com/chapter_tree/summary/ - 2023-10-16 + 2023-10-17 daily \ No newline at end of file diff --git a/sitemap.xml.gz b/sitemap.xml.gz index 3ae617bdf56a6e7cd87ad771cd5380cbb86038f7..c4666d7a2a3efc8cc571aa3ee6ac589dfad874bb 100644 GIT binary patch literal 981 zcmV;`11kI}8Ap_+SZ4$-jo~rlHaH=VdfcCk|0N`?h*rzpS3v zN~g5#-&X(p{nzvN)z96RZ4M@KdMb@=d|M4E`9C+C^ZC4vI`K$8u3hLiQ4XaLH%sN_ zW&LBd`|?yi^#SwU^w}CGcl9Z8dCb7ZxMwoGTT3@?wwv1EV|$8ebZYnYJqvbXO|%DrE-gUnPie9;LMx&-B6 zs-7HsV40_=A5)lOxO*2yvXTX#$mI6p#ebD0dMZ+&s~5CECq2T9fTBbqrjTXIfss%< zidxQ<>FPNmv6t#Hb=4o<_eY@U3r5s?E$0cq?SZw(=CK1E1_U3e^hqn8Vg%`I3G4$J zZl`VOWD%Ky9Ogg=PK2zD5{n=yFhd0Rp9q4lP31paF=EjEhwwO8OJfNPN*DKm#<6*) z{t2)0?;0ZZOx4*(L>ZzpQ>s9C?b&F{a6+)ssTe{`)6x*aYCn-e+Z3Eg?zgawgC!qH z9$-fU)rn@?xoj0H0W}1L-wyzUP@KZ~W&1C}Ag4g}XaX4kVgw)2mxY9aK-|~8mU)*B z%ymup^93Z8w=j=%5t2MfovuiIUAYUc$}$O=Q>r;sAbbSBF;E;@4?N!)>lMWC9+0Io zMmL|2dqA#7{19LvOpIIa%nCSfOJcBMP{QbDa$X#o#+>#{Y8cSE6MX=^O;u4)sAm~C zT+ahHo|1bI0YQf!fX@RZp`%cdnuhj)_aqsDkvf-?NCS(`xdQ}p%ze+X{VkC@Z~Jyu zP>b)fjVtY(8g6|_bEkaBXd^NURy{+4Ya2N*J}6MGRnd z^$d9Xfb)CfWeFNMvQ4*_O+e^R288r+ivo%n4i@AT+JybTd9vveIqiM{5rI?#K}Y}q Dhb+;J literal 981 zcmV;`11kIW-Vm`|8r?{Wo=<_E_iKh0NtC-j^ij0$M5qLt=v25o|Q&3t90hJ zPq2GNWvJLF7ziKvc>4j{ophw$!(LYWCx_S&8Uif@ohDvck`YxOSo6L^+f~+$@!w zm-Ua;?#om8)CbIW(`Rd(+|{SV#03gxTJUpSy23!*j+ zIt?QZzZJ*KY@_hOb%ue+J%=%;X|4gJkGU&RnJ9*d$lks;DffQS4l+~8@I@z3=n|BN zsd{qkfn}beeoSGC;qF}+$x0S{B9q&X7ynh3=&4A7u3pdvo%9GZ0*Vrem_n8*2S!5e zC~7%ZrmN?O#9pe))K!0Y-yeaZFBnnpwVWpaw+GfDo5v1x7!Z7<(kHEWiV>u%L&D$68fPO0Wlf$$Oh#z1jsJ@9;ItXB}jdq9@X z7~On6?g6T8gtq+sbN6tPV@ovHdRGIp`K;n za6J#)cuMX;1Oy#^06q_tgpNW*Y8u)H-jieqM(SKnA`L7$=ME6aG50;k_P0dtyzSdr zK`p+^HmgYmhUMm*HE_a>bsR+U=5A|F=3a@n8PWz@X#C1uELwlP;aF9n1 zJ&?s6vz7-O+(1fSaBw3VmRS0`TtPAcH)@2yA=PATJ!5#xoEeH$@ezy|uaBoP7BPU; z)idDj1J3V_mnCT6$Tr